summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/evp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/evp')
-rw-r--r--src/lib/libcrypto/evp/evp_names.c162
-rw-r--r--src/lib/libcrypto/evp/names.c93
2 files changed, 160 insertions, 95 deletions
diff --git a/src/lib/libcrypto/evp/evp_names.c b/src/lib/libcrypto/evp/evp_names.c
index fbd84711f5..b5d3b95b45 100644
--- a/src/lib/libcrypto/evp/evp_names.c
+++ b/src/lib/libcrypto/evp/evp_names.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: evp_names.c,v 1.2 2024/01/13 11:00:09 tb Exp $ */ 1/* $OpenBSD: evp_names.c,v 1.3 2024/01/13 11:08:39 tb Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Theo Buehler <tb@openbsd.org> 3 * Copyright (c) 2023 Theo Buehler <tb@openbsd.org>
4 * 4 *
@@ -41,7 +41,7 @@ struct digest_name {
41 * regress/lib/libcrypto/evp/evp_test.c checks that. 41 * regress/lib/libcrypto/evp/evp_test.c checks that.
42 */ 42 */
43 43
44const struct cipher_name cipher_names[] = { 44static const struct cipher_name cipher_names[] = {
45#ifndef OPENSSL_NO_AES 45#ifndef OPENSSL_NO_AES
46 { 46 {
47 .name = SN_aes_128_cbc, 47 .name = SN_aes_128_cbc,
@@ -1099,7 +1099,7 @@ const struct cipher_name cipher_names[] = {
1099 * regresss/lib/libcrypto/evp/evp_test.c checks that. 1099 * regresss/lib/libcrypto/evp/evp_test.c checks that.
1100 */ 1100 */
1101 1101
1102const struct digest_name digest_names[] = { 1102static const struct digest_name digest_names[] = {
1103#ifndef OPENSSL_NO_GOST 1103#ifndef OPENSSL_NO_GOST
1104 { 1104 {
1105 .name = LN_id_Gost28147_89_MAC, 1105 .name = LN_id_Gost28147_89_MAC,
@@ -1559,3 +1559,159 @@ const struct digest_name digest_names[] = {
1559}; 1559};
1560 1560
1561#define N_DIGEST_NAMES (sizeof(digest_names) / sizeof(digest_names[0])) 1561#define N_DIGEST_NAMES (sizeof(digest_names) / sizeof(digest_names[0]))
1562
1563void
1564EVP_CIPHER_do_all_sorted(void (*fn)(const EVP_CIPHER *, const char *,
1565 const char *, void *), void *arg)
1566{
1567 size_t i;
1568
1569 /* Prayer and clean living lets you ignore errors, OpenSSL style. */
1570 (void)OPENSSL_init_crypto(0, NULL);
1571
1572 for (i = 0; i < N_CIPHER_NAMES; i++) {
1573 const struct cipher_name *cipher = &cipher_names[i];
1574 const EVP_CIPHER *evp_cipher;
1575
1576 if ((evp_cipher = cipher->cipher()) == NULL)
1577 continue;
1578
1579 if (cipher->alias != NULL)
1580 fn(NULL, cipher->name, cipher->alias, arg);
1581 else
1582 fn(evp_cipher, cipher->name, NULL, arg);
1583 }
1584}
1585
1586void
1587EVP_CIPHER_do_all(void (*fn)(const EVP_CIPHER *, const char *, const char *,
1588 void *), void *arg)
1589{
1590 EVP_CIPHER_do_all_sorted(fn, arg);
1591}
1592
1593void
1594EVP_MD_do_all_sorted(void (*fn)(const EVP_MD *, const char *, const char *,
1595 void *), void *arg)
1596{
1597 size_t i;
1598
1599 /* Prayer and clean living lets you ignore errors, OpenSSL style. */
1600 (void)OPENSSL_init_crypto(0, NULL);
1601
1602 for (i = 0; i < N_DIGEST_NAMES; i++) {
1603 const struct digest_name *digest = &digest_names[i];
1604 const EVP_MD *evp_md;
1605
1606 if ((evp_md = digest->digest()) == NULL)
1607 continue;
1608
1609 if (digest->alias != NULL)
1610 fn(NULL, digest->name, digest->alias, arg);
1611 else
1612 fn(evp_md, digest->name, NULL, arg);
1613 }
1614}
1615
1616void
1617EVP_MD_do_all(void (*fn)(const EVP_MD *, const char *, const char *, void *),
1618 void *arg)
1619{
1620 EVP_MD_do_all_sorted(fn, arg);
1621}
1622
1623/*
1624 * The OBJ_NAME API is completely misnamed. It has little to do with objects
1625 * and a lot to do with EVP. Therefore we implement a saner replacement for
1626 * the part of the old madness that we need to keep in the evp directory.
1627 */
1628
1629static int
1630OBJ_NAME_from_cipher_name(OBJ_NAME *obj_name, const struct cipher_name *cipher)
1631{
1632 const EVP_CIPHER *evp_cipher;
1633
1634 if ((evp_cipher = cipher->cipher()) == NULL)
1635 return 0;
1636
1637 obj_name->type = OBJ_NAME_TYPE_CIPHER_METH;
1638 obj_name->name = cipher->name;
1639 if (cipher->alias != NULL) {
1640 obj_name->alias = OBJ_NAME_ALIAS;
1641 obj_name->data = cipher->alias;
1642 } else {
1643 obj_name->alias = 0;
1644 obj_name->data = (const char *)evp_cipher;
1645 }
1646
1647 return 1;
1648}
1649
1650static void
1651OBJ_NAME_do_all_ciphers(void (*fn)(const OBJ_NAME *, void *), void *arg)
1652{
1653 size_t i;
1654
1655 for (i = 0; i < N_CIPHER_NAMES; i++) {
1656 const struct cipher_name *cipher = &cipher_names[i];
1657 OBJ_NAME name;
1658
1659 if (OBJ_NAME_from_cipher_name(&name, cipher))
1660 fn(&name, arg);
1661 }
1662}
1663
1664static int
1665OBJ_NAME_from_digest_name(OBJ_NAME *obj_name, const struct digest_name *digest)
1666{
1667 const EVP_MD *evp_md;
1668
1669 if ((evp_md = digest->digest()) == NULL)
1670 return 0;
1671
1672 obj_name->type = OBJ_NAME_TYPE_MD_METH;
1673 obj_name->name = digest->name;
1674 if (digest->alias != NULL) {
1675 obj_name->alias = OBJ_NAME_ALIAS;
1676 obj_name->data = digest->alias;
1677 } else {
1678 obj_name->alias = 0;
1679 obj_name->data = (const char *)evp_md;
1680 }
1681
1682 return 1;
1683}
1684
1685static void
1686OBJ_NAME_do_all_digests(void (*fn)(const OBJ_NAME *, void *), void *arg)
1687{
1688 size_t i;
1689
1690 for (i = 0; i < N_DIGEST_NAMES; i++) {
1691 const struct digest_name *digest = &digest_names[i];
1692 OBJ_NAME name;
1693
1694 if (OBJ_NAME_from_digest_name(&name, digest))
1695 fn(&name, arg);
1696 }
1697}
1698
1699void
1700OBJ_NAME_do_all_sorted(int type, void (*fn)(const OBJ_NAME *, void *), void *arg)
1701{
1702 /* Prayer and clean living lets you ignore errors, OpenSSL style. */
1703 (void)OPENSSL_init_crypto(0, NULL);
1704
1705 if (type == OBJ_NAME_TYPE_CIPHER_METH)
1706 OBJ_NAME_do_all_ciphers(fn, arg);
1707 if (type == OBJ_NAME_TYPE_MD_METH)
1708 OBJ_NAME_do_all_digests(fn, arg);
1709}
1710LCRYPTO_ALIAS(OBJ_NAME_do_all_sorted);
1711
1712void
1713OBJ_NAME_do_all(int type, void (*fn)(const OBJ_NAME *, void *), void *arg)
1714{
1715 OBJ_NAME_do_all_sorted(type, fn, arg);
1716}
1717LCRYPTO_ALIAS(OBJ_NAME_do_all);
diff --git a/src/lib/libcrypto/evp/names.c b/src/lib/libcrypto/evp/names.c
index a96301ed56..7f5b455088 100644
--- a/src/lib/libcrypto/evp/names.c
+++ b/src/lib/libcrypto/evp/names.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: names.c,v 1.22 2023/12/15 14:22:10 tb Exp $ */ 1/* $OpenBSD: names.c,v 1.23 2024/01/13 11:08:39 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -146,94 +146,3 @@ EVP_cleanup(void)
146 OBJ_cleanup(); 146 OBJ_cleanup();
147 } 147 }
148} 148}
149
150struct doall_cipher {
151 void *arg;
152 void (*fn)(const EVP_CIPHER *ciph, const char *from, const char *to,
153 void *arg);
154};
155
156static void
157do_all_cipher_fn(const OBJ_NAME *nm, void *arg)
158{
159 struct doall_cipher *dc = arg;
160
161 if (nm->alias)
162 dc->fn(NULL, nm->name, nm->data, dc->arg);
163 else
164 dc->fn((const EVP_CIPHER *)nm->data, nm->name, NULL, dc->arg);
165}
166
167void
168EVP_CIPHER_do_all(void (*fn)(const EVP_CIPHER *ciph, const char *from,
169 const char *to, void *x), void *arg)
170{
171 struct doall_cipher dc;
172
173 /* Prayer and clean living lets you ignore errors, OpenSSL style */
174 (void) OPENSSL_init_crypto(0, NULL);
175
176 dc.fn = fn;
177 dc.arg = arg;
178 OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);
179}
180
181void
182EVP_CIPHER_do_all_sorted(void (*fn)(const EVP_CIPHER *ciph, const char *from,
183 const char *to, void *x), void *arg)
184{
185 struct doall_cipher dc;
186
187 /* Prayer and clean living lets you ignore errors, OpenSSL style */
188 (void) OPENSSL_init_crypto(0, NULL);
189
190 dc.fn = fn;
191 dc.arg = arg;
192 OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH,
193 do_all_cipher_fn, &dc);
194}
195
196struct doall_md {
197 void *arg;
198 void (*fn)(const EVP_MD *ciph, const char *from, const char *to,
199 void *arg);
200};
201
202static void
203do_all_md_fn(const OBJ_NAME *nm, void *arg)
204{
205 struct doall_md *dc = arg;
206
207 if (nm->alias)
208 dc->fn(NULL, nm->name, nm->data, dc->arg);
209 else
210 dc->fn((const EVP_MD *)nm->data, nm->name, NULL, dc->arg);
211}
212
213void
214EVP_MD_do_all(void (*fn)(const EVP_MD *md, const char *from, const char *to,
215 void *x), void *arg)
216{
217 struct doall_md dc;
218
219 /* Prayer and clean living lets you ignore errors, OpenSSL style */
220 (void) OPENSSL_init_crypto(0, NULL);
221
222 dc.fn = fn;
223 dc.arg = arg;
224 OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
225}
226
227void
228EVP_MD_do_all_sorted(void (*fn)(const EVP_MD *md,
229 const char *from, const char *to, void *x), void *arg)
230{
231 struct doall_md dc;
232
233 /* Prayer and clean living lets you ignore errors, OpenSSL style */
234 (void) OPENSSL_init_crypto(0, NULL);
235
236 dc.fn = fn;
237 dc.arg = arg;
238 OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
239}