blob: 615e467579eeacd72196883d0cbfe5ab2eb3c9a1 [file] [log] [blame]
Jason Cooper449bb812014-03-24 01:48:58 +00001
2#ifndef THREEFISHAPI_H
3#define THREEFISHAPI_H
4
5/**
Jake Edge85dfd522014-05-19 17:48:24 -06006 * @file threefish_api.h
Jason Cooper449bb812014-03-24 01:48:58 +00007 * @brief A Threefish cipher API and its functions.
8 * @{
9 *
10 * This API and the functions that implement this API simplify the usage
Jason Cooper06a620f2014-03-24 01:49:10 +000011 * of the Threefish cipher. The design and the way to use the functions
Jason Cooper449bb812014-03-24 01:48:58 +000012 * follow the openSSL design but at the same time take care of some Threefish
13 * specific behaviour and possibilities.
14 *
Jake Edgea82100e2014-05-20 08:00:33 -060015 * These are the low level functions that deal with Threefish blocks only.
Jason Cooper06a620f2014-03-24 01:49:10 +000016 * Implementations for cipher modes such as ECB, CFB, or CBC may use these
Jason Cooper449bb812014-03-24 01:48:58 +000017 * functions.
Jason Cooper06a620f2014-03-24 01:49:10 +000018 *
Jason Cooper449bb812014-03-24 01:48:58 +000019@code
Anton Saraev9435d3a2014-05-19 12:09:57 +040020 // Threefish cipher context data
21 struct threefish_key key_ctx;
Jason Cooper449bb812014-03-24 01:48:58 +000022
Anton Saraev9435d3a2014-05-19 12:09:57 +040023 // Initialize the context
24 threefish_set_key(&key_ctx, THREEFISH_512, key, tweak);
Jason Cooper449bb812014-03-24 01:48:58 +000025
Anton Saraev9435d3a2014-05-19 12:09:57 +040026 // Encrypt
27 threefish_encrypt_block_bytes(&key_ctx, input, cipher);
Jason Cooper449bb812014-03-24 01:48:58 +000028@endcode
29 */
30
Jason Cooperc2c74262014-03-24 02:32:49 +000031#include <linux/types.h>
Eric Rostc17cdeb2014-10-24 17:32:53 -050032#include "skein_base.h"
Jason Cooper449bb812014-03-24 01:48:58 +000033
Anton Saraev0264b7b2014-05-19 12:09:58 +040034#define KEY_SCHEDULE_CONST 0x1BD11BDAA9FC1A22L
Jason Cooper449bb812014-03-24 01:48:58 +000035
Jason Cooper39bd42b2014-03-24 01:49:09 +000036/**
37 * Which Threefish size to use
38 */
39enum threefish_size {
Anton Saraev9435d3a2014-05-19 12:09:57 +040040 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 Cooper39bd42b2014-03-24 01:49:09 +000043};
Jason Cooper449bb812014-03-24 01:48:58 +000044
Jason Cooper39bd42b2014-03-24 01:49:09 +000045/**
46 * Context for Threefish key and tweak words.
Jason Cooper06a620f2014-03-24 01:49:10 +000047 *
Jason Cooper39bd42b2014-03-24 01:49:09 +000048 * 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 */
53struct threefish_key {
Anton Saraev95f1840a2014-05-19 12:09:55 +040054 u64 state_size;
Manu Kumar78930e72016-04-04 16:53:01 -070055 u64 key[SKEIN_MAX_STATE_WORDS + 1]; /* max number of key words*/
Jason Cooper39bd42b2014-03-24 01:49:09 +000056 u64 tweak[3];
57};
Jason Cooper449bb812014-03-24 01:48:58 +000058
Jason Cooper39bd42b2014-03-24 01:49:09 +000059/**
60 * Set Threefish key and tweak data.
Jason Cooper06a620f2014-03-24 01:49:10 +000061 *
Jason Cooper39bd42b2014-03-24 01:49:09 +000062 * 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 Cooper06a620f2014-03-24 01:49:10 +000064 * as the state size
Jason Cooper39bd42b2014-03-24 01:49:09 +000065 *
Anton Saraev95f1840a2014-05-19 12:09:55 +040066 * @param key_ctx
Jason Cooper39bd42b2014-03-24 01:49:09 +000067 * Pointer to a Threefish key structure.
68 * @param size
69 * Which Skein size to use.
Anton Saraev95f1840a2014-05-19 12:09:55 +040070 * @param key_data
Jason Cooper39bd42b2014-03-24 01:49:09 +000071 * 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 Saraev95f1840a2014-05-19 12:09:55 +040075void threefish_set_key(struct threefish_key *key_ctx,
76 enum threefish_size state_size,
77 u64 *key_data, u64 *tweak);
Jason Cooper449bb812014-03-24 01:48:58 +000078
Jason Cooper39bd42b2014-03-24 01:49:09 +000079/**
Jake Edgea82100e2014-05-20 08:00:33 -060080 * Encrypt Threefish block (bytes).
Jason Cooper06a620f2014-03-24 01:49:10 +000081 *
Jake Edgea82100e2014-05-20 08:00:33 -060082 * The buffer must have at least the same length (number of bits) as the
Anton Saraev95f1840a2014-05-19 12:09:55 +040083 * state size for this key. The function uses the first @c state_size bits
Jason Cooper39bd42b2014-03-24 01:49:09 +000084 * of the input buffer, encrypts them and stores the result in the output
85 * buffer.
Jason Cooper06a620f2014-03-24 01:49:10 +000086 *
Anton Saraev95f1840a2014-05-19 12:09:55 +040087 * @param key_ctx
Jason Cooper39bd42b2014-03-24 01:49:09 +000088 * 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 Saraev95f1840a2014-05-19 12:09:55 +040094void threefish_encrypt_block_bytes(struct threefish_key *key_ctx, u8 *in,
Anton Saraev68ace622014-05-19 12:09:54 +040095 u8 *out);
Jason Cooper449bb812014-03-24 01:48:58 +000096
Jason Cooper39bd42b2014-03-24 01:49:09 +000097/**
Jake Edgea82100e2014-05-20 08:00:33 -060098 * Encrypt Threefish block (words).
Jason Cooper06a620f2014-03-24 01:49:10 +000099 *
Jake Edgea82100e2014-05-20 08:00:33 -0600100 * The buffer must have at least the same length (number of bits) as the
Anton Saraev95f1840a2014-05-19 12:09:55 +0400101 * state size for this key. The function uses the first @c state_size bits
Jason Cooper39bd42b2014-03-24 01:49:09 +0000102 * of the input buffer, encrypts them and stores the result in the output
103 * buffer.
Jason Cooper06a620f2014-03-24 01:49:10 +0000104 *
Jason Cooper39bd42b2014-03-24 01:49:09 +0000105 * The wordsize ist set to 64 bits.
Jason Cooper06a620f2014-03-24 01:49:10 +0000106 *
Anton Saraev95f1840a2014-05-19 12:09:55 +0400107 * @param key_ctx
Jason Cooper39bd42b2014-03-24 01:49:09 +0000108 * 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 Saraev95f1840a2014-05-19 12:09:55 +0400114void threefish_encrypt_block_words(struct threefish_key *key_ctx, u64 *in,
Anton Saraev68ace622014-05-19 12:09:54 +0400115 u64 *out);
Jason Cooper39bd42b2014-03-24 01:49:09 +0000116
117/**
Jake Edgea82100e2014-05-20 08:00:33 -0600118 * Decrypt Threefish block (bytes).
Jason Cooper06a620f2014-03-24 01:49:10 +0000119 *
Jake Edgea82100e2014-05-20 08:00:33 -0600120 * The buffer must have at least the same length (number of bits) as the
Anton Saraev95f1840a2014-05-19 12:09:55 +0400121 * state size for this key. The function uses the first @c state_size bits
Jason Cooper39bd42b2014-03-24 01:49:09 +0000122 * of the input buffer, decrypts them and stores the result in the output
123 * buffer
Jason Cooper06a620f2014-03-24 01:49:10 +0000124 *
Anton Saraev95f1840a2014-05-19 12:09:55 +0400125 * @param key_ctx
Jason Cooper39bd42b2014-03-24 01:49:09 +0000126 * 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 Saraev95f1840a2014-05-19 12:09:55 +0400132void threefish_decrypt_block_bytes(struct threefish_key *key_ctx, u8 *in,
Anton Saraev68ace622014-05-19 12:09:54 +0400133 u8 *out);
Jason Cooper39bd42b2014-03-24 01:49:09 +0000134
135/**
Jake Edgea82100e2014-05-20 08:00:33 -0600136 * Decrypt Threefish block (words).
Jason Cooper06a620f2014-03-24 01:49:10 +0000137 *
Jake Edgea82100e2014-05-20 08:00:33 -0600138 * The buffer must have at least the same length (number of bits) as the
Anton Saraev95f1840a2014-05-19 12:09:55 +0400139 * state size for this key. The function uses the first @c state_size bits
Jason Cooper39bd42b2014-03-24 01:49:09 +0000140 * of the input buffer, encrypts them and stores the result in the output
141 * buffer.
Jason Cooper06a620f2014-03-24 01:49:10 +0000142 *
Jason Cooper39bd42b2014-03-24 01:49:09 +0000143 * The wordsize ist set to 64 bits.
Jason Cooper06a620f2014-03-24 01:49:10 +0000144 *
Anton Saraev95f1840a2014-05-19 12:09:55 +0400145 * @param key_ctx
Jason Cooper39bd42b2014-03-24 01:49:09 +0000146 * 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 Saraev95f1840a2014-05-19 12:09:55 +0400152void threefish_decrypt_block_words(struct threefish_key *key_ctx, u64 *in,
Anton Saraev68ace622014-05-19 12:09:54 +0400153 u64 *out);
Jason Cooper39bd42b2014-03-24 01:49:09 +0000154
Anton Saraev95f1840a2014-05-19 12:09:55 +0400155void threefish_encrypt_256(struct threefish_key *key_ctx, u64 *input,
Anton Saraev68ace622014-05-19 12:09:54 +0400156 u64 *output);
Anton Saraev95f1840a2014-05-19 12:09:55 +0400157void threefish_encrypt_512(struct threefish_key *key_ctx, u64 *input,
Anton Saraev68ace622014-05-19 12:09:54 +0400158 u64 *output);
Anton Saraev95f1840a2014-05-19 12:09:55 +0400159void threefish_encrypt_1024(struct threefish_key *key_ctx, u64 *input,
Anton Saraev68ace622014-05-19 12:09:54 +0400160 u64 *output);
Anton Saraev95f1840a2014-05-19 12:09:55 +0400161void threefish_decrypt_256(struct threefish_key *key_ctx, u64 *input,
Anton Saraev68ace622014-05-19 12:09:54 +0400162 u64 *output);
Anton Saraev95f1840a2014-05-19 12:09:55 +0400163void threefish_decrypt_512(struct threefish_key *key_ctx, u64 *input,
Anton Saraev68ace622014-05-19 12:09:54 +0400164 u64 *output);
Anton Saraev95f1840a2014-05-19 12:09:55 +0400165void threefish_decrypt_1024(struct threefish_key *key_ctx, u64 *input,
Anton Saraev68ace622014-05-19 12:09:54 +0400166 u64 *output);
Jason Cooper449bb812014-03-24 01:48:58 +0000167/**
168 * @}
169 */
170#endif