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
27 /* Get _shishi_escapeprint, etc. */
31 aes128_encrypt (Shishi
* handle
,
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
);
43 aes128_decrypt (Shishi
* handle
,
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
);
55 aes256_encrypt (Shishi
* handle
,
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
);
67 aes256_decrypt (Shishi
* handle
,
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
);
79 aes_string_to_key (Shishi
* handle
,
83 size_t saltlen
, const char *parameter
, Shishi_key
* outkey
)
86 int keylen
= shishi_key_length (outkey
);
88 int iterations
= 0x00001000;
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
)
117 res
= shishi_key_from_value (handle
, shishi_key_type (outkey
),
119 if (res
!= SHISHI_OK
)
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
)
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
));
145 aes128_string_to_key (Shishi
* handle
,
146 const char *password
,
150 const char *parameter
, Shishi_key
* outkey
)
152 return aes_string_to_key (handle
, password
, passwordlen
,
153 salt
, saltlen
, parameter
, outkey
);
157 aes256_string_to_key (Shishi
* handle
,
158 const char *password
,
162 const char *parameter
, Shishi_key
* outkey
)
164 return aes_string_to_key (handle
, password
, passwordlen
,
165 salt
, saltlen
, parameter
, outkey
);
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
);
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
);
193 aes128_checksum (Shishi
* handle
,
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
);
204 aes256_checksum (Shishi
* handle
,
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",
221 SHISHI_HMAC_SHA1_96_AES128
,
222 aes128_random_to_key
,
223 aes128_string_to_key
,
228 cipherinfo aes256_cts_hmac_sha1_96_info
= {
229 SHISHI_AES256_CTS_HMAC_SHA1_96
,
230 "aes256-cts-hmac-sha1-96",
235 SHISHI_HMAC_SHA1_96_AES256
,
236 aes256_random_to_key
,
237 aes256_string_to_key
,
242 checksuminfo hmac_sha1_96_aes128_info
= {
243 SHISHI_HMAC_SHA1_96_AES128
,
244 "hmac-sha1-96-aes128",
249 checksuminfo hmac_sha1_96_aes256_info
= {
250 SHISHI_HMAC_SHA1_96_AES256
,
251 "hmac-sha1-96-aes256",