1193645Ssimon/* fips_dh_lib.c */
2296465Sdelphij/*
3296465Sdelphij * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4296465Sdelphij * 2007.
5193645Ssimon */
6193645Ssimon/* ====================================================================
7193645Ssimon * Copyright (c) 2007 The OpenSSL Project.  All rights reserved.
8193645Ssimon *
9193645Ssimon * Redistribution and use in source and binary forms, with or without
10193645Ssimon * modification, are permitted provided that the following conditions
11193645Ssimon * are met:
12193645Ssimon *
13193645Ssimon * 1. Redistributions of source code must retain the above copyright
14296465Sdelphij *    notice, this list of conditions and the following disclaimer.
15193645Ssimon *
16193645Ssimon * 2. Redistributions in binary form must reproduce the above copyright
17193645Ssimon *    notice, this list of conditions and the following disclaimer in
18193645Ssimon *    the documentation and/or other materials provided with the
19193645Ssimon *    distribution.
20193645Ssimon *
21193645Ssimon * 3. All advertising materials mentioning features or use of this
22193645Ssimon *    software must display the following acknowledgment:
23193645Ssimon *    "This product includes software developed by the OpenSSL Project
24193645Ssimon *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25193645Ssimon *
26193645Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27193645Ssimon *    endorse or promote products derived from this software without
28193645Ssimon *    prior written permission. For written permission, please contact
29193645Ssimon *    licensing@OpenSSL.org.
30193645Ssimon *
31193645Ssimon * 5. Products derived from this software may not be called "OpenSSL"
32193645Ssimon *    nor may "OpenSSL" appear in their names without prior written
33193645Ssimon *    permission of the OpenSSL Project.
34193645Ssimon *
35193645Ssimon * 6. Redistributions of any form whatsoever must retain the following
36193645Ssimon *    acknowledgment:
37193645Ssimon *    "This product includes software developed by the OpenSSL Project
38193645Ssimon *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39193645Ssimon *
40193645Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41193645Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42193645Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43193645Ssimon * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44193645Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45193645Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46193645Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47193645Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48193645Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49193645Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50193645Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51193645Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE.
52193645Ssimon * ====================================================================
53193645Ssimon *
54193645Ssimon * This product includes cryptographic software written by Eric Young
55193645Ssimon * (eay@cryptsoft.com).  This product includes software written by Tim
56193645Ssimon * Hudson (tjh@cryptsoft.com).
57193645Ssimon *
58193645Ssimon */
59193645Ssimon
60193645Ssimon#include <string.h>
61193645Ssimon#include <openssl/bn.h>
62193645Ssimon#include <openssl/dh.h>
63193645Ssimon
64296465Sdelphij/*
65296465Sdelphij * Minimal FIPS versions of FIPS_dh_new() and FIPS_dh_free(): to reduce
66296465Sdelphij * external dependencies.
67193645Ssimon */
68193645Ssimon
69193645SsimonDH *FIPS_dh_new(void)
70296465Sdelphij{
71296465Sdelphij    DH *ret;
72296465Sdelphij    ret = OPENSSL_malloc(sizeof(DH));
73296465Sdelphij    if (!ret)
74296465Sdelphij        return NULL;
75296465Sdelphij    memset(ret, 0, sizeof(DH));
76296465Sdelphij    ret->meth = DH_OpenSSL();
77296465Sdelphij    if (ret->meth->init)
78296465Sdelphij        ret->meth->init(ret);
79296465Sdelphij    return ret;
80296465Sdelphij}
81193645Ssimon
82193645Ssimonvoid FIPS_dh_free(DH *r)
83296465Sdelphij{
84296465Sdelphij    if (!r)
85296465Sdelphij        return;
86296465Sdelphij    if (r->meth->finish)
87296465Sdelphij        r->meth->finish(r);
88296465Sdelphij    if (r->p != NULL)
89296465Sdelphij        BN_clear_free(r->p);
90296465Sdelphij    if (r->g != NULL)
91296465Sdelphij        BN_clear_free(r->g);
92296465Sdelphij    if (r->q != NULL)
93296465Sdelphij        BN_clear_free(r->q);
94296465Sdelphij    if (r->j != NULL)
95296465Sdelphij        BN_clear_free(r->j);
96296465Sdelphij    if (r->seed)
97296465Sdelphij        OPENSSL_free(r->seed);
98296465Sdelphij    if (r->counter != NULL)
99296465Sdelphij        BN_clear_free(r->counter);
100296465Sdelphij    if (r->pub_key != NULL)
101296465Sdelphij        BN_clear_free(r->pub_key);
102296465Sdelphij    if (r->priv_key != NULL)
103296465Sdelphij        BN_clear_free(r->priv_key);
104296465Sdelphij    OPENSSL_free(r);
105296465Sdelphij}
106