1238384Sjkim/********************************************************************** 2238384Sjkim * gosthash.c * 3238384Sjkim * Copyright (c) 2005-2006 Cryptocom LTD * 4238384Sjkim * This file is distributed under the same license as OpenSSL * 5238384Sjkim * * 6238384Sjkim * Implementation of GOST R 34.11-94 hash function * 7238384Sjkim * uses on gost89.c and gost89.h Doesn't need OpenSSL * 8238384Sjkim **********************************************************************/ 9238384Sjkim#include <string.h> 10238384Sjkim 11238384Sjkim#include "gost89.h" 12238384Sjkim#include "gosthash.h" 13238384Sjkim 14280297Sjkim/* 15280297Sjkim * Use OPENSSL_malloc for memory allocation if compiled with 16238384Sjkim * -DOPENSSL_BUILD, and libc malloc otherwise 17238384Sjkim */ 18238384Sjkim#ifndef MYALLOC 19238384Sjkim# ifdef OPENSSL_BUILD 20238384Sjkim# include <openssl/crypto.h> 21238384Sjkim# define MYALLOC(size) OPENSSL_malloc(size) 22238384Sjkim# define MYFREE(ptr) OPENSSL_free(ptr) 23238384Sjkim# else 24238384Sjkim# define MYALLOC(size) malloc(size) 25238384Sjkim# define MYFREE(ptr) free(ptr) 26238384Sjkim# endif 27238384Sjkim#endif 28280297Sjkim/* 29280297Sjkim * Following functions are various bit meshing routines used in GOST R 30280297Sjkim * 34.11-94 algorithms 31280297Sjkim */ 32280297Sjkimstatic void swap_bytes(byte * w, byte * k) 33280297Sjkim{ 34280297Sjkim int i, j; 35280297Sjkim for (i = 0; i < 4; i++) 36280297Sjkim for (j = 0; j < 8; j++) 37280297Sjkim k[i + 4 * j] = w[8 * i + j]; 38238384Sjkim 39280297Sjkim} 40238384Sjkim 41238384Sjkim/* was A_A */ 42280297Sjkimstatic void circle_xor8(const byte * w, byte * k) 43280297Sjkim{ 44280297Sjkim byte buf[8]; 45280297Sjkim int i; 46280297Sjkim memcpy(buf, w, 8); 47280297Sjkim memmove(k, w + 8, 24); 48280297Sjkim for (i = 0; i < 8; i++) 49280297Sjkim k[i + 24] = buf[i] ^ k[i]; 50280297Sjkim} 51238384Sjkim 52238384Sjkim/* was R_R */ 53280297Sjkimstatic void transform_3(byte * data) 54280297Sjkim{ 55280297Sjkim unsigned short int acc; 56280297Sjkim acc = (data[0] ^ data[2] ^ data[4] ^ data[6] ^ data[24] ^ data[30]) | 57280297Sjkim ((data[1] ^ data[3] ^ data[5] ^ data[7] ^ data[25] ^ data[31]) << 8); 58280297Sjkim memmove(data, data + 2, 30); 59280297Sjkim data[30] = acc & 0xff; 60280297Sjkim data[31] = acc >> 8; 61280297Sjkim} 62238384Sjkim 63238384Sjkim/* Adds blocks of N bytes modulo 2**(8*n). Returns carry*/ 64280297Sjkimstatic int add_blocks(int n, byte * left, const byte * right) 65280297Sjkim{ 66280297Sjkim int i; 67280297Sjkim int carry = 0; 68280297Sjkim int sum; 69280297Sjkim for (i = 0; i < n; i++) { 70280297Sjkim sum = (int)left[i] + (int)right[i] + carry; 71280297Sjkim left[i] = sum & 0xff; 72280297Sjkim carry = sum >> 8; 73280297Sjkim } 74280297Sjkim return carry; 75280297Sjkim} 76238384Sjkim 77238384Sjkim/* Xor two sequences of bytes */ 78280297Sjkimstatic void xor_blocks(byte * result, const byte * a, const byte * b, 79280297Sjkim size_t len) 80280297Sjkim{ 81280297Sjkim size_t i; 82280297Sjkim for (i = 0; i < len; i++) 83280297Sjkim result[i] = a[i] ^ b[i]; 84280297Sjkim} 85238384Sjkim 86280297Sjkim/* 87280297Sjkim * Calculate H(i+1) = Hash(Hi,Mi) 88280297Sjkim * Where H and M are 32 bytes long 89238384Sjkim */ 90280297Sjkimstatic int hash_step(gost_ctx * c, byte * H, const byte * M) 91280297Sjkim{ 92280297Sjkim byte U[32], W[32], V[32], S[32], Key[32]; 93280297Sjkim int i; 94280297Sjkim /* Compute first key */ 95280297Sjkim xor_blocks(W, H, M, 32); 96280297Sjkim swap_bytes(W, Key); 97280297Sjkim /* Encrypt first 8 bytes of H with first key */ 98280297Sjkim gost_enc_with_key(c, Key, H, S); 99280297Sjkim /* Compute second key */ 100280297Sjkim circle_xor8(H, U); 101280297Sjkim circle_xor8(M, V); 102280297Sjkim circle_xor8(V, V); 103280297Sjkim xor_blocks(W, U, V, 32); 104280297Sjkim swap_bytes(W, Key); 105280297Sjkim /* encrypt second 8 bytes of H with second key */ 106280297Sjkim gost_enc_with_key(c, Key, H + 8, S + 8); 107280297Sjkim /* compute third key */ 108280297Sjkim circle_xor8(U, U); 109280297Sjkim U[31] = ~U[31]; 110280297Sjkim U[29] = ~U[29]; 111280297Sjkim U[28] = ~U[28]; 112280297Sjkim U[24] = ~U[24]; 113280297Sjkim U[23] = ~U[23]; 114280297Sjkim U[20] = ~U[20]; 115280297Sjkim U[18] = ~U[18]; 116280297Sjkim U[17] = ~U[17]; 117280297Sjkim U[14] = ~U[14]; 118280297Sjkim U[12] = ~U[12]; 119280297Sjkim U[10] = ~U[10]; 120280297Sjkim U[8] = ~U[8]; 121280297Sjkim U[7] = ~U[7]; 122280297Sjkim U[5] = ~U[5]; 123280297Sjkim U[3] = ~U[3]; 124280297Sjkim U[1] = ~U[1]; 125280297Sjkim circle_xor8(V, V); 126280297Sjkim circle_xor8(V, V); 127280297Sjkim xor_blocks(W, U, V, 32); 128280297Sjkim swap_bytes(W, Key); 129280297Sjkim /* encrypt third 8 bytes of H with third key */ 130280297Sjkim gost_enc_with_key(c, Key, H + 16, S + 16); 131280297Sjkim /* Compute fourth key */ 132280297Sjkim circle_xor8(U, U); 133280297Sjkim circle_xor8(V, V); 134280297Sjkim circle_xor8(V, V); 135280297Sjkim xor_blocks(W, U, V, 32); 136280297Sjkim swap_bytes(W, Key); 137280297Sjkim /* Encrypt last 8 bytes with fourth key */ 138280297Sjkim gost_enc_with_key(c, Key, H + 24, S + 24); 139280297Sjkim for (i = 0; i < 12; i++) 140280297Sjkim transform_3(S); 141280297Sjkim xor_blocks(S, S, M, 32); 142280297Sjkim transform_3(S); 143280297Sjkim xor_blocks(S, S, H, 32); 144280297Sjkim for (i = 0; i < 61; i++) 145280297Sjkim transform_3(S); 146280297Sjkim memcpy(H, S, 32); 147280297Sjkim return 1; 148280297Sjkim} 149238384Sjkim 150280297Sjkim/* 151280297Sjkim * Initialize gost_hash ctx - cleans up temporary structures and set up 152280297Sjkim * substitution blocks 153238384Sjkim */ 154280297Sjkimint init_gost_hash_ctx(gost_hash_ctx * ctx, 155280297Sjkim const gost_subst_block * subst_block) 156280297Sjkim{ 157280297Sjkim memset(ctx, 0, sizeof(gost_hash_ctx)); 158280297Sjkim ctx->cipher_ctx = (gost_ctx *) MYALLOC(sizeof(gost_ctx)); 159280297Sjkim if (!ctx->cipher_ctx) { 160280297Sjkim return 0; 161280297Sjkim } 162280297Sjkim gost_init(ctx->cipher_ctx, subst_block); 163280297Sjkim return 1; 164280297Sjkim} 165238384Sjkim 166238384Sjkim/* 167238384Sjkim * Free cipher CTX if it is dynamically allocated. Do not use 168238384Sjkim * if cipher ctx is statically allocated as in OpenSSL implementation of 169238384Sjkim * GOST hash algroritm 170238384Sjkim * 171280297Sjkim */ 172280297Sjkimvoid done_gost_hash_ctx(gost_hash_ctx * ctx) 173280297Sjkim{ 174280297Sjkim /* 175280297Sjkim * No need to use gost_destroy, because cipher keys are not really secret 176280297Sjkim * when hashing 177280297Sjkim */ 178280297Sjkim MYFREE(ctx->cipher_ctx); 179280297Sjkim} 180238384Sjkim 181238384Sjkim/* 182238384Sjkim * reset state of hash context to begin hashing new message 183238384Sjkim */ 184280297Sjkimint start_hash(gost_hash_ctx * ctx) 185280297Sjkim{ 186280297Sjkim if (!ctx->cipher_ctx) 187280297Sjkim return 0; 188280297Sjkim memset(&(ctx->H), 0, 32); 189280297Sjkim memset(&(ctx->S), 0, 32); 190280297Sjkim ctx->len = 0L; 191280297Sjkim ctx->left = 0; 192280297Sjkim return 1; 193280297Sjkim} 194238384Sjkim 195238384Sjkim/* 196238384Sjkim * Hash block of arbitrary length 197238384Sjkim * 198238384Sjkim * 199238384Sjkim */ 200280297Sjkimint hash_block(gost_hash_ctx * ctx, const byte * block, size_t length) 201280297Sjkim{ 202280297Sjkim if (ctx->left) { 203280297Sjkim /* 204280297Sjkim * There are some bytes from previous step 205280297Sjkim */ 206280297Sjkim unsigned int add_bytes = 32 - ctx->left; 207280297Sjkim if (add_bytes > length) { 208280297Sjkim add_bytes = length; 209280297Sjkim } 210280297Sjkim memcpy(&(ctx->remainder[ctx->left]), block, add_bytes); 211280297Sjkim ctx->left += add_bytes; 212280297Sjkim if (ctx->left < 32) { 213280297Sjkim return 1; 214280297Sjkim } 215280297Sjkim block += add_bytes; 216280297Sjkim length -= add_bytes; 217280297Sjkim hash_step(ctx->cipher_ctx, ctx->H, ctx->remainder); 218280297Sjkim add_blocks(32, ctx->S, ctx->remainder); 219280297Sjkim ctx->len += 32; 220280297Sjkim ctx->left = 0; 221280297Sjkim } 222280297Sjkim while (length >= 32) { 223280297Sjkim hash_step(ctx->cipher_ctx, ctx->H, block); 224238384Sjkim 225280297Sjkim add_blocks(32, ctx->S, block); 226280297Sjkim ctx->len += 32; 227280297Sjkim block += 32; 228280297Sjkim length -= 32; 229280297Sjkim } 230280297Sjkim if (length) { 231280297Sjkim memcpy(ctx->remainder, block, ctx->left = length); 232280297Sjkim } 233280297Sjkim return 1; 234280297Sjkim} 235280297Sjkim 236238384Sjkim/* 237238384Sjkim * Compute hash value from current state of ctx 238238384Sjkim * state of hash ctx becomes invalid and cannot be used for further 239238384Sjkim * hashing. 240280297Sjkim */ 241280297Sjkimint finish_hash(gost_hash_ctx * ctx, byte * hashval) 242280297Sjkim{ 243280297Sjkim byte buf[32]; 244280297Sjkim byte H[32]; 245280297Sjkim byte S[32]; 246280297Sjkim ghosthash_len fin_len = ctx->len; 247280297Sjkim byte *bptr; 248280297Sjkim memcpy(H, ctx->H, 32); 249280297Sjkim memcpy(S, ctx->S, 32); 250280297Sjkim if (ctx->left) { 251280297Sjkim memset(buf, 0, 32); 252280297Sjkim memcpy(buf, ctx->remainder, ctx->left); 253280297Sjkim hash_step(ctx->cipher_ctx, H, buf); 254280297Sjkim add_blocks(32, S, buf); 255280297Sjkim fin_len += ctx->left; 256280297Sjkim } 257280297Sjkim memset(buf, 0, 32); 258280297Sjkim bptr = buf; 259280297Sjkim fin_len <<= 3; /* Hash length in BITS!! */ 260280297Sjkim while (fin_len > 0) { 261280297Sjkim *(bptr++) = (byte) (fin_len & 0xFF); 262280297Sjkim fin_len >>= 8; 263280297Sjkim }; 264280297Sjkim hash_step(ctx->cipher_ctx, H, buf); 265280297Sjkim hash_step(ctx->cipher_ctx, H, S); 266280297Sjkim memcpy(hashval, H, 32); 267280297Sjkim return 1; 268280297Sjkim} 269