blob: 05423294de2e2e1df92b3a8cb33a7c2be2070c89 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file des.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief Debug functions
5 *
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
30#define DES_ENCRYPT 1
31#define DES_DECRYPT 0
32
Paul Bakkerf3ccc682010-03-18 21:21:02 +000033#define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0C00
34
Paul Bakker1f87fb62011-01-15 17:32:24 +000035#define DES_KEY_SIZE 8
36
Paul Bakker5121ce52009-01-03 21:22:43 +000037/**
38 * \brief DES context structure
39 */
40typedef struct
41{
42 int mode; /*!< encrypt/decrypt */
43 unsigned long sk[32]; /*!< DES subkeys */
44}
45des_context;
46
47/**
48 * \brief Triple-DES context structure
49 */
50typedef struct
51{
52 int mode; /*!< encrypt/decrypt */
53 unsigned long sk[96]; /*!< 3DES subkeys */
54}
55des3_context;
56
57#ifdef __cplusplus
58extern "C" {
59#endif
60
61/**
Paul Bakker1f87fb62011-01-15 17:32:24 +000062 * \brief Set key parity on the given key to odd.
63 *
64 * DES keys are 56 bits long, but each byte is padded with
65 * a parity bit to allow verification.
66 *
67 * \param key 8-byte secret key
68 */
69void des_key_set_parity( unsigned char key[DES_KEY_SIZE] );
70
71/**
72 * \brief Check that key parity on the given key is odd.
73 *
74 * DES keys are 56 bits long, but each byte is padded with
75 * a parity bit to allow verification.
76 *
77 * \param key 8-byte secret key
78 */
79int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] );
80
81
82/**
83 * \brief Check that key is not a weak or semi-weak DES key
84 *
85 * \param key 8-byte secret key
86 */
87int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] );
88
89/**
Paul Bakker5121ce52009-01-03 21:22:43 +000090 * \brief DES key schedule (56-bit, encryption)
91 *
92 * \param ctx DES context to be initialized
93 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +000094 *
95 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +000096 */
Paul Bakker1f87fb62011-01-15 17:32:24 +000097int des_setkey_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +000098
99/**
100 * \brief DES key schedule (56-bit, decryption)
101 *
102 * \param ctx DES context to be initialized
103 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000104 *
105 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000107int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
109/**
110 * \brief Triple-DES key schedule (112-bit, encryption)
111 *
112 * \param ctx 3DES context to be initialized
113 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000114 *
115 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000117int des3_set2key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
119/**
120 * \brief Triple-DES key schedule (112-bit, decryption)
121 *
122 * \param ctx 3DES context to be initialized
123 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000124 *
125 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000127int des3_set2key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
129/**
130 * \brief Triple-DES key schedule (168-bit, encryption)
131 *
132 * \param ctx 3DES context to be initialized
133 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000134 *
135 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000137int des3_set3key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
139/**
140 * \brief Triple-DES key schedule (168-bit, decryption)
141 *
142 * \param ctx 3DES context to be initialized
143 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000144 *
145 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000147int des3_set3key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
149/**
150 * \brief DES-ECB block encryption/decryption
151 *
152 * \param ctx DES context
153 * \param input 64-bit input block
154 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000155 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000156 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000158int des_crypt_ecb( des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000159 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 unsigned char output[8] );
161
162/**
163 * \brief DES-CBC buffer encryption/decryption
164 *
165 * \param ctx DES context
166 * \param mode DES_ENCRYPT or DES_DECRYPT
167 * \param length length of the input data
168 * \param iv initialization vector (updated after use)
169 * \param input buffer holding the input data
170 * \param output buffer holding the output data
171 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000172int des_crypt_cbc( des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 int mode,
174 int length,
175 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000176 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 unsigned char *output );
178
179/**
180 * \brief 3DES-ECB block encryption/decryption
181 *
182 * \param ctx 3DES context
183 * \param input 64-bit input block
184 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000185 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000186 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000188int des3_crypt_ecb( des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000189 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 unsigned char output[8] );
191
192/**
193 * \brief 3DES-CBC buffer encryption/decryption
194 *
195 * \param ctx 3DES context
196 * \param mode DES_ENCRYPT or DES_DECRYPT
197 * \param length length of the input data
198 * \param iv initialization vector (updated after use)
199 * \param input buffer holding the input data
200 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000201 *
202 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000204int des3_crypt_cbc( des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 int mode,
206 int length,
207 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000208 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 unsigned char *output );
210
211/*
212 * \brief Checkup routine
213 *
214 * \return 0 if successful, or 1 if the test failed
215 */
216int des_self_test( int verbose );
217
218#ifdef __cplusplus
219}
220#endif
221
222#endif /* des.h */