| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** | 
|  | 2 | * \file des.h | 
| Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * | 
| Paul Bakker | f3b86c1 | 2011-01-27 15:24:17 +0000 | [diff] [blame] | 4 | * \brief DES block cipher | 
| Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 5 | * | 
| Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 6 | *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 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. | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 20 | * | 
| Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 21 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 22 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #ifndef MBEDTLS_DES_H | 
|  | 24 | #define MBEDTLS_DES_H | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if !defined(MBEDTLS_CONFIG_FILE) | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 27 | #include "config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 28 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #include MBEDTLS_CONFIG_FILE | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 30 | #endif | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 31 |  | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 32 | #include <stddef.h> | 
| Manuel Pégourié-Gonnard | ab22910 | 2015-04-15 11:53:16 +0200 | [diff] [blame] | 33 | #include <stdint.h> | 
| Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 34 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 35 | #define MBEDTLS_DES_ENCRYPT     1 | 
|  | 36 | #define MBEDTLS_DES_DECRYPT     0 | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 37 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 38 | #define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH              -0x0032  /**< The data input has an invalid length. */ | 
| Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 39 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | #define MBEDTLS_DES_KEY_SIZE    8 | 
| Paul Bakker | 1f87fb6 | 2011-01-15 17:32:24 +0000 | [diff] [blame] | 41 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | #if !defined(MBEDTLS_DES_ALT) | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 43 | // Regular implementation | 
|  | 44 | // | 
|  | 45 |  | 
| Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 46 | #ifdef __cplusplus | 
|  | 47 | extern "C" { | 
|  | 48 | #endif | 
|  | 49 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 50 | /** | 
|  | 51 | * \brief          DES context structure | 
|  | 52 | */ | 
|  | 53 | typedef struct | 
|  | 54 | { | 
| Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 55 | uint32_t sk[32];            /*!<  DES subkeys       */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 56 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 57 | mbedtls_des_context; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 58 |  | 
|  | 59 | /** | 
|  | 60 | * \brief          Triple-DES context structure | 
|  | 61 | */ | 
|  | 62 | typedef struct | 
|  | 63 | { | 
| Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 64 | uint32_t sk[96];            /*!<  3DES subkeys      */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 65 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 66 | mbedtls_des3_context; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 67 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 68 | /** | 
| Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 69 | * \brief          Initialize DES context | 
|  | 70 | * | 
|  | 71 | * \param ctx      DES context to be initialized | 
|  | 72 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 73 | void mbedtls_des_init( mbedtls_des_context *ctx ); | 
| Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 74 |  | 
|  | 75 | /** | 
|  | 76 | * \brief          Clear DES context | 
|  | 77 | * | 
|  | 78 | * \param ctx      DES context to be cleared | 
|  | 79 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 80 | void mbedtls_des_free( mbedtls_des_context *ctx ); | 
| Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 81 |  | 
|  | 82 | /** | 
|  | 83 | * \brief          Initialize Triple-DES context | 
|  | 84 | * | 
|  | 85 | * \param ctx      DES3 context to be initialized | 
|  | 86 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 87 | void mbedtls_des3_init( mbedtls_des3_context *ctx ); | 
| Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 88 |  | 
|  | 89 | /** | 
|  | 90 | * \brief          Clear Triple-DES context | 
|  | 91 | * | 
|  | 92 | * \param ctx      DES3 context to be cleared | 
|  | 93 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 94 | void mbedtls_des3_free( mbedtls_des3_context *ctx ); | 
| Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 95 |  | 
|  | 96 | /** | 
| Paul Bakker | 1f87fb6 | 2011-01-15 17:32:24 +0000 | [diff] [blame] | 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 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 104 | void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] ); | 
| Paul Bakker | 1f87fb6 | 2011-01-15 17:32:24 +0000 | [diff] [blame] | 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 | 
| Paul Bakker | 7320695 | 2011-07-06 14:37:33 +0000 | [diff] [blame] | 113 | * | 
|  | 114 | * \return         0 is parity was ok, 1 if parity was not correct. | 
| Paul Bakker | 1f87fb6 | 2011-01-15 17:32:24 +0000 | [diff] [blame] | 115 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 116 | int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] ); | 
| Paul Bakker | 1f87fb6 | 2011-01-15 17:32:24 +0000 | [diff] [blame] | 117 |  | 
| Paul Bakker | 1f87fb6 | 2011-01-15 17:32:24 +0000 | [diff] [blame] | 118 | /** | 
|  | 119 | * \brief          Check that key is not a weak or semi-weak DES key | 
|  | 120 | * | 
|  | 121 | * \param key      8-byte secret key | 
| Paul Bakker | 7320695 | 2011-07-06 14:37:33 +0000 | [diff] [blame] | 122 | * | 
| Paul Bakker | 4793cc4 | 2011-08-17 09:40:55 +0000 | [diff] [blame] | 123 | * \return         0 if no weak key was found, 1 if a weak key was identified. | 
| Paul Bakker | 1f87fb6 | 2011-01-15 17:32:24 +0000 | [diff] [blame] | 124 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 125 | int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] ); | 
| Paul Bakker | 1f87fb6 | 2011-01-15 17:32:24 +0000 | [diff] [blame] | 126 |  | 
|  | 127 | /** | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 128 | * \brief          DES key schedule (56-bit, encryption) | 
|  | 129 | * | 
|  | 130 | * \param ctx      DES context to be initialized | 
|  | 131 | * \param key      8-byte secret key | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 132 | * | 
|  | 133 | * \return         0 | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 134 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 135 | int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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 | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 142 | * | 
|  | 143 | * \return         0 | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 144 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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 | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 152 | * | 
|  | 153 | * \return         0 | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 155 | int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx, | 
|  | 156 | const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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 | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 163 | * | 
|  | 164 | * \return         0 | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 165 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx, | 
|  | 167 | const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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 | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 174 | * | 
|  | 175 | * \return         0 | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 176 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 177 | int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx, | 
|  | 178 | const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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 | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 185 | * | 
|  | 186 | * \return         0 | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 187 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx, | 
|  | 189 | const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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 | 
| Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 197 | * | 
| Paul Bakker | 27caa8a | 2010-03-21 15:43:59 +0000 | [diff] [blame] | 198 | * \return         0 if successful | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 199 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 200 | int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx, | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 201 | const unsigned char input[8], | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 202 | unsigned char output[8] ); | 
|  | 203 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | #if defined(MBEDTLS_CIPHER_MODE_CBC) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 205 | /** | 
|  | 206 | * \brief          DES-CBC buffer encryption/decryption | 
|  | 207 | * | 
| Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 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 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 216 | * \param ctx      DES context | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 217 | * \param mode     MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 223 | int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx, | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 224 | int mode, | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 225 | size_t length, | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 226 | unsigned char iv[8], | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 227 | const unsigned char *input, | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 228 | unsigned char *output ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 229 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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 | 
| Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 237 | * | 
| Paul Bakker | 27caa8a | 2010-03-21 15:43:59 +0000 | [diff] [blame] | 238 | * \return         0 if successful | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 239 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 240 | int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx, | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 241 | const unsigned char input[8], | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 242 | unsigned char output[8] ); | 
|  | 243 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 244 | #if defined(MBEDTLS_CIPHER_MODE_CBC) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 245 | /** | 
|  | 246 | * \brief          3DES-CBC buffer encryption/decryption | 
|  | 247 | * | 
| Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 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 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 256 | * \param ctx      3DES context | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 257 | * \param mode     MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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 | 
| Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 262 | * | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 263 | * \return         0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 264 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 265 | int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx, | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 266 | int mode, | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 267 | size_t length, | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 268 | unsigned char iv[8], | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 269 | const unsigned char *input, | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 270 | unsigned char *output ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 271 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 272 |  | 
| Manuel Pégourié-Gonnard | 70a5010 | 2015-05-12 15:02:45 +0200 | [diff] [blame] | 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] ); | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 283 | #ifdef __cplusplus | 
|  | 284 | } | 
|  | 285 | #endif | 
|  | 286 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 287 | #else  /* MBEDTLS_DES_ALT */ | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 288 | #include "des_alt.h" | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 289 | #endif /* MBEDTLS_DES_ALT */ | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 290 |  | 
|  | 291 | #ifdef __cplusplus | 
|  | 292 | extern "C" { | 
|  | 293 | #endif | 
|  | 294 |  | 
| Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 295 | /** | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 296 | * \brief          Checkup routine | 
|  | 297 | * | 
|  | 298 | * \return         0 if successful, or 1 if the test failed | 
|  | 299 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 300 | int mbedtls_des_self_test( int verbose ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 301 |  | 
|  | 302 | #ifdef __cplusplus | 
|  | 303 | } | 
|  | 304 | #endif | 
|  | 305 |  | 
|  | 306 | #endif /* des.h */ |