]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - test_digest.c
test_digest: Test CMAC using EVP_MAC (provider) API
[openssl-gost/engine.git] / test_digest.c
1 /*
2  * Test GOST 34.11 Digest operation
3  *
4  * Copyright (C) 2019-2020 Vitaly Chikunov <vt@altlinux.org>. All Rights Reserved.
5  *
6  * Contents licensed under the terms of the OpenSSL license
7  * See https://www.openssl.org/source/license.html for details
8  */
9
10 #include <openssl/engine.h>
11 #include <openssl/opensslv.h>
12 #include <openssl/evp.h>
13 #include <openssl/rand.h>
14 #include <openssl/err.h>
15 #include <openssl/asn1.h>
16 # include <openssl/hmac.h>
17 #if OPENSSL_VERSION_MAJOR >= 3
18 # include <openssl/core_names.h>
19 #endif
20 #include <openssl/obj_mac.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #if MIPSEL
24 # include <sys/sysmips.h>
25 #endif
26 #ifndef EVP_MD_CTRL_SET_KEY
27 # include "gost_lcl.h"
28 #endif
29
30 /* Helpers to test OpenSSL API calls. */
31 #define T(e) ({ if (!(e)) { \
32                 ERR_print_errors_fp(stderr); \
33                 OpenSSLDie(__FILE__, __LINE__, #e); \
34             } \
35         })
36 #define TE(e) ({ if (!(e)) { \
37                 ERR_print_errors_fp(stderr); \
38                 fprintf(stderr, "Error at %s:%d %s\n", __FILE__, __LINE__, #e); \
39                 return -1; \
40             } \
41         })
42
43 #define cRED    "\033[1;31m"
44 #define cDRED   "\033[0;31m"
45 #define cGREEN  "\033[1;32m"
46 #define cDGREEN "\033[0;32m"
47 #define cBLUE   "\033[1;34m"
48 #define cDBLUE  "\033[0;34m"
49 #define cMAGENT "\033[1;35m"
50 #define cNORM   "\033[m"
51 #define TEST_ASSERT(e) {if ((test = (e))) \
52                  printf(cRED "  Test FAILED\n" cNORM); \
53              else \
54                  printf(cGREEN "  Test passed\n" cNORM);}
55
56 /* To test older APIs. */
57 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
58
59 /*
60  * Test keys from both GOST R 34.12-2015 and GOST R 34.13-2015,
61  * for 128-bit cipher (A.1).
62  */
63 static const char K[32] = {
64     0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,
65     0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
66 };
67
68 /*
69  * Key for 64-bit cipher (A.2).
70  */
71 static const char Km[32] = {
72     0xff,0xee,0xdd,0xcc,0xbb,0xaa,0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22,0x11,0x00,
73     0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff,
74 };
75
76 /*
77  * Plaintext from GOST R 34.13-2015 A.1.
78  * First 16 bytes is vector (a) from GOST R 34.12-2015 A.1.
79  */
80 static const char P[] = {
81     0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x00,0xff,0xee,0xdd,0xcc,0xbb,0xaa,0x99,0x88,
82     0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xee,0xff,0x0a,
83     0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xee,0xff,0x0a,0x00,
84     0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xee,0xff,0x0a,0x00,0x11,
85 };
86
87 /* Plaintext for 64-bit cipher (A.2) */
88 static const char Pm[] = {
89     0x92,0xde,0xf0,0x6b,0x3c,0x13,0x0a,0x59,0xdb,0x54,0xc7,0x04,0xf8,0x18,0x9d,0x20,
90     0x4a,0x98,0xfb,0x2e,0x67,0xa8,0x02,0x4c,0x89,0x12,0x40,0x9b,0x17,0xb5,0x7e,0x41,
91 };
92
93 /*
94  * Kuznyechik OMAC1/CMAC test vector from GOST R 34.13-2015 А.1.6
95  */
96 static const char MAC_omac[] = { 0x33,0x6f,0x4d,0x29,0x60,0x59,0xfb,0xe3 };
97
98 /*
99  * Magma OMAC1/CMAC test vector from GOST R 34.13-2015 А.2.6
100  */
101 static const char MAC_magma_omac[] = { 0x15,0x4e,0x72,0x10 };
102
103 /*
104  * OMAC-ACPKM test vector from R 1323565.1.017-2018 A.4.1
105  */
106 static const char P_omac_acpkm1[] = {
107     0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x00,0xFF,0xEE,0xDD,0xCC,0xBB,0xAA,0x99,0x88,
108     0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,
109 };
110
111 static const char MAC_omac_acpkm1[] = {
112     0xB5,0x36,0x7F,0x47,0xB6,0x2B,0x99,0x5E,0xEB,0x2A,0x64,0x8C,0x58,0x43,0x14,0x5E,
113 };
114
115 /*
116  * OMAC-ACPKM test vector from R 1323565.1.017-2018 A.4.2
117  */
118 static const char P_omac_acpkm2[] = {
119     0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x00,0xFF,0xEE,0xDD,0xCC,0xBB,0xAA,0x99,0x88,
120     0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xEE,0xFF,0x0A,
121     0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xEE,0xFF,0x0A,0x00,
122     0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xEE,0xFF,0x0A,0x00,0x11,
123     0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xEE,0xFF,0x0A,0x00,0x11,0x22,
124 };
125
126 static const char MAC_omac_acpkm2[] = {
127     0xFB,0xB8,0xDC,0xEE,0x45,0xBE,0xA6,0x7C,0x35,0xF5,0x8C,0x57,0x00,0x89,0x8E,0x5D,
128 };
129
130 /* Some other test vectors. */
131 static const char etalon_M4[64] = { 0 };
132
133 static const char etalon_M5[] = {
134     0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x31,0x32,0x33,0x34,0x35,0x36,
135     0x37,0x38,0x39,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x20,0x0a,
136     0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x31,0x32,0x33,0x34,0x35,0x36,
137     0x37,0x38,0x39,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x20,0x0a,
138 };
139
140 static const char etalon_M6[] = {
141     0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x31,0x32,0x33,0x34,0x35,0x36,
142     0x37,0x38,0x39,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x20,0x0a,
143     0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x31,0x32,0x33,0x34,0x35,0x36,
144     0x37,0x38,0x39,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x20,0x0a,
145     0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x31,0x32,0x33,0x34,0x35,0x36,
146     0x37,0x38,0x39,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x20,0x0a,
147     0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x31,0x32,0x33,0x34,0x35,0x36,
148     0x37,0x38,0x39,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x20,0x0a,
149     0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x31,0x32,0x33,0x34,0x35,0x36,
150     0x37,0x38,0x39,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x20,0x0a,
151     0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x31,0x32,0x33,0x34,0x35,0x36,
152     0x37,0x38,0x39,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x20,0x0a,
153 };
154
155 static const char etalon_carry[] = {
156     0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,
157     0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,
158     0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,
159     0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,
160     0x16,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
161     0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
162     0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
163     0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x16,
164 };
165
166 struct hash_testvec {
167     int nid;               /* OpenSSL algorithm numeric id. */
168     const char *name;      /* Test name and source. */
169     const char *plaintext; /* Input (of psize), NULL for synthetic test. */
170     const char *digest;    /* Expected output (of EVP_MD_size or truncate). */
171     const char *hmac;      /* Expected output for HMAC tests. */
172     const char *key;       /* MAC key.*/
173     int psize;             /* Input (plaintext) size. */
174     int mdsize;            /* Compare to EVP_MD_size() if non-zero. */
175     int truncate;          /* Truncated output (digest) size. */
176     int key_size;          /* MAC key size. */
177     int block_size;        /* Internal block size. */
178     int acpkm;             /* The section size N (the number of bits that are
179                               processed with one section key before this key is
180                               transformed) (bytes) */
181     int acpkm_t;           /* Master key (change) frequency T* (bytes) */
182 };
183
184 static const struct hash_testvec testvecs[] = {
185     { /* Test vectors from standards. */
186         .nid = NID_id_GostR3411_2012_512,
187         .name = "M1 from RFC 6986 (10.1.1) and GOST R 34.11-2012 (А.1.1)",
188         .plaintext =
189             "012345678901234567890123456789012345678901234567890123456789012",
190         .psize = 63,
191         .digest =
192             "\x1b\x54\xd0\x1a\x4a\xf5\xb9\xd5\xcc\x3d\x86\xd6\x8d\x28\x54\x62"
193             "\xb1\x9a\xbc\x24\x75\x22\x2f\x35\xc0\x85\x12\x2b\xe4\xba\x1f\xfa"
194             "\x00\xad\x30\xf8\x76\x7b\x3a\x82\x38\x4c\x65\x74\xf0\x24\xc3\x11"
195             "\xe2\xa4\x81\x33\x2b\x08\xef\x7f\x41\x79\x78\x91\xc1\x64\x6f\x48",
196         .mdsize = 512 / 8,
197         .block_size = 512 / 8,
198     },
199     {
200         .nid = NID_id_GostR3411_2012_256,
201         .name = "M1 from RFC 6986 (10.1.2) and GOST R 34.11-2012 (А.1.2)",
202         .plaintext =
203             "012345678901234567890123456789012345678901234567890123456789012",
204         .psize = 63,
205         .digest =
206             "\x9d\x15\x1e\xef\xd8\x59\x0b\x89\xda\xa6\xba\x6c\xb7\x4a\xf9\x27"
207             "\x5d\xd0\x51\x02\x6b\xb1\x49\xa4\x52\xfd\x84\xe5\xe5\x7b\x55\x00",
208         .mdsize = 256 / 8,
209         .block_size = 512 / 8,
210     },
211     {
212         .nid = NID_id_GostR3411_2012_512,
213         .name = "M2 from RFC 6986 (10.2.1) and GOST R 34.11-2012 (А.2.1)",
214         .plaintext =
215             "\xd1\xe5\x20\xe2\xe5\xf2\xf0\xe8\x2c\x20\xd1\xf2\xf0\xe8\xe1\xee"
216             "\xe6\xe8\x20\xe2\xed\xf3\xf6\xe8\x2c\x20\xe2\xe5\xfe\xf2\xfa\x20"
217             "\xf1\x20\xec\xee\xf0\xff\x20\xf1\xf2\xf0\xe5\xeb\xe0\xec\xe8\x20"
218             "\xed\xe0\x20\xf5\xf0\xe0\xe1\xf0\xfb\xff\x20\xef\xeb\xfa\xea\xfb"
219             "\x20\xc8\xe3\xee\xf0\xe5\xe2\xfb",
220         .psize = 72,
221         .digest =
222             "\x1e\x88\xe6\x22\x26\xbf\xca\x6f\x99\x94\xf1\xf2\xd5\x15\x69\xe0"
223             "\xda\xf8\x47\x5a\x3b\x0f\xe6\x1a\x53\x00\xee\xe4\x6d\x96\x13\x76"
224             "\x03\x5f\xe8\x35\x49\xad\xa2\xb8\x62\x0f\xcd\x7c\x49\x6c\xe5\xb3"
225             "\x3f\x0c\xb9\xdd\xdc\x2b\x64\x60\x14\x3b\x03\xda\xba\xc9\xfb\x28",
226     },
227     {
228         .nid = NID_id_GostR3411_2012_256,
229         .name = "M2 from RFC 6986 (10.2.2) and GOST R 34.11-2012 (А.2.2)",
230         .plaintext =
231             "\xd1\xe5\x20\xe2\xe5\xf2\xf0\xe8\x2c\x20\xd1\xf2\xf0\xe8\xe1\xee"
232             "\xe6\xe8\x20\xe2\xed\xf3\xf6\xe8\x2c\x20\xe2\xe5\xfe\xf2\xfa\x20"
233             "\xf1\x20\xec\xee\xf0\xff\x20\xf1\xf2\xf0\xe5\xeb\xe0\xec\xe8\x20"
234             "\xed\xe0\x20\xf5\xf0\xe0\xe1\xf0\xfb\xff\x20\xef\xeb\xfa\xea\xfb"
235             "\x20\xc8\xe3\xee\xf0\xe5\xe2\xfb",
236         .psize = 72,
237         .digest =
238             "\x9d\xd2\xfe\x4e\x90\x40\x9e\x5d\xa8\x7f\x53\x97\x6d\x74\x05\xb0"
239             "\xc0\xca\xc6\x28\xfc\x66\x9a\x74\x1d\x50\x06\x3c\x55\x7e\x8f\x50",
240     },
241     /* OMAC tests */
242     {
243         .nid = NID_grasshopper_mac,
244         .name = "P from GOST R 34.13-2015 (А.1.6)",
245         .plaintext = P,
246         .psize = sizeof(P),
247         .key = K,
248         .key_size = sizeof(K),
249         .digest = MAC_omac,
250         .mdsize = 128 / 8,
251         .truncate = sizeof(MAC_omac),
252     },
253     {
254         .nid = NID_magma_mac,
255         .name = "P from GOST R 34.13-2015 (А.2.6)",
256         .plaintext = Pm,
257         .psize = sizeof(Pm),
258         .key = Km,
259         .key_size = sizeof(Km),
260         .digest = MAC_magma_omac,
261         .mdsize = 64 / 8,
262         .truncate = sizeof(MAC_magma_omac),
263     },
264     {
265         .nid = NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
266         .name = "M from R 1323565.1.017-2018 (A.4.1)",
267         .plaintext = P_omac_acpkm1,
268         .psize = sizeof(P_omac_acpkm1),
269         .key = K,
270         .key_size = sizeof(K),
271         .acpkm = 32,
272         .acpkm_t = 768 / 8,
273         .digest = MAC_omac_acpkm1,
274         .mdsize = sizeof(MAC_omac_acpkm1),
275     },
276     {
277         .nid = NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
278         .name = "M from R 1323565.1.017-2018 (A.4.2)",
279         .plaintext = P_omac_acpkm2,
280         .psize = sizeof(P_omac_acpkm2),
281         .key = K,
282         .key_size = sizeof(K),
283         .acpkm = 32,
284         .acpkm_t = 768 / 8,
285         .digest = MAC_omac_acpkm2,
286         .mdsize = sizeof(MAC_omac_acpkm2),
287     },
288     { /* HMAC tests. */
289         .nid = NID_id_GostR3411_2012_512,
290         .name = "HMAC_GOSTR3411_2012_512 from RFC 7836 (B) and R 50.1.113-2016 (A)",
291         .plaintext =
292             "\x01\x26\xbd\xb8\x78\x00\xaf\x21\x43\x41\x45\x65\x63\x78\x01\x00",
293         .psize = 16,
294         .key =
295             "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
296             "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
297         .key_size = 32,
298         .hmac =
299             "\xa5\x9b\xab\x22\xec\xae\x19\xc6\x5f\xbd\xe6\xe5\xf4\xe9\xf5\xd8"
300             "\x54\x9d\x31\xf0\x37\xf9\xdf\x9b\x90\x55\x00\xe1\x71\x92\x3a\x77"
301             "\x3d\x5f\x15\x30\xf2\xed\x7e\x96\x4c\xb2\xee\xdc\x29\xe9\xad\x2f"
302             "\x3a\xfe\x93\xb2\x81\x4f\x79\xf5\x00\x0f\xfc\x03\x66\xc2\x51\xe6",
303     },
304     {
305         .nid = NID_id_GostR3411_2012_256,
306         .name = "HMAC_GOSTR3411_2012_256 from RFC 7836 (B) and R 50.1.113-2016 (A)",
307         .plaintext =
308             "\x01\x26\xbd\xb8\x78\x00\xaf\x21\x43\x41\x45\x65\x63\x78\x01\x00",
309         .psize = 16,
310         .key =
311             "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
312             "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
313         .key_size = 32,
314         .hmac =
315             "\xa1\xaa\x5f\x7d\xe4\x02\xd7\xb3\xd3\x23\xf2\x99\x1c\x8d\x45\x34"
316             "\x01\x31\x37\x01\x0a\x83\x75\x4f\xd0\xaf\x6d\x7c\xd4\x92\x2e\xd9",
317     },
318     /* Other KATs. */
319     {
320         .nid = NID_id_GostR3411_2012_512,
321         .name = "Zero length string (M3)",
322         .plaintext = "",
323         .psize = 0,
324         .digest =
325             "\x8e\x94\x5d\xa2\x09\xaa\x86\x9f\x04\x55\x92\x85\x29\xbc\xae\x46"
326             "\x79\xe9\x87\x3a\xb7\x07\xb5\x53\x15\xf5\x6c\xeb\x98\xbe\xf0\xa7"
327             "\x36\x2f\x71\x55\x28\x35\x6e\xe8\x3c\xda\x5f\x2a\xac\x4c\x6a\xd2"
328             "\xba\x3a\x71\x5c\x1b\xcd\x81\xcb\x8e\x9f\x90\xbf\x4c\x1c\x1a\x8a",
329         .mdsize = 512 / 8,
330     },
331     {
332         .nid = NID_id_GostR3411_2012_256,
333         .name = "Zero length string (M3)",
334         .plaintext = "",
335         .psize = 0,
336         .digest =
337             "\x3f\x53\x9a\x21\x3e\x97\xc8\x02\xcc\x22\x9d\x47\x4c\x6a\xa3\x2a"
338             "\x82\x5a\x36\x0b\x2a\x93\x3a\x94\x9f\xd9\x25\x20\x8d\x9c\xe1\xbb",
339         .mdsize = 256 / 8,
340     },
341     { /* M4 */
342         .nid = NID_id_GostR3411_2012_512,
343         .name = "64 bytes of zeros (M4)",
344         .plaintext = etalon_M4,
345         .psize = sizeof(etalon_M4),
346         .digest =
347             "\xb0\xfd\x29\xac\x1b\x0d\xf4\x41\x76\x9f\xf3\xfd\xb8\xdc\x56\x4d"
348             "\xf6\x77\x21\xd6\xac\x06\xfb\x28\xce\xff\xb7\xbb\xaa\x79\x48\xc6"
349             "\xc0\x14\xac\x99\x92\x35\xb5\x8c\xb2\x6f\xb6\x0f\xb1\x12\xa1\x45"
350             "\xd7\xb4\xad\xe9\xae\x56\x6b\xf2\x61\x14\x02\xc5\x52\xd2\x0d\xb7"
351     },
352     {
353         .nid = NID_id_GostR3411_2012_256,
354         .name = "64 bytes of zeros (M4)",
355         .plaintext = etalon_M4,
356         .psize = sizeof(etalon_M4),
357         .digest =
358             "\xdf\x1f\xda\x9c\xe8\x31\x91\x39\x05\x37\x35\x80\x31\xdb\x2e\xca"
359             "\xa6\xaa\x54\xcd\x0e\xda\x24\x1d\xc1\x07\x10\x5e\x13\x63\x6b\x95"
360     },
361     { /* M5 */
362         .nid = NID_id_GostR3411_2012_512,
363         .name = "64 bytes of (M5)",
364         .plaintext = etalon_M5,
365         .psize = sizeof(etalon_M5),
366         .digest =
367             "\x36\x3b\x44\x9e\xc8\x1a\xe4\x0b\x3a\x40\x7b\x12\x5c\x3b\x1c\x2b"
368             "\x76\x8b\x50\x49\x6b\xcb\x5f\x69\x0b\x89\xe9\x00\x7b\x06\xe4\x08"
369             "\x41\x82\xed\x45\xd4\x07\x2a\x67\xfe\xc9\xd3\x42\x1d\xab\x01\x3d"
370             "\xa2\xaa\xbc\x1d\x65\x28\xe8\xe7\x75\xae\xc7\xb3\x45\x7a\xc6\x75"
371     },
372     {
373         .nid = NID_id_GostR3411_2012_256,
374         .name = "64 bytes of (M5)",
375         .plaintext = etalon_M5,
376         .psize = sizeof(etalon_M5),
377         .digest =
378             "\xf0\xa5\x57\xf6\xa0\x4a\x90\xab\x18\x11\xc1\xb6\xe9\xb0\x78\xe4"
379             "\x16\x3b\x74\x03\x7c\x6c\xf5\x9f\x52\x44\x4a\x37\xf4\x8e\x11\xd8"
380     },
381     { /* M6 */
382         .nid = NID_id_GostR3411_2012_512,
383         .name = "(M6)",
384         .plaintext = etalon_M6,
385         .psize = sizeof(etalon_M6),
386         .digest =
387             "\x87\x81\xdf\xc8\x1d\x2d\xb6\xa4\x1d\x18\x57\xf3\x23\x0b\x3f\xfe"
388             "\x2b\xda\x57\x42\x73\xea\x19\x47\x18\x9a\xaa\x54\x68\x47\x0d\xf1"
389             "\xc4\xb3\x74\xb1\xa2\xb5\x6e\x59\xd1\x1d\x21\x3f\xea\x57\xe3\x51"
390             "\x45\x43\xb0\xce\xd9\xb2\x0e\x55\x3a\xe6\x64\x25\xec\x90\x9c\xfd"
391     },
392     {
393         .nid = NID_id_GostR3411_2012_256,
394         .name = "(M6)",
395         .plaintext = etalon_M6,
396         .psize = sizeof(etalon_M6),
397         .digest =
398             "\x2f\x4f\x65\x1f\xe8\x8f\xea\x46\xec\x6f\x22\x23\x72\x8d\x8d\xff"
399             "\x39\x68\x89\x35\x58\xef\x00\xa3\x10\xc2\x3e\x7d\x19\x23\xba\x0c"
400     },
401     { /* carry */
402         .nid = NID_id_GostR3411_2012_512,
403         .name = "(carry)",
404         .plaintext = etalon_carry,
405         .psize = sizeof(etalon_carry),
406         .digest =
407             "\x8b\x06\xf4\x1e\x59\x90\x7d\x96\x36\xe8\x92\xca\xf5\x94\x2f\xcd"
408             "\xfb\x71\xfa\x31\x16\x9a\x5e\x70\xf0\xed\xb8\x73\x66\x4d\xf4\x1c"
409             "\x2c\xce\x6e\x06\xdc\x67\x55\xd1\x5a\x61\xcd\xeb\x92\xbd\x60\x7c"
410             "\xc4\xaa\xca\x67\x32\xbf\x35\x68\xa2\x3a\x21\x0d\xd5\x20\xfd\x41"
411     },
412     {
413         .nid = NID_id_GostR3411_2012_256,
414         .name = "(carry)",
415         .plaintext = etalon_carry,
416         .psize = sizeof(etalon_carry),
417         .digest =
418             "\x81\xbb\x63\x2f\xa3\x1f\xcc\x38\xb4\xc3\x79\xa6\x62\xdb\xc5\x8b"
419             "\x9b\xed\x83\xf5\x0d\x3a\x1b\x2c\xe7\x27\x1a\xb0\x2d\x25\xba\xbb"
420     },
421     /* Synthetic tests. */
422     {
423         .nid = NID_id_GostR3411_2012_256,
424         .name = "streebog256 synthetic test",
425         .mdsize = 32,
426         .block_size = 64,
427         .digest =
428             "\xa2\xf3\x6d\x9c\x42\xa1\x1e\xad\xe3\xc1\xfe\x99\xf9\x99\xc3\x84"
429             "\xe7\x98\xae\x24\x50\x75\x73\xd7\xfc\x99\x81\xa0\x45\x85\x41\xf6"
430     }, {
431         .nid = NID_id_GostR3411_2012_512,
432         .name = "streebog512 synthetic test",
433         .mdsize = 64,
434         .block_size = 64,
435         .digest =
436             "\x1d\x14\x4d\xd8\xb8\x27\xfb\x55\x1a\x5a\x7d\x03\xbb\xdb\xfa\xcb"
437             "\x43\x6b\x5b\xc5\x77\x59\xfd\x5f\xf2\x3b\x8e\xf9\xc4\xdd\x6f\x79"
438             "\x45\xd8\x16\x59\x9e\xaa\xbc\xf2\xb1\x4f\xd0\xe4\xf6\xad\x46\x60"
439             "\x90\x89\xf7\x2f\x93\xd8\x85\x0c\xb0\x43\xff\x5a\xb6\xe3\x69\xbd"
440     },
441     { 0 }
442 };
443
444 static void hexdump(const void *ptr, size_t len)
445 {
446     const unsigned char *p = ptr;
447     size_t i, j;
448
449     for (i = 0; i < len; i += j) {
450         for (j = 0; j < 16 && i + j < len; j++)
451             printf("%s%02x", j? "" : " ", p[i + j]);
452     }
453     printf("\n");
454 }
455
456 static int do_hmac_old(const EVP_MD *type, const char *plaintext,
457     unsigned int psize, const char *etalon, int mdsize,
458     const char *key, unsigned int key_size)
459 {
460     unsigned int len;
461     unsigned char md[EVP_MAX_MD_SIZE];
462
463     HMAC_CTX *ctx;
464     T(ctx = HMAC_CTX_new());
465     T(HMAC_Init_ex(ctx, key, key_size, type, NULL));
466     T(HMAC_Update(ctx, (const unsigned char *)plaintext, psize));
467     T(HMAC_Final(ctx, md, &len));
468     HMAC_CTX_free(ctx);
469
470     if (mdsize)
471         T(len == mdsize);
472     if (memcmp(md, etalon, len) != 0) {
473         printf(cRED "hmac mismatch\n" cNORM);
474         hexdump(etalon, len);
475         hexdump(md, len);
476         return 1;
477     }
478     return 0;
479 }
480 #if OPENSSL_VERSION_MAJOR >= 3
481 static int do_hmac_prov(const EVP_MD *type, const char *plaintext,
482     unsigned int psize, const char *etalon, int mdsize,
483     const char *key, unsigned int key_size)
484 {
485     size_t len;
486     unsigned char md[EVP_MAX_MD_SIZE];
487
488     EVP_MAC *hmac;
489     T(hmac = EVP_MAC_fetch(NULL, "HMAC", NULL));
490     EVP_MAC_CTX *ctx;
491     T(ctx = EVP_MAC_CTX_new(hmac));
492     OSSL_PARAM params[] = {
493         OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST,
494             (char *)EVP_MD_name(type), 0),
495         OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, (char *)key, key_size),
496         OSSL_PARAM_END
497     };
498     T(EVP_MAC_CTX_set_params(ctx, params));
499     T(EVP_MAC_init(ctx));
500     T(EVP_MAC_update(ctx, (unsigned char *)plaintext, psize));
501     T(EVP_MAC_final(ctx, md, &len, EVP_MAX_MD_SIZE));
502     EVP_MAC_CTX_free(ctx);
503     EVP_MAC_free(hmac);
504
505     if (mdsize)
506         T(len == mdsize);
507     if (memcmp(md, etalon, len) != 0) {
508         printf(cRED "hmac mismatch\n" cNORM);
509         hexdump(etalon, len);
510         hexdump(md, len);
511         return 1;
512     }
513     return 0;
514 }
515 #endif
516
517 static int do_hmac(const EVP_MD *type, const char *plaintext,
518     unsigned int psize, const char *etalon, int mdsize,
519     const char *key, unsigned int key_size)
520 {
521     int ret;
522
523     /* Test old (deprecated) and (too) new APIs. */
524     ret = do_hmac_old(type, plaintext, psize, etalon, mdsize, key, key_size);
525 #if OPENSSL_VERSION_MAJOR >= 3
526     ret |= do_hmac_prov(type, plaintext, psize, etalon, mdsize, key, key_size);
527 #endif
528
529     return ret;
530 }
531
532 /*
533  * If we have OMAC1/CMAC test vector,
534  * use CMAC provider to test it.
535  */
536 static int do_cmac_prov(int iter, const char *plaintext,
537     const struct hash_testvec *t)
538 {
539 #if OPENSSL_VERSION_MAJOR >= 3
540     char *mdname = NULL;
541     /*
542      * CMAC needs CBC.
543      * Convert 'mac' digest to the underlying CBC cipher.
544      */
545     switch (t->nid) {
546         case NID_grasshopper_mac:
547             mdname = "kuznyechik-cbc";
548             break;
549         case NID_magma_mac:
550             mdname = "magma-cbc";
551             break;
552         default:
553             return 0;
554     }
555
556     if (!iter)
557         printf("[Test CMAC using provider using %s] ", mdname);
558
559     size_t len;
560     unsigned char md[EVP_MAX_MD_SIZE];
561     size_t mdsize = t->mdsize;
562     if (t->truncate)
563         mdsize = t->truncate;
564
565     EVP_MAC *cmac;
566     T(cmac = EVP_MAC_fetch(NULL, "CMAC", NULL));
567     EVP_MAC_CTX *ctx;
568     T(ctx = EVP_MAC_CTX_new(cmac));
569     OSSL_PARAM params[] = {
570         OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, mdname, 0),
571         OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, (char *)t->key, t->key_size),
572         OSSL_PARAM_END
573     };
574     T(EVP_MAC_CTX_set_params(ctx, params));
575     T(EVP_MAC_init(ctx));
576     T(EVP_MAC_update(ctx, (unsigned char *)plaintext, t->psize));
577     T(EVP_MAC_final(ctx, md, &len, EVP_MAX_MD_SIZE));
578     EVP_MAC_CTX_free(ctx);
579     EVP_MAC_free(cmac);
580
581     /* CMAC provider will not respect outsize, and will output full block.
582      * So, just compare until what we need. */
583     T(mdsize <= len);
584     if (memcmp(md, t->digest, mdsize) != 0) {
585         printf(cRED "cmac mismatch\n" cNORM);
586         hexdump(t->digest, mdsize);
587         hexdump(md, len);
588         return 1;
589     }
590 #endif
591     return 0;
592 }
593
594 static int do_digest(const EVP_MD *type, const char *plaintext,
595     unsigned int psize, const char *etalon, int mdsize, int truncate,
596     const char *key, unsigned int key_size, int acpkm, int acpkm_t,
597     int block_size)
598 {
599     if (mdsize)
600         T(EVP_MD_size(type) == mdsize);
601     if (truncate)
602         mdsize = truncate;
603     else
604         mdsize = EVP_MD_size(type);
605
606     if (block_size)
607         T(EVP_MD_block_size(type) == block_size);
608     EVP_MD_CTX *ctx;
609     T(ctx = EVP_MD_CTX_new());
610     T(EVP_MD_CTX_init(ctx));
611     T(EVP_DigestInit_ex(ctx, type, NULL));
612     if (key)
613         T(EVP_MD_CTX_ctrl(ctx, EVP_MD_CTRL_SET_KEY, key_size, (void *)key));
614     if (acpkm)
615         T(EVP_MD_CTX_ctrl(ctx,
616                 EVP_CTRL_KEY_MESH, acpkm, acpkm_t? &acpkm_t : NULL));
617     T(EVP_DigestUpdate(ctx, plaintext, psize));
618
619     unsigned int len;
620     unsigned char md[EVP_MAX_MD_SIZE];
621
622     if (EVP_MD_flags(EVP_MD_CTX_md(ctx)) & EVP_MD_FLAG_XOF) {
623         T(EVP_DigestFinalXOF(ctx, md, mdsize));
624         len = mdsize;
625     } else {
626         T(EVP_MD_CTX_size(ctx) == mdsize);
627         T(EVP_DigestFinal_ex(ctx, md, &len));
628     }
629
630     EVP_MD_CTX_free(ctx);
631     T(len == mdsize);
632     if (memcmp(md, etalon, mdsize) != 0) {
633         printf(cRED "digest mismatch\n" cNORM);
634         hexdump(etalon, mdsize);
635         hexdump(md, mdsize);
636         return 1;
637     }
638
639     return 0;
640 }
641
642 static int do_test(const struct hash_testvec *tv)
643 {
644     int ret = 0;
645
646     const EVP_MD *type;
647     T(type = EVP_get_digestbynid(tv->nid));
648     const char *name = EVP_MD_name(type);
649     printf(cBLUE "%s Test %s: %s: " cNORM, tv->hmac? "HMAC" : "MD",
650         name, tv->name);
651
652     /* Test alignment problems. */
653     int shifts = 32;
654     int i;
655     char *buf;
656     T(buf = OPENSSL_malloc(tv->psize + shifts));
657     for (i = 0; i < shifts; i++) {
658         memcpy(buf + i, tv->plaintext, tv->psize);
659         if (tv->hmac)
660             ret |= do_hmac(type, buf + i, tv->psize, tv->hmac,
661                 tv->mdsize, tv->key, tv->key_size);
662         else
663             ret |= do_digest(type, buf + i, tv->psize, tv->digest,
664                 tv->mdsize, tv->truncate, tv->key, tv->key_size,
665                 tv->acpkm, tv->acpkm_t, tv->block_size);
666         /* Test CMAC provider. */
667         ret |= do_cmac_prov(i, buf + i, tv);
668         /* No need to continue loop on failure. */
669         if (ret)
670             break;
671     }
672     OPENSSL_free(buf);
673
674     if (!ret)
675         printf(cGREEN "success\n" cNORM);
676     else
677         printf(cRED "fail\n" cNORM);
678     return ret;
679 }
680
681 #define SUPER_SIZE 256
682 /*
683  * For 256-byte buffer filled with 256 bytes from 0 to 255;
684  * Digest them 256 times from the buffer end with lengths from 0 to 256,
685  * and from beginning of the buffer with lengths from 0 to 256;
686  * Each produced digest is digested again into final sum.
687  */
688 static int do_synthetic_once(const struct hash_testvec *tv, unsigned int shifts)
689 {
690     unsigned char *ibuf, *md;
691     T(ibuf = OPENSSL_zalloc(SUPER_SIZE + shifts));
692
693     /* fill with pattern */
694     unsigned int len;
695     for (len = 0; len < SUPER_SIZE; len++)
696             ibuf[shifts + len] = len & 0xff;
697
698     const EVP_MD *mdtype;
699     T(mdtype = EVP_get_digestbynid(tv->nid));
700     OPENSSL_assert(tv->nid == EVP_MD_type(mdtype));
701     EVP_MD_CTX *ctx, *ctx2;
702     T(ctx  = EVP_MD_CTX_new());
703     T(ctx2 = EVP_MD_CTX_new());
704     T(EVP_DigestInit(ctx2, mdtype));
705     OPENSSL_assert(tv->nid == EVP_MD_CTX_type(ctx2));
706     OPENSSL_assert(EVP_MD_block_size(mdtype) == tv->block_size);
707     OPENSSL_assert(EVP_MD_CTX_size(ctx2) == tv->mdsize);
708     OPENSSL_assert(EVP_MD_CTX_block_size(ctx2) == tv->block_size);
709
710     const unsigned int mdlen = EVP_MD_size(mdtype);
711     OPENSSL_assert(mdlen == tv->mdsize);
712     T(md = OPENSSL_zalloc(mdlen + shifts));
713     md += shifts; /* test for output digest alignment problems */
714
715     /* digest cycles */
716     for (len = 0; len < SUPER_SIZE; len++) {
717         /* for each len digest len bytes from the end of buf */
718         T(EVP_DigestInit(ctx, mdtype));
719         T(EVP_DigestUpdate(ctx, ibuf + shifts + SUPER_SIZE - len, len));
720         T(EVP_DigestFinal(ctx, md, NULL));
721         T(EVP_DigestUpdate(ctx2, md, mdlen));
722     }
723
724     for (len = 0; len < SUPER_SIZE; len++) {
725         /* for each len digest len bytes from the beginning of buf */
726         T(EVP_DigestInit(ctx, mdtype));
727         T(EVP_DigestUpdate(ctx, ibuf + shifts, len));
728         T(EVP_DigestFinal(ctx, md, NULL));
729         T(EVP_DigestUpdate(ctx2, md, mdlen));
730     }
731
732     OPENSSL_free(ibuf);
733     EVP_MD_CTX_free(ctx);
734
735     T(EVP_DigestFinal(ctx2, md, &len));
736     EVP_MD_CTX_free(ctx2);
737
738     if (len != mdlen) {
739         printf(cRED "digest output len mismatch %u != %u (expected)\n" cNORM,
740             len, mdlen);
741         goto err;
742     }
743
744     if (memcmp(md, tv->digest, mdlen) != 0) {
745         printf(cRED "digest mismatch\n" cNORM);
746
747         unsigned int i;
748         printf("  Expected value is: ");
749         for (i = 0; i < mdlen; i++)
750             printf("\\x%02x", md[i]);
751         printf("\n");
752         goto err;
753     }
754
755     OPENSSL_free(md - shifts);
756     return 0;
757 err:
758     OPENSSL_free(md - shifts);
759     return 1;
760 }
761
762 /* do different block sizes and different memory offsets */
763 static int do_synthetic_test(const struct hash_testvec *tv)
764 {
765     int ret = 0;
766
767     printf(cBLUE "MD Test %s: " cNORM, tv->name);
768     fflush(stdout);
769
770     unsigned int shifts;
771     for (shifts = 0; shifts < 16 && !ret; shifts++)
772         ret |= do_synthetic_once(tv, shifts);
773
774     if (!ret)
775         printf(cGREEN "success\n" cNORM);
776     else
777         printf(cRED "fail\n" cNORM);
778     return 0;
779 }
780
781 int main(int argc, char **argv)
782 {
783     int ret = 0;
784
785 #if MIPSEL
786     /* Trigger SIGBUS for unaligned access. */
787     sysmips(MIPS_FIXADE, 0);
788 #endif
789     setenv("OPENSSL_ENGINES", ENGINE_DIR, 0);
790     OPENSSL_add_all_algorithms_conf();
791     ERR_load_crypto_strings();
792     ENGINE *eng;
793     T(eng = ENGINE_by_id("gost"));
794     T(ENGINE_init(eng));
795     T(ENGINE_set_default(eng, ENGINE_METHOD_ALL));
796
797     const struct hash_testvec *tv;
798     for (tv = testvecs; tv->nid; tv++) {
799         if (tv->plaintext)
800             ret |= do_test(tv);
801         else
802             ret |= do_synthetic_test(tv);
803     }
804
805     ENGINE_DIGESTS_PTR fn_c;
806     T(fn_c = ENGINE_get_digests(eng));
807     const int *nids;
808     int n, k;
809     n = fn_c(eng, NULL, &nids, 0);
810     for (k = 0; k < n; ++k) {
811         for (tv = testvecs; tv->nid; tv++)
812             if (tv->nid == nids[k])
813                 break;
814         if (!tv->nid)
815             printf(cMAGENT "Digest %s is untested!\n" cNORM, OBJ_nid2sn(nids[k]));
816     }
817
818     ENGINE_finish(eng);
819     ENGINE_free(eng);
820
821     if (ret)
822         printf(cDRED "= Some tests FAILED!\n" cNORM);
823     else
824         printf(cDGREEN "= All tests passed!\n" cNORM);
825     return ret;
826 }