X-Git-Url: http://wagner.pp.ru/gitweb/?a=blobdiff_plain;f=gost89.c;h=670dfb55f5da84e9fe683edbda09e5a625e5b179;hb=a430c355a1d2eff5df6c96c7b6f928a609c2dd73;hp=4ff4ddd7a56531bf65d24dd5d3df607461391906;hpb=c98ba9d03213d0c63d6874539d59f7b55fbc3fae;p=openssl-gost%2Fengine.git diff --git a/gost89.c b/gost89.c index 4ff4ddd..670dfb5 100644 --- a/gost89.c +++ b/gost89.c @@ -208,6 +208,33 @@ gost_subst_block Gost28147_CryptoProParamSetD = { 0x8, 0x3} }; +/* 1.2.643.7.1.2.5.1.1 */ +gost_subst_block Gost28147_TC26ParamSetZ = { + {0x1, 0x7, 0xe, 0xd, 0x0, 0x5, 0x8, 0x3, 0x4, 0xf, 0xa, 0x6, 0x9, 0xc, + 0xb, 0x2} + , + {0x8, 0xe, 0x2, 0x5, 0x6, 0x9, 0x1, 0xc, 0xf, 0x4, 0xb, 0x0, 0xd, 0xa, + 0x3, 0x7} + , + {0x5, 0xd, 0xf, 0x6, 0x9, 0x2, 0xc, 0xa, 0xb, 0x7, 0x8, 0x1, 0x4, 0x3, + 0xe, 0x0} + , + {0x7, 0xf, 0x5, 0xa, 0x8, 0x1, 0x6, 0xd, 0x0, 0x9, 0x3, 0xe, 0xb, 0x4, + 0x2, 0xc} + , + {0xc, 0x8, 0x2, 0x1, 0xd, 0x4, 0xf, 0x6, 0x7, 0x0, 0xa, 0x5, 0x3, 0xe, + 0x9, 0xb} + , + {0xb, 0x3, 0x5, 0x8, 0x2, 0xf, 0xa, 0xd, 0xe, 0x1, 0x7, 0x4, 0xc, 0x9, + 0x6, 0x0} + , + {0x6, 0x8, 0x2, 0x3, 0x9, 0xa, 0x5, 0xc, 0x1, 0xe, 0x4, 0x7, 0xb, 0xd, + 0x0, 0xf} + , + {0xc, 0x4, 0x6, 0x2, 0xa, 0x5, 0xb, 0x9, 0xe, 0x8, 0xd, 0x7, 0x0, 0x3, + 0xf, 0x1} +}; + const byte CryptoProKeyMeshingKey[] = { 0x69, 0x00, 0x72, 0x22, 0x64, 0xC9, 0x04, 0x23, 0x8D, 0x3A, 0xDB, 0x96, 0x46, 0xE9, 0x2A, 0xC4, @@ -215,6 +242,13 @@ const byte CryptoProKeyMeshingKey[] = { 0xC0, 0x86, 0xDC, 0xC2, 0xEF, 0x4C, 0xA9, 0x2B }; +const byte ACPKM_D_const[] = { + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, + 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, +}; + /* Initialization of gost_ctx subst blocks*/ static void kboxinit(gost_ctx * c, const gost_subst_block * b) { @@ -411,7 +445,7 @@ void gost_enc_with_key(gost_ctx * c, byte * key, byte * inblock, gostcrypt(c, inblock, outblock); } -/* Set 256 bit key into context */ +/* Set 256 bit gost89 key into context */ void gost_key(gost_ctx * c, const byte * k) { int i, j; @@ -422,7 +456,18 @@ void gost_key(gost_ctx * c, const byte * k) } } -/* Retrieve 256-bit key from context */ +/* Set 256 bit Magma key into context */ +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); + } +} + +/* Retrieve 256-bit gost89 key from context */ void gost_get_key(gost_ctx * c, byte * k) { int i, j; @@ -434,6 +479,18 @@ void gost_get_key(gost_ctx * c, byte * k) } } +/* Retrieve 256-bit magma key from context */ +void magma_get_key(gost_ctx * c, byte * k) +{ + 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); + } +} + /* Initalize context. Provides default value for subst_block */ void gost_init(gost_ctx * c, const gost_subst_block * b) { @@ -574,3 +631,83 @@ void cryptopro_key_meshing(gost_ctx * ctx, unsigned char *iv) gostcrypt(ctx, iv, newiv); memcpy(iv, newiv, 8); } + +void acpkm_magma_key_meshing(gost_ctx * ctx) +{ + unsigned char newkey[32]; + int i, j; + unsigned char buf[8], keybuf[8]; + + for (i = 0; i < 4; i++) { + 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); + } + /* set new key */ + gost_key(ctx, newkey); +} + +#ifdef ENABLE_UNIT_TESTS +# include +# include + +static void hexdump(FILE *f, const char *title, const unsigned char *s, int l) +{ + int n = 0; + + fprintf(f, "%s", title); + for (; n < l; ++n) { + if ((n % 16) == 0) + fprintf(f, "\n%04x", n); + fprintf(f, " %02x", s[n]); + } + fprintf(f, "\n"); +} + +int main(void) +{ + const unsigned char initial_key[] = { + 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, + 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, + 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, + 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF + }; + + const unsigned char meshed_key[] = { + 0x86, 0x3E, 0xA0, 0x17, 0x84, 0x2C, 0x3D, 0x37, + 0x2B, 0x18, 0xA8, 0x5A, 0x28, 0xE2, 0x31, 0x7D, + 0x74, 0xBE, 0xFC, 0x10, 0x77, 0x20, 0xDE, 0x0C, + 0x9E, 0x8A, 0xB9, 0x74, 0xAB, 0xD0, 0x0C, 0xA0, + }; + + unsigned char buf[32]; + + gost_ctx ctx; + kboxinit(&ctx, &Gost28147_TC26ParamSetZ); + magma_key(&ctx, initial_key); + magma_get_key(&ctx, buf); + + hexdump(stdout, "Initial key", buf, 32); + + acpkm_magma_key_meshing(&ctx); + magma_get_key(&ctx, buf); + hexdump(stdout, "Meshed key - K2", buf, 32); + + if (memcmp(meshed_key, buf, 32)) { + fprintf(stderr, "Magma meshing failed"); + } + + acpkm_magma_key_meshing(&ctx); + magma_get_key(&ctx, buf); + hexdump(stdout, "Meshed key - K3", buf, 32); + + acpkm_magma_key_meshing(&ctx); + magma_get_key(&ctx, buf); + hexdump(stdout, "Meshed key - K4", buf, 32); + +} + +#endif