New interface for AES-256. Also deleted old aes-meta.c.
[gd/nettle] / nettle-internal.c
1 /* nettle-internal.c
2  *
3  * Things that are used only by the testsuite and benchmark, and
4  * subject to change.
5  */
6
7 /* nettle, low-level cryptographics library
8  *
9  * Copyright (C) 2002 Niels Möller
10  *  
11  * The nettle library is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation; either version 2.1 of the License, or (at your
14  * option) any later version.
15  * 
16  * The nettle library is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
19  * License for more details.
20  * 
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with the nettle library; see the file COPYING.LIB.  If not, write to
23  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24  * MA 02111-1301, USA.
25  */
26
27 #if HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <assert.h>
32 #include <stdlib.h>
33
34 #include "nettle-internal.h"
35 #include "blowfish.h"
36 #include "des.h"
37 #include "gcm.h"
38 #include "salsa20.h"
39
40 /* DES uses a different signature for the key set function. We ignore
41    the return value indicating weak keys. */
42 static void
43 des_set_key_hack(void *ctx, size_t length, const uint8_t *key)
44 {
45   assert(length == DES_KEY_SIZE);
46   des_set_key(ctx, key);
47 }
48
49 static void
50 des3_set_key_hack(void *ctx, size_t length, const uint8_t *key)
51 {
52   assert(length == DES3_KEY_SIZE);
53   des3_set_key(ctx, key);
54 }
55
56 /* NOTE: A bit ugly. Ignores weak keys, and pretends the set:key
57    functions have no return value. */
58 const struct nettle_cipher
59 nettle_des = {
60   "des", sizeof(struct des_ctx),
61   DES_BLOCK_SIZE, DES_KEY_SIZE,
62   des_set_key_hack, des_set_key_hack,
63   (nettle_crypt_func *) des_encrypt,
64   (nettle_crypt_func *) des_decrypt
65 };
66
67 const struct nettle_cipher
68 nettle_des3 = {
69  "des3", sizeof(struct des3_ctx),
70  DES3_BLOCK_SIZE, DES3_KEY_SIZE,
71  des3_set_key_hack, des3_set_key_hack,
72  (nettle_crypt_func *) des3_encrypt,
73  (nettle_crypt_func *) des3_decrypt
74 };
75
76 /* NOTE: This is not as nice as one might think, as we pretend
77    blowfish_set_key has no return value. */
78 const struct nettle_cipher
79 nettle_blowfish128 = _NETTLE_CIPHER(blowfish, BLOWFISH, 128);
80
81 /* Sets a fix zero iv. For benchmarking only. */
82 static void
83 salsa20_set_key_hack(void *ctx, size_t length, const uint8_t *key)
84 {
85   static const uint8_t iv[SALSA20_IV_SIZE];
86   salsa20_set_key (ctx, length, key);
87   salsa20_set_iv (ctx, iv);
88 }
89
90 /* Claim zero block size, to classify as a stream cipher. */
91 const struct nettle_cipher
92 nettle_salsa20 = {
93   "salsa20", sizeof(struct salsa20_ctx),
94   0, SALSA20_KEY_SIZE,
95   salsa20_set_key_hack, salsa20_set_key_hack,
96   (nettle_crypt_func *) salsa20_crypt,
97   (nettle_crypt_func *) salsa20_crypt
98 };
99
100 const struct nettle_cipher
101 nettle_salsa20r12 = {
102   "salsa20r12", sizeof(struct salsa20_ctx),
103   0, SALSA20_KEY_SIZE,
104   salsa20_set_key_hack, salsa20_set_key_hack,
105   (nettle_crypt_func *) salsa20r12_crypt,
106   (nettle_crypt_func *) salsa20r12_crypt
107 };
108
109 const struct nettle_aead
110 nettle_gcm_aes128 = _NETTLE_AEAD(gcm, GCM, aes, 128);
111 const struct nettle_aead
112 nettle_gcm_aes192 = _NETTLE_AEAD(gcm, GCM, aes, 192);
113 const struct nettle_aead
114 nettle_gcm_aes256 = _NETTLE_AEAD(gcm, GCM, aes, 256);
115
116 /* Old, unified, interface */
117 const struct nettle_cipher nettle_unified_aes128
118 = _NETTLE_CIPHER_SEP(aes, AES, 128);
119
120 const struct nettle_cipher nettle_unified_aes192
121 = _NETTLE_CIPHER_SEP(aes, AES, 192);
122
123 const struct nettle_cipher nettle_unified_aes256
124 = _NETTLE_CIPHER_SEP(aes, AES, 256);