3 * Wrapper for builtin functions
5 * Copyright (c) 2001 Marko Kreen
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * System reseeds should be separated at least this much.
47 #define SYSTEM_RESEED_MIN (20*60) /* 20 min */
49 * How often to roll dice.
51 #define SYSTEM_RESEED_CHECK_TIME (10*60) /* 10 min */
53 * The chance is x/256 that the reseed happens.
55 #define SYSTEM_RESEED_CHANCE (4) /* 256/4 * 10min ~ 10h */
58 * If this much time has passed, force reseed.
60 #define SYSTEM_RESEED_MAX (12*60*60) /* 12h */
63 #ifndef MD5_DIGEST_LENGTH
64 #define MD5_DIGEST_LENGTH 16
67 #ifndef SHA1_DIGEST_LENGTH
69 #define SHA1_DIGEST_LENGTH SHA1_RESULTLEN
71 #define SHA1_DIGEST_LENGTH 20
75 #define SHA1_BLOCK_SIZE 64
76 #define MD5_BLOCK_SIZE 64
78 static void init_md5(PX_MD
* h
);
79 static void init_sha1(PX_MD
* h
);
81 void init_sha224(PX_MD
* h
);
82 void init_sha256(PX_MD
* h
);
83 void init_sha384(PX_MD
* h
);
84 void init_sha512(PX_MD
* h
);
89 void (*init
) (PX_MD
* h
);
92 static const struct int_digest
96 {"sha224", init_sha224
},
97 {"sha256", init_sha256
},
98 {"sha384", init_sha384
},
99 {"sha512", init_sha512
},
106 int_md5_len(PX_MD
* h
)
108 return MD5_DIGEST_LENGTH
;
112 int_md5_block_len(PX_MD
* h
)
114 return MD5_BLOCK_SIZE
;
118 int_md5_update(PX_MD
* h
, const uint8
*data
, unsigned dlen
)
120 MD5_CTX
*ctx
= (MD5_CTX
*) h
->p
.ptr
;
122 MD5Update(ctx
, data
, dlen
);
126 int_md5_reset(PX_MD
* h
)
128 MD5_CTX
*ctx
= (MD5_CTX
*) h
->p
.ptr
;
134 int_md5_finish(PX_MD
* h
, uint8
*dst
)
136 MD5_CTX
*ctx
= (MD5_CTX
*) h
->p
.ptr
;
142 int_md5_free(PX_MD
* h
)
144 MD5_CTX
*ctx
= (MD5_CTX
*) h
->p
.ptr
;
146 memset(ctx
, 0, sizeof(*ctx
));
154 int_sha1_len(PX_MD
* h
)
156 return SHA1_DIGEST_LENGTH
;
160 int_sha1_block_len(PX_MD
* h
)
162 return SHA1_BLOCK_SIZE
;
166 int_sha1_update(PX_MD
* h
, const uint8
*data
, unsigned dlen
)
168 SHA1_CTX
*ctx
= (SHA1_CTX
*) h
->p
.ptr
;
170 SHA1Update(ctx
, data
, dlen
);
174 int_sha1_reset(PX_MD
* h
)
176 SHA1_CTX
*ctx
= (SHA1_CTX
*) h
->p
.ptr
;
182 int_sha1_finish(PX_MD
* h
, uint8
*dst
)
184 SHA1_CTX
*ctx
= (SHA1_CTX
*) h
->p
.ptr
;
190 int_sha1_free(PX_MD
* h
)
192 SHA1_CTX
*ctx
= (SHA1_CTX
*) h
->p
.ptr
;
194 memset(ctx
, 0, sizeof(*ctx
));
206 ctx
= px_alloc(sizeof(*ctx
));
207 memset(ctx
, 0, sizeof(*ctx
));
211 md
->result_size
= int_md5_len
;
212 md
->block_size
= int_md5_block_len
;
213 md
->reset
= int_md5_reset
;
214 md
->update
= int_md5_update
;
215 md
->finish
= int_md5_finish
;
216 md
->free
= int_md5_free
;
222 init_sha1(PX_MD
* md
)
226 ctx
= px_alloc(sizeof(*ctx
));
227 memset(ctx
, 0, sizeof(*ctx
));
231 md
->result_size
= int_sha1_len
;
232 md
->block_size
= int_sha1_block_len
;
233 md
->reset
= int_sha1_reset
;
234 md
->update
= int_sha1_update
;
235 md
->finish
= int_sha1_finish
;
236 md
->free
= int_sha1_free
;
245 #define INT_MAX_KEY (512/8)
246 #define INT_MAX_IV (128/8)
250 uint8 keybuf
[INT_MAX_KEY
];
251 uint8 iv
[INT_MAX_IV
];
263 intctx_free(PX_Cipher
* c
)
265 struct int_ctx
*cx
= (struct int_ctx
*) c
->ptr
;
269 memset(cx
, 0, sizeof *cx
);
283 rj_block_size(PX_Cipher
* c
)
289 rj_key_size(PX_Cipher
* c
)
295 rj_iv_size(PX_Cipher
* c
)
301 rj_init(PX_Cipher
* c
, const uint8
*key
, unsigned klen
, const uint8
*iv
)
303 struct int_ctx
*cx
= (struct int_ctx
*) c
->ptr
;
306 cx
->keylen
= 128 / 8;
307 else if (klen
<= 192 / 8)
308 cx
->keylen
= 192 / 8;
309 else if (klen
<= 256 / 8)
310 cx
->keylen
= 256 / 8;
312 return PXE_KEY_TOO_BIG
;
314 memcpy(&cx
->keybuf
, key
, klen
);
317 memcpy(cx
->iv
, iv
, 128 / 8);
323 rj_real_init(struct int_ctx
* cx
, int dir
)
325 aes_set_key(&cx
->ctx
.rj
, cx
->keybuf
, cx
->keylen
* 8, dir
);
330 rj_encrypt(PX_Cipher
* c
, const uint8
*data
, unsigned dlen
, uint8
*res
)
332 struct int_ctx
*cx
= (struct int_ctx
*) c
->ptr
;
336 if (rj_real_init(cx
, 1))
337 return PXE_CIPHER_INIT
;
344 return PXE_NOTBLOCKSIZE
;
346 memcpy(res
, data
, dlen
);
348 if (cx
->mode
== MODE_CBC
)
350 aes_cbc_encrypt(&cx
->ctx
.rj
, cx
->iv
, res
, dlen
);
351 memcpy(cx
->iv
, res
+ dlen
- 16, 16);
354 aes_ecb_encrypt(&cx
->ctx
.rj
, res
, dlen
);
360 rj_decrypt(PX_Cipher
* c
, const uint8
*data
, unsigned dlen
, uint8
*res
)
362 struct int_ctx
*cx
= (struct int_ctx
*) c
->ptr
;
365 if (rj_real_init(cx
, 0))
366 return PXE_CIPHER_INIT
;
372 return PXE_NOTBLOCKSIZE
;
374 memcpy(res
, data
, dlen
);
376 if (cx
->mode
== MODE_CBC
)
378 aes_cbc_decrypt(&cx
->ctx
.rj
, cx
->iv
, res
, dlen
);
379 memcpy(cx
->iv
, data
+ dlen
- 16, 16);
382 aes_ecb_decrypt(&cx
->ctx
.rj
, res
, dlen
);
397 c
= px_alloc(sizeof *c
);
398 memset(c
, 0, sizeof *c
);
400 c
->block_size
= rj_block_size
;
401 c
->key_size
= rj_key_size
;
402 c
->iv_size
= rj_iv_size
;
404 c
->encrypt
= rj_encrypt
;
405 c
->decrypt
= rj_decrypt
;
406 c
->free
= intctx_free
;
408 cx
= px_alloc(sizeof *cx
);
409 memset(cx
, 0, sizeof *cx
);
421 bf_block_size(PX_Cipher
* c
)
427 bf_key_size(PX_Cipher
* c
)
433 bf_iv_size(PX_Cipher
* c
)
439 bf_init(PX_Cipher
* c
, const uint8
*key
, unsigned klen
, const uint8
*iv
)
441 struct int_ctx
*cx
= (struct int_ctx
*) c
->ptr
;
443 blowfish_setkey(&cx
->ctx
.bf
, key
, klen
);
445 blowfish_setiv(&cx
->ctx
.bf
, iv
);
451 bf_encrypt(PX_Cipher
* c
, const uint8
*data
, unsigned dlen
, uint8
*res
)
453 struct int_ctx
*cx
= (struct int_ctx
*) c
->ptr
;
454 BlowfishContext
*bfctx
= &cx
->ctx
.bf
;
460 return PXE_NOTBLOCKSIZE
;
462 memcpy(res
, data
, dlen
);
466 blowfish_encrypt_ecb(res
, dlen
, bfctx
);
469 blowfish_encrypt_cbc(res
, dlen
, bfctx
);
476 bf_decrypt(PX_Cipher
* c
, const uint8
*data
, unsigned dlen
, uint8
*res
)
478 struct int_ctx
*cx
= (struct int_ctx
*) c
->ptr
;
479 BlowfishContext
*bfctx
= &cx
->ctx
.bf
;
485 return PXE_NOTBLOCKSIZE
;
487 memcpy(res
, data
, dlen
);
491 blowfish_decrypt_ecb(res
, dlen
, bfctx
);
494 blowfish_decrypt_cbc(res
, dlen
, bfctx
);
506 c
= px_alloc(sizeof *c
);
507 memset(c
, 0, sizeof *c
);
509 c
->block_size
= bf_block_size
;
510 c
->key_size
= bf_key_size
;
511 c
->iv_size
= bf_iv_size
;
513 c
->encrypt
= bf_encrypt
;
514 c
->decrypt
= bf_decrypt
;
515 c
->free
= intctx_free
;
517 cx
= px_alloc(sizeof *cx
);
518 memset(cx
, 0, sizeof *cx
);
529 return rj_load(MODE_ECB
);
535 return rj_load(MODE_CBC
);
541 return bf_load(MODE_ECB
);
547 return bf_load(MODE_CBC
);
553 PX_Cipher
*(*load
) (void);
556 static const struct int_cipher
558 {"bf-cbc", bf_cbc_load
},
559 {"bf-ecb", bf_ecb_load
},
560 {"aes-128-cbc", rj_128_cbc
},
561 {"aes-128-ecb", rj_128_ecb
},
565 static const PX_Alias int_aliases
[] = {
567 {"blowfish", "bf-cbc"},
568 {"aes", "aes-128-cbc"},
569 {"aes-ecb", "aes-128-ecb"},
570 {"aes-cbc", "aes-128-cbc"},
571 {"aes-128", "aes-128-cbc"},
572 {"rijndael", "aes-128-cbc"},
573 {"rijndael-128", "aes-128-cbc"},
577 /* PUBLIC FUNCTIONS */
580 px_find_digest(const char *name
, PX_MD
** res
)
582 const struct int_digest
*p
;
585 for (p
= int_digest_list
; p
->name
; p
++)
586 if (pg_strcasecmp(p
->name
, name
) == 0)
588 h
= px_alloc(sizeof(*h
));
599 px_find_cipher(const char *name
, PX_Cipher
** res
)
604 name
= px_resolve_alias(int_aliases
, name
);
606 for (i
= 0; int_ciphers
[i
].name
; i
++)
607 if (!strcmp(int_ciphers
[i
].name
, name
))
609 c
= int_ciphers
[i
].load();
614 return PXE_NO_CIPHER
;
621 * Randomness provider
625 * Use always strong randomness.
628 px_get_pseudo_random_bytes(uint8
*dst
, unsigned count
)
630 return px_get_random_bytes(dst
, count
);
633 static time_t seed_time
= 0;
634 static time_t check_time
= 0;
648 else if ((t
- seed_time
) < SYSTEM_RESEED_MIN
)
650 else if ((t
- seed_time
) > SYSTEM_RESEED_MAX
)
652 else if (check_time
== 0 ||
653 (t
- check_time
) > SYSTEM_RESEED_CHECK_TIME
)
658 px_get_random_bytes(buf
, 1);
659 skip
= buf
[0] >= SYSTEM_RESEED_CHANCE
;
662 memset(buf
, 0, sizeof(buf
));
667 n
= px_acquire_system_randomness(buf
);
669 fortuna_add_entropy(buf
, n
);
672 memset(buf
, 0, sizeof(buf
));
676 px_get_random_bytes(uint8
*dst
, unsigned count
)
679 fortuna_get_bytes(count
, dst
);
684 px_add_entropy(const uint8
*data
, unsigned count
)
687 fortuna_add_entropy(data
, count
);