1 /* crypto-aes.c AES crypto functions
2 * Copyright (C) 2002, 2003 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 aes128_encrypt (Shishi
* handle
,
30 const char *iv
, size_t ivlen
,
31 char **ivout
, size_t * ivoutlen
,
32 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
34 return _shishi_simplified_encrypt (handle
, key
, keyusage
, iv
, ivlen
, ivout
,
35 ivoutlen
, in
, inlen
, out
, outlen
);
39 aes128_decrypt (Shishi
* handle
,
42 const char *iv
, size_t ivlen
,
43 char **ivout
, size_t * ivoutlen
,
44 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
46 return _shishi_simplified_decrypt (handle
, key
, keyusage
, iv
, ivlen
, ivout
,
47 ivoutlen
, in
, inlen
, out
, outlen
);
51 aes256_encrypt (Shishi
* handle
,
54 const char *iv
, size_t ivlen
,
55 char **ivout
, size_t * ivoutlen
,
56 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
58 return _shishi_simplified_encrypt (handle
, key
, keyusage
, iv
, ivlen
, ivout
,
59 ivoutlen
, in
, inlen
, out
, outlen
);
63 aes256_decrypt (Shishi
* handle
,
66 const char *iv
, size_t ivlen
,
67 char **ivout
, size_t * ivoutlen
,
68 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
70 return _shishi_simplified_decrypt (handle
, key
, keyusage
, iv
, ivlen
, ivout
,
71 ivoutlen
, in
, inlen
, out
, outlen
);
75 aes_string_to_key (Shishi
* handle
,
79 size_t saltlen
, const char *parameter
, Shishi_key
* outkey
)
81 unsigned char key
[256 / 8];
82 int keylen
= shishi_key_length (outkey
);
84 int iterations
= 0x0000b000;
89 iterations
= (parameter
[0] & 0xFF) << 24;
90 iterations
|= (parameter
[1] & 0xFF) << 16;
91 iterations
|= (parameter
[2] & 0xFF) << 8;
92 iterations
|= parameter
[3] & 0xFF;
95 if (VERBOSECRYPTO (handle
))
97 printf ("aes_string_to_key (password, salt)\n");
98 printf ("\t ;; Password:\n");
99 _shishi_escapeprint (password
, passwordlen
);
100 _shishi_hexprint (password
, passwordlen
);
101 printf ("\t ;; Salt:\n");
102 _shishi_escapeprint (salt
, saltlen
);
103 _shishi_hexprint (salt
, saltlen
);
104 printf ("\t ;; Iteration count %d (%08x):\n", iterations
, iterations
);
107 /* tkey = random2key(PBKDF2(passphrase, salt, iter_count, keylength)) */
108 res
= shishi_pbkdf2_sha1 (handle
, password
, passwordlen
, salt
, saltlen
,
109 iterations
, keylen
, key
);
110 if (res
!= SHISHI_OK
)
113 res
= shishi_key_from_value (handle
, shishi_key_type (outkey
),
115 if (res
!= SHISHI_OK
)
118 /* key = DK(tkey, "kerberos") */
119 res
= shishi_dk (handle
, tmpkey
, "kerberos", strlen ("kerberos"), outkey
);
121 shishi_key_done (tmpkey
);
123 if (res
!= SHISHI_OK
)
126 if (VERBOSECRYPTO (handle
))
128 printf ("aes_string_to_key (password, salt)\n");
129 printf ("\t ;; Key:\n");
130 _shishi_hexprint (shishi_key_value (outkey
),
131 shishi_key_length (outkey
));
132 _shishi_binprint (shishi_key_value (outkey
),
133 shishi_key_length (outkey
));
140 aes128_string_to_key (Shishi
* handle
,
141 const char *password
,
145 const char *parameter
, Shishi_key
* outkey
)
147 return aes_string_to_key (handle
, password
, passwordlen
,
148 salt
, saltlen
, parameter
, outkey
);
152 aes256_string_to_key (Shishi
* handle
,
153 const char *password
,
157 const char *parameter
, Shishi_key
* outkey
)
159 return aes_string_to_key (handle
, password
, passwordlen
,
160 salt
, saltlen
, parameter
, outkey
);
164 aes128_random_to_key (Shishi
* handle
,
166 size_t randomlen
, Shishi_key
* outkey
)
168 if (randomlen
< shishi_key_length (outkey
))
169 return SHISHI_CRYPTO_ERROR
;
171 shishi_key_value_set (outkey
, random
);
177 aes256_random_to_key (Shishi
* handle
,
179 size_t randomlen
, Shishi_key
* outkey
)
181 if (randomlen
< shishi_key_length (outkey
))
182 return SHISHI_CRYPTO_ERROR
;
184 shishi_key_value_set (outkey
, random
);
190 aes128_checksum (Shishi
* handle
,
194 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
196 return _shishi_simplified_checksum (handle
, key
, keyusage
, cksumtype
,
197 in
, inlen
, out
, outlen
);
201 aes256_checksum (Shishi
* handle
,
205 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
207 return _shishi_simplified_checksum (handle
, key
, keyusage
, cksumtype
,
208 in
, inlen
, out
, outlen
);
211 cipherinfo aes128_cts_hmac_sha1_96_info
= {
212 SHISHI_AES128_CTS_HMAC_SHA1_96
,
213 "aes128-cts-hmac-sha1-96",
219 SHISHI_HMAC_SHA1_96_AES128
,
220 aes128_random_to_key
,
221 aes128_string_to_key
,
226 cipherinfo aes256_cts_hmac_sha1_96_info
= {
227 SHISHI_AES256_CTS_HMAC_SHA1_96
,
228 "aes256-cts-hmac-sha1-96",
234 SHISHI_HMAC_SHA1_96_AES256
,
235 aes256_random_to_key
,
236 aes256_string_to_key
,
241 checksuminfo hmac_sha1_96_aes128_info
= {
242 SHISHI_HMAC_SHA1_96_AES128
,
243 "hmac-sha1-96-aes128",
248 checksuminfo hmac_sha1_96_aes256_info
= {
249 SHISHI_HMAC_SHA1_96_AES256
,
250 "hmac-sha1-96-aes256",