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
20 * Note: This file is #include'd by crypto.c.
25 aes128_encrypt (Shishi
* handle
,
28 const char *iv
, size_t ivlen
,
29 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
31 return simplified_encrypt (handle
, key
, keyusage
, iv
, ivlen
,
32 in
, inlen
, out
, outlen
);
36 aes128_decrypt (Shishi
* handle
,
39 const char *iv
, size_t ivlen
,
40 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
42 return simplified_decrypt (handle
, key
, keyusage
, iv
, ivlen
,
43 in
, inlen
, out
, outlen
);
47 aes256_encrypt (Shishi
* handle
,
50 const char *iv
, size_t ivlen
,
51 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
53 return simplified_encrypt (handle
, key
, keyusage
, iv
, ivlen
,
54 in
, inlen
, out
, outlen
);
58 aes256_decrypt (Shishi
* handle
,
61 const char *iv
, size_t ivlen
,
62 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
64 return simplified_decrypt (handle
, key
, keyusage
, iv
, ivlen
,
65 in
, inlen
, out
, outlen
);
69 aes_string_to_key (Shishi
* handle
,
73 size_t saltlen
, const char *parameter
, Shishi_key
* outkey
)
75 unsigned char key
[256 / 8];
76 int keylen
= shishi_key_length (outkey
);
78 int iterations
= 0x0000b000;
83 iterations
= (parameter
[0] & 0xFF) << 24;
84 iterations
|= (parameter
[1] & 0xFF) << 16;
85 iterations
|= (parameter
[2] & 0xFF) << 8;
86 iterations
|= parameter
[3] & 0xFF;
89 if (VERBOSECRYPTO (handle
))
92 printf ("aes_string_to_key (password, salt)\n");
93 printf ("\t ;; Password:\n");
94 escapeprint (password
, passwordlen
);
95 hexprint (password
, passwordlen
);
97 printf ("\t ;; Salt:\n");
98 escapeprint (salt
, saltlen
);
99 hexprint (salt
, saltlen
);
101 printf ("\t ;; Iteration count %d (%08x):\n", iterations
, iterations
);
104 /* tkey = random2key(PBKDF2(passphrase, salt, iter_count, keylength)) */
105 res
= shishi_pbkdf2_sha1 (password
, passwordlen
, salt
, saltlen
,
106 iterations
, keylen
, key
);
107 if (res
!= SHISHI_OK
)
111 shishi_key_from_value (handle
, shishi_key_type (outkey
), key
, &tmpkey
);
112 if (res
!= SHISHI_OK
)
115 /* key = DK(tkey, "kerberos") */
116 res
= shishi_dk (handle
, tmpkey
, "kerberos", strlen ("kerberos"), outkey
);
118 shishi_key_done (&tmpkey
);
120 if (res
!= SHISHI_OK
)
123 if (VERBOSECRYPTO (handle
))
125 printf ("aes_string_to_key (password, salt)\n");
126 printf ("\t ;; Key:\n");
127 hexprint (shishi_key_value (outkey
), shishi_key_length (outkey
));
129 binprint (shishi_key_value (outkey
), shishi_key_length (outkey
));
137 aes128_string_to_key (Shishi
* handle
,
138 const char *password
,
142 const char *parameter
, Shishi_key
* outkey
)
144 return aes_string_to_key (handle
, password
, passwordlen
,
145 salt
, saltlen
, parameter
, outkey
);
149 aes256_string_to_key (Shishi
* handle
,
150 const char *password
,
154 const char *parameter
, Shishi_key
* outkey
)
156 return aes_string_to_key (handle
, password
, passwordlen
,
157 salt
, saltlen
, parameter
, outkey
);
161 aes128_random_to_key (Shishi
* handle
,
163 size_t randomlen
, Shishi_key
* outkey
)
165 if (randomlen
< shishi_key_length (outkey
))
166 return SHISHI_CRYPTO_ERROR
;
168 shishi_key_value_set (outkey
, random
);
174 aes256_random_to_key (Shishi
* handle
,
176 size_t randomlen
, Shishi_key
* outkey
)
178 if (randomlen
< shishi_key_length (outkey
))
179 return SHISHI_CRYPTO_ERROR
;
181 shishi_key_value_set (outkey
, random
);
187 aes128_checksum (Shishi
* handle
,
191 char *in
, size_t inlen
,
192 char **out
, size_t * outlen
)
194 return simplified_checksum (handle
, key
, keyusage
, cksumtype
,
195 in
, inlen
, out
, outlen
);
199 aes256_checksum (Shishi
* handle
,
203 char *in
, size_t inlen
,
204 char **out
, size_t * outlen
)
206 return simplified_checksum (handle
, key
, keyusage
, cksumtype
,
207 in
, inlen
, out
, outlen
);