blob: f79055cfd4a0ca1446f3c722f37ffd8f709bd32e [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002 * Wrapper functions for OpenSSL libcrypto
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003 * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10#include <openssl/opensslv.h>
11#include <openssl/err.h>
12#include <openssl/des.h>
13#include <openssl/aes.h>
14#include <openssl/bn.h>
15#include <openssl/evp.h>
16#include <openssl/dh.h>
Dmitry Shmidt04949592012-07-19 12:16:46 -070017#include <openssl/hmac.h>
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070018#include <openssl/rand.h>
19#ifdef CONFIG_OPENSSL_CMAC
20#include <openssl/cmac.h>
21#endif /* CONFIG_OPENSSL_CMAC */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080022#ifdef CONFIG_ECC
23#include <openssl/ec.h>
24#endif /* CONFIG_ECC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025
26#include "common.h"
27#include "wpabuf.h"
28#include "dh_group5.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080029#include "sha1.h"
30#include "sha256.h"
Dmitry Shmidt807291d2015-01-27 13:40:23 -080031#include "sha384.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032#include "crypto.h"
33
34#if OPENSSL_VERSION_NUMBER < 0x00907000
35#define DES_key_schedule des_key_schedule
36#define DES_cblock des_cblock
37#define DES_set_key(key, schedule) des_set_key((key), *(schedule))
38#define DES_ecb_encrypt(input, output, ks, enc) \
39 des_ecb_encrypt((input), (output), *(ks), (enc))
40#endif /* openssl < 0.9.7 */
41
42static BIGNUM * get_group5_prime(void)
43{
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -070044#if OPENSSL_VERSION_NUMBER < 0x00908000 || defined(OPENSSL_IS_BORINGSSL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045 static const unsigned char RFC3526_PRIME_1536[] = {
46 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,
47 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
48 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6,
49 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
50 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D,
51 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
52 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9,
53 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
54 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11,
55 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
56 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36,
57 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
58 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56,
59 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
60 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08,
61 0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
62 };
63 return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL);
64#else /* openssl < 0.9.8 */
65 return get_rfc3526_prime_1536(NULL);
66#endif /* openssl < 0.9.8 */
67}
68
69#if OPENSSL_VERSION_NUMBER < 0x00908000
70#ifndef OPENSSL_NO_SHA256
71#ifndef OPENSSL_FIPS
72#define NO_SHA256_WRAPPER
73#endif
74#endif
75
76#endif /* openssl < 0.9.8 */
77
78#ifdef OPENSSL_NO_SHA256
79#define NO_SHA256_WRAPPER
80#endif
81
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070082static int openssl_digest_vector(const EVP_MD *type, size_t num_elem,
83 const u8 *addr[], const size_t *len, u8 *mac)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070084{
85 EVP_MD_CTX ctx;
86 size_t i;
87 unsigned int mac_len;
88
89 EVP_MD_CTX_init(&ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070090 if (!EVP_DigestInit_ex(&ctx, type, NULL)) {
91 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
92 ERR_error_string(ERR_get_error(), NULL));
93 return -1;
94 }
95 for (i = 0; i < num_elem; i++) {
96 if (!EVP_DigestUpdate(&ctx, addr[i], len[i])) {
97 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
98 "failed: %s",
99 ERR_error_string(ERR_get_error(), NULL));
100 return -1;
101 }
102 }
103 if (!EVP_DigestFinal(&ctx, mac, &mac_len)) {
104 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
105 ERR_error_string(ERR_get_error(), NULL));
106 return -1;
107 }
108
109 return 0;
110}
111
112
113int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
114{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700115 return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116}
117
118
119void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
120{
121 u8 pkey[8], next, tmp;
122 int i;
123 DES_key_schedule ks;
124
125 /* Add parity bits to the key */
126 next = 0;
127 for (i = 0; i < 7; i++) {
128 tmp = key[i];
129 pkey[i] = (tmp >> i) | next | 1;
130 next = tmp << (7 - i);
131 }
132 pkey[i] = next | 1;
133
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700134 DES_set_key((DES_cblock *) &pkey, &ks);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700135 DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
136 DES_ENCRYPT);
137}
138
139
140int rc4_skip(const u8 *key, size_t keylen, size_t skip,
141 u8 *data, size_t data_len)
142{
143#ifdef OPENSSL_NO_RC4
144 return -1;
145#else /* OPENSSL_NO_RC4 */
146 EVP_CIPHER_CTX ctx;
147 int outl;
148 int res = -1;
149 unsigned char skip_buf[16];
150
151 EVP_CIPHER_CTX_init(&ctx);
152 if (!EVP_CIPHER_CTX_set_padding(&ctx, 0) ||
153 !EVP_CipherInit_ex(&ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
154 !EVP_CIPHER_CTX_set_key_length(&ctx, keylen) ||
155 !EVP_CipherInit_ex(&ctx, NULL, NULL, key, NULL, 1))
156 goto out;
157
158 while (skip >= sizeof(skip_buf)) {
159 size_t len = skip;
160 if (len > sizeof(skip_buf))
161 len = sizeof(skip_buf);
162 if (!EVP_CipherUpdate(&ctx, skip_buf, &outl, skip_buf, len))
163 goto out;
164 skip -= len;
165 }
166
167 if (EVP_CipherUpdate(&ctx, data, &outl, data, data_len))
168 res = 0;
169
170out:
171 EVP_CIPHER_CTX_cleanup(&ctx);
172 return res;
173#endif /* OPENSSL_NO_RC4 */
174}
175
176
177int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
178{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700179 return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700180}
181
182
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700183int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
184{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700185 return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700186}
187
188
189#ifndef NO_SHA256_WRAPPER
190int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
191 u8 *mac)
192{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700193 return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194}
195#endif /* NO_SHA256_WRAPPER */
196
197
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700198static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen)
199{
200 switch (keylen) {
201 case 16:
202 return EVP_aes_128_ecb();
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700203#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700204 case 24:
205 return EVP_aes_192_ecb();
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700206#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700207 case 32:
208 return EVP_aes_256_ecb();
209 }
210
211 return NULL;
212}
213
214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215void * aes_encrypt_init(const u8 *key, size_t len)
216{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700217 EVP_CIPHER_CTX *ctx;
218 const EVP_CIPHER *type;
219
220 type = aes_get_evp_cipher(len);
221 if (type == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700223
224 ctx = os_malloc(sizeof(*ctx));
225 if (ctx == NULL)
226 return NULL;
227 EVP_CIPHER_CTX_init(ctx);
228 if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
229 os_free(ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230 return NULL;
231 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700232 EVP_CIPHER_CTX_set_padding(ctx, 0);
233 return ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234}
235
236
237void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
238{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700239 EVP_CIPHER_CTX *c = ctx;
240 int clen = 16;
241 if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) {
242 wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s",
243 ERR_error_string(ERR_get_error(), NULL));
244 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245}
246
247
248void aes_encrypt_deinit(void *ctx)
249{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700250 EVP_CIPHER_CTX *c = ctx;
251 u8 buf[16];
252 int len = sizeof(buf);
253 if (EVP_EncryptFinal_ex(c, buf, &len) != 1) {
254 wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_ex failed: "
255 "%s", ERR_error_string(ERR_get_error(), NULL));
256 }
257 if (len != 0) {
258 wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
259 "in AES encrypt", len);
260 }
261 EVP_CIPHER_CTX_cleanup(c);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800262 bin_clear_free(c, sizeof(*c));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700263}
264
265
266void * aes_decrypt_init(const u8 *key, size_t len)
267{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700268 EVP_CIPHER_CTX *ctx;
269 const EVP_CIPHER *type;
270
271 type = aes_get_evp_cipher(len);
272 if (type == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700273 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700274
275 ctx = os_malloc(sizeof(*ctx));
276 if (ctx == NULL)
277 return NULL;
278 EVP_CIPHER_CTX_init(ctx);
279 if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
280 os_free(ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281 return NULL;
282 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700283 EVP_CIPHER_CTX_set_padding(ctx, 0);
284 return ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285}
286
287
288void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
289{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700290 EVP_CIPHER_CTX *c = ctx;
291 int plen = 16;
292 if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) {
293 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s",
294 ERR_error_string(ERR_get_error(), NULL));
295 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700296}
297
298
299void aes_decrypt_deinit(void *ctx)
300{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700301 EVP_CIPHER_CTX *c = ctx;
302 u8 buf[16];
303 int len = sizeof(buf);
304 if (EVP_DecryptFinal_ex(c, buf, &len) != 1) {
305 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: "
306 "%s", ERR_error_string(ERR_get_error(), NULL));
307 }
308 if (len != 0) {
309 wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
310 "in AES decrypt", len);
311 }
312 EVP_CIPHER_CTX_cleanup(c);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800313 bin_clear_free(c, sizeof(*c));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314}
315
316
317int crypto_mod_exp(const u8 *base, size_t base_len,
318 const u8 *power, size_t power_len,
319 const u8 *modulus, size_t modulus_len,
320 u8 *result, size_t *result_len)
321{
322 BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
323 int ret = -1;
324 BN_CTX *ctx;
325
326 ctx = BN_CTX_new();
327 if (ctx == NULL)
328 return -1;
329
330 bn_base = BN_bin2bn(base, base_len, NULL);
331 bn_exp = BN_bin2bn(power, power_len, NULL);
332 bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
333 bn_result = BN_new();
334
335 if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
336 bn_result == NULL)
337 goto error;
338
339 if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
340 goto error;
341
342 *result_len = BN_bn2bin(bn_result, result);
343 ret = 0;
344
345error:
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700346 BN_clear_free(bn_base);
347 BN_clear_free(bn_exp);
348 BN_clear_free(bn_modulus);
349 BN_clear_free(bn_result);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350 BN_CTX_free(ctx);
351 return ret;
352}
353
354
355struct crypto_cipher {
356 EVP_CIPHER_CTX enc;
357 EVP_CIPHER_CTX dec;
358};
359
360
361struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
362 const u8 *iv, const u8 *key,
363 size_t key_len)
364{
365 struct crypto_cipher *ctx;
366 const EVP_CIPHER *cipher;
367
368 ctx = os_zalloc(sizeof(*ctx));
369 if (ctx == NULL)
370 return NULL;
371
372 switch (alg) {
373#ifndef OPENSSL_NO_RC4
374 case CRYPTO_CIPHER_ALG_RC4:
375 cipher = EVP_rc4();
376 break;
377#endif /* OPENSSL_NO_RC4 */
378#ifndef OPENSSL_NO_AES
379 case CRYPTO_CIPHER_ALG_AES:
380 switch (key_len) {
381 case 16:
382 cipher = EVP_aes_128_cbc();
383 break;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700384#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385 case 24:
386 cipher = EVP_aes_192_cbc();
387 break;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700388#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700389 case 32:
390 cipher = EVP_aes_256_cbc();
391 break;
392 default:
393 os_free(ctx);
394 return NULL;
395 }
396 break;
397#endif /* OPENSSL_NO_AES */
398#ifndef OPENSSL_NO_DES
399 case CRYPTO_CIPHER_ALG_3DES:
400 cipher = EVP_des_ede3_cbc();
401 break;
402 case CRYPTO_CIPHER_ALG_DES:
403 cipher = EVP_des_cbc();
404 break;
405#endif /* OPENSSL_NO_DES */
406#ifndef OPENSSL_NO_RC2
407 case CRYPTO_CIPHER_ALG_RC2:
408 cipher = EVP_rc2_ecb();
409 break;
410#endif /* OPENSSL_NO_RC2 */
411 default:
412 os_free(ctx);
413 return NULL;
414 }
415
416 EVP_CIPHER_CTX_init(&ctx->enc);
417 EVP_CIPHER_CTX_set_padding(&ctx->enc, 0);
418 if (!EVP_EncryptInit_ex(&ctx->enc, cipher, NULL, NULL, NULL) ||
419 !EVP_CIPHER_CTX_set_key_length(&ctx->enc, key_len) ||
420 !EVP_EncryptInit_ex(&ctx->enc, NULL, NULL, key, iv)) {
421 EVP_CIPHER_CTX_cleanup(&ctx->enc);
422 os_free(ctx);
423 return NULL;
424 }
425
426 EVP_CIPHER_CTX_init(&ctx->dec);
427 EVP_CIPHER_CTX_set_padding(&ctx->dec, 0);
428 if (!EVP_DecryptInit_ex(&ctx->dec, cipher, NULL, NULL, NULL) ||
429 !EVP_CIPHER_CTX_set_key_length(&ctx->dec, key_len) ||
430 !EVP_DecryptInit_ex(&ctx->dec, NULL, NULL, key, iv)) {
431 EVP_CIPHER_CTX_cleanup(&ctx->enc);
432 EVP_CIPHER_CTX_cleanup(&ctx->dec);
433 os_free(ctx);
434 return NULL;
435 }
436
437 return ctx;
438}
439
440
441int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
442 u8 *crypt, size_t len)
443{
444 int outl;
445 if (!EVP_EncryptUpdate(&ctx->enc, crypt, &outl, plain, len))
446 return -1;
447 return 0;
448}
449
450
451int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
452 u8 *plain, size_t len)
453{
454 int outl;
455 outl = len;
456 if (!EVP_DecryptUpdate(&ctx->dec, plain, &outl, crypt, len))
457 return -1;
458 return 0;
459}
460
461
462void crypto_cipher_deinit(struct crypto_cipher *ctx)
463{
464 EVP_CIPHER_CTX_cleanup(&ctx->enc);
465 EVP_CIPHER_CTX_cleanup(&ctx->dec);
466 os_free(ctx);
467}
468
469
470void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
471{
472 DH *dh;
473 struct wpabuf *pubkey = NULL, *privkey = NULL;
474 size_t publen, privlen;
475
476 *priv = NULL;
477 *publ = NULL;
478
479 dh = DH_new();
480 if (dh == NULL)
481 return NULL;
482
483 dh->g = BN_new();
484 if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
485 goto err;
486
487 dh->p = get_group5_prime();
488 if (dh->p == NULL)
489 goto err;
490
491 if (DH_generate_key(dh) != 1)
492 goto err;
493
494 publen = BN_num_bytes(dh->pub_key);
495 pubkey = wpabuf_alloc(publen);
496 if (pubkey == NULL)
497 goto err;
498 privlen = BN_num_bytes(dh->priv_key);
499 privkey = wpabuf_alloc(privlen);
500 if (privkey == NULL)
501 goto err;
502
503 BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
504 BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
505
506 *priv = privkey;
507 *publ = pubkey;
508 return dh;
509
510err:
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800511 wpabuf_clear_free(pubkey);
512 wpabuf_clear_free(privkey);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513 DH_free(dh);
514 return NULL;
515}
516
517
Dmitry Shmidt04949592012-07-19 12:16:46 -0700518void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
519{
520 DH *dh;
521
522 dh = DH_new();
523 if (dh == NULL)
524 return NULL;
525
526 dh->g = BN_new();
527 if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
528 goto err;
529
530 dh->p = get_group5_prime();
531 if (dh->p == NULL)
532 goto err;
533
534 dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
535 if (dh->priv_key == NULL)
536 goto err;
537
538 dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
539 if (dh->pub_key == NULL)
540 goto err;
541
542 if (DH_generate_key(dh) != 1)
543 goto err;
544
545 return dh;
546
547err:
548 DH_free(dh);
549 return NULL;
550}
551
552
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
554 const struct wpabuf *own_private)
555{
556 BIGNUM *pub_key;
557 struct wpabuf *res = NULL;
558 size_t rlen;
559 DH *dh = ctx;
560 int keylen;
561
562 if (ctx == NULL)
563 return NULL;
564
565 pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
566 NULL);
567 if (pub_key == NULL)
568 return NULL;
569
570 rlen = DH_size(dh);
571 res = wpabuf_alloc(rlen);
572 if (res == NULL)
573 goto err;
574
575 keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
576 if (keylen < 0)
577 goto err;
578 wpabuf_put(res, keylen);
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700579 BN_clear_free(pub_key);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700580
581 return res;
582
583err:
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700584 BN_clear_free(pub_key);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800585 wpabuf_clear_free(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700586 return NULL;
587}
588
589
590void dh5_free(void *ctx)
591{
592 DH *dh;
593 if (ctx == NULL)
594 return;
595 dh = ctx;
596 DH_free(dh);
597}
Dmitry Shmidt04949592012-07-19 12:16:46 -0700598
599
600struct crypto_hash {
601 HMAC_CTX ctx;
602};
603
604
605struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
606 size_t key_len)
607{
608 struct crypto_hash *ctx;
609 const EVP_MD *md;
610
611 switch (alg) {
612#ifndef OPENSSL_NO_MD5
613 case CRYPTO_HASH_ALG_HMAC_MD5:
614 md = EVP_md5();
615 break;
616#endif /* OPENSSL_NO_MD5 */
617#ifndef OPENSSL_NO_SHA
618 case CRYPTO_HASH_ALG_HMAC_SHA1:
619 md = EVP_sha1();
620 break;
621#endif /* OPENSSL_NO_SHA */
622#ifndef OPENSSL_NO_SHA256
623#ifdef CONFIG_SHA256
624 case CRYPTO_HASH_ALG_HMAC_SHA256:
625 md = EVP_sha256();
626 break;
627#endif /* CONFIG_SHA256 */
628#endif /* OPENSSL_NO_SHA256 */
629 default:
630 return NULL;
631 }
632
633 ctx = os_zalloc(sizeof(*ctx));
634 if (ctx == NULL)
635 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700636 HMAC_CTX_init(&ctx->ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700637
638#if OPENSSL_VERSION_NUMBER < 0x00909000
639 HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL);
640#else /* openssl < 0.9.9 */
641 if (HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL) != 1) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800642 bin_clear_free(ctx, sizeof(*ctx));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700643 return NULL;
644 }
645#endif /* openssl < 0.9.9 */
646
647 return ctx;
648}
649
650
651void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
652{
653 if (ctx == NULL)
654 return;
655 HMAC_Update(&ctx->ctx, data, len);
656}
657
658
659int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
660{
661 unsigned int mdlen;
662 int res;
663
664 if (ctx == NULL)
665 return -2;
666
667 if (mac == NULL || len == NULL) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800668 bin_clear_free(ctx, sizeof(*ctx));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700669 return 0;
670 }
671
672 mdlen = *len;
673#if OPENSSL_VERSION_NUMBER < 0x00909000
674 HMAC_Final(&ctx->ctx, mac, &mdlen);
675 res = 1;
676#else /* openssl < 0.9.9 */
677 res = HMAC_Final(&ctx->ctx, mac, &mdlen);
678#endif /* openssl < 0.9.9 */
679 HMAC_CTX_cleanup(&ctx->ctx);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800680 bin_clear_free(ctx, sizeof(*ctx));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700681
682 if (res == 1) {
683 *len = mdlen;
684 return 0;
685 }
686
687 return -1;
688}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700689
690
691int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
692 int iterations, u8 *buf, size_t buflen)
693{
694#if OPENSSL_VERSION_NUMBER < 0x00908000
695 if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase),
696 (unsigned char *) ssid,
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800697 ssid_len, iterations, buflen, buf) != 1)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700698 return -1;
699#else /* openssl < 0.9.8 */
700 if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800701 ssid_len, iterations, buflen, buf) != 1)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700702 return -1;
703#endif /* openssl < 0.9.8 */
704 return 0;
705}
706
707
708int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
709 const u8 *addr[], const size_t *len, u8 *mac)
710{
711 HMAC_CTX ctx;
712 size_t i;
713 unsigned int mdlen;
714 int res;
715
716 HMAC_CTX_init(&ctx);
717#if OPENSSL_VERSION_NUMBER < 0x00909000
718 HMAC_Init_ex(&ctx, key, key_len, EVP_sha1(), NULL);
719#else /* openssl < 0.9.9 */
720 if (HMAC_Init_ex(&ctx, key, key_len, EVP_sha1(), NULL) != 1)
721 return -1;
722#endif /* openssl < 0.9.9 */
723
724 for (i = 0; i < num_elem; i++)
725 HMAC_Update(&ctx, addr[i], len[i]);
726
727 mdlen = 20;
728#if OPENSSL_VERSION_NUMBER < 0x00909000
729 HMAC_Final(&ctx, mac, &mdlen);
730 res = 1;
731#else /* openssl < 0.9.9 */
732 res = HMAC_Final(&ctx, mac, &mdlen);
733#endif /* openssl < 0.9.9 */
734 HMAC_CTX_cleanup(&ctx);
735
736 return res == 1 ? 0 : -1;
737}
738
739
740int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
741 u8 *mac)
742{
743 return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
744}
745
746
747#ifdef CONFIG_SHA256
748
749int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
750 const u8 *addr[], const size_t *len, u8 *mac)
751{
752 HMAC_CTX ctx;
753 size_t i;
754 unsigned int mdlen;
755 int res;
756
757 HMAC_CTX_init(&ctx);
758#if OPENSSL_VERSION_NUMBER < 0x00909000
759 HMAC_Init_ex(&ctx, key, key_len, EVP_sha256(), NULL);
760#else /* openssl < 0.9.9 */
761 if (HMAC_Init_ex(&ctx, key, key_len, EVP_sha256(), NULL) != 1)
762 return -1;
763#endif /* openssl < 0.9.9 */
764
765 for (i = 0; i < num_elem; i++)
766 HMAC_Update(&ctx, addr[i], len[i]);
767
768 mdlen = 32;
769#if OPENSSL_VERSION_NUMBER < 0x00909000
770 HMAC_Final(&ctx, mac, &mdlen);
771 res = 1;
772#else /* openssl < 0.9.9 */
773 res = HMAC_Final(&ctx, mac, &mdlen);
774#endif /* openssl < 0.9.9 */
775 HMAC_CTX_cleanup(&ctx);
776
777 return res == 1 ? 0 : -1;
778}
779
780
781int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
782 size_t data_len, u8 *mac)
783{
784 return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
785}
786
787#endif /* CONFIG_SHA256 */
788
789
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800790#ifdef CONFIG_SHA384
791
792int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem,
793 const u8 *addr[], const size_t *len, u8 *mac)
794{
795 HMAC_CTX ctx;
796 size_t i;
797 unsigned int mdlen;
798 int res;
799
800 HMAC_CTX_init(&ctx);
801 if (HMAC_Init_ex(&ctx, key, key_len, EVP_sha384(), NULL) != 1)
802 return -1;
803
804 for (i = 0; i < num_elem; i++)
805 HMAC_Update(&ctx, addr[i], len[i]);
806
807 mdlen = 32;
808 res = HMAC_Final(&ctx, mac, &mdlen);
809 HMAC_CTX_cleanup(&ctx);
810
811 return res == 1 ? 0 : -1;
812}
813
814
815int hmac_sha384(const u8 *key, size_t key_len, const u8 *data,
816 size_t data_len, u8 *mac)
817{
818 return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac);
819}
820
821#endif /* CONFIG_SHA384 */
822
823
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700824int crypto_get_random(void *buf, size_t len)
825{
826 if (RAND_bytes(buf, len) != 1)
827 return -1;
828 return 0;
829}
830
831
832#ifdef CONFIG_OPENSSL_CMAC
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800833int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem,
834 const u8 *addr[], const size_t *len, u8 *mac)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700835{
836 CMAC_CTX *ctx;
837 int ret = -1;
838 size_t outlen, i;
839
840 ctx = CMAC_CTX_new();
841 if (ctx == NULL)
842 return -1;
843
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800844 if (key_len == 32) {
845 if (!CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL))
846 goto fail;
847 } else if (key_len == 16) {
848 if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
849 goto fail;
850 } else {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700851 goto fail;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800852 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700853 for (i = 0; i < num_elem; i++) {
854 if (!CMAC_Update(ctx, addr[i], len[i]))
855 goto fail;
856 }
857 if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16)
858 goto fail;
859
860 ret = 0;
861fail:
862 CMAC_CTX_free(ctx);
863 return ret;
864}
865
866
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800867int omac1_aes_128_vector(const u8 *key, size_t num_elem,
868 const u8 *addr[], const size_t *len, u8 *mac)
869{
870 return omac1_aes_vector(key, 16, num_elem, addr, len, mac);
871}
872
873
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700874int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
875{
876 return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
877}
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800878
879
880int omac1_aes_256(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
881{
882 return omac1_aes_vector(key, 32, 1, &data, &data_len, mac);
883}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700884#endif /* CONFIG_OPENSSL_CMAC */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800885
886
887struct crypto_bignum * crypto_bignum_init(void)
888{
889 return (struct crypto_bignum *) BN_new();
890}
891
892
893struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len)
894{
895 BIGNUM *bn = BN_bin2bn(buf, len, NULL);
896 return (struct crypto_bignum *) bn;
897}
898
899
900void crypto_bignum_deinit(struct crypto_bignum *n, int clear)
901{
902 if (clear)
903 BN_clear_free((BIGNUM *) n);
904 else
905 BN_free((BIGNUM *) n);
906}
907
908
909int crypto_bignum_to_bin(const struct crypto_bignum *a,
910 u8 *buf, size_t buflen, size_t padlen)
911{
912 int num_bytes, offset;
913
914 if (padlen > buflen)
915 return -1;
916
917 num_bytes = BN_num_bytes((const BIGNUM *) a);
918 if ((size_t) num_bytes > buflen)
919 return -1;
920 if (padlen > (size_t) num_bytes)
921 offset = padlen - num_bytes;
922 else
923 offset = 0;
924
925 os_memset(buf, 0, offset);
926 BN_bn2bin((const BIGNUM *) a, buf + offset);
927
928 return num_bytes + offset;
929}
930
931
932int crypto_bignum_add(const struct crypto_bignum *a,
933 const struct crypto_bignum *b,
934 struct crypto_bignum *c)
935{
936 return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
937 0 : -1;
938}
939
940
941int crypto_bignum_mod(const struct crypto_bignum *a,
942 const struct crypto_bignum *b,
943 struct crypto_bignum *c)
944{
945 int res;
946 BN_CTX *bnctx;
947
948 bnctx = BN_CTX_new();
949 if (bnctx == NULL)
950 return -1;
951 res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
952 bnctx);
953 BN_CTX_free(bnctx);
954
955 return res ? 0 : -1;
956}
957
958
959int crypto_bignum_exptmod(const struct crypto_bignum *a,
960 const struct crypto_bignum *b,
961 const struct crypto_bignum *c,
962 struct crypto_bignum *d)
963{
964 int res;
965 BN_CTX *bnctx;
966
967 bnctx = BN_CTX_new();
968 if (bnctx == NULL)
969 return -1;
970 res = BN_mod_exp((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
971 (const BIGNUM *) c, bnctx);
972 BN_CTX_free(bnctx);
973
974 return res ? 0 : -1;
975}
976
977
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800978int crypto_bignum_inverse(const struct crypto_bignum *a,
979 const struct crypto_bignum *b,
980 struct crypto_bignum *c)
981{
982 BIGNUM *res;
983 BN_CTX *bnctx;
984
985 bnctx = BN_CTX_new();
986 if (bnctx == NULL)
987 return -1;
988 res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a,
989 (const BIGNUM *) b, bnctx);
990 BN_CTX_free(bnctx);
991
992 return res ? 0 : -1;
993}
994
995
996int crypto_bignum_sub(const struct crypto_bignum *a,
997 const struct crypto_bignum *b,
998 struct crypto_bignum *c)
999{
1000 return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
1001 0 : -1;
1002}
1003
1004
1005int crypto_bignum_div(const struct crypto_bignum *a,
1006 const struct crypto_bignum *b,
1007 struct crypto_bignum *c)
1008{
1009 int res;
1010
1011 BN_CTX *bnctx;
1012
1013 bnctx = BN_CTX_new();
1014 if (bnctx == NULL)
1015 return -1;
1016 res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a,
1017 (const BIGNUM *) b, bnctx);
1018 BN_CTX_free(bnctx);
1019
1020 return res ? 0 : -1;
1021}
1022
1023
1024int crypto_bignum_mulmod(const struct crypto_bignum *a,
1025 const struct crypto_bignum *b,
1026 const struct crypto_bignum *c,
1027 struct crypto_bignum *d)
1028{
1029 int res;
1030
1031 BN_CTX *bnctx;
1032
1033 bnctx = BN_CTX_new();
1034 if (bnctx == NULL)
1035 return -1;
1036 res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
1037 (const BIGNUM *) c, bnctx);
1038 BN_CTX_free(bnctx);
1039
1040 return res ? 0 : -1;
1041}
1042
1043
1044int crypto_bignum_cmp(const struct crypto_bignum *a,
1045 const struct crypto_bignum *b)
1046{
1047 return BN_cmp((const BIGNUM *) a, (const BIGNUM *) b);
1048}
1049
1050
1051int crypto_bignum_bits(const struct crypto_bignum *a)
1052{
1053 return BN_num_bits((const BIGNUM *) a);
1054}
1055
1056
1057int crypto_bignum_is_zero(const struct crypto_bignum *a)
1058{
1059 return BN_is_zero((const BIGNUM *) a);
1060}
1061
1062
1063int crypto_bignum_is_one(const struct crypto_bignum *a)
1064{
1065 return BN_is_one((const BIGNUM *) a);
1066}
1067
1068
1069#ifdef CONFIG_ECC
1070
1071struct crypto_ec {
1072 EC_GROUP *group;
1073 BN_CTX *bnctx;
1074 BIGNUM *prime;
1075 BIGNUM *order;
1076};
1077
1078struct crypto_ec * crypto_ec_init(int group)
1079{
1080 struct crypto_ec *e;
1081 int nid;
1082
1083 /* Map from IANA registry for IKE D-H groups to OpenSSL NID */
1084 switch (group) {
1085 case 19:
1086 nid = NID_X9_62_prime256v1;
1087 break;
1088 case 20:
1089 nid = NID_secp384r1;
1090 break;
1091 case 21:
1092 nid = NID_secp521r1;
1093 break;
1094 case 25:
1095 nid = NID_X9_62_prime192v1;
1096 break;
1097 case 26:
1098 nid = NID_secp224r1;
1099 break;
1100 default:
1101 return NULL;
1102 }
1103
1104 e = os_zalloc(sizeof(*e));
1105 if (e == NULL)
1106 return NULL;
1107
1108 e->bnctx = BN_CTX_new();
1109 e->group = EC_GROUP_new_by_curve_name(nid);
1110 e->prime = BN_new();
1111 e->order = BN_new();
1112 if (e->group == NULL || e->bnctx == NULL || e->prime == NULL ||
1113 e->order == NULL ||
1114 !EC_GROUP_get_curve_GFp(e->group, e->prime, NULL, NULL, e->bnctx) ||
1115 !EC_GROUP_get_order(e->group, e->order, e->bnctx)) {
1116 crypto_ec_deinit(e);
1117 e = NULL;
1118 }
1119
1120 return e;
1121}
1122
1123
1124void crypto_ec_deinit(struct crypto_ec *e)
1125{
1126 if (e == NULL)
1127 return;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001128 BN_clear_free(e->order);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001129 BN_clear_free(e->prime);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001130 EC_GROUP_free(e->group);
1131 BN_CTX_free(e->bnctx);
1132 os_free(e);
1133}
1134
1135
1136struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e)
1137{
1138 if (e == NULL)
1139 return NULL;
1140 return (struct crypto_ec_point *) EC_POINT_new(e->group);
1141}
1142
1143
1144size_t crypto_ec_prime_len(struct crypto_ec *e)
1145{
1146 return BN_num_bytes(e->prime);
1147}
1148
1149
1150size_t crypto_ec_prime_len_bits(struct crypto_ec *e)
1151{
1152 return BN_num_bits(e->prime);
1153}
1154
1155
1156const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e)
1157{
1158 return (const struct crypto_bignum *) e->prime;
1159}
1160
1161
1162const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e)
1163{
1164 return (const struct crypto_bignum *) e->order;
1165}
1166
1167
1168void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear)
1169{
1170 if (clear)
1171 EC_POINT_clear_free((EC_POINT *) p);
1172 else
1173 EC_POINT_free((EC_POINT *) p);
1174}
1175
1176
1177int crypto_ec_point_to_bin(struct crypto_ec *e,
1178 const struct crypto_ec_point *point, u8 *x, u8 *y)
1179{
1180 BIGNUM *x_bn, *y_bn;
1181 int ret = -1;
1182 int len = BN_num_bytes(e->prime);
1183
1184 x_bn = BN_new();
1185 y_bn = BN_new();
1186
1187 if (x_bn && y_bn &&
1188 EC_POINT_get_affine_coordinates_GFp(e->group, (EC_POINT *) point,
1189 x_bn, y_bn, e->bnctx)) {
1190 if (x) {
1191 crypto_bignum_to_bin((struct crypto_bignum *) x_bn,
1192 x, len, len);
1193 }
1194 if (y) {
1195 crypto_bignum_to_bin((struct crypto_bignum *) y_bn,
1196 y, len, len);
1197 }
1198 ret = 0;
1199 }
1200
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001201 BN_clear_free(x_bn);
1202 BN_clear_free(y_bn);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001203 return ret;
1204}
1205
1206
1207struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
1208 const u8 *val)
1209{
1210 BIGNUM *x, *y;
1211 EC_POINT *elem;
1212 int len = BN_num_bytes(e->prime);
1213
1214 x = BN_bin2bn(val, len, NULL);
1215 y = BN_bin2bn(val + len, len, NULL);
1216 elem = EC_POINT_new(e->group);
1217 if (x == NULL || y == NULL || elem == NULL) {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001218 BN_clear_free(x);
1219 BN_clear_free(y);
1220 EC_POINT_clear_free(elem);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001221 return NULL;
1222 }
1223
1224 if (!EC_POINT_set_affine_coordinates_GFp(e->group, elem, x, y,
1225 e->bnctx)) {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001226 EC_POINT_clear_free(elem);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001227 elem = NULL;
1228 }
1229
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001230 BN_clear_free(x);
1231 BN_clear_free(y);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001232
1233 return (struct crypto_ec_point *) elem;
1234}
1235
1236
1237int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
1238 const struct crypto_ec_point *b,
1239 struct crypto_ec_point *c)
1240{
1241 return EC_POINT_add(e->group, (EC_POINT *) c, (const EC_POINT *) a,
1242 (const EC_POINT *) b, e->bnctx) ? 0 : -1;
1243}
1244
1245
1246int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
1247 const struct crypto_bignum *b,
1248 struct crypto_ec_point *res)
1249{
1250 return EC_POINT_mul(e->group, (EC_POINT *) res, NULL,
1251 (const EC_POINT *) p, (const BIGNUM *) b, e->bnctx)
1252 ? 0 : -1;
1253}
1254
1255
1256int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p)
1257{
1258 return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1;
1259}
1260
1261
1262int crypto_ec_point_solve_y_coord(struct crypto_ec *e,
1263 struct crypto_ec_point *p,
1264 const struct crypto_bignum *x, int y_bit)
1265{
1266 if (!EC_POINT_set_compressed_coordinates_GFp(e->group, (EC_POINT *) p,
1267 (const BIGNUM *) x, y_bit,
1268 e->bnctx) ||
1269 !EC_POINT_is_on_curve(e->group, (EC_POINT *) p, e->bnctx))
1270 return -1;
1271 return 0;
1272}
1273
1274
1275int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
1276 const struct crypto_ec_point *p)
1277{
1278 return EC_POINT_is_at_infinity(e->group, (const EC_POINT *) p);
1279}
1280
1281
1282int crypto_ec_point_is_on_curve(struct crypto_ec *e,
1283 const struct crypto_ec_point *p)
1284{
1285 return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p, e->bnctx);
1286}
1287
1288#endif /* CONFIG_ECC */