1656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* ====================================================================
2656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
3656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
4656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Redistribution and use in source and binary forms, with or without
5656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * modification, are permitted provided that the following conditions
6656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * are met:
7656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
8656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 1. Redistributions of source code must retain the above copyright
9656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    notice, this list of conditions and the following disclaimer.
10656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
11656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
12656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    notice, this list of conditions and the following disclaimer in
13656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    the documentation and/or other materials provided with the
14656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    distribution.
15656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
16656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 3. All advertising materials mentioning features or use of this
17656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    software must display the following acknowledgment:
18656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    "This product includes software developed by the OpenSSL Project
19656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
20656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
21656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    endorse or promote products derived from this software without
23656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    prior written permission. For written permission, please contact
24656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    licensing@OpenSSL.org.
25656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
26656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 5. Products derived from this software may not be called "OpenSSL"
27656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    nor may "OpenSSL" appear in their names without prior written
28656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    permission of the OpenSSL Project.
29656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
30656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 6. Redistributions of any form whatsoever must retain the following
31656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    acknowledgment:
32656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    "This product includes software developed by the OpenSSL Project
33656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
34656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
35656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * OF THE POSSIBILITY OF SUCH DAMAGE.
47656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ====================================================================
48656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
49656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * This product includes cryptographic software written by Eric Young
50656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * (eay@cryptsoft.com).  This product includes software written by Tim
51656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Hudson (tjh@cryptsoft.com).
52656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
53656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */
54656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
55656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include "eng_int.h"
56656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
57656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* If this symbol is defined then ENGINE_get_digest_engine(), the function that
58656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * is used by EVP to hook in digest code and cache defaults (etc), will display
59656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * brief debugging summaries to stderr with the 'nid'. */
60656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* #define ENGINE_DIGEST_DEBUG */
61656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
62656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectstatic ENGINE_TABLE *digest_table = NULL;
63656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
64656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectvoid ENGINE_unregister_digests(ENGINE *e)
65656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	{
66656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	engine_table_unregister(&digest_table, e);
67656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
68656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
69656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectstatic void engine_unregister_all_digests(void)
70656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	{
71656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	engine_table_cleanup(&digest_table);
72656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
73656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
74656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint ENGINE_register_digests(ENGINE *e)
75656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	{
76656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(e->digests)
77656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
78656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		const int *nids;
79656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		int num_nids = e->digests(e, NULL, &nids, 0);
80656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(num_nids > 0)
81656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			return engine_table_register(&digest_table,
82656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project					engine_unregister_all_digests, e, nids,
83656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project					num_nids, 0);
84656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
85656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return 1;
86656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
87656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
88656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectvoid ENGINE_register_all_digests()
89656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	{
90656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	ENGINE *e;
91656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
92656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	for(e=ENGINE_get_first() ; e ; e=ENGINE_get_next(e))
93656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		ENGINE_register_digests(e);
94656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
95656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
96656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint ENGINE_set_default_digests(ENGINE *e)
97656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	{
98656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(e->digests)
99656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
100656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		const int *nids;
101656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		int num_nids = e->digests(e, NULL, &nids, 0);
102656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(num_nids > 0)
103656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			return engine_table_register(&digest_table,
104656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project					engine_unregister_all_digests, e, nids,
105656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project					num_nids, 1);
106656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
107656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return 1;
108656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
109656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
110656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* Exposed API function to get a functional reference from the implementation
111656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * table (ie. try to get a functional reference from the tabled structural
112656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * references) for a given digest 'nid' */
113656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source ProjectENGINE *ENGINE_get_digest_engine(int nid)
114656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	{
115656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return engine_table_select(&digest_table, nid);
116656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
117656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
118656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* Obtains a digest implementation from an ENGINE functional reference */
119656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectconst EVP_MD *ENGINE_get_digest(ENGINE *e, int nid)
120656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	{
121656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	const EVP_MD *ret;
122656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	ENGINE_DIGESTS_PTR fn = ENGINE_get_digests(e);
123656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(!fn || !fn(e, &ret, NULL, nid))
124656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
125656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		ENGINEerr(ENGINE_F_ENGINE_GET_DIGEST,
126656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project				ENGINE_R_UNIMPLEMENTED_DIGEST);
127656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		return NULL;
128656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
129656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return ret;
130656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
131656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
132656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* Gets the digest callback from an ENGINE structure */
133656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source ProjectENGINE_DIGESTS_PTR ENGINE_get_digests(const ENGINE *e)
134656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	{
135656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return e->digests;
136656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
137656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
138656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* Sets the digest callback in an ENGINE structure */
139656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint ENGINE_set_digests(ENGINE *e, ENGINE_DIGESTS_PTR f)
140656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	{
141656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	e->digests = f;
142656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return 1;
143656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
144