blob: e8ce06a9122f40abcf98d20bcb7041f4cea3a058 [file] [log] [blame]
Jason Cooper449bb812014-03-24 01:48:58 +00001
2
Jason Cooperc2c74262014-03-24 02:32:49 +00003#include <linux/string.h>
Jason Cooper449bb812014-03-24 01:48:58 +00004#include <threefishApi.h>
Jason Cooper449bb812014-03-24 01:48:58 +00005
Jason Coopercd4811a2014-03-24 01:49:06 +00006void threefishSetKey(struct threefish_key *keyCtx, enum threefish_size stateSize,
Jason Cooper39bd42b2014-03-24 01:49:09 +00007 u64 *keyData, u64 *tweak)
Jason Cooper449bb812014-03-24 01:48:58 +00008{
Jason Cooper39bd42b2014-03-24 01:49:09 +00009 int keyWords = stateSize / 64;
10 int i;
11 u64 parity = KeyScheduleConst;
Jason Cooper449bb812014-03-24 01:48:58 +000012
Jason Cooper39bd42b2014-03-24 01:49:09 +000013 keyCtx->tweak[0] = tweak[0];
14 keyCtx->tweak[1] = tweak[1];
15 keyCtx->tweak[2] = tweak[0] ^ tweak[1];
Jason Cooper449bb812014-03-24 01:48:58 +000016
Jason Cooper39bd42b2014-03-24 01:49:09 +000017 for (i = 0; i < keyWords; i++) {
18 keyCtx->key[i] = keyData[i];
19 parity ^= keyData[i];
20 }
21 keyCtx->key[i] = parity;
22 keyCtx->stateSize = stateSize;
Jason Cooper449bb812014-03-24 01:48:58 +000023}
24
Jason Coopercd4811a2014-03-24 01:49:06 +000025void threefishEncryptBlockBytes(struct threefish_key *keyCtx, u8 *in,
Jason Cooper39bd42b2014-03-24 01:49:09 +000026 u8 *out)
Jason Cooper449bb812014-03-24 01:48:58 +000027{
Jason Cooper39bd42b2014-03-24 01:49:09 +000028 u64 plain[SKEIN_MAX_STATE_WORDS]; /* max number of words*/
29 u64 cipher[SKEIN_MAX_STATE_WORDS];
30
31 Skein_Get64_LSB_First(plain, in, keyCtx->stateSize / 64); /* bytes to words */
32 threefishEncryptBlockWords(keyCtx, plain, cipher);
33 Skein_Put64_LSB_First(out, cipher, keyCtx->stateSize / 8); /* words to bytes */
Jason Cooper449bb812014-03-24 01:48:58 +000034}
35
Jason Coopercd4811a2014-03-24 01:49:06 +000036void threefishEncryptBlockWords(struct threefish_key *keyCtx, u64 *in,
Jason Cooper39bd42b2014-03-24 01:49:09 +000037 u64 *out)
Jason Cooper449bb812014-03-24 01:48:58 +000038{
Jason Cooper39bd42b2014-03-24 01:49:09 +000039 switch (keyCtx->stateSize) {
40 case Threefish256:
41 threefishEncrypt256(keyCtx, in, out);
42 break;
43 case Threefish512:
44 threefishEncrypt512(keyCtx, in, out);
45 break;
46 case Threefish1024:
47 threefishEncrypt1024(keyCtx, in, out);
48 break;
49 }
Jason Cooper449bb812014-03-24 01:48:58 +000050}
51
Jason Coopercd4811a2014-03-24 01:49:06 +000052void threefishDecryptBlockBytes(struct threefish_key *keyCtx, u8 *in,
Jason Cooper39bd42b2014-03-24 01:49:09 +000053 u8 *out)
Jason Cooper449bb812014-03-24 01:48:58 +000054{
Jason Cooper39bd42b2014-03-24 01:49:09 +000055 u64 plain[SKEIN_MAX_STATE_WORDS]; /* max number of words*/
56 u64 cipher[SKEIN_MAX_STATE_WORDS];
57
58 Skein_Get64_LSB_First(cipher, in, keyCtx->stateSize / 64); /* bytes to words */
59 threefishDecryptBlockWords(keyCtx, cipher, plain);
60 Skein_Put64_LSB_First(out, plain, keyCtx->stateSize / 8); /* words to bytes */
Jason Cooper449bb812014-03-24 01:48:58 +000061}
62
Jason Coopercd4811a2014-03-24 01:49:06 +000063void threefishDecryptBlockWords(struct threefish_key *keyCtx, u64 *in,
Jason Cooper39bd42b2014-03-24 01:49:09 +000064 u64 *out)
Jason Cooper449bb812014-03-24 01:48:58 +000065{
Jason Cooper39bd42b2014-03-24 01:49:09 +000066 switch (keyCtx->stateSize) {
67 case Threefish256:
68 threefishDecrypt256(keyCtx, in, out);
69 break;
70 case Threefish512:
71 threefishDecrypt512(keyCtx, in, out);
72 break;
73 case Threefish1024:
74 threefishDecrypt1024(keyCtx, in, out);
75 break;
76 }
Jason Cooper449bb812014-03-24 01:48:58 +000077}
78