Add CBC-MAC.
[shishi.git] / lib / key.c
blob5ba21947ffdae924f36b1cfdaf8ee39ebf417946
1 /* key.c Key related 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
22 #include "internal.h"
24 struct Shishi_key
26 Shishi *handle;
27 char *principal;
28 char *realm;
29 int type;
30 char value[MAX_KEY_LEN];
31 int version;
34 /**
35 * shishi_key_principal:
36 * @key: structure that holds key information
38 * Return value: Returns the principal owning the key. (Not a copy of
39 * it, so don't modify or deallocate it.)
40 **/
41 const char *
42 shishi_key_principal (Shishi_key * key)
44 return key->principal;
47 /**
48 * shishi_key_principal_set:
49 * @key: structure that holds key information
50 * @principal: string with new principal name.
52 * Set the principal owning the key. The string is copied into the
53 * key, so you can dispose of the variable immediately after calling
54 * this function.
55 **/
56 void
57 shishi_key_principal_set (Shishi_key * key, const char *principal)
59 if (key->principal)
60 free (key->principal);
61 if (principal)
62 key->principal = strdup (principal);
63 else
64 key->principal = NULL;
67 /**
68 * shishi_key_realm:
69 * @key: structure that holds key information
71 * Return value: Returns the realm for the principal owning the key.
72 * (Not a copy of it, so don't modify or deallocate it.)
73 **/
74 const char *
75 shishi_key_realm (Shishi_key * key)
77 return key->realm;
80 /**
81 * shishi_key_realm_set:
82 * @key: structure that holds key information
83 * @realm: string with new realm name.
85 * Set the realm for the principal owning the key. The string is
86 * copied into the key, so you can dispose of the variable immediately
87 * after calling this function.
88 **/
89 void
90 shishi_key_realm_set (Shishi_key * key, const char *realm)
92 if (key->realm)
93 free (key->realm);
94 if (realm)
95 key->realm = strdup (realm);
96 else
97 key->realm = NULL;
101 * shishi_key_type:
102 * @key: structure that holds key information
104 * Return value: Returns the type of key as an integer as described in
105 * the standard.
108 shishi_key_type (Shishi_key * key)
110 return key->type;
114 * shishi_key_type_set:
115 * @key: structure that holds key information
117 * Set the type of key in key structure.
119 void
120 shishi_key_type_set (Shishi_key * key, int32_t type)
122 key->type = type;
126 * shishi_key_value:
127 * @key: structure that holds key information
129 * Return value: Returns the key value as a pointer which is valid
130 * throughout the lifetime of the key structure.
132 char *
133 shishi_key_value (Shishi_key * key)
135 return key->value;
139 * shishi_key_value_set:
140 * @key: structure that holds key information
141 * @value: input array with key data.
142 * @length: length of input array with key data.
144 * Set the key value and length in key structure.
146 void
147 shishi_key_value_set (Shishi_key * key, const char *value)
149 if (value &&
150 shishi_cipher_keylen (key->type) > 0 &&
151 shishi_cipher_keylen (key->type) <= MAX_KEY_LEN)
152 memcpy (key->value, value, shishi_cipher_keylen (key->type));
156 * shishi_key_version:
157 * @key: structure that holds key information
159 * Return value: Returns the version of key ("kvno").
162 shishi_key_version (Shishi_key * key)
164 return key->version;
168 * shishi_key_version_set:
169 * @key: structure that holds key information
170 * @version: new version integer.
172 * Set the version of key ("kvno") in key structure.
174 void
175 shishi_key_version_set (Shishi_key * key, int version)
177 key->version = version;
181 * shishi_key_name:
182 * @key: structure that holds key information
184 * Calls shishi_cipher_name for key type.
186 * Return value: Return name of key.
188 const char *
189 shishi_key_name (Shishi_key * key)
191 return shishi_cipher_name (key->type);
195 * shishi_key_length:
196 * @key: structure that holds key information
198 * Calls shishi_cipher_keylen for key type.
200 * Return value: Returns the length of the key value.
202 size_t
203 shishi_key_length (Shishi_key * key)
205 return shishi_cipher_keylen (key->type);
209 * shishi_key:
210 * @handle: Shishi library handle create by shishi_init().
211 * @key: pointer to structure that will hold newly created key information
213 * Create a new Key information structure.
215 * Return value: Returns SHISHI_MALLOC_ERROR on memory allocation
216 * errors, and SHISHI_OK on success.
219 shishi_key (Shishi * handle, Shishi_key ** key)
221 *key = malloc (sizeof (**key));
222 if (!*key)
223 return SHISHI_MALLOC_ERROR;
224 memset (*key, 0, sizeof (**key));
226 (*key)->handle = handle;
228 return SHISHI_OK;
232 * shishi_key_done:
233 * @key: pointer to structure that holds key information.
235 * Deallocates key information structure and set key handle to NULL.
237 void
238 shishi_key_done (Shishi_key ** key)
240 free (*key);
241 *key = NULL;
245 * shishi_key_copy:
246 * @dstkey: structure that holds destination key information
247 * @srckey: structure that holds source key information
249 * Copies source key into existing allocated destination key.
251 void
252 shishi_key_copy (Shishi_key * dstkey, Shishi_key * srckey)
254 shishi_key_principal_set (dstkey, shishi_key_principal (srckey));
255 shishi_key_realm_set (dstkey, shishi_key_realm (srckey));
256 shishi_key_type_set (dstkey, shishi_key_type (srckey));
257 shishi_key_value_set (dstkey, shishi_key_value (srckey));
258 shishi_key_version_set (dstkey, shishi_key_version (srckey));
262 * shishi_key_from_value:
263 * @handle: Shishi library handle create by shishi_init().
264 * @type: type of key.
265 * @value: input array with key value, or NULL.
266 * @key: pointer to structure that will hold newly created key information
268 * Create a new Key information structure, and set the key type and
269 * key value. KEY contains a newly allocated structure only if this
270 * function is successful.
272 * Return value: Returns SHISHI_MALLOC_ERROR on memory allocation
273 * errors, and SHISHI_OK on success.
276 shishi_key_from_value (Shishi * handle,
277 int32_t type, char *value, Shishi_key ** key)
279 int rc;
281 rc = shishi_key (handle, key);
282 if (rc != SHISHI_OK)
283 return rc;
285 shishi_key_type_set (*key, type);
286 if (value)
287 shishi_key_value_set (*key, value);
289 return SHISHI_OK;
293 * shishi_key_from_base64:
294 * @handle: Shishi library handle create by shishi_init().
295 * @type: type of key.
296 * @value: input string with base64 encoded key value, or NULL.
297 * @key: pointer to structure that will hold newly created key information
299 * Create a new Key information structure, and set the key type and
300 * key value. KEY contains a newly allocated structure only if this
301 * function is successful.
303 * Return value: Returns SHISHI_MALLOC_ERROR on memory allocation
304 * errors, SHISHI_INVALID_KEY if the base64 encoded key
305 * length doesn't match the key type, and SHISHI_OK on
306 * success.
309 shishi_key_from_base64 (Shishi * handle,
310 int32_t type, char *value, Shishi_key ** key)
312 int rc;
314 rc = shishi_key (handle, key);
315 if (rc != SHISHI_OK)
316 return rc;
318 shishi_key_type_set (*key, type);
320 if (value)
322 size_t len;
323 char *buf;
325 buf = malloc (strlen (value) + 1);
326 if (!buf)
327 return SHISHI_MALLOC_ERROR;
329 len = shishi_from_base64 (buf, value);
331 if (len != shishi_key_length (*key))
333 free (buf);
334 return SHISHI_INVALID_KEY;
337 shishi_key_value_set (*key, buf);
339 free (buf);
342 return SHISHI_OK;
346 * shishi_key_random
347 * @handle: Shishi library handle create by shishi_init().
348 * @type: type of key.
350 * Create a new Key information structure for the key type and some
351 * random data. KEY contains a newly allocated structure only if this
352 * function is successful.
354 * Return value: Returns SHISHI_OK iff successful.
357 shishi_key_random (Shishi * handle, int32_t type, Shishi_key ** key)
359 char buf[MAX_RANDOM_LEN];
360 int len = shishi_cipher_randomlen (type);
361 int rc;
363 rc = shishi_key (handle, key);
364 if (rc != SHISHI_OK)
365 return rc;
367 rc = shishi_randomize (handle, buf, len);
368 if (rc != SHISHI_OK)
369 return rc;
371 rc = shishi_random_to_key (handle, type, buf, len, *key);
372 if (rc != SHISHI_OK)
373 return rc;
375 return SHISHI_OK;
379 * shishi_key_from_random
380 * @handle: Shishi library handle create by shishi_init().
381 * @type: type of key.
382 * @random: random data.
383 * @randomlen: length of random data.
385 * Create a new Key information structure, and set the key type and
386 * key value using shishi_random_to_key(). KEY contains a newly
387 * allocated structure only if this function is successful.
389 * Return value: Returns SHISHI_MALLOC_ERROR on memory allocation
390 * errors, and SHISHI_OK on success.
393 shishi_key_from_random (Shishi * handle,
394 int32_t type,
395 char *random, size_t randomlen, Shishi_key ** key)
397 int rc;
399 rc = shishi_key (handle, key);
400 if (rc != SHISHI_OK)
401 return rc;
403 rc = shishi_random_to_key (handle, type, random, randomlen, *key);
405 return rc;
409 * shishi_key_from_string
410 * @handle: Shishi library handle create by shishi_init().
411 * @type: type of key.
412 * @password: input array containing password.
413 * @passwordlen: length of input array containing password.
414 * @salt: input array containing salt.
415 * @saltlen: length of input array containing salt.
416 * @parameter: input array with opaque encryption type specific information.
418 * Create a new Key information structure, and set the key type and
419 * key value using shishi_string_to_key(). KEY contains a newly
420 * allocated structure only if this function is successful.
422 * Return value: Returns SHISHI_MALLOC_ERROR on memory allocation
423 * errors, and SHISHI_OK on success.
426 shishi_key_from_string (Shishi * handle,
427 int32_t type,
428 const char *password, size_t passwordlen,
429 const char *salt, size_t saltlen,
430 const char *parameter, Shishi_key ** key)
432 int rc;
434 rc = shishi_key (handle, key);
435 if (rc != SHISHI_OK)
436 return rc;
438 rc = shishi_string_to_key (handle, type, password, passwordlen,
439 salt, saltlen, parameter, *key);
440 if (rc != SHISHI_OK)
441 return rc;
443 return SHISHI_OK;