diff options
| author | miod <> | 2014-07-09 10:16:24 +0000 |
|---|---|---|
| committer | miod <> | 2014-07-09 10:16:24 +0000 |
| commit | 8d28b2bf57f7e00739466371d21495941076bf89 (patch) | |
| tree | 9316d73cdc8c916f7b3c4bf0de6c3ad956b5f6da /src/lib/libcrypto/dsa/dsa_lib.c | |
| parent | 54273e9b127b0bf3d689158719a7a7ca58ac6f75 (diff) | |
| download | openbsd-8d28b2bf57f7e00739466371d21495941076bf89.tar.gz openbsd-8d28b2bf57f7e00739466371d21495941076bf89.tar.bz2 openbsd-8d28b2bf57f7e00739466371d21495941076bf89.zip | |
KNF
Diffstat (limited to 'src/lib/libcrypto/dsa/dsa_lib.c')
| -rw-r--r-- | src/lib/libcrypto/dsa/dsa_lib.c | 251 |
1 files changed, 132 insertions, 119 deletions
diff --git a/src/lib/libcrypto/dsa/dsa_lib.c b/src/lib/libcrypto/dsa/dsa_lib.c index d625f0f282..334d5ba7f2 100644 --- a/src/lib/libcrypto/dsa/dsa_lib.c +++ b/src/lib/libcrypto/dsa/dsa_lib.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: dsa_lib.c,v 1.16 2014/06/12 15:49:28 deraadt Exp $ */ | 1 | /* $OpenBSD: dsa_lib.c,v 1.17 2014/07/09 10:16:24 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 | * |
| @@ -70,198 +70,212 @@ | |||
| 70 | #include <openssl/dh.h> | 70 | #include <openssl/dh.h> |
| 71 | #endif | 71 | #endif |
| 72 | 72 | ||
| 73 | const char DSA_version[]="DSA" OPENSSL_VERSION_PTEXT; | 73 | const char DSA_version[] = "DSA" OPENSSL_VERSION_PTEXT; |
| 74 | 74 | ||
| 75 | static const DSA_METHOD *default_DSA_method = NULL; | 75 | static const DSA_METHOD *default_DSA_method = NULL; |
| 76 | 76 | ||
| 77 | void DSA_set_default_method(const DSA_METHOD *meth) | 77 | void |
| 78 | { | 78 | DSA_set_default_method(const DSA_METHOD *meth) |
| 79 | { | ||
| 79 | default_DSA_method = meth; | 80 | default_DSA_method = meth; |
| 80 | } | 81 | } |
| 81 | 82 | ||
| 82 | const DSA_METHOD *DSA_get_default_method(void) | 83 | const DSA_METHOD * |
| 83 | { | 84 | DSA_get_default_method(void) |
| 84 | if(!default_DSA_method) | 85 | { |
| 85 | { | 86 | if (!default_DSA_method) |
| 86 | default_DSA_method = DSA_OpenSSL(); | 87 | default_DSA_method = DSA_OpenSSL(); |
| 87 | } | ||
| 88 | return default_DSA_method; | 88 | return default_DSA_method; |
| 89 | } | 89 | } |
| 90 | 90 | ||
| 91 | DSA *DSA_new(void) | 91 | DSA * |
| 92 | { | 92 | DSA_new(void) |
| 93 | { | ||
| 93 | return DSA_new_method(NULL); | 94 | return DSA_new_method(NULL); |
| 94 | } | 95 | } |
| 95 | 96 | ||
| 96 | int DSA_set_method(DSA *dsa, const DSA_METHOD *meth) | 97 | int |
| 97 | { | 98 | DSA_set_method(DSA *dsa, const DSA_METHOD *meth) |
| 98 | /* NB: The caller is specifically setting a method, so it's not up to us | 99 | { |
| 99 | * to deal with which ENGINE it comes from. */ | 100 | /* |
| 101 | * NB: The caller is specifically setting a method, so it's not up to us | ||
| 102 | * to deal with which ENGINE it comes from. | ||
| 103 | */ | ||
| 100 | const DSA_METHOD *mtmp; | 104 | const DSA_METHOD *mtmp; |
| 101 | mtmp = dsa->meth; | 105 | mtmp = dsa->meth; |
| 102 | if (mtmp->finish) mtmp->finish(dsa); | 106 | if (mtmp->finish) |
| 107 | mtmp->finish(dsa); | ||
| 103 | #ifndef OPENSSL_NO_ENGINE | 108 | #ifndef OPENSSL_NO_ENGINE |
| 104 | if (dsa->engine) | 109 | if (dsa->engine) { |
| 105 | { | ||
| 106 | ENGINE_finish(dsa->engine); | 110 | ENGINE_finish(dsa->engine); |
| 107 | dsa->engine = NULL; | 111 | dsa->engine = NULL; |
| 108 | } | 112 | } |
| 109 | #endif | 113 | #endif |
| 110 | dsa->meth = meth; | 114 | dsa->meth = meth; |
| 111 | if (meth->init) meth->init(dsa); | 115 | if (meth->init) |
| 116 | meth->init(dsa); | ||
| 112 | return 1; | 117 | return 1; |
| 113 | } | 118 | } |
| 114 | 119 | ||
| 115 | DSA *DSA_new_method(ENGINE *engine) | 120 | DSA * |
| 116 | { | 121 | DSA_new_method(ENGINE *engine) |
| 122 | { | ||
| 117 | DSA *ret; | 123 | DSA *ret; |
| 118 | 124 | ||
| 119 | ret = malloc(sizeof(DSA)); | 125 | ret = malloc(sizeof(DSA)); |
| 120 | if (ret == NULL) | 126 | if (ret == NULL) { |
| 121 | { | 127 | DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE); |
| 122 | DSAerr(DSA_F_DSA_NEW_METHOD,ERR_R_MALLOC_FAILURE); | 128 | return NULL; |
| 123 | return(NULL); | 129 | } |
| 124 | } | ||
| 125 | ret->meth = DSA_get_default_method(); | 130 | ret->meth = DSA_get_default_method(); |
| 126 | #ifndef OPENSSL_NO_ENGINE | 131 | #ifndef OPENSSL_NO_ENGINE |
| 127 | if (engine) | 132 | if (engine) { |
| 128 | { | 133 | if (!ENGINE_init(engine)) { |
| 129 | if (!ENGINE_init(engine)) | ||
| 130 | { | ||
| 131 | DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB); | 134 | DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB); |
| 132 | free(ret); | 135 | free(ret); |
| 133 | return NULL; | 136 | return NULL; |
| 134 | } | ||
| 135 | ret->engine = engine; | ||
| 136 | } | 137 | } |
| 137 | else | 138 | ret->engine = engine; |
| 139 | } else | ||
| 138 | ret->engine = ENGINE_get_default_DSA(); | 140 | ret->engine = ENGINE_get_default_DSA(); |
| 139 | if(ret->engine) | 141 | if (ret->engine) { |
| 140 | { | ||
| 141 | ret->meth = ENGINE_get_DSA(ret->engine); | 142 | ret->meth = ENGINE_get_DSA(ret->engine); |
| 142 | if(!ret->meth) | 143 | if (!ret->meth) { |
| 143 | { | 144 | DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB); |
| 144 | DSAerr(DSA_F_DSA_NEW_METHOD, | ||
| 145 | ERR_R_ENGINE_LIB); | ||
| 146 | ENGINE_finish(ret->engine); | 145 | ENGINE_finish(ret->engine); |
| 147 | free(ret); | 146 | free(ret); |
| 148 | return NULL; | 147 | return NULL; |
| 149 | } | ||
| 150 | } | 148 | } |
| 149 | } | ||
| 151 | #endif | 150 | #endif |
| 152 | 151 | ||
| 153 | ret->pad=0; | 152 | ret->pad = 0; |
| 154 | ret->version=0; | 153 | ret->version = 0; |
| 155 | ret->write_params=1; | 154 | ret->write_params = 1; |
| 156 | ret->p=NULL; | 155 | ret->p = NULL; |
| 157 | ret->q=NULL; | 156 | ret->q = NULL; |
| 158 | ret->g=NULL; | 157 | ret->g = NULL; |
| 159 | 158 | ||
| 160 | ret->pub_key=NULL; | 159 | ret->pub_key = NULL; |
| 161 | ret->priv_key=NULL; | 160 | ret->priv_key = NULL; |
| 162 | 161 | ||
| 163 | ret->kinv=NULL; | 162 | ret->kinv = NULL; |
| 164 | ret->r=NULL; | 163 | ret->r = NULL; |
| 165 | ret->method_mont_p=NULL; | 164 | ret->method_mont_p = NULL; |
| 166 | 165 | ||
| 167 | ret->references=1; | 166 | ret->references = 1; |
| 168 | ret->flags=ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; | 167 | ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; |
| 169 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); | 168 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); |
| 170 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) | 169 | if (ret->meth->init != NULL && !ret->meth->init(ret)) { |
| 171 | { | ||
| 172 | #ifndef OPENSSL_NO_ENGINE | 170 | #ifndef OPENSSL_NO_ENGINE |
| 173 | if (ret->engine) | 171 | if (ret->engine) |
| 174 | ENGINE_finish(ret->engine); | 172 | ENGINE_finish(ret->engine); |
| 175 | #endif | 173 | #endif |
| 176 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); | 174 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); |
| 177 | free(ret); | 175 | free(ret); |
| 178 | ret=NULL; | 176 | ret = NULL; |
| 179 | } | ||
| 180 | |||
| 181 | return(ret); | ||
| 182 | } | 177 | } |
| 178 | |||
| 179 | return ret; | ||
| 180 | } | ||
| 183 | 181 | ||
| 184 | void DSA_free(DSA *r) | 182 | void |
| 185 | { | 183 | DSA_free(DSA *r) |
| 184 | { | ||
| 186 | int i; | 185 | int i; |
| 187 | 186 | ||
| 188 | if (r == NULL) return; | 187 | if (r == NULL) |
| 188 | return; | ||
| 189 | 189 | ||
| 190 | i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_DSA); | 190 | i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DSA); |
| 191 | if (i > 0) return; | 191 | if (i > 0) |
| 192 | return; | ||
| 192 | 193 | ||
| 193 | if(r->meth->finish) | 194 | if (r->meth->finish) |
| 194 | r->meth->finish(r); | 195 | r->meth->finish(r); |
| 195 | #ifndef OPENSSL_NO_ENGINE | 196 | #ifndef OPENSSL_NO_ENGINE |
| 196 | if(r->engine) | 197 | if (r->engine) |
| 197 | ENGINE_finish(r->engine); | 198 | ENGINE_finish(r->engine); |
| 198 | #endif | 199 | #endif |
| 199 | 200 | ||
| 200 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data); | 201 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data); |
| 201 | 202 | ||
| 202 | if (r->p != NULL) BN_clear_free(r->p); | 203 | if (r->p != NULL) |
| 203 | if (r->q != NULL) BN_clear_free(r->q); | 204 | BN_clear_free(r->p); |
| 204 | if (r->g != NULL) BN_clear_free(r->g); | 205 | if (r->q != NULL) |
| 205 | if (r->pub_key != NULL) BN_clear_free(r->pub_key); | 206 | BN_clear_free(r->q); |
| 206 | if (r->priv_key != NULL) BN_clear_free(r->priv_key); | 207 | if (r->g != NULL) |
| 207 | if (r->kinv != NULL) BN_clear_free(r->kinv); | 208 | BN_clear_free(r->g); |
| 208 | if (r->r != NULL) BN_clear_free(r->r); | 209 | if (r->pub_key != NULL) |
| 210 | BN_clear_free(r->pub_key); | ||
| 211 | if (r->priv_key != NULL) | ||
| 212 | BN_clear_free(r->priv_key); | ||
| 213 | if (r->kinv != NULL) | ||
| 214 | BN_clear_free(r->kinv); | ||
| 215 | if (r->r != NULL) | ||
| 216 | BN_clear_free(r->r); | ||
| 209 | free(r); | 217 | free(r); |
| 210 | } | 218 | } |
| 211 | 219 | ||
| 212 | int DSA_up_ref(DSA *r) | 220 | int |
| 213 | { | 221 | DSA_up_ref(DSA *r) |
| 222 | { | ||
| 214 | int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DSA); | 223 | int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DSA); |
| 215 | return ((i > 1) ? 1 : 0); | 224 | return i > 1 ? 1 : 0; |
| 216 | } | 225 | } |
| 217 | 226 | ||
| 218 | int DSA_size(const DSA *r) | 227 | int |
| 219 | { | 228 | DSA_size(const DSA *r) |
| 220 | int ret,i; | 229 | { |
| 230 | int ret, i; | ||
| 221 | ASN1_INTEGER bs; | 231 | ASN1_INTEGER bs; |
| 222 | unsigned char buf[4]; /* 4 bytes looks really small. | 232 | unsigned char buf[4]; /* 4 bytes looks really small. |
| 223 | However, i2d_ASN1_INTEGER() will not look | 233 | However, i2d_ASN1_INTEGER() will not look |
| 224 | beyond the first byte, as long as the second | 234 | beyond the first byte, as long as the second |
| 225 | parameter is NULL. */ | 235 | parameter is NULL. */ |
| 226 | 236 | ||
| 227 | i=BN_num_bits(r->q); | 237 | i = BN_num_bits(r->q); |
| 228 | bs.length=(i+7)/8; | 238 | bs.length = (i + 7) / 8; |
| 229 | bs.data=buf; | 239 | bs.data = buf; |
| 230 | bs.type=V_ASN1_INTEGER; | 240 | bs.type = V_ASN1_INTEGER; |
| 231 | /* If the top bit is set the asn1 encoding is 1 larger. */ | 241 | /* If the top bit is set the asn1 encoding is 1 larger. */ |
| 232 | buf[0]=0xff; | 242 | buf[0] = 0xff; |
| 233 | 243 | ||
| 234 | i=i2d_ASN1_INTEGER(&bs,NULL); | 244 | i = i2d_ASN1_INTEGER(&bs, NULL); |
| 235 | i+=i; /* r and s */ | 245 | i += i; /* r and s */ |
| 236 | ret=ASN1_object_size(1,i,V_ASN1_SEQUENCE); | 246 | ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE); |
| 237 | return(ret); | 247 | return ret; |
| 238 | } | 248 | } |
| 239 | 249 | ||
| 240 | int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | 250 | int |
| 241 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) | 251 | DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, |
| 242 | { | 252 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) |
| 253 | { | ||
| 243 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, argl, argp, | 254 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, argl, argp, |
| 244 | new_func, dup_func, free_func); | 255 | new_func, dup_func, free_func); |
| 245 | } | 256 | } |
| 246 | 257 | ||
| 247 | int DSA_set_ex_data(DSA *d, int idx, void *arg) | 258 | int |
| 248 | { | 259 | DSA_set_ex_data(DSA *d, int idx, void *arg) |
| 249 | return(CRYPTO_set_ex_data(&d->ex_data,idx,arg)); | 260 | { |
| 250 | } | 261 | return CRYPTO_set_ex_data(&d->ex_data, idx, arg); |
| 262 | } | ||
| 251 | 263 | ||
| 252 | void *DSA_get_ex_data(DSA *d, int idx) | 264 | void * |
| 253 | { | 265 | DSA_get_ex_data(DSA *d, int idx) |
| 254 | return(CRYPTO_get_ex_data(&d->ex_data,idx)); | 266 | { |
| 255 | } | 267 | return CRYPTO_get_ex_data(&d->ex_data, idx); |
| 268 | } | ||
| 256 | 269 | ||
| 257 | #ifndef OPENSSL_NO_DH | 270 | #ifndef OPENSSL_NO_DH |
| 258 | DH *DSA_dup_DH(const DSA *r) | 271 | DH * |
| 259 | { | 272 | DSA_dup_DH(const DSA *r) |
| 260 | /* DSA has p, q, g, optional pub_key, optional priv_key. | 273 | { |
| 274 | /* | ||
| 275 | * DSA has p, q, g, optional pub_key, optional priv_key. | ||
| 261 | * DH has p, optional length, g, optional pub_key, optional priv_key, | 276 | * DH has p, optional length, g, optional pub_key, optional priv_key, |
| 262 | * optional q. | 277 | * optional q. |
| 263 | */ | 278 | */ |
| 264 | |||
| 265 | DH *ret = NULL; | 279 | DH *ret = NULL; |
| 266 | 280 | ||
| 267 | if (r == NULL) | 281 | if (r == NULL) |
| @@ -272,12 +286,11 @@ DH *DSA_dup_DH(const DSA *r) | |||
| 272 | if (r->p != NULL) | 286 | if (r->p != NULL) |
| 273 | if ((ret->p = BN_dup(r->p)) == NULL) | 287 | if ((ret->p = BN_dup(r->p)) == NULL) |
| 274 | goto err; | 288 | goto err; |
| 275 | if (r->q != NULL) | 289 | if (r->q != NULL) { |
| 276 | { | ||
| 277 | ret->length = BN_num_bits(r->q); | 290 | ret->length = BN_num_bits(r->q); |
| 278 | if ((ret->q = BN_dup(r->q)) == NULL) | 291 | if ((ret->q = BN_dup(r->q)) == NULL) |
| 279 | goto err; | 292 | goto err; |
| 280 | } | 293 | } |
| 281 | if (r->g != NULL) | 294 | if (r->g != NULL) |
| 282 | if ((ret->g = BN_dup(r->g)) == NULL) | 295 | if ((ret->g = BN_dup(r->g)) == NULL) |
| 283 | goto err; | 296 | goto err; |
| @@ -290,9 +303,9 @@ DH *DSA_dup_DH(const DSA *r) | |||
| 290 | 303 | ||
| 291 | return ret; | 304 | return ret; |
| 292 | 305 | ||
| 293 | err: | 306 | err: |
| 294 | if (ret != NULL) | 307 | if (ret != NULL) |
| 295 | DH_free(ret); | 308 | DH_free(ret); |
| 296 | return NULL; | 309 | return NULL; |
| 297 | } | 310 | } |
| 298 | #endif | 311 | #endif |
