tb_rand.c revision 296465
166200Simp/* ====================================================================
252506Simp * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
3139749Simp *
452506Simp * Redistribution and use in source and binary forms, with or without
552506Simp * modification, are permitted provided that the following conditions
652506Simp * are met:
752506Simp *
852506Simp * 1. Redistributions of source code must retain the above copyright
952506Simp *    notice, this list of conditions and the following disclaimer.
1052506Simp *
1152506Simp * 2. Redistributions in binary form must reproduce the above copyright
1252506Simp *    notice, this list of conditions and the following disclaimer in
1352506Simp *    the documentation and/or other materials provided with the
1452506Simp *    distribution.
1552506Simp *
1652506Simp * 3. All advertising materials mentioning features or use of this
1752506Simp *    software must display the following acknowledgment:
1852506Simp *    "This product includes software developed by the OpenSSL Project
1952506Simp *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
2052506Simp *
2152506Simp * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2252506Simp *    endorse or promote products derived from this software without
2352506Simp *    prior written permission. For written permission, please contact
2452506Simp *    licensing@OpenSSL.org.
2552506Simp *
2652506Simp * 5. Products derived from this software may not be called "OpenSSL"
2752506Simp *    nor may "OpenSSL" appear in their names without prior written
2852506Simp *    permission of the OpenSSL Project.
2952506Simp *
3052506Simp * 6. Redistributions of any form whatsoever must retain the following
3152506Simp *    acknowledgment:
32119418Sobrien *    "This product includes software developed by the OpenSSL Project
33119418Sobrien *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
34119418Sobrien *
3552506Simp * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
3652506Simp * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3752506Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3852506Simp * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
3952506Simp * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4052506Simp * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4191786Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4252506Simp * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4352506Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4452506Simp * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4552506Simp * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
4652506Simp * OF THE POSSIBILITY OF SUCH DAMAGE.
4752506Simp * ====================================================================
4852506Simp *
4982781Sshiba * This product includes cryptographic software written by Eric Young
5082781Sshiba * (eay@cryptsoft.com).  This product includes software written by Tim
5152506Simp * Hudson (tjh@cryptsoft.com).
5252506Simp *
53150362Simp */
54144930Simp
5552506Simp#include "eng_int.h"
5655500Simp
5759193Simp/*
5855500Simp * If this symbol is defined then ENGINE_get_default_RAND(), the function
5955500Simp * that is used by RAND to hook in implementation code and cache defaults
6055500Simp * (etc), will display brief debugging summaries to stderr with the 'nid'.
6191786Simp */
62248085Smarius/* #define ENGINE_RAND_DEBUG */
6391786Simp
6491786Simpstatic ENGINE_TABLE *rand_table = NULL;
6591786Simpstatic const int dummy_nid = 1;
6691786Simp
6791786Simpvoid ENGINE_unregister_RAND(ENGINE *e)
6891786Simp{
6991786Simp    engine_table_unregister(&rand_table, e);
7091786Simp}
7191786Simp
7291786Simpstatic void engine_unregister_all_RAND(void)
7391786Simp{
7491786Simp    engine_table_cleanup(&rand_table);
7552506Simp}
7652506Simp
7755500Simpint ENGINE_register_RAND(ENGINE *e)
7867333Simp{
7967333Simp    if (e->rand_meth)
8052506Simp        return engine_table_register(&rand_table,
8152506Simp                                     engine_unregister_all_RAND, e,
8255500Simp                                     &dummy_nid, 1, 0);
8367333Simp    return 1;
8467333Simp}
8552506Simp
8652506Simpvoid ENGINE_register_all_RAND()
8782378Sjon{
8882378Sjon    ENGINE *e;
8982378Sjon
90106362Simp    for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))
91181342Simp        ENGINE_register_RAND(e);
9282378Sjon}
9382378Sjon
9482378Sjonint ENGINE_set_default_RAND(ENGINE *e)
9582378Sjon{
9682378Sjon    if (e->rand_meth)
9782378Sjon        return engine_table_register(&rand_table,
9882378Sjon                                     engine_unregister_all_RAND, e,
9982378Sjon                                     &dummy_nid, 1, 1);
10082378Sjon    return 1;
10182378Sjon}
10282378Sjon
10382378Sjon/*
10482378Sjon * Exposed API function to get a functional reference from the implementation
10582378Sjon * table (ie. try to get a functional reference from the tabled structural
10682378Sjon * references).
10782378Sjon */
108188179SimpENGINE *ENGINE_get_default_RAND(void)
10982378Sjon{
110140692Simp    return engine_table_select(&rand_table, dummy_nid);
111181342Simp}
112181342Simp
113104641Simp/* Obtains an RAND implementation from an ENGINE functional reference */
11482378Sjonconst RAND_METHOD *ENGINE_get_RAND(const ENGINE *e)
115188179Simp{
11682378Sjon    return e->rand_meth;
11782378Sjon}
11882378Sjon
11982378Sjon/* Sets an RAND implementation in an ENGINE structure */
12082378Sjonint ENGINE_set_RAND(ENGINE *e, const RAND_METHOD *rand_meth)
12182378Sjon{
12282378Sjon    e->rand_meth = rand_meth;
123170163Spiso    return 1;
124170163Spiso}
12582378Sjon