summaryrefslogtreecommitdiffstats
path: root/drivers/staging/skein/threefishApi.c
blob: 5afa0338aef48da119668dc93a1d40ca3b2d8061 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79


#include <threefishApi.h>
#include <stdlib.h>
#include <string.h>

void threefishSetKey(ThreefishKey_t* keyCtx, ThreefishSize_t stateSize,
                     uint64_t* keyData, uint64_t* tweak)
{
    int keyWords = stateSize / 64;
    int i;
    uint64_t parity = KeyScheduleConst;

    keyCtx->tweak[0] = tweak[0];
    keyCtx->tweak[1] = tweak[1];
    keyCtx->tweak[2] = tweak[0] ^ tweak[1];

    for (i = 0; i < keyWords; i++) {
        keyCtx->key[i] = keyData[i];
        parity ^= keyData[i];
    }
    keyCtx->key[i] = parity;
    keyCtx->stateSize = stateSize;
}

void threefishEncryptBlockBytes(ThreefishKey_t* keyCtx, uint8_t* in,
                                uint8_t* out)
{
    u64b_t plain[SKEIN_MAX_STATE_WORDS];        /* max number of words*/
    u64b_t cipher[SKEIN_MAX_STATE_WORDS];
    
    Skein_Get64_LSB_First(plain, in, keyCtx->stateSize / 64);   /* bytes to words */
    threefishEncryptBlockWords(keyCtx, plain, cipher);
    Skein_Put64_LSB_First(out, cipher, keyCtx->stateSize / 8);  /* words to bytes */
}

void threefishEncryptBlockWords(ThreefishKey_t* keyCtx, uint64_t* in,
                                uint64_t* out)
{
    switch (keyCtx->stateSize) {
        case Threefish256:
            threefishEncrypt256(keyCtx, in, out);
            break;
        case Threefish512:
            threefishEncrypt512(keyCtx, in, out);
            break;
        case Threefish1024:
            threefishEncrypt1024(keyCtx, in, out);
            break;
    }
}

void threefishDecryptBlockBytes(ThreefishKey_t* keyCtx, uint8_t* in,
                                uint8_t* out)
{
    u64b_t plain[SKEIN_MAX_STATE_WORDS];        /* max number of words*/
    u64b_t cipher[SKEIN_MAX_STATE_WORDS];
    
    Skein_Get64_LSB_First(cipher, in, keyCtx->stateSize / 64);  /* bytes to words */
    threefishDecryptBlockWords(keyCtx, cipher, plain);
    Skein_Put64_LSB_First(out, plain, keyCtx->stateSize / 8);   /* words to bytes */
}

void threefishDecryptBlockWords(ThreefishKey_t* keyCtx, uint64_t* in,
                                uint64_t* out)
{
    switch (keyCtx->stateSize) {
        case Threefish256:
            threefishDecrypt256(keyCtx, in, out);
            break;
        case Threefish512:
            threefishDecrypt512(keyCtx, in, out);
            break;
        case Threefish1024:
            threefishDecrypt1024(keyCtx, in, out);
            break;
    }
}