]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ec/ecx_key.c
Fix external symbols related to ec & sm2 keys
[thirdparty/openssl.git] / crypto / ec / ecx_key.c
1 /*
2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <openssl/err.h>
11 #include "crypto/ecx.h"
12
13 ECX_KEY *ossl_ecx_key_new(OSSL_LIB_CTX *libctx, ECX_KEY_TYPE type, int haspubkey,
14 const char *propq)
15 {
16 ECX_KEY *ret = OPENSSL_zalloc(sizeof(*ret));
17
18 if (ret == NULL)
19 return NULL;
20
21 ret->libctx = libctx;
22 ret->haspubkey = haspubkey;
23 switch (type) {
24 case ECX_KEY_TYPE_X25519:
25 ret->keylen = X25519_KEYLEN;
26 break;
27 case ECX_KEY_TYPE_X448:
28 ret->keylen = X448_KEYLEN;
29 break;
30 case ECX_KEY_TYPE_ED25519:
31 ret->keylen = ED25519_KEYLEN;
32 break;
33 case ECX_KEY_TYPE_ED448:
34 ret->keylen = ED448_KEYLEN;
35 break;
36 }
37 ret->type = type;
38 ret->references = 1;
39
40 if (propq != NULL) {
41 ret->propq = OPENSSL_strdup(propq);
42 ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
43 if (ret->propq == NULL)
44 goto err;
45 }
46
47 ret->lock = CRYPTO_THREAD_lock_new();
48 if (ret->lock == NULL)
49 goto err;
50 return ret;
51 err:
52 ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
53 OPENSSL_free(ret);
54 return NULL;
55 }
56
57 void ossl_ecx_key_free(ECX_KEY *key)
58 {
59 int i;
60
61 if (key == NULL)
62 return;
63
64 CRYPTO_DOWN_REF(&key->references, &i, key->lock);
65 REF_PRINT_COUNT("ECX_KEY", key);
66 if (i > 0)
67 return;
68 REF_ASSERT_ISNT(i < 0);
69
70 OPENSSL_free(key->propq);
71 OPENSSL_secure_clear_free(key->privkey, key->keylen);
72 CRYPTO_THREAD_lock_free(key->lock);
73 OPENSSL_free(key);
74 }
75
76 void ossl_ecx_key_set0_libctx(ECX_KEY *key, OSSL_LIB_CTX *libctx)
77 {
78 key->libctx = libctx;
79 }
80
81 int ossl_ecx_key_up_ref(ECX_KEY *key)
82 {
83 int i;
84
85 if (CRYPTO_UP_REF(&key->references, &i, key->lock) <= 0)
86 return 0;
87
88 REF_PRINT_COUNT("ECX_KEY", key);
89 REF_ASSERT_ISNT(i < 2);
90 return ((i > 1) ? 1 : 0);
91 }
92
93 unsigned char *ossl_ecx_key_allocate_privkey(ECX_KEY *key)
94 {
95 key->privkey = OPENSSL_secure_zalloc(key->keylen);
96
97 return key->privkey;
98 }