xref: /netbsd-src/crypto/external/bsd/openssl/dist/test/namemap_internal_test.c (revision b0d1725196a7921d003d2c66a14f186abda4176b)
1*b0d17251Schristos /*
2*b0d17251Schristos  * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b0d17251Schristos  *
4*b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b0d17251Schristos  * this file except in compliance with the License.  You can obtain a copy
6*b0d17251Schristos  * in the file LICENSE in the source distribution or at
7*b0d17251Schristos  * https://www.openssl.org/source/license.html
8*b0d17251Schristos  */
9*b0d17251Schristos 
10*b0d17251Schristos #include <openssl/evp.h>
11*b0d17251Schristos #include "internal/namemap.h"
12*b0d17251Schristos #include "testutil.h"
13*b0d17251Schristos 
14*b0d17251Schristos #define NAME1 "name1"
15*b0d17251Schristos #define NAME2 "name2"
16*b0d17251Schristos #define ALIAS1 "alias1"
17*b0d17251Schristos #define ALIAS1_UC "ALIAS1"
18*b0d17251Schristos 
test_namemap_empty(void)19*b0d17251Schristos static int test_namemap_empty(void)
20*b0d17251Schristos {
21*b0d17251Schristos     OSSL_NAMEMAP *nm = NULL;
22*b0d17251Schristos     int ok;
23*b0d17251Schristos 
24*b0d17251Schristos     ok = TEST_int_eq(ossl_namemap_empty(NULL), 1)
25*b0d17251Schristos          && TEST_ptr(nm = ossl_namemap_new())
26*b0d17251Schristos          && TEST_int_eq(ossl_namemap_empty(nm), 1)
27*b0d17251Schristos          && TEST_int_ne(ossl_namemap_add_name(nm, 0, NAME1), 0)
28*b0d17251Schristos          && TEST_int_eq(ossl_namemap_empty(nm), 0);
29*b0d17251Schristos     ossl_namemap_free(nm);
30*b0d17251Schristos     return ok;
31*b0d17251Schristos }
32*b0d17251Schristos 
test_namemap(OSSL_NAMEMAP * nm)33*b0d17251Schristos static int test_namemap(OSSL_NAMEMAP *nm)
34*b0d17251Schristos {
35*b0d17251Schristos     int num1 = ossl_namemap_add_name(nm, 0, NAME1);
36*b0d17251Schristos     int num2 = ossl_namemap_add_name(nm, 0, NAME2);
37*b0d17251Schristos     int num3 = ossl_namemap_add_name(nm, num1, ALIAS1);
38*b0d17251Schristos     int num4 = ossl_namemap_add_name(nm, 0, ALIAS1_UC);
39*b0d17251Schristos     int check1 = ossl_namemap_name2num(nm, NAME1);
40*b0d17251Schristos     int check2 = ossl_namemap_name2num(nm, NAME2);
41*b0d17251Schristos     int check3 = ossl_namemap_name2num(nm, ALIAS1);
42*b0d17251Schristos     int check4 = ossl_namemap_name2num(nm, ALIAS1_UC);
43*b0d17251Schristos     int false1 = ossl_namemap_name2num(nm, "cookie");
44*b0d17251Schristos 
45*b0d17251Schristos     return TEST_int_ne(num1, 0)
46*b0d17251Schristos         && TEST_int_ne(num2, 0)
47*b0d17251Schristos         && TEST_int_eq(num1, num3)
48*b0d17251Schristos         && TEST_int_eq(num3, num4)
49*b0d17251Schristos         && TEST_int_eq(num1, check1)
50*b0d17251Schristos         && TEST_int_eq(num2, check2)
51*b0d17251Schristos         && TEST_int_eq(num3, check3)
52*b0d17251Schristos         && TEST_int_eq(num4, check4)
53*b0d17251Schristos         && TEST_int_eq(false1, 0);
54*b0d17251Schristos }
55*b0d17251Schristos 
test_namemap_independent(void)56*b0d17251Schristos static int test_namemap_independent(void)
57*b0d17251Schristos {
58*b0d17251Schristos     OSSL_NAMEMAP *nm = ossl_namemap_new();
59*b0d17251Schristos     int ok = TEST_ptr(nm) && test_namemap(nm);
60*b0d17251Schristos 
61*b0d17251Schristos     ossl_namemap_free(nm);
62*b0d17251Schristos     return ok;
63*b0d17251Schristos }
64*b0d17251Schristos 
test_namemap_stored(void)65*b0d17251Schristos static int test_namemap_stored(void)
66*b0d17251Schristos {
67*b0d17251Schristos     OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL);
68*b0d17251Schristos 
69*b0d17251Schristos     return TEST_ptr(nm)
70*b0d17251Schristos         && test_namemap(nm);
71*b0d17251Schristos }
72*b0d17251Schristos 
73*b0d17251Schristos /*
74*b0d17251Schristos  * Test that EVP_get_digestbyname() will use the namemap when it can't find
75*b0d17251Schristos  * entries in the legacy method database.
76*b0d17251Schristos  */
test_digestbyname(void)77*b0d17251Schristos static int test_digestbyname(void)
78*b0d17251Schristos {
79*b0d17251Schristos     int id;
80*b0d17251Schristos     OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL);
81*b0d17251Schristos     const EVP_MD *sha256, *foo;
82*b0d17251Schristos 
83*b0d17251Schristos     if (!TEST_ptr(nm))
84*b0d17251Schristos         return 0;
85*b0d17251Schristos     id = ossl_namemap_add_name(nm, 0, "SHA256");
86*b0d17251Schristos     if (!TEST_int_ne(id, 0))
87*b0d17251Schristos         return 0;
88*b0d17251Schristos     if (!TEST_int_eq(ossl_namemap_add_name(nm, id, "foo"), id))
89*b0d17251Schristos         return 0;
90*b0d17251Schristos 
91*b0d17251Schristos     sha256 = EVP_get_digestbyname("SHA256");
92*b0d17251Schristos     if (!TEST_ptr(sha256))
93*b0d17251Schristos         return 0;
94*b0d17251Schristos     foo = EVP_get_digestbyname("foo");
95*b0d17251Schristos     if (!TEST_ptr_eq(sha256, foo))
96*b0d17251Schristos         return 0;
97*b0d17251Schristos 
98*b0d17251Schristos     return 1;
99*b0d17251Schristos }
100*b0d17251Schristos 
101*b0d17251Schristos /*
102*b0d17251Schristos  * Test that EVP_get_cipherbyname() will use the namemap when it can't find
103*b0d17251Schristos  * entries in the legacy method database.
104*b0d17251Schristos  */
test_cipherbyname(void)105*b0d17251Schristos static int test_cipherbyname(void)
106*b0d17251Schristos {
107*b0d17251Schristos     int id;
108*b0d17251Schristos     OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL);
109*b0d17251Schristos     const EVP_CIPHER *aes128, *bar;
110*b0d17251Schristos 
111*b0d17251Schristos     if (!TEST_ptr(nm))
112*b0d17251Schristos         return 0;
113*b0d17251Schristos     id = ossl_namemap_add_name(nm, 0, "AES-128-CBC");
114*b0d17251Schristos     if (!TEST_int_ne(id, 0))
115*b0d17251Schristos         return 0;
116*b0d17251Schristos     if (!TEST_int_eq(ossl_namemap_add_name(nm, id, "bar"), id))
117*b0d17251Schristos         return 0;
118*b0d17251Schristos 
119*b0d17251Schristos     aes128 = EVP_get_cipherbyname("AES-128-CBC");
120*b0d17251Schristos     if (!TEST_ptr(aes128))
121*b0d17251Schristos         return 0;
122*b0d17251Schristos     bar = EVP_get_cipherbyname("bar");
123*b0d17251Schristos     if (!TEST_ptr_eq(aes128, bar))
124*b0d17251Schristos         return 0;
125*b0d17251Schristos 
126*b0d17251Schristos     return 1;
127*b0d17251Schristos }
128*b0d17251Schristos 
129*b0d17251Schristos /*
130*b0d17251Schristos  * Test that EVP_CIPHER_is_a() responds appropriately, even for ciphers that
131*b0d17251Schristos  * are entirely legacy.
132*b0d17251Schristos  */
test_cipher_is_a(void)133*b0d17251Schristos static int test_cipher_is_a(void)
134*b0d17251Schristos {
135*b0d17251Schristos     EVP_CIPHER *fetched = EVP_CIPHER_fetch(NULL, "AES-256-CCM", NULL);
136*b0d17251Schristos     int rv = 1;
137*b0d17251Schristos 
138*b0d17251Schristos     if (!TEST_ptr(fetched))
139*b0d17251Schristos         return 0;
140*b0d17251Schristos     if (!TEST_true(EVP_CIPHER_is_a(fetched, "id-aes256-CCM"))
141*b0d17251Schristos         || !TEST_false(EVP_CIPHER_is_a(fetched, "AES-128-GCM")))
142*b0d17251Schristos         rv = 0;
143*b0d17251Schristos     if (!TEST_true(EVP_CIPHER_is_a(EVP_aes_256_gcm(), "AES-256-GCM"))
144*b0d17251Schristos         || !TEST_false(EVP_CIPHER_is_a(EVP_aes_256_gcm(), "AES-128-CCM")))
145*b0d17251Schristos         rv = 0;
146*b0d17251Schristos 
147*b0d17251Schristos     EVP_CIPHER_free(fetched);
148*b0d17251Schristos     return rv;
149*b0d17251Schristos }
150*b0d17251Schristos 
151*b0d17251Schristos /*
152*b0d17251Schristos  * Test that EVP_MD_is_a() responds appropriately, even for MDs that are
153*b0d17251Schristos  * entirely legacy.
154*b0d17251Schristos  */
test_digest_is_a(void)155*b0d17251Schristos static int test_digest_is_a(void)
156*b0d17251Schristos {
157*b0d17251Schristos     EVP_MD *fetched = EVP_MD_fetch(NULL, "SHA2-512", NULL);
158*b0d17251Schristos     int rv = 1;
159*b0d17251Schristos 
160*b0d17251Schristos     if (!TEST_ptr(fetched))
161*b0d17251Schristos         return 0;
162*b0d17251Schristos     if (!TEST_true(EVP_MD_is_a(fetched, "SHA512"))
163*b0d17251Schristos         || !TEST_false(EVP_MD_is_a(fetched, "SHA1")))
164*b0d17251Schristos         rv = 0;
165*b0d17251Schristos     if (!TEST_true(EVP_MD_is_a(EVP_sha256(), "SHA2-256"))
166*b0d17251Schristos         || !TEST_false(EVP_MD_is_a(EVP_sha256(), "SHA3-256")))
167*b0d17251Schristos         rv = 0;
168*b0d17251Schristos 
169*b0d17251Schristos     EVP_MD_free(fetched);
170*b0d17251Schristos     return rv;
171*b0d17251Schristos }
172*b0d17251Schristos 
setup_tests(void)173*b0d17251Schristos int setup_tests(void)
174*b0d17251Schristos {
175*b0d17251Schristos     ADD_TEST(test_namemap_empty);
176*b0d17251Schristos     ADD_TEST(test_namemap_independent);
177*b0d17251Schristos     ADD_TEST(test_namemap_stored);
178*b0d17251Schristos     ADD_TEST(test_digestbyname);
179*b0d17251Schristos     ADD_TEST(test_cipherbyname);
180*b0d17251Schristos     ADD_TEST(test_digest_is_a);
181*b0d17251Schristos     ADD_TEST(test_cipher_is_a);
182*b0d17251Schristos     return 1;
183*b0d17251Schristos }
184