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