diff options
| author | jsing <> | 2023-04-19 11:05:11 +0000 |
|---|---|---|
| committer | jsing <> | 2023-04-19 11:05:11 +0000 |
| commit | 53dbc22f0ae91254f1f5cff7a85cd3238f8bcab1 (patch) | |
| tree | c42f09d1f559ebce11b80b21056efa362d244e94 /src | |
| parent | 0b5fd2ea19990474e5075358b9e4bec724ec2e38 (diff) | |
| download | openbsd-53dbc22f0ae91254f1f5cff7a85cd3238f8bcab1.tar.gz openbsd-53dbc22f0ae91254f1f5cff7a85cd3238f8bcab1.tar.bz2 openbsd-53dbc22f0ae91254f1f5cff7a85cd3238f8bcab1.zip | |
Reorder functions.
No functional change.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libcrypto/bn/bn_convert.c | 204 |
1 files changed, 102 insertions, 102 deletions
diff --git a/src/lib/libcrypto/bn/bn_convert.c b/src/lib/libcrypto/bn/bn_convert.c index 20bea92218..6fe30fdbb9 100644 --- a/src/lib/libcrypto/bn/bn_convert.c +++ b/src/lib/libcrypto/bn/bn_convert.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: bn_convert.c,v 1.3 2023/04/19 10:54:49 jsing Exp $ */ | 1 | /* $OpenBSD: bn_convert.c,v 1.4 2023/04/19 11:05:11 jsing 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,39 +70,23 @@ | |||
| 70 | 70 | ||
| 71 | static const char Hex[]="0123456789ABCDEF"; | 71 | static const char Hex[]="0123456789ABCDEF"; |
| 72 | 72 | ||
| 73 | /* Must 'free' the returned data */ | 73 | int |
| 74 | char * | 74 | BN_asc2bn(BIGNUM **bn, const char *a) |
| 75 | BN_bn2hex(const BIGNUM *a) | ||
| 76 | { | 75 | { |
| 77 | int i, j, v, z = 0; | 76 | const char *p = a; |
| 78 | char *buf; | 77 | if (*p == '-') |
| 79 | char *p; | 78 | p++; |
| 80 | 79 | ||
| 81 | buf = malloc(BN_is_negative(a) + a->top * BN_BYTES * 2 + 2); | 80 | if (p[0] == '0' && (p[1] == 'X' || p[1] == 'x')) { |
| 82 | if (buf == NULL) { | 81 | if (!BN_hex2bn(bn, p + 2)) |
| 83 | BNerror(ERR_R_MALLOC_FAILURE); | 82 | return 0; |
| 84 | goto err; | 83 | } else { |
| 85 | } | 84 | if (!BN_dec2bn(bn, p)) |
| 86 | p = buf; | 85 | return 0; |
| 87 | if (BN_is_negative(a)) | ||
| 88 | *p++ = '-'; | ||
| 89 | if (BN_is_zero(a)) | ||
| 90 | *p++ = '0'; | ||
| 91 | for (i = a->top - 1; i >=0; i--) { | ||
| 92 | for (j = BN_BITS2 - 8; j >= 0; j -= 8) { | ||
| 93 | /* strip leading zeros */ | ||
| 94 | v = ((int)(a->d[i] >> (long)j)) & 0xff; | ||
| 95 | if (z || (v != 0)) { | ||
| 96 | *p++ = Hex[v >> 4]; | ||
| 97 | *p++ = Hex[v & 0x0f]; | ||
| 98 | z = 1; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | 86 | } |
| 102 | *p = '\0'; | 87 | if (*a == '-') |
| 103 | 88 | BN_set_negative(*bn, 1); | |
| 104 | err: | 89 | return 1; |
| 105 | return (buf); | ||
| 106 | } | 90 | } |
| 107 | 91 | ||
| 108 | /* Must 'free' the returned data */ | 92 | /* Must 'free' the returned data */ |
| @@ -187,22 +171,21 @@ err: | |||
| 187 | } | 171 | } |
| 188 | 172 | ||
| 189 | int | 173 | int |
| 190 | BN_hex2bn(BIGNUM **bn, const char *a) | 174 | BN_dec2bn(BIGNUM **bn, const char *a) |
| 191 | { | 175 | { |
| 192 | BIGNUM *ret = NULL; | 176 | BIGNUM *ret = NULL; |
| 193 | BN_ULONG l = 0; | 177 | BN_ULONG l = 0; |
| 194 | int neg = 0, h, m, i,j, k, c; | 178 | int neg = 0, i, j; |
| 195 | int num; | 179 | int num; |
| 196 | 180 | ||
| 197 | if ((a == NULL) || (*a == '\0')) | 181 | if ((a == NULL) || (*a == '\0')) |
| 198 | return (0); | 182 | return (0); |
| 199 | |||
| 200 | if (*a == '-') { | 183 | if (*a == '-') { |
| 201 | neg = 1; | 184 | neg = 1; |
| 202 | a++; | 185 | a++; |
| 203 | } | 186 | } |
| 204 | 187 | ||
| 205 | for (i = 0; i <= (INT_MAX / 4) && isxdigit((unsigned char)a[i]); i++) | 188 | for (i = 0; i <= (INT_MAX / 4) && isdigit((unsigned char)a[i]); i++) |
| 206 | ; | 189 | ; |
| 207 | if (i > INT_MAX / 4) | 190 | if (i > INT_MAX / 4) |
| 208 | return (0); | 191 | return (0); |
| @@ -211,7 +194,8 @@ BN_hex2bn(BIGNUM **bn, const char *a) | |||
| 211 | if (bn == NULL) | 194 | if (bn == NULL) |
| 212 | return (num); | 195 | return (num); |
| 213 | 196 | ||
| 214 | /* a is the start of the hex digits, and it is 'i' long */ | 197 | /* a is the start of the digits, and it is 'i' long. |
| 198 | * We chop it into BN_DEC_NUM digits at a time */ | ||
| 215 | if (*bn == NULL) { | 199 | if (*bn == NULL) { |
| 216 | if ((ret = BN_new()) == NULL) | 200 | if ((ret = BN_new()) == NULL) |
| 217 | return (0); | 201 | return (0); |
| @@ -220,36 +204,28 @@ BN_hex2bn(BIGNUM **bn, const char *a) | |||
| 220 | BN_zero(ret); | 204 | BN_zero(ret); |
| 221 | } | 205 | } |
| 222 | 206 | ||
| 223 | /* i is the number of hex digits */ | 207 | /* i is the number of digits, a bit of an over expand */ |
| 224 | if (!bn_expand(ret, i * 4)) | 208 | if (!bn_expand(ret, i * 4)) |
| 225 | goto err; | 209 | goto err; |
| 226 | 210 | ||
| 227 | j = i; /* least significant 'hex' */ | 211 | j = BN_DEC_NUM - (i % BN_DEC_NUM); |
| 228 | m = 0; | 212 | if (j == BN_DEC_NUM) |
| 229 | h = 0; | 213 | j = 0; |
| 230 | while (j > 0) { | 214 | l = 0; |
| 231 | m = ((BN_BYTES * 2) <= j) ? (BN_BYTES * 2) : j; | 215 | while (*a) { |
| 232 | l = 0; | 216 | l *= 10; |
| 233 | for (;;) { | 217 | l += *a - '0'; |
| 234 | c = a[j - m]; | 218 | a++; |
| 235 | if ((c >= '0') && (c <= '9')) | 219 | if (++j == BN_DEC_NUM) { |
| 236 | k = c - '0'; | 220 | if (!BN_mul_word(ret, BN_DEC_CONV)) |
| 237 | else if ((c >= 'a') && (c <= 'f')) | 221 | goto err; |
| 238 | k = c - 'a' + 10; | 222 | if (!BN_add_word(ret, l)) |
| 239 | else if ((c >= 'A') && (c <= 'F')) | 223 | goto err; |
| 240 | k = c - 'A' + 10; | 224 | l = 0; |
| 241 | else | 225 | j = 0; |
| 242 | k = 0; /* paranoia */ | ||
| 243 | l = (l << 4) | k; | ||
| 244 | |||
| 245 | if (--m <= 0) { | ||
| 246 | ret->d[h++] = l; | ||
| 247 | break; | ||
| 248 | } | ||
| 249 | } | 226 | } |
| 250 | j -= (BN_BYTES * 2); | ||
| 251 | } | 227 | } |
| 252 | ret->top = h; | 228 | |
| 253 | bn_correct_top(ret); | 229 | bn_correct_top(ret); |
| 254 | 230 | ||
| 255 | BN_set_negative(ret, neg); | 231 | BN_set_negative(ret, neg); |
| @@ -263,22 +239,58 @@ err: | |||
| 263 | return (0); | 239 | return (0); |
| 264 | } | 240 | } |
| 265 | 241 | ||
| 242 | /* Must 'free' the returned data */ | ||
| 243 | char * | ||
| 244 | BN_bn2hex(const BIGNUM *a) | ||
| 245 | { | ||
| 246 | int i, j, v, z = 0; | ||
| 247 | char *buf; | ||
| 248 | char *p; | ||
| 249 | |||
| 250 | buf = malloc(BN_is_negative(a) + a->top * BN_BYTES * 2 + 2); | ||
| 251 | if (buf == NULL) { | ||
| 252 | BNerror(ERR_R_MALLOC_FAILURE); | ||
| 253 | goto err; | ||
| 254 | } | ||
| 255 | p = buf; | ||
| 256 | if (BN_is_negative(a)) | ||
| 257 | *p++ = '-'; | ||
| 258 | if (BN_is_zero(a)) | ||
| 259 | *p++ = '0'; | ||
| 260 | for (i = a->top - 1; i >=0; i--) { | ||
| 261 | for (j = BN_BITS2 - 8; j >= 0; j -= 8) { | ||
| 262 | /* strip leading zeros */ | ||
| 263 | v = ((int)(a->d[i] >> (long)j)) & 0xff; | ||
| 264 | if (z || (v != 0)) { | ||
| 265 | *p++ = Hex[v >> 4]; | ||
| 266 | *p++ = Hex[v & 0x0f]; | ||
| 267 | z = 1; | ||
| 268 | } | ||
| 269 | } | ||
| 270 | } | ||
| 271 | *p = '\0'; | ||
| 272 | |||
| 273 | err: | ||
| 274 | return (buf); | ||
| 275 | } | ||
| 276 | |||
| 266 | int | 277 | int |
| 267 | BN_dec2bn(BIGNUM **bn, const char *a) | 278 | BN_hex2bn(BIGNUM **bn, const char *a) |
| 268 | { | 279 | { |
| 269 | BIGNUM *ret = NULL; | 280 | BIGNUM *ret = NULL; |
| 270 | BN_ULONG l = 0; | 281 | BN_ULONG l = 0; |
| 271 | int neg = 0, i, j; | 282 | int neg = 0, h, m, i,j, k, c; |
| 272 | int num; | 283 | int num; |
| 273 | 284 | ||
| 274 | if ((a == NULL) || (*a == '\0')) | 285 | if ((a == NULL) || (*a == '\0')) |
| 275 | return (0); | 286 | return (0); |
| 287 | |||
| 276 | if (*a == '-') { | 288 | if (*a == '-') { |
| 277 | neg = 1; | 289 | neg = 1; |
| 278 | a++; | 290 | a++; |
| 279 | } | 291 | } |
| 280 | 292 | ||
| 281 | for (i = 0; i <= (INT_MAX / 4) && isdigit((unsigned char)a[i]); i++) | 293 | for (i = 0; i <= (INT_MAX / 4) && isxdigit((unsigned char)a[i]); i++) |
| 282 | ; | 294 | ; |
| 283 | if (i > INT_MAX / 4) | 295 | if (i > INT_MAX / 4) |
| 284 | return (0); | 296 | return (0); |
| @@ -287,8 +299,7 @@ BN_dec2bn(BIGNUM **bn, const char *a) | |||
| 287 | if (bn == NULL) | 299 | if (bn == NULL) |
| 288 | return (num); | 300 | return (num); |
| 289 | 301 | ||
| 290 | /* a is the start of the digits, and it is 'i' long. | 302 | /* a is the start of the hex digits, and it is 'i' long */ |
| 291 | * We chop it into BN_DEC_NUM digits at a time */ | ||
| 292 | if (*bn == NULL) { | 303 | if (*bn == NULL) { |
| 293 | if ((ret = BN_new()) == NULL) | 304 | if ((ret = BN_new()) == NULL) |
| 294 | return (0); | 305 | return (0); |
| @@ -297,28 +308,36 @@ BN_dec2bn(BIGNUM **bn, const char *a) | |||
| 297 | BN_zero(ret); | 308 | BN_zero(ret); |
| 298 | } | 309 | } |
| 299 | 310 | ||
| 300 | /* i is the number of digits, a bit of an over expand */ | 311 | /* i is the number of hex digits */ |
| 301 | if (!bn_expand(ret, i * 4)) | 312 | if (!bn_expand(ret, i * 4)) |
| 302 | goto err; | 313 | goto err; |
| 303 | 314 | ||
| 304 | j = BN_DEC_NUM - (i % BN_DEC_NUM); | 315 | j = i; /* least significant 'hex' */ |
| 305 | if (j == BN_DEC_NUM) | 316 | m = 0; |
| 306 | j = 0; | 317 | h = 0; |
| 307 | l = 0; | 318 | while (j > 0) { |
| 308 | while (*a) { | 319 | m = ((BN_BYTES * 2) <= j) ? (BN_BYTES * 2) : j; |
| 309 | l *= 10; | 320 | l = 0; |
| 310 | l += *a - '0'; | 321 | for (;;) { |
| 311 | a++; | 322 | c = a[j - m]; |
| 312 | if (++j == BN_DEC_NUM) { | 323 | if ((c >= '0') && (c <= '9')) |
| 313 | if (!BN_mul_word(ret, BN_DEC_CONV)) | 324 | k = c - '0'; |
| 314 | goto err; | 325 | else if ((c >= 'a') && (c <= 'f')) |
| 315 | if (!BN_add_word(ret, l)) | 326 | k = c - 'a' + 10; |
| 316 | goto err; | 327 | else if ((c >= 'A') && (c <= 'F')) |
| 317 | l = 0; | 328 | k = c - 'A' + 10; |
| 318 | j = 0; | 329 | else |
| 330 | k = 0; /* paranoia */ | ||
| 331 | l = (l << 4) | k; | ||
| 332 | |||
| 333 | if (--m <= 0) { | ||
| 334 | ret->d[h++] = l; | ||
| 335 | break; | ||
| 336 | } | ||
| 319 | } | 337 | } |
| 338 | j -= (BN_BYTES * 2); | ||
| 320 | } | 339 | } |
| 321 | 340 | ret->top = h; | |
| 322 | bn_correct_top(ret); | 341 | bn_correct_top(ret); |
| 323 | 342 | ||
| 324 | BN_set_negative(ret, neg); | 343 | BN_set_negative(ret, neg); |
| @@ -333,25 +352,6 @@ err: | |||
| 333 | } | 352 | } |
| 334 | 353 | ||
| 335 | int | 354 | int |
| 336 | BN_asc2bn(BIGNUM **bn, const char *a) | ||
| 337 | { | ||
| 338 | const char *p = a; | ||
| 339 | if (*p == '-') | ||
| 340 | p++; | ||
| 341 | |||
| 342 | if (p[0] == '0' && (p[1] == 'X' || p[1] == 'x')) { | ||
| 343 | if (!BN_hex2bn(bn, p + 2)) | ||
| 344 | return 0; | ||
| 345 | } else { | ||
| 346 | if (!BN_dec2bn(bn, p)) | ||
| 347 | return 0; | ||
| 348 | } | ||
| 349 | if (*a == '-') | ||
| 350 | BN_set_negative(*bn, 1); | ||
| 351 | return 1; | ||
| 352 | } | ||
| 353 | |||
| 354 | int | ||
| 355 | BN_bn2mpi(const BIGNUM *a, unsigned char *d) | 355 | BN_bn2mpi(const BIGNUM *a, unsigned char *d) |
| 356 | { | 356 | { |
| 357 | int bits; | 357 | int bits; |
