Use GPL instead of LGPL.
[shishi.git] / lib / key.c
blob0d29c22caec2c20ca27b78d428485e384c45a34e
1 /* key.c Key related functions.
2 * Copyright (C) 2002 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 int type;
27 char value[MAX_KEY_LEN];
28 int version;
31 /**
32 * shishi_key_type:
33 * @key: structure that holds key information
35 * Return value: Returns the type of key as an integer as described in
36 * the standard.
37 **/
38 int
39 shishi_key_type (Shishi_key * key)
41 return key->type;
44 /**
45 * shishi_key_type_set:
46 * @key: structure that holds key information
48 * Set the type of key in key structure.
49 **/
50 void
51 shishi_key_type_set (Shishi_key * key, int type)
53 key->type = type;
56 /**
57 * shishi_key_value:
58 * @key: structure that holds key information
60 * Return value: Returns the key value as a pointer which is valid
61 * throughout the lifetime of the key structure.
62 **/
63 char *
64 shishi_key_value (Shishi_key * key)
66 return key->value;
69 /**
70 * shishi_key_value_set:
71 * @key: structure that holds key information
72 * @value: input array with key data.
73 * @length: length of input array with key data.
75 * Set the key value and length in key structure.
76 **/
77 void
78 shishi_key_value_set (Shishi_key * key, char *value)
80 if (value &&
81 shishi_cipher_keylen (key->type) > 0 &&
82 shishi_cipher_keylen (key->type) <= MAX_KEY_LEN)
83 memcpy(key->value, value, shishi_cipher_keylen (key->type));
86 /**
87 * shishi_key_version:
88 * @key: structure that holds key information
90 * Return value: Returns the version of key ("kvno").
91 **/
92 int
93 shishi_key_version (Shishi_key * key)
95 return key->version;
98 /**
99 * shishi_key_version_set:
100 * @key: structure that holds key information
101 * @version: new version integer.
103 * Set the version of key ("kvno") in key structure.
105 void
106 shishi_key_version_set (Shishi_key * key, int version)
108 key->version = version;
112 * shishi_key_name:
113 * @key: structure that holds key information
115 * Calls shishi_cipher_name for key type.
117 * Return value: Return name of key.
119 const char *
120 shishi_key_name (Shishi_key * key)
122 return shishi_cipher_name(key->type);
126 * shishi_key_length:
127 * @key: structure that holds key information
129 * Calls shishi_cipher_keylen for key type.
131 * Return value: Returns the length of the key value.
133 size_t
134 shishi_key_length (Shishi_key * key)
136 return shishi_cipher_keylen(key->type);
140 * shishi_key:
141 * @type: type of key.
142 * @value: key value.
144 * Allocates a new key information structure and copies the supplied
145 * data into it.
147 * Return value: Returns newly allocated key structure, or NULL on failure.
149 Shishi_key *
150 shishi_key (int type, char *value)
152 Shishi_key *key;
154 key = malloc (sizeof (*key));
155 if (!key)
156 return NULL;
158 shishi_key_type_set (key, type);
159 shishi_key_version_set (key, 0);
160 if (value)
161 shishi_key_value_set (key, value);
163 return key;
167 * shishi_key_done:
168 * @key: structure that holds key information
170 * Deallocates key information structure.
172 void
173 shishi_key_done (Shishi_key *key)
175 free(key);
179 * shishi_key_copy:
180 * @dstkey: structure that holds destination key information
181 * @srckey: structure that holds source key information
183 * Copies source key into existing allocated destination key.
185 void
186 shishi_key_copy (Shishi_key * dstkey, Shishi_key *srckey)
188 shishi_key_type_set(dstkey, shishi_key_type(srckey));
189 shishi_key_value_set(dstkey, shishi_key_value(srckey));
193 * shishi_key_from_random
194 * @type: type of key.
195 * @random: random data.
196 * @randomlen: length of random data.
198 * Allocates a new key information structure and creates key based on
199 * random data supplied. KEY contains a newly allocated structure if
200 * succesful. See also shishi_random_to_key().
202 * Return value: Returns SHISHI_OK iff succesful.
205 shishi_key_from_random (Shishi *handle,
206 int type,
207 char *random,
208 int randomlen,
209 Shishi_key **outkey)
211 int res;
213 *outkey = shishi_key(type, NULL);
215 res = shishi_random_to_key (handle, type, random, randomlen, *outkey);
217 return res;
221 * shishi_key_from_string
222 * @type: type of key.
223 * @password: password.
225 * Allocates a new key information structure and copies the supplied
226 * data into it. See also shishi_string_to_key().
228 * Return value: Returns newly allocated key structure, or NULL on failure.
231 shishi_key_from_string (Shishi *handle,
232 int type,
233 char *password,
234 int passwordlen,
235 char *salt,
236 int saltlen,
237 char *parameter,
238 Shishi_key **outkey)
240 int res;
242 *outkey = shishi_key(type, NULL);
244 res = shishi_string_to_key (handle, type, password, passwordlen,
245 salt, saltlen, parameter, *outkey);
247 return res;