GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / staging / skein / threefish_api.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/string.h>
3 #include "threefish_api.h"
4
5 void threefish_set_key(struct threefish_key *key_ctx,
6                        enum threefish_size state_size,
7                        u64 *key_data, u64 *tweak)
8 {
9         int key_words = state_size / 64;
10         int i;
11         u64 parity = KEY_SCHEDULE_CONST;
12
13         key_ctx->tweak[0] = tweak[0];
14         key_ctx->tweak[1] = tweak[1];
15         key_ctx->tweak[2] = tweak[0] ^ tweak[1];
16
17         for (i = 0; i < key_words; i++) {
18                 key_ctx->key[i] = key_data[i];
19                 parity ^= key_data[i];
20         }
21         key_ctx->key[i] = parity;
22         key_ctx->state_size = state_size;
23 }
24
25 void threefish_encrypt_block_bytes(struct threefish_key *key_ctx, u8 *in,
26                                    u8 *out)
27 {
28         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, key_ctx->state_size / 64);
32         threefish_encrypt_block_words(key_ctx, plain, cipher);
33         skein_put64_lsb_first(out, cipher, key_ctx->state_size / 8);
34 }
35
36 void threefish_encrypt_block_words(struct threefish_key *key_ctx, u64 *in,
37                                    u64 *out)
38 {
39         switch (key_ctx->state_size) {
40         case THREEFISH_256:
41                 threefish_encrypt_256(key_ctx, in, out);
42                 break;
43         case THREEFISH_512:
44                 threefish_encrypt_512(key_ctx, in, out);
45                 break;
46         case THREEFISH_1024:
47                 threefish_encrypt_1024(key_ctx, in, out);
48                 break;
49         }
50 }
51
52 void threefish_decrypt_block_bytes(struct threefish_key *key_ctx, u8 *in,
53                                    u8 *out)
54 {
55         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, key_ctx->state_size / 64);
59         threefish_decrypt_block_words(key_ctx, cipher, plain);
60         skein_put64_lsb_first(out, plain, key_ctx->state_size / 8);
61 }
62
63 void threefish_decrypt_block_words(struct threefish_key *key_ctx, u64 *in,
64                                    u64 *out)
65 {
66         switch (key_ctx->state_size) {
67         case THREEFISH_256:
68                 threefish_decrypt_256(key_ctx, in, out);
69                 break;
70         case THREEFISH_512:
71                 threefish_decrypt_512(key_ctx, in, out);
72                 break;
73         case THREEFISH_1024:
74                 threefish_decrypt_1024(key_ctx, in, out);
75                 break;
76         }
77 }
78