blob: 1a09ad191b06339f6c3e00a8a3b540b9134f501b [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
29/**
30 * \brief DES context structure
31 */
32typedef struct
33{
34 int mode; /*!< encrypt/decrypt */
35 unsigned long sk[32]; /*!< DES subkeys */
36}
37des_context;
38
39/**
40 * \brief Triple-DES context structure
41 */
42typedef struct
43{
44 int mode; /*!< encrypt/decrypt */
45 unsigned long sk[96]; /*!< 3DES subkeys */
46}
47des3_context;
48
49#ifdef __cplusplus
50extern "C" {
51#endif
52
53/**
54 * \brief DES key schedule (56-bit, encryption)
55 *
56 * \param ctx DES context to be initialized
57 * \param key 8-byte secret key
58 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000059void des_setkey_enc( des_context *ctx, const unsigned char key[8] );
Paul Bakker5121ce52009-01-03 21:22:43 +000060
61/**
62 * \brief DES key schedule (56-bit, decryption)
63 *
64 * \param ctx DES context to be initialized
65 * \param key 8-byte secret key
66 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000067void des_setkey_dec( des_context *ctx, const unsigned char key[8] );
Paul Bakker5121ce52009-01-03 21:22:43 +000068
69/**
70 * \brief Triple-DES key schedule (112-bit, encryption)
71 *
72 * \param ctx 3DES context to be initialized
73 * \param key 16-byte secret key
74 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000075void des3_set2key_enc( des3_context *ctx, const unsigned char key[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +000076
77/**
78 * \brief Triple-DES key schedule (112-bit, decryption)
79 *
80 * \param ctx 3DES context to be initialized
81 * \param key 16-byte secret key
82 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000083void des3_set2key_dec( des3_context *ctx, const unsigned char key[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +000084
85/**
86 * \brief Triple-DES key schedule (168-bit, encryption)
87 *
88 * \param ctx 3DES context to be initialized
89 * \param key 24-byte secret key
90 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000091void des3_set3key_enc( des3_context *ctx, const unsigned char key[24] );
Paul Bakker5121ce52009-01-03 21:22:43 +000092
93/**
94 * \brief Triple-DES key schedule (168-bit, decryption)
95 *
96 * \param ctx 3DES context to be initialized
97 * \param key 24-byte secret key
98 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000099void des3_set3key_dec( des3_context *ctx, const unsigned char key[24] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
101/**
102 * \brief DES-ECB block encryption/decryption
103 *
104 * \param ctx DES context
105 * \param input 64-bit input block
106 * \param output 64-bit output block
107 */
108void des_crypt_ecb( des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000109 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 unsigned char output[8] );
111
112/**
113 * \brief DES-CBC buffer encryption/decryption
114 *
115 * \param ctx DES context
116 * \param mode DES_ENCRYPT or DES_DECRYPT
117 * \param length length of the input data
118 * \param iv initialization vector (updated after use)
119 * \param input buffer holding the input data
120 * \param output buffer holding the output data
121 */
122void des_crypt_cbc( des_context *ctx,
123 int mode,
124 int length,
125 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000126 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 unsigned char *output );
128
129/**
130 * \brief 3DES-ECB block encryption/decryption
131 *
132 * \param ctx 3DES context
133 * \param input 64-bit input block
134 * \param output 64-bit output block
135 */
136void des3_crypt_ecb( des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000137 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 unsigned char output[8] );
139
140/**
141 * \brief 3DES-CBC buffer encryption/decryption
142 *
143 * \param ctx 3DES context
144 * \param mode DES_ENCRYPT or DES_DECRYPT
145 * \param length length of the input data
146 * \param iv initialization vector (updated after use)
147 * \param input buffer holding the input data
148 * \param output buffer holding the output data
149 */
150void des3_crypt_cbc( des3_context *ctx,
151 int mode,
152 int length,
153 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000154 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 unsigned char *output );
156
157/*
158 * \brief Checkup routine
159 *
160 * \return 0 if successful, or 1 if the test failed
161 */
162int des_self_test( int verbose );
163
164#ifdef __cplusplus
165}
166#endif
167
168#endif /* des.h */