Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 1 | |
| 2 | |
Jason Cooper | c2c7426 | 2014-03-24 02:32:49 +0000 | [diff] [blame^] | 3 | #include <linux/string.h> |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 4 | #include <threefishApi.h> |
Jason Cooper | 449bb81 | 2014-03-24 01:48:58 +0000 | [diff] [blame] | 5 | |
| 6 | void threefishSetKey(ThreefishKey_t* keyCtx, ThreefishSize_t stateSize, |
| 7 | uint64_t* keyData, uint64_t* tweak) |
| 8 | { |
| 9 | int keyWords = stateSize / 64; |
| 10 | int i; |
| 11 | uint64_t parity = KeyScheduleConst; |
| 12 | |
| 13 | keyCtx->tweak[0] = tweak[0]; |
| 14 | keyCtx->tweak[1] = tweak[1]; |
| 15 | keyCtx->tweak[2] = tweak[0] ^ tweak[1]; |
| 16 | |
| 17 | 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; |
| 23 | } |
| 24 | |
| 25 | void threefishEncryptBlockBytes(ThreefishKey_t* keyCtx, uint8_t* in, |
| 26 | uint8_t* out) |
| 27 | { |
| 28 | u64b_t plain[SKEIN_MAX_STATE_WORDS]; /* max number of words*/ |
| 29 | u64b_t 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 */ |
| 34 | } |
| 35 | |
| 36 | void threefishEncryptBlockWords(ThreefishKey_t* keyCtx, uint64_t* in, |
| 37 | uint64_t* out) |
| 38 | { |
| 39 | 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 | } |
| 50 | } |
| 51 | |
| 52 | void threefishDecryptBlockBytes(ThreefishKey_t* keyCtx, uint8_t* in, |
| 53 | uint8_t* out) |
| 54 | { |
| 55 | u64b_t plain[SKEIN_MAX_STATE_WORDS]; /* max number of words*/ |
| 56 | u64b_t 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 */ |
| 61 | } |
| 62 | |
| 63 | void threefishDecryptBlockWords(ThreefishKey_t* keyCtx, uint64_t* in, |
| 64 | uint64_t* out) |
| 65 | { |
| 66 | 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 | } |
| 77 | } |
| 78 | |