1160814Ssimon/* crypto/engine/tb_ecdh.c */
2160814Ssimon/* ====================================================================
3160814Ssimon * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4160814Ssimon *
5160814Ssimon * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
6160814Ssimon * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
7160814Ssimon * to the OpenSSL project.
8160814Ssimon *
9160814Ssimon * The ECC Code is licensed pursuant to the OpenSSL open source
10160814Ssimon * license provided below.
11160814Ssimon *
12160814Ssimon * The ECDH engine software is originally written by Nils Gura and
13160814Ssimon * Douglas Stebila of Sun Microsystems Laboratories.
14160814Ssimon *
15160814Ssimon */
16160814Ssimon/* ====================================================================
17160814Ssimon * Copyright (c) 2000-2002 The OpenSSL Project.  All rights reserved.
18160814Ssimon *
19160814Ssimon * Redistribution and use in source and binary forms, with or without
20160814Ssimon * modification, are permitted provided that the following conditions
21160814Ssimon * are met:
22160814Ssimon *
23160814Ssimon * 1. Redistributions of source code must retain the above copyright
24280297Sjkim *    notice, this list of conditions and the following disclaimer.
25160814Ssimon *
26160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright
27160814Ssimon *    notice, this list of conditions and the following disclaimer in
28160814Ssimon *    the documentation and/or other materials provided with the
29160814Ssimon *    distribution.
30160814Ssimon *
31160814Ssimon * 3. All advertising materials mentioning features or use of this
32160814Ssimon *    software must display the following acknowledgment:
33160814Ssimon *    "This product includes software developed by the OpenSSL Project
34160814Ssimon *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
35160814Ssimon *
36160814Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
37160814Ssimon *    endorse or promote products derived from this software without
38160814Ssimon *    prior written permission. For written permission, please contact
39160814Ssimon *    licensing@OpenSSL.org.
40160814Ssimon *
41160814Ssimon * 5. Products derived from this software may not be called "OpenSSL"
42160814Ssimon *    nor may "OpenSSL" appear in their names without prior written
43160814Ssimon *    permission of the OpenSSL Project.
44160814Ssimon *
45160814Ssimon * 6. Redistributions of any form whatsoever must retain the following
46160814Ssimon *    acknowledgment:
47160814Ssimon *    "This product includes software developed by the OpenSSL Project
48160814Ssimon *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
49160814Ssimon *
50160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
51160814Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53160814Ssimon * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
54160814Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55160814Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57160814Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59160814Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60160814Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
61160814Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE.
62160814Ssimon * ====================================================================
63160814Ssimon *
64160814Ssimon * This product includes cryptographic software written by Eric Young
65160814Ssimon * (eay@cryptsoft.com).  This product includes software written by Tim
66160814Ssimon * Hudson (tjh@cryptsoft.com).
67160814Ssimon *
68160814Ssimon */
69160814Ssimon
70160814Ssimon#include "eng_int.h"
71160814Ssimon
72280297Sjkim/*
73280297Sjkim * If this symbol is defined then ENGINE_get_default_ECDH(), the function
74280297Sjkim * that is used by ECDH to hook in implementation code and cache defaults
75280297Sjkim * (etc), will display brief debugging summaries to stderr with the 'nid'.
76280297Sjkim */
77160814Ssimon/* #define ENGINE_ECDH_DEBUG */
78160814Ssimon
79160814Ssimonstatic ENGINE_TABLE *ecdh_table = NULL;
80160814Ssimonstatic const int dummy_nid = 1;
81160814Ssimon
82160814Ssimonvoid ENGINE_unregister_ECDH(ENGINE *e)
83280297Sjkim{
84280297Sjkim    engine_table_unregister(&ecdh_table, e);
85280297Sjkim}
86160814Ssimon
87160814Ssimonstatic void engine_unregister_all_ECDH(void)
88280297Sjkim{
89280297Sjkim    engine_table_cleanup(&ecdh_table);
90280297Sjkim}
91160814Ssimon
92160814Ssimonint ENGINE_register_ECDH(ENGINE *e)
93280297Sjkim{
94280297Sjkim    if (e->ecdh_meth)
95280297Sjkim        return engine_table_register(&ecdh_table,
96280297Sjkim                                     engine_unregister_all_ECDH, e,
97280297Sjkim                                     &dummy_nid, 1, 0);
98280297Sjkim    return 1;
99280297Sjkim}
100160814Ssimon
101160814Ssimonvoid ENGINE_register_all_ECDH()
102280297Sjkim{
103280297Sjkim    ENGINE *e;
104160814Ssimon
105280297Sjkim    for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))
106280297Sjkim        ENGINE_register_ECDH(e);
107280297Sjkim}
108160814Ssimon
109160814Ssimonint ENGINE_set_default_ECDH(ENGINE *e)
110280297Sjkim{
111280297Sjkim    if (e->ecdh_meth)
112280297Sjkim        return engine_table_register(&ecdh_table,
113280297Sjkim                                     engine_unregister_all_ECDH, e,
114280297Sjkim                                     &dummy_nid, 1, 1);
115280297Sjkim    return 1;
116280297Sjkim}
117160814Ssimon
118280297Sjkim/*
119280297Sjkim * Exposed API function to get a functional reference from the implementation
120160814Ssimon * table (ie. try to get a functional reference from the tabled structural
121280297Sjkim * references).
122280297Sjkim */
123160814SsimonENGINE *ENGINE_get_default_ECDH(void)
124280297Sjkim{
125280297Sjkim    return engine_table_select(&ecdh_table, dummy_nid);
126280297Sjkim}
127160814Ssimon
128160814Ssimon/* Obtains an ECDH implementation from an ENGINE functional reference */
129160814Ssimonconst ECDH_METHOD *ENGINE_get_ECDH(const ENGINE *e)
130280297Sjkim{
131280297Sjkim    return e->ecdh_meth;
132280297Sjkim}
133160814Ssimon
134160814Ssimon/* Sets an ECDH implementation in an ENGINE structure */
135160814Ssimonint ENGINE_set_ECDH(ENGINE *e, const ECDH_METHOD *ecdh_meth)
136280297Sjkim{
137280297Sjkim    e->ecdh_meth = ecdh_meth;
138280297Sjkim    return 1;
139280297Sjkim}
140