diff options
| author | miod <> | 2014-07-09 13:26:47 +0000 |
|---|---|---|
| committer | miod <> | 2014-07-09 13:26:47 +0000 |
| commit | 20bc42573a8eb2be6fdb1002a3d413e4fdebabb6 (patch) | |
| tree | efa6e3fe5e9cccf6532f4f9d3fd222d9fbb45764 /src/lib/libcrypto/dh/dh_lib.c | |
| parent | ba945b87624f5f68372aa004088259c15c1b6257 (diff) | |
| download | openbsd-20bc42573a8eb2be6fdb1002a3d413e4fdebabb6.tar.gz openbsd-20bc42573a8eb2be6fdb1002a3d413e4fdebabb6.tar.bz2 openbsd-20bc42573a8eb2be6fdb1002a3d413e4fdebabb6.zip | |
KNF
Diffstat (limited to 'src/lib/libcrypto/dh/dh_lib.c')
| -rw-r--r-- | src/lib/libcrypto/dh/dh_lib.c | 209 |
1 files changed, 113 insertions, 96 deletions
diff --git a/src/lib/libcrypto/dh/dh_lib.c b/src/lib/libcrypto/dh/dh_lib.c index 793a8afdce..094a78041d 100644 --- a/src/lib/libcrypto/dh/dh_lib.c +++ b/src/lib/libcrypto/dh/dh_lib.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: dh_lib.c,v 1.16 2014/07/09 11:10:50 bcook Exp $ */ | 1 | /* $OpenBSD: dh_lib.c,v 1.17 2014/07/09 13:26:47 miod Exp $ */ |
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * | 4 | * |
| @@ -66,117 +66,121 @@ | |||
| 66 | 66 | ||
| 67 | static const DH_METHOD *default_DH_method = NULL; | 67 | static const DH_METHOD *default_DH_method = NULL; |
| 68 | 68 | ||
| 69 | void DH_set_default_method(const DH_METHOD *meth) | 69 | void |
| 70 | { | 70 | DH_set_default_method(const DH_METHOD *meth) |
| 71 | { | ||
| 71 | default_DH_method = meth; | 72 | default_DH_method = meth; |
| 72 | } | 73 | } |
| 73 | 74 | ||
| 74 | const DH_METHOD *DH_get_default_method(void) | 75 | const DH_METHOD * |
| 75 | { | 76 | DH_get_default_method(void) |
| 76 | if(!default_DH_method) | 77 | { |
| 77 | { | 78 | if (!default_DH_method) |
| 78 | default_DH_method = DH_OpenSSL(); | 79 | default_DH_method = DH_OpenSSL(); |
| 79 | } | ||
| 80 | return default_DH_method; | 80 | return default_DH_method; |
| 81 | } | 81 | } |
| 82 | 82 | ||
| 83 | int DH_set_method(DH *dh, const DH_METHOD *meth) | 83 | int |
| 84 | { | 84 | DH_set_method(DH *dh, const DH_METHOD *meth) |
| 85 | /* NB: The caller is specifically setting a method, so it's not up to us | 85 | { |
| 86 | * to deal with which ENGINE it comes from. */ | 86 | /* |
| 87 | * NB: The caller is specifically setting a method, so it's not up to us | ||
| 88 | * to deal with which ENGINE it comes from. | ||
| 89 | */ | ||
| 87 | const DH_METHOD *mtmp; | 90 | const DH_METHOD *mtmp; |
| 91 | |||
| 88 | mtmp = dh->meth; | 92 | mtmp = dh->meth; |
| 89 | if (mtmp->finish) mtmp->finish(dh); | 93 | if (mtmp->finish) |
| 94 | mtmp->finish(dh); | ||
| 90 | #ifndef OPENSSL_NO_ENGINE | 95 | #ifndef OPENSSL_NO_ENGINE |
| 91 | if (dh->engine) | 96 | if (dh->engine) { |
| 92 | { | ||
| 93 | ENGINE_finish(dh->engine); | 97 | ENGINE_finish(dh->engine); |
| 94 | dh->engine = NULL; | 98 | dh->engine = NULL; |
| 95 | } | 99 | } |
| 96 | #endif | 100 | #endif |
| 97 | dh->meth = meth; | 101 | dh->meth = meth; |
| 98 | if (meth->init) meth->init(dh); | 102 | if (meth->init) |
| 103 | meth->init(dh); | ||
| 99 | return 1; | 104 | return 1; |
| 100 | } | 105 | } |
| 101 | 106 | ||
| 102 | DH *DH_new(void) | 107 | DH * |
| 103 | { | 108 | DH_new(void) |
| 109 | { | ||
| 104 | return DH_new_method(NULL); | 110 | return DH_new_method(NULL); |
| 105 | } | 111 | } |
| 106 | 112 | ||
| 107 | DH *DH_new_method(ENGINE *engine) | 113 | DH * |
| 108 | { | 114 | DH_new_method(ENGINE *engine) |
| 115 | { | ||
| 109 | DH *ret; | 116 | DH *ret; |
| 110 | 117 | ||
| 111 | ret = malloc(sizeof(DH)); | 118 | ret = malloc(sizeof(DH)); |
| 112 | if (ret == NULL) | 119 | if (ret == NULL) { |
| 113 | { | 120 | DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE); |
| 114 | DHerr(DH_F_DH_NEW_METHOD,ERR_R_MALLOC_FAILURE); | 121 | return NULL; |
| 115 | return(NULL); | 122 | } |
| 116 | } | ||
| 117 | 123 | ||
| 118 | ret->meth = DH_get_default_method(); | 124 | ret->meth = DH_get_default_method(); |
| 119 | #ifndef OPENSSL_NO_ENGINE | 125 | #ifndef OPENSSL_NO_ENGINE |
| 120 | if (engine) | 126 | if (engine) { |
| 121 | { | 127 | if (!ENGINE_init(engine)) { |
| 122 | if (!ENGINE_init(engine)) | ||
| 123 | { | ||
| 124 | DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB); | 128 | DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB); |
| 125 | free(ret); | 129 | free(ret); |
| 126 | return NULL; | 130 | return NULL; |
| 127 | } | ||
| 128 | ret->engine = engine; | ||
| 129 | } | 131 | } |
| 130 | else | 132 | ret->engine = engine; |
| 133 | } else | ||
| 131 | ret->engine = ENGINE_get_default_DH(); | 134 | ret->engine = ENGINE_get_default_DH(); |
| 132 | if(ret->engine) | 135 | if(ret->engine) { |
| 133 | { | ||
| 134 | ret->meth = ENGINE_get_DH(ret->engine); | 136 | ret->meth = ENGINE_get_DH(ret->engine); |
| 135 | if(!ret->meth) | 137 | if (!ret->meth) { |
| 136 | { | 138 | DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB); |
| 137 | DHerr(DH_F_DH_NEW_METHOD,ERR_R_ENGINE_LIB); | ||
| 138 | ENGINE_finish(ret->engine); | 139 | ENGINE_finish(ret->engine); |
| 139 | free(ret); | 140 | free(ret); |
| 140 | return NULL; | 141 | return NULL; |
| 141 | } | ||
| 142 | } | 142 | } |
| 143 | } | ||
| 143 | #endif | 144 | #endif |
| 144 | 145 | ||
| 145 | ret->pad=0; | 146 | ret->pad = 0; |
| 146 | ret->version=0; | 147 | ret->version = 0; |
| 147 | ret->p=NULL; | 148 | ret->p = NULL; |
| 148 | ret->g=NULL; | 149 | ret->g = NULL; |
| 149 | ret->length=0; | 150 | ret->length = 0; |
| 150 | ret->pub_key=NULL; | 151 | ret->pub_key = NULL; |
| 151 | ret->priv_key=NULL; | 152 | ret->priv_key = NULL; |
| 152 | ret->q=NULL; | 153 | ret->q = NULL; |
| 153 | ret->j=NULL; | 154 | ret->j = NULL; |
| 154 | ret->seed = NULL; | 155 | ret->seed = NULL; |
| 155 | ret->seedlen = 0; | 156 | ret->seedlen = 0; |
| 156 | ret->counter = NULL; | 157 | ret->counter = NULL; |
| 157 | ret->method_mont_p=NULL; | 158 | ret->method_mont_p=NULL; |
| 158 | ret->references = 1; | 159 | ret->references = 1; |
| 159 | ret->flags=ret->meth->flags & ~DH_FLAG_NON_FIPS_ALLOW; | 160 | ret->flags = ret->meth->flags & ~DH_FLAG_NON_FIPS_ALLOW; |
| 160 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data); | 161 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data); |
| 161 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) | 162 | if (ret->meth->init != NULL && !ret->meth->init(ret)) { |
| 162 | { | ||
| 163 | #ifndef OPENSSL_NO_ENGINE | 163 | #ifndef OPENSSL_NO_ENGINE |
| 164 | if (ret->engine) | 164 | if (ret->engine) |
| 165 | ENGINE_finish(ret->engine); | 165 | ENGINE_finish(ret->engine); |
| 166 | #endif | 166 | #endif |
| 167 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data); | 167 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data); |
| 168 | free(ret); | 168 | free(ret); |
| 169 | ret=NULL; | 169 | ret = NULL; |
| 170 | } | ||
| 171 | return(ret); | ||
| 172 | } | 170 | } |
| 171 | return ret; | ||
| 172 | } | ||
| 173 | 173 | ||
| 174 | void DH_free(DH *r) | 174 | void |
| 175 | { | 175 | DH_free(DH *r) |
| 176 | { | ||
| 176 | int i; | 177 | int i; |
| 177 | if(r == NULL) return; | 178 | |
| 179 | if (r == NULL) | ||
| 180 | return; | ||
| 178 | i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH); | 181 | i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH); |
| 179 | if (i > 0) return; | 182 | if (i > 0) |
| 183 | return; | ||
| 180 | 184 | ||
| 181 | if (r->meth->finish) | 185 | if (r->meth->finish) |
| 182 | r->meth->finish(r); | 186 | r->meth->finish(r); |
| @@ -187,41 +191,54 @@ void DH_free(DH *r) | |||
| 187 | 191 | ||
| 188 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data); | 192 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data); |
| 189 | 193 | ||
| 190 | if (r->p != NULL) BN_clear_free(r->p); | 194 | if (r->p != NULL) |
| 191 | if (r->g != NULL) BN_clear_free(r->g); | 195 | BN_clear_free(r->p); |
| 192 | if (r->q != NULL) BN_clear_free(r->q); | 196 | if (r->g != NULL) |
| 193 | if (r->j != NULL) BN_clear_free(r->j); | 197 | BN_clear_free(r->g); |
| 198 | if (r->q != NULL) | ||
| 199 | BN_clear_free(r->q); | ||
| 200 | if (r->j != NULL) | ||
| 201 | BN_clear_free(r->j); | ||
| 194 | free(r->seed); | 202 | free(r->seed); |
| 195 | if (r->counter != NULL) BN_clear_free(r->counter); | 203 | if (r->counter != NULL) |
| 196 | if (r->pub_key != NULL) BN_clear_free(r->pub_key); | 204 | BN_clear_free(r->counter); |
| 197 | if (r->priv_key != NULL) BN_clear_free(r->priv_key); | 205 | if (r->pub_key != NULL) |
| 206 | BN_clear_free(r->pub_key); | ||
| 207 | if (r->priv_key != NULL) | ||
| 208 | BN_clear_free(r->priv_key); | ||
| 198 | free(r); | 209 | free(r); |
| 199 | } | 210 | } |
| 200 | 211 | ||
| 201 | int DH_up_ref(DH *r) | 212 | int |
| 202 | { | 213 | DH_up_ref(DH *r) |
| 214 | { | ||
| 203 | int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DH); | 215 | int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DH); |
| 204 | return ((i > 1) ? 1 : 0); | ||
| 205 | } | ||
| 206 | 216 | ||
| 207 | int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | 217 | return i > 1 ? 1 : 0; |
| 208 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) | 218 | } |
| 209 | { | 219 | |
| 210 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, argl, argp, | 220 | int |
| 211 | new_func, dup_func, free_func); | 221 | DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, |
| 212 | } | 222 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) |
| 213 | 223 | { | |
| 214 | int DH_set_ex_data(DH *d, int idx, void *arg) | 224 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, argl, argp, new_func, |
| 215 | { | 225 | dup_func, free_func); |
| 216 | return(CRYPTO_set_ex_data(&d->ex_data,idx,arg)); | 226 | } |
| 217 | } | 227 | |
| 218 | 228 | int | |
| 219 | void *DH_get_ex_data(DH *d, int idx) | 229 | DH_set_ex_data(DH *d, int idx, void *arg) |
| 220 | { | 230 | { |
| 221 | return(CRYPTO_get_ex_data(&d->ex_data,idx)); | 231 | return CRYPTO_set_ex_data(&d->ex_data, idx, arg); |
| 222 | } | 232 | } |
| 223 | 233 | ||
| 224 | int DH_size(const DH *dh) | 234 | void * |
| 225 | { | 235 | DH_get_ex_data(DH *d, int idx) |
| 226 | return(BN_num_bytes(dh->p)); | 236 | { |
| 227 | } | 237 | return CRYPTO_get_ex_data(&d->ex_data, idx); |
| 238 | } | ||
| 239 | |||
| 240 | int | ||
| 241 | DH_size(const DH *dh) | ||
| 242 | { | ||
| 243 | return BN_num_bytes(dh->p); | ||
| 244 | } | ||
