• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <linux/string.h>
2 #include "threefish_api.h"
3 
threefish_set_key(struct threefish_key * key_ctx,enum threefish_size state_size,u64 * key_data,u64 * tweak)4 void threefish_set_key(struct threefish_key *key_ctx,
5 		       enum threefish_size state_size,
6 		       u64 *key_data, u64 *tweak)
7 {
8 	int key_words = state_size / 64;
9 	int i;
10 	u64 parity = KEY_SCHEDULE_CONST;
11 
12 	key_ctx->tweak[0] = tweak[0];
13 	key_ctx->tweak[1] = tweak[1];
14 	key_ctx->tweak[2] = tweak[0] ^ tweak[1];
15 
16 	for (i = 0; i < key_words; i++) {
17 		key_ctx->key[i] = key_data[i];
18 		parity ^= key_data[i];
19 	}
20 	key_ctx->key[i] = parity;
21 	key_ctx->state_size = state_size;
22 }
23 
threefish_encrypt_block_bytes(struct threefish_key * key_ctx,u8 * in,u8 * out)24 void threefish_encrypt_block_bytes(struct threefish_key *key_ctx, u8 *in,
25 				   u8 *out)
26 {
27 	u64 plain[SKEIN_MAX_STATE_WORDS];        /* max number of words*/
28 	u64 cipher[SKEIN_MAX_STATE_WORDS];
29 
30 	skein_get64_lsb_first(plain, in, key_ctx->state_size / 64);
31 	threefish_encrypt_block_words(key_ctx, plain, cipher);
32 	skein_put64_lsb_first(out, cipher, key_ctx->state_size / 8);
33 }
34 
threefish_encrypt_block_words(struct threefish_key * key_ctx,u64 * in,u64 * out)35 void threefish_encrypt_block_words(struct threefish_key *key_ctx, u64 *in,
36 				   u64 *out)
37 {
38 	switch (key_ctx->state_size) {
39 	case THREEFISH_256:
40 		threefish_encrypt_256(key_ctx, in, out);
41 		break;
42 	case THREEFISH_512:
43 		threefish_encrypt_512(key_ctx, in, out);
44 		break;
45 	case THREEFISH_1024:
46 		threefish_encrypt_1024(key_ctx, in, out);
47 		break;
48 	}
49 }
50 
threefish_decrypt_block_bytes(struct threefish_key * key_ctx,u8 * in,u8 * out)51 void threefish_decrypt_block_bytes(struct threefish_key *key_ctx, u8 *in,
52 				   u8 *out)
53 {
54 	u64 plain[SKEIN_MAX_STATE_WORDS];        /* max number of words*/
55 	u64 cipher[SKEIN_MAX_STATE_WORDS];
56 
57 	skein_get64_lsb_first(cipher, in, key_ctx->state_size / 64);
58 	threefish_decrypt_block_words(key_ctx, cipher, plain);
59 	skein_put64_lsb_first(out, plain, key_ctx->state_size / 8);
60 }
61 
threefish_decrypt_block_words(struct threefish_key * key_ctx,u64 * in,u64 * out)62 void threefish_decrypt_block_words(struct threefish_key *key_ctx, u64 *in,
63 				   u64 *out)
64 {
65 	switch (key_ctx->state_size) {
66 	case THREEFISH_256:
67 		threefish_decrypt_256(key_ctx, in, out);
68 		break;
69 	case THREEFISH_512:
70 		threefish_decrypt_512(key_ctx, in, out);
71 		break;
72 	case THREEFISH_1024:
73 		threefish_decrypt_1024(key_ctx, in, out);
74 		break;
75 	}
76 }
77 
78