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