blob: 53bbbff22d55b597dbeff0cacf73f02be3e5df52 [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
64 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000065void des_setkey_enc( des_context *ctx, const unsigned char key[8] );
Paul Bakker5121ce52009-01-03 21:22:43 +000066
67/**
68 * \brief DES key schedule (56-bit, decryption)
69 *
70 * \param ctx DES context to be initialized
71 * \param key 8-byte secret key
72 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000073void des_setkey_dec( des_context *ctx, const unsigned char key[8] );
Paul Bakker5121ce52009-01-03 21:22:43 +000074
75/**
76 * \brief Triple-DES key schedule (112-bit, encryption)
77 *
78 * \param ctx 3DES context to be initialized
79 * \param key 16-byte secret key
80 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000081void des3_set2key_enc( des3_context *ctx, const unsigned char key[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +000082
83/**
84 * \brief Triple-DES key schedule (112-bit, decryption)
85 *
86 * \param ctx 3DES context to be initialized
87 * \param key 16-byte secret key
88 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000089void des3_set2key_dec( des3_context *ctx, const unsigned char key[16] );
Paul Bakker5121ce52009-01-03 21:22:43 +000090
91/**
92 * \brief Triple-DES key schedule (168-bit, encryption)
93 *
94 * \param ctx 3DES context to be initialized
95 * \param key 24-byte secret key
96 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000097void des3_set3key_enc( des3_context *ctx, const unsigned char key[24] );
Paul Bakker5121ce52009-01-03 21:22:43 +000098
99/**
100 * \brief Triple-DES key schedule (168-bit, decryption)
101 *
102 * \param ctx 3DES context to be initialized
103 * \param key 24-byte secret key
104 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000105void des3_set3key_dec( des3_context *ctx, const unsigned char key[24] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
107/**
108 * \brief DES-ECB block encryption/decryption
109 *
110 * \param ctx DES context
111 * \param input 64-bit input block
112 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000113 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000114 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000116int des_crypt_ecb( des_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000117 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 unsigned char output[8] );
119
120/**
121 * \brief DES-CBC buffer encryption/decryption
122 *
123 * \param ctx DES context
124 * \param mode DES_ENCRYPT or DES_DECRYPT
125 * \param length length of the input data
126 * \param iv initialization vector (updated after use)
127 * \param input buffer holding the input data
128 * \param output buffer holding the output data
129 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000130int des_crypt_cbc( des_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 int mode,
132 int length,
133 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000134 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 unsigned char *output );
136
137/**
138 * \brief 3DES-ECB block encryption/decryption
139 *
140 * \param ctx 3DES context
141 * \param input 64-bit input block
142 * \param output 64-bit output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000143 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000144 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000146int des3_crypt_ecb( des3_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000147 const unsigned char input[8],
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 unsigned char output[8] );
149
150/**
151 * \brief 3DES-CBC buffer encryption/decryption
152 *
153 * \param ctx 3DES context
154 * \param mode DES_ENCRYPT or DES_DECRYPT
155 * \param length length of the input data
156 * \param iv initialization vector (updated after use)
157 * \param input buffer holding the input data
158 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000159 *
160 * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000162int des3_crypt_cbc( des3_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 int mode,
164 int length,
165 unsigned char iv[8],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000166 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 unsigned char *output );
168
169/*
170 * \brief Checkup routine
171 *
172 * \return 0 if successful, or 1 if the test failed
173 */
174int des_self_test( int verbose );
175
176#ifdef __cplusplus
177}
178#endif
179
180#endif /* des.h */