blob: f9778df8d2c84b3472211641977c4ea04506acd8 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file des.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief Debug functions
5 *
Paul Bakker84f12b72010-07-18 10:13:04 +00006 * Copyright (C) 2006-2010, 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
30#define DES_ENCRYPT 1
31#define DES_DECRYPT 0
32
Paul Bakkerf3ccc682010-03-18 21:21:02 +000033#define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0C00
34
Paul Bakker5121ce52009-01-03 21:22:43 +000035/**
36 * \brief DES context structure
37 */
38typedef struct
39{
40 int mode; /*!< encrypt/decrypt */
41 unsigned long sk[32]; /*!< DES subkeys */
42}
43des_context;
44
45/**
46 * \brief Triple-DES context structure
47 */
48typedef struct
49{
50 int mode; /*!< encrypt/decrypt */
51 unsigned long sk[96]; /*!< 3DES subkeys */
52}
53des3_context;
54
55#ifdef __cplusplus
56extern "C" {
57#endif
58
59/**
60 * \brief DES key schedule (56-bit, encryption)
61 *
62 * \param ctx DES context to be initialized
63 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +000064 *
65 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +000066 */
Paul Bakker8123e9d2011-01-06 15:37:30 +000067int des_setkey_enc( des_context *ctx, const unsigned char key[8] );
Paul Bakker5121ce52009-01-03 21:22:43 +000068
69/**
70 * \brief DES key schedule (56-bit, decryption)
71 *
72 * \param ctx DES context to be initialized
73 * \param key 8-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +000074 *
75 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +000076 */
Paul Bakker8123e9d2011-01-06 15:37:30 +000077int des_setkey_dec( des_context *ctx, const unsigned char key[8] );
Paul Bakker5121ce52009-01-03 21:22:43 +000078
79/**
80 * \brief Triple-DES key schedule (112-bit, encryption)
81 *
82 * \param ctx 3DES context to be initialized
83 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +000084 *
85 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +000086 */
Paul Bakker8123e9d2011-01-06 15:37:30 +000087int des3_set2key_enc( des3_context *ctx, const unsigned char key[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +000088
89/**
90 * \brief Triple-DES key schedule (112-bit, decryption)
91 *
92 * \param ctx 3DES context to be initialized
93 * \param key 16-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +000094 *
95 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +000096 */
Paul Bakker8123e9d2011-01-06 15:37:30 +000097int des3_set2key_dec( des3_context *ctx, const unsigned char key[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +000098
99/**
100 * \brief Triple-DES key schedule (168-bit, encryption)
101 *
102 * \param ctx 3DES context to be initialized
103 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000104 *
105 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000107int des3_set3key_enc( des3_context *ctx, const unsigned char key[24] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
109/**
110 * \brief Triple-DES key schedule (168-bit, decryption)
111 *
112 * \param ctx 3DES context to be initialized
113 * \param key 24-byte secret key
Paul Bakker8123e9d2011-01-06 15:37:30 +0000114 *
115 * \return 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000117int des3_set3key_dec( des3_context *ctx, const unsigned char key[24] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
119/**
120 * \brief DES-ECB block encryption/decryption
121 *
122 * \param ctx DES context
123 * \param input 64-bit input block
124 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000125 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000126 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000128int des_crypt_ecb( des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000129 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 unsigned char output[8] );
131
132/**
133 * \brief DES-CBC buffer encryption/decryption
134 *
135 * \param ctx DES context
136 * \param mode DES_ENCRYPT or DES_DECRYPT
137 * \param length length of the input data
138 * \param iv initialization vector (updated after use)
139 * \param input buffer holding the input data
140 * \param output buffer holding the output data
141 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000142int des_crypt_cbc( des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 int mode,
144 int length,
145 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000146 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 unsigned char *output );
148
149/**
150 * \brief 3DES-ECB block encryption/decryption
151 *
152 * \param ctx 3DES context
153 * \param input 64-bit input block
154 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000155 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000156 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000158int des3_crypt_ecb( des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000159 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 unsigned char output[8] );
161
162/**
163 * \brief 3DES-CBC buffer encryption/decryption
164 *
165 * \param ctx 3DES context
166 * \param mode DES_ENCRYPT or DES_DECRYPT
167 * \param length length of the input data
168 * \param iv initialization vector (updated after use)
169 * \param input buffer holding the input data
170 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000171 *
172 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000174int des3_crypt_cbc( des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 int mode,
176 int length,
177 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000178 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 unsigned char *output );
180
181/*
182 * \brief Checkup routine
183 *
184 * \return 0 if successful, or 1 if the test failed
185 */
186int des_self_test( int verbose );
187
188#ifdef __cplusplus
189}
190#endif
191
192#endif /* des.h */