5e75e64e6918933e0c96ef3fa35c3e6bf44e2c11
[openssl.git] / crypto / engine / tb_digest.c
1 /*
2  * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
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
8  */
9
10 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
12
13 #include "eng_local.h"
14
15 static ENGINE_TABLE *digest_table = NULL;
16
17 void ENGINE_unregister_digests(ENGINE *e)
18 {
19     engine_table_unregister(&digest_table, e);
20 }
21
22 static void engine_unregister_all_digests(void)
23 {
24     engine_table_cleanup(&digest_table);
25 }
26
27 int ENGINE_register_digests(ENGINE *e)
28 {
29     if (e->digests) {
30         const int *nids;
31         int num_nids = e->digests(e, NULL, &nids, 0);
32         if (num_nids > 0)
33             return engine_table_register(&digest_table,
34                                          engine_unregister_all_digests, e,
35                                          nids, num_nids, 0);
36     }
37     return 1;
38 }
39
40 void ENGINE_register_all_digests(void)
41 {
42     ENGINE *e;
43
44     for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))
45         ENGINE_register_digests(e);
46 }
47
48 int ENGINE_set_default_digests(ENGINE *e)
49 {
50     if (e->digests) {
51         const int *nids;
52         int num_nids = e->digests(e, NULL, &nids, 0);
53         if (num_nids > 0)
54             return engine_table_register(&digest_table,
55                                          engine_unregister_all_digests, e,
56                                          nids, num_nids, 1);
57     }
58     return 1;
59 }
60
61 /*
62  * Exposed API function to get a functional reference from the implementation
63  * table (ie. try to get a functional reference from the tabled structural
64  * references) for a given digest 'nid'
65  */
66 ENGINE *ENGINE_get_digest_engine(int nid)
67 {
68     return engine_table_select(&digest_table, nid);
69 }
70
71 /* Obtains a digest implementation from an ENGINE functional reference */
72 const EVP_MD *ENGINE_get_digest(ENGINE *e, int nid)
73 {
74     const EVP_MD *ret;
75     ENGINE_DIGESTS_PTR fn = ENGINE_get_digests(e);
76     if (!fn || !fn(e, &ret, NULL, nid)) {
77         ENGINEerr(ENGINE_F_ENGINE_GET_DIGEST, ENGINE_R_UNIMPLEMENTED_DIGEST);
78         return NULL;
79     }
80     return ret;
81 }
82
83 /* Gets the digest callback from an ENGINE structure */
84 ENGINE_DIGESTS_PTR ENGINE_get_digests(const ENGINE *e)
85 {
86     return e->digests;
87 }
88
89 /* Sets the digest callback in an ENGINE structure */
90 int ENGINE_set_digests(ENGINE *e, ENGINE_DIGESTS_PTR f)
91 {
92     e->digests = f;
93     return 1;
94 }