blob: 199257e3781314b111a6a72379dfc0e10d0fc37a [file] [log] [blame]
Jason Cooper449bb812014-03-24 01:48:58 +00001
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 Cooper11d9ffb2014-03-24 01:49:04 +000021 struct threefish_key keyCtx;
Jason Cooper449bb812014-03-24 01:48:58 +000022
23 // Initialize the context
24 threefishSetKey(&keyCtx, Threefish512, key, tweak);
25
26 // Encrypt
27 threefishEncryptBlockBytes(&keyCtx, input, cipher);
28@endcode
29 */
30
Jason Cooperc2c74262014-03-24 02:32:49 +000031#include <linux/types.h>
Jason Cooper449bb812014-03-24 01:48:58 +000032#include <skein.h>
Jason Cooper449bb812014-03-24 01:48:58 +000033
34#define KeyScheduleConst 0x1BD11BDAA9FC1A22L
35
Jason Cooper39bd42b2014-03-24 01:49:09 +000036/**
37 * Which Threefish size to use
38 */
39enum 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 Cooper449bb812014-03-24 01:48:58 +000044
Jason Cooper39bd42b2014-03-24 01:49:09 +000045/**
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 */
53struct threefish_key {
54 u64 stateSize;
55 u64 key[SKEIN_MAX_STATE_WORDS+1]; /* max number of key words*/
56 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.
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 */
75void threefishSetKey(struct threefish_key *keyCtx, enum threefish_size stateSize, u64 *keyData, u64 *tweak);
Jason Cooper449bb812014-03-24 01:48:58 +000076
Jason Cooper39bd42b2014-03-24 01:49:09 +000077/**
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 */
92void threefishEncryptBlockBytes(struct threefish_key *keyCtx, u8 *in, u8 *out);
Jason Cooper449bb812014-03-24 01:48:58 +000093
Jason Cooper39bd42b2014-03-24 01:49:09 +000094/**
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 */
111void 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 */
128void 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 */
147void threefishDecryptBlockWords(struct threefish_key *keyCtx, u64 *in, u64 *out);
148
149void threefishEncrypt256(struct threefish_key *keyCtx, u64 *input, u64 *output);
150void threefishEncrypt512(struct threefish_key *keyCtx, u64 *input, u64 *output);
151void threefishEncrypt1024(struct threefish_key *keyCtx, u64 *input, u64 *output);
152void threefishDecrypt256(struct threefish_key *keyCtx, u64 *input, u64 *output);
153void threefishDecrypt512(struct threefish_key *keyCtx, u64 *input, u64 *output);
154void threefishDecrypt1024(struct threefish_key *keyCtx, u64 *input, u64 *output);
Jason Cooper449bb812014-03-24 01:48:58 +0000155/**
156 * @}
157 */
158#endif