**********************************************************************/
#include <string.h>
#include <openssl/crypto.h>
+#include <openssl/rand.h>
#include "gost89.h"
/*-
Substitution blocks from RFC 4357
n2 = in[4] | (in[5] << 8) | (in[6] << 16) | ((word32) in[7] << 24);
/* Instead of swapping halves, swap names each round */
- n2 ^= f(c, n1 + c->k[0]);
- n1 ^= f(c, n2 + c->k[1]);
- n2 ^= f(c, n1 + c->k[2]);
- n1 ^= f(c, n2 + c->k[3]);
- n2 ^= f(c, n1 + c->k[4]);
- n1 ^= f(c, n2 + c->k[5]);
- n2 ^= f(c, n1 + c->k[6]);
- n1 ^= f(c, n2 + c->k[7]);
-
- n2 ^= f(c, n1 + c->k[0]);
- n1 ^= f(c, n2 + c->k[1]);
- n2 ^= f(c, n1 + c->k[2]);
- n1 ^= f(c, n2 + c->k[3]);
- n2 ^= f(c, n1 + c->k[4]);
- n1 ^= f(c, n2 + c->k[5]);
- n2 ^= f(c, n1 + c->k[6]);
- n1 ^= f(c, n2 + c->k[7]);
-
- n2 ^= f(c, n1 + c->k[0]);
- n1 ^= f(c, n2 + c->k[1]);
- n2 ^= f(c, n1 + c->k[2]);
- n1 ^= f(c, n2 + c->k[3]);
- n2 ^= f(c, n1 + c->k[4]);
- n1 ^= f(c, n2 + c->k[5]);
- n2 ^= f(c, n1 + c->k[6]);
- n1 ^= f(c, n2 + c->k[7]);
-
- n2 ^= f(c, n1 + c->k[7]);
- n1 ^= f(c, n2 + c->k[6]);
- n2 ^= f(c, n1 + c->k[5]);
- n1 ^= f(c, n2 + c->k[4]);
- n2 ^= f(c, n1 + c->k[3]);
- n1 ^= f(c, n2 + c->k[2]);
- n2 ^= f(c, n1 + c->k[1]);
- n1 ^= f(c, n2 + c->k[0]);
+ n2 ^= f(c, n1 + c->key[0] + c->mask[0]);
+ n1 ^= f(c, n2 + c->key[1] + c->mask[1]);
+ n2 ^= f(c, n1 + c->key[2] + c->mask[2]);
+ n1 ^= f(c, n2 + c->key[3] + c->mask[3]);
+ n2 ^= f(c, n1 + c->key[4] + c->mask[4]);
+ n1 ^= f(c, n2 + c->key[5] + c->mask[5]);
+ n2 ^= f(c, n1 + c->key[6] + c->mask[6]);
+ n1 ^= f(c, n2 + c->key[7] + c->mask[7]);
+
+ n2 ^= f(c, n1 + c->key[0] + c->mask[0]);
+ n1 ^= f(c, n2 + c->key[1] + c->mask[1]);
+ n2 ^= f(c, n1 + c->key[2] + c->mask[2]);
+ n1 ^= f(c, n2 + c->key[3] + c->mask[3]);
+ n2 ^= f(c, n1 + c->key[4] + c->mask[4]);
+ n1 ^= f(c, n2 + c->key[5] + c->mask[5]);
+ n2 ^= f(c, n1 + c->key[6] + c->mask[6]);
+ n1 ^= f(c, n2 + c->key[7] + c->mask[7]);
+
+ n2 ^= f(c, n1 + c->key[0] + c->mask[0]);
+ n1 ^= f(c, n2 + c->key[1] + c->mask[1]);
+ n2 ^= f(c, n1 + c->key[2] + c->mask[2]);
+ n1 ^= f(c, n2 + c->key[3] + c->mask[3]);
+ n2 ^= f(c, n1 + c->key[4] + c->mask[4]);
+ n1 ^= f(c, n2 + c->key[5] + c->mask[5]);
+ n2 ^= f(c, n1 + c->key[6] + c->mask[6]);
+ n1 ^= f(c, n2 + c->key[7] + c->mask[7]);
+
+ n2 ^= f(c, n1 + c->key[7] + c->mask[7]);
+ n1 ^= f(c, n2 + c->key[6] + c->mask[6]);
+ n2 ^= f(c, n1 + c->key[5] + c->mask[5]);
+ n1 ^= f(c, n2 + c->key[4] + c->mask[4]);
+ n2 ^= f(c, n1 + c->key[3] + c->mask[3]);
+ n1 ^= f(c, n2 + c->key[2] + c->mask[2]);
+ n2 ^= f(c, n1 + c->key[1] + c->mask[1]);
+ n1 ^= f(c, n2 + c->key[0] + c->mask[0]);
out[0] = (byte) (n2 & 0xff);
out[1] = (byte) ((n2 >> 8) & 0xff);
n1 = in[0] | (in[1] << 8) | (in[2] << 16) | ((word32) in[3] << 24);
n2 = in[4] | (in[5] << 8) | (in[6] << 16) | ((word32) in[7] << 24);
- n2 ^= f(c, n1 + c->k[0]);
- n1 ^= f(c, n2 + c->k[1]);
- n2 ^= f(c, n1 + c->k[2]);
- n1 ^= f(c, n2 + c->k[3]);
- n2 ^= f(c, n1 + c->k[4]);
- n1 ^= f(c, n2 + c->k[5]);
- n2 ^= f(c, n1 + c->k[6]);
- n1 ^= f(c, n2 + c->k[7]);
-
- n2 ^= f(c, n1 + c->k[7]);
- n1 ^= f(c, n2 + c->k[6]);
- n2 ^= f(c, n1 + c->k[5]);
- n1 ^= f(c, n2 + c->k[4]);
- n2 ^= f(c, n1 + c->k[3]);
- n1 ^= f(c, n2 + c->k[2]);
- n2 ^= f(c, n1 + c->k[1]);
- n1 ^= f(c, n2 + c->k[0]);
-
- n2 ^= f(c, n1 + c->k[7]);
- n1 ^= f(c, n2 + c->k[6]);
- n2 ^= f(c, n1 + c->k[5]);
- n1 ^= f(c, n2 + c->k[4]);
- n2 ^= f(c, n1 + c->k[3]);
- n1 ^= f(c, n2 + c->k[2]);
- n2 ^= f(c, n1 + c->k[1]);
- n1 ^= f(c, n2 + c->k[0]);
-
- n2 ^= f(c, n1 + c->k[7]);
- n1 ^= f(c, n2 + c->k[6]);
- n2 ^= f(c, n1 + c->k[5]);
- n1 ^= f(c, n2 + c->k[4]);
- n2 ^= f(c, n1 + c->k[3]);
- n1 ^= f(c, n2 + c->k[2]);
- n2 ^= f(c, n1 + c->k[1]);
- n1 ^= f(c, n2 + c->k[0]);
+ n2 ^= f(c, n1 + c->key[0] + c->mask[0]);
+ n1 ^= f(c, n2 + c->key[1] + c->mask[1]);
+ n2 ^= f(c, n1 + c->key[2] + c->mask[2]);
+ n1 ^= f(c, n2 + c->key[3] + c->mask[3]);
+ n2 ^= f(c, n1 + c->key[4] + c->mask[4]);
+ n1 ^= f(c, n2 + c->key[5] + c->mask[5]);
+ n2 ^= f(c, n1 + c->key[6] + c->mask[6]);
+ n1 ^= f(c, n2 + c->key[7] + c->mask[7]);
+
+ n2 ^= f(c, n1 + c->key[7] + c->mask[7]);
+ n1 ^= f(c, n2 + c->key[6] + c->mask[6]);
+ n2 ^= f(c, n1 + c->key[5] + c->mask[5]);
+ n1 ^= f(c, n2 + c->key[4] + c->mask[4]);
+ n2 ^= f(c, n1 + c->key[3] + c->mask[3]);
+ n1 ^= f(c, n2 + c->key[2] + c->mask[2]);
+ n2 ^= f(c, n1 + c->key[1] + c->mask[1]);
+ n1 ^= f(c, n2 + c->key[0] + c->mask[0]);
+
+ n2 ^= f(c, n1 + c->key[7] + c->mask[7]);
+ n1 ^= f(c, n2 + c->key[6] + c->mask[6]);
+ n2 ^= f(c, n1 + c->key[5] + c->mask[5]);
+ n1 ^= f(c, n2 + c->key[4] + c->mask[4]);
+ n2 ^= f(c, n1 + c->key[3] + c->mask[3]);
+ n1 ^= f(c, n2 + c->key[2] + c->mask[2]);
+ n2 ^= f(c, n1 + c->key[1] + c->mask[1]);
+ n1 ^= f(c, n2 + c->key[0] + c->mask[0]);
+
+ n2 ^= f(c, n1 + c->key[7] + c->mask[7]);
+ n1 ^= f(c, n2 + c->key[6] + c->mask[6]);
+ n2 ^= f(c, n1 + c->key[5] + c->mask[5]);
+ n1 ^= f(c, n2 + c->key[4] + c->mask[4]);
+ n2 ^= f(c, n1 + c->key[3] + c->mask[3]);
+ n1 ^= f(c, n2 + c->key[2] + c->mask[2]);
+ n2 ^= f(c, n1 + c->key[1] + c->mask[1]);
+ n1 ^= f(c, n2 + c->key[0] + c->mask[0]);
out[0] = (byte) (n2 & 0xff);
out[1] = (byte) ((n2 >> 8) & 0xff);
out[7] = (byte) (n1 >> 24);
}
+
/* Encrypts several blocks in ECB mode */
void gost_enc(gost_ctx * c, const byte * clear, byte * cipher, int blocks)
{
void gost_key(gost_ctx * c, const byte * k)
{
int i, j;
- for (i = 0, j = 0; i < 8; i++, j += 4) {
- c->k[i] =
- k[j] | (k[j + 1] << 8) | (k[j + 2] << 16) | ((word32) k[j + 3] <<
- 24);
+ RAND_bytes((unsigned char *)c->mask, sizeof(c->mask));
+ for (i = 0, j = 0; i < 8; ++i, j += 4) {
+ c->key[i] =
+ (k[j] | (k[j + 1] << 8) | (k[j + 2] << 16) | ((word32) k[j + 3] <<
+ 24)) - c->mask[i];
}
}
void magma_key(gost_ctx * c, const byte * k)
{
int i, j;
- for (i = 0, j = 0; i < 8; i++, j += 4) {
- c->k[i] =
- k[j + 3] | (k[j + 2] << 8) | (k[j + 1] << 16) | ((word32) k[j] <<
- 24);
+ RAND_bytes((unsigned char *)c->mask, sizeof(c->mask));
+ for (i = 0, j = 0; i < 8; ++i, j += 4) {
+ c->key[i] =
+ (k[j + 3] | (k[j + 2] << 8) | (k[j + 1] << 16) | ((word32) k[j] <<
+ 24)) - c->mask[i];
}
}
{
int i, j;
for (i = 0, j = 0; i < 8; i++, j += 4) {
- k[j] = (byte) (c->k[i] & 0xFF);
- k[j + 1] = (byte) ((c->k[i] >> 8) & 0xFF);
- k[j + 2] = (byte) ((c->k[i] >> 16) & 0xFF);
- k[j + 3] = (byte) ((c->k[i] >> 24) & 0xFF);
+ k[j] = (byte)((c->key[i] + c->mask[i]) & 0xFF);
+ k[j+1] = (byte)(((c->key[i] + c->mask[i]) >> 8 )& 0xFF);
+ k[j+2] = (byte)(((c->key[i] + c->mask[i]) >> 16) & 0xFF);
+ k[j+3] = (byte)(((c->key[i] + c->mask[i]) >> 24) & 0xFF);
}
}
{
int i, j;
for (i = 0, j = 0; i < 8; i++, j += 4) {
- k[j + 3] = (byte) (c->k[i] & 0xFF);
- k[j + 2] = (byte) ((c->k[i] >> 8) & 0xFF);
- k[j + 1] = (byte) ((c->k[i] >> 16) & 0xFF);
- k[j + 0] = (byte) ((c->k[i] >> 24) & 0xFF);
+ k[j + 3] = (byte) ((c->key[i] + c->mask[i]) & 0xFF);
+ k[j + 2] = (byte) (((c->key[i] + c->mask[i]) >> 8) & 0xFF);
+ k[j + 1] = (byte) (((c->key[i] + c->mask[i]) >> 16) & 0xFF);
+ k[j + 0] = (byte) (((c->key[i] + c->mask[i]) >> 24) & 0xFF);
}
}
/* Cleans up key from context */
void gost_destroy(gost_ctx * c)
{
- OPENSSL_cleanse(c->k, sizeof(c->k));
+ OPENSSL_cleanse(c->key, sizeof(c->key));
+ OPENSSL_cleanse(c->mask, sizeof(c->mask));
}
/*
buffer[7] << 24);
/* Instead of swapping halves, swap names each round */
- n2 ^= f(c, n1 + c->k[0]);
- n1 ^= f(c, n2 + c->k[1]);
- n2 ^= f(c, n1 + c->k[2]);
- n1 ^= f(c, n2 + c->k[3]);
- n2 ^= f(c, n1 + c->k[4]);
- n1 ^= f(c, n2 + c->k[5]);
- n2 ^= f(c, n1 + c->k[6]);
- n1 ^= f(c, n2 + c->k[7]);
-
- n2 ^= f(c, n1 + c->k[0]);
- n1 ^= f(c, n2 + c->k[1]);
- n2 ^= f(c, n1 + c->k[2]);
- n1 ^= f(c, n2 + c->k[3]);
- n2 ^= f(c, n1 + c->k[4]);
- n1 ^= f(c, n2 + c->k[5]);
- n2 ^= f(c, n1 + c->k[6]);
- n1 ^= f(c, n2 + c->k[7]);
+ n2 ^= f(c, n1 + c->key[0] + c->mask[0]);
+ n1 ^= f(c, n2 + c->key[1] + c->mask[1]);
+ n2 ^= f(c, n1 + c->key[2] + c->mask[2]);
+ n1 ^= f(c, n2 + c->key[3] + c->mask[3]);
+ n2 ^= f(c, n1 + c->key[4] + c->mask[4]);
+ n1 ^= f(c, n2 + c->key[5] + c->mask[5]);
+ n2 ^= f(c, n1 + c->key[6] + c->mask[6]);
+ n1 ^= f(c, n2 + c->key[7] + c->mask[7]);
+
+ n2 ^= f(c, n1 + c->key[0] + c->mask[0]);
+ n1 ^= f(c, n2 + c->key[1] + c->mask[1]);
+ n2 ^= f(c, n1 + c->key[2] + c->mask[2]);
+ n1 ^= f(c, n2 + c->key[3] + c->mask[3]);
+ n2 ^= f(c, n1 + c->key[4] + c->mask[4]);
+ n1 ^= f(c, n2 + c->key[5] + c->mask[5]);
+ n2 ^= f(c, n1 + c->key[6] + c->mask[6]);
+ n1 ^= f(c, n2 + c->key[7] + c->mask[7]);
buffer[0] = (byte) (n1 & 0xff);
buffer[1] = (byte) ((n1 >> 8) & 0xff);