X-Git-Url: http://www.wagner.pp.ru/gitweb/?p=openssl-gost%2Fengine.git;a=blobdiff_plain;f=gost89.c;h=c103e72844cda6ccb4e74b8c892069543e1a0fd5;hp=90d5515d9c7041c7305a9279afe69fe85350532d;hb=HEAD;hpb=798a847d840c42cc14e67bad882323b830aacab1 diff --git a/gost89.c b/gost89.c index 90d5515..c103e72 100644 --- a/gost89.c +++ b/gost89.c @@ -328,6 +328,60 @@ void gostcrypt(gost_ctx * c, const byte * in, byte * out) out[7] = (byte) (n1 >> 24); } +/* Low-level encryption routine - encrypts one 64 bit block*/ +void magmacrypt(gost_ctx * c, const byte * in, byte * out) +{ + register word32 n1, n2; /* As named in the GOST */ + n1 = in[7-0] | (in[7-1] << 8) | (in[7-2] << 16) | ((word32) in[7-3] << 24); + n2 = in[7-4] | (in[7-5] << 8) | (in[7-6] << 16) | ((word32) in[7-7] << 24); + /* Instead of swapping halves, swap names each round */ + + 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[7-0] = (byte) (n2 & 0xff); + out[7-1] = (byte) ((n2 >> 8) & 0xff); + out[7-2] = (byte) ((n2 >> 16) & 0xff); + out[7-3] = (byte) (n2 >> 24); + out[7-4] = (byte) (n1 & 0xff); + out[7-5] = (byte) ((n1 >> 8) & 0xff); + out[7-6] = (byte) ((n1 >> 16) & 0xff); + out[7-7] = (byte) (n1 >> 24); +} + /* Low-level decryption routine. Decrypts one 64-bit block */ void gostdecrypt(gost_ctx * c, const byte * in, byte * out) { @@ -381,6 +435,59 @@ void gostdecrypt(gost_ctx * c, const byte * in, byte * out) out[7] = (byte) (n1 >> 24); } +/* Low-level decryption routine. Decrypts one 64-bit block */ +void magmadecrypt(gost_ctx * c, const byte * in, byte * out) +{ + register word32 n1, n2; /* As named in the GOST */ + n1 = in[7-0] | (in[7-1] << 8) | (in[7-2] << 16) | ((word32) in[7-3] << 24); + n2 = in[7-4] | (in[7-5] << 8) | (in[7-6] << 16) | ((word32) in[7-7] << 24); + + 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[7-0] = (byte) (n2 & 0xff); + out[7-1] = (byte) ((n2 >> 8) & 0xff); + out[7-2] = (byte) ((n2 >> 16) & 0xff); + out[7-3] = (byte) (n2 >> 24); + out[7-4] = (byte) (n1 & 0xff); + out[7-5] = (byte) ((n1 >> 8) & 0xff); + out[7-6] = (byte) ((n1 >> 16) & 0xff); + out[7-7] = (byte) (n1 >> 24); +} + /* Encrypts several blocks in ECB mode */ void gost_enc(gost_ctx * c, const byte * clear, byte * cipher, int blocks) @@ -661,20 +768,11 @@ void cryptopro_key_meshing(gost_ctx * ctx, unsigned char *iv) void acpkm_magma_key_meshing(gost_ctx * ctx) { unsigned char newkey[32]; - int i, j; - + int i; for (i = 0; i < 4; i++) { - unsigned char buf[8], keybuf[8]; - for (j = 0; j < 8; j++) { - buf[j] = ACPKM_D_const[8 * i + 7 - j]; - } - gostcrypt(ctx, buf, keybuf); - memcpy(newkey + 8 * i, keybuf + 4, 4); - memcpy(newkey + 8 * i + 4, keybuf, 4); - OPENSSL_cleanse(keybuf, sizeof(keybuf)); - OPENSSL_cleanse(buf, sizeof(buf)); + magmacrypt(ctx, ACPKM_D_const + 8 * i, newkey + 8 * i); } /* set new key */ - gost_key(ctx, newkey); + magma_key(ctx, newkey); OPENSSL_cleanse(newkey, sizeof(newkey)); }