]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - test_context.c
Merge pull request #83 from vt-alt/in-places
[openssl-gost/engine.git] / test_context.c
1 /*
2  * Copyright (C) 2018 vt@altlinux.org. All Rights Reserved.
3  *
4  * Contents licensed under the terms of the OpenSSL license
5  * See https://www.openssl.org/source/license.html for details
6  */
7
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"
13 #include "gost_lcl.h"
14 #include <openssl/evp.h>
15 #include <openssl/rand.h>
16 #include <openssl/err.h>
17 #include <openssl/asn1.h>
18 #include <string.h>
19
20 #define T(e) if (!(e)) {\
21         ERR_print_errors_fp(stderr);\
22         OpenSSLDie(__FILE__, __LINE__, #e);\
23     }
24
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); \
34              else \
35                  printf(cGREEN "  Test passed\n" cNORM);}
36
37 static void hexdump(const void *ptr, size_t len)
38 {
39     const unsigned char *p = ptr;
40     size_t i, j;
41
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]);
45     }
46     printf("\n");
47 }
48
49 #define TEST_SIZE 256
50 #define STEP_SIZE 16
51
52 static int test_contexts(const EVP_CIPHER *type, const int enc, const char *msg,
53     int acpkm)
54 {
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};
61     int outlen, tmplen;
62     int ret = 0, test = 0;
63
64     printf(cBLUE "%s test for %s\n" cNORM, enc ? "Encryption" : "Decryption", msg);
65
66     /* produce base encryption */
67     ctx = EVP_CIPHER_CTX_new();
68     T(ctx);
69     T(EVP_CipherInit_ex(ctx, type, NULL, K, iv, enc));
70     if (acpkm)
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));
75
76     /* and now tests */
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));
82     if (acpkm)
83         T(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_KEY_MESH, acpkm, NULL));
84
85     save = ctx;
86     int i;
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();
90         T(copy);
91         T(EVP_CIPHER_CTX_copy(copy, ctx));
92         if (save != ctx) /* else original context */
93             EVP_CIPHER_CTX_free(ctx);
94         ctx = copy;
95
96         T(EVP_CipherUpdate(ctx, c + STEP_SIZE * i, &outlen,
97                 pt + STEP_SIZE * i, STEP_SIZE));
98     }
99
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);
104     if (test) {
105         printf("  b[%d] = ", outlen);
106         hexdump(b, outlen);
107         printf("  c[%d] = ", outlen);
108         hexdump(c, outlen);
109     }
110     ret |= test;
111
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);
121     if (test) {
122         printf("  b[%d] = ", outlen);
123         hexdump(b, outlen);
124         printf("  c[%d] = ", outlen);
125         hexdump(c, outlen);
126     }
127     ret |= test;
128
129     return ret;
130 }
131
132
133 int main(int argc, char **argv)
134 {
135     int ret = 0;
136
137     test_contexts(cipher_gost_grasshopper_ecb(), 1, "grasshopper ecb", 0);
138     test_contexts(cipher_gost_grasshopper_ecb(), 0, "grasshopper ecb", 0);
139     test_contexts(cipher_gost_grasshopper_cbc(), 1, "grasshopper cbc", 0);
140     test_contexts(cipher_gost_grasshopper_cbc(), 0, "grasshopper cbc", 0);
141     test_contexts(cipher_gost_grasshopper_ctr(), 1, "grasshopper ctr", 0);
142     test_contexts(cipher_gost_grasshopper_ctr(), 0, "grasshopper ctr", 0);
143     test_contexts(cipher_gost_grasshopper_ofb(), 1, "grasshopper ofb", 0);
144     test_contexts(cipher_gost_grasshopper_ofb(), 0, "grasshopper ofb", 0);
145     test_contexts(cipher_gost_grasshopper_ctracpkm(), 1, "grasshopper ctracpkm", 256 / 8);
146     test_contexts(cipher_gost_grasshopper_ctracpkm(), 0, "grasshopper ctracpkm", 256 / 8);
147
148     if (ret)
149         printf(cDRED "= Some tests FAILED!\n" cNORM);
150     else
151         printf(cDGREEN "= All tests passed!\n" cNORM);
152     return ret;
153 }