blob: fd816febbc3a8c0c4f00d5fec9944a6d98446e07 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
Paul Bakkerfae35f02013-03-13 10:33:51 +01002 * \file cipher_wrap.c
Paul Bakker9af723c2014-05-01 13:03:14 +02003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic cipher wrapper for mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00008 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker8123e9d2011-01-06 15:37:30 +00009 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +000010 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker8123e9d2011-01-06 15:37:30 +000011 *
Paul Bakker8123e9d2011-01-06 15:37:30 +000012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker8123e9d2011-01-06 15:37:30 +000028#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000032
33#if defined(POLARSSL_CIPHER_C)
34
35#include "polarssl/cipher_wrap.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000036
37#if defined(POLARSSL_AES_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000038#include "polarssl/aes.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000039#endif
40
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020041#if defined(POLARSSL_ARC4_C)
42#include "polarssl/arc4.h"
43#endif
44
Paul Bakkerf6543712012-03-05 14:01:29 +000045#if defined(POLARSSL_CAMELLIA_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000046#include "polarssl/camellia.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000047#endif
48
49#if defined(POLARSSL_DES_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000050#include "polarssl/des.h"
Paul Bakker02f61692012-03-15 10:54:25 +000051#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000052
Paul Bakker6132d0a2012-07-04 17:10:40 +000053#if defined(POLARSSL_BLOWFISH_C)
54#include "polarssl/blowfish.h"
55#endif
56
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020057#if defined(POLARSSL_GCM_C)
58#include "polarssl/gcm.h"
59#endif
60
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020061#if defined(POLARSSL_CCM_C)
62#include "polarssl/ccm.h"
63#endif
64
Manuel Pégourié-Gonnard0c851ee2015-02-10 12:47:52 +000065#if defined(POLARSSL_CIPHER_NULL_CIPHER)
66#include <string.h>
67#endif
68
Paul Bakker7dc4c442014-02-01 22:50:26 +010069#if defined(POLARSSL_PLATFORM_C)
70#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020071#else
Rich Evans00ab4702015-02-06 13:43:58 +000072#include <stdlib.h>
Paul Bakker6e339b52013-07-03 13:37:05 +020073#define polarssl_malloc malloc
74#define polarssl_free free
75#endif
76
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +020077#if defined(POLARSSL_GCM_C)
78/* shared by all GCM ciphers */
79static void *gcm_ctx_alloc( void )
80{
81 return polarssl_malloc( sizeof( gcm_context ) );
82}
83
84static void gcm_ctx_free( void *ctx )
85{
86 gcm_free( ctx );
87 polarssl_free( ctx );
88}
Paul Bakker9af723c2014-05-01 13:03:14 +020089#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +020090
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020091#if defined(POLARSSL_CCM_C)
92/* shared by all CCM ciphers */
93static void *ccm_ctx_alloc( void )
94{
95 return polarssl_malloc( sizeof( ccm_context ) );
96}
97
98static void ccm_ctx_free( void *ctx )
99{
100 ccm_free( ctx );
101 polarssl_free( ctx );
102}
103#endif /* POLARSSL_CCM_C */
104
Paul Bakker8123e9d2011-01-06 15:37:30 +0000105#if defined(POLARSSL_AES_C)
106
Paul Bakker5e0efa72013-09-08 23:04:04 +0200107static int aes_crypt_ecb_wrap( void *ctx, operation_t operation,
108 const unsigned char *input, unsigned char *output )
109{
110 return aes_crypt_ecb( (aes_context *) ctx, operation, input, output );
111}
112
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100113#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerfae35f02013-03-13 10:33:51 +0100114static int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000115 unsigned char *iv, const unsigned char *input, unsigned char *output )
116{
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200117 return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input,
118 output );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000119}
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100120#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000121
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100122#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200123static int aes_crypt_cfb128_wrap( void *ctx, operation_t operation,
124 size_t length, size_t *iv_off, unsigned char *iv,
125 const unsigned char *input, unsigned char *output )
Paul Bakker343a8702011-06-09 14:27:58 +0000126{
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200127 return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv,
128 input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000129}
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100130#endif /* POLARSSL_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000131
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100132#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200133static int aes_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
134 unsigned char *nonce_counter, unsigned char *stream_block,
Paul Bakker343a8702011-06-09 14:27:58 +0000135 const unsigned char *input, unsigned char *output )
136{
Paul Bakker343a8702011-06-09 14:27:58 +0000137 return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
138 stream_block, input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000139}
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100140#endif /* POLARSSL_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000141
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200142static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
143 unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000144{
145 return aes_setkey_dec( (aes_context *) ctx, key, key_length );
146}
147
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200148static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
149 unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000150{
151 return aes_setkey_enc( (aes_context *) ctx, key, key_length );
152}
153
154static void * aes_ctx_alloc( void )
155{
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500156 aes_context *aes = polarssl_malloc( sizeof( aes_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200157
158 if( aes == NULL )
159 return( NULL );
160
161 aes_init( aes );
162
163 return( aes );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000164}
165
166static void aes_ctx_free( void *ctx )
167{
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200168 aes_free( (aes_context *) ctx );
Paul Bakker6e339b52013-07-03 13:37:05 +0200169 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000170}
171
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000172static const cipher_base_t aes_info = {
Paul Bakker343a8702011-06-09 14:27:58 +0000173 POLARSSL_CIPHER_ID_AES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200174 aes_crypt_ecb_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100175#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +0000176 aes_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100177#endif
178#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker343a8702011-06-09 14:27:58 +0000179 aes_crypt_cfb128_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100180#endif
181#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000182 aes_crypt_ctr_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100183#endif
184#if defined(POLARSSL_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200185 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100186#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000187 aes_setkey_enc_wrap,
188 aes_setkey_dec_wrap,
189 aes_ctx_alloc,
190 aes_ctx_free
191};
192
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000193static const cipher_info_t aes_128_ecb_info = {
Paul Bakker5e0efa72013-09-08 23:04:04 +0200194 POLARSSL_CIPHER_AES_128_ECB,
195 POLARSSL_MODE_ECB,
196 128,
197 "AES-128-ECB",
198 16,
199 0,
200 16,
201 &aes_info
202};
203
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000204static const cipher_info_t aes_192_ecb_info = {
Paul Bakker5e0efa72013-09-08 23:04:04 +0200205 POLARSSL_CIPHER_AES_192_ECB,
206 POLARSSL_MODE_ECB,
207 192,
208 "AES-192-ECB",
209 16,
210 0,
211 16,
212 &aes_info
213};
214
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000215static const cipher_info_t aes_256_ecb_info = {
Paul Bakker5e0efa72013-09-08 23:04:04 +0200216 POLARSSL_CIPHER_AES_256_ECB,
217 POLARSSL_MODE_ECB,
218 256,
219 "AES-256-ECB",
220 16,
221 0,
222 16,
223 &aes_info
224};
225
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200226#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000227static const cipher_info_t aes_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000228 POLARSSL_CIPHER_AES_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000229 POLARSSL_MODE_CBC,
230 128,
231 "AES-128-CBC",
232 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200233 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000234 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000235 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000236};
237
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000238static const cipher_info_t aes_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000239 POLARSSL_CIPHER_AES_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000240 POLARSSL_MODE_CBC,
241 192,
242 "AES-192-CBC",
243 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200244 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000245 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000246 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000247};
248
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000249static const cipher_info_t aes_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000250 POLARSSL_CIPHER_AES_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000251 POLARSSL_MODE_CBC,
252 256,
253 "AES-256-CBC",
254 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200255 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000256 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000257 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000258};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200259#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000260
261#if defined(POLARSSL_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000262static const cipher_info_t aes_128_cfb128_info = {
Paul Bakker343a8702011-06-09 14:27:58 +0000263 POLARSSL_CIPHER_AES_128_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000264 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000265 128,
266 "AES-128-CFB128",
267 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200268 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000269 16,
270 &aes_info
271};
272
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000273static const cipher_info_t aes_192_cfb128_info = {
Paul Bakker343a8702011-06-09 14:27:58 +0000274 POLARSSL_CIPHER_AES_192_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000275 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000276 192,
277 "AES-192-CFB128",
278 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200279 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000280 16,
281 &aes_info
282};
283
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000284static const cipher_info_t aes_256_cfb128_info = {
Paul Bakker343a8702011-06-09 14:27:58 +0000285 POLARSSL_CIPHER_AES_256_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000286 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000287 256,
288 "AES-256-CFB128",
289 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200290 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000291 16,
292 &aes_info
293};
294#endif /* POLARSSL_CIPHER_MODE_CFB */
295
296#if defined(POLARSSL_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000297static const cipher_info_t aes_128_ctr_info = {
Paul Bakker343a8702011-06-09 14:27:58 +0000298 POLARSSL_CIPHER_AES_128_CTR,
299 POLARSSL_MODE_CTR,
300 128,
301 "AES-128-CTR",
302 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200303 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000304 16,
305 &aes_info
306};
307
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000308static const cipher_info_t aes_192_ctr_info = {
Paul Bakker343a8702011-06-09 14:27:58 +0000309 POLARSSL_CIPHER_AES_192_CTR,
310 POLARSSL_MODE_CTR,
311 192,
312 "AES-192-CTR",
313 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200314 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000315 16,
316 &aes_info
317};
318
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000319static const cipher_info_t aes_256_ctr_info = {
Paul Bakker343a8702011-06-09 14:27:58 +0000320 POLARSSL_CIPHER_AES_256_CTR,
321 POLARSSL_MODE_CTR,
322 256,
323 "AES-256-CTR",
324 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200325 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000326 16,
327 &aes_info
328};
329#endif /* POLARSSL_CIPHER_MODE_CTR */
330
Paul Bakker68884e32013-01-07 18:20:04 +0100331#if defined(POLARSSL_GCM_C)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200332static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key,
333 unsigned int key_length )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200334{
Paul Bakker43aff2a2013-09-09 00:10:27 +0200335 return gcm_init( (gcm_context *) ctx, POLARSSL_CIPHER_ID_AES,
336 key, key_length );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200337}
338
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000339static const cipher_base_t gcm_aes_info = {
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200340 POLARSSL_CIPHER_ID_AES,
341 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100342#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200343 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100344#endif
345#if defined(POLARSSL_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200346 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100347#endif
348#if defined(POLARSSL_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200349 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100350#endif
351#if defined(POLARSSL_CIPHER_MODE_STREAM)
Paul Bakker5e0efa72013-09-08 23:04:04 +0200352 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100353#endif
Paul Bakker43aff2a2013-09-09 00:10:27 +0200354 gcm_aes_setkey_wrap,
355 gcm_aes_setkey_wrap,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200356 gcm_ctx_alloc,
357 gcm_ctx_free,
358};
359
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000360static const cipher_info_t aes_128_gcm_info = {
Paul Bakker68884e32013-01-07 18:20:04 +0100361 POLARSSL_CIPHER_AES_128_GCM,
362 POLARSSL_MODE_GCM,
363 128,
364 "AES-128-GCM",
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200365 12,
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +0200366 POLARSSL_CIPHER_VARIABLE_IV_LEN,
Paul Bakker68884e32013-01-07 18:20:04 +0100367 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200368 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100369};
370
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000371static const cipher_info_t aes_192_gcm_info = {
Manuel Pégourié-Gonnard83f3fc02013-09-04 12:07:24 +0200372 POLARSSL_CIPHER_AES_192_GCM,
373 POLARSSL_MODE_GCM,
374 192,
375 "AES-192-GCM",
376 12,
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +0200377 POLARSSL_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard83f3fc02013-09-04 12:07:24 +0200378 16,
379 &gcm_aes_info
380};
381
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000382static const cipher_info_t aes_256_gcm_info = {
Paul Bakker68884e32013-01-07 18:20:04 +0100383 POLARSSL_CIPHER_AES_256_GCM,
384 POLARSSL_MODE_GCM,
385 256,
386 "AES-256-GCM",
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200387 12,
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +0200388 POLARSSL_CIPHER_VARIABLE_IV_LEN,
Paul Bakker68884e32013-01-07 18:20:04 +0100389 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200390 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100391};
392#endif /* POLARSSL_GCM_C */
393
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200394#if defined(POLARSSL_CCM_C)
395static int ccm_aes_setkey_wrap( void *ctx, const unsigned char *key,
396 unsigned int key_length )
397{
398 return ccm_init( (ccm_context *) ctx, POLARSSL_CIPHER_ID_AES,
399 key, key_length );
400}
401
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000402static const cipher_base_t ccm_aes_info = {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200403 POLARSSL_CIPHER_ID_AES,
404 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100405#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200406 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100407#endif
408#if defined(POLARSSL_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200409 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100410#endif
411#if defined(POLARSSL_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200412 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100413#endif
414#if defined(POLARSSL_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200415 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100416#endif
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200417 ccm_aes_setkey_wrap,
418 ccm_aes_setkey_wrap,
419 ccm_ctx_alloc,
420 ccm_ctx_free,
421};
422
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000423static const cipher_info_t aes_128_ccm_info = {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200424 POLARSSL_CIPHER_AES_128_CCM,
425 POLARSSL_MODE_CCM,
426 128,
427 "AES-128-CCM",
428 12,
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +0200429 POLARSSL_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200430 16,
431 &ccm_aes_info
432};
433
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000434static const cipher_info_t aes_192_ccm_info = {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200435 POLARSSL_CIPHER_AES_192_CCM,
436 POLARSSL_MODE_CCM,
437 192,
438 "AES-192-CCM",
439 12,
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +0200440 POLARSSL_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200441 16,
442 &ccm_aes_info
443};
444
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000445static const cipher_info_t aes_256_ccm_info = {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200446 POLARSSL_CIPHER_AES_256_CCM,
447 POLARSSL_MODE_CCM,
448 256,
449 "AES-256-CCM",
450 12,
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +0200451 POLARSSL_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200452 16,
453 &ccm_aes_info
454};
455#endif /* POLARSSL_CCM_C */
456
Paul Bakker9af723c2014-05-01 13:03:14 +0200457#endif /* POLARSSL_AES_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000458
459#if defined(POLARSSL_CAMELLIA_C)
460
Paul Bakker5e0efa72013-09-08 23:04:04 +0200461static int camellia_crypt_ecb_wrap( void *ctx, operation_t operation,
462 const unsigned char *input, unsigned char *output )
463{
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200464 return camellia_crypt_ecb( (camellia_context *) ctx, operation, input,
465 output );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200466}
467
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100468#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200469static int camellia_crypt_cbc_wrap( void *ctx, operation_t operation,
470 size_t length, unsigned char *iv,
471 const unsigned char *input, unsigned char *output )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000472{
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200473 return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv,
474 input, output );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000475}
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100476#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000477
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100478#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200479static int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation,
480 size_t length, size_t *iv_off, unsigned char *iv,
481 const unsigned char *input, unsigned char *output )
Paul Bakker343a8702011-06-09 14:27:58 +0000482{
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200483 return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length,
484 iv_off, iv, input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000485}
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100486#endif /* POLARSSL_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000487
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100488#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200489static int camellia_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
490 unsigned char *nonce_counter, unsigned char *stream_block,
Paul Bakker343a8702011-06-09 14:27:58 +0000491 const unsigned char *input, unsigned char *output )
492{
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200493 return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off,
494 nonce_counter, stream_block, input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000495}
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100496#endif /* POLARSSL_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000497
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200498static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key,
499 unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000500{
501 return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
502}
503
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200504static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key,
505 unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000506{
507 return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
508}
509
510static void * camellia_ctx_alloc( void )
511{
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200512 camellia_context *ctx;
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500513 ctx = polarssl_malloc( sizeof( camellia_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200514
515 if( ctx == NULL )
516 return( NULL );
517
518 camellia_init( ctx );
519
520 return( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000521}
522
523static void camellia_ctx_free( void *ctx )
524{
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200525 camellia_free( (camellia_context *) ctx );
Paul Bakker6e339b52013-07-03 13:37:05 +0200526 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000527}
528
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000529static const cipher_base_t camellia_info = {
Paul Bakker343a8702011-06-09 14:27:58 +0000530 POLARSSL_CIPHER_ID_CAMELLIA,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200531 camellia_crypt_ecb_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100532#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +0000533 camellia_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100534#endif
535#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker343a8702011-06-09 14:27:58 +0000536 camellia_crypt_cfb128_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100537#endif
538#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000539 camellia_crypt_ctr_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100540#endif
541#if defined(POLARSSL_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200542 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100543#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000544 camellia_setkey_enc_wrap,
545 camellia_setkey_dec_wrap,
546 camellia_ctx_alloc,
547 camellia_ctx_free
548};
549
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000550static const cipher_info_t camellia_128_ecb_info = {
Paul Bakker5e0efa72013-09-08 23:04:04 +0200551 POLARSSL_CIPHER_CAMELLIA_128_ECB,
552 POLARSSL_MODE_ECB,
553 128,
554 "CAMELLIA-128-ECB",
555 16,
556 0,
557 16,
558 &camellia_info
559};
560
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000561static const cipher_info_t camellia_192_ecb_info = {
Paul Bakker5e0efa72013-09-08 23:04:04 +0200562 POLARSSL_CIPHER_CAMELLIA_192_ECB,
563 POLARSSL_MODE_ECB,
564 192,
565 "CAMELLIA-192-ECB",
566 16,
567 0,
568 16,
569 &camellia_info
570};
571
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000572static const cipher_info_t camellia_256_ecb_info = {
Paul Bakker5e0efa72013-09-08 23:04:04 +0200573 POLARSSL_CIPHER_CAMELLIA_256_ECB,
574 POLARSSL_MODE_ECB,
575 256,
576 "CAMELLIA-256-ECB",
577 16,
578 0,
579 16,
580 &camellia_info
581};
582
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200583#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000584static const cipher_info_t camellia_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000585 POLARSSL_CIPHER_CAMELLIA_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000586 POLARSSL_MODE_CBC,
587 128,
588 "CAMELLIA-128-CBC",
589 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200590 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000591 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000592 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000593};
594
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000595static const cipher_info_t camellia_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000596 POLARSSL_CIPHER_CAMELLIA_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000597 POLARSSL_MODE_CBC,
598 192,
599 "CAMELLIA-192-CBC",
600 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200601 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000602 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000603 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000604};
605
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000606static const cipher_info_t camellia_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000607 POLARSSL_CIPHER_CAMELLIA_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000608 POLARSSL_MODE_CBC,
609 256,
610 "CAMELLIA-256-CBC",
611 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200612 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000613 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000614 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000615};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200616#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000617
618#if defined(POLARSSL_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000619static const cipher_info_t camellia_128_cfb128_info = {
Paul Bakker343a8702011-06-09 14:27:58 +0000620 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000621 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000622 128,
623 "CAMELLIA-128-CFB128",
624 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200625 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000626 16,
627 &camellia_info
628};
629
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000630static const cipher_info_t camellia_192_cfb128_info = {
Paul Bakker343a8702011-06-09 14:27:58 +0000631 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000632 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000633 192,
634 "CAMELLIA-192-CFB128",
635 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200636 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000637 16,
638 &camellia_info
639};
640
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000641static const cipher_info_t camellia_256_cfb128_info = {
Paul Bakker343a8702011-06-09 14:27:58 +0000642 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000643 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000644 256,
645 "CAMELLIA-256-CFB128",
646 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200647 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000648 16,
649 &camellia_info
650};
651#endif /* POLARSSL_CIPHER_MODE_CFB */
652
653#if defined(POLARSSL_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000654static const cipher_info_t camellia_128_ctr_info = {
Paul Bakker343a8702011-06-09 14:27:58 +0000655 POLARSSL_CIPHER_CAMELLIA_128_CTR,
656 POLARSSL_MODE_CTR,
657 128,
658 "CAMELLIA-128-CTR",
659 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200660 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000661 16,
662 &camellia_info
663};
664
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000665static const cipher_info_t camellia_192_ctr_info = {
Paul Bakker343a8702011-06-09 14:27:58 +0000666 POLARSSL_CIPHER_CAMELLIA_192_CTR,
667 POLARSSL_MODE_CTR,
668 192,
669 "CAMELLIA-192-CTR",
670 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200671 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000672 16,
673 &camellia_info
674};
675
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000676static const cipher_info_t camellia_256_ctr_info = {
Paul Bakker343a8702011-06-09 14:27:58 +0000677 POLARSSL_CIPHER_CAMELLIA_256_CTR,
678 POLARSSL_MODE_CTR,
679 256,
680 "CAMELLIA-256-CTR",
681 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200682 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000683 16,
684 &camellia_info
685};
686#endif /* POLARSSL_CIPHER_MODE_CTR */
687
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200688#if defined(POLARSSL_GCM_C)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200689static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
690 unsigned int key_length )
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200691{
692 return gcm_init( (gcm_context *) ctx, POLARSSL_CIPHER_ID_CAMELLIA,
693 key, key_length );
694}
695
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000696static const cipher_base_t gcm_camellia_info = {
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200697 POLARSSL_CIPHER_ID_CAMELLIA,
698 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100699#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200700 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100701#endif
702#if defined(POLARSSL_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200703 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100704#endif
705#if defined(POLARSSL_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200706 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100707#endif
708#if defined(POLARSSL_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200709 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100710#endif
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200711 gcm_camellia_setkey_wrap,
712 gcm_camellia_setkey_wrap,
713 gcm_ctx_alloc,
714 gcm_ctx_free,
715};
716
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000717static const cipher_info_t camellia_128_gcm_info = {
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200718 POLARSSL_CIPHER_CAMELLIA_128_GCM,
719 POLARSSL_MODE_GCM,
720 128,
721 "CAMELLIA-128-GCM",
722 12,
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +0200723 POLARSSL_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200724 16,
725 &gcm_camellia_info
726};
727
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000728static const cipher_info_t camellia_192_gcm_info = {
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200729 POLARSSL_CIPHER_CAMELLIA_192_GCM,
730 POLARSSL_MODE_GCM,
731 192,
732 "CAMELLIA-192-GCM",
733 12,
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +0200734 POLARSSL_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200735 16,
736 &gcm_camellia_info
737};
738
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000739static const cipher_info_t camellia_256_gcm_info = {
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200740 POLARSSL_CIPHER_CAMELLIA_256_GCM,
741 POLARSSL_MODE_GCM,
742 256,
743 "CAMELLIA-256-GCM",
744 12,
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +0200745 POLARSSL_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200746 16,
747 &gcm_camellia_info
748};
749#endif /* POLARSSL_GCM_C */
750
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200751#if defined(POLARSSL_CCM_C)
752static int ccm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
753 unsigned int key_length )
754{
755 return ccm_init( (ccm_context *) ctx, POLARSSL_CIPHER_ID_CAMELLIA,
756 key, key_length );
757}
758
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000759static const cipher_base_t ccm_camellia_info = {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200760 POLARSSL_CIPHER_ID_CAMELLIA,
761 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100762#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200763 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100764#endif
765#if defined(POLARSSL_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200766 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100767#endif
768#if defined(POLARSSL_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200769 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100770#endif
771#if defined(POLARSSL_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200772 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100773#endif
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200774 ccm_camellia_setkey_wrap,
775 ccm_camellia_setkey_wrap,
776 ccm_ctx_alloc,
777 ccm_ctx_free,
778};
779
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000780static const cipher_info_t camellia_128_ccm_info = {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200781 POLARSSL_CIPHER_CAMELLIA_128_CCM,
782 POLARSSL_MODE_CCM,
783 128,
784 "CAMELLIA-128-CCM",
785 12,
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +0200786 POLARSSL_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200787 16,
788 &ccm_camellia_info
789};
790
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000791static const cipher_info_t camellia_192_ccm_info = {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200792 POLARSSL_CIPHER_CAMELLIA_192_CCM,
793 POLARSSL_MODE_CCM,
794 192,
795 "CAMELLIA-192-CCM",
796 12,
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +0200797 POLARSSL_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200798 16,
799 &ccm_camellia_info
800};
801
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000802static const cipher_info_t camellia_256_ccm_info = {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200803 POLARSSL_CIPHER_CAMELLIA_256_CCM,
804 POLARSSL_MODE_CCM,
805 256,
806 "CAMELLIA-256-CCM",
807 12,
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +0200808 POLARSSL_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200809 16,
810 &ccm_camellia_info
811};
812#endif /* POLARSSL_CCM_C */
813
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200814#endif /* POLARSSL_CAMELLIA_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000815
816#if defined(POLARSSL_DES_C)
817
Paul Bakker5e0efa72013-09-08 23:04:04 +0200818static int des_crypt_ecb_wrap( void *ctx, operation_t operation,
819 const unsigned char *input, unsigned char *output )
820{
821 ((void) operation);
822 return des_crypt_ecb( (des_context *) ctx, input, output );
823}
824
825static int des3_crypt_ecb_wrap( void *ctx, operation_t operation,
826 const unsigned char *input, unsigned char *output )
827{
828 ((void) operation);
829 return des3_crypt_ecb( (des3_context *) ctx, input, output );
830}
831
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100832#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerfae35f02013-03-13 10:33:51 +0100833static int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000834 unsigned char *iv, const unsigned char *input, unsigned char *output )
835{
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200836 return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input,
837 output );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000838}
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100839#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000840
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100841#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerfae35f02013-03-13 10:33:51 +0100842static int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000843 unsigned char *iv, const unsigned char *input, unsigned char *output )
844{
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200845 return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input,
846 output );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000847}
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100848#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000849
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200850static int des_setkey_dec_wrap( void *ctx, const unsigned char *key,
851 unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000852{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000853 ((void) key_length);
854
Paul Bakker8123e9d2011-01-06 15:37:30 +0000855 return des_setkey_dec( (des_context *) ctx, key );
856}
857
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200858static int des_setkey_enc_wrap( void *ctx, const unsigned char *key,
859 unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000860{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000861 ((void) key_length);
862
Paul Bakker8123e9d2011-01-06 15:37:30 +0000863 return des_setkey_enc( (des_context *) ctx, key );
864}
865
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200866static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key,
867 unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000868{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000869 ((void) key_length);
870
Paul Bakker8123e9d2011-01-06 15:37:30 +0000871 return des3_set2key_dec( (des3_context *) ctx, key );
872}
873
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200874static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key,
875 unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000876{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000877 ((void) key_length);
878
Paul Bakker8123e9d2011-01-06 15:37:30 +0000879 return des3_set2key_enc( (des3_context *) ctx, key );
880}
881
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200882static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key,
883 unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000884{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000885 ((void) key_length);
886
Paul Bakker8123e9d2011-01-06 15:37:30 +0000887 return des3_set3key_dec( (des3_context *) ctx, key );
888}
889
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200890static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key,
891 unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000892{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000893 ((void) key_length);
894
Paul Bakker8123e9d2011-01-06 15:37:30 +0000895 return des3_set3key_enc( (des3_context *) ctx, key );
896}
897
898static void * des_ctx_alloc( void )
899{
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500900 des_context *des = polarssl_malloc( sizeof( des_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000901
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200902 if( des == NULL )
903 return( NULL );
904
905 des_init( des );
906
907 return( des );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000908}
909
910static void des_ctx_free( void *ctx )
911{
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200912 des_free( (des_context *) ctx );
Paul Bakker34617722014-06-13 17:20:13 +0200913 polarssl_free( ctx );
914}
915
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200916static void * des3_ctx_alloc( void )
917{
918 des3_context *des3;
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500919 des3 = polarssl_malloc( sizeof( des3_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200920
921 if( des3 == NULL )
922 return( NULL );
923
924 des3_init( des3 );
925
926 return( des3 );
927}
928
Paul Bakker34617722014-06-13 17:20:13 +0200929static void des3_ctx_free( void *ctx )
930{
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200931 des3_free( (des3_context *) ctx );
Paul Bakker6e339b52013-07-03 13:37:05 +0200932 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000933}
934
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000935static const cipher_base_t des_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000936 POLARSSL_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200937 des_crypt_ecb_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100938#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker23986e52011-04-24 08:57:21 +0000939 des_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100940#endif
941#if defined(POLARSSL_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +0200942 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100943#endif
944#if defined(POLARSSL_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +0200945 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100946#endif
947#if defined(POLARSSL_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200948 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100949#endif
Paul Bakker23986e52011-04-24 08:57:21 +0000950 des_setkey_enc_wrap,
951 des_setkey_dec_wrap,
952 des_ctx_alloc,
953 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000954};
955
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000956static const cipher_info_t des_ecb_info = {
Paul Bakker5e0efa72013-09-08 23:04:04 +0200957 POLARSSL_CIPHER_DES_ECB,
958 POLARSSL_MODE_ECB,
959 POLARSSL_KEY_LENGTH_DES,
960 "DES-ECB",
961 8,
962 0,
963 8,
964 &des_info
965};
966
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200967#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000968static const cipher_info_t des_cbc_info = {
Paul Bakker343a8702011-06-09 14:27:58 +0000969 POLARSSL_CIPHER_DES_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000970 POLARSSL_MODE_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +0000971 POLARSSL_KEY_LENGTH_DES,
972 "DES-CBC",
973 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200974 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000975 8,
976 &des_info
977};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200978#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000979
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +0000980static const cipher_base_t des_ede_info = {
Paul Bakker343a8702011-06-09 14:27:58 +0000981 POLARSSL_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200982 des3_crypt_ecb_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100983#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker23986e52011-04-24 08:57:21 +0000984 des3_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100985#endif
986#if defined(POLARSSL_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +0200987 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100988#endif
989#if defined(POLARSSL_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +0200990 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100991#endif
992#if defined(POLARSSL_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200993 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100994#endif
Paul Bakker23986e52011-04-24 08:57:21 +0000995 des3_set2key_enc_wrap,
996 des3_set2key_dec_wrap,
997 des3_ctx_alloc,
Paul Bakker34617722014-06-13 17:20:13 +0200998 des3_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000999};
1000
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +00001001static const cipher_info_t des_ede_ecb_info = {
Paul Bakker5e0efa72013-09-08 23:04:04 +02001002 POLARSSL_CIPHER_DES_EDE_ECB,
1003 POLARSSL_MODE_ECB,
1004 POLARSSL_KEY_LENGTH_DES_EDE,
1005 "DES-EDE-ECB",
1006 8,
1007 0,
1008 8,
1009 &des_ede_info
1010};
1011
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001012#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +00001013static const cipher_info_t des_ede_cbc_info = {
Paul Bakker343a8702011-06-09 14:27:58 +00001014 POLARSSL_CIPHER_DES_EDE_CBC,
1015 POLARSSL_MODE_CBC,
1016 POLARSSL_KEY_LENGTH_DES_EDE,
1017 "DES-EDE-CBC",
Paul Bakker0e342352013-06-24 19:33:02 +02001018 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001019 0,
Paul Bakker0e342352013-06-24 19:33:02 +02001020 8,
Paul Bakker343a8702011-06-09 14:27:58 +00001021 &des_ede_info
1022};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001023#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +00001024
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +00001025static const cipher_base_t des_ede3_info = {
Paul Bakker343a8702011-06-09 14:27:58 +00001026 POLARSSL_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001027 des3_crypt_ecb_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001028#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +00001029 des3_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001030#endif
1031#if defined(POLARSSL_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001032 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001033#endif
1034#if defined(POLARSSL_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001035 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001036#endif
1037#if defined(POLARSSL_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001038 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001039#endif
Paul Bakker343a8702011-06-09 14:27:58 +00001040 des3_set3key_enc_wrap,
1041 des3_set3key_dec_wrap,
1042 des3_ctx_alloc,
Paul Bakker34617722014-06-13 17:20:13 +02001043 des3_ctx_free
Paul Bakker343a8702011-06-09 14:27:58 +00001044};
1045
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +00001046static const cipher_info_t des_ede3_ecb_info = {
Paul Bakker5e0efa72013-09-08 23:04:04 +02001047 POLARSSL_CIPHER_DES_EDE3_ECB,
1048 POLARSSL_MODE_ECB,
1049 POLARSSL_KEY_LENGTH_DES_EDE3,
1050 "DES-EDE3-ECB",
1051 8,
1052 0,
1053 8,
1054 &des_ede3_info
1055};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001056#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +00001057static const cipher_info_t des_ede3_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +00001058 POLARSSL_CIPHER_DES_EDE3_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +00001059 POLARSSL_MODE_CBC,
1060 POLARSSL_KEY_LENGTH_DES_EDE3,
1061 "DES-EDE3-CBC",
1062 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001063 0,
Paul Bakker23986e52011-04-24 08:57:21 +00001064 8,
Paul Bakker343a8702011-06-09 14:27:58 +00001065 &des_ede3_info
Paul Bakker8123e9d2011-01-06 15:37:30 +00001066};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001067#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker9af723c2014-05-01 13:03:14 +02001068#endif /* POLARSSL_DES_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001069
Paul Bakker6132d0a2012-07-04 17:10:40 +00001070#if defined(POLARSSL_BLOWFISH_C)
1071
Paul Bakker5e0efa72013-09-08 23:04:04 +02001072static int blowfish_crypt_ecb_wrap( void *ctx, operation_t operation,
1073 const unsigned char *input, unsigned char *output )
1074{
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001075 return blowfish_crypt_ecb( (blowfish_context *) ctx, operation, input,
1076 output );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001077}
1078
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001079#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001080static int blowfish_crypt_cbc_wrap( void *ctx, operation_t operation,
1081 size_t length, unsigned char *iv, const unsigned char *input,
1082 unsigned char *output )
Paul Bakker6132d0a2012-07-04 17:10:40 +00001083{
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001084 return blowfish_crypt_cbc( (blowfish_context *) ctx, operation, length, iv,
1085 input, output );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001086}
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001087#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001088
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001089#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001090static int blowfish_crypt_cfb64_wrap( void *ctx, operation_t operation,
1091 size_t length, size_t *iv_off, unsigned char *iv,
1092 const unsigned char *input, unsigned char *output )
Paul Bakker6132d0a2012-07-04 17:10:40 +00001093{
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001094 return blowfish_crypt_cfb64( (blowfish_context *) ctx, operation, length,
1095 iv_off, iv, input, output );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001096}
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001097#endif /* POLARSSL_CIPHER_MODE_CFB */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001098
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001099#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001100static int blowfish_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
1101 unsigned char *nonce_counter, unsigned char *stream_block,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001102 const unsigned char *input, unsigned char *output )
1103{
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001104 return blowfish_crypt_ctr( (blowfish_context *) ctx, length, nc_off,
1105 nonce_counter, stream_block, input, output );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001106}
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001107#endif /* POLARSSL_CIPHER_MODE_CTR */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001108
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001109static int blowfish_setkey_wrap( void *ctx, const unsigned char *key,
1110 unsigned int key_length )
Paul Bakker6132d0a2012-07-04 17:10:40 +00001111{
1112 return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
1113}
1114
1115static void * blowfish_ctx_alloc( void )
1116{
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001117 blowfish_context *ctx;
Mansour Moufidc531b4a2015-02-15 17:35:38 -05001118 ctx = polarssl_malloc( sizeof( blowfish_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001119
1120 if( ctx == NULL )
1121 return( NULL );
1122
1123 blowfish_init( ctx );
1124
1125 return( ctx );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001126}
1127
1128static void blowfish_ctx_free( void *ctx )
1129{
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001130 blowfish_free( (blowfish_context *) ctx );
Paul Bakker6e339b52013-07-03 13:37:05 +02001131 polarssl_free( ctx );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001132}
1133
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +00001134static const cipher_base_t blowfish_info = {
Paul Bakker6132d0a2012-07-04 17:10:40 +00001135 POLARSSL_CIPHER_ID_BLOWFISH,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001136 blowfish_crypt_ecb_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001137#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001138 blowfish_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001139#endif
1140#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001141 blowfish_crypt_cfb64_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001142#endif
1143#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001144 blowfish_crypt_ctr_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001145#endif
1146#if defined(POLARSSL_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001147 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001148#endif
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001149 blowfish_setkey_wrap,
1150 blowfish_setkey_wrap,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001151 blowfish_ctx_alloc,
1152 blowfish_ctx_free
1153};
1154
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +00001155static const cipher_info_t blowfish_ecb_info = {
Paul Bakker5e0efa72013-09-08 23:04:04 +02001156 POLARSSL_CIPHER_BLOWFISH_ECB,
1157 POLARSSL_MODE_ECB,
1158 128,
1159 "BLOWFISH-ECB",
1160 8,
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +02001161 POLARSSL_CIPHER_VARIABLE_KEY_LEN,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001162 8,
1163 &blowfish_info
1164};
1165
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001166#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +00001167static const cipher_info_t blowfish_cbc_info = {
Paul Bakker6132d0a2012-07-04 17:10:40 +00001168 POLARSSL_CIPHER_BLOWFISH_CBC,
1169 POLARSSL_MODE_CBC,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001170 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001171 "BLOWFISH-CBC",
1172 8,
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +02001173 POLARSSL_CIPHER_VARIABLE_KEY_LEN,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001174 8,
1175 &blowfish_info
1176};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001177#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001178
1179#if defined(POLARSSL_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +00001180static const cipher_info_t blowfish_cfb64_info = {
Paul Bakker6132d0a2012-07-04 17:10:40 +00001181 POLARSSL_CIPHER_BLOWFISH_CFB64,
1182 POLARSSL_MODE_CFB,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001183 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001184 "BLOWFISH-CFB64",
1185 8,
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +02001186 POLARSSL_CIPHER_VARIABLE_KEY_LEN,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001187 8,
1188 &blowfish_info
1189};
1190#endif /* POLARSSL_CIPHER_MODE_CFB */
1191
1192#if defined(POLARSSL_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +00001193static const cipher_info_t blowfish_ctr_info = {
Paul Bakker6132d0a2012-07-04 17:10:40 +00001194 POLARSSL_CIPHER_BLOWFISH_CTR,
1195 POLARSSL_MODE_CTR,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001196 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001197 "BLOWFISH-CTR",
1198 8,
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +02001199 POLARSSL_CIPHER_VARIABLE_KEY_LEN,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001200 8,
1201 &blowfish_info
1202};
1203#endif /* POLARSSL_CIPHER_MODE_CTR */
1204#endif /* POLARSSL_BLOWFISH_C */
1205
Paul Bakker68884e32013-01-07 18:20:04 +01001206#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001207static int arc4_crypt_stream_wrap( void *ctx, size_t length,
1208 const unsigned char *input,
1209 unsigned char *output )
Paul Bakker68884e32013-01-07 18:20:04 +01001210{
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001211 return( arc4_crypt( (arc4_context *) ctx, length, input, output ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001212}
1213
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001214static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
1215 unsigned int key_length )
1216{
Manuel Pégourié-Gonnardce411252013-09-04 12:28:37 +02001217 /* we get key_length in bits, arc4 expects it in bytes */
Paul Bakker66d5d072014-06-17 16:39:18 +02001218 if( key_length % 8 != 0 )
Manuel Pégourié-Gonnardce411252013-09-04 12:28:37 +02001219 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
1220
1221 arc4_setup( (arc4_context *) ctx, key, key_length / 8 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001222 return( 0 );
1223}
1224
1225static void * arc4_ctx_alloc( void )
1226{
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001227 arc4_context *ctx;
Mansour Moufidc531b4a2015-02-15 17:35:38 -05001228 ctx = polarssl_malloc( sizeof( arc4_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001229
1230 if( ctx == NULL )
1231 return( NULL );
1232
1233 arc4_init( ctx );
1234
1235 return( ctx );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001236}
Paul Bakker68884e32013-01-07 18:20:04 +01001237
1238static void arc4_ctx_free( void *ctx )
1239{
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001240 arc4_free( (arc4_context *) ctx );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001241 polarssl_free( ctx );
Paul Bakker68884e32013-01-07 18:20:04 +01001242}
1243
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +00001244static const cipher_base_t arc4_base_info = {
Paul Bakker68884e32013-01-07 18:20:04 +01001245 POLARSSL_CIPHER_ID_ARC4,
1246 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001247#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker68884e32013-01-07 18:20:04 +01001248 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001249#endif
1250#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker68884e32013-01-07 18:20:04 +01001251 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001252#endif
1253#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker5e0efa72013-09-08 23:04:04 +02001254 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001255#endif
1256#if defined(POLARSSL_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001257 arc4_crypt_stream_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001258#endif
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001259 arc4_setkey_wrap,
1260 arc4_setkey_wrap,
Paul Bakker68884e32013-01-07 18:20:04 +01001261 arc4_ctx_alloc,
1262 arc4_ctx_free
1263};
1264
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +00001265static const cipher_info_t arc4_128_info = {
Paul Bakker68884e32013-01-07 18:20:04 +01001266 POLARSSL_CIPHER_ARC4_128,
1267 POLARSSL_MODE_STREAM,
1268 128,
1269 "ARC4-128",
1270 0,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001271 0,
Paul Bakker68884e32013-01-07 18:20:04 +01001272 1,
1273 &arc4_base_info
1274};
1275#endif /* POLARSSL_ARC4_C */
1276
Paul Bakkerfab5c822012-02-06 16:45:10 +00001277#if defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001278static int null_crypt_stream( void *ctx, size_t length,
1279 const unsigned char *input,
1280 unsigned char *output )
1281{
1282 ((void) ctx);
1283 memmove( output, input, length );
1284 return( 0 );
1285}
1286
1287static int null_setkey( void *ctx, const unsigned char *key,
1288 unsigned int key_length )
1289{
1290 ((void) ctx);
1291 ((void) key);
1292 ((void) key_length);
1293
1294 return( 0 );
1295}
1296
Paul Bakkerfab5c822012-02-06 16:45:10 +00001297static void * null_ctx_alloc( void )
1298{
Manuel Pégourié-Gonnard86bbc7f2014-07-12 02:14:41 +02001299 return( (void *) 1 );
Paul Bakkerfab5c822012-02-06 16:45:10 +00001300}
1301
Paul Bakkerfab5c822012-02-06 16:45:10 +00001302static void null_ctx_free( void *ctx )
1303{
1304 ((void) ctx);
1305}
1306
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +00001307static const cipher_base_t null_base_info = {
Paul Bakkerfab5c822012-02-06 16:45:10 +00001308 POLARSSL_CIPHER_ID_NULL,
1309 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001310#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerfab5c822012-02-06 16:45:10 +00001311 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001312#endif
1313#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakkerfab5c822012-02-06 16:45:10 +00001314 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001315#endif
1316#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker5e0efa72013-09-08 23:04:04 +02001317 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001318#endif
1319#if defined(POLARSSL_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001320 null_crypt_stream,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001321#endif
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001322 null_setkey,
1323 null_setkey,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001324 null_ctx_alloc,
1325 null_ctx_free
1326};
1327
Manuel Pégourié-Gonnard385069f2015-03-05 15:47:55 +00001328static const cipher_info_t null_cipher_info = {
Paul Bakkerfab5c822012-02-06 16:45:10 +00001329 POLARSSL_CIPHER_NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001330 POLARSSL_MODE_STREAM,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001331 0,
1332 "NULL",
Paul Bakker68884e32013-01-07 18:20:04 +01001333 0,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001334 0,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001335 1,
1336 &null_base_info
1337};
1338#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
1339
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001340const cipher_definition_t cipher_definitions[] =
1341{
1342#if defined(POLARSSL_AES_C)
1343 { POLARSSL_CIPHER_AES_128_ECB, &aes_128_ecb_info },
1344 { POLARSSL_CIPHER_AES_192_ECB, &aes_192_ecb_info },
1345 { POLARSSL_CIPHER_AES_256_ECB, &aes_256_ecb_info },
1346#if defined(POLARSSL_CIPHER_MODE_CBC)
1347 { POLARSSL_CIPHER_AES_128_CBC, &aes_128_cbc_info },
1348 { POLARSSL_CIPHER_AES_192_CBC, &aes_192_cbc_info },
1349 { POLARSSL_CIPHER_AES_256_CBC, &aes_256_cbc_info },
1350#endif
1351#if defined(POLARSSL_CIPHER_MODE_CFB)
1352 { POLARSSL_CIPHER_AES_128_CFB128, &aes_128_cfb128_info },
1353 { POLARSSL_CIPHER_AES_192_CFB128, &aes_192_cfb128_info },
1354 { POLARSSL_CIPHER_AES_256_CFB128, &aes_256_cfb128_info },
1355#endif
1356#if defined(POLARSSL_CIPHER_MODE_CTR)
1357 { POLARSSL_CIPHER_AES_128_CTR, &aes_128_ctr_info },
1358 { POLARSSL_CIPHER_AES_192_CTR, &aes_192_ctr_info },
1359 { POLARSSL_CIPHER_AES_256_CTR, &aes_256_ctr_info },
1360#endif
1361#if defined(POLARSSL_GCM_C)
1362 { POLARSSL_CIPHER_AES_128_GCM, &aes_128_gcm_info },
1363 { POLARSSL_CIPHER_AES_192_GCM, &aes_192_gcm_info },
1364 { POLARSSL_CIPHER_AES_256_GCM, &aes_256_gcm_info },
1365#endif
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001366#if defined(POLARSSL_CCM_C)
1367 { POLARSSL_CIPHER_AES_128_CCM, &aes_128_ccm_info },
1368 { POLARSSL_CIPHER_AES_192_CCM, &aes_192_ccm_info },
1369 { POLARSSL_CIPHER_AES_256_CCM, &aes_256_ccm_info },
1370#endif
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001371#endif /* POLARSSL_AES_C */
1372
1373#if defined(POLARSSL_ARC4_C)
1374 { POLARSSL_CIPHER_ARC4_128, &arc4_128_info },
1375#endif
1376
1377#if defined(POLARSSL_BLOWFISH_C)
1378 { POLARSSL_CIPHER_BLOWFISH_ECB, &blowfish_ecb_info },
1379#if defined(POLARSSL_CIPHER_MODE_CBC)
1380 { POLARSSL_CIPHER_BLOWFISH_CBC, &blowfish_cbc_info },
1381#endif
1382#if defined(POLARSSL_CIPHER_MODE_CFB)
1383 { POLARSSL_CIPHER_BLOWFISH_CFB64, &blowfish_cfb64_info },
1384#endif
1385#if defined(POLARSSL_CIPHER_MODE_CTR)
1386 { POLARSSL_CIPHER_BLOWFISH_CTR, &blowfish_ctr_info },
1387#endif
1388#endif /* POLARSSL_BLOWFISH_C */
1389
1390#if defined(POLARSSL_CAMELLIA_C)
1391 { POLARSSL_CIPHER_CAMELLIA_128_ECB, &camellia_128_ecb_info },
1392 { POLARSSL_CIPHER_CAMELLIA_192_ECB, &camellia_192_ecb_info },
Manuel Pégourié-Gonnard13e0d442013-10-24 12:59:00 +02001393 { POLARSSL_CIPHER_CAMELLIA_256_ECB, &camellia_256_ecb_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001394#if defined(POLARSSL_CIPHER_MODE_CBC)
1395 { POLARSSL_CIPHER_CAMELLIA_128_CBC, &camellia_128_cbc_info },
1396 { POLARSSL_CIPHER_CAMELLIA_192_CBC, &camellia_192_cbc_info },
1397 { POLARSSL_CIPHER_CAMELLIA_256_CBC, &camellia_256_cbc_info },
1398#endif
1399#if defined(POLARSSL_CIPHER_MODE_CFB)
1400 { POLARSSL_CIPHER_CAMELLIA_128_CFB128, &camellia_128_cfb128_info },
1401 { POLARSSL_CIPHER_CAMELLIA_192_CFB128, &camellia_192_cfb128_info },
1402 { POLARSSL_CIPHER_CAMELLIA_256_CFB128, &camellia_256_cfb128_info },
1403#endif
1404#if defined(POLARSSL_CIPHER_MODE_CTR)
1405 { POLARSSL_CIPHER_CAMELLIA_128_CTR, &camellia_128_ctr_info },
1406 { POLARSSL_CIPHER_CAMELLIA_192_CTR, &camellia_192_ctr_info },
1407 { POLARSSL_CIPHER_CAMELLIA_256_CTR, &camellia_256_ctr_info },
1408#endif
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +02001409#if defined(POLARSSL_GCM_C)
1410 { POLARSSL_CIPHER_CAMELLIA_128_GCM, &camellia_128_gcm_info },
1411 { POLARSSL_CIPHER_CAMELLIA_192_GCM, &camellia_192_gcm_info },
1412 { POLARSSL_CIPHER_CAMELLIA_256_GCM, &camellia_256_gcm_info },
1413#endif
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001414#if defined(POLARSSL_CCM_C)
1415 { POLARSSL_CIPHER_CAMELLIA_128_CCM, &camellia_128_ccm_info },
1416 { POLARSSL_CIPHER_CAMELLIA_192_CCM, &camellia_192_ccm_info },
1417 { POLARSSL_CIPHER_CAMELLIA_256_CCM, &camellia_256_ccm_info },
1418#endif
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001419#endif /* POLARSSL_CAMELLIA_C */
1420
1421#if defined(POLARSSL_DES_C)
1422 { POLARSSL_CIPHER_DES_ECB, &des_ecb_info },
1423 { POLARSSL_CIPHER_DES_EDE_ECB, &des_ede_ecb_info },
1424 { POLARSSL_CIPHER_DES_EDE3_ECB, &des_ede3_ecb_info },
1425#if defined(POLARSSL_CIPHER_MODE_CBC)
1426 { POLARSSL_CIPHER_DES_CBC, &des_cbc_info },
1427 { POLARSSL_CIPHER_DES_EDE_CBC, &des_ede_cbc_info },
1428 { POLARSSL_CIPHER_DES_EDE3_CBC, &des_ede3_cbc_info },
1429#endif
1430#endif /* POLARSSL_DES_C */
1431
1432#if defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard057e0cf2013-10-14 14:19:31 +02001433 { POLARSSL_CIPHER_NULL, &null_cipher_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001434#endif /* POLARSSL_CIPHER_NULL_CIPHER */
1435
Manuel Pégourié-Gonnarda2733712015-02-10 17:32:14 +01001436 { POLARSSL_CIPHER_NONE, NULL }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001437};
1438
1439#define NUM_CIPHERS sizeof cipher_definitions / sizeof cipher_definitions[0]
1440int supported_ciphers[NUM_CIPHERS];
1441
Paul Bakker9af723c2014-05-01 13:03:14 +02001442#endif /* POLARSSL_CIPHER_C */