bn.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2014-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef OSSL_CRYPTO_BN_H
  10. # define OSSL_CRYPTO_BN_H
  11. # pragma once
  12. # include <openssl/bn.h>
  13. # include <limits.h>
  14. BIGNUM *bn_wexpand(BIGNUM *a, int words);
  15. BIGNUM *bn_expand2(BIGNUM *a, int words);
  16. void bn_correct_top(BIGNUM *a);
  17. /*
  18. * Determine the modified width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'.
  19. * This is an array r[] of values that are either zero or odd with an
  20. * absolute value less than 2^w satisfying scalar = \sum_j r[j]*2^j where at
  21. * most one of any w+1 consecutive digits is non-zero with the exception that
  22. * the most significant digit may be only w-1 zeros away from that next
  23. * non-zero digit.
  24. */
  25. signed char *bn_compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len);
  26. int bn_get_top(const BIGNUM *a);
  27. int bn_get_dmax(const BIGNUM *a);
  28. /* Set all words to zero */
  29. void bn_set_all_zero(BIGNUM *a);
  30. /*
  31. * Copy the internal BIGNUM words into out which holds size elements (and size
  32. * must be bigger than top)
  33. */
  34. int bn_copy_words(BN_ULONG *out, const BIGNUM *in, int size);
  35. BN_ULONG *bn_get_words(const BIGNUM *a);
  36. /*
  37. * Set the internal data words in a to point to words which contains size
  38. * elements. The BN_FLG_STATIC_DATA flag is set
  39. */
  40. void bn_set_static_words(BIGNUM *a, const BN_ULONG *words, int size);
  41. /*
  42. * Copy words into the BIGNUM |a|, reallocating space as necessary.
  43. * The negative flag of |a| is not modified.
  44. * Returns 1 on success and 0 on failure.
  45. */
  46. /*
  47. * |num_words| is int because bn_expand2 takes an int. This is an internal
  48. * function so we simply trust callers not to pass negative values.
  49. */
  50. int bn_set_words(BIGNUM *a, const BN_ULONG *words, int num_words);
  51. /*
  52. * Some BIGNUM functions assume most significant limb to be non-zero, which
  53. * is customarily arranged by bn_correct_top. Output from below functions
  54. * is not processed with bn_correct_top, and for this reason it may not be
  55. * returned out of public API. It may only be passed internally into other
  56. * functions known to support non-minimal or zero-padded BIGNUMs. Even
  57. * though the goal is to facilitate constant-time-ness, not each subroutine
  58. * is constant-time by itself. They all have pre-conditions, consult source
  59. * code...
  60. */
  61. int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  62. BN_MONT_CTX *mont, BN_CTX *ctx);
  63. int bn_to_mont_fixed_top(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
  64. BN_CTX *ctx);
  65. int bn_from_mont_fixed_top(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
  66. BN_CTX *ctx);
  67. int bn_mod_add_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  68. const BIGNUM *m);
  69. int bn_mod_sub_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  70. const BIGNUM *m);
  71. int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
  72. int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx);
  73. int bn_lshift_fixed_top(BIGNUM *r, const BIGNUM *a, int n);
  74. int bn_rshift_fixed_top(BIGNUM *r, const BIGNUM *a, int n);
  75. int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,
  76. const BIGNUM *d, BN_CTX *ctx);
  77. #define BN_PRIMETEST_COMPOSITE 0
  78. #define BN_PRIMETEST_COMPOSITE_WITH_FACTOR 1
  79. #define BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME 2
  80. #define BN_PRIMETEST_PROBABLY_PRIME 3
  81. int ossl_bn_miller_rabin_is_prime(const BIGNUM *w, int iterations, BN_CTX *ctx,
  82. BN_GENCB *cb, int enhanced, int *status);
  83. const BIGNUM *ossl_bn_get0_small_factors(void);
  84. int ossl_bn_rsa_fips186_4_gen_prob_primes(BIGNUM *p, BIGNUM *Xpout,
  85. BIGNUM *p1, BIGNUM *p2,
  86. const BIGNUM *Xp, const BIGNUM *Xp1,
  87. const BIGNUM *Xp2, int nlen,
  88. const BIGNUM *e, BN_CTX *ctx,
  89. BN_GENCB *cb);
  90. int ossl_bn_rsa_fips186_4_derive_prime(BIGNUM *Y, BIGNUM *X, const BIGNUM *Xin,
  91. const BIGNUM *r1, const BIGNUM *r2,
  92. int nlen, const BIGNUM *e, BN_CTX *ctx,
  93. BN_GENCB *cb);
  94. OSSL_LIB_CTX *ossl_bn_get_libctx(BN_CTX *ctx);
  95. extern const BIGNUM ossl_bn_inv_sqrt_2;
  96. #endif