1 /** 2 * \file des.h 3 * 4 * \brief DES block cipher 5 * 6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 * SPDX-License-Identifier: Apache-2.0 8 * 9 * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 * not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 * 21 * This file is part of mbed TLS (https://tls.mbed.org) 22 */ 23 #ifndef MBEDTLS_DES_H 24 #define MBEDTLS_DES_H 25 26 #if !defined(MBEDTLS_CONFIG_FILE) 27 #include "config.h" 28 #else 29 #include MBEDTLS_CONFIG_FILE 30 #endif 31 32 #include <stddef.h> 33 #include <stdint.h> 34 35 #define MBEDTLS_DES_ENCRYPT 1 36 #define MBEDTLS_DES_DECRYPT 0 37 38 #define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */ 39 40 #define MBEDTLS_DES_KEY_SIZE 8 41 42 #if !defined(MBEDTLS_DES_ALT) 43 // Regular implementation 44 // 45 46 #ifdef __cplusplus 47 extern "C" { 48 #endif 49 50 /** 51 * \brief DES context structure 52 */ 53 typedef struct 54 { 55 uint32_t sk[32]; /*!< DES subkeys */ 56 } 57 mbedtls_des_context; 58 59 /** 60 * \brief Triple-DES context structure 61 */ 62 typedef struct 63 { 64 uint32_t sk[96]; /*!< 3DES subkeys */ 65 } 66 mbedtls_des3_context; 67 68 /** 69 * \brief Initialize DES context 70 * 71 * \param ctx DES context to be initialized 72 */ 73 void mbedtls_des_init( mbedtls_des_context *ctx ); 74 75 /** 76 * \brief Clear DES context 77 * 78 * \param ctx DES context to be cleared 79 */ 80 void mbedtls_des_free( mbedtls_des_context *ctx ); 81 82 /** 83 * \brief Initialize Triple-DES context 84 * 85 * \param ctx DES3 context to be initialized 86 */ 87 void mbedtls_des3_init( mbedtls_des3_context *ctx ); 88 89 /** 90 * \brief Clear Triple-DES context 91 * 92 * \param ctx DES3 context to be cleared 93 */ 94 void mbedtls_des3_free( mbedtls_des3_context *ctx ); 95 96 /** 97 * \brief Set key parity on the given key to odd. 98 * 99 * DES keys are 56 bits long, but each byte is padded with 100 * a parity bit to allow verification. 101 * 102 * \param key 8-byte secret key 103 */ 104 void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] ); 105 106 /** 107 * \brief Check that key parity on the given key is odd. 108 * 109 * DES keys are 56 bits long, but each byte is padded with 110 * a parity bit to allow verification. 111 * 112 * \param key 8-byte secret key 113 * 114 * \return 0 is parity was ok, 1 if parity was not correct. 115 */ 116 int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] ); 117 118 /** 119 * \brief Check that key is not a weak or semi-weak DES key 120 * 121 * \param key 8-byte secret key 122 * 123 * \return 0 if no weak key was found, 1 if a weak key was identified. 124 */ 125 int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] ); 126 127 /** 128 * \brief DES key schedule (56-bit, encryption) 129 * 130 * \param ctx DES context to be initialized 131 * \param key 8-byte secret key 132 * 133 * \return 0 134 */ 135 int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] ); 136 137 /** 138 * \brief DES key schedule (56-bit, decryption) 139 * 140 * \param ctx DES context to be initialized 141 * \param key 8-byte secret key 142 * 143 * \return 0 144 */ 145 int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] ); 146 147 /** 148 * \brief Triple-DES key schedule (112-bit, encryption) 149 * 150 * \param ctx 3DES context to be initialized 151 * \param key 16-byte secret key 152 * 153 * \return 0 154 */ 155 int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx, 156 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] ); 157 158 /** 159 * \brief Triple-DES key schedule (112-bit, decryption) 160 * 161 * \param ctx 3DES context to be initialized 162 * \param key 16-byte secret key 163 * 164 * \return 0 165 */ 166 int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx, 167 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] ); 168 169 /** 170 * \brief Triple-DES key schedule (168-bit, encryption) 171 * 172 * \param ctx 3DES context to be initialized 173 * \param key 24-byte secret key 174 * 175 * \return 0 176 */ 177 int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx, 178 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] ); 179 180 /** 181 * \brief Triple-DES key schedule (168-bit, decryption) 182 * 183 * \param ctx 3DES context to be initialized 184 * \param key 24-byte secret key 185 * 186 * \return 0 187 */ 188 int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx, 189 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] ); 190 191 /** 192 * \brief DES-ECB block encryption/decryption 193 * 194 * \param ctx DES context 195 * \param input 64-bit input block 196 * \param output 64-bit output block 197 * 198 * \return 0 if successful 199 */ 200 int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx, 201 const unsigned char input[8], 202 unsigned char output[8] ); 203 204 #if defined(MBEDTLS_CIPHER_MODE_CBC) 205 /** 206 * \brief DES-CBC buffer encryption/decryption 207 * 208 * \note Upon exit, the content of the IV is updated so that you can 209 * call the function same function again on the following 210 * block(s) of data and get the same result as if it was 211 * encrypted in one call. This allows a "streaming" usage. 212 * If on the other hand you need to retain the contents of the 213 * IV, you should either save it manually or use the cipher 214 * module instead. 215 * 216 * \param ctx DES context 217 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT 218 * \param length length of the input data 219 * \param iv initialization vector (updated after use) 220 * \param input buffer holding the input data 221 * \param output buffer holding the output data 222 */ 223 int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx, 224 int mode, 225 size_t length, 226 unsigned char iv[8], 227 const unsigned char *input, 228 unsigned char *output ); 229 #endif /* MBEDTLS_CIPHER_MODE_CBC */ 230 231 /** 232 * \brief 3DES-ECB block encryption/decryption 233 * 234 * \param ctx 3DES context 235 * \param input 64-bit input block 236 * \param output 64-bit output block 237 * 238 * \return 0 if successful 239 */ 240 int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx, 241 const unsigned char input[8], 242 unsigned char output[8] ); 243 244 #if defined(MBEDTLS_CIPHER_MODE_CBC) 245 /** 246 * \brief 3DES-CBC buffer encryption/decryption 247 * 248 * \note Upon exit, the content of the IV is updated so that you can 249 * call the function same function again on the following 250 * block(s) of data and get the same result as if it was 251 * encrypted in one call. This allows a "streaming" usage. 252 * If on the other hand you need to retain the contents of the 253 * IV, you should either save it manually or use the cipher 254 * module instead. 255 * 256 * \param ctx 3DES context 257 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT 258 * \param length length of the input data 259 * \param iv initialization vector (updated after use) 260 * \param input buffer holding the input data 261 * \param output buffer holding the output data 262 * 263 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH 264 */ 265 int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx, 266 int mode, 267 size_t length, 268 unsigned char iv[8], 269 const unsigned char *input, 270 unsigned char *output ); 271 #endif /* MBEDTLS_CIPHER_MODE_CBC */ 272 273 /** 274 * \brief Internal function for key expansion. 275 * (Only exposed to allow overriding it, 276 * see MBEDTLS_DES_SETKEY_ALT) 277 * 278 * \param SK Round keys 279 * \param key Base key 280 */ 281 void mbedtls_des_setkey( uint32_t SK[32], 282 const unsigned char key[MBEDTLS_DES_KEY_SIZE] ); 283 #ifdef __cplusplus 284 } 285 #endif 286 287 #else /* MBEDTLS_DES_ALT */ 288 #include "des_alt.h" 289 #endif /* MBEDTLS_DES_ALT */ 290 291 #ifdef __cplusplus 292 extern "C" { 293 #endif 294 295 /** 296 * \brief Checkup routine 297 * 298 * \return 0 if successful, or 1 if the test failed 299 */ 300 int mbedtls_des_self_test( int verbose ); 301 302 #ifdef __cplusplus 303 } 304 #endif 305 306 #endif /* des.h */ 307