-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcipher.h
125 lines (93 loc) · 2.65 KB
/
cipher.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#ifndef __CIPHER_H__5071821__
#define __CIPHER_H__5071821__ 1
/*
* Helper functions to select AES-256-CTR or Chacha20 cipher
* for the random number generator.
*/
#include "aes.h"
#include "chacha_private.h"
#include "cryptorand.h"
static inline void
__chacha_key_setup(crypto_rand_state *st, uint8_t *key, uint8_t *iv)
{
chacha_keysetup(&st->chacha, key, ARC4R_KEYSZ * 8, 0);
chacha_ivsetup(&st->chacha, iv);
}
static void
__chacha_crypt_buf(crypto_rand_state *st)
{
chacha_encrypt_bytes(&st->chacha, st->buf, st->buf, sizeof st->buf);
}
static void
__chacha_rekey(crypto_rand_state *st)
{
uint8_t rnd[ARC4R_KEYSZ + ARC4R_IVSZ];
int r = (*st->entropy)(rnd, sizeof rnd);
assert(r == 0);
_rs_rekey(st, rnd, sizeof(rnd));
}
static void
__chacha_reinit(crypto_rand_state *st)
{
uint8_t *key = &st->buf[0];
uint8_t *iv = key + ARC4R_KEYSZ;
__chacha_key_setup(st, key, iv);
// erase the key & iv, reduce the amount of rand we have
memset(key, 0, ARC4R_KEYSZ + ARC4R_IVSZ);
st->ptr = st->buf + (ARC4R_KEYSZ + ARC4R_IVSZ);
}
// one time initialization function for chacha20 cipher based rand
static inline void
__chacha_init(crypto_rand_state *st)
{
uint8_t rnd[ARC4R_KEYSZ + ARC4R_IVSZ];
// We expect to see at least 64 bytes of entropy when the
// state is setup.
int r = (*st->entropy)(rnd, sizeof rnd);
assert(r == 0);
// setup the state
__chacha_key_setup(st, rnd, rnd+ARC4R_KEYSZ);
}
/*
* Helper functions for AES cipher
*/
static inline void
__aes_key_setup(crypto_rand_state *st, uint8_t *key, uint8_t *iv)
{
AES_init_ctx_iv(&st->aes, key, iv);
}
static void
__aes_crypt_buf(crypto_rand_state *st)
{
AES_CTR_xcrypt_buffer(&st->aes, st->buf, sizeof st->buf);
}
static void
__aes_rekey(crypto_rand_state *st)
{
uint8_t rnd[AESRAND_KEYSZ + AESRAND_IVSZ];
int r = (*st->entropy)(rnd, sizeof rnd);
assert(r == 0);
_rs_rekey(st, rnd, sizeof(rnd));
}
static void
__aes_reinit(crypto_rand_state *st)
{
uint8_t *key = &st->buf[0];
uint8_t *iv = key + AESRAND_KEYSZ;
__aes_key_setup(st, key, iv);
// erase the key & iv, reduce the amount of rand we have
memset(key, 0, AESRAND_KEYSZ + AESRAND_IVSZ);
st->ptr = st->buf + (AESRAND_KEYSZ + AESRAND_IVSZ);
}
// one time initialization function for aes-ctr cipher based rand
static inline void
__aes_init(crypto_rand_state *st)
{
uint8_t rnd[AESRAND_KEYSZ + AESRAND_IVSZ];
// We expect to see at least 64 bytes of entropy when the
// state is setup.
int r = (*st->entropy)(rnd, sizeof rnd);
assert(r == 0);
__aes_key_setup(st, rnd, rnd + AESRAND_KEYSZ);
}
#endif // __CIPHER_H__5071821__