blob: 5a1a636522854da85cf6177bbc26ee29d29e6e5e [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
Hanno Beckerbbca8c52017-09-25 14:53:51 +01005 *
6 * \warning DES is considered a weak cipher and its use constitutes a
7 * security risk. We recommend considering stronger ciphers
8 * instead.
Darryl Greena40a1012018-01-05 15:33:17 +00009 */
10/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +020011 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 * 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 Bakkerb96f1542010-07-18 20:36:00 +000025 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000026 * This file is part of mbed TLS (https://tls.mbed.org)
Hanno Beckerbbca8c52017-09-25 14:53:51 +010027 *
Paul Bakker5121ce52009-01-03 21:22:43 +000028 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#ifndef MBEDTLS_DES_H
30#define MBEDTLS_DES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020033#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#endif
Paul Bakker90995b52013-06-24 19:20:35 +020037
Rich Evans00ab4702015-02-06 13:43:58 +000038#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020039#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#define MBEDTLS_DES_ENCRYPT 1
42#define MBEDTLS_DES_DECRYPT 0
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010045#define MBEDTLS_ERR_DES_HW_ACCEL_FAILED -0x0033 /**< DES hardware accelerator failed. */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#define MBEDTLS_DES_KEY_SIZE 8
Paul Bakker1f87fb62011-01-15 17:32:24 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_DES_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020050// Regular implementation
51//
52
Paul Bakker407a0da2013-06-27 14:29:21 +020053#ifdef __cplusplus
54extern "C" {
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057/**
58 * \brief DES context structure
Hanno Beckerbbca8c52017-09-25 14:53:51 +010059 *
60 * \warning DES is considered a weak cipher and its use constitutes a
61 * security risk. We recommend considering stronger ciphers
62 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +000063 */
64typedef struct
65{
Paul Bakker5c2364c2012-10-01 14:41:15 +000066 uint32_t sk[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000067}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068mbedtls_des_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000069
70/**
71 * \brief Triple-DES context structure
72 */
73typedef struct
74{
Paul Bakker5c2364c2012-10-01 14:41:15 +000075 uint32_t sk[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000076}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077mbedtls_des3_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000078
Paul Bakker5121ce52009-01-03 21:22:43 +000079/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020080 * \brief Initialize DES context
81 *
82 * \param ctx DES context to be initialized
Hanno Beckerbbca8c52017-09-25 14:53:51 +010083 *
84 * \warning DES is considered a weak cipher and its use constitutes a
85 * security risk. We recommend considering stronger ciphers
86 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088void mbedtls_des_init( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020089
90/**
91 * \brief Clear DES context
92 *
93 * \param ctx DES context to be cleared
Hanno Beckerbbca8c52017-09-25 14:53:51 +010094 *
95 * \warning DES is considered a weak cipher and its use constitutes a
96 * security risk. We recommend considering stronger ciphers
97 * instead.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020098 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099void mbedtls_des_free( mbedtls_des_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200100
101/**
102 * \brief Initialize Triple-DES context
103 *
104 * \param ctx DES3 context to be initialized
105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106void mbedtls_des3_init( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200107
108/**
109 * \brief Clear Triple-DES context
110 *
111 * \param ctx DES3 context to be cleared
112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113void mbedtls_des3_free( mbedtls_des3_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200114
115/**
Paul Bakker1f87fb62011-01-15 17:32:24 +0000116 * \brief Set key parity on the given key to odd.
117 *
118 * DES keys are 56 bits long, but each byte is padded with
119 * a parity bit to allow verification.
120 *
121 * \param key 8-byte secret key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100122 *
123 * \warning DES is considered a weak cipher and its use constitutes a
124 * security risk. We recommend considering stronger ciphers
125 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000128
129/**
130 * \brief Check that key parity on the given key is odd.
131 *
132 * DES keys are 56 bits long, but each byte is padded with
133 * a parity bit to allow verification.
134 *
135 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000136 *
137 * \return 0 is parity was ok, 1 if parity was not correct.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100138 *
139 * \warning DES is considered a weak cipher and its use constitutes a
140 * security risk. We recommend considering stronger ciphers
141 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000144
Paul Bakker1f87fb62011-01-15 17:32:24 +0000145/**
146 * \brief Check that key is not a weak or semi-weak DES key
147 *
148 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000149 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000150 * \return 0 if no weak key was found, 1 if a weak key was identified.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100151 *
152 * \warning DES is considered a weak cipher and its use constitutes a
153 * security risk. We recommend considering stronger ciphers
154 * instead.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000155 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker1f87fb62011-01-15 17:32:24 +0000157
158/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 * \brief DES key schedule (56-bit, encryption)
160 *
161 * \param ctx DES context to be initialized
162 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000163 *
164 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100165 *
166 * \warning DES is considered a weak cipher and its use constitutes a
167 * security risk. We recommend considering stronger ciphers
168 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
172/**
173 * \brief DES key schedule (56-bit, decryption)
174 *
175 * \param ctx DES context to be initialized
176 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000177 *
178 * \return 0
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100179 *
180 * \warning DES is considered a weak cipher and its use constitutes a
181 * security risk. We recommend considering stronger ciphers
182 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
186/**
187 * \brief Triple-DES key schedule (112-bit, encryption)
188 *
189 * \param ctx 3DES context to be initialized
190 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000191 *
192 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
195 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
197/**
198 * \brief Triple-DES key schedule (112-bit, decryption)
199 *
200 * \param ctx 3DES context to be initialized
201 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000202 *
203 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
206 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208/**
209 * \brief Triple-DES key schedule (168-bit, encryption)
210 *
211 * \param ctx 3DES context to be initialized
212 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000213 *
214 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
217 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000218
219/**
220 * \brief Triple-DES key schedule (168-bit, decryption)
221 *
222 * \param ctx 3DES context to be initialized
223 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000224 *
225 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
228 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000229
230/**
231 * \brief DES-ECB block encryption/decryption
232 *
233 * \param ctx DES context
234 * \param input 64-bit input block
235 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000236 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000237 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100238 *
239 * \warning DES is considered a weak cipher and its use constitutes a
240 * security risk. We recommend considering stronger ciphers
241 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000244 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 unsigned char output[8] );
246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000248/**
249 * \brief DES-CBC buffer encryption/decryption
250 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000251 * \note Upon exit, the content of the IV is updated so that you can
252 * call the function same function again on the following
253 * block(s) of data and get the same result as if it was
254 * encrypted in one call. This allows a "streaming" usage.
255 * If on the other hand you need to retain the contents of the
256 * IV, you should either save it manually or use the cipher
257 * module instead.
258 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000259 * \param ctx DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000261 * \param length length of the input data
262 * \param iv initialization vector (updated after use)
263 * \param input buffer holding the input data
264 * \param output buffer holding the output data
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100265 *
266 * \warning DES is considered a weak cipher and its use constitutes a
267 * security risk. We recommend considering stronger ciphers
268 * instead.
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000272 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000274 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000277
278/**
279 * \brief 3DES-ECB block encryption/decryption
280 *
281 * \param ctx 3DES context
282 * \param input 64-bit input block
283 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000284 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000285 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000288 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000289 unsigned char output[8] );
290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000292/**
293 * \brief 3DES-CBC buffer encryption/decryption
294 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000295 * \note Upon exit, the content of the IV is updated so that you can
296 * call the function same function again on the following
297 * block(s) of data and get the same result as if it was
298 * encrypted in one call. This allows a "streaming" usage.
299 * If on the other hand you need to retain the contents of the
300 * IV, you should either save it manually or use the cipher
301 * module instead.
302 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 * \param ctx 3DES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 * \param length length of the input data
306 * \param iv initialization vector (updated after use)
307 * \param input buffer holding the input data
308 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000309 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000314 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000316 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200320/**
321 * \brief Internal function for key expansion.
322 * (Only exposed to allow overriding it,
323 * see MBEDTLS_DES_SETKEY_ALT)
324 *
325 * \param SK Round keys
326 * \param key Base key
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100327 *
328 * \warning DES is considered a weak cipher and its use constitutes a
329 * security risk. We recommend considering stronger ciphers
330 * instead.
Manuel Pégourié-Gonnard70a50102015-05-12 15:02:45 +0200331 */
332void mbedtls_des_setkey( uint32_t SK[32],
333 const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
Paul Bakker90995b52013-06-24 19:20:35 +0200334#ifdef __cplusplus
335}
336#endif
337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338#else /* MBEDTLS_DES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200339#include "des_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#endif /* MBEDTLS_DES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200341
342#ifdef __cplusplus
343extern "C" {
344#endif
345
Paul Bakker9a736322012-11-14 12:39:52 +0000346/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000347 * \brief Checkup routine
348 *
349 * \return 0 if successful, or 1 if the test failed
350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351int mbedtls_des_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
353#ifdef __cplusplus
354}
355#endif
356
357#endif /* des.h */