blob: 1ca87c1c8fd58ac0d672a09200772c013ee4ba5e [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 *
Paul Bakker530927b2015-02-13 14:24:10 +01006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00008 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00009 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000023 */
Paul Bakker40e46942009-01-03 21:51:57 +000024#ifndef POLARSSL_DES_H
25#define POLARSSL_DES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Paul Bakker4087c472013-06-12 16:49:10 +020027#include "config.h"
28
Paul Bakker23986e52011-04-24 08:57:21 +000029#include <string.h>
30
Paul Bakker5c2364c2012-10-01 14:41:15 +000031#ifdef _MSC_VER
32#include <basetsd.h>
33typedef UINT32 uint32_t;
34#else
35#include <inttypes.h>
36#endif
37
Paul Bakker5121ce52009-01-03 21:22:43 +000038#define DES_ENCRYPT 1
39#define DES_DECRYPT 0
40
Paul Bakker9d781402011-05-09 16:17:09 +000041#define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000042
Paul Bakker1f87fb62011-01-15 17:32:24 +000043#define DES_KEY_SIZE 8
44
Paul Bakker4087c472013-06-12 16:49:10 +020045#if !defined(POLARSSL_DES_ALT)
46// Regular implementation
47//
48
Paul Bakker5121ce52009-01-03 21:22:43 +000049/**
50 * \brief DES context structure
51 */
52typedef struct
53{
54 int mode; /*!< encrypt/decrypt */
Paul Bakker5c2364c2012-10-01 14:41:15 +000055 uint32_t sk[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000056}
57des_context;
58
59/**
60 * \brief Triple-DES context structure
61 */
62typedef struct
63{
64 int mode; /*!< encrypt/decrypt */
Paul Bakker5c2364c2012-10-01 14:41:15 +000065 uint32_t sk[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000066}
67des3_context;
68
69#ifdef __cplusplus
70extern "C" {
71#endif
72
73/**
Paul Bakker1f87fb62011-01-15 17:32:24 +000074 * \brief Set key parity on the given key to odd.
75 *
76 * DES keys are 56 bits long, but each byte is padded with
77 * a parity bit to allow verification.
78 *
79 * \param key 8-byte secret key
80 */
81void des_key_set_parity( unsigned char key[DES_KEY_SIZE] );
82
83/**
84 * \brief Check that key parity on the given key is odd.
85 *
86 * DES keys are 56 bits long, but each byte is padded with
87 * a parity bit to allow verification.
88 *
89 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +000090 *
91 * \return 0 is parity was ok, 1 if parity was not correct.
Paul Bakker1f87fb62011-01-15 17:32:24 +000092 */
93int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] );
94
Paul Bakker1f87fb62011-01-15 17:32:24 +000095/**
96 * \brief Check that key is not a weak or semi-weak DES key
97 *
98 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +000099 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000100 * \return 0 if no weak key was found, 1 if a weak key was identified.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000101 */
102int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] );
103
104/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 * \brief DES key schedule (56-bit, encryption)
106 *
107 * \param ctx DES context to be initialized
108 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000109 *
110 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000112int des_setkey_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
114/**
115 * \brief DES key schedule (56-bit, decryption)
116 *
117 * \param ctx DES context to be initialized
118 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000119 *
120 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000122int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
124/**
125 * \brief Triple-DES key schedule (112-bit, encryption)
126 *
127 * \param ctx 3DES context to be initialized
128 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000129 *
130 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000132int des3_set2key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
134/**
135 * \brief Triple-DES key schedule (112-bit, decryption)
136 *
137 * \param ctx 3DES context to be initialized
138 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000139 *
140 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000142int des3_set2key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
144/**
145 * \brief Triple-DES key schedule (168-bit, encryption)
146 *
147 * \param ctx 3DES context to be initialized
148 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000149 *
150 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000152int des3_set3key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
154/**
155 * \brief Triple-DES key schedule (168-bit, decryption)
156 *
157 * \param ctx 3DES context to be initialized
158 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000159 *
160 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000162int des3_set3key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
164/**
165 * \brief DES-ECB block encryption/decryption
166 *
167 * \param ctx DES context
168 * \param input 64-bit input block
169 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000170 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000171 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000173int des_crypt_ecb( des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000174 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 unsigned char output[8] );
176
177/**
178 * \brief DES-CBC buffer encryption/decryption
179 *
180 * \param ctx DES context
181 * \param mode DES_ENCRYPT or DES_DECRYPT
182 * \param length length of the input data
183 * \param iv initialization vector (updated after use)
184 * \param input buffer holding the input data
185 * \param output buffer holding the output data
186 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000187int des_crypt_cbc( des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000189 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000191 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 unsigned char *output );
193
194/**
195 * \brief 3DES-ECB block encryption/decryption
196 *
197 * \param ctx 3DES context
198 * \param input 64-bit input block
199 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000200 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000201 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000203int des3_crypt_ecb( des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000204 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 unsigned char output[8] );
206
207/**
208 * \brief 3DES-CBC buffer encryption/decryption
209 *
210 * \param ctx 3DES context
211 * \param mode DES_ENCRYPT or DES_DECRYPT
212 * \param length length of the input data
213 * \param iv initialization vector (updated after use)
214 * \param input buffer holding the input data
215 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000216 *
217 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000219int des3_crypt_cbc( des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000221 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000223 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 unsigned char *output );
225
Paul Bakker4087c472013-06-12 16:49:10 +0200226#ifdef __cplusplus
227}
228#endif
229
230#else /* POLARSSL_DES_ALT */
231#include "des_alt.h"
232#endif /* POLARSSL_DES_ALT */
233
234#ifdef __cplusplus
235extern "C" {
236#endif
237
Paul Bakker9a736322012-11-14 12:39:52 +0000238/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 * \brief Checkup routine
240 *
241 * \return 0 if successful, or 1 if the test failed
242 */
243int des_self_test( int verbose );
244
245#ifdef __cplusplus
246}
247#endif
248
249#endif /* des.h */