blob: 172877e2685265976169fcf3245c8c8c4ca5b1ba [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file des.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker83ded912010-03-21 17:46:26 +00004 * Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00005 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
Paul Bakker40e46942009-01-03 21:51:57 +000021#ifndef POLARSSL_DES_H
22#define POLARSSL_DES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000023
24#define DES_ENCRYPT 1
25#define DES_DECRYPT 0
26
Paul Bakkerf3ccc682010-03-18 21:21:02 +000027#define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0C00
28
Paul Bakker5121ce52009-01-03 21:22:43 +000029/**
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
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000107 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000108 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000110int des_crypt_ecb( des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000111 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 unsigned char output[8] );
113
114/**
115 * \brief DES-CBC buffer encryption/decryption
116 *
117 * \param ctx DES context
118 * \param mode DES_ENCRYPT or DES_DECRYPT
119 * \param length length of the input data
120 * \param iv initialization vector (updated after use)
121 * \param input buffer holding the input data
122 * \param output buffer holding the output data
123 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000124int des_crypt_cbc( des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 int mode,
126 int length,
127 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000128 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 unsigned char *output );
130
131/**
132 * \brief 3DES-ECB block encryption/decryption
133 *
134 * \param ctx 3DES context
135 * \param input 64-bit input block
136 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000137 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000138 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000140int des3_crypt_ecb( des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000141 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 unsigned char output[8] );
143
144/**
145 * \brief 3DES-CBC buffer encryption/decryption
146 *
147 * \param ctx 3DES context
148 * \param mode DES_ENCRYPT or DES_DECRYPT
149 * \param length length of the input data
150 * \param iv initialization vector (updated after use)
151 * \param input buffer holding the input data
152 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000153 *
154 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000156int des3_crypt_cbc( des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 int mode,
158 int length,
159 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000160 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 unsigned char *output );
162
163/*
164 * \brief Checkup routine
165 *
166 * \return 0 if successful, or 1 if the test failed
167 */
168int des_self_test( int verbose );
169
170#ifdef __cplusplus
171}
172#endif
173
174#endif /* des.h */