blob: f9e8e60bf3f4fefee4792d2ba5cad2d10e03d40a [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 Bakker84f12b72010-07-18 10:13:04 +00006 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000026 */
Paul Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_DES_H
28#define POLARSSL_DES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker23986e52011-04-24 08:57:21 +000030#include <string.h>
31
Paul Bakker5121ce52009-01-03 21:22:43 +000032#define DES_ENCRYPT 1
33#define DES_DECRYPT 0
34
Paul Bakkerf3ccc682010-03-18 21:21:02 +000035#define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0C00
36
Paul Bakker1f87fb62011-01-15 17:32:24 +000037#define DES_KEY_SIZE 8
38
Paul Bakker5121ce52009-01-03 21:22:43 +000039/**
40 * \brief DES context structure
41 */
42typedef struct
43{
44 int mode; /*!< encrypt/decrypt */
45 unsigned long sk[32]; /*!< DES subkeys */
46}
47des_context;
48
49/**
50 * \brief Triple-DES context structure
51 */
52typedef struct
53{
54 int mode; /*!< encrypt/decrypt */
55 unsigned long sk[96]; /*!< 3DES subkeys */
56}
57des3_context;
58
59#ifdef __cplusplus
60extern "C" {
61#endif
62
63/**
Paul Bakker1f87fb62011-01-15 17:32:24 +000064 * \brief Set key parity on the given key to odd.
65 *
66 * DES keys are 56 bits long, but each byte is padded with
67 * a parity bit to allow verification.
68 *
69 * \param key 8-byte secret key
70 */
71void des_key_set_parity( unsigned char key[DES_KEY_SIZE] );
72
73/**
74 * \brief Check that key parity on the given key is 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 */
81int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] );
82
83
84/**
85 * \brief Check that key is not a weak or semi-weak DES key
86 *
87 * \param key 8-byte secret key
88 */
89int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] );
90
91/**
Paul Bakker5121ce52009-01-03 21:22:43 +000092 * \brief DES key schedule (56-bit, encryption)
93 *
94 * \param ctx DES context to be initialized
95 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +000096 *
97 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +000098 */
Paul Bakker1f87fb62011-01-15 17:32:24 +000099int des_setkey_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
101/**
102 * \brief DES key schedule (56-bit, decryption)
103 *
104 * \param ctx DES context to be initialized
105 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000106 *
107 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000109int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
111/**
112 * \brief Triple-DES key schedule (112-bit, encryption)
113 *
114 * \param ctx 3DES context to be initialized
115 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000116 *
117 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000119int des3_set2key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121/**
122 * \brief Triple-DES key schedule (112-bit, decryption)
123 *
124 * \param ctx 3DES context to be initialized
125 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000126 *
127 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000129int des3_set2key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
131/**
132 * \brief Triple-DES key schedule (168-bit, encryption)
133 *
134 * \param ctx 3DES context to be initialized
135 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000136 *
137 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000139int des3_set3key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141/**
142 * \brief Triple-DES key schedule (168-bit, decryption)
143 *
144 * \param ctx 3DES context to be initialized
145 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000146 *
147 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000149int des3_set3key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151/**
152 * \brief DES-ECB block encryption/decryption
153 *
154 * \param ctx DES context
155 * \param input 64-bit input block
156 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000157 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000158 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000160int des_crypt_ecb( des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000161 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 unsigned char output[8] );
163
164/**
165 * \brief DES-CBC buffer encryption/decryption
166 *
167 * \param ctx DES context
168 * \param mode DES_ENCRYPT or DES_DECRYPT
169 * \param length length of the input data
170 * \param iv initialization vector (updated after use)
171 * \param input buffer holding the input data
172 * \param output buffer holding the output data
173 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000174int des_crypt_cbc( des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000176 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000178 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 unsigned char *output );
180
181/**
182 * \brief 3DES-ECB block encryption/decryption
183 *
184 * \param ctx 3DES context
185 * \param input 64-bit input block
186 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000187 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000188 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000190int des3_crypt_ecb( des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000191 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 unsigned char output[8] );
193
194/**
195 * \brief 3DES-CBC buffer encryption/decryption
196 *
197 * \param ctx 3DES context
198 * \param mode DES_ENCRYPT or DES_DECRYPT
199 * \param length length of the input data
200 * \param iv initialization vector (updated after use)
201 * \param input buffer holding the input data
202 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000203 *
204 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000206int des3_crypt_cbc( des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000208 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000210 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 unsigned char *output );
212
213/*
214 * \brief Checkup routine
215 *
216 * \return 0 if successful, or 1 if the test failed
217 */
218int des_self_test( int verbose );
219
220#ifdef __cplusplus
221}
222#endif
223
224#endif /* des.h */