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 |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 5 | * |
| 6 | * \warning DES is considered a weak cipher and its use constitutes a |
| 7 | * security risk. We recommend considering stronger ciphers |
| 8 | * instead. |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 9 | */ |
| 10 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 11 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * SPDX-License-Identifier: Apache-2.0 |
| 13 | * |
| 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 15 | * not use this file except in compliance with the License. |
| 16 | * You may obtain a copy of the License at |
| 17 | * |
| 18 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 19 | * |
| 20 | * Unless required by applicable law or agreed to in writing, software |
| 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 23 | * See the License for the specific language governing permissions and |
| 24 | * limitations under the License. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 25 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 27 | #ifndef MBEDTLS_DES_H |
| 28 | #define MBEDTLS_DES_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #if !defined(MBEDTLS_CONFIG_FILE) |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 31 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 34 | #endif |
Gilles Peskine | 7b8571f | 2021-07-07 21:02:36 +0200 | [diff] [blame] | 35 | #include "mbedtls/platform_util.h" |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 36 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 37 | #include <stddef.h> |
Manuel Pégourié-Gonnard | ab22910 | 2015-04-15 11:53:16 +0200 | [diff] [blame] | 38 | #include <stdint.h> |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 39 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | #define MBEDTLS_DES_ENCRYPT 1 |
| 41 | #define MBEDTLS_DES_DECRYPT 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 42 | |
Gilles Peskine | a397443 | 2021-07-26 18:48:10 +0200 | [diff] [blame] | 43 | /** The data input has an invalid length. */ |
| 44 | #define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0032 |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 45 | |
| 46 | /* MBEDTLS_ERR_DES_HW_ACCEL_FAILED is deprecated and should not be used. */ |
Gilles Peskine | a397443 | 2021-07-26 18:48:10 +0200 | [diff] [blame] | 47 | /** DES hardware accelerator failed. */ |
| 48 | #define MBEDTLS_ERR_DES_HW_ACCEL_FAILED -0x0033 |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 49 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 50 | #define MBEDTLS_DES_KEY_SIZE 8 |
Paul Bakker | 1f87fb6 | 2011-01-15 17:32:24 +0000 | [diff] [blame] | 51 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 52 | #ifdef __cplusplus |
| 53 | extern "C" { |
| 54 | #endif |
| 55 | |
Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 56 | #if !defined(MBEDTLS_DES_ALT) |
| 57 | // Regular implementation |
| 58 | // |
| 59 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 60 | /** |
| 61 | * \brief DES context structure |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 62 | * |
| 63 | * \warning DES is considered a weak cipher and its use constitutes a |
| 64 | * security risk. We recommend considering stronger ciphers |
| 65 | * instead. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 66 | */ |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 67 | typedef struct mbedtls_des_context { |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 68 | uint32_t sk[32]; /*!< DES subkeys */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 69 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 70 | mbedtls_des_context; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * \brief Triple-DES context structure |
| 74 | */ |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 75 | typedef struct mbedtls_des3_context { |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 76 | uint32_t sk[96]; /*!< 3DES subkeys */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 77 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 78 | mbedtls_des3_context; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 79 | |
Ron Eldor | 05d0e51 | 2018-04-16 17:40:04 +0300 | [diff] [blame] | 80 | #else /* MBEDTLS_DES_ALT */ |
| 81 | #include "des_alt.h" |
| 82 | #endif /* MBEDTLS_DES_ALT */ |
| 83 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 84 | /** |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 85 | * \brief Initialize DES context |
| 86 | * |
| 87 | * \param ctx DES context to be initialized |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 88 | * |
| 89 | * \warning DES is considered a weak cipher and its use constitutes a |
| 90 | * security risk. We recommend considering stronger ciphers |
| 91 | * instead. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 92 | */ |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 93 | void mbedtls_des_init(mbedtls_des_context *ctx); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * \brief Clear DES context |
| 97 | * |
| 98 | * \param ctx DES context to be cleared |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 99 | * |
| 100 | * \warning DES is considered a weak cipher and its use constitutes a |
| 101 | * security risk. We recommend considering stronger ciphers |
| 102 | * instead. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 103 | */ |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 104 | void mbedtls_des_free(mbedtls_des_context *ctx); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * \brief Initialize Triple-DES context |
| 108 | * |
| 109 | * \param ctx DES3 context to be initialized |
| 110 | */ |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 111 | void mbedtls_des3_init(mbedtls_des3_context *ctx); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * \brief Clear Triple-DES context |
| 115 | * |
| 116 | * \param ctx DES3 context to be cleared |
| 117 | */ |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 118 | void mbedtls_des3_free(mbedtls_des3_context *ctx); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 119 | |
| 120 | /** |
Paul Bakker | 1f87fb6 | 2011-01-15 17:32:24 +0000 | [diff] [blame] | 121 | * \brief Set key parity on the given key to odd. |
| 122 | * |
| 123 | * DES keys are 56 bits long, but each byte is padded with |
| 124 | * a parity bit to allow verification. |
| 125 | * |
| 126 | * \param key 8-byte secret key |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 127 | * |
| 128 | * \warning DES is considered a weak cipher and its use constitutes a |
| 129 | * security risk. We recommend considering stronger ciphers |
| 130 | * instead. |
Paul Bakker | 1f87fb6 | 2011-01-15 17:32:24 +0000 | [diff] [blame] | 131 | */ |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 132 | 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] | 133 | |
| 134 | /** |
| 135 | * \brief Check that key parity on the given key is odd. |
| 136 | * |
| 137 | * DES keys are 56 bits long, but each byte is padded with |
| 138 | * a parity bit to allow verification. |
| 139 | * |
| 140 | * \param key 8-byte secret key |
Paul Bakker | 7320695 | 2011-07-06 14:37:33 +0000 | [diff] [blame] | 141 | * |
| 142 | * \return 0 is parity was ok, 1 if parity was not correct. |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 143 | * |
| 144 | * \warning DES is considered a weak cipher and its use constitutes a |
| 145 | * security risk. We recommend considering stronger ciphers |
| 146 | * instead. |
Paul Bakker | 1f87fb6 | 2011-01-15 17:32:24 +0000 | [diff] [blame] | 147 | */ |
Gilles Peskine | ce555e4 | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 148 | MBEDTLS_CHECK_RETURN_TYPICAL |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 149 | 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] | 150 | |
Paul Bakker | 1f87fb6 | 2011-01-15 17:32:24 +0000 | [diff] [blame] | 151 | /** |
| 152 | * \brief Check that key is not a weak or semi-weak DES key |
| 153 | * |
| 154 | * \param key 8-byte secret key |
Paul Bakker | 7320695 | 2011-07-06 14:37:33 +0000 | [diff] [blame] | 155 | * |
Paul Bakker | 4793cc4 | 2011-08-17 09:40:55 +0000 | [diff] [blame] | 156 | * \return 0 if no weak key was found, 1 if a weak key was identified. |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 157 | * |
| 158 | * \warning DES is considered a weak cipher and its use constitutes a |
| 159 | * security risk. We recommend considering stronger ciphers |
| 160 | * instead. |
Paul Bakker | 1f87fb6 | 2011-01-15 17:32:24 +0000 | [diff] [blame] | 161 | */ |
Gilles Peskine | ce555e4 | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 162 | MBEDTLS_CHECK_RETURN_TYPICAL |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 163 | 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] | 164 | |
| 165 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 166 | * \brief DES key schedule (56-bit, encryption) |
| 167 | * |
| 168 | * \param ctx DES context to be initialized |
| 169 | * \param key 8-byte secret key |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 170 | * |
| 171 | * \return 0 |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 172 | * |
| 173 | * \warning DES is considered a weak cipher and its use constitutes a |
| 174 | * security risk. We recommend considering stronger ciphers |
| 175 | * instead. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 176 | */ |
Gilles Peskine | ce555e4 | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 177 | MBEDTLS_CHECK_RETURN_TYPICAL |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 178 | 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] | 179 | |
| 180 | /** |
| 181 | * \brief DES key schedule (56-bit, decryption) |
| 182 | * |
| 183 | * \param ctx DES context to be initialized |
| 184 | * \param key 8-byte secret key |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 185 | * |
| 186 | * \return 0 |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 187 | * |
| 188 | * \warning DES is considered a weak cipher and its use constitutes a |
| 189 | * security risk. We recommend considering stronger ciphers |
| 190 | * instead. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 191 | */ |
Gilles Peskine | ce555e4 | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 192 | MBEDTLS_CHECK_RETURN_TYPICAL |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 193 | 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] | 194 | |
| 195 | /** |
| 196 | * \brief Triple-DES key schedule (112-bit, encryption) |
| 197 | * |
| 198 | * \param ctx 3DES context to be initialized |
| 199 | * \param key 16-byte secret key |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 200 | * |
| 201 | * \return 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 202 | */ |
Gilles Peskine | ce555e4 | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 203 | MBEDTLS_CHECK_RETURN_TYPICAL |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 204 | int mbedtls_des3_set2key_enc(mbedtls_des3_context *ctx, |
| 205 | const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2]); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 206 | |
| 207 | /** |
| 208 | * \brief Triple-DES key schedule (112-bit, decryption) |
| 209 | * |
| 210 | * \param ctx 3DES context to be initialized |
| 211 | * \param key 16-byte secret key |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 212 | * |
| 213 | * \return 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 214 | */ |
Gilles Peskine | ce555e4 | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 215 | MBEDTLS_CHECK_RETURN_TYPICAL |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 216 | int mbedtls_des3_set2key_dec(mbedtls_des3_context *ctx, |
| 217 | const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2]); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 218 | |
| 219 | /** |
| 220 | * \brief Triple-DES key schedule (168-bit, encryption) |
| 221 | * |
| 222 | * \param ctx 3DES context to be initialized |
| 223 | * \param key 24-byte secret key |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 224 | * |
| 225 | * \return 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 226 | */ |
Gilles Peskine | ce555e4 | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 227 | MBEDTLS_CHECK_RETURN_TYPICAL |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 228 | int mbedtls_des3_set3key_enc(mbedtls_des3_context *ctx, |
| 229 | const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3]); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 230 | |
| 231 | /** |
| 232 | * \brief Triple-DES key schedule (168-bit, decryption) |
| 233 | * |
| 234 | * \param ctx 3DES context to be initialized |
| 235 | * \param key 24-byte secret key |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 236 | * |
| 237 | * \return 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 238 | */ |
Gilles Peskine | ce555e4 | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 239 | MBEDTLS_CHECK_RETURN_TYPICAL |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 240 | int mbedtls_des3_set3key_dec(mbedtls_des3_context *ctx, |
| 241 | const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3]); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 242 | |
| 243 | /** |
| 244 | * \brief DES-ECB block encryption/decryption |
| 245 | * |
| 246 | * \param ctx DES context |
| 247 | * \param input 64-bit input block |
| 248 | * \param output 64-bit output block |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 249 | * |
Paul Bakker | 27caa8a | 2010-03-21 15:43:59 +0000 | [diff] [blame] | 250 | * \return 0 if successful |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 251 | * |
| 252 | * \warning DES is considered a weak cipher and its use constitutes a |
| 253 | * security risk. We recommend considering stronger ciphers |
| 254 | * instead. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 255 | */ |
Gilles Peskine | ce555e4 | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 256 | MBEDTLS_CHECK_RETURN_TYPICAL |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 257 | int mbedtls_des_crypt_ecb(mbedtls_des_context *ctx, |
| 258 | const unsigned char input[8], |
| 259 | unsigned char output[8]); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 260 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 261 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 262 | /** |
| 263 | * \brief DES-CBC buffer encryption/decryption |
| 264 | * |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 265 | * \note Upon exit, the content of the IV is updated so that you can |
| 266 | * call the function same function again on the following |
| 267 | * block(s) of data and get the same result as if it was |
| 268 | * encrypted in one call. This allows a "streaming" usage. |
| 269 | * If on the other hand you need to retain the contents of the |
| 270 | * IV, you should either save it manually or use the cipher |
| 271 | * module instead. |
| 272 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 273 | * \param ctx DES context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 274 | * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 275 | * \param length length of the input data |
| 276 | * \param iv initialization vector (updated after use) |
| 277 | * \param input buffer holding the input data |
| 278 | * \param output buffer holding the output data |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 279 | * |
| 280 | * \warning DES is considered a weak cipher and its use constitutes a |
| 281 | * security risk. We recommend considering stronger ciphers |
| 282 | * instead. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 283 | */ |
Gilles Peskine | ce555e4 | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 284 | MBEDTLS_CHECK_RETURN_TYPICAL |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 285 | int mbedtls_des_crypt_cbc(mbedtls_des_context *ctx, |
| 286 | int mode, |
| 287 | size_t length, |
| 288 | unsigned char iv[8], |
| 289 | const unsigned char *input, |
| 290 | unsigned char *output); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 291 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 292 | |
| 293 | /** |
| 294 | * \brief 3DES-ECB block encryption/decryption |
| 295 | * |
| 296 | * \param ctx 3DES context |
| 297 | * \param input 64-bit input block |
| 298 | * \param output 64-bit output block |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 299 | * |
Paul Bakker | 27caa8a | 2010-03-21 15:43:59 +0000 | [diff] [blame] | 300 | * \return 0 if successful |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 301 | */ |
Gilles Peskine | ce555e4 | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 302 | MBEDTLS_CHECK_RETURN_TYPICAL |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 303 | int mbedtls_des3_crypt_ecb(mbedtls_des3_context *ctx, |
| 304 | const unsigned char input[8], |
| 305 | unsigned char output[8]); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 306 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 307 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 308 | /** |
| 309 | * \brief 3DES-CBC buffer encryption/decryption |
| 310 | * |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 311 | * \note Upon exit, the content of the IV is updated so that you can |
| 312 | * call the function same function again on the following |
| 313 | * block(s) of data and get the same result as if it was |
| 314 | * encrypted in one call. This allows a "streaming" usage. |
| 315 | * If on the other hand you need to retain the contents of the |
| 316 | * IV, you should either save it manually or use the cipher |
| 317 | * module instead. |
| 318 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 319 | * \param ctx 3DES context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 320 | * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 321 | * \param length length of the input data |
| 322 | * \param iv initialization vector (updated after use) |
| 323 | * \param input buffer holding the input data |
| 324 | * \param output buffer holding the output data |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 325 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 326 | * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 327 | */ |
Gilles Peskine | ce555e4 | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 328 | MBEDTLS_CHECK_RETURN_TYPICAL |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 329 | int mbedtls_des3_crypt_cbc(mbedtls_des3_context *ctx, |
| 330 | int mode, |
| 331 | size_t length, |
| 332 | unsigned char iv[8], |
| 333 | const unsigned char *input, |
| 334 | unsigned char *output); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 335 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 336 | |
Manuel Pégourié-Gonnard | 70a5010 | 2015-05-12 15:02:45 +0200 | [diff] [blame] | 337 | /** |
| 338 | * \brief Internal function for key expansion. |
| 339 | * (Only exposed to allow overriding it, |
| 340 | * see MBEDTLS_DES_SETKEY_ALT) |
| 341 | * |
| 342 | * \param SK Round keys |
| 343 | * \param key Base key |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 344 | * |
| 345 | * \warning DES is considered a weak cipher and its use constitutes a |
| 346 | * security risk. We recommend considering stronger ciphers |
| 347 | * instead. |
Manuel Pégourié-Gonnard | 70a5010 | 2015-05-12 15:02:45 +0200 | [diff] [blame] | 348 | */ |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 349 | void mbedtls_des_setkey(uint32_t SK[32], |
| 350 | const unsigned char key[MBEDTLS_DES_KEY_SIZE]); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 351 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 352 | #if defined(MBEDTLS_SELF_TEST) |
| 353 | |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 354 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 355 | * \brief Checkup routine |
| 356 | * |
| 357 | * \return 0 if successful, or 1 if the test failed |
| 358 | */ |
Gilles Peskine | ce555e4 | 2021-09-23 17:35:37 +0200 | [diff] [blame] | 359 | MBEDTLS_CHECK_RETURN_CRITICAL |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 360 | int mbedtls_des_self_test(int verbose); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 361 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 362 | #endif /* MBEDTLS_SELF_TEST */ |
| 363 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 364 | #ifdef __cplusplus |
| 365 | } |
| 366 | #endif |
| 367 | |
| 368 | #endif /* des.h */ |