2 * Copyright (C) 2018 vt@altlinux.org. All Rights Reserved.
4 * Contents licensed under the terms of the OpenSSL license
5 * See https://www.openssl.org/source/license.html for details
8 #include "gost_grasshopper_cipher.h"
9 #include "gost_grasshopper_defines.h"
10 #include "gost_grasshopper_math.h"
11 #include "gost_grasshopper_core.h"
12 #include "e_gost_err.h"
14 #include <openssl/evp.h>
15 #include <openssl/rand.h>
16 #include <openssl/err.h>
17 #include <openssl/asn1.h>
20 #define T(e) if (!(e)) {\
21 ERR_print_errors_fp(stderr);\
22 OpenSSLDie(__FILE__, __LINE__, #e);\
25 #define cRED "\033[1;31m"
26 #define cDRED "\033[0;31m"
27 #define cGREEN "\033[1;32m"
28 #define cDGREEN "\033[0;32m"
29 #define cBLUE "\033[1;34m"
30 #define cDBLUE "\033[0;34m"
31 #define cNORM "\033[m"
32 #define TEST_ASSERT(e) {if ((test = (e))) \
33 printf(cRED " Test FAILED\n" cNORM); \
35 printf(cGREEN " Test passed\n" cNORM);}
37 static void hexdump(const void *ptr, size_t len)
39 const unsigned char *p = ptr;
42 for (i = 0; i < len; i += j) {
43 for (j = 0; j < 16 && i + j < len; j++)
44 printf("%s%02x", j? "" : " ", p[i + j]);
52 static int test_contexts(const EVP_CIPHER *type, const int enc, const char *msg,
55 EVP_CIPHER_CTX *ctx, *save;
56 unsigned char pt[TEST_SIZE] = {1};
57 unsigned char b[TEST_SIZE];
58 unsigned char c[TEST_SIZE];
59 unsigned char K[32] = {1};
60 unsigned char iv[16] = {1};
62 int ret = 0, test = 0;
64 printf(cBLUE "%s test for %s\n" cNORM, enc ? "Encryption" : "Decryption", msg);
66 /* produce base encryption */
67 ctx = EVP_CIPHER_CTX_new();
69 T(EVP_CipherInit_ex(ctx, type, NULL, K, iv, enc));
71 T(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_KEY_MESH, acpkm, NULL));
72 T(EVP_CIPHER_CTX_set_padding(ctx, 0));
73 T(EVP_CipherUpdate(ctx, b, &outlen, pt, sizeof(b)));
74 T(EVP_CipherFinal_ex(ctx, b + outlen, &tmplen));
77 printf(" cloned contexts\n");
78 EVP_CIPHER_CTX_reset(ctx);
79 EVP_CIPHER_CTX_reset(ctx); /* double call is intentional */
80 T(EVP_CipherInit_ex(ctx, type, NULL, K, iv, enc));
81 T(EVP_CIPHER_CTX_set_padding(ctx, 0));
83 T(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_KEY_MESH, acpkm, NULL));
87 memset(c, 0, sizeof(c));
88 for (i = 0; i < TEST_SIZE / STEP_SIZE; i++) {
89 EVP_CIPHER_CTX *copy = EVP_CIPHER_CTX_new();
91 T(EVP_CIPHER_CTX_copy(copy, ctx));
92 if (save != ctx) /* else original context */
93 EVP_CIPHER_CTX_free(ctx);
96 T(EVP_CipherUpdate(ctx, c + STEP_SIZE * i, &outlen,
97 pt + STEP_SIZE * i, STEP_SIZE));
100 outlen = i * GRASSHOPPER_BLOCK_SIZE;
101 T(EVP_CipherFinal_ex(ctx, c + outlen, &tmplen));
102 TEST_ASSERT(outlen != TEST_SIZE || memcmp(c, b, TEST_SIZE));
103 EVP_CIPHER_CTX_free(ctx);
105 printf(" b[%d] = ", outlen);
107 printf(" c[%d] = ", outlen);
112 /* resume original context */
113 printf(" base context\n");
114 memset(c, 0, sizeof(c));
115 T(EVP_CipherUpdate(save, c, &outlen, pt, sizeof(c)));
116 T(EVP_CipherFinal_ex(save, c + outlen, &tmplen));
117 TEST_ASSERT(outlen != TEST_SIZE || memcmp(c, b, TEST_SIZE));
118 EVP_CIPHER_CTX_cleanup(save); /* multiple calls are intentional */
119 EVP_CIPHER_CTX_cleanup(save);
120 EVP_CIPHER_CTX_free(save);
122 printf(" b[%d] = ", outlen);
124 printf(" c[%d] = ", outlen);
133 int main(int argc, char **argv)
137 ret |= test_contexts(cipher_gost_grasshopper_ecb(), 1, "grasshopper ecb", 0);
138 ret |= test_contexts(cipher_gost_grasshopper_ecb(), 0, "grasshopper ecb", 0);
139 ret |= test_contexts(cipher_gost_grasshopper_cbc(), 1, "grasshopper cbc", 0);
140 ret |= test_contexts(cipher_gost_grasshopper_cbc(), 0, "grasshopper cbc", 0);
141 ret |= test_contexts(cipher_gost_grasshopper_ctr(), 1, "grasshopper ctr", 0);
142 ret |= test_contexts(cipher_gost_grasshopper_ctr(), 0, "grasshopper ctr", 0);
143 ret |= test_contexts(cipher_gost_grasshopper_ofb(), 1, "grasshopper ofb", 0);
144 ret |= test_contexts(cipher_gost_grasshopper_ofb(), 0, "grasshopper ofb", 0);
145 ret |= test_contexts(cipher_gost_grasshopper_ctracpkm(), 1, "grasshopper ctracpkm", 256 / 8);
146 ret |= test_contexts(cipher_gost_grasshopper_ctracpkm(), 0, "grasshopper ctracpkm", 256 / 8);
149 printf(cDRED "= Some tests FAILED!\n" cNORM);
151 printf(cDGREEN "= All tests passed!\n" cNORM);