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