Fix API.
[shishi.git] / lib / crypto-aes.c
blob8c2640bf2437c34aeadb51fc1653608ceaea2535
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.
24 static int
25 aes128_encrypt (Shishi * handle,
26 Shishi_key * key,
27 int keyusage,
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);
36 static int
37 aes128_decrypt (Shishi * handle,
38 Shishi_key * key,
39 int keyusage,
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);
48 static int
49 aes256_encrypt (Shishi * handle,
50 Shishi_key * key,
51 int keyusage,
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);
60 static int
61 aes256_decrypt (Shishi * handle,
62 Shishi_key * key,
63 int keyusage,
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);
72 static int
73 aes_string_to_key (Shishi * handle,
74 const char *password,
75 size_t passwordlen,
76 const char *salt,
77 size_t saltlen, const char *parameter, Shishi_key * outkey)
79 unsigned char key[256 / 8];
80 int keylen = shishi_key_length (outkey);
81 Shishi_key *tmpkey;
82 int iterations = 0x0000b000;
83 int res;
85 if (parameter)
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))
95 puts ("");
96 printf ("aes_string_to_key (password, salt)\n");
97 printf ("\t ;; Password:\n");
98 escapeprint (password, passwordlen);
99 hexprint (password, passwordlen);
100 puts ("");
101 printf ("\t ;; Salt:\n");
102 escapeprint (salt, saltlen);
103 hexprint (salt, saltlen);
104 puts ("");
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)
112 return res;
114 res =
115 shishi_key_from_value (handle, shishi_key_type (outkey), key, &tmpkey);
116 if (res != SHISHI_OK)
117 return res;
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)
125 return res;
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));
132 puts ("");
133 binprint (shishi_key_value (outkey), shishi_key_length (outkey));
134 puts ("");
137 return SHISHI_OK;
140 static int
141 aes128_string_to_key (Shishi * handle,
142 const char *password,
143 size_t passwordlen,
144 const char *salt,
145 size_t saltlen,
146 const char *parameter, Shishi_key * outkey)
148 return aes_string_to_key (handle, password, passwordlen,
149 salt, saltlen, parameter, outkey);
152 static int
153 aes256_string_to_key (Shishi * handle,
154 const char *password,
155 size_t passwordlen,
156 const char *salt,
157 size_t saltlen,
158 const char *parameter, Shishi_key * outkey)
160 return aes_string_to_key (handle, password, passwordlen,
161 salt, saltlen, parameter, outkey);
164 static int
165 aes128_random_to_key (Shishi * handle,
166 const char *random,
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);
174 return SHISHI_OK;
177 static int
178 aes256_random_to_key (Shishi * handle,
179 const char *random,
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);
187 return SHISHI_OK;
190 static int
191 aes128_checksum (Shishi * handle,
192 Shishi_key * key,
193 int keyusage,
194 int cksumtype,
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);
201 static int
202 aes256_checksum (Shishi * handle,
203 Shishi_key * key,
204 int keyusage,
205 int cksumtype,
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);