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. */
34 _shishi_crypto_init (void)
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
48 * Return value: Returns %SHISHI_OK iff successful.
51 shishi_randomize (Shishi
* handle
, char *data
, size_t datalen
)
58 device
= "/dev/random";
60 fd
= open (device
, O_RDONLY
);
63 shishi_error_printf (handle
, "Could not open random device: %s",
65 return SHISHI_FILE_ERROR
;
72 tmp
= read (fd
, data
, datalen
);
76 shishi_error_printf (handle
, "Could not read from random device: %s",
78 return SHISHI_FILE_ERROR
;
84 shishi_error_printf (handle
, "Short read from random device: %d < %d",
87 while (len
< datalen
);
91 shishi_warn (handle
, "Could not close random device: %s",
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
])
116 md4_update (&md4
, inlen
, in
);
117 *out
= xmalloc (MD4_DIGEST_SIZE
);
118 md4_digest (&md4
, MD4_DIGEST_SIZE
, *out
);
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
])
142 md5_update (&md5
, inlen
, in
);
143 *out
= xmalloc (MD5_DIGEST_SIZE
);
144 md5_digest (&md5
, MD5_DIGEST_SIZE
, *out
);
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
);
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
;
201 rc
= des_set_key (&des
.ctx
, key
);
204 shishi_error_printf (handle
, "Nettle des_set_key() failed (%d)", rc
);
205 return SHISHI_CRYPTO_INTERNAL_ERROR
;
209 CBC_SET_IV (&des
, iv
);
211 memset (des
.iv
, 0, DES_BLOCK_SIZE
);
213 *out
= xmalloc (DES_BLOCK_SIZE
);
215 CBC_MAC (&des
, des_encrypt
, inlen
, *out
, in
);
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
,
243 struct CBC_CTX (struct des_ctx
, DES_BLOCK_SIZE
) des
;
246 *out
= xmalloc (inlen
);
248 rc
= des_set_key (&des
.ctx
, key
);
251 shishi_error_printf (handle
, "Nettle setkey failed");
252 return SHISHI_CRYPTO_INTERNAL_ERROR
;
256 CBC_SET_IV (&des
, iv
);
258 memset (des
.iv
, 0, sizeof (des
.iv
));
261 CBC_DECRYPT (&des
, des_decrypt
, inlen
, *out
, in
);
263 CBC_ENCRYPT (&des
, des_encrypt
, inlen
, *out
, in
);
266 *ivout
= xmemdup (des
.iv
, DES_BLOCK_SIZE
);
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
,
294 struct CBC_CTX (struct des3_ctx
, DES3_BLOCK_SIZE
) des3
;
297 *out
= xmalloc (inlen
);
299 rc
= des3_set_key (&des3
.ctx
, key
);
302 shishi_error_printf (handle
, "Nettle setkey failed");
303 return SHISHI_CRYPTO_INTERNAL_ERROR
;
307 CBC_SET_IV (&des3
, iv
);
309 memset (des3
.iv
, 0, sizeof (des3
.iv
));
312 CBC_DECRYPT (&des3
, des3_decrypt
, inlen
, *out
, in
);
314 CBC_ENCRYPT (&des3
, des3_encrypt
, inlen
, *out
, in
);
317 *ivout
= xmemdup (des3
.iv
, DES3_BLOCK_SIZE
);
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
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
,
348 struct CBC_CTS_CTX (struct aes_ctx
, AES_BLOCK_SIZE
) aes
;
350 *out
= xmalloc (inlen
);
354 CBC_SET_IV (&aes
, iv
);
356 memset (aes
.iv
, 0, sizeof (aes
.iv
));
360 aes_set_decrypt_key (&aes
.ctx
, keylen
, key
);
361 CBC_CTS_DECRYPT (&aes
, aes_decrypt
, inlen
, *out
, in
);
365 aes_set_encrypt_key (&aes
.ctx
, keylen
, key
);
366 CBC_CTS_ENCRYPT (&aes
, aes_encrypt
, inlen
, *out
, in
);
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
);