| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/bn.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <ctype.h> |
| 19 | #include <limits.h> |
| 20 | #include <stdio.h> |
| 21 |
| 22 | #include <openssl/bio.h> |
| 23 | #include <openssl/bytestring.h> |
| 24 | #include <openssl/err.h> |
| 25 | #include <openssl/mem.h> |
| 26 |
| 27 | #include "../fipsmodule/bn/internal.h" |
| 28 |
| 29 |
| 30 | int BN_bn2cbb_padded(CBB *out, size_t len, const BIGNUM *in) { |
| 31 | uint8_t *ptr; |
| 32 | return CBB_add_space(out, &ptr, len) && BN_bn2bin_padded(ptr, len, in); |
| 33 | } |
| 34 |
| 35 | static const char hextable[] = "0123456789abcdef"; |
| 36 |
| 37 | char *BN_bn2hex(const BIGNUM *bn) { |
| 38 | int width = bn_minimal_width(bn); |
| 39 | char *buf = reinterpret_cast<char *>( |
| 40 | OPENSSL_malloc(1 /* leading '-' */ + 1 /* zero is non-empty */ + |
| 41 | width * BN_BYTES * 2 + 1 /* trailing NUL */)); |
| 42 | if (buf == NULL) { |
| 43 | return NULL; |
| 44 | } |
| 45 |
| 46 | char *p = buf; |
| 47 | if (bn->neg) { |
| 48 | *(p++) = '-'; |
| 49 | } |
| 50 |
| 51 | if (BN_is_zero(bn)) { |
| 52 | *(p++) = '0'; |
| 53 | } |
| 54 |
| 55 | int z = 0; |
| 56 | for (int i = width - 1; i >= 0; i--) { |
| 57 | for (int j = BN_BITS2 - 8; j >= 0; j -= 8) { |
| 58 | // strip leading zeros |
| 59 | int v = ((int)(bn->d[i] >> (long)j)) & 0xff; |
| 60 | if (z || v != 0) { |
| 61 | *(p++) = hextable[v >> 4]; |
| 62 | *(p++) = hextable[v & 0x0f]; |
| 63 | z = 1; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | *p = '\0'; |
| 68 |
| 69 | return buf; |
| 70 | } |
| 71 |
| 72 | // decode_hex decodes |in_len| bytes of hex data from |in| and updates |bn|. |
| 73 | static int decode_hex(BIGNUM *bn, const char *in, int in_len) { |
| 74 | if (in_len > INT_MAX / 4) { |
| 75 | OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG); |
| 76 | return 0; |
| 77 | } |
| 78 | // |in_len| is the number of hex digits. |
| 79 | if (!bn_expand(bn, in_len * 4)) { |
| 80 | return 0; |
| 81 | } |
| 82 |
| 83 | int i = 0; |
| 84 | while (in_len > 0) { |
| 85 | // Decode one |BN_ULONG| at a time. |
| 86 | int todo = BN_BYTES * 2; |
| 87 | if (todo > in_len) { |
| 88 | todo = in_len; |
| 89 | } |
| 90 |
| 91 | BN_ULONG word = 0; |
| 92 | int j; |
| 93 | for (j = todo; j > 0; j--) { |
| 94 | uint8_t hex = 0; |
| 95 | if (!OPENSSL_fromxdigit(&hex, in[in_len - j])) { |
| 96 | // This shouldn't happen. The caller checks |OPENSSL_isxdigit|. |
| 97 | assert(0); |
| 98 | } |
| 99 | word = (word << 4) | hex; |
| 100 | } |
| 101 |
| 102 | bn->d[i++] = word; |
| 103 | in_len -= todo; |
| 104 | } |
| 105 | assert(i <= bn->dmax); |
| 106 | bn->width = i; |
| 107 | return 1; |
| 108 | } |
| 109 |
| 110 | // decode_dec decodes |in_len| bytes of decimal data from |in| and updates |bn|. |
| 111 | static int decode_dec(BIGNUM *bn, const char *in, int in_len) { |
| 112 | int i, j; |
| 113 | BN_ULONG l = 0; |
| 114 |
| 115 | // Decode |BN_DEC_NUM| digits at a time. |
| 116 | j = BN_DEC_NUM - (in_len % BN_DEC_NUM); |
| 117 | if (j == BN_DEC_NUM) { |
| 118 | j = 0; |
| 119 | } |
| 120 | l = 0; |
| 121 | for (i = 0; i < in_len; i++) { |
| 122 | l *= 10; |
| 123 | l += in[i] - '0'; |
| 124 | if (++j == BN_DEC_NUM) { |
| 125 | if (!BN_mul_word(bn, BN_DEC_CONV) || !BN_add_word(bn, l)) { |
| 126 | return 0; |
| 127 | } |
| 128 | l = 0; |
| 129 | j = 0; |
| 130 | } |
| 131 | } |
| 132 | return 1; |
| 133 | } |
| 134 |
| 135 | typedef int (*decode_func)(BIGNUM *bn, const char *in, int in_len); |
| 136 | typedef int (*char_test_func)(int c); |
| 137 |
| 138 | static int bn_x2bn(BIGNUM **outp, const char *in, decode_func decode, |
| 139 | char_test_func want_char) { |
| 140 | BIGNUM *ret = NULL; |
| 141 | int neg = 0, i; |
| 142 | int num; |
| 143 |
| 144 | if (in == NULL || *in == 0) { |
| 145 | return 0; |
| 146 | } |
| 147 |
| 148 | if (*in == '-') { |
| 149 | neg = 1; |
| 150 | in++; |
| 151 | } |
| 152 |
| 153 | for (i = 0; want_char((unsigned char)in[i]) && i + neg < INT_MAX; i++) { |
| 154 | } |
| 155 |
| 156 | num = i + neg; |
| 157 | if (outp == NULL) { |
| 158 | return num; |
| 159 | } |
| 160 |
| 161 | // in is the start of the hex digits, and it is 'i' long |
| 162 | if (*outp == NULL) { |
| 163 | ret = BN_new(); |
| 164 | if (ret == NULL) { |
| 165 | return 0; |
| 166 | } |
| 167 | } else { |
| 168 | ret = *outp; |
| 169 | BN_zero(ret); |
| 170 | } |
| 171 |
| 172 | if (!decode(ret, in, i)) { |
| 173 | goto err; |
| 174 | } |
| 175 |
| 176 | bn_set_minimal_width(ret); |
| 177 | if (!BN_is_zero(ret)) { |
| 178 | ret->neg = neg; |
| 179 | } |
| 180 |
| 181 | *outp = ret; |
| 182 | return num; |
| 183 |
| 184 | err: |
| 185 | if (*outp == NULL) { |
| 186 | BN_free(ret); |
| 187 | } |
| 188 |
| 189 | return 0; |
| 190 | } |
| 191 |
| 192 | int BN_hex2bn(BIGNUM **outp, const char *in) { |
| 193 | return bn_x2bn(outp, in, decode_hex, OPENSSL_isxdigit); |
| 194 | } |
| 195 |
| 196 | char *BN_bn2dec(const BIGNUM *a) { |
| 197 | // It is easier to print strings little-endian, so we assemble it in reverse |
| 198 | // and fix at the end. |
| 199 | BIGNUM *copy = NULL; |
| 200 | CBB cbb; |
| 201 | if (!CBB_init(&cbb, 16) || // |
| 202 | !CBB_add_u8(&cbb, 0 /* trailing NUL */)) { |
| 203 | goto err; |
| 204 | } |
| 205 |
| 206 | if (BN_is_zero(a)) { |
| 207 | if (!CBB_add_u8(&cbb, '0')) { |
| 208 | goto err; |
| 209 | } |
| 210 | } else { |
| 211 | copy = BN_dup(a); |
| 212 | if (copy == NULL) { |
| 213 | goto err; |
| 214 | } |
| 215 |
| 216 | while (!BN_is_zero(copy)) { |
| 217 | BN_ULONG word = BN_div_word(copy, BN_DEC_CONV); |
| 218 | if (word == (BN_ULONG)-1) { |
| 219 | goto err; |
| 220 | } |
| 221 |
| 222 | const int add_leading_zeros = !BN_is_zero(copy); |
| 223 | for (int i = 0; i < BN_DEC_NUM && (add_leading_zeros || word != 0); i++) { |
| 224 | if (!CBB_add_u8(&cbb, '0' + word % 10)) { |
| 225 | goto err; |
| 226 | } |
| 227 | word /= 10; |
| 228 | } |
| 229 | assert(word == 0); |
| 230 | } |
| 231 | } |
| 232 |
| 233 | if (BN_is_negative(a) && // |
| 234 | !CBB_add_u8(&cbb, '-')) { |
| 235 | goto err; |
| 236 | } |
| 237 |
| 238 | uint8_t *data; |
| 239 | size_t len; |
| 240 | if (!CBB_finish(&cbb, &data, &len)) { |
| 241 | goto err; |
| 242 | } |
| 243 |
| 244 | // Reverse the buffer. |
| 245 | for (size_t i = 0; i < len / 2; i++) { |
| 246 | uint8_t tmp = data[i]; |
| 247 | data[i] = data[len - 1 - i]; |
| 248 | data[len - 1 - i] = tmp; |
| 249 | } |
| 250 |
| 251 | BN_free(copy); |
| 252 | return (char *)data; |
| 253 |
| 254 | err: |
| 255 | BN_free(copy); |
| 256 | CBB_cleanup(&cbb); |
| 257 | return NULL; |
| 258 | } |
| 259 |
| 260 | int BN_dec2bn(BIGNUM **outp, const char *in) { |
| 261 | return bn_x2bn(outp, in, decode_dec, OPENSSL_isdigit); |
| 262 | } |
| 263 |
| 264 | int BN_asc2bn(BIGNUM **outp, const char *in) { |
| 265 | const char *const orig_in = in; |
| 266 | if (*in == '-') { |
| 267 | in++; |
| 268 | } |
| 269 |
| 270 | if (in[0] == '0' && (in[1] == 'X' || in[1] == 'x')) { |
| 271 | if (!BN_hex2bn(outp, in + 2)) { |
| 272 | return 0; |
| 273 | } |
| 274 | } else { |
| 275 | if (!BN_dec2bn(outp, in)) { |
| 276 | return 0; |
| 277 | } |
| 278 | } |
| 279 |
| 280 | if (*orig_in == '-' && !BN_is_zero(*outp)) { |
| 281 | (*outp)->neg = 1; |
| 282 | } |
| 283 |
| 284 | return 1; |
| 285 | } |
| 286 |
| 287 | int BN_print(BIO *bp, const BIGNUM *a) { |
| 288 | int i, j, v, z = 0; |
| 289 | int ret = 0; |
| 290 |
| 291 | if (a->neg && BIO_write(bp, "-", 1) != 1) { |
| 292 | goto end; |
| 293 | } |
| 294 |
| 295 | if (BN_is_zero(a) && BIO_write(bp, "0", 1) != 1) { |
| 296 | goto end; |
| 297 | } |
| 298 |
| 299 | for (i = bn_minimal_width(a) - 1; i >= 0; i--) { |
| 300 | for (j = BN_BITS2 - 4; j >= 0; j -= 4) { |
| 301 | // strip leading zeros |
| 302 | v = ((int)(a->d[i] >> (long)j)) & 0x0f; |
| 303 | if (z || v != 0) { |
| 304 | if (BIO_write(bp, &hextable[v], 1) != 1) { |
| 305 | goto end; |
| 306 | } |
| 307 | z = 1; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | ret = 1; |
| 312 |
| 313 | end: |
| 314 | return ret; |
| 315 | } |
| 316 |
| 317 | int BN_print_fp(FILE *fp, const BIGNUM *a) { |
| 318 | BIO *b = BIO_new_fp(fp, BIO_NOCLOSE); |
| 319 | if (b == NULL) { |
| 320 | return 0; |
| 321 | } |
| 322 |
| 323 | int ret = BN_print(b, a); |
| 324 | BIO_free(b); |
| 325 | return ret; |
| 326 | } |
| 327 |
| 328 |
| 329 | size_t BN_bn2mpi(const BIGNUM *in, uint8_t *out) { |
| 330 | const size_t bits = BN_num_bits(in); |
| 331 | const size_t bytes = (bits + 7) / 8; |
| 332 | // If the number of bits is a multiple of 8, i.e. if the MSB is set, |
| 333 | // prefix with a zero byte. |
| 334 | int extend = 0; |
| 335 | if (bytes != 0 && (bits & 0x07) == 0) { |
| 336 | extend = 1; |
| 337 | } |
| 338 |
| 339 | const size_t len = bytes + extend; |
| 340 | if (len < bytes || 4 + len < len || (len & 0xffffffff) != len) { |
| 341 | // If we cannot represent the number then we emit zero as the interface |
| 342 | // doesn't allow an error to be signalled. |
| 343 | if (out) { |
| 344 | OPENSSL_memset(out, 0, 4); |
| 345 | } |
| 346 | return 4; |
| 347 | } |
| 348 |
| 349 | if (out == NULL) { |
| 350 | return 4 + len; |
| 351 | } |
| 352 |
| 353 | out[0] = len >> 24; |
| 354 | out[1] = len >> 16; |
| 355 | out[2] = len >> 8; |
| 356 | out[3] = len; |
| 357 | if (extend) { |
| 358 | out[4] = 0; |
| 359 | } |
| 360 | BN_bn2bin(in, out + 4 + extend); |
| 361 | if (in->neg && len > 0) { |
| 362 | out[4] |= 0x80; |
| 363 | } |
| 364 | return len + 4; |
| 365 | } |
| 366 |
| 367 | BIGNUM *BN_mpi2bn(const uint8_t *in, size_t len, BIGNUM *out) { |
| 368 | if (len < 4) { |
| 369 | OPENSSL_PUT_ERROR(BN, BN_R_BAD_ENCODING); |
| 370 | return NULL; |
| 371 | } |
| 372 | const size_t in_len = ((size_t)in[0] << 24) | // |
| 373 | ((size_t)in[1] << 16) | // |
| 374 | ((size_t)in[2] << 8) | // |
| 375 | ((size_t)in[3]); |
| 376 | if (in_len != len - 4) { |
| 377 | OPENSSL_PUT_ERROR(BN, BN_R_BAD_ENCODING); |
| 378 | return NULL; |
| 379 | } |
| 380 |
| 381 | int out_is_alloced = 0; |
| 382 | if (out == NULL) { |
| 383 | out = BN_new(); |
| 384 | if (out == NULL) { |
| 385 | return NULL; |
| 386 | } |
| 387 | out_is_alloced = 1; |
| 388 | } |
| 389 |
| 390 | if (in_len == 0) { |
| 391 | BN_zero(out); |
| 392 | return out; |
| 393 | } |
| 394 |
| 395 | in += 4; |
| 396 | if (BN_bin2bn(in, in_len, out) == NULL) { |
| 397 | if (out_is_alloced) { |
| 398 | BN_free(out); |
| 399 | } |
| 400 | return NULL; |
| 401 | } |
| 402 | out->neg = ((*in) & 0x80) != 0; |
| 403 | if (out->neg) { |
| 404 | BN_clear_bit(out, BN_num_bits(out) - 1); |
| 405 | } |
| 406 | return out; |
| 407 | } |
| 408 |
| 409 | int BN_bn2binpad(const BIGNUM *in, uint8_t *out, int len) { |
| 410 | if (len < 0 || // |
| 411 | !BN_bn2bin_padded(out, (size_t)len, in)) { |
| 412 | return -1; |
| 413 | } |
| 414 | return len; |
| 415 | } |
| 416 |
| 417 | int BN_bn2lebinpad(const BIGNUM *in, uint8_t *out, int len) { |
| 418 | if (len < 0 || // |
| 419 | !BN_bn2le_padded(out, (size_t)len, in)) { |
| 420 | return -1; |
| 421 | } |
| 422 | return len; |
| 423 | } |
| 424 |