GTK-DOC fixes.
[shishi.git] / lib / crypto-aes.c
blob6bdf379e727b401591b5f4612e72977582053469
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 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);
35 static int
36 aes128_decrypt (Shishi * handle,
37 Shishi_key * key,
38 int keyusage,
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);
46 static int
47 aes256_encrypt (Shishi * handle,
48 Shishi_key * key,
49 int keyusage,
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);
57 static int
58 aes256_decrypt (Shishi * handle,
59 Shishi_key * key,
60 int keyusage,
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);
68 static int
69 aes_string_to_key (Shishi * handle,
70 const char *password,
71 size_t passwordlen,
72 const char *salt,
73 size_t saltlen, const char *parameter, Shishi_key * outkey)
75 unsigned char key[256 / 8];
76 int keylen = shishi_key_length (outkey);
77 Shishi_key *tmpkey;
78 int iterations = 0x0000b000;
79 int res;
81 if (parameter)
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))
91 puts ("");
92 printf ("aes_string_to_key (password, salt)\n");
93 printf ("\t ;; Password:\n");
94 escapeprint (password, passwordlen);
95 hexprint (password, passwordlen);
96 puts ("");
97 printf ("\t ;; Salt:\n");
98 escapeprint (salt, saltlen);
99 hexprint (salt, saltlen);
100 puts ("");
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)
108 return res;
110 res =
111 shishi_key_from_value (handle, shishi_key_type (outkey), key, &tmpkey);
112 if (res != SHISHI_OK)
113 return res;
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)
121 return res;
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));
128 puts ("");
129 binprint (shishi_key_value (outkey), shishi_key_length (outkey));
130 puts ("");
133 return SHISHI_OK;
136 static int
137 aes128_string_to_key (Shishi * handle,
138 const char *password,
139 size_t passwordlen,
140 const char *salt,
141 size_t saltlen,
142 const char *parameter, Shishi_key * outkey)
144 return aes_string_to_key (handle, password, passwordlen,
145 salt, saltlen, parameter, outkey);
148 static int
149 aes256_string_to_key (Shishi * handle,
150 const char *password,
151 size_t passwordlen,
152 const char *salt,
153 size_t saltlen,
154 const char *parameter, Shishi_key * outkey)
156 return aes_string_to_key (handle, password, passwordlen,
157 salt, saltlen, parameter, outkey);
160 static int
161 aes128_random_to_key (Shishi * handle,
162 const char *random,
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);
170 return SHISHI_OK;
173 static int
174 aes256_random_to_key (Shishi * handle,
175 const char *random,
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);
183 return SHISHI_OK;
186 static int
187 aes128_checksum (Shishi * handle,
188 Shishi_key * key,
189 int keyusage,
190 int cksumtype,
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);
198 static int
199 aes256_checksum (Shishi * handle,
200 Shishi_key * key,
201 int keyusage,
202 int cksumtype,
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);