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