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