示例#1
0
/*
 *      Calculate H(i+1) = Hash(Hi,Mi)
 *      Where H and M are 32 bytes long
 */
static int hash_step(gost_ctx * c, byte * H, const byte * M)
{
    byte U[32], W[32], V[32], S[32], Key[32];
    int i;
    /* Compute first key */
    xor_blocks(W, H, M, 32);
    swap_bytes(W, Key);
    /* Encrypt first 8 bytes of H with first key */
    gost_enc_with_key(c, Key, H, S);
    /* Compute second key */
    circle_xor8(H, U);
    circle_xor8(M, V);
    circle_xor8(V, V);
    xor_blocks(W, U, V, 32);
    swap_bytes(W, Key);
    /* encrypt second 8 bytes of H with second key */
    gost_enc_with_key(c, Key, H + 8, S + 8);
    /* compute third key */
    circle_xor8(U, U);
    U[31] = ~U[31];
    U[29] = ~U[29];
    U[28] = ~U[28];
    U[24] = ~U[24];
    U[23] = ~U[23];
    U[20] = ~U[20];
    U[18] = ~U[18];
    U[17] = ~U[17];
    U[14] = ~U[14];
    U[12] = ~U[12];
    U[10] = ~U[10];
    U[8] = ~U[8];
    U[7] = ~U[7];
    U[5] = ~U[5];
    U[3] = ~U[3];
    U[1] = ~U[1];
    circle_xor8(V, V);
    circle_xor8(V, V);
    xor_blocks(W, U, V, 32);
    swap_bytes(W, Key);
    /* encrypt third 8 bytes of H with third key */
    gost_enc_with_key(c, Key, H + 16, S + 16);
    /* Compute fourth key */
    circle_xor8(U, U);
    circle_xor8(V, V);
    circle_xor8(V, V);
    xor_blocks(W, U, V, 32);
    swap_bytes(W, Key);
    /* Encrypt last 8 bytes with fourth key */
    gost_enc_with_key(c, Key, H + 24, S + 24);
    for (i = 0; i < 12; i++)
        transform_3(S);
    xor_blocks(S, S, M, 32);
    transform_3(S);
    xor_blocks(S, S, H, 32);
    for (i = 0; i < 61; i++)
        transform_3(S);
    memcpy(H, S, 32);
    return 1;
}
示例#2
0
/* 
 * 	Calculate H(i+1) = Hash(Hi,Mi) 
 * 	Where H and M are 32 bytes long
 */
static int
hash_step(GOSTR341194_CTX *c, unsigned char *H, const unsigned char *M)
{
	unsigned char U[32], W[32], V[32], S[32], Key[32];
	int i;

	/* Compute first key */
	xor_blocks(W, H, M, 32);
	swap_bytes(W, Key);
	/* Encrypt first 8 bytes of H with first key */
	Gost2814789_set_key(&c->cipher, Key, 256);
	Gost2814789_encrypt(H, S, &c->cipher);

	/* Compute second key */
	circle_xor8(H, U);
	circle_xor8(M, V);
	circle_xor8(V, V);
	xor_blocks(W, U, V, 32);
	swap_bytes(W, Key);
	/* encrypt second 8 bytes of H with second key */
	Gost2814789_set_key(&c->cipher, Key, 256);
	Gost2814789_encrypt(H+8, S+8, &c->cipher);

	/* compute third key */
	circle_xor8(U, U);
	U[31] = ~U[31];
	U[29] = ~U[29];
	U[28] = ~U[28];
	U[24] = ~U[24];
	U[23] = ~U[23];
	U[20] = ~U[20];
	U[18] = ~U[18];
	U[17] = ~U[17];
	U[14] = ~U[14];
	U[12] = ~U[12];
	U[10] = ~U[10];
	U[8] = ~U[8];
	U[7] = ~U[7];
	U[5] = ~U[5];
	U[3] = ~U[3];
	U[1] = ~U[1];
	circle_xor8(V, V);
	circle_xor8(V, V);
	xor_blocks(W, U, V, 32);
	swap_bytes(W, Key);
	/* encrypt third 8 bytes of H with third key */
	Gost2814789_set_key(&c->cipher, Key, 256);
	Gost2814789_encrypt(H+16, S+16, &c->cipher);

	/* Compute fourth key */
	circle_xor8(U, U);
	circle_xor8(V, V);
	circle_xor8(V, V);
	xor_blocks(W, U, V, 32);
	swap_bytes(W, Key);
	/* Encrypt last 8 bytes with fourth key */
	Gost2814789_set_key(&c->cipher, Key, 256);
	Gost2814789_encrypt(H+24, S+24, &c->cipher);

	for (i = 0; i < 12; i++)
		transform_3(S);
	xor_blocks(S, S, M, 32);
	transform_3(S);
	xor_blocks(S, S, H, 32);
	for (i = 0; i < 61; i++)
		transform_3(S);
	memcpy(H, S, 32);
	return 1;
}