Commit | Line | Data |
---|---|---|
449bb812 JC |
1 | |
2 | #ifndef THREEFISHAPI_H | |
3 | #define THREEFISHAPI_H | |
4 | ||
5 | /** | |
85dfd522 | 6 | * @file threefish_api.h |
449bb812 JC |
7 | * @brief A Threefish cipher API and its functions. |
8 | * @{ | |
9 | * | |
10 | * This API and the functions that implement this API simplify the usage | |
06a620f0 | 11 | * of the Threefish cipher. The design and the way to use the functions |
449bb812 JC |
12 | * follow the openSSL design but at the same time take care of some Threefish |
13 | * specific behaviour and possibilities. | |
14 | * | |
a82100e7 | 15 | * These are the low level functions that deal with Threefish blocks only. |
06a620f0 | 16 | * Implementations for cipher modes such as ECB, CFB, or CBC may use these |
449bb812 | 17 | * functions. |
06a620f0 | 18 | * |
449bb812 | 19 | @code |
9435d3ac AS |
20 | // Threefish cipher context data |
21 | struct threefish_key key_ctx; | |
449bb812 | 22 | |
9435d3ac AS |
23 | // Initialize the context |
24 | threefish_set_key(&key_ctx, THREEFISH_512, key, tweak); | |
449bb812 | 25 | |
9435d3ac AS |
26 | // Encrypt |
27 | threefish_encrypt_block_bytes(&key_ctx, input, cipher); | |
449bb812 JC |
28 | @endcode |
29 | */ | |
30 | ||
c2c7426b | 31 | #include <linux/types.h> |
85dfd522 | 32 | #include "skein.h" |
449bb812 | 33 | |
0264b7b7 | 34 | #define KEY_SCHEDULE_CONST 0x1BD11BDAA9FC1A22L |
449bb812 | 35 | |
39bd42b0 JC |
36 | /** |
37 | * Which Threefish size to use | |
38 | */ | |
39 | enum threefish_size { | |
9435d3ac AS |
40 | THREEFISH_256 = 256, /*!< Skein with 256 bit state */ |
41 | THREEFISH_512 = 512, /*!< Skein with 512 bit state */ | |
42 | THREEFISH_1024 = 1024 /*!< Skein with 1024 bit state */ | |
39bd42b0 JC |
43 | }; |
44 | ||
45 | /** | |
46 | * Context for Threefish key and tweak words. | |
06a620f0 | 47 | * |
39bd42b0 JC |
48 | * This structure was setup with some know-how of the internal |
49 | * Skein structures, in particular ordering of header and size dependent | |
50 | * variables. If Skein implementation changes this, the adapt these | |
51 | * structures as well. | |
52 | */ | |
53 | struct threefish_key { | |
95f1840a | 54 | u64 state_size; |
39bd42b0 JC |
55 | u64 key[SKEIN_MAX_STATE_WORDS+1]; /* max number of key words*/ |
56 | u64 tweak[3]; | |
57 | }; | |
58 | ||
59 | /** | |
60 | * Set Threefish key and tweak data. | |
06a620f0 | 61 | * |
39bd42b0 JC |
62 | * This function sets the key and tweak data for the Threefish cipher of |
63 | * the given size. The key data must have the same length (number of bits) | |
06a620f0 | 64 | * as the state size |
39bd42b0 | 65 | * |
95f1840a | 66 | * @param key_ctx |
39bd42b0 JC |
67 | * Pointer to a Threefish key structure. |
68 | * @param size | |
69 | * Which Skein size to use. | |
95f1840a | 70 | * @param key_data |
39bd42b0 JC |
71 | * Pointer to the key words (word has 64 bits). |
72 | * @param tweak | |
73 | * Pointer to the two tweak words (word has 64 bits). | |
74 | */ | |
95f1840a AS |
75 | void threefish_set_key(struct threefish_key *key_ctx, |
76 | enum threefish_size state_size, | |
77 | u64 *key_data, u64 *tweak); | |
39bd42b0 JC |
78 | |
79 | /** | |
a82100e7 | 80 | * Encrypt Threefish block (bytes). |
06a620f0 | 81 | * |
a82100e7 | 82 | * The buffer must have at least the same length (number of bits) as the |
95f1840a | 83 | * state size for this key. The function uses the first @c state_size bits |
39bd42b0 JC |
84 | * of the input buffer, encrypts them and stores the result in the output |
85 | * buffer. | |
06a620f0 | 86 | * |
95f1840a | 87 | * @param key_ctx |
39bd42b0 JC |
88 | * Pointer to a Threefish key structure. |
89 | * @param in | |
90 | * Poionter to plaintext data buffer. | |
91 | * @param out | |
92 | * Pointer to cipher buffer. | |
93 | */ | |
95f1840a | 94 | void threefish_encrypt_block_bytes(struct threefish_key *key_ctx, u8 *in, |
68ace624 | 95 | u8 *out); |
449bb812 | 96 | |
39bd42b0 | 97 | /** |
a82100e7 | 98 | * Encrypt Threefish block (words). |
06a620f0 | 99 | * |
a82100e7 | 100 | * The buffer must have at least the same length (number of bits) as the |
95f1840a | 101 | * state size for this key. The function uses the first @c state_size bits |
39bd42b0 JC |
102 | * of the input buffer, encrypts them and stores the result in the output |
103 | * buffer. | |
06a620f0 | 104 | * |
39bd42b0 | 105 | * The wordsize ist set to 64 bits. |
06a620f0 | 106 | * |
95f1840a | 107 | * @param key_ctx |
39bd42b0 JC |
108 | * Pointer to a Threefish key structure. |
109 | * @param in | |
110 | * Poionter to plaintext data buffer. | |
111 | * @param out | |
112 | * Pointer to cipher buffer. | |
113 | */ | |
95f1840a | 114 | void threefish_encrypt_block_words(struct threefish_key *key_ctx, u64 *in, |
68ace624 | 115 | u64 *out); |
449bb812 | 116 | |
39bd42b0 | 117 | /** |
a82100e7 | 118 | * Decrypt Threefish block (bytes). |
06a620f0 | 119 | * |
a82100e7 | 120 | * The buffer must have at least the same length (number of bits) as the |
95f1840a | 121 | * state size for this key. The function uses the first @c state_size bits |
39bd42b0 JC |
122 | * of the input buffer, decrypts them and stores the result in the output |
123 | * buffer | |
06a620f0 | 124 | * |
95f1840a | 125 | * @param key_ctx |
39bd42b0 JC |
126 | * Pointer to a Threefish key structure. |
127 | * @param in | |
128 | * Poionter to cipher data buffer. | |
129 | * @param out | |
130 | * Pointer to plaintext buffer. | |
131 | */ | |
95f1840a | 132 | void threefish_decrypt_block_bytes(struct threefish_key *key_ctx, u8 *in, |
68ace624 | 133 | u8 *out); |
449bb812 | 134 | |
39bd42b0 | 135 | /** |
a82100e7 | 136 | * Decrypt Threefish block (words). |
06a620f0 | 137 | * |
a82100e7 | 138 | * The buffer must have at least the same length (number of bits) as the |
95f1840a | 139 | * state size for this key. The function uses the first @c state_size bits |
39bd42b0 JC |
140 | * of the input buffer, encrypts them and stores the result in the output |
141 | * buffer. | |
06a620f0 | 142 | * |
39bd42b0 | 143 | * The wordsize ist set to 64 bits. |
06a620f0 | 144 | * |
95f1840a | 145 | * @param key_ctx |
39bd42b0 JC |
146 | * Pointer to a Threefish key structure. |
147 | * @param in | |
148 | * Poionter to cipher data buffer. | |
149 | * @param out | |
150 | * Pointer to plaintext buffer. | |
151 | */ | |
95f1840a | 152 | void threefish_decrypt_block_words(struct threefish_key *key_ctx, u64 *in, |
68ace624 | 153 | u64 *out); |
449bb812 | 154 | |
95f1840a | 155 | void threefish_encrypt_256(struct threefish_key *key_ctx, u64 *input, |
68ace624 | 156 | u64 *output); |
95f1840a | 157 | void threefish_encrypt_512(struct threefish_key *key_ctx, u64 *input, |
68ace624 | 158 | u64 *output); |
95f1840a | 159 | void threefish_encrypt_1024(struct threefish_key *key_ctx, u64 *input, |
68ace624 | 160 | u64 *output); |
95f1840a | 161 | void threefish_decrypt_256(struct threefish_key *key_ctx, u64 *input, |
68ace624 | 162 | u64 *output); |
95f1840a | 163 | void threefish_decrypt_512(struct threefish_key *key_ctx, u64 *input, |
68ace624 | 164 | u64 *output); |
95f1840a | 165 | void threefish_decrypt_1024(struct threefish_key *key_ctx, u64 *input, |
68ace624 | 166 | u64 *output); |
449bb812 JC |
167 | /** |
168 | * @} | |
169 | */ | |
170 | #endif |