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