2 * Copyright (C) 2010-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GNUTLS.
8 * The GNUTLS library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 /* Here lie nettle's wrappers for cipher support.
26 #include <gnutls_int.h>
27 #include <gnutls_errors.h>
28 #include <gnutls_cipher_int.h>
29 #include <nettle/aes.h>
30 #include <nettle/camellia.h>
31 #include <nettle/arcfour.h>
32 #include <nettle/arctwo.h>
33 #include <nettle/des.h>
34 #include <nettle/nettle-meta.h>
35 #include <nettle/cbc.h>
36 #include <nettle/gcm.h>
38 /* Functions that refer to the nettle library.
41 #define MAX_BLOCK_SIZE 32
43 typedef void (*encrypt_func
) (void *, nettle_crypt_func
, unsigned, uint8_t *,
44 unsigned, uint8_t *, const uint8_t *);
45 typedef void (*decrypt_func
) (void *, nettle_crypt_func
, unsigned, uint8_t *,
46 unsigned, uint8_t *, const uint8_t *);
47 typedef void (*auth_func
) (void *, unsigned, const uint8_t *);
49 typedef void (*tag_func
) (void *, unsigned, uint8_t *);
51 typedef void (*setkey_func
) (void *, unsigned, const uint8_t *);
54 stream_encrypt (void *ctx
, nettle_crypt_func func
, unsigned block_size
,
55 uint8_t * iv
, unsigned length
, uint8_t * dst
,
58 func (ctx
, length
, dst
, src
);
61 struct nettle_cipher_ctx
66 struct camellia_ctx camellia
;
67 struct arcfour_ctx arcfour
;
68 struct arctwo_ctx arctwo
;
71 struct gcm_aes_ctx aes_gcm
;
74 uint8_t iv
[MAX_BLOCK_SIZE
];
75 gnutls_cipher_algorithm_t algo
;
77 nettle_crypt_func
*i_encrypt
;
78 nettle_crypt_func
*i_decrypt
;
86 #define GCM_DEFAULT_NONCE_SIZE 12
88 static void _gcm_encrypt(void *_ctx
, nettle_crypt_func f
,
89 unsigned block_size
, uint8_t *iv
,
90 unsigned length
, uint8_t *dst
,
93 gcm_aes_encrypt(_ctx
, length
, dst
, src
);
96 static void _gcm_decrypt(void *_ctx
, nettle_crypt_func f
,
97 unsigned block_size
, uint8_t *iv
,
98 unsigned length
, uint8_t *dst
,
101 gcm_aes_decrypt(_ctx
, length
, dst
, src
);
104 static int wrap_nettle_cipher_exists(gnutls_cipher_algorithm_t algo
)
108 case GNUTLS_CIPHER_AES_128_GCM
:
109 case GNUTLS_CIPHER_AES_256_GCM
:
110 case GNUTLS_CIPHER_CAMELLIA_128_CBC
:
111 case GNUTLS_CIPHER_CAMELLIA_192_CBC
:
112 case GNUTLS_CIPHER_CAMELLIA_256_CBC
:
113 case GNUTLS_CIPHER_AES_128_CBC
:
114 case GNUTLS_CIPHER_AES_192_CBC
:
115 case GNUTLS_CIPHER_AES_256_CBC
:
116 case GNUTLS_CIPHER_3DES_CBC
:
117 case GNUTLS_CIPHER_DES_CBC
:
118 case GNUTLS_CIPHER_ARCFOUR_128
:
119 case GNUTLS_CIPHER_ARCFOUR_40
:
120 case GNUTLS_CIPHER_RC2_40_CBC
:
128 wrap_nettle_cipher_init (gnutls_cipher_algorithm_t algo
, void **_ctx
, int enc
)
130 struct nettle_cipher_ctx
*ctx
;
132 ctx
= gnutls_calloc (1, sizeof (*ctx
));
136 return GNUTLS_E_MEMORY_ERROR
;
144 case GNUTLS_CIPHER_AES_128_GCM
:
145 case GNUTLS_CIPHER_AES_256_GCM
:
146 ctx
->encrypt
= _gcm_encrypt
;
147 ctx
->decrypt
= _gcm_decrypt
;
148 ctx
->i_encrypt
= (nettle_crypt_func
*) aes_encrypt
;
149 ctx
->auth
= (auth_func
)gcm_aes_update
;
150 ctx
->tag
= (tag_func
)gcm_aes_digest
;
151 ctx
->ctx_ptr
= &ctx
->ctx
.aes_gcm
;
152 ctx
->block_size
= AES_BLOCK_SIZE
;
154 case GNUTLS_CIPHER_CAMELLIA_128_CBC
:
155 case GNUTLS_CIPHER_CAMELLIA_192_CBC
:
156 case GNUTLS_CIPHER_CAMELLIA_256_CBC
:
157 ctx
->encrypt
= cbc_encrypt
;
158 ctx
->decrypt
= cbc_decrypt
;
159 ctx
->i_encrypt
= (nettle_crypt_func
*)camellia_crypt
;
160 ctx
->i_decrypt
= (nettle_crypt_func
*)camellia_crypt
;
161 ctx
->ctx_ptr
= &ctx
->ctx
.camellia
;
162 ctx
->block_size
= CAMELLIA_BLOCK_SIZE
;
164 case GNUTLS_CIPHER_AES_128_CBC
:
165 case GNUTLS_CIPHER_AES_192_CBC
:
166 case GNUTLS_CIPHER_AES_256_CBC
:
167 ctx
->encrypt
= cbc_encrypt
;
168 ctx
->decrypt
= cbc_decrypt
;
169 ctx
->i_encrypt
= (nettle_crypt_func
*)aes_encrypt
;
170 ctx
->i_decrypt
= (nettle_crypt_func
*)aes_decrypt
;
171 ctx
->ctx_ptr
= &ctx
->ctx
.aes
;
172 ctx
->block_size
= AES_BLOCK_SIZE
;
174 case GNUTLS_CIPHER_3DES_CBC
:
175 ctx
->encrypt
= cbc_encrypt
;
176 ctx
->decrypt
= cbc_decrypt
;
177 ctx
->i_encrypt
= (nettle_crypt_func
*) des3_encrypt
;
178 ctx
->i_decrypt
= (nettle_crypt_func
*) des3_decrypt
;
179 ctx
->ctx_ptr
= &ctx
->ctx
.des3
;
180 ctx
->block_size
= DES3_BLOCK_SIZE
;
182 case GNUTLS_CIPHER_DES_CBC
:
183 ctx
->encrypt
= cbc_encrypt
;
184 ctx
->decrypt
= cbc_decrypt
;
185 ctx
->i_encrypt
= (nettle_crypt_func
*) des_encrypt
;
186 ctx
->i_decrypt
= (nettle_crypt_func
*) des_decrypt
;
187 ctx
->ctx_ptr
= &ctx
->ctx
.des
;
188 ctx
->block_size
= DES_BLOCK_SIZE
;
190 case GNUTLS_CIPHER_ARCFOUR_128
:
191 case GNUTLS_CIPHER_ARCFOUR_40
:
192 ctx
->encrypt
= stream_encrypt
;
193 ctx
->decrypt
= stream_encrypt
;
194 ctx
->i_encrypt
= (nettle_crypt_func
*) arcfour_crypt
;
195 ctx
->i_decrypt
= (nettle_crypt_func
*) arcfour_crypt
;
196 ctx
->ctx_ptr
= &ctx
->ctx
.arcfour
;
199 case GNUTLS_CIPHER_RC2_40_CBC
:
200 ctx
->encrypt
= cbc_encrypt
;
201 ctx
->decrypt
= cbc_decrypt
;
202 ctx
->i_encrypt
= (nettle_crypt_func
*) arctwo_encrypt
;
203 ctx
->i_decrypt
= (nettle_crypt_func
*) arctwo_decrypt
;
204 ctx
->ctx_ptr
= &ctx
->ctx
.arctwo
;
205 ctx
->block_size
= ARCTWO_BLOCK_SIZE
;
210 return GNUTLS_E_INVALID_REQUEST
;
219 wrap_nettle_cipher_setkey (void *_ctx
, const void *key
, size_t keysize
)
221 struct nettle_cipher_ctx
*ctx
= _ctx
;
222 uint8_t des_key
[DES3_KEY_SIZE
];
226 case GNUTLS_CIPHER_AES_128_GCM
:
227 case GNUTLS_CIPHER_AES_256_GCM
:
228 gcm_aes_set_key(&ctx
->ctx
.aes_gcm
, keysize
, key
);
230 case GNUTLS_CIPHER_AES_128_CBC
:
231 case GNUTLS_CIPHER_AES_192_CBC
:
232 case GNUTLS_CIPHER_AES_256_CBC
:
234 aes_set_encrypt_key (ctx
->ctx_ptr
, keysize
, key
);
236 aes_set_decrypt_key (ctx
->ctx_ptr
, keysize
, key
);
238 case GNUTLS_CIPHER_CAMELLIA_128_CBC
:
239 case GNUTLS_CIPHER_CAMELLIA_192_CBC
:
240 case GNUTLS_CIPHER_CAMELLIA_256_CBC
:
242 camellia_set_encrypt_key (ctx
->ctx_ptr
, keysize
, key
);
244 camellia_set_decrypt_key (ctx
->ctx_ptr
, keysize
, key
);
246 case GNUTLS_CIPHER_3DES_CBC
:
247 if (keysize
!= DES3_KEY_SIZE
)
250 return GNUTLS_E_INTERNAL_ERROR
;
253 des_fix_parity (keysize
, des_key
, key
);
255 /* this fails on weak keys */
256 if (des3_set_key (ctx
->ctx_ptr
, des_key
) != 1)
259 return GNUTLS_E_INTERNAL_ERROR
;
262 case GNUTLS_CIPHER_DES_CBC
:
263 if (keysize
!= DES_KEY_SIZE
)
266 return GNUTLS_E_INTERNAL_ERROR
;
269 des_fix_parity (keysize
, des_key
, key
);
271 if (des_set_key (ctx
->ctx_ptr
, des_key
) != 1)
274 return GNUTLS_E_INTERNAL_ERROR
;
277 case GNUTLS_CIPHER_ARCFOUR_128
:
278 case GNUTLS_CIPHER_ARCFOUR_40
:
279 arcfour_set_key (ctx
->ctx_ptr
, keysize
, key
);
281 case GNUTLS_CIPHER_RC2_40_CBC
:
282 arctwo_set_key (ctx
->ctx_ptr
, keysize
, key
);
286 return GNUTLS_E_INVALID_REQUEST
;
293 wrap_nettle_cipher_setiv (void *_ctx
, const void *iv
, size_t ivsize
)
295 struct nettle_cipher_ctx
*ctx
= _ctx
;
299 case GNUTLS_CIPHER_AES_128_GCM
:
300 case GNUTLS_CIPHER_AES_256_GCM
:
301 if (ivsize
!= GCM_DEFAULT_NONCE_SIZE
)
304 return GNUTLS_E_INVALID_REQUEST
;
307 gcm_aes_set_iv(&ctx
->ctx
.aes_gcm
, GCM_DEFAULT_NONCE_SIZE
, iv
);
310 if (ivsize
> ctx
->block_size
)
313 return GNUTLS_E_INVALID_REQUEST
;
316 memcpy (ctx
->iv
, iv
, ivsize
);
323 wrap_nettle_cipher_decrypt (void *_ctx
, const void *encr
, size_t encrsize
,
324 void *plain
, size_t plainsize
)
326 struct nettle_cipher_ctx
*ctx
= _ctx
;
328 ctx
->decrypt (ctx
->ctx_ptr
, ctx
->i_decrypt
, ctx
->block_size
, ctx
->iv
,
329 encrsize
, plain
, encr
);
335 wrap_nettle_cipher_encrypt (void *_ctx
, const void *plain
, size_t plainsize
,
336 void *encr
, size_t encrsize
)
338 struct nettle_cipher_ctx
*ctx
= _ctx
;
340 ctx
->encrypt (ctx
->ctx_ptr
, ctx
->i_encrypt
, ctx
->block_size
, ctx
->iv
,
341 plainsize
, encr
, plain
);
347 wrap_nettle_cipher_auth (void *_ctx
, const void *plain
, size_t plainsize
)
349 struct nettle_cipher_ctx
*ctx
= _ctx
;
351 ctx
->auth (ctx
->ctx_ptr
, plainsize
, plain
);
357 wrap_nettle_cipher_tag (void *_ctx
, void *tag
, size_t tagsize
)
359 struct nettle_cipher_ctx
*ctx
= _ctx
;
361 ctx
->tag (ctx
->ctx_ptr
, tagsize
, tag
);
366 wrap_nettle_cipher_close (void *h
)
371 gnutls_crypto_cipher_st _gnutls_cipher_ops
= {
372 .init
= wrap_nettle_cipher_init
,
373 .exists
= wrap_nettle_cipher_exists
,
374 .setiv
= wrap_nettle_cipher_setiv
,
375 .setkey
= wrap_nettle_cipher_setkey
,
376 .encrypt
= wrap_nettle_cipher_encrypt
,
377 .decrypt
= wrap_nettle_cipher_decrypt
,
378 .deinit
= wrap_nettle_cipher_close
,
379 .auth
= wrap_nettle_cipher_auth
,
380 .tag
= wrap_nettle_cipher_tag
,