#include "gostr34_11_94.h"
static const u8 gostr34_11_94_sbox_norm[8][16] =
{
{ 4, 10, 9, 2, 13, 8, 0, 14, 6, 11, 1, 12, 7, 15, 5, 3 },
{ 14, 11, 4, 12, 6, 13, 15, 10, 2, 3, 8, 1, 0, 7, 5, 9 },
{ 5, 8, 1, 13, 10, 3, 4, 2, 14, 15, 12, 7, 6, 0, 9, 11 },
{ 7, 13, 10, 1, 0, 8, 9, 15, 14, 4, 6, 12, 11, 2, 5, 3 },
{ 6, 12, 7, 1, 5, 15, 13, 8, 4, 10, 9, 14, 0, 3, 11, 2 },
{ 4, 11, 10, 0, 7, 2, 1, 13, 3, 6, 8, 5, 9, 12, 15, 14 },
{ 13, 11, 4, 1, 3, 15, 5, 9, 0, 10, 14, 7, 6, 8, 2, 12 },
{ 1, 15, 13, 0, 5, 7, 10, 4, 9, 2, 3, 14, 6, 11, 8, 12 }
};
static const u8 gostr34_11_94_sbox_rfc4357[8][16] =
{
{ 10, 4, 5, 6, 8, 1, 3, 7, 13, 12, 14, 0, 9, 2, 11, 15},
{ 5, 15, 4, 0, 2, 13, 11, 9, 1, 7, 6, 3, 12, 14, 10, 8},
{ 7, 15, 12, 14, 9, 4, 1, 0, 3, 11, 5, 2, 6, 10, 8, 13},
{ 4, 10, 7, 12, 0, 15, 2, 8, 14, 1, 6, 5, 13, 11, 9, 3},
{ 7, 6, 4, 11, 9, 12, 2, 10, 1, 8, 0, 14, 15, 13, 3, 5},
{ 7, 6, 2, 4, 13, 9, 15, 0, 10, 1, 5, 11, 8, 14, 12, 3},
{ 13, 14, 4, 1, 7, 0, 5, 10, 3, 12, 8, 15, 6, 2, 9, 11},
{ 1, 3, 10, 9, 5, 11, 4, 15, 8, 6, 7, 14, 13, 0, 2, 12}
};
ATTRIBUTE_WARN_UNUSED_RET static inline int gostr34_11_94_arch_is_big_endian(void)
{
const u16 val = 0x0102;
const u8 *buf = (const u8 *)(&val);
return (buf[0] == 0x01);
}
static inline void gostr34_11_94_A(const u64 Y[GOSTR34_11_94_STATE_SIZE], u64 Y_[GOSTR34_11_94_STATE_SIZE])
{
u64 y1, y2, y3, y4;
y1 = Y[3];
y2 = Y[2];
y3 = Y[1];
y4 = Y[0];
Y_[0] = (y1 ^ y2);
Y_[1] = y4;
Y_[2] = y3;
Y_[3] = y2;
return;
}
static inline void gostr34_11_94_P(const u64 Y[GOSTR34_11_94_STATE_SIZE], u64 Y_[GOSTR34_11_94_STATE_SIZE])
{
unsigned int i, k;
const u8 *y = (const u8*)Y;
u8 *y_ = (u8*)Y_;
for(i = 0; i < 4; i++){
for(k = 1; k < 9; k++){
unsigned int phi_idx = (8 * GOSTR34_11_94_STATE_SIZE) - (i + (4 * (k - 1)));
unsigned int phi = ((8 * i) + k);
y_[phi_idx - 1] = y[phi - 1];
}
}
return;
}
static const u64 gostr34_11_94_C[3][GOSTR34_11_94_STATE_SIZE] = {
{ 0, 0, 0, 0 },
{ 0xff000000ffff00ffULL, 0x00ffff00ff0000ffULL, 0xff00ff00ff00ff00ULL, 0x00ff00ff00ff00ffULL },
{ 0, 0, 0, 0 },
};
ATTRIBUTE_WARN_UNUSED_RET static inline int gostr34_11_94_key_generation(const u64 H[GOSTR34_11_94_STATE_SIZE], const u64 M[GOSTR34_11_94_STATE_SIZE], u64 K[4][GOSTR34_11_94_STATE_SIZE])
{
u64 U[GOSTR34_11_94_STATE_SIZE], V[GOSTR34_11_94_STATE_SIZE], W[GOSTR34_11_94_STATE_SIZE];
unsigned int i, j;
int ret;
ret = local_memcpy(U, H, sizeof(U)); EG(ret, err);
ret = local_memcpy(V, M, sizeof(V)); EG(ret, err);
for(j = 0; j < GOSTR34_11_94_STATE_SIZE; j++){
W[j] = (U[j] ^ V[j]);
}
gostr34_11_94_P(W, K[0]);
for(i = 1; i < 4; i++){
gostr34_11_94_A(U, U);
for(j = 0; j < GOSTR34_11_94_STATE_SIZE; j++){
u64 C;
GET_UINT64_LE(C, (const u8*)&gostr34_11_94_C[i - 1][j], 0);
U[j] = (u64)(U[j] ^ C);
}
gostr34_11_94_A(V, V);
gostr34_11_94_A(V, V);
for(j = 0; j < GOSTR34_11_94_STATE_SIZE; j++){
W[j] = (u64)(U[j] ^ V[j]);
}
gostr34_11_94_P(W, K[i]);
}
ret = 0;
err:
return ret;
}
ATTRIBUTE_WARN_UNUSED_RET static inline int gostr34_11_94_block_encryption(const u64 K[GOSTR34_11_94_STATE_SIZE], const u64 P, u64 *E, const u8 sbox[8][16])
{
int ret;
unsigned int round, i;
u32 R_i, L_i, R_i1 = 0, L_i1 = 0;
const u8 *p = (const u8*)&P;
u8 *e = (u8*)E;
MUST_HAVE((K != NULL) && (sbox != NULL) && (E != NULL), ret, err);
GET_UINT32_BE(L_i, p, 0);
GET_UINT32_BE(R_i, p, 4);
for(round = 0; round < 32; round++){
u32 sk;
const u8 *k = (const u8*)K;
u8 *r_i1 = (u8 *)&R_i1;
if(round < 24){
GET_UINT32_LE(sk, k, (4 * (round % 8)));
}
else{
GET_UINT32_LE(sk, k, (4 * (7 - (round % 8))));
}
R_i1 = (u32)(R_i + sk);
for(i = 0; i < 4; i++){
unsigned int sb_idx;
if(gostr34_11_94_arch_is_big_endian()){
sb_idx = (2 * (3 - i));
}
else{
sb_idx = (2 * i);
}
r_i1[i] = (u8)((sbox[sb_idx + 1][(r_i1[i] & 0xf0) >> 4] << 4) | (sbox[sb_idx][(r_i1[i] & 0x0f)]));
}
R_i1 = (u32)(ROTL_GOSTR34_11_94(R_i1, 11) ^ L_i);
L_i1 = R_i;
R_i = R_i1;
L_i = L_i1;
}
PUT_UINT32_LE(L_i1, e, 0);
PUT_UINT32_LE(R_i1, e, 4);
ret = 0;
err:
return ret;
}
ATTRIBUTE_WARN_UNUSED_RET static inline int gostr34_11_94_state_encryption(const u64 K[4][GOSTR34_11_94_STATE_SIZE], const u64 H[GOSTR34_11_94_STATE_SIZE], u64 S[GOSTR34_11_94_STATE_SIZE], const u8 sbox[8][16])
{
int ret;
MUST_HAVE((GOSTR34_11_94_STATE_SIZE == 4), ret, err);
ret = gostr34_11_94_block_encryption(K[0], H[3], &S[0], sbox); EG(ret, err);
ret = gostr34_11_94_block_encryption(K[1], H[2], &S[1], sbox); EG(ret, err);
ret = gostr34_11_94_block_encryption(K[2], H[1], &S[2], sbox); EG(ret, err);
ret = gostr34_11_94_block_encryption(K[3], H[0], &S[3], sbox); EG(ret, err);
ret = 0;
err:
return ret;
}
typedef union {
u64 A[GOSTR34_11_94_STATE_SIZE];
u16 B[16];
} gostr34_11_94_union;
ATTRIBUTE_WARN_UNUSED_RET static inline int gostr34_11_94_state_psi(const u64 G[GOSTR34_11_94_STATE_SIZE], u64 G_[GOSTR34_11_94_STATE_SIZE])
{
int ret;
unsigned int i;
gostr34_11_94_union G_copy;
gostr34_11_94_union *g = &G_copy;
gostr34_11_94_union *g_ = (gostr34_11_94_union*)G_;
MUST_HAVE((sizeof(gostr34_11_94_union) == (sizeof(u64) * GOSTR34_11_94_STATE_SIZE)), ret, err);
ret = local_memcpy(g, G, sizeof(gostr34_11_94_union)); EG(ret, err);
for(i = 0; i < 15; i++){
g_->B[i] = g->B[i + 1];
}
g_->B[15] = (u16)((g->B[0]) ^ (g->B[1]) ^ (g->B[2]) ^ (g->B[3]) ^ (g->B[12]) ^ (g->B[15]));
ret = 0;
err:
return ret;
}
ATTRIBUTE_WARN_UNUSED_RET static inline int gostr34_11_94_state_output_transform(const u64 H[GOSTR34_11_94_STATE_SIZE], const u64 S[GOSTR34_11_94_STATE_SIZE], const u64 M[GOSTR34_11_94_STATE_SIZE], u64 H_[GOSTR34_11_94_STATE_SIZE])
{
unsigned int i;
int ret;
ret = local_memcpy(H_, S, GOSTR34_11_94_STATE_SIZE * sizeof(u64)); EG(ret, err);
for(i = 0; i < 12; i++){
ret = gostr34_11_94_state_psi(H_, H_); EG(ret, err);
}
for(i = 0; i < GOSTR34_11_94_STATE_SIZE; i++){
u64 m;
if(gostr34_11_94_arch_is_big_endian()){
GET_UINT64_LE(m, (const u8*)&M[GOSTR34_11_94_STATE_SIZE - i - 1], 0);
}
else{
GET_UINT64_BE(m, (const u8*)&M[GOSTR34_11_94_STATE_SIZE - i - 1], 0);
}
H_[i] = (u64)(H_[i] ^ m);
}
ret = gostr34_11_94_state_psi(H_, H_); EG(ret, err);
for(i = 0; i < GOSTR34_11_94_STATE_SIZE; i++){
u64 h;
if(gostr34_11_94_arch_is_big_endian()){
GET_UINT64_LE(h, (const u8*)&H[GOSTR34_11_94_STATE_SIZE - i - 1], 0);
}
else{
GET_UINT64_BE(h, (const u8*)&H[GOSTR34_11_94_STATE_SIZE - i - 1], 0);
}
H_[i] = (u64)(H_[i] ^ h);
}
for(i = 0; i < 61; i++){
ret = gostr34_11_94_state_psi(H_, H_); EG(ret, err);
}
ret = 0;
err:
return ret;
}
static inline void gostr34_11_94_256bit_sum(const u64 A[GOSTR34_11_94_STATE_SIZE], const u64 B[GOSTR34_11_94_STATE_SIZE], u64 C[GOSTR34_11_94_STATE_SIZE])
{
unsigned int i;
u64 tmp, carry1, carry2, _carry;
_carry = 0;
for(i = 0; i < GOSTR34_11_94_STATE_SIZE; i++){
u64 a, b, c;
unsigned int idx = (GOSTR34_11_94_STATE_SIZE - i - 1);
GET_UINT64_BE(a, (const u8*)(&A[idx]), 0);
GET_UINT64_BE(b, (const u8*)(&B[idx]), 0);
tmp = (u64)(a + b);
carry1 = (u64)(tmp < a);
c = (u64)(tmp + _carry);
carry2 = (u64)(c < tmp);
_carry = (u64)(carry1 | carry2);
PUT_UINT64_BE(c, (u8*)(&C[idx]), 0);
}
return;
}
ATTRIBUTE_WARN_UNUSED_RET static inline int gostr34_11_94_process(gostr34_11_94_context *ctx,
const u8 data[GOSTR34_11_94_BLOCK_SIZE])
{
int ret;
unsigned int i;
u64 K[4][GOSTR34_11_94_STATE_SIZE];
u64 H[GOSTR34_11_94_STATE_SIZE], S[GOSTR34_11_94_STATE_SIZE], M[GOSTR34_11_94_STATE_SIZE];
MUST_HAVE((data != NULL), ret, err);
GOSTR34_11_94_HASH_CHECK_INITIALIZED(ctx, ret, err);
for(i = 0; i < GOSTR34_11_94_BLOCK_SIZE; i++){
((u8*)M)[i] = data[GOSTR34_11_94_BLOCK_SIZE - i - 1];
}
for(i = 0; i < GOSTR34_11_94_BLOCK_SIZE; i++){
((u8*)H)[i] = ((u8*)ctx->gostr34_11_94_state)[GOSTR34_11_94_BLOCK_SIZE - i - 1];
}
ret = gostr34_11_94_key_generation(H, M, K); EG(ret, err);
switch(ctx->gostr34_11_94_t){
case GOST34_11_94_NORM:{
ret = gostr34_11_94_state_encryption((const u64 (*)[4])K, H, S, gostr34_11_94_sbox_norm); EG(ret, err);
break;
}
case GOST34_11_94_RFC4357:{
ret = gostr34_11_94_state_encryption((const u64 (*)[4])K, H, S, gostr34_11_94_sbox_rfc4357); EG(ret, err);
break;
}
default:{
ret = -1;
goto err;
}
}
ret = gostr34_11_94_state_output_transform(H, S, M, ctx->gostr34_11_94_state); EG(ret, err);
gostr34_11_94_256bit_sum(ctx->gostr34_11_94_sum, M, ctx->gostr34_11_94_sum);
ret = 0;
err:
return ret;
}
ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94_init(gostr34_11_94_context *ctx)
{
int ret;
MUST_HAVE((ctx != NULL), ret, err);
MUST_HAVE((GOSTR34_11_94_DIGEST_SIZE <= MAX_DIGEST_SIZE), ret, err);
ctx->gostr34_11_94_total = 0;
ctx->gostr34_11_94_state[0] = 0;
ctx->gostr34_11_94_state[1] = 0;
ctx->gostr34_11_94_state[2] = 0;
ctx->gostr34_11_94_state[3] = 0;
ret = local_memset(ctx->gostr34_11_94_sum, 0, sizeof(ctx->gostr34_11_94_sum)); EG(ret, err);
ctx->gostr34_11_94_t = GOST34_11_94_NORM;
ctx->magic = GOSTR34_11_94_HASH_MAGIC;
ret = 0;
err:
return ret;
}
ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94_set_iv(gostr34_11_94_context *ctx, const u64 iv[GOSTR34_11_94_STATE_SIZE])
{
int ret;
MUST_HAVE((iv != NULL), ret, err);
GOSTR34_11_94_HASH_CHECK_INITIALIZED(ctx, ret, err);
MUST_HAVE((ctx->gostr34_11_94_total == 0), ret, err);
ctx->gostr34_11_94_state[0] = iv[0];
ctx->gostr34_11_94_state[1] = iv[1];
ctx->gostr34_11_94_state[2] = iv[2];
ctx->gostr34_11_94_state[3] = iv[3];
ret = 0;
err:
return ret;
}
ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94_set_type(gostr34_11_94_context *ctx, gostr34_11_94_type type)
{
int ret;
GOSTR34_11_94_HASH_CHECK_INITIALIZED(ctx, ret, err);
MUST_HAVE((ctx->gostr34_11_94_total == 0), ret, err);
if((type != GOST34_11_94_NORM) && (type != GOST34_11_94_RFC4357)){
ret = -1;
goto err;
}
ctx->gostr34_11_94_t = type;
ret = 0;
err:
return ret;
}
ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94_update(gostr34_11_94_context *ctx, const u8 *input, u32 ilen)
{
const u8 *data_ptr = input;
u32 remain_ilen = ilen;
u16 fill;
u8 left;
int ret;
MUST_HAVE((input != NULL) || (ilen == 0), ret, err);
GOSTR34_11_94_HASH_CHECK_INITIALIZED(ctx, ret, err);
if (ilen == 0) {
ret = 0;
goto err;
}
left = (ctx->gostr34_11_94_total & 0x3F);
fill = (u16)(GOSTR34_11_94_BLOCK_SIZE - left);
ctx->gostr34_11_94_total += ilen;
if ((left > 0) && (remain_ilen >= fill)) {
ret = local_memcpy(ctx->gostr34_11_94_buffer + left, data_ptr, fill); EG(ret, err);
ret = gostr34_11_94_process(ctx, ctx->gostr34_11_94_buffer); EG(ret, err);
data_ptr += fill;
remain_ilen -= fill;
left = 0;
}
while (remain_ilen >= GOSTR34_11_94_BLOCK_SIZE) {
ret = gostr34_11_94_process(ctx, data_ptr); EG(ret, err);
data_ptr += GOSTR34_11_94_BLOCK_SIZE;
remain_ilen -= GOSTR34_11_94_BLOCK_SIZE;
}
if (remain_ilen > 0) {
ret = local_memcpy(ctx->gostr34_11_94_buffer + left, data_ptr, remain_ilen); EG(ret, err);
}
ret = 0;
err:
return ret;
}
ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94_final(gostr34_11_94_context *ctx, u8 output[GOSTR34_11_94_DIGEST_SIZE])
{
unsigned int block_present = 0;
u8 last_padded_block[2 * GOSTR34_11_94_BLOCK_SIZE];
int ret;
MUST_HAVE((output != NULL), ret, err);
GOSTR34_11_94_HASH_CHECK_INITIALIZED(ctx, ret, err);
ret = local_memset(last_padded_block, 0, sizeof(last_padded_block)); EG(ret, err);
block_present = ctx->gostr34_11_94_total % GOSTR34_11_94_BLOCK_SIZE;
ret = local_memcpy(last_padded_block, ctx->gostr34_11_94_buffer,
block_present); EG(ret, err);
PUT_UINT64_LE(8 * ctx->gostr34_11_94_total, last_padded_block, GOSTR34_11_94_BLOCK_SIZE);
if(block_present != 0){
ret = gostr34_11_94_process(ctx, last_padded_block); EG(ret, err);
}
if(gostr34_11_94_arch_is_big_endian()){
PUT_UINT64_LE(ctx->gostr34_11_94_sum[3], last_padded_block, 0);
PUT_UINT64_LE(ctx->gostr34_11_94_sum[2], last_padded_block, 8);
PUT_UINT64_LE(ctx->gostr34_11_94_sum[1], last_padded_block, 16);
PUT_UINT64_LE(ctx->gostr34_11_94_sum[0], last_padded_block, 24);
}
else{
PUT_UINT64_BE(ctx->gostr34_11_94_sum[3], last_padded_block, 0);
PUT_UINT64_BE(ctx->gostr34_11_94_sum[2], last_padded_block, 8);
PUT_UINT64_BE(ctx->gostr34_11_94_sum[1], last_padded_block, 16);
PUT_UINT64_BE(ctx->gostr34_11_94_sum[0], last_padded_block, 24);
}
ret = gostr34_11_94_process(ctx, last_padded_block + GOSTR34_11_94_BLOCK_SIZE); EG(ret, err);
ret = gostr34_11_94_process(ctx, last_padded_block); EG(ret, err);
if(gostr34_11_94_arch_is_big_endian()){
PUT_UINT64_BE(ctx->gostr34_11_94_state[0], output, 0);
PUT_UINT64_BE(ctx->gostr34_11_94_state[1], output, 8);
PUT_UINT64_BE(ctx->gostr34_11_94_state[2], output, 16);
PUT_UINT64_BE(ctx->gostr34_11_94_state[3], output, 24);
}
else{
PUT_UINT64_LE(ctx->gostr34_11_94_state[0], output, 0);
PUT_UINT64_LE(ctx->gostr34_11_94_state[1], output, 8);
PUT_UINT64_LE(ctx->gostr34_11_94_state[2], output, 16);
PUT_UINT64_LE(ctx->gostr34_11_94_state[3], output, 24);
}
ctx->magic = WORD(0);
ret = 0;
err:
return ret;
}
ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94_scattered(const u8 **inputs, const u32 *ilens,
u8 output[GOSTR34_11_94_DIGEST_SIZE], gostr34_11_94_type type)
{
gostr34_11_94_context ctx;
int ret, pos = 0;
MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);
ret = gostr34_11_94_init(&ctx); EG(ret, err);
ret = gostr34_11_94_set_type(&ctx, type); EG(ret, err);
while (inputs[pos] != NULL) {
ret = gostr34_11_94_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);
pos += 1;
}
ret = gostr34_11_94_final(&ctx, output);
err:
return ret;
}
ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94_scattered_norm(const u8 **inputs, const u32 *ilens,
u8 output[GOSTR34_11_94_DIGEST_SIZE])
{
return gostr34_11_94_scattered(inputs, ilens, output, GOST34_11_94_NORM);
}
ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94_scattered_rfc4357(const u8 **inputs, const u32 *ilens,
u8 output[GOSTR34_11_94_DIGEST_SIZE])
{
return gostr34_11_94_scattered(inputs, ilens, output, GOST34_11_94_RFC4357);
}
ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94(const u8 *input, u32 ilen, u8 output[GOSTR34_11_94_DIGEST_SIZE], gostr34_11_94_type type)
{
gostr34_11_94_context ctx;
int ret;
ret = gostr34_11_94_init(&ctx); EG(ret, err);
ret = gostr34_11_94_set_type(&ctx, type); EG(ret, err);
ret = gostr34_11_94_update(&ctx, input, ilen); EG(ret, err);
ret = gostr34_11_94_final(&ctx, output);
err:
return ret;
}
ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94_norm(const u8 *input, u32 ilen, u8 output[GOSTR34_11_94_DIGEST_SIZE])
{
return gostr34_11_94(input, ilen, output, GOST34_11_94_NORM);
}
ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94_rfc4357(const u8 *input, u32 ilen, u8 output[GOSTR34_11_94_DIGEST_SIZE])
{
return gostr34_11_94(input, ilen, output, GOST34_11_94_RFC4357);
}