1109998Smarkm/* ====================================================================
2109998Smarkm * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
3109998Smarkm *
4109998Smarkm * Redistribution and use in source and binary forms, with or without
5109998Smarkm * modification, are permitted provided that the following conditions
6109998Smarkm * are met:
7109998Smarkm *
8109998Smarkm * 1. Redistributions of source code must retain the above copyright
9296465Sdelphij *    notice, this list of conditions and the following disclaimer.
10109998Smarkm *
11109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright
12109998Smarkm *    notice, this list of conditions and the following disclaimer in
13109998Smarkm *    the documentation and/or other materials provided with the
14109998Smarkm *    distribution.
15109998Smarkm *
16109998Smarkm * 3. All advertising materials mentioning features or use of this
17109998Smarkm *    software must display the following acknowledgment:
18109998Smarkm *    "This product includes software developed by the OpenSSL Project
19109998Smarkm *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
20109998Smarkm *
21109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22109998Smarkm *    endorse or promote products derived from this software without
23109998Smarkm *    prior written permission. For written permission, please contact
24109998Smarkm *    licensing@OpenSSL.org.
25109998Smarkm *
26109998Smarkm * 5. Products derived from this software may not be called "OpenSSL"
27109998Smarkm *    nor may "OpenSSL" appear in their names without prior written
28109998Smarkm *    permission of the OpenSSL Project.
29109998Smarkm *
30109998Smarkm * 6. Redistributions of any form whatsoever must retain the following
31109998Smarkm *    acknowledgment:
32109998Smarkm *    "This product includes software developed by the OpenSSL Project
33109998Smarkm *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
34109998Smarkm *
35109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38109998Smarkm * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE.
47109998Smarkm * ====================================================================
48109998Smarkm *
49109998Smarkm * This product includes cryptographic software written by Eric Young
50109998Smarkm * (eay@cryptsoft.com).  This product includes software written by Tim
51109998Smarkm * Hudson (tjh@cryptsoft.com).
52109998Smarkm *
53109998Smarkm */
54109998Smarkm
55109998Smarkm#include "eng_int.h"
56109998Smarkm
57296465Sdelphij/*
58296465Sdelphij * If this symbol is defined then ENGINE_get_default_RAND(), the function
59296465Sdelphij * that is used by RAND to hook in implementation code and cache defaults
60296465Sdelphij * (etc), will display brief debugging summaries to stderr with the 'nid'.
61296465Sdelphij */
62109998Smarkm/* #define ENGINE_RAND_DEBUG */
63109998Smarkm
64109998Smarkmstatic ENGINE_TABLE *rand_table = NULL;
65109998Smarkmstatic const int dummy_nid = 1;
66109998Smarkm
67109998Smarkmvoid ENGINE_unregister_RAND(ENGINE *e)
68296465Sdelphij{
69296465Sdelphij    engine_table_unregister(&rand_table, e);
70296465Sdelphij}
71109998Smarkm
72109998Smarkmstatic void engine_unregister_all_RAND(void)
73296465Sdelphij{
74296465Sdelphij    engine_table_cleanup(&rand_table);
75296465Sdelphij}
76109998Smarkm
77109998Smarkmint ENGINE_register_RAND(ENGINE *e)
78296465Sdelphij{
79296465Sdelphij    if (e->rand_meth)
80296465Sdelphij        return engine_table_register(&rand_table,
81296465Sdelphij                                     engine_unregister_all_RAND, e,
82296465Sdelphij                                     &dummy_nid, 1, 0);
83296465Sdelphij    return 1;
84296465Sdelphij}
85109998Smarkm
86109998Smarkmvoid ENGINE_register_all_RAND()
87296465Sdelphij{
88296465Sdelphij    ENGINE *e;
89109998Smarkm
90296465Sdelphij    for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))
91296465Sdelphij        ENGINE_register_RAND(e);
92296465Sdelphij}
93109998Smarkm
94109998Smarkmint ENGINE_set_default_RAND(ENGINE *e)
95296465Sdelphij{
96296465Sdelphij    if (e->rand_meth)
97296465Sdelphij        return engine_table_register(&rand_table,
98296465Sdelphij                                     engine_unregister_all_RAND, e,
99296465Sdelphij                                     &dummy_nid, 1, 1);
100296465Sdelphij    return 1;
101296465Sdelphij}
102109998Smarkm
103296465Sdelphij/*
104296465Sdelphij * Exposed API function to get a functional reference from the implementation
105109998Smarkm * table (ie. try to get a functional reference from the tabled structural
106296465Sdelphij * references).
107296465Sdelphij */
108109998SmarkmENGINE *ENGINE_get_default_RAND(void)
109296465Sdelphij{
110296465Sdelphij    return engine_table_select(&rand_table, dummy_nid);
111296465Sdelphij}
112109998Smarkm
113109998Smarkm/* Obtains an RAND implementation from an ENGINE functional reference */
114109998Smarkmconst RAND_METHOD *ENGINE_get_RAND(const ENGINE *e)
115296465Sdelphij{
116296465Sdelphij    return e->rand_meth;
117296465Sdelphij}
118109998Smarkm
119109998Smarkm/* Sets an RAND implementation in an ENGINE structure */
120109998Smarkmint ENGINE_set_RAND(ENGINE *e, const RAND_METHOD *rand_meth)
121296465Sdelphij{
122296465Sdelphij    e->rand_meth = rand_meth;
123296465Sdelphij    return 1;
124296465Sdelphij}
125