summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ecdsa/ecs_lib.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/ecdsa/ecs_lib.c124
1 files changed, 64 insertions, 60 deletions
diff --git a/src/lib/libcrypto/ecdsa/ecs_lib.c b/src/lib/libcrypto/ecdsa/ecs_lib.c
index 6cdf4c124b..dba888cb48 100644
--- a/src/lib/libcrypto/ecdsa/ecs_lib.c
+++ b/src/lib/libcrypto/ecdsa/ecs_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ecs_lib.c,v 1.8 2015/02/07 13:19:15 doug Exp $ */ 1/* $OpenBSD: ecs_lib.c,v 1.9 2015/02/08 13:35:07 jsing Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -7,7 +7,7 @@
7 * are met: 7 * are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
11 * 11 *
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in 13 * notice, this list of conditions and the following disclaimer in
@@ -70,21 +70,23 @@ static void *ecdsa_data_new(void);
70static void *ecdsa_data_dup(void *); 70static void *ecdsa_data_dup(void *);
71static void ecdsa_data_free(void *); 71static void ecdsa_data_free(void *);
72 72
73void ECDSA_set_default_method(const ECDSA_METHOD *meth) 73void
74ECDSA_set_default_method(const ECDSA_METHOD *meth)
74{ 75{
75 default_ECDSA_method = meth; 76 default_ECDSA_method = meth;
76} 77}
77 78
78const ECDSA_METHOD *ECDSA_get_default_method(void) 79const ECDSA_METHOD *
80ECDSA_get_default_method(void)
79{ 81{
80 if(!default_ECDSA_method) 82 if (!default_ECDSA_method) {
81 {
82 default_ECDSA_method = ECDSA_OpenSSL(); 83 default_ECDSA_method = ECDSA_OpenSSL();
83 } 84 }
84 return default_ECDSA_method; 85 return default_ECDSA_method;
85} 86}
86 87
87int ECDSA_set_method(EC_KEY *eckey, const ECDSA_METHOD *meth) 88int
89ECDSA_set_method(EC_KEY *eckey, const ECDSA_METHOD *meth)
88{ 90{
89 ECDSA_DATA *ecdsa; 91 ECDSA_DATA *ecdsa;
90 92
@@ -94,26 +96,25 @@ int ECDSA_set_method(EC_KEY *eckey, const ECDSA_METHOD *meth)
94 return 0; 96 return 0;
95 97
96#ifndef OPENSSL_NO_ENGINE 98#ifndef OPENSSL_NO_ENGINE
97 if (ecdsa->engine) 99 if (ecdsa->engine) {
98 {
99 ENGINE_finish(ecdsa->engine); 100 ENGINE_finish(ecdsa->engine);
100 ecdsa->engine = NULL; 101 ecdsa->engine = NULL;
101 } 102 }
102#endif 103#endif
103 ecdsa->meth = meth; 104 ecdsa->meth = meth;
104 105
105 return 1; 106 return 1;
106} 107}
107 108
108static ECDSA_DATA *ECDSA_DATA_new_method(ENGINE *engine) 109static ECDSA_DATA *
110ECDSA_DATA_new_method(ENGINE *engine)
109{ 111{
110 ECDSA_DATA *ret; 112 ECDSA_DATA *ret;
111 113
112 ret = malloc(sizeof(ECDSA_DATA)); 114 ret = malloc(sizeof(ECDSA_DATA));
113 if (ret == NULL) 115 if (ret == NULL) {
114 {
115 ECDSAerr(ECDSA_F_ECDSA_DATA_NEW_METHOD, ERR_R_MALLOC_FAILURE); 116 ECDSAerr(ECDSA_F_ECDSA_DATA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
116 return(NULL); 117 return (NULL);
117 } 118 }
118 119
119 ret->init = NULL; 120 ret->init = NULL;
@@ -123,12 +124,11 @@ static ECDSA_DATA *ECDSA_DATA_new_method(ENGINE *engine)
123#ifndef OPENSSL_NO_ENGINE 124#ifndef OPENSSL_NO_ENGINE
124 if (!ret->engine) 125 if (!ret->engine)
125 ret->engine = ENGINE_get_default_ECDSA(); 126 ret->engine = ENGINE_get_default_ECDSA();
126 if (ret->engine) 127 if (ret->engine) {
127 {
128 ret->meth = ENGINE_get_ECDSA(ret->engine); 128 ret->meth = ENGINE_get_ECDSA(ret->engine);
129 if (!ret->meth) 129 if (!ret->meth) {
130 { 130 ECDSAerr(ECDSA_F_ECDSA_DATA_NEW_METHOD,
131 ECDSAerr(ECDSA_F_ECDSA_DATA_NEW_METHOD, ERR_R_ENGINE_LIB); 131 ERR_R_ENGINE_LIB);
132 ENGINE_finish(ret->engine); 132 ENGINE_finish(ret->engine);
133 free(ret); 133 free(ret);
134 return NULL; 134 return NULL;
@@ -138,15 +138,17 @@ static ECDSA_DATA *ECDSA_DATA_new_method(ENGINE *engine)
138 138
139 ret->flags = ret->meth->flags; 139 ret->flags = ret->meth->flags;
140 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ECDSA, ret, &ret->ex_data); 140 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ECDSA, ret, &ret->ex_data);
141 return(ret); 141 return (ret);
142} 142}
143 143
144static void *ecdsa_data_new(void) 144static void *
145ecdsa_data_new(void)
145{ 146{
146 return (void *)ECDSA_DATA_new_method(NULL); 147 return (void *)ECDSA_DATA_new_method(NULL);
147} 148}
148 149
149static void *ecdsa_data_dup(void *data) 150static void *
151ecdsa_data_dup(void *data)
150{ 152{
151 ECDSA_DATA *r = (ECDSA_DATA *)data; 153 ECDSA_DATA *r = (ECDSA_DATA *)data;
152 154
@@ -157,7 +159,8 @@ static void *ecdsa_data_dup(void *data)
157 return ecdsa_data_new(); 159 return ecdsa_data_new();
158} 160}
159 161
160static void ecdsa_data_free(void *data) 162static void
163ecdsa_data_free(void *data)
161{ 164{
162 ECDSA_DATA *r = (ECDSA_DATA *)data; 165 ECDSA_DATA *r = (ECDSA_DATA *)data;
163 166
@@ -172,38 +175,37 @@ static void ecdsa_data_free(void *data)
172 free(r); 175 free(r);
173} 176}
174 177
175ECDSA_DATA *ecdsa_check(EC_KEY *key) 178ECDSA_DATA *
179ecdsa_check(EC_KEY *key)
176{ 180{
177 ECDSA_DATA *ecdsa_data; 181 ECDSA_DATA *ecdsa_data;
178 182
179 void *data = EC_KEY_get_key_method_data(key, ecdsa_data_dup, 183 void *data = EC_KEY_get_key_method_data(key, ecdsa_data_dup,
180 ecdsa_data_free, ecdsa_data_free); 184 ecdsa_data_free, ecdsa_data_free);
181 if (data == NULL) 185 if (data == NULL) {
182 {
183 ecdsa_data = (ECDSA_DATA *)ecdsa_data_new(); 186 ecdsa_data = (ECDSA_DATA *)ecdsa_data_new();
184 if (ecdsa_data == NULL) 187 if (ecdsa_data == NULL)
185 return NULL; 188 return NULL;
186 data = EC_KEY_insert_key_method_data(key, (void *)ecdsa_data, 189 data = EC_KEY_insert_key_method_data(key, (void *)ecdsa_data,
187 ecdsa_data_dup, ecdsa_data_free, ecdsa_data_free); 190 ecdsa_data_dup, ecdsa_data_free, ecdsa_data_free);
188 if (data != NULL) 191 if (data != NULL) {
189 {
190 /* Another thread raced us to install the key_method 192 /* Another thread raced us to install the key_method
191 * data and won. */ 193 * data and won. */
192 ecdsa_data_free(ecdsa_data); 194 ecdsa_data_free(ecdsa_data);
193 ecdsa_data = (ECDSA_DATA *)data; 195 ecdsa_data = (ECDSA_DATA *)data;
194 } 196 }
195 } 197 } else
196 else
197 ecdsa_data = (ECDSA_DATA *)data; 198 ecdsa_data = (ECDSA_DATA *)data;
198 199
199 return ecdsa_data; 200 return ecdsa_data;
200} 201}
201 202
202int ECDSA_size(const EC_KEY *r) 203int
204ECDSA_size(const EC_KEY *r)
203{ 205{
204 int ret,i; 206 int ret, i;
205 ASN1_INTEGER bs; 207 ASN1_INTEGER bs;
206 BIGNUM *order=NULL; 208 BIGNUM *order = NULL;
207 unsigned char buf[4]; 209 unsigned char buf[4];
208 const EC_GROUP *group; 210 const EC_GROUP *group;
209 211
@@ -213,48 +215,50 @@ int ECDSA_size(const EC_KEY *r)
213 if (group == NULL) 215 if (group == NULL)
214 return 0; 216 return 0;
215 217
216 if ((order = BN_new()) == NULL) return 0; 218 if ((order = BN_new()) == NULL)
217 if (!EC_GROUP_get_order(group,order,NULL)) 219 return 0;
218 { 220 if (!EC_GROUP_get_order(group, order, NULL)) {
219 BN_clear_free(order); 221 BN_clear_free(order);
220 return 0; 222 return 0;
221 } 223 }
222 i=BN_num_bits(order); 224 i = BN_num_bits(order);
223 bs.length=(i+7)/8; 225 bs.length = (i + 7) / 8;
224 bs.data=buf; 226 bs.data = buf;
225 bs.type=V_ASN1_INTEGER; 227 bs.type = V_ASN1_INTEGER;
226 /* If the top bit is set the asn1 encoding is 1 larger. */ 228 /* If the top bit is set the asn1 encoding is 1 larger. */
227 buf[0]=0xff; 229 buf[0] = 0xff;
228 230
229 i=i2d_ASN1_INTEGER(&bs,NULL); 231 i = i2d_ASN1_INTEGER(&bs, NULL);
230 i+=i; /* r and s */ 232 i += i; /* r and s */
231 ret=ASN1_object_size(1,i,V_ASN1_SEQUENCE); 233 ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
232 BN_clear_free(order); 234 BN_clear_free(order);
233 return(ret); 235 return (ret);
234} 236}
235 237
236 238int
237int ECDSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 239ECDSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
238 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 240 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
239{ 241{
240 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ECDSA, argl, argp, 242 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ECDSA, argl, argp,
241 new_func, dup_func, free_func); 243 new_func, dup_func, free_func);
242} 244}
243 245
244int ECDSA_set_ex_data(EC_KEY *d, int idx, void *arg) 246int
247ECDSA_set_ex_data(EC_KEY *d, int idx, void *arg)
245{ 248{
246 ECDSA_DATA *ecdsa; 249 ECDSA_DATA *ecdsa;
247 ecdsa = ecdsa_check(d); 250 ecdsa = ecdsa_check(d);
248 if (ecdsa == NULL) 251 if (ecdsa == NULL)
249 return 0; 252 return 0;
250 return(CRYPTO_set_ex_data(&ecdsa->ex_data,idx,arg)); 253 return (CRYPTO_set_ex_data(&ecdsa->ex_data, idx, arg));
251} 254}
252 255
253void *ECDSA_get_ex_data(EC_KEY *d, int idx) 256void *
257ECDSA_get_ex_data(EC_KEY *d, int idx)
254{ 258{
255 ECDSA_DATA *ecdsa; 259 ECDSA_DATA *ecdsa;
256 ecdsa = ecdsa_check(d); 260 ecdsa = ecdsa_check(d);
257 if (ecdsa == NULL) 261 if (ecdsa == NULL)
258 return NULL; 262 return NULL;
259 return(CRYPTO_get_ex_data(&ecdsa->ex_data,idx)); 263 return (CRYPTO_get_ex_data(&ecdsa->ex_data, idx));
260} 264}