Coverage Report

Created: 2022-11-30 06:20

/src/openssl/engines/ccgost/gosthash.c
Line
Count
Source (jump to first uncovered line)
1
/**********************************************************************
2
 *                          gosthash.c                                *
3
 *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4
 *         This file is distributed under the same license as OpenSSL *
5
 *                                                                    *
6
 *    Implementation of GOST R 34.11-94 hash function                 *
7
 *       uses on gost89.c and gost89.h Doesn't need OpenSSL           *
8
 **********************************************************************/
9
#include <string.h>
10
11
#include "gost89.h"
12
#include "gosthash.h"
13
14
/*
15
 * Use OPENSSL_malloc for memory allocation if compiled with
16
 * -DOPENSSL_BUILD, and libc malloc otherwise
17
 */
18
#ifndef MYALLOC
19
# ifdef OPENSSL_BUILD
20
#  include <openssl/crypto.h>
21
#  define MYALLOC(size) OPENSSL_malloc(size)
22
#  define MYFREE(ptr) OPENSSL_free(ptr)
23
# else
24
0
#  define MYALLOC(size) malloc(size)
25
0
#  define MYFREE(ptr) free(ptr)
26
# endif
27
#endif
28
/*
29
 * Following functions are various bit meshing routines used in GOST R
30
 * 34.11-94 algorithms
31
 */
32
static void swap_bytes(byte * w, byte * k)
33
0
{
34
0
    int i, j;
35
0
    for (i = 0; i < 4; i++)
36
0
        for (j = 0; j < 8; j++)
37
0
            k[i + 4 * j] = w[8 * i + j];
38
39
0
}
40
41
/* was A_A */
42
static void circle_xor8(const byte * w, byte * k)
43
0
{
44
0
    byte buf[8];
45
0
    int i;
46
0
    memcpy(buf, w, 8);
47
0
    memmove(k, w + 8, 24);
48
0
    for (i = 0; i < 8; i++)
49
0
        k[i + 24] = buf[i] ^ k[i];
50
0
}
51
52
/* was R_R */
53
static void transform_3(byte * data)
54
0
{
55
0
    unsigned short int acc;
56
0
    acc = (data[0] ^ data[2] ^ data[4] ^ data[6] ^ data[24] ^ data[30]) |
57
0
        ((data[1] ^ data[3] ^ data[5] ^ data[7] ^ data[25] ^ data[31]) << 8);
58
0
    memmove(data, data + 2, 30);
59
0
    data[30] = acc & 0xff;
60
0
    data[31] = acc >> 8;
61
0
}
62
63
/* Adds blocks of N bytes modulo 2**(8*n). Returns carry*/
64
static int add_blocks(int n, byte * left, const byte * right)
65
0
{
66
0
    int i;
67
0
    int carry = 0;
68
0
    int sum;
69
0
    for (i = 0; i < n; i++) {
70
0
        sum = (int)left[i] + (int)right[i] + carry;
71
0
        left[i] = sum & 0xff;
72
0
        carry = sum >> 8;
73
0
    }
74
0
    return carry;
75
0
}
76
77
/* Xor two sequences of bytes */
78
static void xor_blocks(byte * result, const byte * a, const byte * b,
79
                       size_t len)
80
0
{
81
0
    size_t i;
82
0
    for (i = 0; i < len; i++)
83
0
        result[i] = a[i] ^ b[i];
84
0
}
85
86
/*
87
 *      Calculate H(i+1) = Hash(Hi,Mi)
88
 *      Where H and M are 32 bytes long
89
 */
90
static int hash_step(gost_ctx * c, byte * H, const byte * M)
91
0
{
92
0
    byte U[32], W[32], V[32], S[32], Key[32];
93
0
    int i;
94
    /* Compute first key */
95
0
    xor_blocks(W, H, M, 32);
96
0
    swap_bytes(W, Key);
97
    /* Encrypt first 8 bytes of H with first key */
98
0
    gost_enc_with_key(c, Key, H, S);
99
    /* Compute second key */
100
0
    circle_xor8(H, U);
101
0
    circle_xor8(M, V);
102
0
    circle_xor8(V, V);
103
0
    xor_blocks(W, U, V, 32);
104
0
    swap_bytes(W, Key);
105
    /* encrypt second 8 bytes of H with second key */
106
0
    gost_enc_with_key(c, Key, H + 8, S + 8);
107
    /* compute third key */
108
0
    circle_xor8(U, U);
109
0
    U[31] = ~U[31];
110
0
    U[29] = ~U[29];
111
0
    U[28] = ~U[28];
112
0
    U[24] = ~U[24];
113
0
    U[23] = ~U[23];
114
0
    U[20] = ~U[20];
115
0
    U[18] = ~U[18];
116
0
    U[17] = ~U[17];
117
0
    U[14] = ~U[14];
118
0
    U[12] = ~U[12];
119
0
    U[10] = ~U[10];
120
0
    U[8] = ~U[8];
121
0
    U[7] = ~U[7];
122
0
    U[5] = ~U[5];
123
0
    U[3] = ~U[3];
124
0
    U[1] = ~U[1];
125
0
    circle_xor8(V, V);
126
0
    circle_xor8(V, V);
127
0
    xor_blocks(W, U, V, 32);
128
0
    swap_bytes(W, Key);
129
    /* encrypt third 8 bytes of H with third key */
130
0
    gost_enc_with_key(c, Key, H + 16, S + 16);
131
    /* Compute fourth key */
132
0
    circle_xor8(U, U);
133
0
    circle_xor8(V, V);
134
0
    circle_xor8(V, V);
135
0
    xor_blocks(W, U, V, 32);
136
0
    swap_bytes(W, Key);
137
    /* Encrypt last 8 bytes with fourth key */
138
0
    gost_enc_with_key(c, Key, H + 24, S + 24);
139
0
    for (i = 0; i < 12; i++)
140
0
        transform_3(S);
141
0
    xor_blocks(S, S, M, 32);
142
0
    transform_3(S);
143
0
    xor_blocks(S, S, H, 32);
144
0
    for (i = 0; i < 61; i++)
145
0
        transform_3(S);
146
0
    memcpy(H, S, 32);
147
0
    return 1;
148
0
}
149
150
/*
151
 * Initialize gost_hash ctx - cleans up temporary structures and set up
152
 * substitution blocks
153
 */
154
int init_gost_hash_ctx(gost_hash_ctx * ctx,
155
                       const gost_subst_block * subst_block)
156
0
{
157
0
    memset(ctx, 0, sizeof(gost_hash_ctx));
158
0
    ctx->cipher_ctx = (gost_ctx *) MYALLOC(sizeof(gost_ctx));
159
0
    if (!ctx->cipher_ctx) {
160
0
        return 0;
161
0
    }
162
0
    gost_init(ctx->cipher_ctx, subst_block);
163
0
    return 1;
164
0
}
165
166
/*
167
 * Free cipher CTX if it is dynamically allocated. Do not use
168
 * if cipher ctx is statically allocated as in OpenSSL implementation of
169
 * GOST hash algroritm
170
 *
171
 */
172
void done_gost_hash_ctx(gost_hash_ctx * ctx)
173
0
{
174
    /*
175
     * No need to use gost_destroy, because cipher keys are not really secret
176
     * when hashing
177
     */
178
0
    MYFREE(ctx->cipher_ctx);
179
0
}
180
181
/*
182
 * reset state of hash context to begin hashing new message
183
 */
184
int start_hash(gost_hash_ctx * ctx)
185
0
{
186
0
    if (!ctx->cipher_ctx)
187
0
        return 0;
188
0
    memset(&(ctx->H), 0, 32);
189
0
    memset(&(ctx->S), 0, 32);
190
0
    ctx->len = 0L;
191
0
    ctx->left = 0;
192
0
    return 1;
193
0
}
194
195
/*
196
 * Hash block of arbitrary length
197
 *
198
 *
199
 */
200
int hash_block(gost_hash_ctx * ctx, const byte * block, size_t length)
201
0
{
202
0
    if (ctx->left) {
203
        /*
204
         * There are some bytes from previous step
205
         */
206
0
        unsigned int add_bytes = 32 - ctx->left;
207
0
        if (add_bytes > length) {
208
0
            add_bytes = length;
209
0
        }
210
0
        memcpy(&(ctx->remainder[ctx->left]), block, add_bytes);
211
0
        ctx->left += add_bytes;
212
0
        if (ctx->left < 32) {
213
0
            return 1;
214
0
        }
215
0
        block += add_bytes;
216
0
        length -= add_bytes;
217
0
        hash_step(ctx->cipher_ctx, ctx->H, ctx->remainder);
218
0
        add_blocks(32, ctx->S, ctx->remainder);
219
0
        ctx->len += 32;
220
0
        ctx->left = 0;
221
0
    }
222
0
    while (length >= 32) {
223
0
        hash_step(ctx->cipher_ctx, ctx->H, block);
224
225
0
        add_blocks(32, ctx->S, block);
226
0
        ctx->len += 32;
227
0
        block += 32;
228
0
        length -= 32;
229
0
    }
230
0
    if (length) {
231
0
        memcpy(ctx->remainder, block, ctx->left = length);
232
0
    }
233
0
    return 1;
234
0
}
235
236
/*
237
 * Compute hash value from current state of ctx
238
 * state of hash ctx becomes invalid and cannot be used for further
239
 * hashing.
240
 */
241
int finish_hash(gost_hash_ctx * ctx, byte * hashval)
242
0
{
243
0
    byte buf[32];
244
0
    byte H[32];
245
0
    byte S[32];
246
0
    ghosthash_len fin_len = ctx->len;
247
0
    byte *bptr;
248
0
    memcpy(H, ctx->H, 32);
249
0
    memcpy(S, ctx->S, 32);
250
0
    if (ctx->left) {
251
0
        memset(buf, 0, 32);
252
0
        memcpy(buf, ctx->remainder, ctx->left);
253
0
        hash_step(ctx->cipher_ctx, H, buf);
254
0
        add_blocks(32, S, buf);
255
0
        fin_len += ctx->left;
256
0
    }
257
0
    memset(buf, 0, 32);
258
0
    bptr = buf;
259
0
    fin_len <<= 3;              /* Hash length in BITS!! */
260
0
    while (fin_len > 0) {
261
0
        *(bptr++) = (byte) (fin_len & 0xFF);
262
0
        fin_len >>= 8;
263
0
    };
264
0
    hash_step(ctx->cipher_ctx, H, buf);
265
0
    hash_step(ctx->cipher_ctx, H, S);
266
0
    memcpy(hashval, H, 32);
267
0
    return 1;
268
0
}