ctr_add: replace dead with working code.
[tfcrypt.git] / tfdef.h
1 #ifndef _THREEFISH_CIPHER_DEFINITIONS_HEADER
2 #define _THREEFISH_CIPHER_DEFINITIONS_HEADER
3
4 #ifndef _DEFAULT_SOURCE
5 #define _DEFAULT_SOURCE
6 #endif
7
8 #ifndef _BSD_SOURCE
9 #define _BSD_SOURCE
10 #endif
11
12 /* config block */
13 /* #define TF_256BITS */
14 /* #define TF_512BITS */
15 #define TF_1024BITS
16 /* #define TF_NO_ENDIAN */
17 /* #define TF_BIG_ENDIAN */
18
19 #include <stddef.h>
20 #include <stdint.h>
21 #ifndef TF_NO_ENDIAN
22 #include <sys/param.h>
23 #else
24 #undef TF_BIG_ENDIAN
25 #endif
26
27 #define TF_UNIT_TYPE uint64_t
28
29 #ifdef TF_BIG_ENDIAN
30 #define TF_SWAP_FUNC htobe64
31 #else
32 #define TF_SWAP_FUNC htole64
33 #endif
34
35 #if defined(TF_256BITS)
36 #define TF_NR_BLOCK_BITS 256
37 #define TF_NR_KEY_BITS 512
38 #define TF_NR_BLOCK_UNITS 4
39 #define TF_NR_KEY_UNITS 8
40 #define IRR_POLY_CONST 0x425
41 #elif defined(TF_512BITS)
42 #define TF_NR_BLOCK_BITS 512
43 #define TF_NR_KEY_BITS 768
44 #define TF_NR_BLOCK_UNITS 8
45 #define TF_NR_KEY_UNITS 12
46 #define IRR_POLY_CONST 0x125
47 #elif defined(TF_1024BITS)
48 #define TF_NR_BLOCK_BITS 1024
49 #define TF_NR_KEY_BITS 1280
50 #define TF_NR_BLOCK_UNITS 16
51 #define TF_NR_KEY_UNITS 20
52 #define IRR_POLY_CONST 0x80043
53 #else
54 #error Please edit tfdef.h include file and select at least one cipher!
55 #endif
56
57 #define TF_BYTE_TYPE uint8_t
58 #define TF_SIZE_UNIT (sizeof(TF_UNIT_TYPE))
59 #define TF_BLOCK_SIZE (TF_SIZE_UNIT * TF_NR_BLOCK_UNITS)
60 #define TF_KEY_SIZE (TF_SIZE_UNIT * TF_NR_KEY_UNITS)
61
62 #define TF_TWEAK_WORD1 (TF_NR_KEY_UNITS-3)
63 #define TF_TWEAK_WORD2 (TF_NR_KEY_UNITS-2)
64 #define TF_TWEAK_WORD3 (TF_NR_KEY_UNITS-1)
65
66 #define TF_TO_BITS(x) ((x) * 8)
67 #define TF_FROM_BITS(x) ((x) / 8)
68 #define TF_MAX_BITS TF_NR_BLOCK_BITS
69 #define TF_UNIT_BITS (TF_SIZE_UNIT * 8)
70
71 #define TF_TO_BLOCKS(x) ((x) / TF_BLOCK_SIZE)
72 #define TF_FROM_BLOCKS(x) ((x) * TF_BLOCK_SIZE)
73 #define TF_BLOCKS_TO_BYTES(x) TF_FROM_BLOCKS(x)
74 #define TF_BLOCKS_FROM_BYTES(x) TF_TO_BLOCKS(x)
75
76 static inline void data_to_words(void *p, size_t l)
77 {
78 #ifndef TF_NO_ENDIAN
79         size_t idx;
80         TF_UNIT_TYPE *P = p;
81         TF_UNIT_TYPE t;
82
83         for (idx = 0; idx < (l/sizeof(TF_UNIT_TYPE)); idx++) {
84                 t = TF_SWAP_FUNC(P[idx]);
85                 P[idx] = t;
86         }
87 #endif
88 }
89
90 static inline void ctr_inc(TF_UNIT_TYPE *x, size_t xl)
91 {
92         size_t z;
93
94         for (z = 0; z < xl; z++) {
95                 x[z] = ((x[z] + (TF_UNIT_TYPE)1) & ((TF_UNIT_TYPE)~0));
96                 if (x[z]) break;
97         }
98 }
99
100 static inline void ctr_add(TF_UNIT_TYPE *x, size_t xl, const TF_UNIT_TYPE *y, size_t yl)
101 {
102         size_t z, cf;
103         TF_UNIT_TYPE t;
104
105         for (z = 0, cf = 0; z < xl; z++) {
106                 t = x[z] + (z >= yl ? (TF_UNIT_TYPE)0 : y[z]) + cf;
107                 if (cf) cf = (x[z] >= t ? 1 : 0);
108                 else cf = (x[z] > t ? 1 : 0);
109                 x[z] = t;
110         }
111 }
112
113 struct tfe_stream;
114
115 #define tf_convkey(k) do { data_to_words(k, TF_KEY_SIZE); } while (0)
116
117 void tf_encrypt_rawblk(TF_UNIT_TYPE *O, const TF_UNIT_TYPE *I, const TF_UNIT_TYPE *K);
118 void tf_decrypt_rawblk(TF_UNIT_TYPE *O, const TF_UNIT_TYPE *I, const TF_UNIT_TYPE *K);
119
120 void tf_encrypt_block(const void *key, void *out, const void *in);
121 void tf_decrypt_block(const void *key, void *out, const void *in);
122
123 void tf_ctr_set(void *ctr, const void *sctr, size_t sctrsz);
124 void tf_ctr_crypt(const void *key, void *ctr, void *out, const void *in, size_t sz);
125 void tf_stream_crypt(struct tfe_stream *tfe, void *out, const void *in, size_t sz);
126 void tf_ecb_encrypt(const void *key, void *out, const void *in, size_t sz);
127 void tf_ecb_decrypt(const void *key, void *out, const void *in, size_t sz);
128 void tf_cbc_encrypt(const void *key, void *iv, void *out, const void *in, size_t sz);
129 void tf_cbc_decrypt(const void *key, void *iv, void *out, const void *in, size_t sz);
130 void tf_xts_encrypt(const void *keyx, const void *keyz, void *ctr, void *out, const void *in, size_t sz, size_t bpi);
131 void tf_xts_decrypt(const void *keyx, const void *keyz, void *ctr, void *out, const void *in, size_t sz, size_t bpi);
132 void tf_ocb_encrypt(const void *key, void *ctr, void *out, void *tag, const void *in, size_t sz, size_t bpi);
133 void tf_ocb_decrypt(const void *key, void *ctr, void *out, void *tag, const void *in, size_t sz, size_t bpi);
134
135 #endif