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
9 # include <openssl/err.h>
10 # include <openssl/evp.h>
12 static void hexdump(FILE *f, const char *title, const unsigned char *s, int l)
16 fprintf(f, "%s", title);
19 fprintf(f, "\n%04x", n);
20 fprintf(f, " %02x", s[n]);
27 #ifdef EVP_MD_CTRL_TLSTREE
28 const unsigned char mac_secret[] = {
29 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
30 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
33 const unsigned char enc_key[] = {
34 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
35 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
38 const unsigned char full_iv[] = {
39 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44 unsigned char seq0[] = {
45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48 const unsigned char rec0_header[] = {
49 0x17, 0x03, 0x03, 0x00, 0x0F
52 const unsigned char data0[15] = {
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
56 const unsigned char mac0_etl[16] = {
57 0x75, 0x53, 0x09, 0xCB, 0xC7, 0x3B, 0xB9, 0x49, 0xC5, 0x0E, 0xBB, 0x86, 0x16, 0x0A, 0x0F, 0xEE
60 const unsigned char enc0_etl[31] = {
61 0xf3, 0x17, 0xa7, 0x1d, 0x3a, 0xce, 0x43, 0x3b, 0x01, 0xd4, 0xe7, 0xd4, 0xef, 0x61, 0xae, 0x00,
62 0xd5, 0x3b, 0x41, 0x52, 0x7a, 0x26, 0x1e, 0xdf, 0xc2, 0xba, 0x78, 0x57, 0xc1, 0x93, 0x2d
65 unsigned char data0_processed[31];
66 unsigned char mac0[16];
68 unsigned char seq63[] = {
69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F,
72 const unsigned char rec63_header[] = {
73 0x17, 0x03, 0x03, 0x10, 0x00
76 unsigned char data63[4096];
78 const unsigned char mac63_etl[16] = {
79 0x0A, 0x3B, 0xFD, 0x43, 0x0F, 0xCD, 0xD8, 0xD8, 0x5C, 0x96, 0x46, 0x86, 0x81, 0x78, 0x4F, 0x7D
82 const unsigned char enc63_etl_head[32] = {
83 0x6A, 0x18, 0x38, 0xB0, 0xA0, 0xD5, 0xA0, 0x4D, 0x1F, 0x29, 0x64, 0x89, 0x6D, 0x08, 0x5F, 0xB7,
84 0xDA, 0x84, 0xD7, 0x76, 0xC3, 0x9F, 0x5C, 0xDC, 0x37, 0x20, 0xB7, 0xB5, 0x59, 0xEF, 0x13, 0x9D
86 const unsigned char enc63_etl_tail[48] = {
87 0x0A, 0x81, 0x29, 0x9B, 0x35, 0x98, 0x19, 0x5D, 0xD4, 0x51, 0x68, 0xA6, 0x38, 0x50, 0xA7, 0x6E,
88 0x1A, 0x4F, 0x1E, 0x6D, 0xD5, 0xEF, 0x72, 0x59, 0x3F, 0xAE, 0x76, 0x55, 0x71, 0xEC, 0x37, 0xE7,
89 0x17, 0xF5, 0xB8, 0x62, 0x85, 0xBB, 0x5B, 0xFD, 0x83, 0xB6, 0x6A, 0xB7, 0x63, 0x86, 0x52, 0x08
92 unsigned char data63_processed[4096+16];
93 unsigned char mac63[16];
95 EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
96 EVP_CIPHER_CTX *enc = NULL;
98 const EVP_CIPHER *ciph;
103 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
105 memset(data63, 0, 4096);
107 md = EVP_get_digestbynid(NID_grasshopper_mac);
109 EVP_DigestInit_ex(mdctx, md, NULL);
110 mac_key = EVP_PKEY_new_mac_key(NID_grasshopper_mac, NULL, mac_secret, 32);
111 EVP_DigestSignInit(mdctx, NULL, md, NULL, mac_key);
112 EVP_PKEY_free(mac_key);
114 EVP_MD_CTX_ctrl(mdctx, EVP_MD_CTRL_TLSTREE, 0, seq0);
115 EVP_DigestSignUpdate(mdctx, seq0, 8);
116 EVP_DigestSignUpdate(mdctx, rec0_header, 5);
117 EVP_DigestSignUpdate(mdctx, data0, 15);
118 EVP_DigestSignFinal(mdctx, mac0, &mac_len);
120 EVP_MD_CTX_free(mdctx);
121 hexdump(stderr, "MAC0 result", mac0, mac_len);
122 if (memcmp(mac0, mac0_etl, 16) != 0) {
123 fprintf(stderr, "MAC0 mismatch");
127 ciph = EVP_get_cipherbynid(NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm);
128 enc = EVP_CIPHER_CTX_new();
129 EVP_EncryptInit_ex(enc, ciph, NULL, enc_key, full_iv);
131 for (i = 7; i >= 0; i--) {
136 EVP_CIPHER_CTX_ctrl(enc, EVP_CTRL_TLSTREE, 0, seq0);
137 EVP_Cipher(enc, data0_processed, data0, sizeof(data0));
138 EVP_Cipher(enc, data0_processed+sizeof(data0), mac0, 16);
140 hexdump(stderr, "ENC0 result", data0_processed, 31);
141 if (memcmp(enc0_etl, data0_processed, sizeof(data0_processed)) != 0) {
142 fprintf(stderr, "ENC0 mismatch");
146 mdctx = EVP_MD_CTX_new();
147 EVP_DigestInit_ex(mdctx, md, NULL);
148 mac_key = EVP_PKEY_new_mac_key(NID_grasshopper_mac, NULL, mac_secret, 32);
149 EVP_DigestSignInit(mdctx, NULL, md, NULL, mac_key);
150 EVP_PKEY_free(mac_key);
152 EVP_MD_CTX_ctrl(mdctx, EVP_MD_CTRL_TLSTREE, 0, seq63);
153 EVP_DigestSignUpdate(mdctx, seq63, 8);
154 EVP_DigestSignUpdate(mdctx, rec63_header, 5);
155 EVP_DigestSignUpdate(mdctx, data63, 4096);
156 EVP_DigestSignFinal(mdctx, mac63, &mac_len);
158 EVP_MD_CTX_free(mdctx);
159 hexdump(stderr, "MAC63 result", mac63, mac_len);
160 if (memcmp(mac63, mac63_etl, 16) != 0) {
161 fprintf(stderr, "MAC63 mismatch");
165 for (i = 7; i >= 0; i--) {
170 EVP_CIPHER_CTX_ctrl(enc, EVP_CTRL_TLSTREE, 0, seq63);
171 EVP_Cipher(enc, data63_processed, data63, sizeof(data63));
172 EVP_Cipher(enc, data63_processed+sizeof(data63), mac63, 16);
174 hexdump(stderr, "ENC63 result: head", data63_processed, 32);
175 if (memcmp(enc63_etl_head, data63_processed, sizeof(enc63_etl_head)) != 0) {
176 fprintf(stderr, "ENC63 mismatch: head");
179 hexdump(stderr, "ENC63 result: tail", data63_processed+4096+16-48, 48);
180 if (memcmp(enc63_etl_tail, data63_processed+4096+16-48, sizeof(enc63_etl_tail)) != 0) {
181 fprintf(stderr, "ENC63 mismatch: tail");