blob: 04fdf671512c6128c8b4dbd22481823035f3122e [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file des.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker77b385e2009-07-28 17:23:11 +00004 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
5 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakker77b385e2009-07-28 17:23:11 +00007 * Joined copyright on original XySSL code with: Christophe Devine
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Paul Bakker40e46942009-01-03 21:51:57 +000023#ifndef POLARSSL_DES_H
24#define POLARSSL_DES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
26#define DES_ENCRYPT 1
27#define DES_DECRYPT 0
28
Paul Bakkerf3ccc682010-03-18 21:21:02 +000029#define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0C00
30
Paul Bakker5121ce52009-01-03 21:22:43 +000031/**
32 * \brief DES context structure
33 */
34typedef struct
35{
36 int mode; /*!< encrypt/decrypt */
37 unsigned long sk[32]; /*!< DES subkeys */
38}
39des_context;
40
41/**
42 * \brief Triple-DES context structure
43 */
44typedef struct
45{
46 int mode; /*!< encrypt/decrypt */
47 unsigned long sk[96]; /*!< 3DES subkeys */
48}
49des3_context;
50
51#ifdef __cplusplus
52extern "C" {
53#endif
54
55/**
56 * \brief DES key schedule (56-bit, encryption)
57 *
58 * \param ctx DES context to be initialized
59 * \param key 8-byte secret key
60 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000061void des_setkey_enc( des_context *ctx, const unsigned char key[8] );
Paul Bakker5121ce52009-01-03 21:22:43 +000062
63/**
64 * \brief DES key schedule (56-bit, decryption)
65 *
66 * \param ctx DES context to be initialized
67 * \param key 8-byte secret key
68 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000069void des_setkey_dec( des_context *ctx, const unsigned char key[8] );
Paul Bakker5121ce52009-01-03 21:22:43 +000070
71/**
72 * \brief Triple-DES key schedule (112-bit, encryption)
73 *
74 * \param ctx 3DES context to be initialized
75 * \param key 16-byte secret key
76 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000077void des3_set2key_enc( des3_context *ctx, const unsigned char key[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +000078
79/**
80 * \brief Triple-DES key schedule (112-bit, decryption)
81 *
82 * \param ctx 3DES context to be initialized
83 * \param key 16-byte secret key
84 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000085void des3_set2key_dec( des3_context *ctx, const unsigned char key[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +000086
87/**
88 * \brief Triple-DES key schedule (168-bit, encryption)
89 *
90 * \param ctx 3DES context to be initialized
91 * \param key 24-byte secret key
92 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000093void des3_set3key_enc( des3_context *ctx, const unsigned char key[24] );
Paul Bakker5121ce52009-01-03 21:22:43 +000094
95/**
96 * \brief Triple-DES key schedule (168-bit, decryption)
97 *
98 * \param ctx 3DES context to be initialized
99 * \param key 24-byte secret key
100 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000101void des3_set3key_dec( des3_context *ctx, const unsigned char key[24] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
103/**
104 * \brief DES-ECB block encryption/decryption
105 *
106 * \param ctx DES context
107 * \param input 64-bit input block
108 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000109 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000110 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000112int des_crypt_ecb( des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000113 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 unsigned char output[8] );
115
116/**
117 * \brief DES-CBC buffer encryption/decryption
118 *
119 * \param ctx DES context
120 * \param mode DES_ENCRYPT or DES_DECRYPT
121 * \param length length of the input data
122 * \param iv initialization vector (updated after use)
123 * \param input buffer holding the input data
124 * \param output buffer holding the output data
125 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000126int des_crypt_cbc( des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 int mode,
128 int length,
129 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000130 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 unsigned char *output );
132
133/**
134 * \brief 3DES-ECB block encryption/decryption
135 *
136 * \param ctx 3DES context
137 * \param input 64-bit input block
138 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000139 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000140 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000142int des3_crypt_ecb( des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000143 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 unsigned char output[8] );
145
146/**
147 * \brief 3DES-CBC buffer encryption/decryption
148 *
149 * \param ctx 3DES context
150 * \param mode DES_ENCRYPT or DES_DECRYPT
151 * \param length length of the input data
152 * \param iv initialization vector (updated after use)
153 * \param input buffer holding the input data
154 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000155 *
156 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000158int des3_crypt_cbc( des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 int mode,
160 int length,
161 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000162 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 unsigned char *output );
164
165/*
166 * \brief Checkup routine
167 *
168 * \return 0 if successful, or 1 if the test failed
169 */
170int des_self_test( int verbose );
171
172#ifdef __cplusplus
173}
174#endif
175
176#endif /* des.h */