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