bn.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 2014-2024 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. int ossl_bn_mask_bits_fixed_top(BIGNUM *a, int n);
  78. int ossl_bn_is_word_fixed_top(const BIGNUM *a, BN_ULONG w);
  79. int ossl_bn_priv_rand_range_fixed_top(BIGNUM *r, const BIGNUM *range,
  80. unsigned int strength, BN_CTX *ctx);
  81. int ossl_bn_gen_dsa_nonce_fixed_top(BIGNUM *out, const BIGNUM *range,
  82. const BIGNUM *priv,
  83. const unsigned char *message,
  84. size_t message_len, BN_CTX *ctx);
  85. #define BN_PRIMETEST_COMPOSITE 0
  86. #define BN_PRIMETEST_COMPOSITE_WITH_FACTOR 1
  87. #define BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME 2
  88. #define BN_PRIMETEST_PROBABLY_PRIME 3
  89. int ossl_bn_miller_rabin_is_prime(const BIGNUM *w, int iterations, BN_CTX *ctx,
  90. BN_GENCB *cb, int enhanced, int *status);
  91. int ossl_bn_check_generated_prime(const BIGNUM *w, int checks, BN_CTX *ctx,
  92. BN_GENCB *cb);
  93. const BIGNUM *ossl_bn_get0_small_factors(void);
  94. int ossl_bn_rsa_fips186_4_gen_prob_primes(BIGNUM *p, BIGNUM *Xpout,
  95. BIGNUM *p1, BIGNUM *p2,
  96. const BIGNUM *Xp, const BIGNUM *Xp1,
  97. const BIGNUM *Xp2, int nlen,
  98. const BIGNUM *e, BN_CTX *ctx,
  99. BN_GENCB *cb);
  100. int ossl_bn_rsa_fips186_4_derive_prime(BIGNUM *Y, BIGNUM *X, const BIGNUM *Xin,
  101. const BIGNUM *r1, const BIGNUM *r2,
  102. int nlen, const BIGNUM *e, BN_CTX *ctx,
  103. BN_GENCB *cb);
  104. OSSL_LIB_CTX *ossl_bn_get_libctx(BN_CTX *ctx);
  105. extern const BIGNUM ossl_bn_inv_sqrt_2;
  106. #if defined(OPENSSL_SYS_LINUX) && !defined(FIPS_MODULE) && defined (__s390x__) \
  107. && !defined (OPENSSL_NO_ASM)
  108. # define S390X_MOD_EXP
  109. #endif
  110. int s390x_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  111. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  112. int s390x_crt(BIGNUM *r, const BIGNUM *i, const BIGNUM *p, const BIGNUM *q,
  113. const BIGNUM *dmp, const BIGNUM *dmq, const BIGNUM *iqmp);
  114. #endif