Improve man pages, unfortunately somewhat Shishi specific for the moment.
[shishi.git] / lib / nettle.c
blob398cf88017f88455d8815ab07f797fd723de3680
1 /* nettle.c shishi crypto wrappers around nettle.
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 /* Note: This file is only built if Shishi uses Nettle. */
24 #include "internal.h"
26 #include "hmac.h"
27 #include "des.h"
28 #include "aes.h"
29 #include "cbc.h"
30 #include "cbc-cts.h"
31 #include "cbc-mac.h"
33 int
34 _shishi_crypto_init (void)
36 return SHISHI_OK;
39 /**
40 * shishi_randomize:
41 * @handle: shishi handle as allocated by shishi_init().
42 * @data: output array to be filled with random data.
43 * @datalen: size of output array.
45 * Store cryptographically strong random data of given size in the
46 * provided buffer.
48 * Return value: Returns %SHISHI_OK iff successful.
49 **/
50 int
51 shishi_randomize (Shishi * handle, char *data, size_t datalen)
53 int fd;
54 char *device;
55 size_t len = 0;
56 int rc;
58 device = "/dev/random";
60 fd = open (device, O_RDONLY);
61 if (fd < 0)
63 shishi_error_printf (handle, "Could not open random device: %s",
64 strerror (errno));
65 return SHISHI_FILE_ERROR;
70 ssize_t tmp;
72 tmp = read (fd, data, datalen);
74 if (tmp < 0)
76 shishi_error_printf (handle, "Could not read from random device: %s",
77 strerror (errno));
78 return SHISHI_FILE_ERROR;
81 len += tmp;
83 if (len < datalen)
84 shishi_error_printf (handle, "Short read from random device: %d < %d",
85 len, datalen);
87 while (len < datalen);
89 rc = close (fd);
90 if (rc < 0)
91 shishi_warn (handle, "Could not close random device: %s",
92 strerror (errno));
94 return SHISHI_OK;
97 /**
98 * shishi_md4:
99 * @handle: shishi handle as allocated by shishi_init().
100 * @in: input character array of data to hash.
101 * @inlen: length of input character array of data to hash.
102 * @out: newly allocated character array with hash of data.
104 * Compute hash of data using MD4.
106 * Return value: Returns SHISHI_OK iff successful.
109 shishi_md4 (Shishi * handle,
110 const char *in, size_t inlen,
111 char *out[MD4_DIGEST_SIZE])
113 struct md4_ctx md4;
115 md4_init (&md4);
116 md4_update (&md4, inlen, in);
117 *out = xmalloc (MD4_DIGEST_SIZE);
118 md4_digest (&md4, MD4_DIGEST_SIZE, *out);
120 return SHISHI_OK;
124 * shishi_md5:
125 * @handle: shishi handle as allocated by shishi_init().
126 * @in: input character array of data to hash.
127 * @inlen: length of input character array of data to hash.
128 * @out: newly allocated character array with hash of data.
130 * Compute hash of data using MD5.
132 * Return value: Returns SHISHI_OK iff successful.
135 shishi_md5 (Shishi * handle,
136 const char *in, size_t inlen,
137 char *out[MD5_DIGEST_SIZE])
139 struct md5_ctx md5;
141 md5_init (&md5);
142 md5_update (&md5, inlen, in);
143 *out = xmalloc (MD5_DIGEST_SIZE);
144 md5_digest (&md5, MD5_DIGEST_SIZE, *out);
146 return SHISHI_OK;
150 * shishi_hmac_sha1:
151 * @handle: shishi handle as allocated by shishi_init().
152 * @key: input character array with key to use.
153 * @keylen: length of input character array with key to use.
154 * @in: input character array of data to hash.
155 * @inlen: length of input character array of data to hash.
156 * @outhash: newly allocated character array with keyed hash of data.
158 * Compute keyed checksum of data using HMAC-SHA1
160 * Return value: Returns SHISHI_OK iff successful.
163 shishi_hmac_sha1 (Shishi * handle,
164 const char *key, size_t keylen,
165 const char *in, size_t inlen,
166 char *outhash[SHA1_DIGEST_SIZE])
168 struct hmac_sha1_ctx ctx;
170 hmac_sha1_set_key (&ctx, keylen, key);
171 hmac_sha1_update (&ctx, inlen, in);
172 *outhash = xmalloc (SHA1_DIGEST_SIZE);
173 hmac_sha1_digest (&ctx, SHA1_DIGEST_SIZE, *outhash);
175 return SHISHI_OK;
179 * shishi_des_cbc_mac:
180 * @handle: shishi handle as allocated by shishi_init().
181 * @key: input character array with key to use.
182 * @iv: input character array with initialization vector to use, can be NULL.
183 * @in: input character array of data to hash.
184 * @inlen: length of input character array of data to hash.
185 * @out: newly allocated character array with keyed hash of data.
187 * Computed keyed checksum of data using DES-CBC-MAC.
189 * Return value: Returns SHISHI_OK iff successful.
192 shishi_des_cbc_mac (Shishi * handle,
193 const char key[DES_KEY_SIZE],
194 const char iv[DES_BLOCK_SIZE],
195 const char *in, size_t inlen,
196 char *out[DES_BLOCK_SIZE])
198 struct CBC_MAC_CTX (struct des_ctx, DES_BLOCK_SIZE) des;
199 int rc;
201 rc = des_set_key (&des.ctx, key);
202 if (!rc)
204 shishi_error_printf (handle, "Nettle des_set_key() failed (%d)", rc);
205 return SHISHI_CRYPTO_INTERNAL_ERROR;
208 if (iv)
209 CBC_SET_IV (&des, iv);
210 else
211 memset (des.iv, 0, DES_BLOCK_SIZE);
213 *out = xmalloc (DES_BLOCK_SIZE);
215 CBC_MAC (&des, des_encrypt, inlen, *out, in);
217 return SHISHI_OK;
221 * shishi_des:
222 * @handle: shishi handle as allocated by shishi_init().
223 * @decryptp: 0 to indicate encryption, non-0 to indicate decryption.
224 * @key: input character array with key to use.
225 * @iv: input character array with initialization vector to use, or NULL.
226 * @ivout: output character array with updated initialization vector, or NULL.
227 * @in: input character array of data to encrypt/decrypt.
228 * @inlen: length of input character array of data to encrypt/decrypt.
229 * @out: newly allocated character array with encrypted/decrypted data.
231 * Encrypt or decrypt data (depending on DECRYPTP) using DES in CBC mode.
233 * Return value: Returns SHISHI_OK iff successful.
236 shishi_des (Shishi * handle, int decryptp,
237 const char key[DES_KEY_SIZE],
238 const char iv[DES_BLOCK_SIZE],
239 char *ivout[DES_BLOCK_SIZE],
240 const char *in, size_t inlen,
241 char **out)
243 struct CBC_CTX (struct des_ctx, DES_BLOCK_SIZE) des;
244 int rc;
246 *out = xmalloc (inlen);
248 rc = des_set_key (&des.ctx, key);
249 if (!rc)
251 shishi_error_printf (handle, "Nettle setkey failed");
252 return SHISHI_CRYPTO_INTERNAL_ERROR;
255 if (iv)
256 CBC_SET_IV (&des, iv);
257 else
258 memset (des.iv, 0, sizeof (des.iv));
260 if (decryptp)
261 CBC_DECRYPT (&des, des_decrypt, inlen, *out, in);
262 else
263 CBC_ENCRYPT (&des, des_encrypt, inlen, *out, in);
265 if (ivout)
266 *ivout = xmemdup (des.iv, DES_BLOCK_SIZE);
268 return SHISHI_OK;
272 * shishi_3des:
273 * @handle: shishi handle as allocated by shishi_init().
274 * @decryptp: 0 to indicate encryption, non-0 to indicate decryption.
275 * @key: input character array with key to use.
276 * @iv: input character array with initialization vector to use, or NULL.
277 * @ivout: output character array with updated initialization vector, or NULL.
278 * @in: input character array of data to encrypt/decrypt.
279 * @inlen: length of input character array of data to encrypt/decrypt.
280 * @out: newly allocated character array with encrypted/decrypted data.
282 * Encrypt or decrypt data (depending on DECRYPTP) using 3DES in CBC mode.
284 * Return value: Returns SHISHI_OK iff successful.
287 shishi_3des (Shishi * handle, int decryptp,
288 const char key[DES3_KEY_SIZE],
289 const char iv[DES3_BLOCK_SIZE],
290 char *ivout[DES3_BLOCK_SIZE],
291 const char *in, size_t inlen,
292 char **out)
294 struct CBC_CTX (struct des3_ctx, DES3_BLOCK_SIZE) des3;
295 int rc;
297 *out = xmalloc (inlen);
299 rc = des3_set_key (&des3.ctx, key);
300 if (!rc)
302 shishi_error_printf (handle, "Nettle setkey failed");
303 return SHISHI_CRYPTO_INTERNAL_ERROR;
306 if (iv)
307 CBC_SET_IV (&des3, iv);
308 else
309 memset (des3.iv, 0, sizeof (des3.iv));
311 if (decryptp)
312 CBC_DECRYPT (&des3, des3_decrypt, inlen, *out, in);
313 else
314 CBC_ENCRYPT (&des3, des3_encrypt, inlen, *out, in);
316 if (ivout)
317 *ivout = xmemdup (des3.iv, DES3_BLOCK_SIZE);
319 return SHISHI_OK;
323 * shishi_aes_cts:
324 * @handle: shishi handle as allocated by shishi_init().
325 * @decryptp: 0 to indicate encryption, non-0 to indicate decryption.
326 * @key: input character array with key to use.
327 * @keylen: length of input character array with key to use.
328 * @iv: input character array with initialization vector to use, or NULL.
329 * @ivout: output character array with updated initialization vector, or NULL.
330 * @in: input character array of data to encrypt/decrypt.
331 * @inlen: length of input character array of data to encrypt/decrypt.
332 * @out: newly allocated character array with encrypted/decrypted data.
334 * Encrypt or decrypt data (depending on DECRYPTP) using AES in
335 * CBC-CTS mode. The length of the key decide if AES 128 or AES 256
336 * should be used.
338 * Return value: Returns SHISHI_OK iff successful.
341 shishi_aes_cts (Shishi * handle, int decryptp,
342 const char *key, size_t keylen,
343 const char iv[AES_BLOCK_SIZE],
344 char *ivout[AES_BLOCK_SIZE],
345 const char *in, size_t inlen,
346 char **out)
348 struct CBC_CTS_CTX (struct aes_ctx, AES_BLOCK_SIZE) aes;
350 *out = xmalloc (inlen);
353 if (iv)
354 CBC_SET_IV (&aes, iv);
355 else
356 memset (aes.iv, 0, sizeof (aes.iv));
358 if (decryptp)
360 aes_set_decrypt_key (&aes.ctx, keylen, key);
361 CBC_CTS_DECRYPT (&aes, aes_decrypt, inlen, *out, in);
363 else
365 aes_set_encrypt_key (&aes.ctx, keylen, key);
366 CBC_CTS_ENCRYPT (&aes, aes_encrypt, inlen, *out, in);
369 if (ivout)
370 /* XXX what is the output iv for CBC-CTS mode?
371 but is this value useful at all for that mode anyway?
372 Mostly it is DES apps that want the updated iv, so this is ok. */
373 *ivout = xmemdup (aes.iv, AES_BLOCK_SIZE);
375 return SHISHI_OK;