]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/names.c
Implement EVP_MAC_do_all_ex()
[thirdparty/openssl.git] / crypto / evp / names.c
CommitLineData
62867571 1/*
567db2c1 2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
4a8b0c55 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
d02b48c6
RE
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
ec577822 12#include <openssl/evp.h>
d2ba8123 13#include <openssl/kdf.h>
176db6dc 14#include "internal/objects.h"
ec577822 15#include <openssl/x509.h>
ab0a14bb 16#include "internal/evp_int.h"
d02b48c6 17
13588350 18int EVP_add_cipher(const EVP_CIPHER *c)
0f113f3e
MC
19{
20 int r;
21
22 if (c == NULL)
23 return 0;
24
25 r = OBJ_NAME_add(OBJ_nid2sn(c->nid), OBJ_NAME_TYPE_CIPHER_METH,
26 (const char *)c);
27 if (r == 0)
26a7d938 28 return 0;
0f113f3e
MC
29 r = OBJ_NAME_add(OBJ_nid2ln(c->nid), OBJ_NAME_TYPE_CIPHER_METH,
30 (const char *)c);
26a7d938 31 return r;
0f113f3e 32}
5ba4bf35 33
13588350 34int EVP_add_digest(const EVP_MD *md)
0f113f3e
MC
35{
36 int r;
37 const char *name;
38
39 name = OBJ_nid2sn(md->type);
40 r = OBJ_NAME_add(name, OBJ_NAME_TYPE_MD_METH, (const char *)md);
41 if (r == 0)
26a7d938 42 return 0;
0f113f3e
MC
43 r = OBJ_NAME_add(OBJ_nid2ln(md->type), OBJ_NAME_TYPE_MD_METH,
44 (const char *)md);
45 if (r == 0)
26a7d938 46 return 0;
0f113f3e
MC
47
48 if (md->pkey_type && md->type != md->pkey_type) {
49 r = OBJ_NAME_add(OBJ_nid2sn(md->pkey_type),
50 OBJ_NAME_TYPE_MD_METH | OBJ_NAME_ALIAS, name);
51 if (r == 0)
26a7d938 52 return 0;
0f113f3e
MC
53 r = OBJ_NAME_add(OBJ_nid2ln(md->pkey_type),
54 OBJ_NAME_TYPE_MD_METH | OBJ_NAME_ALIAS, name);
55 }
26a7d938 56 return r;
0f113f3e 57}
d02b48c6 58
d2ba8123
SL
59/* TODO(3.0) Is this needed after changing to providers? */
60int EVP_add_kdf(const EVP_KDF *k)
61{
62 int r;
63
64 if (k == NULL)
65 return 0;
66
67 r = OBJ_NAME_add(OBJ_nid2sn(k->type), OBJ_NAME_TYPE_KDF_METH,
68 (const char *)k);
69 if (r == 0)
70 return 0;
71 r = OBJ_NAME_add(OBJ_nid2ln(k->type), OBJ_NAME_TYPE_KDF_METH,
72 (const char *)k);
73 return r;
74}
75
6b691a5c 76const EVP_CIPHER *EVP_get_cipherbyname(const char *name)
0f113f3e
MC
77{
78 const EVP_CIPHER *cp;
d02b48c6 79
0fc32b07
MC
80 if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL))
81 return NULL;
7b9f8f7f 82
0f113f3e 83 cp = (const EVP_CIPHER *)OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
26a7d938 84 return cp;
0f113f3e 85}
d02b48c6 86
6b691a5c 87const EVP_MD *EVP_get_digestbyname(const char *name)
0f113f3e
MC
88{
89 const EVP_MD *cp;
d02b48c6 90
0fc32b07
MC
91 if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL))
92 return NULL;
7b9f8f7f 93
0f113f3e 94 cp = (const EVP_MD *)OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
26a7d938 95 return cp;
0f113f3e 96}
d02b48c6 97
d2ba8123
SL
98/* TODO(3.0) Is this API needed after implementing providers? */
99const EVP_KDF *EVP_get_kdfbyname(const char *name)
100{
101 const EVP_KDF *kdf;
102
103 if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_KDFS, NULL))
104 return NULL;
105
106 kdf = (const EVP_KDF *)OBJ_NAME_get(name, OBJ_NAME_TYPE_KDF_METH);
107 return kdf;
108}
109
b3599dbb 110void evp_cleanup_int(void)
0f113f3e 111{
d2ba8123 112 OBJ_NAME_cleanup(OBJ_NAME_TYPE_KDF_METH);
0f113f3e
MC
113 OBJ_NAME_cleanup(OBJ_NAME_TYPE_CIPHER_METH);
114 OBJ_NAME_cleanup(OBJ_NAME_TYPE_MD_METH);
115 /*
116 * The above calls will only clean out the contents of the name hash
117 * table, but not the hash table itself. The following line does that
118 * part. -- Richard Levitte
119 */
120 OBJ_NAME_cleanup(-1);
121
122 EVP_PBE_cleanup();
0f113f3e 123 OBJ_sigid_free();
0822e89a
PY
124
125 evp_app_cleanup_int();
0f113f3e
MC
126}
127
128struct doall_cipher {
129 void *arg;
130 void (*fn) (const EVP_CIPHER *ciph,
131 const char *from, const char *to, void *arg);
132};
5ba4bf35
DSH
133
134static void do_all_cipher_fn(const OBJ_NAME *nm, void *arg)
0f113f3e
MC
135{
136 struct doall_cipher *dc = arg;
137 if (nm->alias)
138 dc->fn(NULL, nm->name, nm->data, dc->arg);
139 else
140 dc->fn((const EVP_CIPHER *)nm->data, nm->name, NULL, dc->arg);
141}
142
143void EVP_CIPHER_do_all(void (*fn) (const EVP_CIPHER *ciph,
144 const char *from, const char *to, void *x),
145 void *arg)
146{
147 struct doall_cipher dc;
7b9f8f7f 148
0fc32b07 149 /* Ignore errors */
f672aee4 150 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL);
7b9f8f7f 151
0f113f3e
MC
152 dc.fn = fn;
153 dc.arg = arg;
154 OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);
155}
156
157void EVP_CIPHER_do_all_sorted(void (*fn) (const EVP_CIPHER *ciph,
158 const char *from, const char *to,
159 void *x), void *arg)
160{
161 struct doall_cipher dc;
7b9f8f7f 162
0fc32b07 163 /* Ignore errors */
f672aee4 164 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL);
7b9f8f7f 165
0f113f3e
MC
166 dc.fn = fn;
167 dc.arg = arg;
168 OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);
169}
170
171struct doall_md {
172 void *arg;
173 void (*fn) (const EVP_MD *ciph,
174 const char *from, const char *to, void *arg);
175};
5ba4bf35
DSH
176
177static void do_all_md_fn(const OBJ_NAME *nm, void *arg)
0f113f3e
MC
178{
179 struct doall_md *dc = arg;
180 if (nm->alias)
181 dc->fn(NULL, nm->name, nm->data, dc->arg);
182 else
183 dc->fn((const EVP_MD *)nm->data, nm->name, NULL, dc->arg);
184}
185
186void EVP_MD_do_all(void (*fn) (const EVP_MD *md,
187 const char *from, const char *to, void *x),
188 void *arg)
189{
190 struct doall_md dc;
7b9f8f7f 191
0fc32b07 192 /* Ignore errors */
f672aee4 193 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
7b9f8f7f 194
0f113f3e
MC
195 dc.fn = fn;
196 dc.arg = arg;
197 OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
198}
199
200void EVP_MD_do_all_sorted(void (*fn) (const EVP_MD *md,
201 const char *from, const char *to,
202 void *x), void *arg)
203{
204 struct doall_md dc;
7b9f8f7f 205
f672aee4 206 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
7b9f8f7f 207
0f113f3e
MC
208 dc.fn = fn;
209 dc.arg = arg;
210 OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
211}