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 char **ivout
, size_t * ivoutlen
,
30 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
32 return simplified_encrypt (handle
, key
, keyusage
, iv
, ivlen
, ivout
,
33 ivoutlen
, in
, inlen
, out
, outlen
);
37 aes128_decrypt (Shishi
* handle
,
40 const char *iv
, size_t ivlen
,
41 char **ivout
, size_t * ivoutlen
,
42 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
44 return simplified_decrypt (handle
, key
, keyusage
, iv
, ivlen
, ivout
,
45 ivoutlen
, in
, inlen
, out
, outlen
);
49 aes256_encrypt (Shishi
* handle
,
52 const char *iv
, size_t ivlen
,
53 char **ivout
, size_t * ivoutlen
,
54 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
56 return simplified_encrypt (handle
, key
, keyusage
, iv
, ivlen
, ivout
,
57 ivoutlen
, in
, inlen
, out
, outlen
);
61 aes256_decrypt (Shishi
* handle
,
64 const char *iv
, size_t ivlen
,
65 char **ivout
, size_t * ivoutlen
,
66 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
68 return simplified_decrypt (handle
, key
, keyusage
, iv
, ivlen
, ivout
,
69 ivoutlen
, in
, inlen
, out
, outlen
);
73 aes_string_to_key (Shishi
* handle
,
77 size_t saltlen
, const char *parameter
, Shishi_key
* outkey
)
79 unsigned char key
[256 / 8];
80 int keylen
= shishi_key_length (outkey
);
82 int iterations
= 0x0000b000;
87 iterations
= (parameter
[0] & 0xFF) << 24;
88 iterations
|= (parameter
[1] & 0xFF) << 16;
89 iterations
|= (parameter
[2] & 0xFF) << 8;
90 iterations
|= parameter
[3] & 0xFF;
93 if (VERBOSECRYPTO (handle
))
96 printf ("aes_string_to_key (password, salt)\n");
97 printf ("\t ;; Password:\n");
98 escapeprint (password
, passwordlen
);
99 hexprint (password
, passwordlen
);
101 printf ("\t ;; Salt:\n");
102 escapeprint (salt
, saltlen
);
103 hexprint (salt
, saltlen
);
105 printf ("\t ;; Iteration count %d (%08x):\n", iterations
, iterations
);
108 /* tkey = random2key(PBKDF2(passphrase, salt, iter_count, keylength)) */
109 res
= shishi_pbkdf2_sha1 (password
, passwordlen
, salt
, saltlen
,
110 iterations
, keylen
, key
);
111 if (res
!= SHISHI_OK
)
115 shishi_key_from_value (handle
, shishi_key_type (outkey
), key
, &tmpkey
);
116 if (res
!= SHISHI_OK
)
119 /* key = DK(tkey, "kerberos") */
120 res
= shishi_dk (handle
, tmpkey
, "kerberos", strlen ("kerberos"), outkey
);
122 shishi_key_done (tmpkey
);
124 if (res
!= SHISHI_OK
)
127 if (VERBOSECRYPTO (handle
))
129 printf ("aes_string_to_key (password, salt)\n");
130 printf ("\t ;; Key:\n");
131 hexprint (shishi_key_value (outkey
), shishi_key_length (outkey
));
133 binprint (shishi_key_value (outkey
), shishi_key_length (outkey
));
141 aes128_string_to_key (Shishi
* handle
,
142 const char *password
,
146 const char *parameter
, Shishi_key
* outkey
)
148 return aes_string_to_key (handle
, password
, passwordlen
,
149 salt
, saltlen
, parameter
, outkey
);
153 aes256_string_to_key (Shishi
* handle
,
154 const char *password
,
158 const char *parameter
, Shishi_key
* outkey
)
160 return aes_string_to_key (handle
, password
, passwordlen
,
161 salt
, saltlen
, parameter
, outkey
);
165 aes128_random_to_key (Shishi
* handle
,
167 size_t randomlen
, Shishi_key
* outkey
)
169 if (randomlen
< shishi_key_length (outkey
))
170 return SHISHI_CRYPTO_ERROR
;
172 shishi_key_value_set (outkey
, random
);
178 aes256_random_to_key (Shishi
* handle
,
180 size_t randomlen
, Shishi_key
* outkey
)
182 if (randomlen
< shishi_key_length (outkey
))
183 return SHISHI_CRYPTO_ERROR
;
185 shishi_key_value_set (outkey
, random
);
191 aes128_checksum (Shishi
* handle
,
195 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
197 return simplified_checksum (handle
, key
, keyusage
, cksumtype
,
198 in
, inlen
, out
, outlen
);
202 aes256_checksum (Shishi
* handle
,
206 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
208 return simplified_checksum (handle
, key
, keyusage
, cksumtype
,
209 in
, inlen
, out
, outlen
);