Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 1 | |
| 2 | #ifndef THREEFISHAPI_H |
| 3 | #define THREEFISHAPI_H |
| 4 | |
| 5 | /** |
Jake Edge | 85dfd52 | 2014-05-19 17:48:24 -0600 | [diff] [blame] | 6 | * @file threefish_api.h |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 7 | * @brief A Threefish cipher API and its functions. |
| 8 | * @{ |
| 9 | * |
| 10 | * This API and the functions that implement this API simplify the usage |
Jason Cooper | 06a620f | 2014-03-24 01:49:10 +0000 | [diff] [blame] | 11 | * of the Threefish cipher. The design and the way to use the functions |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 12 | * follow the openSSL design but at the same time take care of some Threefish |
| 13 | * specific behaviour and possibilities. |
| 14 | * |
Jake Edge | a82100e | 2014-05-20 08:00:33 -0600 | [diff] [blame] | 15 | * These are the low level functions that deal with Threefish blocks only. |
Jason Cooper | 06a620f | 2014-03-24 01:49:10 +0000 | [diff] [blame] | 16 | * Implementations for cipher modes such as ECB, CFB, or CBC may use these |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 17 | * functions. |
Jason Cooper | 06a620f | 2014-03-24 01:49:10 +0000 | [diff] [blame] | 18 | * |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 19 | @code |
Anton Saraev | 9435d3a | 2014-05-19 12:09:57 +0400 | [diff] [blame] | 20 | // Threefish cipher context data |
| 21 | struct threefish_key key_ctx; |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 22 | |
Anton Saraev | 9435d3a | 2014-05-19 12:09:57 +0400 | [diff] [blame] | 23 | // Initialize the context |
| 24 | threefish_set_key(&key_ctx, THREEFISH_512, key, tweak); |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 25 | |
Anton Saraev | 9435d3a | 2014-05-19 12:09:57 +0400 | [diff] [blame] | 26 | // Encrypt |
| 27 | threefish_encrypt_block_bytes(&key_ctx, input, cipher); |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 28 | @endcode |
| 29 | */ |
| 30 | |
Jason Cooper | c2c7426 | 2014-03-24 02:32:49 +0000 | [diff] [blame] | 31 | #include <linux/types.h> |
Eric Rost | c17cdeb | 2014-10-24 17:32:53 -0500 | [diff] [blame] | 32 | #include "skein_base.h" |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 33 | |
Anton Saraev | 0264b7b | 2014-05-19 12:09:58 +0400 | [diff] [blame] | 34 | #define KEY_SCHEDULE_CONST 0x1BD11BDAA9FC1A22L |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 35 | |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 36 | /** |
| 37 | * Which Threefish size to use |
| 38 | */ |
| 39 | enum threefish_size { |
Anton Saraev | 9435d3a | 2014-05-19 12:09:57 +0400 | [diff] [blame] | 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 */ |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 43 | }; |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 44 | |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 45 | /** |
| 46 | * Context for Threefish key and tweak words. |
Jason Cooper | 06a620f | 2014-03-24 01:49:10 +0000 | [diff] [blame] | 47 | * |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 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 { |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 54 | u64 state_size; |
Manu Kumar | 78930e7 | 2016-04-04 16:53:01 -0700 | [diff] [blame] | 55 | u64 key[SKEIN_MAX_STATE_WORDS + 1]; /* max number of key words*/ |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 56 | u64 tweak[3]; |
| 57 | }; |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 58 | |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 59 | /** |
| 60 | * Set Threefish key and tweak data. |
Jason Cooper | 06a620f | 2014-03-24 01:49:10 +0000 | [diff] [blame] | 61 | * |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 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) |
Jason Cooper | 06a620f | 2014-03-24 01:49:10 +0000 | [diff] [blame] | 64 | * as the state size |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 65 | * |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 66 | * @param key_ctx |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 67 | * Pointer to a Threefish key structure. |
| 68 | * @param size |
| 69 | * Which Skein size to use. |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 70 | * @param key_data |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 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 | */ |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 75 | void threefish_set_key(struct threefish_key *key_ctx, |
| 76 | enum threefish_size state_size, |
| 77 | u64 *key_data, u64 *tweak); |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 78 | |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 79 | /** |
Jake Edge | a82100e | 2014-05-20 08:00:33 -0600 | [diff] [blame] | 80 | * Encrypt Threefish block (bytes). |
Jason Cooper | 06a620f | 2014-03-24 01:49:10 +0000 | [diff] [blame] | 81 | * |
Jake Edge | a82100e | 2014-05-20 08:00:33 -0600 | [diff] [blame] | 82 | * The buffer must have at least the same length (number of bits) as the |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 83 | * state size for this key. The function uses the first @c state_size bits |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 84 | * of the input buffer, encrypts them and stores the result in the output |
| 85 | * buffer. |
Jason Cooper | 06a620f | 2014-03-24 01:49:10 +0000 | [diff] [blame] | 86 | * |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 87 | * @param key_ctx |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 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 | */ |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 94 | void threefish_encrypt_block_bytes(struct threefish_key *key_ctx, u8 *in, |
Anton Saraev | 68ace62 | 2014-05-19 12:09:54 +0400 | [diff] [blame] | 95 | u8 *out); |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 96 | |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 97 | /** |
Jake Edge | a82100e | 2014-05-20 08:00:33 -0600 | [diff] [blame] | 98 | * Encrypt Threefish block (words). |
Jason Cooper | 06a620f | 2014-03-24 01:49:10 +0000 | [diff] [blame] | 99 | * |
Jake Edge | a82100e | 2014-05-20 08:00:33 -0600 | [diff] [blame] | 100 | * The buffer must have at least the same length (number of bits) as the |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 101 | * state size for this key. The function uses the first @c state_size bits |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 102 | * of the input buffer, encrypts them and stores the result in the output |
| 103 | * buffer. |
Jason Cooper | 06a620f | 2014-03-24 01:49:10 +0000 | [diff] [blame] | 104 | * |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 105 | * The wordsize ist set to 64 bits. |
Jason Cooper | 06a620f | 2014-03-24 01:49:10 +0000 | [diff] [blame] | 106 | * |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 107 | * @param key_ctx |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 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 | */ |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 114 | void threefish_encrypt_block_words(struct threefish_key *key_ctx, u64 *in, |
Anton Saraev | 68ace62 | 2014-05-19 12:09:54 +0400 | [diff] [blame] | 115 | u64 *out); |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 116 | |
| 117 | /** |
Jake Edge | a82100e | 2014-05-20 08:00:33 -0600 | [diff] [blame] | 118 | * Decrypt Threefish block (bytes). |
Jason Cooper | 06a620f | 2014-03-24 01:49:10 +0000 | [diff] [blame] | 119 | * |
Jake Edge | a82100e | 2014-05-20 08:00:33 -0600 | [diff] [blame] | 120 | * The buffer must have at least the same length (number of bits) as the |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 121 | * state size for this key. The function uses the first @c state_size bits |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 122 | * of the input buffer, decrypts them and stores the result in the output |
| 123 | * buffer |
Jason Cooper | 06a620f | 2014-03-24 01:49:10 +0000 | [diff] [blame] | 124 | * |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 125 | * @param key_ctx |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 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 | */ |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 132 | void threefish_decrypt_block_bytes(struct threefish_key *key_ctx, u8 *in, |
Anton Saraev | 68ace62 | 2014-05-19 12:09:54 +0400 | [diff] [blame] | 133 | u8 *out); |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 134 | |
| 135 | /** |
Jake Edge | a82100e | 2014-05-20 08:00:33 -0600 | [diff] [blame] | 136 | * Decrypt Threefish block (words). |
Jason Cooper | 06a620f | 2014-03-24 01:49:10 +0000 | [diff] [blame] | 137 | * |
Jake Edge | a82100e | 2014-05-20 08:00:33 -0600 | [diff] [blame] | 138 | * The buffer must have at least the same length (number of bits) as the |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 139 | * state size for this key. The function uses the first @c state_size bits |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 140 | * of the input buffer, encrypts them and stores the result in the output |
| 141 | * buffer. |
Jason Cooper | 06a620f | 2014-03-24 01:49:10 +0000 | [diff] [blame] | 142 | * |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 143 | * The wordsize ist set to 64 bits. |
Jason Cooper | 06a620f | 2014-03-24 01:49:10 +0000 | [diff] [blame] | 144 | * |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 145 | * @param key_ctx |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 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 | */ |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 152 | void threefish_decrypt_block_words(struct threefish_key *key_ctx, u64 *in, |
Anton Saraev | 68ace62 | 2014-05-19 12:09:54 +0400 | [diff] [blame] | 153 | u64 *out); |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame] | 154 | |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 155 | void threefish_encrypt_256(struct threefish_key *key_ctx, u64 *input, |
Anton Saraev | 68ace62 | 2014-05-19 12:09:54 +0400 | [diff] [blame] | 156 | u64 *output); |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 157 | void threefish_encrypt_512(struct threefish_key *key_ctx, u64 *input, |
Anton Saraev | 68ace62 | 2014-05-19 12:09:54 +0400 | [diff] [blame] | 158 | u64 *output); |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 159 | void threefish_encrypt_1024(struct threefish_key *key_ctx, u64 *input, |
Anton Saraev | 68ace62 | 2014-05-19 12:09:54 +0400 | [diff] [blame] | 160 | u64 *output); |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 161 | void threefish_decrypt_256(struct threefish_key *key_ctx, u64 *input, |
Anton Saraev | 68ace62 | 2014-05-19 12:09:54 +0400 | [diff] [blame] | 162 | u64 *output); |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 163 | void threefish_decrypt_512(struct threefish_key *key_ctx, u64 *input, |
Anton Saraev | 68ace62 | 2014-05-19 12:09:54 +0400 | [diff] [blame] | 164 | u64 *output); |
Anton Saraev | 95f1840a | 2014-05-19 12:09:55 +0400 | [diff] [blame] | 165 | void threefish_decrypt_1024(struct threefish_key *key_ctx, u64 *input, |
Anton Saraev | 68ace62 | 2014-05-19 12:09:54 +0400 | [diff] [blame] | 166 | u64 *output); |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 167 | /** |
| 168 | * @} |
| 169 | */ |
| 170 | #endif |