Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-27 09:37:30

0001 /**
0002  * \file des.h
0003  *
0004  * \brief DES block cipher
0005  *
0006  * \warning   DES/3DES are considered weak ciphers and their use constitutes a
0007  *            security risk. We recommend considering stronger ciphers
0008  *            instead.
0009  */
0010 /*
0011  *  Copyright The Mbed TLS Contributors
0012  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
0013  *
0014  */
0015 #ifndef MBEDTLS_DES_H
0016 #define MBEDTLS_DES_H
0017 #include "mbedtls/private_access.h"
0018 
0019 #include "mbedtls/build_info.h"
0020 #include "mbedtls/platform_util.h"
0021 
0022 #include <stddef.h>
0023 #include <stdint.h>
0024 
0025 #define MBEDTLS_DES_ENCRYPT     1
0026 #define MBEDTLS_DES_DECRYPT     0
0027 
0028 /** The data input has an invalid length. */
0029 #define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH              -0x0032
0030 
0031 #define MBEDTLS_DES_KEY_SIZE    8
0032 
0033 #ifdef __cplusplus
0034 extern "C" {
0035 #endif
0036 
0037 #if !defined(MBEDTLS_DES_ALT)
0038 // Regular implementation
0039 //
0040 
0041 /**
0042  * \brief          DES context structure
0043  *
0044  * \warning        DES/3DES are considered weak ciphers and their use constitutes a
0045  *                 security risk. We recommend considering stronger ciphers
0046  *                 instead.
0047  */
0048 typedef struct mbedtls_des_context {
0049     uint32_t MBEDTLS_PRIVATE(sk)[32];            /*!<  DES subkeys       */
0050 }
0051 mbedtls_des_context;
0052 
0053 /**
0054  * \brief          Triple-DES context structure
0055  *
0056  * \warning        DES/3DES are considered weak ciphers and their use constitutes a
0057  *                 security risk. We recommend considering stronger ciphers
0058  *                 instead.
0059  */
0060 typedef struct mbedtls_des3_context {
0061     uint32_t MBEDTLS_PRIVATE(sk)[96];            /*!<  3DES subkeys      */
0062 }
0063 mbedtls_des3_context;
0064 
0065 #else  /* MBEDTLS_DES_ALT */
0066 #include "des_alt.h"
0067 #endif /* MBEDTLS_DES_ALT */
0068 
0069 /**
0070  * \brief          Initialize DES context
0071  *
0072  * \param ctx      DES context to be initialized
0073  *
0074  * \warning        DES/3DES are considered weak ciphers and their use constitutes a
0075  *                 security risk. We recommend considering stronger ciphers
0076  *                 instead.
0077  */
0078 void mbedtls_des_init(mbedtls_des_context *ctx);
0079 
0080 /**
0081  * \brief          Clear DES context
0082  *
0083  * \param ctx      DES context to be cleared
0084  *
0085  * \warning        DES/3DES are considered weak ciphers and their use constitutes a
0086  *                 security risk. We recommend considering stronger ciphers
0087  *                 instead.
0088  */
0089 void mbedtls_des_free(mbedtls_des_context *ctx);
0090 
0091 /**
0092  * \brief          Initialize Triple-DES context
0093  *
0094  * \param ctx      DES3 context to be initialized
0095  *
0096  * \warning        DES/3DES are considered weak ciphers and their use constitutes a
0097  *                 security risk. We recommend considering stronger ciphers
0098  *                 instead.
0099  */
0100 void mbedtls_des3_init(mbedtls_des3_context *ctx);
0101 
0102 /**
0103  * \brief          Clear Triple-DES context
0104  *
0105  * \param ctx      DES3 context to be cleared
0106  *
0107  * \warning        DES/3DES are considered weak ciphers and their use constitutes a
0108  *                 security risk. We recommend considering stronger ciphers
0109  *                 instead.
0110  */
0111 void mbedtls_des3_free(mbedtls_des3_context *ctx);
0112 
0113 /**
0114  * \brief          Set key parity on the given key to odd.
0115  *
0116  *                 DES keys are 56 bits long, but each byte is padded with
0117  *                 a parity bit to allow verification.
0118  *
0119  * \param key      8-byte secret key
0120  *
0121  * \warning        DES/3DES are considered weak ciphers and their use constitutes a
0122  *                 security risk. We recommend considering stronger ciphers
0123  *                 instead.
0124  */
0125 void mbedtls_des_key_set_parity(unsigned char key[MBEDTLS_DES_KEY_SIZE]);
0126 
0127 /**
0128  * \brief          Check that key parity on the given key is odd.
0129  *
0130  *                 DES keys are 56 bits long, but each byte is padded with
0131  *                 a parity bit to allow verification.
0132  *
0133  * \param key      8-byte secret key
0134  *
0135  * \return         0 is parity was ok, 1 if parity was not correct.
0136  *
0137  * \warning        DES/3DES are considered weak ciphers and their use constitutes a
0138  *                 security risk. We recommend considering stronger ciphers
0139  *                 instead.
0140  */
0141 MBEDTLS_CHECK_RETURN_TYPICAL
0142 int mbedtls_des_key_check_key_parity(const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
0143 
0144 /**
0145  * \brief          Check that key is not a weak or semi-weak DES key
0146  *
0147  * \param key      8-byte secret key
0148  *
0149  * \return         0 if no weak key was found, 1 if a weak key was identified.
0150  *
0151  * \warning        DES/3DES are considered weak ciphers and their use constitutes a
0152  *                 security risk. We recommend considering stronger ciphers
0153  *                 instead.
0154  */
0155 MBEDTLS_CHECK_RETURN_TYPICAL
0156 int mbedtls_des_key_check_weak(const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
0157 
0158 /**
0159  * \brief          DES key schedule (56-bit, encryption)
0160  *
0161  * \param ctx      DES context to be initialized
0162  * \param key      8-byte secret key
0163  *
0164  * \return         0
0165  *
0166  * \warning        DES/3DES are considered weak ciphers and their use constitutes a
0167  *                 security risk. We recommend considering stronger ciphers
0168  *                 instead.
0169  */
0170 MBEDTLS_CHECK_RETURN_TYPICAL
0171 int mbedtls_des_setkey_enc(mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
0172 
0173 /**
0174  * \brief          DES key schedule (56-bit, decryption)
0175  *
0176  * \param ctx      DES context to be initialized
0177  * \param key      8-byte secret key
0178  *
0179  * \return         0
0180  *
0181  * \warning        DES/3DES are considered weak ciphers and their use constitutes a
0182  *                 security risk. We recommend considering stronger ciphers
0183  *                 instead.
0184  */
0185 MBEDTLS_CHECK_RETURN_TYPICAL
0186 int mbedtls_des_setkey_dec(mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
0187 
0188 /**
0189  * \brief          Triple-DES key schedule (112-bit, encryption)
0190  *
0191  * \param ctx      3DES context to be initialized
0192  * \param key      16-byte secret key
0193  *
0194  * \return         0
0195  *
0196  * \warning        DES/3DES are considered weak ciphers and their use constitutes a
0197  *                 security risk. We recommend considering stronger ciphers
0198  *                 instead.
0199  */
0200 MBEDTLS_CHECK_RETURN_TYPICAL
0201 int mbedtls_des3_set2key_enc(mbedtls_des3_context *ctx,
0202                              const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2]);
0203 
0204 /**
0205  * \brief          Triple-DES key schedule (112-bit, decryption)
0206  *
0207  * \param ctx      3DES context to be initialized
0208  * \param key      16-byte secret key
0209  *
0210  * \return         0
0211  *
0212  * \warning        DES/3DES are considered weak ciphers and their use constitutes a
0213  *                 security risk. We recommend considering stronger ciphers
0214  *                 instead.
0215  */
0216 MBEDTLS_CHECK_RETURN_TYPICAL
0217 int mbedtls_des3_set2key_dec(mbedtls_des3_context *ctx,
0218                              const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2]);
0219 
0220 /**
0221  * \brief          Triple-DES key schedule (168-bit, encryption)
0222  *
0223  * \param ctx      3DES context to be initialized
0224  * \param key      24-byte secret key
0225  *
0226  * \return         0
0227  *
0228  * \warning        DES/3DES are considered weak ciphers and their use constitutes a
0229  *                 security risk. We recommend considering stronger ciphers
0230  *                 instead.
0231  */
0232 MBEDTLS_CHECK_RETURN_TYPICAL
0233 int mbedtls_des3_set3key_enc(mbedtls_des3_context *ctx,
0234                              const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3]);
0235 
0236 /**
0237  * \brief          Triple-DES key schedule (168-bit, decryption)
0238  *
0239  * \param ctx      3DES context to be initialized
0240  * \param key      24-byte secret key
0241  *
0242  * \return         0
0243  *
0244  * \warning        DES/3DES are considered weak ciphers and their use constitutes a
0245  *                 security risk. We recommend considering stronger ciphers
0246  *                 instead.
0247  */
0248 MBEDTLS_CHECK_RETURN_TYPICAL
0249 int mbedtls_des3_set3key_dec(mbedtls_des3_context *ctx,
0250                              const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3]);
0251 
0252 /**
0253  * \brief          DES-ECB block encryption/decryption
0254  *
0255  * \param ctx      DES context
0256  * \param input    64-bit input block
0257  * \param output   64-bit output block
0258  *
0259  * \return         0 if successful
0260  *
0261  * \warning        DES/3DES are considered weak ciphers and their use constitutes a
0262  *                 security risk. We recommend considering stronger ciphers
0263  *                 instead.
0264  */
0265 MBEDTLS_CHECK_RETURN_TYPICAL
0266 int mbedtls_des_crypt_ecb(mbedtls_des_context *ctx,
0267                           const unsigned char input[8],
0268                           unsigned char output[8]);
0269 
0270 #if defined(MBEDTLS_CIPHER_MODE_CBC)
0271 /**
0272  * \brief          DES-CBC buffer encryption/decryption
0273  *
0274  * \note           Upon exit, the content of the IV is updated so that you can
0275  *                 call the function same function again on the following
0276  *                 block(s) of data and get the same result as if it was
0277  *                 encrypted in one call. This allows a "streaming" usage.
0278  *                 If on the other hand you need to retain the contents of the
0279  *                 IV, you should either save it manually or use the cipher
0280  *                 module instead.
0281  *
0282  * \param ctx      DES context
0283  * \param mode     MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
0284  * \param length   length of the input data
0285  * \param iv       initialization vector (updated after use)
0286  * \param input    buffer holding the input data
0287  * \param output   buffer holding the output data
0288  *
0289  * \warning        DES/3DES are considered weak ciphers and their use constitutes a
0290  *                 security risk. We recommend considering stronger ciphers
0291  *                 instead.
0292  */
0293 MBEDTLS_CHECK_RETURN_TYPICAL
0294 int mbedtls_des_crypt_cbc(mbedtls_des_context *ctx,
0295                           int mode,
0296                           size_t length,
0297                           unsigned char iv[8],
0298                           const unsigned char *input,
0299                           unsigned char *output);
0300 #endif /* MBEDTLS_CIPHER_MODE_CBC */
0301 
0302 /**
0303  * \brief          3DES-ECB block encryption/decryption
0304  *
0305  * \param ctx      3DES context
0306  * \param input    64-bit input block
0307  * \param output   64-bit output block
0308  *
0309  * \return         0 if successful
0310  *
0311  * \warning        DES/3DES are considered weak ciphers and their use constitutes a
0312  *                 security risk. We recommend considering stronger ciphers
0313  *                 instead.
0314  */
0315 MBEDTLS_CHECK_RETURN_TYPICAL
0316 int mbedtls_des3_crypt_ecb(mbedtls_des3_context *ctx,
0317                            const unsigned char input[8],
0318                            unsigned char output[8]);
0319 
0320 #if defined(MBEDTLS_CIPHER_MODE_CBC)
0321 /**
0322  * \brief          3DES-CBC buffer encryption/decryption
0323  *
0324  * \note           Upon exit, the content of the IV is updated so that you can
0325  *                 call the function same function again on the following
0326  *                 block(s) of data and get the same result as if it was
0327  *                 encrypted in one call. This allows a "streaming" usage.
0328  *                 If on the other hand you need to retain the contents of the
0329  *                 IV, you should either save it manually or use the cipher
0330  *                 module instead.
0331  *
0332  * \param ctx      3DES context
0333  * \param mode     MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
0334  * \param length   length of the input data
0335  * \param iv       initialization vector (updated after use)
0336  * \param input    buffer holding the input data
0337  * \param output   buffer holding the output data
0338  *
0339  * \return         0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
0340  *
0341  * \warning        DES/3DES are considered weak ciphers and their use constitutes a
0342  *                 security risk. We recommend considering stronger ciphers
0343  *                 instead.
0344  */
0345 MBEDTLS_CHECK_RETURN_TYPICAL
0346 int mbedtls_des3_crypt_cbc(mbedtls_des3_context *ctx,
0347                            int mode,
0348                            size_t length,
0349                            unsigned char iv[8],
0350                            const unsigned char *input,
0351                            unsigned char *output);
0352 #endif /* MBEDTLS_CIPHER_MODE_CBC */
0353 
0354 /**
0355  * \brief          Internal function for key expansion.
0356  *                 (Only exposed to allow overriding it,
0357  *                 see MBEDTLS_DES_SETKEY_ALT)
0358  *
0359  * \param SK       Round keys
0360  * \param key      Base key
0361  *
0362  * \warning        DES/3DES are considered weak ciphers and their use constitutes a
0363  *                 security risk. We recommend considering stronger ciphers
0364  *                 instead.
0365  */
0366 void mbedtls_des_setkey(uint32_t SK[32],
0367                         const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
0368 
0369 #if defined(MBEDTLS_SELF_TEST)
0370 
0371 /**
0372  * \brief          Checkup routine
0373  *
0374  * \return         0 if successful, or 1 if the test failed
0375  */
0376 MBEDTLS_CHECK_RETURN_CRITICAL
0377 int mbedtls_des_self_test(int verbose);
0378 
0379 #endif /* MBEDTLS_SELF_TEST */
0380 
0381 #ifdef __cplusplus
0382 }
0383 #endif
0384 
0385 #endif /* des.h */