2014-03-23 20:32:49 -06:00
|
|
|
#include <linux/string.h>
|
2014-05-19 17:48:24 -06:00
|
|
|
#include "threefish_api.h"
|
2014-03-23 19:48:58 -06:00
|
|
|
|
2014-05-19 02:09:55 -06:00
|
|
|
void threefish_set_key(struct threefish_key *key_ctx,
|
|
|
|
enum threefish_size state_size,
|
|
|
|
u64 *key_data, u64 *tweak)
|
2014-03-23 19:48:58 -06:00
|
|
|
{
|
2014-05-19 02:09:55 -06:00
|
|
|
int key_words = state_size / 64;
|
2014-03-23 19:49:09 -06:00
|
|
|
int i;
|
2014-05-19 02:09:58 -06:00
|
|
|
u64 parity = KEY_SCHEDULE_CONST;
|
2014-03-23 19:48:58 -06:00
|
|
|
|
2014-05-19 02:09:55 -06:00
|
|
|
key_ctx->tweak[0] = tweak[0];
|
|
|
|
key_ctx->tweak[1] = tweak[1];
|
|
|
|
key_ctx->tweak[2] = tweak[0] ^ tweak[1];
|
2014-03-23 19:48:58 -06:00
|
|
|
|
2014-05-19 02:09:55 -06:00
|
|
|
for (i = 0; i < key_words; i++) {
|
|
|
|
key_ctx->key[i] = key_data[i];
|
|
|
|
parity ^= key_data[i];
|
2014-03-23 19:49:09 -06:00
|
|
|
}
|
2014-05-19 02:09:55 -06:00
|
|
|
key_ctx->key[i] = parity;
|
|
|
|
key_ctx->state_size = state_size;
|
2014-03-23 19:48:58 -06:00
|
|
|
}
|
|
|
|
|
2014-05-19 02:09:55 -06:00
|
|
|
void threefish_encrypt_block_bytes(struct threefish_key *key_ctx, u8 *in,
|
2014-05-19 02:09:54 -06:00
|
|
|
u8 *out)
|
2014-03-23 19:48:58 -06:00
|
|
|
{
|
2014-03-23 19:49:09 -06:00
|
|
|
u64 plain[SKEIN_MAX_STATE_WORDS]; /* max number of words*/
|
|
|
|
u64 cipher[SKEIN_MAX_STATE_WORDS];
|
|
|
|
|
2014-05-19 02:09:58 -06:00
|
|
|
skein_get64_lsb_first(plain, in, key_ctx->state_size / 64);
|
2014-05-19 02:09:55 -06:00
|
|
|
threefish_encrypt_block_words(key_ctx, plain, cipher);
|
2014-05-19 02:09:58 -06:00
|
|
|
skein_put64_lsb_first(out, cipher, key_ctx->state_size / 8);
|
2014-03-23 19:48:58 -06:00
|
|
|
}
|
|
|
|
|
2014-05-19 02:09:55 -06:00
|
|
|
void threefish_encrypt_block_words(struct threefish_key *key_ctx, u64 *in,
|
2014-05-19 02:09:54 -06:00
|
|
|
u64 *out)
|
2014-03-23 19:48:58 -06:00
|
|
|
{
|
2014-05-19 02:09:55 -06:00
|
|
|
switch (key_ctx->state_size) {
|
2014-05-19 02:09:57 -06:00
|
|
|
case THREEFISH_256:
|
2014-05-19 02:09:55 -06:00
|
|
|
threefish_encrypt_256(key_ctx, in, out);
|
2014-03-23 19:49:09 -06:00
|
|
|
break;
|
2014-05-19 02:09:57 -06:00
|
|
|
case THREEFISH_512:
|
2014-05-19 02:09:55 -06:00
|
|
|
threefish_encrypt_512(key_ctx, in, out);
|
2014-03-23 19:49:09 -06:00
|
|
|
break;
|
2014-05-19 02:09:57 -06:00
|
|
|
case THREEFISH_1024:
|
2014-05-19 02:09:55 -06:00
|
|
|
threefish_encrypt_1024(key_ctx, in, out);
|
2014-03-23 19:49:09 -06:00
|
|
|
break;
|
|
|
|
}
|
2014-03-23 19:48:58 -06:00
|
|
|
}
|
|
|
|
|
2014-05-19 02:09:55 -06:00
|
|
|
void threefish_decrypt_block_bytes(struct threefish_key *key_ctx, u8 *in,
|
2014-05-19 02:09:54 -06:00
|
|
|
u8 *out)
|
2014-03-23 19:48:58 -06:00
|
|
|
{
|
2014-03-23 19:49:09 -06:00
|
|
|
u64 plain[SKEIN_MAX_STATE_WORDS]; /* max number of words*/
|
|
|
|
u64 cipher[SKEIN_MAX_STATE_WORDS];
|
|
|
|
|
2014-05-19 02:09:58 -06:00
|
|
|
skein_get64_lsb_first(cipher, in, key_ctx->state_size / 64);
|
2014-05-19 02:09:55 -06:00
|
|
|
threefish_decrypt_block_words(key_ctx, cipher, plain);
|
2014-05-19 02:09:58 -06:00
|
|
|
skein_put64_lsb_first(out, plain, key_ctx->state_size / 8);
|
2014-03-23 19:48:58 -06:00
|
|
|
}
|
|
|
|
|
2014-05-19 02:09:55 -06:00
|
|
|
void threefish_decrypt_block_words(struct threefish_key *key_ctx, u64 *in,
|
2014-05-19 02:09:54 -06:00
|
|
|
u64 *out)
|
2014-03-23 19:48:58 -06:00
|
|
|
{
|
2014-05-19 02:09:55 -06:00
|
|
|
switch (key_ctx->state_size) {
|
2014-05-19 02:09:57 -06:00
|
|
|
case THREEFISH_256:
|
2014-05-19 02:09:55 -06:00
|
|
|
threefish_decrypt_256(key_ctx, in, out);
|
2014-03-23 19:49:09 -06:00
|
|
|
break;
|
2014-05-19 02:09:57 -06:00
|
|
|
case THREEFISH_512:
|
2014-05-19 02:09:55 -06:00
|
|
|
threefish_decrypt_512(key_ctx, in, out);
|
2014-03-23 19:49:09 -06:00
|
|
|
break;
|
2014-05-19 02:09:57 -06:00
|
|
|
case THREEFISH_1024:
|
2014-05-19 02:09:55 -06:00
|
|
|
threefish_decrypt_1024(key_ctx, in, out);
|
2014-03-23 19:49:09 -06:00
|
|
|
break;
|
|
|
|
}
|
2014-03-23 19:48:58 -06:00
|
|
|
}
|
|
|
|
|