-
Notifications
You must be signed in to change notification settings - Fork 1
/
tfdef.h
155 lines (128 loc) · 4.51 KB
/
tfdef.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
#ifndef _THREEFISH_CIPHER_DEFINITIONS_HEADER
#define _THREEFISH_CIPHER_DEFINITIONS_HEADER
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#endif
#ifndef _BSD_SOURCE
#define _BSD_SOURCE
#endif
/* config block */
/* #define TF_256BITS */
/* #define TF_512BITS */
#define TF_1024BITS
/* #define TF_NO_ENDIAN */
/* #define TF_BIG_ENDIAN */
#include <stddef.h>
#include <stdint.h>
#ifndef TF_NO_ENDIAN
#include <sys/param.h>
#else
#undef TF_BIG_ENDIAN
#endif
#define TF_UNIT_TYPE uint64_t
#ifdef TF_BIG_ENDIAN
#define TF_SWAP_FUNC htobe64
#else
#define TF_SWAP_FUNC htole64
#endif
#if defined(TF_256BITS)
#define TF_NR_BLOCK_BITS 256
#define TF_NR_KEY_BITS 512
#define TF_NR_BLOCK_UNITS 4
#define TF_NR_KEY_UNITS 8
#define IRR_POLY_CONST 0x425
#elif defined(TF_512BITS)
#define TF_NR_BLOCK_BITS 512
#define TF_NR_KEY_BITS 768
#define TF_NR_BLOCK_UNITS 8
#define TF_NR_KEY_UNITS 12
#define IRR_POLY_CONST 0x125
#elif defined(TF_1024BITS)
#define TF_NR_BLOCK_BITS 1024
#define TF_NR_KEY_BITS 1280
#define TF_NR_BLOCK_UNITS 16
#define TF_NR_KEY_UNITS 20
#define IRR_POLY_CONST 0x80043
#else
#error Please edit tfdef.h include file and select at least one cipher!
#endif
#define TF_BYTE_TYPE uint8_t
#define TF_SIZE_UNIT (sizeof(TF_UNIT_TYPE))
#define TF_BLOCK_SIZE (TF_SIZE_UNIT * TF_NR_BLOCK_UNITS)
#define TF_KEY_SIZE (TF_SIZE_UNIT * TF_NR_KEY_UNITS)
#define TF_NR_TWEAK_UNITS 2
#define TF_NR_TWEAK_BITS 128
#define TF_TWEAK_SIZE (TF_SIZE_UNIT * TF_NR_TWEAK_UNITS)
#define TF_TWEAKEY_SIZE (TF_KEY_SIZE - (2 * TF_TWEAK_SIZE))
#define TF_NR_TWEAKEY_BITS (TF_NR_KEY_BITS - (2 * TF_NR_TWEAK_BITS))
#define TF_TWEAK_WORD1 (TF_NR_KEY_UNITS-3)
#define TF_TWEAK_WORD2 (TF_NR_KEY_UNITS-2)
#define TF_TWEAK_WORD3 (TF_NR_KEY_UNITS-1)
#define TF_TO_BITS(x) ((x) * 8)
#define TF_FROM_BITS(x) ((x) / 8)
#define TF_MAX_BITS TF_NR_BLOCK_BITS
#define TF_UNIT_BITS (TF_SIZE_UNIT * 8)
#define TF_TO_BLOCKS(x) ((x) / TF_BLOCK_SIZE)
#define TF_FROM_BLOCKS(x) ((x) * TF_BLOCK_SIZE)
#define TF_BLOCKS_TO_BYTES(x) TF_FROM_BLOCKS(x)
#define TF_BLOCKS_FROM_BYTES(x) TF_TO_BLOCKS(x)
static inline void data_to_words(void *p, size_t l)
{
#ifndef TF_NO_ENDIAN
size_t idx;
TF_UNIT_TYPE *P = p;
TF_UNIT_TYPE t;
for (idx = 0; idx < (l/sizeof(TF_UNIT_TYPE)); idx++) {
t = TF_SWAP_FUNC(P[idx]);
P[idx] = t;
}
#endif
}
static inline void xor_block(void *dst, const void *src, size_t sz)
{
const size_t *sx = (const size_t *)src;
const TF_BYTE_TYPE *usx = (const TF_BYTE_TYPE *)src;
size_t *dx = (size_t *)dst;
TF_BYTE_TYPE *udx = (TF_BYTE_TYPE *)dst;
size_t sl = sz;
for (sl = 0; sl < (sz / sizeof(size_t)); sl++) dx[sl] ^= sx[sl];
if (sz - (sl * sizeof(size_t))) for (sl *= sizeof(size_t); sl < sz; sl++) udx[sl] ^= usx[sl];
}
static inline void ctr_inc(TF_UNIT_TYPE *x, size_t xl)
{
size_t z;
for (z = 0; z < xl; z++) {
x[z] = ((x[z] + (TF_UNIT_TYPE)1) & ((TF_UNIT_TYPE)~0));
if (x[z]) break;
}
}
static inline void ctr_add(TF_UNIT_TYPE *x, size_t xl, const TF_UNIT_TYPE *y, size_t yl)
{
size_t z, cf;
TF_UNIT_TYPE t;
for (z = 0, cf = 0; z < xl; z++) {
t = x[z] + (z >= yl ? (TF_UNIT_TYPE)0 : y[z]) + cf;
if (cf) cf = (x[z] >= t ? 1 : 0);
else cf = (x[z] > t ? 1 : 0);
x[z] = t;
}
}
struct tfe_stream;
#define tf_convkey(k) do { data_to_words(k, TF_KEY_SIZE); } while (0)
void tf_encrypt_rawblk(TF_UNIT_TYPE *O, const TF_UNIT_TYPE *I, const TF_UNIT_TYPE *K);
void tf_decrypt_rawblk(TF_UNIT_TYPE *O, const TF_UNIT_TYPE *I, const TF_UNIT_TYPE *K);
void tf_encrypt_block(const void *key, void *out, const void *in);
void tf_decrypt_block(const void *key, void *out, const void *in);
void tf_ctr_set(void *ctr, const void *sctr, size_t sctrsz);
void tf_ctr_crypt(const void *key, void *ctr, void *out, const void *in, size_t sz);
void tf_tweak_set(void *key, const void *tweak);
void tf_stream_crypt(struct tfe_stream *tfe, void *out, const void *in, size_t sz);
void tf_ecb_encrypt(const void *key, void *out, const void *in, size_t sz);
void tf_ecb_decrypt(const void *key, void *out, const void *in, size_t sz);
void tf_cbc_encrypt(const void *key, void *iv, void *out, const void *in, size_t sz);
void tf_cbc_decrypt(const void *key, void *iv, void *out, const void *in, size_t sz);
void tf_pcbc_encrypt(const void *key, void *iv, void *out, const void *in, size_t sz);
void tf_pcbc_decrypt(const void *key, void *iv, void *out, const void *in, size_t sz);
void tf_xts_encrypt(const void *keyx, const void *keyz, void *ctr, void *out, const void *in, size_t sz, size_t bpi);
void tf_xts_decrypt(const void *keyx, const void *keyz, void *ctr, void *out, const void *in, size_t sz, size_t bpi);
#endif