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