-
Notifications
You must be signed in to change notification settings - Fork 2
/
freestyle.h
236 lines (196 loc) · 5.47 KB
/
freestyle.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* Copyright (c) 2019 P. Arun Babu and Jithin Jose Thomas
* arun DOT hbni AT gmail DOT com, jithinjosethomas AT gmail DOT com
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Some code is taken from D. J. Bernstein's
* chacha-merged.c version 20080118
* Public domain.
*/
#ifndef FREESTYLE_H
#define FREESTYLE_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#define MAX_HASH_VALUES (256)
#define MAX_INIT_HASHES (56)
#include <assert.h>
#ifdef __linux__
#include <bsd/stdlib.h>
#endif
/*--- Elements of the cipher state --- */
#define CONSTANT0 (0)
#define CONSTANT1 (1)
#define CONSTANT2 (2)
#define CONSTANT3 (3)
#define KEY0 (4)
#define KEY1 (5)
#define KEY2 (6)
#define KEY3 (7)
#define KEY4 (8)
#define KEY5 (9)
#define KEY6 (10)
#define KEY7 (11)
#define COUNTER (12)
#define IV0 (13)
#define IV1 (14)
#define IV2 (15)
/*------*/
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
#define freestyle_encrypt(...) freestyle_xcrypt(__VA_ARGS__,true)
#define freestyle_decrypt(...) freestyle_xcrypt(__VA_ARGS__,false)
#define freestyle_encrypt_block(...) freestyle_xcrypt_block(__VA_ARGS__,true)
#define freestyle_decrypt_block(...) freestyle_xcrypt_block(__VA_ARGS__,false)
#define U8C(v) (v##U)
#define U32C(v) (v##U)
#define U8V(v) ((u8)(v) & U8C(0xFF))
#define U32V(v) ((u32)(v) & U32C(0xFFFFFFFF))
#define ROTL32(v, n) (U32V((v) << (n)) | ((v) >> (32 - (n))))
#define U8TO32_LITTLE(p) \
(((u32)((p)[0]) ) | \
((u32)((p)[1]) << 8) | \
((u32)((p)[2]) << 16) | \
((u32)((p)[3]) << 24))
#define U32TO8_LITTLE(p, v) \
do { \
(p)[0] = U8V((v) ); \
(p)[1] = U8V((v) >> 8); \
(p)[2] = U8V((v) >> 16); \
(p)[3] = U8V((v) >> 24); \
} while (0)
#define ROTATE(v,c) (ROTL32(v,c))
#define XOR(v,w) ((v) ^ (w))
#define PLUS(v,w) (U32V((v) + (w)))
#define MINUS(v,w) (U32V((v) - (w)))
#define PLUSONE(v) (PLUS((v),1))
#define QR(a,b,c,d) { \
a = PLUS(a,b); d = ROTATE(XOR(d,a), 16); \
c = PLUS(c,d); b = ROTATE(XOR(b,c), 12); \
a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); \
c = PLUS(c,d); b = ROTATE(XOR(b,c), 7); \
}
static const char sigma[16] = "expand 32-byte k";
static const char tau[16] = "expand 16-byte k";
#define AXR(a,b,c,r) {a = PLUS(a,b); c = ROTATE(XOR(c,a),r);}
typedef struct freestyle_ctx {
u32 input[16];
u32 initial_counter;
u8 min_rounds;
u8 max_rounds;
u8 num_precomputed_rounds;
u8 hash_interval;
u32 rand[8];
u8 num_init_hashes;
u8 init_hash [MAX_INIT_HASHES];
u8 pepper_bits;
u32 pepper;
bool is_pepper_set;
} freestyle_ctx;
void freestyle_set_counter (freestyle_ctx* const x, const u32 counter);
void freestyle_init_encrypt (
freestyle_ctx* const x,
const u8* const key,
const u16 key_length_bits,
const u8* const iv,
const u8 min_rounds,
const u8 max_rounds,
const u8 num_precomputed_rounds,
const u8 pepper_bits,
const u8 num_init_hashes
);
void freestyle_init_encrypt_with_pepper (
freestyle_ctx* const x,
const u8* const key,
const u16 key_length_bits,
const u8* const iv,
const u8 min_rounds,
const u8 max_rounds,
const u8 num_precomputed_rounds,
const u8 pepper_bits,
const u8 num_init_hashes,
const u32 pepper
);
bool freestyle_init_decrypt (
freestyle_ctx* const x,
const u8* const key,
const u16 key_length_bits,
const u8* const iv,
const u8 min_rounds,
const u8 max_rounds,
const u8 num_precomputed_rounds,
const u8 pepper_bits,
const u8 num_init_hashes,
const u8* const init_hash
);
bool freestyle_init_decrypt_with_pepper (
freestyle_ctx* const x,
const u8* const key,
const u16 key_length_bits,
const u8* const iv,
const u8 min_rounds,
const u8 max_rounds,
const u8 num_precomputed_rounds,
const u8 pepper_bits,
const u8 num_init_hashes,
const u32 pepper,
const u8* const init_hash
);
int freestyle_xcrypt (
freestyle_ctx* const x,
const u8* const input,
u8* const output,
u32 bytes,
u8* const hash,
const bool do_encryption
);
void freestyle_hash_password (
const char* const password,
const u8* const salt,
u8* const hash,
const size_t hash_len,
const u8 min_rounds,
const u8 max_rounds,
const u8 num_precomputed_rounds,
const u8 pepper_bits,
const u8 num_init_hashes
);
void freestyle_hash_password_with_pepper (
const char* const password,
const u8* const salt,
u8* const hash,
const size_t hash_len,
const u8 min_rounds,
const u8 max_rounds,
const u8 num_precomputed_rounds,
const u8 pepper_bits,
const u8 num_init_hashes,
const u32 pepper
);
bool freestyle_verify_password_hash (
const char* const password,
const u8* const salt,
const u8* const hash,
const size_t hash_len,
const u8 min_rounds,
const u8 max_rounds,
const u8 num_precomputed_rounds,
const u8 pepper_bits,
const u8 num_init_hashes
);
#endif /* FREESTYLE_H */