GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/crypto/../../libssl/src/crypto/evp/pmeth_gn.c Lines: 26 79 32.9 %
Date: 2016-12-06 Branches: 14 60 23.3 %

Line Branch Exec Source
1
/* $OpenBSD: pmeth_gn.c,v 1.5 2014/07/12 16:03:37 miod Exp $ */
2
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3
 * project 2006.
4
 */
5
/* ====================================================================
6
 * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 *
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in
17
 *    the documentation and/or other materials provided with the
18
 *    distribution.
19
 *
20
 * 3. All advertising materials mentioning features or use of this
21
 *    software must display the following acknowledgment:
22
 *    "This product includes software developed by the OpenSSL Project
23
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24
 *
25
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26
 *    endorse or promote products derived from this software without
27
 *    prior written permission. For written permission, please contact
28
 *    licensing@OpenSSL.org.
29
 *
30
 * 5. Products derived from this software may not be called "OpenSSL"
31
 *    nor may "OpenSSL" appear in their names without prior written
32
 *    permission of the OpenSSL Project.
33
 *
34
 * 6. Redistributions of any form whatsoever must retain the following
35
 *    acknowledgment:
36
 *    "This product includes software developed by the OpenSSL Project
37
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38
 *
39
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50
 * OF THE POSSIBILITY OF SUCH DAMAGE.
51
 * ====================================================================
52
 *
53
 * This product includes cryptographic software written by Eric Young
54
 * (eay@cryptsoft.com).  This product includes software written by Tim
55
 * Hudson (tjh@cryptsoft.com).
56
 *
57
 */
58
59
#include <stdio.h>
60
#include <stdlib.h>
61
62
#include <openssl/bn.h>
63
#include <openssl/err.h>
64
#include <openssl/evp.h>
65
#include <openssl/objects.h>
66
67
#include "evp_locl.h"
68
69
int
70
EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
71
{
72
	int ret;
73
74
	if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
75
		EVPerr(EVP_F_EVP_PKEY_PARAMGEN_INIT,
76
		    EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
77
		return -2;
78
	}
79
	ctx->operation = EVP_PKEY_OP_PARAMGEN;
80
	if (!ctx->pmeth->paramgen_init)
81
		return 1;
82
	ret = ctx->pmeth->paramgen_init(ctx);
83
	if (ret <= 0)
84
		ctx->operation = EVP_PKEY_OP_UNDEFINED;
85
	return ret;
86
}
87
88
int
89
EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
90
{
91
	int ret;
92
93
	if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
94
		EVPerr(EVP_F_EVP_PKEY_PARAMGEN,
95
		    EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
96
		return -2;
97
	}
98
99
	if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
100
		EVPerr(EVP_F_EVP_PKEY_PARAMGEN, EVP_R_OPERATON_NOT_INITIALIZED);
101
		return -1;
102
	}
103
104
	if (!ppkey)
105
		return -1;
106
107
	if (!*ppkey)
108
		*ppkey = EVP_PKEY_new();
109
110
	ret = ctx->pmeth->paramgen(ctx, *ppkey);
111
	if (ret <= 0) {
112
		EVP_PKEY_free(*ppkey);
113
		*ppkey = NULL;
114
	}
115
	return ret;
116
}
117
118
int
119
EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
120
18
{
121
	int ret;
122
123

18
	if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
124
		EVPerr(EVP_F_EVP_PKEY_KEYGEN_INIT,
125
		    EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
126
		return -2;
127
	}
128
18
	ctx->operation = EVP_PKEY_OP_KEYGEN;
129
18
	if (!ctx->pmeth->keygen_init)
130
18
		return 1;
131
	ret = ctx->pmeth->keygen_init(ctx);
132
	if (ret <= 0)
133
		ctx->operation = EVP_PKEY_OP_UNDEFINED;
134
	return ret;
135
}
136
137
int
138
EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
139
18
{
140
	int ret;
141
142

18
	if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
143
		EVPerr(EVP_F_EVP_PKEY_KEYGEN,
144
		    EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
145
		return -2;
146
	}
147
18
	if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
148
		EVPerr(EVP_F_EVP_PKEY_KEYGEN, EVP_R_OPERATON_NOT_INITIALIZED);
149
		return -1;
150
	}
151
152
18
	if (!ppkey)
153
		return -1;
154
155
18
	if (!*ppkey)
156
18
		*ppkey = EVP_PKEY_new();
157
158
18
	ret = ctx->pmeth->keygen(ctx, *ppkey);
159
18
	if (ret <= 0) {
160
		EVP_PKEY_free(*ppkey);
161
		*ppkey = NULL;
162
	}
163
18
	return ret;
164
}
165
166
void
167
EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
168
{
169
	ctx->pkey_gencb = cb;
170
}
171
172
EVP_PKEY_gen_cb *
173
EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
174
{
175
	return ctx->pkey_gencb;
176
}
177
178
/* "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB
179
 * style callbacks.
180
 */
181
182
static int
183
trans_cb(int a, int b, BN_GENCB *gcb)
184
{
185
	EVP_PKEY_CTX *ctx = gcb->arg;
186
	ctx->keygen_info[0] = a;
187
	ctx->keygen_info[1] = b;
188
	return ctx->pkey_gencb(ctx);
189
}
190
191
void
192
evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
193
{
194
	BN_GENCB_set(cb, trans_cb, ctx)
195
}
196
197
int
198
EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
199
{
200
	if (idx == -1)
201
		return ctx->keygen_info_count;
202
	if (idx < 0 || idx > ctx->keygen_info_count)
203
		return 0;
204
	return ctx->keygen_info[idx];
205
}
206
207
EVP_PKEY *
208
EVP_PKEY_new_mac_key(int type, ENGINE *e, const unsigned char *key, int keylen)
209
18
{
210
18
	EVP_PKEY_CTX *mac_ctx = NULL;
211
18
	EVP_PKEY *mac_key = NULL;
212
213
18
	mac_ctx = EVP_PKEY_CTX_new_id(type, e);
214
18
	if (!mac_ctx)
215
		return NULL;
216
18
	if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
217
		goto merr;
218
18
	if (EVP_PKEY_CTX_ctrl(mac_ctx, -1, EVP_PKEY_OP_KEYGEN,
219
	    EVP_PKEY_CTRL_SET_MAC_KEY, keylen, (void *)key) <= 0)
220
		goto merr;
221
18
	if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
222
18
		goto merr;
223
224
18
merr:
225
18
	EVP_PKEY_CTX_free(mac_ctx);
226
18
	return mac_key;
227
}