blob: 4a8e8ae30fc3703892dc886a2382cea44b75b339 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file des.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000024 */
Paul Bakker40e46942009-01-03 21:51:57 +000025#ifndef POLARSSL_DES_H
26#define POLARSSL_DES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000027
28#define DES_ENCRYPT 1
29#define DES_DECRYPT 0
30
Paul Bakkerf3ccc682010-03-18 21:21:02 +000031#define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0C00
32
Paul Bakker5121ce52009-01-03 21:22:43 +000033/**
34 * \brief DES context structure
35 */
36typedef struct
37{
38 int mode; /*!< encrypt/decrypt */
39 unsigned long sk[32]; /*!< DES subkeys */
40}
41des_context;
42
43/**
44 * \brief Triple-DES context structure
45 */
46typedef struct
47{
48 int mode; /*!< encrypt/decrypt */
49 unsigned long sk[96]; /*!< 3DES subkeys */
50}
51des3_context;
52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
57/**
58 * \brief DES key schedule (56-bit, encryption)
59 *
60 * \param ctx DES context to be initialized
61 * \param key 8-byte secret key
62 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000063void des_setkey_enc( des_context *ctx, const unsigned char key[8] );
Paul Bakker5121ce52009-01-03 21:22:43 +000064
65/**
66 * \brief DES key schedule (56-bit, decryption)
67 *
68 * \param ctx DES context to be initialized
69 * \param key 8-byte secret key
70 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000071void des_setkey_dec( des_context *ctx, const unsigned char key[8] );
Paul Bakker5121ce52009-01-03 21:22:43 +000072
73/**
74 * \brief Triple-DES key schedule (112-bit, encryption)
75 *
76 * \param ctx 3DES context to be initialized
77 * \param key 16-byte secret key
78 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000079void des3_set2key_enc( des3_context *ctx, const unsigned char key[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +000080
81/**
82 * \brief Triple-DES key schedule (112-bit, decryption)
83 *
84 * \param ctx 3DES context to be initialized
85 * \param key 16-byte secret key
86 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000087void des3_set2key_dec( des3_context *ctx, const unsigned char key[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +000088
89/**
90 * \brief Triple-DES key schedule (168-bit, encryption)
91 *
92 * \param ctx 3DES context to be initialized
93 * \param key 24-byte secret key
94 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000095void des3_set3key_enc( des3_context *ctx, const unsigned char key[24] );
Paul Bakker5121ce52009-01-03 21:22:43 +000096
97/**
98 * \brief Triple-DES key schedule (168-bit, decryption)
99 *
100 * \param ctx 3DES context to be initialized
101 * \param key 24-byte secret key
102 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000103void des3_set3key_dec( des3_context *ctx, const unsigned char key[24] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
105/**
106 * \brief DES-ECB block encryption/decryption
107 *
108 * \param ctx DES context
109 * \param input 64-bit input block
110 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000111 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000112 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000114int des_crypt_ecb( des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000115 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 unsigned char output[8] );
117
118/**
119 * \brief DES-CBC buffer encryption/decryption
120 *
121 * \param ctx DES context
122 * \param mode DES_ENCRYPT or DES_DECRYPT
123 * \param length length of the input data
124 * \param iv initialization vector (updated after use)
125 * \param input buffer holding the input data
126 * \param output buffer holding the output data
127 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000128int des_crypt_cbc( des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 int mode,
130 int length,
131 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000132 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 unsigned char *output );
134
135/**
136 * \brief 3DES-ECB block encryption/decryption
137 *
138 * \param ctx 3DES context
139 * \param input 64-bit input block
140 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000141 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000142 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000144int des3_crypt_ecb( des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000145 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 unsigned char output[8] );
147
148/**
149 * \brief 3DES-CBC buffer encryption/decryption
150 *
151 * \param ctx 3DES context
152 * \param mode DES_ENCRYPT or DES_DECRYPT
153 * \param length length of the input data
154 * \param iv initialization vector (updated after use)
155 * \param input buffer holding the input data
156 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000157 *
158 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000160int des3_crypt_cbc( des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 int mode,
162 int length,
163 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000164 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 unsigned char *output );
166
167/*
168 * \brief Checkup routine
169 *
170 * \return 0 if successful, or 1 if the test failed
171 */
172int des_self_test( int verbose );
173
174#ifdef __cplusplus
175}
176#endif
177
178#endif /* des.h */