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