blob: 74d5acb12355ecbe920de9a3a1253be390bfd923 [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 Bakker90995b52013-06-24 19:20:35 +02006 * Copyright (C) 2006-2013, 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 Bakker90995b52013-06-24 19:20:35 +020030#include "config.h"
31
Paul Bakker23986e52011-04-24 08:57:21 +000032#include <string.h>
33
Paul Bakker5c2364c2012-10-01 14:41:15 +000034#ifdef _MSC_VER
35#include <basetsd.h>
36typedef UINT32 uint32_t;
37#else
38#include <inttypes.h>
39#endif
40
Paul Bakker5121ce52009-01-03 21:22:43 +000041#define DES_ENCRYPT 1
42#define DES_DECRYPT 0
43
Paul Bakker9d781402011-05-09 16:17:09 +000044#define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000045
Paul Bakker1f87fb62011-01-15 17:32:24 +000046#define DES_KEY_SIZE 8
47
Paul Bakker90995b52013-06-24 19:20:35 +020048#if !defined(POLARSSL_DES_ALT)
49// Regular implementation
50//
51
Paul Bakker407a0da2013-06-27 14:29:21 +020052#ifdef __cplusplus
53extern "C" {
54#endif
55
Paul Bakker5121ce52009-01-03 21:22:43 +000056/**
57 * \brief DES context structure
58 */
59typedef struct
60{
61 int mode; /*!< encrypt/decrypt */
Paul Bakker5c2364c2012-10-01 14:41:15 +000062 uint32_t sk[32]; /*!< DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000063}
64des_context;
65
66/**
67 * \brief Triple-DES context structure
68 */
69typedef struct
70{
71 int mode; /*!< encrypt/decrypt */
Paul Bakker5c2364c2012-10-01 14:41:15 +000072 uint32_t sk[96]; /*!< 3DES subkeys */
Paul Bakker5121ce52009-01-03 21:22:43 +000073}
74des3_context;
75
Paul Bakker5121ce52009-01-03 21:22:43 +000076/**
Paul Bakker1f87fb62011-01-15 17:32:24 +000077 * \brief Set key parity on the given key to odd.
78 *
79 * DES keys are 56 bits long, but each byte is padded with
80 * a parity bit to allow verification.
81 *
82 * \param key 8-byte secret key
83 */
84void des_key_set_parity( unsigned char key[DES_KEY_SIZE] );
85
86/**
87 * \brief Check that key parity on the given key is odd.
88 *
89 * DES keys are 56 bits long, but each byte is padded with
90 * a parity bit to allow verification.
91 *
92 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +000093 *
94 * \return 0 is parity was ok, 1 if parity was not correct.
Paul Bakker1f87fb62011-01-15 17:32:24 +000095 */
96int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] );
97
Paul Bakker1f87fb62011-01-15 17:32:24 +000098/**
99 * \brief Check that key is not a weak or semi-weak DES key
100 *
101 * \param key 8-byte secret key
Paul Bakker73206952011-07-06 14:37:33 +0000102 *
Paul Bakker4793cc42011-08-17 09:40:55 +0000103 * \return 0 if no weak key was found, 1 if a weak key was identified.
Paul Bakker1f87fb62011-01-15 17:32:24 +0000104 */
105int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] );
106
107/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 * \brief DES key schedule (56-bit, encryption)
109 *
110 * \param ctx DES context to be initialized
111 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000112 *
113 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000115int des_setkey_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
117/**
118 * \brief DES key schedule (56-bit, decryption)
119 *
120 * \param ctx DES context to be initialized
121 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000122 *
123 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000125int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
127/**
128 * \brief Triple-DES key schedule (112-bit, encryption)
129 *
130 * \param ctx 3DES context to be initialized
131 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000132 *
133 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000135int des3_set2key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
137/**
138 * \brief Triple-DES key schedule (112-bit, decryption)
139 *
140 * \param ctx 3DES context to be initialized
141 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000142 *
143 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000145int des3_set2key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147/**
148 * \brief Triple-DES key schedule (168-bit, encryption)
149 *
150 * \param ctx 3DES context to be initialized
151 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000152 *
153 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000155int des3_set3key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157/**
158 * \brief Triple-DES key schedule (168-bit, decryption)
159 *
160 * \param ctx 3DES context to be initialized
161 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000162 *
163 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000165int des3_set3key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
167/**
168 * \brief DES-ECB block encryption/decryption
169 *
170 * \param ctx DES context
171 * \param input 64-bit input block
172 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000173 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000174 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000176int des_crypt_ecb( des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000177 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 unsigned char output[8] );
179
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200180#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000181/**
182 * \brief DES-CBC buffer encryption/decryption
183 *
184 * \param ctx DES context
185 * \param mode DES_ENCRYPT or DES_DECRYPT
186 * \param length length of the input data
187 * \param iv initialization vector (updated after use)
188 * \param input buffer holding the input data
189 * \param output buffer holding the output data
190 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000191int des_crypt_cbc( des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000193 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000195 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200197#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
199/**
200 * \brief 3DES-ECB block encryption/decryption
201 *
202 * \param ctx 3DES context
203 * \param input 64-bit input block
204 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000205 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000206 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000208int des3_crypt_ecb( des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000209 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 unsigned char output[8] );
211
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200212#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000213/**
214 * \brief 3DES-CBC buffer encryption/decryption
215 *
216 * \param ctx 3DES context
217 * \param mode DES_ENCRYPT or DES_DECRYPT
218 * \param length length of the input data
219 * \param iv initialization vector (updated after use)
220 * \param input buffer holding the input data
221 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000222 *
223 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000225int des3_crypt_cbc( des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000227 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000229 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200231#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
Paul Bakker90995b52013-06-24 19:20:35 +0200233#ifdef __cplusplus
234}
235#endif
236
237#else /* POLARSSL_DES_ALT */
238#include "des_alt.h"
239#endif /* POLARSSL_DES_ALT */
240
241#ifdef __cplusplus
242extern "C" {
243#endif
244
Paul Bakker9a736322012-11-14 12:39:52 +0000245/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 * \brief Checkup routine
247 *
248 * \return 0 if successful, or 1 if the test failed
249 */
250int des_self_test( int verbose );
251
252#ifdef __cplusplus
253}
254#endif
255
256#endif /* des.h */