blob: 175289850657559afed34b1f9d7a1c4f5f5ada7d [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
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000021 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_DES_H
25#define MBEDTLS_DES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker90995b52013-06-24 19:20:35 +020032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020034#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#define MBEDTLS_DES_ENCRYPT 1
37#define MBEDTLS_DES_DECRYPT 0
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010040#define MBEDTLS_ERR_DES_HW_ACCEL_FAILED -0x0033 /**< DES hardware accelerator failed. */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define MBEDTLS_DES_KEY_SIZE 8
Paul Bakker1f87fb62011-01-15 17:32:24 +000043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if !defined(MBEDTLS_DES_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020045// Regular implementation
46//
47
Paul Bakker407a0da2013-06-27 14:29:21 +020048#ifdef __cplusplus
49extern "C" {
50#endif
51
Paul Bakker5121ce52009-01-03 21:22:43 +000052/**
53 * \brief DES context structure
54 */
55typedef struct
56{
Paul Bakker5c2364c2012-10-01 14:41:15 +000057 uint32_t sk[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000058}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059mbedtls_des_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000060
61/**
62 * \brief Triple-DES context structure
63 */
64typedef struct
65{
Paul Bakker5c2364c2012-10-01 14:41:15 +000066 uint32_t sk[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000067}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068mbedtls_des3_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Paul Bakker5121ce52009-01-03 21:22:43 +000070/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020071 * \brief Initialize DES context
72 *
73 * \param ctx DES context to be initialized
74 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075void mbedtls_des_init( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020076
77/**
78 * \brief Clear DES context
79 *
80 * \param ctx DES context to be cleared
81 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082void mbedtls_des_free( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020083
84/**
85 * \brief Initialize Triple-DES context
86 *
87 * \param ctx DES3 context to be initialized
88 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089void mbedtls_des3_init( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020090
91/**
92 * \brief Clear Triple-DES context
93 *
94 * \param ctx DES3 context to be cleared
95 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096void mbedtls_des3_free( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020097
98/**
Paul Bakker1f87fb62011-01-15 17:32:24 +000099 * \brief Set key parity on the given key to odd.
100 *
101 * DES keys are 56 bits long, but each byte is padded with
102 * a parity bit to allow verification.
103 *
104 * \param key 8-byte secret key
105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000107
108/**
109 * \brief Check that key parity on the given key is odd.
110 *
111 * DES keys are 56 bits long, but each byte is padded with
112 * a parity bit to allow verification.
113 *
114 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000115 *
116 * \return 0 is parity was ok, 1 if parity was not correct.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000119
Paul Bakker1f87fb62011-01-15 17:32:24 +0000120/**
121 * \brief Check that key is not a weak or semi-weak DES key
122 *
123 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000124 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000125 * \return 0 if no weak key was found, 1 if a weak key was identified.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000128
129/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 * \brief DES key schedule (56-bit, encryption)
131 *
132 * \param ctx DES context to be initialized
133 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000134 *
135 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
139/**
140 * \brief DES key schedule (56-bit, decryption)
141 *
142 * \param ctx DES context to be initialized
143 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000144 *
145 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
149/**
150 * \brief Triple-DES key schedule (112-bit, encryption)
151 *
152 * \param ctx 3DES context to be initialized
153 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000154 *
155 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
158 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
160/**
161 * \brief Triple-DES key schedule (112-bit, decryption)
162 *
163 * \param ctx 3DES context to be initialized
164 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000165 *
166 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
169 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
171/**
172 * \brief Triple-DES key schedule (168-bit, encryption)
173 *
174 * \param ctx 3DES context to be initialized
175 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000176 *
177 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
180 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
182/**
183 * \brief Triple-DES key schedule (168-bit, decryption)
184 *
185 * \param ctx 3DES context to be initialized
186 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000187 *
188 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
191 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000192
193/**
194 * \brief DES-ECB block encryption/decryption
195 *
196 * \param ctx DES context
197 * \param input 64-bit input block
198 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000199 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000200 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000203 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 unsigned char output[8] );
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000207/**
208 * \brief DES-CBC buffer encryption/decryption
209 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000210 * \note Upon exit, the content of the IV is updated so that you can
211 * call the function same function again on the following
212 * block(s) of data and get the same result as if it was
213 * encrypted in one call. This allows a "streaming" usage.
214 * If on the other hand you need to retain the contents of the
215 * IV, you should either save it manually or use the cipher
216 * module instead.
217 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 * \param ctx DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 * \param length length of the input data
221 * \param iv initialization vector (updated after use)
222 * \param input buffer holding the input data
223 * \param output buffer holding the output data
224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000227 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000229 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
233/**
234 * \brief 3DES-ECB block encryption/decryption
235 *
236 * \param ctx 3DES context
237 * \param input 64-bit input block
238 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000239 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000240 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000243 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 unsigned char output[8] );
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000247/**
248 * \brief 3DES-CBC buffer encryption/decryption
249 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000250 * \note Upon exit, the content of the IV is updated so that you can
251 * call the function same function again on the following
252 * block(s) of data and get the same result as if it was
253 * encrypted in one call. This allows a "streaming" usage.
254 * If on the other hand you need to retain the contents of the
255 * IV, you should either save it manually or use the cipher
256 * module instead.
257 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 * \param ctx 3DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 * \param length length of the input data
261 * \param iv initialization vector (updated after use)
262 * \param input buffer holding the input data
263 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000264 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000269 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000271 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200275/**
276 * \brief Internal function for key expansion.
277 * (Only exposed to allow overriding it,
278 * see MBEDTLS_DES_SETKEY_ALT)
279 *
280 * \param SK Round keys
281 * \param key Base key
282 */
283void mbedtls_des_setkey( uint32_t SK[32],
284 const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker90995b52013-06-24 19:20:35 +0200285#ifdef __cplusplus
286}
287#endif
288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289#else /* MBEDTLS_DES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200290#include "des_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291#endif /* MBEDTLS_DES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200292
293#ifdef __cplusplus
294extern "C" {
295#endif
296
Paul Bakker9a736322012-11-14 12:39:52 +0000297/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 * \brief Checkup routine
299 *
300 * \return 0 if successful, or 1 if the test failed
301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302int mbedtls_des_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000303
304#ifdef __cplusplus
305}
306#endif
307
308#endif /* des.h */