|
| 1 | +#include "gost_3411_2012_calc.h" |
| 2 | + |
| 3 | +//#define DEBUG_MODE |
| 4 | + |
| 5 | +#ifdef DEBUG_MODE |
| 6 | +static void |
| 7 | +GOSTHashPrintDebug(uint8_t *state) |
| 8 | +{ |
| 9 | + int i; |
| 10 | + for (i = 0; i < 64; i++) |
| 11 | + printf("%02x", state[i]); |
| 12 | + printf("\n"); |
| 13 | +} |
| 14 | +#endif |
| 15 | + |
| 16 | +void |
| 17 | +GOSTHashInit(TGOSTHashContext *CTX, uint16_t hash_size) |
| 18 | +{ |
| 19 | + memset(CTX, 0x00, sizeof(TGOSTHashContext)); |
| 20 | + if (hash_size == 256) |
| 21 | + memset(CTX->h, 0x01, BLOCK_SIZE); |
| 22 | + else |
| 23 | + memset(CTX->h, 0x00, BLOCK_SIZE); |
| 24 | + CTX->hash_size = hash_size; |
| 25 | + CTX->v_512[1] = 0x02; |
| 26 | +} |
| 27 | + |
| 28 | +static void |
| 29 | +GOSTHashX(const uint8_t *a, const uint8_t *b, uint8_t *c) |
| 30 | +{ |
| 31 | + int i; |
| 32 | + for (i = 0; i < 64; i++) |
| 33 | + c[i] = a[i]^b[i]; |
| 34 | +} |
| 35 | + |
| 36 | +static void |
| 37 | +GOSTHashAdd512(const uint8_t *a, const uint8_t *b, uint8_t *c) |
| 38 | +{ |
| 39 | + int i; |
| 40 | + int internal = 0; |
| 41 | + for (i = 0; i < 64; i++) |
| 42 | + { |
| 43 | + internal = a[i] + b[i] + (internal >> 8); |
| 44 | + c[i] = internal & 0xff; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +static void |
| 49 | +GOSTHashP(uint8_t *state) |
| 50 | +{ |
| 51 | + int i; |
| 52 | + vect internal; |
| 53 | + for (i = 63; i >= 0; i--) |
| 54 | + internal[i] = state[Tau[i]]; |
| 55 | + memcpy(state, internal, BLOCK_SIZE); |
| 56 | +} |
| 57 | + |
| 58 | +static void |
| 59 | +GOSTHashS(uint8_t *state) |
| 60 | +{ |
| 61 | + int i; |
| 62 | + vect internal; |
| 63 | + for (i = 63; i >= 0; i--) |
| 64 | + internal[i] = Pi[state[i]]; |
| 65 | + memcpy(state, internal, BLOCK_SIZE); |
| 66 | +} |
| 67 | + |
| 68 | +static void |
| 69 | +GOSTHashL(uint8_t *state) |
| 70 | +{ |
| 71 | + uint64_t* internal_in = (uint64_t*)state; |
| 72 | + uint64_t internal_out[8]; |
| 73 | + memset(internal_out, 0x00, BLOCK_SIZE); |
| 74 | + int i, j; |
| 75 | + for (i = 7; i >= 0; i--) |
| 76 | + { |
| 77 | + for (j = 63; j >= 0; j--) |
| 78 | + if ((internal_in[i] >> j) & 1) |
| 79 | + internal_out[i] ^= A[63 - j]; |
| 80 | + } |
| 81 | + memcpy(state, internal_out, 64); |
| 82 | +} |
| 83 | + |
| 84 | +static void |
| 85 | +GOSTHashGetKey(uint8_t *K, int i) |
| 86 | +{ |
| 87 | + GOSTHashX(K, C[i], K); |
| 88 | + GOSTHashS(K); |
| 89 | + GOSTHashP(K); |
| 90 | + GOSTHashL(K); |
| 91 | +} |
| 92 | + |
| 93 | +static void |
| 94 | +GOSTHashE(uint8_t *K, const uint8_t *m, uint8_t *state) |
| 95 | +{ |
| 96 | + int i; |
| 97 | + memcpy(K, K, BLOCK_SIZE); |
| 98 | + GOSTHashX(m, K, state); |
| 99 | + for(i = 0; i < 12; i++) |
| 100 | + { |
| 101 | + GOSTHashS(state); |
| 102 | + GOSTHashP(state); |
| 103 | + GOSTHashL(state); |
| 104 | + GOSTHashGetKey(K, i); |
| 105 | + GOSTHashX(state, K, state); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +static void |
| 110 | +GOSTHashG( uint8_t *h, uint8_t *N, const uint8_t *m) |
| 111 | +{ |
| 112 | + vect K, internal; |
| 113 | + GOSTHashX(N, h, K); |
| 114 | + |
| 115 | + GOSTHashS(K); |
| 116 | + GOSTHashP(K); |
| 117 | + GOSTHashL(K); |
| 118 | + |
| 119 | + GOSTHashE(K, m, internal); |
| 120 | + |
| 121 | + GOSTHashX(internal, h, internal); |
| 122 | + GOSTHashX(internal, m, h); |
| 123 | +} |
| 124 | + |
| 125 | +static void |
| 126 | +GOSTHashPadding(TGOSTHashContext *CTX) |
| 127 | +{ |
| 128 | + vect internal; |
| 129 | + |
| 130 | + if (CTX->buf_size < BLOCK_SIZE) |
| 131 | + { |
| 132 | + memset(internal, 0x00, BLOCK_SIZE); |
| 133 | + memcpy(internal, CTX->buffer, CTX->buf_size); |
| 134 | + internal[CTX->buf_size] = 0x01; |
| 135 | + memcpy(CTX->buffer, internal, BLOCK_SIZE); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +static void |
| 140 | +GOSTHashStage_2(TGOSTHashContext *CTX, const uint8_t *data) |
| 141 | +{ |
| 142 | + GOSTHashG(CTX->h, CTX->N, data); |
| 143 | + GOSTHashAdd512(CTX->N, CTX->v_512, CTX->N); |
| 144 | + GOSTHashAdd512(CTX->Sigma, data, CTX->Sigma); |
| 145 | + |
| 146 | +#ifdef DEBUG_MODE |
| 147 | + printf("Stage 2\n"); |
| 148 | + printf("G:\n"); |
| 149 | + GOSTHashPrintDebug(CTX->h); |
| 150 | + printf("N:\n"); |
| 151 | + GOSTHashPrintDebug(CTX->N); |
| 152 | + printf("Sigma:\n"); |
| 153 | + GOSTHashPrintDebug(CTX->Sigma); |
| 154 | +#endif |
| 155 | +} |
| 156 | + |
| 157 | +static void |
| 158 | +GOSTHashStage_3(TGOSTHashContext *CTX) |
| 159 | +{ |
| 160 | + vect internal; |
| 161 | + memset(internal, 0x00, BLOCK_SIZE); |
| 162 | + internal[1] = ((CTX->buf_size * 8) >> 8) & 0xff; |
| 163 | + internal[0] = (CTX->buf_size * 8) & 0xff; |
| 164 | + |
| 165 | + GOSTHashPadding(CTX); |
| 166 | + |
| 167 | + GOSTHashG(CTX->h, CTX->N, (const uint8_t*)&(CTX->buffer)); |
| 168 | + |
| 169 | + GOSTHashAdd512(CTX->N, internal, CTX->N); |
| 170 | + GOSTHashAdd512(CTX->Sigma, CTX->buffer, CTX->Sigma); |
| 171 | + |
| 172 | + GOSTHashG(CTX->h, CTX->v_0, (const uint8_t*)&(CTX->N)); |
| 173 | + GOSTHashG(CTX->h, CTX->v_0, (const uint8_t*)&(CTX->Sigma)); |
| 174 | + |
| 175 | +#ifdef DEBUG_MODE |
| 176 | + printf("Stage 3\n"); |
| 177 | + printf("G:\n"); |
| 178 | + GOSTHashPrintDebug(CTX->h); |
| 179 | + printf("N:\n"); |
| 180 | + GOSTHashPrintDebug(CTX->N); |
| 181 | + printf("Sigma:\n"); |
| 182 | + GOSTHashPrintDebug(CTX->Sigma); |
| 183 | +#endif |
| 184 | + |
| 185 | + memcpy(CTX->hash, CTX->h, BLOCK_SIZE); |
| 186 | +} |
| 187 | + |
| 188 | +void |
| 189 | +GOSTHashUpdate(TGOSTHashContext *CTX, const uint8_t *data, size_t len) |
| 190 | +{ |
| 191 | + size_t chk_size; |
| 192 | + |
| 193 | + while((len > 63) && (CTX->buf_size) == 0) |
| 194 | + { |
| 195 | + GOSTHashStage_2(CTX, data); |
| 196 | + data += 64; |
| 197 | + len -= 64; |
| 198 | + } |
| 199 | + while (len) |
| 200 | + { |
| 201 | + chk_size = 64 - CTX->buf_size; |
| 202 | + if (chk_size > len) |
| 203 | + chk_size = len; |
| 204 | + memcpy(&CTX->buffer[CTX->buf_size], data, chk_size); |
| 205 | + CTX->buf_size += chk_size; |
| 206 | + len -= chk_size; |
| 207 | + data += chk_size; |
| 208 | + if (CTX->buf_size == 64) |
| 209 | + { |
| 210 | + GOSTHashStage_2(CTX, CTX->buffer); |
| 211 | + CTX->buf_size = 0; |
| 212 | + } |
| 213 | + } |
| 214 | +} |
| 215 | +void |
| 216 | +GOSTHashFinal(TGOSTHashContext *CTX) |
| 217 | +{ |
| 218 | + GOSTHashStage_3(CTX); |
| 219 | + CTX->buf_size = 0; |
| 220 | +} |
0 commit comments