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