staging: crypto: skein: import code from Skein3Fish.git
[cascardo/linux.git] / drivers / staging / skein / threefishApi.c
1
2
3 #include <threefishApi.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 void threefishSetKey(ThreefishKey_t* keyCtx, ThreefishSize_t stateSize,
8                      uint64_t* keyData, uint64_t* tweak)
9 {
10     int keyWords = stateSize / 64;
11     int i;
12     uint64_t parity = KeyScheduleConst;
13
14     keyCtx->tweak[0] = tweak[0];
15     keyCtx->tweak[1] = tweak[1];
16     keyCtx->tweak[2] = tweak[0] ^ tweak[1];
17
18     for (i = 0; i < keyWords; i++) {
19         keyCtx->key[i] = keyData[i];
20         parity ^= keyData[i];
21     }
22     keyCtx->key[i] = parity;
23     keyCtx->stateSize = stateSize;
24 }
25
26 void threefishEncryptBlockBytes(ThreefishKey_t* keyCtx, uint8_t* in,
27                                 uint8_t* out)
28 {
29     u64b_t plain[SKEIN_MAX_STATE_WORDS];        /* max number of words*/
30     u64b_t cipher[SKEIN_MAX_STATE_WORDS];
31     
32     Skein_Get64_LSB_First(plain, in, keyCtx->stateSize / 64);   /* bytes to words */
33     threefishEncryptBlockWords(keyCtx, plain, cipher);
34     Skein_Put64_LSB_First(out, cipher, keyCtx->stateSize / 8);  /* words to bytes */
35 }
36
37 void threefishEncryptBlockWords(ThreefishKey_t* keyCtx, uint64_t* in,
38                                 uint64_t* out)
39 {
40     switch (keyCtx->stateSize) {
41         case Threefish256:
42             threefishEncrypt256(keyCtx, in, out);
43             break;
44         case Threefish512:
45             threefishEncrypt512(keyCtx, in, out);
46             break;
47         case Threefish1024:
48             threefishEncrypt1024(keyCtx, in, out);
49             break;
50     }
51 }
52
53 void threefishDecryptBlockBytes(ThreefishKey_t* keyCtx, uint8_t* in,
54                                 uint8_t* out)
55 {
56     u64b_t plain[SKEIN_MAX_STATE_WORDS];        /* max number of words*/
57     u64b_t cipher[SKEIN_MAX_STATE_WORDS];
58     
59     Skein_Get64_LSB_First(cipher, in, keyCtx->stateSize / 64);  /* bytes to words */
60     threefishDecryptBlockWords(keyCtx, cipher, plain);
61     Skein_Put64_LSB_First(out, plain, keyCtx->stateSize / 8);   /* words to bytes */
62 }
63
64 void threefishDecryptBlockWords(ThreefishKey_t* keyCtx, uint64_t* in,
65                                 uint64_t* out)
66 {
67     switch (keyCtx->stateSize) {
68         case Threefish256:
69             threefishDecrypt256(keyCtx, in, out);
70             break;
71         case Threefish512:
72             threefishDecrypt512(keyCtx, in, out);
73             break;
74         case Threefish1024:
75             threefishDecrypt1024(keyCtx, in, out);
76             break;
77     }
78 }
79