-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfctr.c
44 lines (36 loc) · 1.14 KB
/
tfctr.c
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
#include <string.h>
#include "tfdef.h"
void tf_ctr_crypt(const void *key, void *ctr, void *out, const void *in, size_t sz)
{
const TF_BYTE_TYPE *uin = (const TF_BYTE_TYPE *)in;
TF_BYTE_TYPE *uout = (TF_BYTE_TYPE *)out;
TF_UNIT_TYPE x[TF_NR_BLOCK_UNITS], y[TF_NR_BLOCK_UNITS];
TF_UNIT_TYPE *uctr = (TF_UNIT_TYPE *)ctr;
const TF_UNIT_TYPE *ukey = (const TF_UNIT_TYPE *)key;
size_t sl = sz, i;
if (sl >= TF_BLOCK_SIZE) {
do {
memcpy(x, uin, TF_BLOCK_SIZE);
uin += TF_BLOCK_SIZE;
data_to_words(x, TF_BLOCK_SIZE);
ctr_inc(uctr, TF_NR_BLOCK_UNITS);
tf_encrypt_rawblk(y, uctr, ukey);
for (i = 0; i < TF_NR_BLOCK_UNITS; i++) y[i] ^= x[i];
data_to_words(y, TF_BLOCK_SIZE);
memcpy(uout, y, TF_BLOCK_SIZE);
uout += TF_BLOCK_SIZE;
} while ((sl -= TF_BLOCK_SIZE) >= TF_BLOCK_SIZE);
}
if (sl) {
memset(x, 0, TF_BLOCK_SIZE);
memcpy(x, uin, sl);
data_to_words(x, TF_BLOCK_SIZE);
ctr_inc(uctr, TF_NR_BLOCK_UNITS);
tf_encrypt_rawblk(y, uctr, ukey);
for (i = 0; i < TF_NR_BLOCK_UNITS; i++) y[i] ^= x[i];
data_to_words(y, TF_BLOCK_SIZE);
memcpy(uout, y, sl);
}
memset(x, 0, TF_BLOCK_SIZE);
memset(y, 0, TF_BLOCK_SIZE);
}