Add.
[shishi.git] / lib / crypto-aes.c
blobe814f94e22050606b2f521bcdc510b43927707e8
1 /* crypto-aes.c --- AES crypto functions.
2 * Copyright (C) 2002, 2003, 2004, 2005 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "internal.h"
24 /* Get prototypes. */
25 #include "crypto.h"
27 /* Get _shishi_escapeprint, etc. */
28 #include "utils.h"
30 static int
31 aes128_encrypt (Shishi * handle,
32 Shishi_key * key,
33 int keyusage,
34 const char *iv, size_t ivlen,
35 char **ivout, size_t * ivoutlen,
36 const char *in, size_t inlen, char **out, size_t * outlen)
38 return _shishi_simplified_encrypt (handle, key, keyusage, iv, ivlen, ivout,
39 ivoutlen, in, inlen, out, outlen);
42 static int
43 aes128_decrypt (Shishi * handle,
44 Shishi_key * key,
45 int keyusage,
46 const char *iv, size_t ivlen,
47 char **ivout, size_t * ivoutlen,
48 const char *in, size_t inlen, char **out, size_t * outlen)
50 return _shishi_simplified_decrypt (handle, key, keyusage, iv, ivlen, ivout,
51 ivoutlen, in, inlen, out, outlen);
54 static int
55 aes256_encrypt (Shishi * handle,
56 Shishi_key * key,
57 int keyusage,
58 const char *iv, size_t ivlen,
59 char **ivout, size_t * ivoutlen,
60 const char *in, size_t inlen, char **out, size_t * outlen)
62 return _shishi_simplified_encrypt (handle, key, keyusage, iv, ivlen, ivout,
63 ivoutlen, in, inlen, out, outlen);
66 static int
67 aes256_decrypt (Shishi * handle,
68 Shishi_key * key,
69 int keyusage,
70 const char *iv, size_t ivlen,
71 char **ivout, size_t * ivoutlen,
72 const char *in, size_t inlen, char **out, size_t * outlen)
74 return _shishi_simplified_decrypt (handle, key, keyusage, iv, ivlen, ivout,
75 ivoutlen, in, inlen, out, outlen);
78 static int
79 aes_string_to_key (Shishi * handle,
80 const char *password,
81 size_t passwordlen,
82 const char *salt,
83 size_t saltlen, const char *parameter, Shishi_key * outkey)
85 char key[256 / 8];
86 int keylen = shishi_key_length (outkey);
87 Shishi_key *tmpkey;
88 int iterations = 0x00001000;
89 int res;
91 if (parameter)
93 iterations = (parameter[0] & 0xFF) << 24;
94 iterations |= (parameter[1] & 0xFF) << 16;
95 iterations |= (parameter[2] & 0xFF) << 8;
96 iterations |= parameter[3] & 0xFF;
99 if (VERBOSECRYPTO (handle))
101 printf ("aes_string_to_key (password, salt)\n");
102 printf ("\t ;; Password:\n");
103 _shishi_escapeprint (password, passwordlen);
104 _shishi_hexprint (password, passwordlen);
105 printf ("\t ;; Salt:\n");
106 _shishi_escapeprint (salt, saltlen);
107 _shishi_hexprint (salt, saltlen);
108 printf ("\t ;; Iteration count %d (%08x):\n", iterations, iterations);
111 /* tkey = random2key(PBKDF2(passphrase, salt, iter_count, keylength)) */
112 res = shishi_pbkdf2_sha1 (handle, password, passwordlen, salt, saltlen,
113 iterations, keylen, key);
114 if (res != SHISHI_OK)
115 return res;
117 res = shishi_key_from_value (handle, shishi_key_type (outkey),
118 key, &tmpkey);
119 if (res != SHISHI_OK)
120 return res;
122 /* key = DK(tkey, Constant) */
123 res = shishi_dk (handle, tmpkey, SHISHI_DK_CONSTANT,
124 strlen (SHISHI_DK_CONSTANT), outkey);
126 shishi_key_done (tmpkey);
128 if (res != SHISHI_OK)
129 return res;
131 if (VERBOSECRYPTO (handle))
133 printf ("aes_string_to_key (password, salt)\n");
134 printf ("\t ;; Key:\n");
135 _shishi_hexprint (shishi_key_value (outkey),
136 shishi_key_length (outkey));
137 _shishi_binprint (shishi_key_value (outkey),
138 shishi_key_length (outkey));
141 return SHISHI_OK;
144 static int
145 aes128_string_to_key (Shishi * handle,
146 const char *password,
147 size_t passwordlen,
148 const char *salt,
149 size_t saltlen,
150 const char *parameter, Shishi_key * outkey)
152 return aes_string_to_key (handle, password, passwordlen,
153 salt, saltlen, parameter, outkey);
156 static int
157 aes256_string_to_key (Shishi * handle,
158 const char *password,
159 size_t passwordlen,
160 const char *salt,
161 size_t saltlen,
162 const char *parameter, Shishi_key * outkey)
164 return aes_string_to_key (handle, password, passwordlen,
165 salt, saltlen, parameter, outkey);
168 static int
169 aes128_random_to_key (Shishi * handle,
170 const char *rnd, size_t rndlen, Shishi_key * outkey)
172 if (rndlen < shishi_key_length (outkey))
173 return SHISHI_CRYPTO_ERROR;
175 shishi_key_value_set (outkey, rnd);
177 return SHISHI_OK;
180 static int
181 aes256_random_to_key (Shishi * handle,
182 const char *rnd, size_t rndlen, Shishi_key * outkey)
184 if (rndlen < shishi_key_length (outkey))
185 return SHISHI_CRYPTO_ERROR;
187 shishi_key_value_set (outkey, rnd);
189 return SHISHI_OK;
192 static int
193 aes128_checksum (Shishi * handle,
194 Shishi_key * key,
195 int keyusage,
196 int cksumtype,
197 const char *in, size_t inlen, char **out, size_t * outlen)
199 return _shishi_simplified_checksum (handle, key, keyusage, cksumtype,
200 in, inlen, out, outlen);
203 static int
204 aes256_checksum (Shishi * handle,
205 Shishi_key * key,
206 int keyusage,
207 int cksumtype,
208 const char *in, size_t inlen, char **out, size_t * outlen)
210 return _shishi_simplified_checksum (handle, key, keyusage, cksumtype,
211 in, inlen, out, outlen);
214 cipherinfo aes128_cts_hmac_sha1_96_info = {
215 SHISHI_AES128_CTS_HMAC_SHA1_96,
216 "aes128-cts-hmac-sha1-96",
219 128 / 8,
220 128 / 8,
221 SHISHI_HMAC_SHA1_96_AES128,
222 aes128_random_to_key,
223 aes128_string_to_key,
224 aes128_encrypt,
225 aes128_decrypt
228 cipherinfo aes256_cts_hmac_sha1_96_info = {
229 SHISHI_AES256_CTS_HMAC_SHA1_96,
230 "aes256-cts-hmac-sha1-96",
233 256 / 8,
234 256 / 8,
235 SHISHI_HMAC_SHA1_96_AES256,
236 aes256_random_to_key,
237 aes256_string_to_key,
238 aes256_encrypt,
239 aes256_decrypt
242 checksuminfo hmac_sha1_96_aes128_info = {
243 SHISHI_HMAC_SHA1_96_AES128,
244 "hmac-sha1-96-aes128",
245 96 / 8,
246 aes128_checksum
249 checksuminfo hmac_sha1_96_aes256_info = {
250 SHISHI_HMAC_SHA1_96_AES256,
251 "hmac-sha1-96-aes256",
252 96 / 8,
253 aes256_checksum