2 * Copyright (c) 2019-2020 Dmitry Belyavskiy <beldmit@gmail.com>
4 * Contents licensed under the terms of the OpenSSL license
5 * See https://www.openssl.org/source/license.html for details
8 # pragma warning(push, 3)
9 # include <openssl/applink.c>
14 # include <openssl/err.h>
15 # include <openssl/evp.h>
17 static void hexdump(FILE *f, const char *title, const unsigned char *s, int l)
21 fprintf(f, "%s", title);
24 fprintf(f, "\n%04x", n);
25 fprintf(f, " %02x", s[n]);
32 #ifdef EVP_MD_CTRL_TLSTREE
33 const unsigned char mac_secret[] = {
34 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
35 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
38 const unsigned char enc_key[] = {
39 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43 const unsigned char full_iv[] = {
44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 unsigned char seq0[] = {
50 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53 const unsigned char rec0_header[] = {
54 0x17, 0x03, 0x03, 0x00, 0x0F
57 const unsigned char data0[15] = {
58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
61 const unsigned char mac0_etl[16] = {
62 0x75, 0x53, 0x09, 0xCB, 0xC7, 0x3B, 0xB9, 0x49, 0xC5, 0x0E, 0xBB, 0x86, 0x16, 0x0A, 0x0F, 0xEE
65 const unsigned char enc0_etl[31] = {
66 0xf3, 0x17, 0xa7, 0x1d, 0x3a, 0xce, 0x43, 0x3b, 0x01, 0xd4, 0xe7, 0xd4, 0xef, 0x61, 0xae, 0x00,
67 0xd5, 0x3b, 0x41, 0x52, 0x7a, 0x26, 0x1e, 0xdf, 0xc2, 0xba, 0x78, 0x57, 0xc1, 0x93, 0x2d
70 unsigned char data0_processed[31];
71 unsigned char mac0[16];
73 unsigned char seq63[] = {
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F,
77 const unsigned char rec63_header[] = {
78 0x17, 0x03, 0x03, 0x10, 0x00
81 unsigned char data63[4096];
83 const unsigned char mac63_etl[16] = {
84 0x0A, 0x3B, 0xFD, 0x43, 0x0F, 0xCD, 0xD8, 0xD8, 0x5C, 0x96, 0x46, 0x86, 0x81, 0x78, 0x4F, 0x7D
87 const unsigned char enc63_etl_head[32] = {
88 0x6A, 0x18, 0x38, 0xB0, 0xA0, 0xD5, 0xA0, 0x4D, 0x1F, 0x29, 0x64, 0x89, 0x6D, 0x08, 0x5F, 0xB7,
89 0xDA, 0x84, 0xD7, 0x76, 0xC3, 0x9F, 0x5C, 0xDC, 0x37, 0x20, 0xB7, 0xB5, 0x59, 0xEF, 0x13, 0x9D
91 const unsigned char enc63_etl_tail[48] = {
92 0x0A, 0x81, 0x29, 0x9B, 0x35, 0x98, 0x19, 0x5D, 0xD4, 0x51, 0x68, 0xA6, 0x38, 0x50, 0xA7, 0x6E,
93 0x1A, 0x4F, 0x1E, 0x6D, 0xD5, 0xEF, 0x72, 0x59, 0x3F, 0xAE, 0x76, 0x55, 0x71, 0xEC, 0x37, 0xE7,
94 0x17, 0xF5, 0xB8, 0x62, 0x85, 0xBB, 0x5B, 0xFD, 0x83, 0xB6, 0x6A, 0xB7, 0x63, 0x86, 0x52, 0x08
97 unsigned char data63_processed[4096+16];
98 unsigned char mac63[16];
100 EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
101 EVP_CIPHER_CTX *enc = NULL;
103 const EVP_CIPHER *ciph;
108 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
110 memset(data63, 0, 4096);
112 md = EVP_get_digestbynid(NID_grasshopper_mac);
114 EVP_DigestInit_ex(mdctx, md, NULL);
115 mac_key = EVP_PKEY_new_mac_key(NID_grasshopper_mac, NULL, mac_secret, 32);
116 EVP_DigestSignInit(mdctx, NULL, md, NULL, mac_key);
117 EVP_PKEY_free(mac_key);
119 EVP_MD_CTX_ctrl(mdctx, EVP_MD_CTRL_TLSTREE, 0, seq0);
120 EVP_DigestSignUpdate(mdctx, seq0, 8);
121 EVP_DigestSignUpdate(mdctx, rec0_header, 5);
122 EVP_DigestSignUpdate(mdctx, data0, 15);
123 EVP_DigestSignFinal(mdctx, mac0, &mac_len);
125 EVP_MD_CTX_free(mdctx);
126 hexdump(stderr, "MAC0 result", mac0, mac_len);
127 if (memcmp(mac0, mac0_etl, 16) != 0) {
128 fprintf(stderr, "MAC0 mismatch");
132 ciph = EVP_get_cipherbynid(NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm);
133 enc = EVP_CIPHER_CTX_new();
134 if (EVP_EncryptInit_ex(enc, ciph, NULL, enc_key, full_iv) <= 0) {
135 fprintf(stderr, "Internal error");
139 for (i = 7; i >= 0; i--) {
144 EVP_CIPHER_CTX_ctrl(enc, EVP_CTRL_TLSTREE, 0, seq0);
145 EVP_Cipher(enc, data0_processed, data0, sizeof(data0));
146 EVP_Cipher(enc, data0_processed+sizeof(data0), mac0, 16);
148 hexdump(stderr, "ENC0 result", data0_processed, 31);
149 if (memcmp(enc0_etl, data0_processed, sizeof(data0_processed)) != 0) {
150 fprintf(stderr, "ENC0 mismatch");
154 mdctx = EVP_MD_CTX_new();
155 EVP_DigestInit_ex(mdctx, md, NULL);
156 mac_key = EVP_PKEY_new_mac_key(NID_grasshopper_mac, NULL, mac_secret, 32);
157 EVP_DigestSignInit(mdctx, NULL, md, NULL, mac_key);
158 EVP_PKEY_free(mac_key);
160 EVP_MD_CTX_ctrl(mdctx, EVP_MD_CTRL_TLSTREE, 0, seq63);
161 EVP_DigestSignUpdate(mdctx, seq63, 8);
162 EVP_DigestSignUpdate(mdctx, rec63_header, 5);
163 EVP_DigestSignUpdate(mdctx, data63, 4096);
164 EVP_DigestSignFinal(mdctx, mac63, &mac_len);
166 EVP_MD_CTX_free(mdctx);
167 hexdump(stderr, "MAC63 result", mac63, mac_len);
168 if (memcmp(mac63, mac63_etl, 16) != 0) {
169 fprintf(stderr, "MAC63 mismatch");
173 for (i = 7; i >= 0; i--) {
178 EVP_CIPHER_CTX_ctrl(enc, EVP_CTRL_TLSTREE, 0, seq63);
179 EVP_Cipher(enc, data63_processed, data63, sizeof(data63));
180 EVP_Cipher(enc, data63_processed+sizeof(data63), mac63, 16);
182 hexdump(stderr, "ENC63 result: head", data63_processed, 32);
183 if (memcmp(enc63_etl_head, data63_processed, sizeof(enc63_etl_head)) != 0) {
184 fprintf(stderr, "ENC63 mismatch: head");
187 hexdump(stderr, "ENC63 result: tail", data63_processed+4096+16-48, 48);
188 if (memcmp(enc63_etl_tail, data63_processed+4096+16-48, sizeof(enc63_etl_tail)) != 0) {
189 fprintf(stderr, "ENC63 mismatch: tail");