diff options
Diffstat (limited to 'src/lib/libcrypto/ecdh/ech_lib.c')
| -rw-r--r-- | src/lib/libcrypto/ecdh/ech_lib.c | 133 |
1 files changed, 68 insertions, 65 deletions
diff --git a/src/lib/libcrypto/ecdh/ech_lib.c b/src/lib/libcrypto/ecdh/ech_lib.c index 58dddf638f..579dfc4b4f 100644 --- a/src/lib/libcrypto/ecdh/ech_lib.c +++ b/src/lib/libcrypto/ecdh/ech_lib.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: ech_lib.c,v 1.9 2015/09/10 15:56:25 jsing Exp $ */ | 1 | /* $OpenBSD: ech_lib.c,v 1.10 2015/09/13 10:46:20 jsing Exp $ */ |
| 2 | /* ==================================================================== | 2 | /* ==================================================================== |
| 3 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | 3 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 4 | * | 4 | * |
| @@ -21,7 +21,7 @@ | |||
| 21 | * are met: | 21 | * are met: |
| 22 | * | 22 | * |
| 23 | * 1. Redistributions of source code must retain the above copyright | 23 | * 1. Redistributions of source code must retain the above copyright |
| 24 | * notice, this list of conditions and the following disclaimer. | 24 | * notice, this list of conditions and the following disclaimer. |
| 25 | * | 25 | * |
| 26 | * 2. Redistributions in binary form must reproduce the above copyright | 26 | * 2. Redistributions in binary form must reproduce the above copyright |
| 27 | * notice, this list of conditions and the following disclaimer in | 27 | * notice, this list of conditions and the following disclaimer in |
| @@ -83,22 +83,24 @@ static void *ecdh_data_new(void); | |||
| 83 | static void *ecdh_data_dup(void *); | 83 | static void *ecdh_data_dup(void *); |
| 84 | static void ecdh_data_free(void *); | 84 | static void ecdh_data_free(void *); |
| 85 | 85 | ||
| 86 | void ECDH_set_default_method(const ECDH_METHOD *meth) | 86 | void |
| 87 | { | 87 | ECDH_set_default_method(const ECDH_METHOD *meth) |
| 88 | { | ||
| 88 | default_ECDH_method = meth; | 89 | default_ECDH_method = meth; |
| 89 | } | 90 | } |
| 90 | 91 | ||
| 91 | const ECDH_METHOD *ECDH_get_default_method(void) | 92 | const ECDH_METHOD * |
| 92 | { | 93 | ECDH_get_default_method(void) |
| 93 | if(!default_ECDH_method) | 94 | { |
| 94 | { | 95 | if (!default_ECDH_method) { |
| 95 | default_ECDH_method = ECDH_OpenSSL(); | 96 | default_ECDH_method = ECDH_OpenSSL(); |
| 96 | } | ||
| 97 | return default_ECDH_method; | ||
| 98 | } | 97 | } |
| 98 | return default_ECDH_method; | ||
| 99 | } | ||
| 99 | 100 | ||
| 100 | int ECDH_set_method(EC_KEY *eckey, const ECDH_METHOD *meth) | 101 | int |
| 101 | { | 102 | ECDH_set_method(EC_KEY *eckey, const ECDH_METHOD *meth) |
| 103 | { | ||
| 102 | ECDH_DATA *ecdh; | 104 | ECDH_DATA *ecdh; |
| 103 | 105 | ||
| 104 | ecdh = ecdh_check(eckey); | 106 | ecdh = ecdh_check(eckey); |
| @@ -107,26 +109,25 @@ int ECDH_set_method(EC_KEY *eckey, const ECDH_METHOD *meth) | |||
| 107 | return 0; | 109 | return 0; |
| 108 | 110 | ||
| 109 | #ifndef OPENSSL_NO_ENGINE | 111 | #ifndef OPENSSL_NO_ENGINE |
| 110 | if (ecdh->engine) | 112 | if (ecdh->engine) { |
| 111 | { | ||
| 112 | ENGINE_finish(ecdh->engine); | 113 | ENGINE_finish(ecdh->engine); |
| 113 | ecdh->engine = NULL; | 114 | ecdh->engine = NULL; |
| 114 | } | ||
| 115 | #endif | ||
| 116 | ecdh->meth = meth; | ||
| 117 | return 1; | ||
| 118 | } | 115 | } |
| 116 | #endif | ||
| 117 | ecdh->meth = meth; | ||
| 118 | return 1; | ||
| 119 | } | ||
| 119 | 120 | ||
| 120 | static ECDH_DATA *ECDH_DATA_new_method(ENGINE *engine) | 121 | static ECDH_DATA * |
| 121 | { | 122 | ECDH_DATA_new_method(ENGINE *engine) |
| 123 | { | ||
| 122 | ECDH_DATA *ret; | 124 | ECDH_DATA *ret; |
| 123 | 125 | ||
| 124 | ret = malloc(sizeof(ECDH_DATA)); | 126 | ret = malloc(sizeof(ECDH_DATA)); |
| 125 | if (ret == NULL) | 127 | if (ret == NULL) { |
| 126 | { | ||
| 127 | ECDHerr(ECDH_F_ECDH_DATA_NEW_METHOD, ERR_R_MALLOC_FAILURE); | 128 | ECDHerr(ECDH_F_ECDH_DATA_NEW_METHOD, ERR_R_MALLOC_FAILURE); |
| 128 | return(NULL); | 129 | return (NULL); |
| 129 | } | 130 | } |
| 130 | 131 | ||
| 131 | ret->init = NULL; | 132 | ret->init = NULL; |
| 132 | 133 | ||
| @@ -135,30 +136,30 @@ static ECDH_DATA *ECDH_DATA_new_method(ENGINE *engine) | |||
| 135 | #ifndef OPENSSL_NO_ENGINE | 136 | #ifndef OPENSSL_NO_ENGINE |
| 136 | if (!ret->engine) | 137 | if (!ret->engine) |
| 137 | ret->engine = ENGINE_get_default_ECDH(); | 138 | ret->engine = ENGINE_get_default_ECDH(); |
| 138 | if (ret->engine) | 139 | if (ret->engine) { |
| 139 | { | ||
| 140 | ret->meth = ENGINE_get_ECDH(ret->engine); | 140 | ret->meth = ENGINE_get_ECDH(ret->engine); |
| 141 | if (!ret->meth) | 141 | if (!ret->meth) { |
| 142 | { | ||
| 143 | ECDHerr(ECDH_F_ECDH_DATA_NEW_METHOD, ERR_R_ENGINE_LIB); | 142 | ECDHerr(ECDH_F_ECDH_DATA_NEW_METHOD, ERR_R_ENGINE_LIB); |
| 144 | ENGINE_finish(ret->engine); | 143 | ENGINE_finish(ret->engine); |
| 145 | free(ret); | 144 | free(ret); |
| 146 | return NULL; | 145 | return NULL; |
| 147 | } | ||
| 148 | } | 146 | } |
| 147 | } | ||
| 149 | #endif | 148 | #endif |
| 150 | 149 | ||
| 151 | ret->flags = ret->meth->flags; | 150 | ret->flags = ret->meth->flags; |
| 152 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ECDH, ret, &ret->ex_data); | 151 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ECDH, ret, &ret->ex_data); |
| 153 | return(ret); | 152 | return (ret); |
| 154 | } | 153 | } |
| 155 | 154 | ||
| 156 | static void *ecdh_data_new(void) | 155 | static void * |
| 157 | { | 156 | ecdh_data_new(void) |
| 157 | { | ||
| 158 | return (void *)ECDH_DATA_new_method(NULL); | 158 | return (void *)ECDH_DATA_new_method(NULL); |
| 159 | } | 159 | } |
| 160 | 160 | ||
| 161 | static void *ecdh_data_dup(void *data) | 161 | static void * |
| 162 | ecdh_data_dup(void *data) | ||
| 162 | { | 163 | { |
| 163 | ECDH_DATA *r = (ECDH_DATA *)data; | 164 | ECDH_DATA *r = (ECDH_DATA *)data; |
| 164 | 165 | ||
| @@ -169,8 +170,9 @@ static void *ecdh_data_dup(void *data) | |||
| 169 | return (void *)ecdh_data_new(); | 170 | return (void *)ecdh_data_new(); |
| 170 | } | 171 | } |
| 171 | 172 | ||
| 172 | void ecdh_data_free(void *data) | 173 | void |
| 173 | { | 174 | ecdh_data_free(void *data) |
| 175 | { | ||
| 174 | ECDH_DATA *r = (ECDH_DATA *)data; | 176 | ECDH_DATA *r = (ECDH_DATA *)data; |
| 175 | 177 | ||
| 176 | #ifndef OPENSSL_NO_ENGINE | 178 | #ifndef OPENSSL_NO_ENGINE |
| @@ -183,56 +185,57 @@ void ecdh_data_free(void *data) | |||
| 183 | explicit_bzero((void *)r, sizeof(ECDH_DATA)); | 185 | explicit_bzero((void *)r, sizeof(ECDH_DATA)); |
| 184 | 186 | ||
| 185 | free(r); | 187 | free(r); |
| 186 | } | 188 | } |
| 187 | 189 | ||
| 188 | ECDH_DATA *ecdh_check(EC_KEY *key) | 190 | ECDH_DATA * |
| 189 | { | 191 | ecdh_check(EC_KEY *key) |
| 192 | { | ||
| 190 | ECDH_DATA *ecdh_data; | 193 | ECDH_DATA *ecdh_data; |
| 191 | 194 | ||
| 192 | void *data = EC_KEY_get_key_method_data(key, ecdh_data_dup, | 195 | void *data = EC_KEY_get_key_method_data(key, ecdh_data_dup, |
| 193 | ecdh_data_free, ecdh_data_free); | 196 | ecdh_data_free, ecdh_data_free); |
| 194 | if (data == NULL) | 197 | if (data == NULL) { |
| 195 | { | ||
| 196 | ecdh_data = (ECDH_DATA *)ecdh_data_new(); | 198 | ecdh_data = (ECDH_DATA *)ecdh_data_new(); |
| 197 | if (ecdh_data == NULL) | 199 | if (ecdh_data == NULL) |
| 198 | return NULL; | 200 | return NULL; |
| 199 | data = EC_KEY_insert_key_method_data(key, (void *)ecdh_data, | 201 | data = EC_KEY_insert_key_method_data(key, (void *)ecdh_data, |
| 200 | ecdh_data_dup, ecdh_data_free, ecdh_data_free); | 202 | ecdh_data_dup, ecdh_data_free, ecdh_data_free); |
| 201 | if (data != NULL) | 203 | if (data != NULL) { |
| 202 | { | ||
| 203 | /* Another thread raced us to install the key_method | 204 | /* Another thread raced us to install the key_method |
| 204 | * data and won. */ | 205 | * data and won. */ |
| 205 | ecdh_data_free(ecdh_data); | 206 | ecdh_data_free(ecdh_data); |
| 206 | ecdh_data = (ECDH_DATA *)data; | 207 | ecdh_data = (ECDH_DATA *)data; |
| 207 | } | 208 | } |
| 208 | } | 209 | } else |
| 209 | else | ||
| 210 | ecdh_data = (ECDH_DATA *)data; | 210 | ecdh_data = (ECDH_DATA *)data; |
| 211 | 211 | ||
| 212 | return ecdh_data; | 212 | return ecdh_data; |
| 213 | } | 213 | } |
| 214 | 214 | ||
| 215 | int ECDH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | 215 | int |
| 216 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) | 216 | ECDH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, |
| 217 | { | 217 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) |
| 218 | { | ||
| 218 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ECDH, argl, argp, | 219 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ECDH, argl, argp, |
| 219 | new_func, dup_func, free_func); | 220 | new_func, dup_func, free_func); |
| 220 | } | 221 | } |
| 221 | 222 | ||
| 222 | int ECDH_set_ex_data(EC_KEY *d, int idx, void *arg) | 223 | int |
| 223 | { | 224 | ECDH_set_ex_data(EC_KEY *d, int idx, void *arg) |
| 225 | { | ||
| 224 | ECDH_DATA *ecdh; | 226 | ECDH_DATA *ecdh; |
| 225 | ecdh = ecdh_check(d); | 227 | ecdh = ecdh_check(d); |
| 226 | if (ecdh == NULL) | 228 | if (ecdh == NULL) |
| 227 | return 0; | 229 | return 0; |
| 228 | return(CRYPTO_set_ex_data(&ecdh->ex_data,idx,arg)); | 230 | return (CRYPTO_set_ex_data(&ecdh->ex_data, idx, arg)); |
| 229 | } | 231 | } |
| 230 | 232 | ||
| 231 | void *ECDH_get_ex_data(EC_KEY *d, int idx) | 233 | void * |
| 232 | { | 234 | ECDH_get_ex_data(EC_KEY *d, int idx) |
| 235 | { | ||
| 233 | ECDH_DATA *ecdh; | 236 | ECDH_DATA *ecdh; |
| 234 | ecdh = ecdh_check(d); | 237 | ecdh = ecdh_check(d); |
| 235 | if (ecdh == NULL) | 238 | if (ecdh == NULL) |
| 236 | return NULL; | 239 | return NULL; |
| 237 | return(CRYPTO_get_ex_data(&ecdh->ex_data,idx)); | 240 | return (CRYPTO_get_ex_data(&ecdh->ex_data, idx)); |
| 238 | } | 241 | } |
