Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 1 | |
| 2 | #ifndef THREEFISHAPI_H |
| 3 | #define THREEFISHAPI_H |
| 4 | |
| 5 | /** |
| 6 | * @file threefishApi.h |
| 7 | * @brief A Threefish cipher API and its functions. |
| 8 | * @{ |
| 9 | * |
| 10 | * This API and the functions that implement this API simplify the usage |
| 11 | * of the Threefish cipher. The design and the way to use the functions |
| 12 | * follow the openSSL design but at the same time take care of some Threefish |
| 13 | * specific behaviour and possibilities. |
| 14 | * |
| 15 | * These are the low level functions that deal with Threefisch blocks only. |
| 16 | * Implementations for cipher modes such as ECB, CFB, or CBC may use these |
| 17 | * functions. |
| 18 | * |
| 19 | @code |
| 20 | // Threefish cipher context data |
Jason Cooper | 11d9ffb | 2014-03-24 01:49:04 +0000 | [diff] [blame] | 21 | struct threefish_key keyCtx; |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 22 | |
| 23 | // Initialize the context |
| 24 | threefishSetKey(&keyCtx, Threefish512, key, tweak); |
| 25 | |
| 26 | // Encrypt |
| 27 | threefishEncryptBlockBytes(&keyCtx, input, cipher); |
| 28 | @endcode |
| 29 | */ |
| 30 | |
Jason Cooper | c2c7426 | 2014-03-24 02:32:49 +0000 | [diff] [blame] | 31 | #include <linux/types.h> |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 32 | #include <skein.h> |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 33 | |
| 34 | #define KeyScheduleConst 0x1BD11BDAA9FC1A22L |
| 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 { |
| 40 | Threefish256 = 256, /*!< Skein with 256 bit state */ |
| 41 | Threefish512 = 512, /*!< Skein with 512 bit state */ |
| 42 | Threefish1024 = 1024 /*!< Skein with 1024 bit state */ |
| 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. |
| 47 | * |
| 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 { |
| 54 | u64 stateSize; |
| 55 | u64 key[SKEIN_MAX_STATE_WORDS+1]; /* max number of key words*/ |
| 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. |
| 61 | * |
| 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) |
| 64 | * as the state size |
| 65 | * |
| 66 | * @param keyCtx |
| 67 | * Pointer to a Threefish key structure. |
| 68 | * @param size |
| 69 | * Which Skein size to use. |
| 70 | * @param keyData |
| 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 | */ |
| 75 | void threefishSetKey(struct threefish_key *keyCtx, enum threefish_size stateSize, u64 *keyData, u64 *tweak); |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 76 | |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame^] | 77 | /** |
| 78 | * Encrypt Threefisch block (bytes). |
| 79 | * |
| 80 | * The buffer must have at least the same length (number of bits) aas the |
| 81 | * state size for this key. The function uses the first @c stateSize bits |
| 82 | * of the input buffer, encrypts them and stores the result in the output |
| 83 | * buffer. |
| 84 | * |
| 85 | * @param keyCtx |
| 86 | * Pointer to a Threefish key structure. |
| 87 | * @param in |
| 88 | * Poionter to plaintext data buffer. |
| 89 | * @param out |
| 90 | * Pointer to cipher buffer. |
| 91 | */ |
| 92 | void threefishEncryptBlockBytes(struct threefish_key *keyCtx, u8 *in, u8 *out); |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 93 | |
Jason Cooper | 39bd42b | 2014-03-24 01:49:09 +0000 | [diff] [blame^] | 94 | /** |
| 95 | * Encrypt Threefisch block (words). |
| 96 | * |
| 97 | * The buffer must have at least the same length (number of bits) aas the |
| 98 | * state size for this key. The function uses the first @c stateSize bits |
| 99 | * of the input buffer, encrypts them and stores the result in the output |
| 100 | * buffer. |
| 101 | * |
| 102 | * The wordsize ist set to 64 bits. |
| 103 | * |
| 104 | * @param keyCtx |
| 105 | * Pointer to a Threefish key structure. |
| 106 | * @param in |
| 107 | * Poionter to plaintext data buffer. |
| 108 | * @param out |
| 109 | * Pointer to cipher buffer. |
| 110 | */ |
| 111 | void threefishEncryptBlockWords(struct threefish_key *keyCtx, u64 *in, u64 *out); |
| 112 | |
| 113 | /** |
| 114 | * Decrypt Threefisch block (bytes). |
| 115 | * |
| 116 | * The buffer must have at least the same length (number of bits) aas the |
| 117 | * state size for this key. The function uses the first @c stateSize bits |
| 118 | * of the input buffer, decrypts them and stores the result in the output |
| 119 | * buffer |
| 120 | * |
| 121 | * @param keyCtx |
| 122 | * Pointer to a Threefish key structure. |
| 123 | * @param in |
| 124 | * Poionter to cipher data buffer. |
| 125 | * @param out |
| 126 | * Pointer to plaintext buffer. |
| 127 | */ |
| 128 | void threefishDecryptBlockBytes(struct threefish_key *keyCtx, u8 *in, u8 *out); |
| 129 | |
| 130 | /** |
| 131 | * Decrypt Threefisch block (words). |
| 132 | * |
| 133 | * The buffer must have at least the same length (number of bits) aas the |
| 134 | * state size for this key. The function uses the first @c stateSize bits |
| 135 | * of the input buffer, encrypts them and stores the result in the output |
| 136 | * buffer. |
| 137 | * |
| 138 | * The wordsize ist set to 64 bits. |
| 139 | * |
| 140 | * @param keyCtx |
| 141 | * Pointer to a Threefish key structure. |
| 142 | * @param in |
| 143 | * Poionter to cipher data buffer. |
| 144 | * @param out |
| 145 | * Pointer to plaintext buffer. |
| 146 | */ |
| 147 | void threefishDecryptBlockWords(struct threefish_key *keyCtx, u64 *in, u64 *out); |
| 148 | |
| 149 | void threefishEncrypt256(struct threefish_key *keyCtx, u64 *input, u64 *output); |
| 150 | void threefishEncrypt512(struct threefish_key *keyCtx, u64 *input, u64 *output); |
| 151 | void threefishEncrypt1024(struct threefish_key *keyCtx, u64 *input, u64 *output); |
| 152 | void threefishDecrypt256(struct threefish_key *keyCtx, u64 *input, u64 *output); |
| 153 | void threefishDecrypt512(struct threefish_key *keyCtx, u64 *input, u64 *output); |
| 154 | void threefishDecrypt1024(struct threefish_key *keyCtx, u64 *input, u64 *output); |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 155 | /** |
| 156 | * @} |
| 157 | */ |
| 158 | #endif |