blob: b649ccf207d1b62961033377e8b42804e3b8ab6b [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 Bakker5c2364c2012-10-01 14:41:15 +000032#ifdef _MSC_VER
33#include <basetsd.h>
34typedef UINT32 uint32_t;
35#else
36#include <inttypes.h>
37#endif
38
Paul Bakker5121ce52009-01-03 21:22:43 +000039#define DES_ENCRYPT 1
40#define DES_DECRYPT 0
41
Paul Bakker9d781402011-05-09 16:17:09 +000042#define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000043
Paul Bakker1f87fb62011-01-15 17:32:24 +000044#define DES_KEY_SIZE 8
45
Paul Bakker5121ce52009-01-03 21:22:43 +000046/**
47 * \brief DES context structure
48 */
49typedef struct
50{
51 int mode; /*!< encrypt/decrypt */
Paul Bakker5c2364c2012-10-01 14:41:15 +000052 uint32_t sk[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000053}
54des_context;
55
56/**
57 * \brief Triple-DES context structure
58 */
59typedef struct
60{
61 int mode; /*!< encrypt/decrypt */
Paul Bakker5c2364c2012-10-01 14:41:15 +000062 uint32_t sk[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000063}
64des3_context;
65
66#ifdef __cplusplus
67extern "C" {
68#endif
69
70/**
Paul Bakker1f87fb62011-01-15 17:32:24 +000071 * \brief Set key parity on the given key to odd.
72 *
73 * DES keys are 56 bits long, but each byte is padded with
74 * a parity bit to allow verification.
75 *
76 * \param key 8-byte secret key
77 */
78void des_key_set_parity( unsigned char key[DES_KEY_SIZE] );
79
80/**
81 * \brief Check that key parity on the given key is odd.
82 *
83 * DES keys are 56 bits long, but each byte is padded with
84 * a parity bit to allow verification.
85 *
86 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +000087 *
88 * \return 0 is parity was ok, 1 if parity was not correct.
Paul Bakker1f87fb62011-01-15 17:32:24 +000089 */
90int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] );
91
Paul Bakker1f87fb62011-01-15 17:32:24 +000092/**
93 * \brief Check that key is not a weak or semi-weak DES key
94 *
95 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +000096 *
Paul Bakker4793cc42011-08-17 09:40:55 +000097 * \return 0 if no weak key was found, 1 if a weak key was identified.
Paul Bakker1f87fb62011-01-15 17:32:24 +000098 */
99int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] );
100
101/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 * \brief DES key schedule (56-bit, encryption)
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_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
111/**
112 * \brief DES key schedule (56-bit, decryption)
113 *
114 * \param ctx DES context to be initialized
115 * \param key 8-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 des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121/**
122 * \brief Triple-DES key schedule (112-bit, encryption)
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_enc( 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 (112-bit, decryption)
133 *
134 * \param ctx 3DES context to be initialized
135 * \param key 16-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_set2key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141/**
142 * \brief Triple-DES key schedule (168-bit, encryption)
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_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151/**
152 * \brief Triple-DES key schedule (168-bit, decryption)
153 *
154 * \param ctx 3DES context to be initialized
155 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000156 *
157 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000159int des3_set3key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
161/**
162 * \brief DES-ECB block encryption/decryption
163 *
164 * \param ctx DES context
165 * \param input 64-bit input block
166 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000167 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000168 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000170int des_crypt_ecb( des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000171 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 unsigned char output[8] );
173
174/**
175 * \brief DES-CBC buffer encryption/decryption
176 *
177 * \param ctx DES context
178 * \param mode DES_ENCRYPT or DES_DECRYPT
179 * \param length length of the input data
180 * \param iv initialization vector (updated after use)
181 * \param input buffer holding the input data
182 * \param output buffer holding the output data
183 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000184int des_crypt_cbc( des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000186 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000188 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 unsigned char *output );
190
191/**
192 * \brief 3DES-ECB block encryption/decryption
193 *
194 * \param ctx 3DES context
195 * \param input 64-bit input block
196 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000197 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000198 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000200int des3_crypt_ecb( des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000201 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 unsigned char output[8] );
203
204/**
205 * \brief 3DES-CBC buffer encryption/decryption
206 *
207 * \param ctx 3DES context
208 * \param mode DES_ENCRYPT or DES_DECRYPT
209 * \param length length of the input data
210 * \param iv initialization vector (updated after use)
211 * \param input buffer holding the input data
212 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000213 *
214 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000216int des3_crypt_cbc( des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000218 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000220 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 unsigned char *output );
222
223/*
224 * \brief Checkup routine
225 *
226 * \return 0 if successful, or 1 if the test failed
227 */
228int des_self_test( int verbose );
229
230#ifdef __cplusplus
231}
232#endif
233
234#endif /* des.h */