blob: 5ca2ecf2e0990d83eed1476d9f174470c54b2a60 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file des.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +00004 * \brief DES block cipher
Paul Bakker37ca75d2011-01-06 12:28:03 +00005 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_DES_H
24#define MBEDTLS_DES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020027#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker90995b52013-06-24 19:20:35 +020031
Rich Evans00ab4702015-02-06 13:43:58 +000032#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020033#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#define MBEDTLS_DES_ENCRYPT 1
36#define MBEDTLS_DES_DECRYPT 0
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#define MBEDTLS_DES_KEY_SIZE 8
Paul Bakker1f87fb62011-01-15 17:32:24 +000041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if !defined(MBEDTLS_DES_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020043// Regular implementation
44//
45
Paul Bakker407a0da2013-06-27 14:29:21 +020046#ifdef __cplusplus
47extern "C" {
48#endif
49
Paul Bakker5121ce52009-01-03 21:22:43 +000050/**
51 * \brief DES context structure
52 */
53typedef struct
54{
Paul Bakker5c2364c2012-10-01 14:41:15 +000055 uint32_t sk[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000056}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057mbedtls_des_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000058
59/**
60 * \brief Triple-DES context structure
61 */
62typedef struct
63{
Paul Bakker5c2364c2012-10-01 14:41:15 +000064 uint32_t sk[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000065}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066mbedtls_des3_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Paul Bakker5121ce52009-01-03 21:22:43 +000068/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020069 * \brief Initialize DES context
70 *
71 * \param ctx DES context to be initialized
72 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073void mbedtls_des_init( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020074
75/**
76 * \brief Clear DES context
77 *
78 * \param ctx DES context to be cleared
79 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080void mbedtls_des_free( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020081
82/**
83 * \brief Initialize Triple-DES context
84 *
85 * \param ctx DES3 context to be initialized
86 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087void mbedtls_des3_init( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020088
89/**
90 * \brief Clear Triple-DES context
91 *
92 * \param ctx DES3 context to be cleared
93 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094void mbedtls_des3_free( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020095
96/**
Paul Bakker1f87fb62011-01-15 17:32:24 +000097 * \brief Set key parity on the given key to odd.
98 *
99 * DES keys are 56 bits long, but each byte is padded with
100 * a parity bit to allow verification.
101 *
102 * \param key 8-byte secret key
103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000105
106/**
107 * \brief Check that key parity on the given key is odd.
108 *
109 * DES keys are 56 bits long, but each byte is padded with
110 * a parity bit to allow verification.
111 *
112 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000113 *
114 * \return 0 is parity was ok, 1 if parity was not correct.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000117
Paul Bakker1f87fb62011-01-15 17:32:24 +0000118/**
119 * \brief Check that key is not a weak or semi-weak DES key
120 *
121 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000122 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000123 * \return 0 if no weak key was found, 1 if a weak key was identified.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000126
127/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 * \brief DES key schedule (56-bit, encryption)
129 *
130 * \param ctx DES context to be initialized
131 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000132 *
133 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
137/**
138 * \brief DES key schedule (56-bit, decryption)
139 *
140 * \param ctx DES context to be initialized
141 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000142 *
143 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147/**
148 * \brief Triple-DES key schedule (112-bit, encryption)
149 *
150 * \param ctx 3DES context to be initialized
151 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000152 *
153 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
156 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
158/**
159 * \brief Triple-DES key schedule (112-bit, decryption)
160 *
161 * \param ctx 3DES context to be initialized
162 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000163 *
164 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
167 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169/**
170 * \brief Triple-DES key schedule (168-bit, encryption)
171 *
172 * \param ctx 3DES context to be initialized
173 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000174 *
175 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
178 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
180/**
181 * \brief Triple-DES key schedule (168-bit, decryption)
182 *
183 * \param ctx 3DES context to be initialized
184 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000185 *
186 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
189 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
191/**
192 * \brief DES-ECB block encryption/decryption
193 *
194 * \param ctx DES context
195 * \param input 64-bit input block
196 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000197 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000198 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000201 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 unsigned char output[8] );
203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000205/**
206 * \brief DES-CBC buffer encryption/decryption
207 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000208 * \note Upon exit, the content of the IV is updated so that you can
209 * call the function same function again on the following
210 * block(s) of data and get the same result as if it was
211 * encrypted in one call. This allows a "streaming" usage.
212 * If on the other hand you need to retain the contents of the
213 * IV, you should either save it manually or use the cipher
214 * module instead.
215 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 * \param ctx DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 * \param length length of the input data
219 * \param iv initialization vector (updated after use)
220 * \param input buffer holding the input data
221 * \param output buffer holding the output data
222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000225 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000227 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
231/**
232 * \brief 3DES-ECB block encryption/decryption
233 *
234 * \param ctx 3DES context
235 * \param input 64-bit input block
236 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000237 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000238 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000241 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 unsigned char output[8] );
243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000245/**
246 * \brief 3DES-CBC buffer encryption/decryption
247 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000248 * \note Upon exit, the content of the IV is updated so that you can
249 * call the function same function again on the following
250 * block(s) of data and get the same result as if it was
251 * encrypted in one call. This allows a "streaming" usage.
252 * If on the other hand you need to retain the contents of the
253 * IV, you should either save it manually or use the cipher
254 * module instead.
255 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000256 * \param ctx 3DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 * \param length length of the input data
259 * \param iv initialization vector (updated after use)
260 * \param input buffer holding the input data
261 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000262 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000267 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000269 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000272
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200273/**
274 * \brief Internal function for key expansion.
275 * (Only exposed to allow overriding it,
276 * see MBEDTLS_DES_SETKEY_ALT)
277 *
278 * \param SK Round keys
279 * \param key Base key
280 */
281void mbedtls_des_setkey( uint32_t SK[32],
282 const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker90995b52013-06-24 19:20:35 +0200283#ifdef __cplusplus
284}
285#endif
286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287#else /* MBEDTLS_DES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200288#include "des_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289#endif /* MBEDTLS_DES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200290
291#ifdef __cplusplus
292extern "C" {
293#endif
294
Paul Bakker9a736322012-11-14 12:39:52 +0000295/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000296 * \brief Checkup routine
297 *
298 * \return 0 if successful, or 1 if the test failed
299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300int mbedtls_des_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000301
302#ifdef __cplusplus
303}
304#endif
305
306#endif /* des.h */