bn.pod 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. =pod
  2. =head1 NAME
  3. bn - multiprecision integer arithmetics
  4. =head1 SYNOPSIS
  5. #include <openssl/bn.h>
  6. BIGNUM *BN_new(void);
  7. void BN_free(BIGNUM *a);
  8. void BN_init(BIGNUM *);
  9. void BN_clear(BIGNUM *a);
  10. void BN_clear_free(BIGNUM *a);
  11. BN_CTX *BN_CTX_new(void);
  12. void BN_CTX_init(BN_CTX *c);
  13. void BN_CTX_free(BN_CTX *c);
  14. BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b);
  15. BIGNUM *BN_dup(const BIGNUM *a);
  16. BIGNUM *BN_swap(BIGNUM *a, BIGNUM *b);
  17. int BN_num_bytes(const BIGNUM *a);
  18. int BN_num_bits(const BIGNUM *a);
  19. int BN_num_bits_word(BN_ULONG w);
  20. void BN_set_negative(BIGNUM *a, int n);
  21. int BN_is_negative(const BIGNUM *a);
  22. int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
  23. int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
  24. int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);
  25. int BN_sqr(BIGNUM *r, BIGNUM *a, BN_CTX *ctx);
  26. int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *a, const BIGNUM *d,
  27. BN_CTX *ctx);
  28. int BN_mod(BIGNUM *rem, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
  29. int BN_nnmod(BIGNUM *rem, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
  30. int BN_mod_add(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const BIGNUM *m,
  31. BN_CTX *ctx);
  32. int BN_mod_sub(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const BIGNUM *m,
  33. BN_CTX *ctx);
  34. int BN_mod_mul(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const BIGNUM *m,
  35. BN_CTX *ctx);
  36. int BN_mod_sqr(BIGNUM *ret, BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
  37. int BN_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BN_CTX *ctx);
  38. int BN_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
  39. const BIGNUM *m, BN_CTX *ctx);
  40. int BN_gcd(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);
  41. int BN_add_word(BIGNUM *a, BN_ULONG w);
  42. int BN_sub_word(BIGNUM *a, BN_ULONG w);
  43. int BN_mul_word(BIGNUM *a, BN_ULONG w);
  44. BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w);
  45. BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w);
  46. int BN_cmp(BIGNUM *a, BIGNUM *b);
  47. int BN_ucmp(BIGNUM *a, BIGNUM *b);
  48. int BN_is_zero(BIGNUM *a);
  49. int BN_is_one(BIGNUM *a);
  50. int BN_is_word(BIGNUM *a, BN_ULONG w);
  51. int BN_is_odd(BIGNUM *a);
  52. int BN_zero(BIGNUM *a);
  53. int BN_one(BIGNUM *a);
  54. const BIGNUM *BN_value_one(void);
  55. int BN_set_word(BIGNUM *a, unsigned long w);
  56. unsigned long BN_get_word(BIGNUM *a);
  57. int BN_rand(BIGNUM *rnd, int bits, int top, int bottom);
  58. int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom);
  59. int BN_rand_range(BIGNUM *rnd, BIGNUM *range);
  60. int BN_pseudo_rand_range(BIGNUM *rnd, BIGNUM *range);
  61. BIGNUM *BN_generate_prime(BIGNUM *ret, int bits,int safe, BIGNUM *add,
  62. BIGNUM *rem, void (*callback)(int, int, void *), void *cb_arg);
  63. int BN_is_prime(const BIGNUM *p, int nchecks,
  64. void (*callback)(int, int, void *), BN_CTX *ctx, void *cb_arg);
  65. int BN_set_bit(BIGNUM *a, int n);
  66. int BN_clear_bit(BIGNUM *a, int n);
  67. int BN_is_bit_set(const BIGNUM *a, int n);
  68. int BN_mask_bits(BIGNUM *a, int n);
  69. int BN_lshift(BIGNUM *r, const BIGNUM *a, int n);
  70. int BN_lshift1(BIGNUM *r, BIGNUM *a);
  71. int BN_rshift(BIGNUM *r, BIGNUM *a, int n);
  72. int BN_rshift1(BIGNUM *r, BIGNUM *a);
  73. int BN_bn2bin(const BIGNUM *a, unsigned char *to);
  74. BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret);
  75. char *BN_bn2hex(const BIGNUM *a);
  76. char *BN_bn2dec(const BIGNUM *a);
  77. int BN_hex2bn(BIGNUM **a, const char *str);
  78. int BN_dec2bn(BIGNUM **a, const char *str);
  79. int BN_print(BIO *fp, const BIGNUM *a);
  80. int BN_print_fp(FILE *fp, const BIGNUM *a);
  81. int BN_bn2mpi(const BIGNUM *a, unsigned char *to);
  82. BIGNUM *BN_mpi2bn(unsigned char *s, int len, BIGNUM *ret);
  83. BIGNUM *BN_mod_inverse(BIGNUM *r, BIGNUM *a, const BIGNUM *n,
  84. BN_CTX *ctx);
  85. BN_RECP_CTX *BN_RECP_CTX_new(void);
  86. void BN_RECP_CTX_init(BN_RECP_CTX *recp);
  87. void BN_RECP_CTX_free(BN_RECP_CTX *recp);
  88. int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *m, BN_CTX *ctx);
  89. int BN_mod_mul_reciprocal(BIGNUM *r, BIGNUM *a, BIGNUM *b,
  90. BN_RECP_CTX *recp, BN_CTX *ctx);
  91. BN_MONT_CTX *BN_MONT_CTX_new(void);
  92. void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
  93. void BN_MONT_CTX_free(BN_MONT_CTX *mont);
  94. int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *m, BN_CTX *ctx);
  95. BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from);
  96. int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b,
  97. BN_MONT_CTX *mont, BN_CTX *ctx);
  98. int BN_from_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont,
  99. BN_CTX *ctx);
  100. int BN_to_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont,
  101. BN_CTX *ctx);
  102. BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai,
  103. BIGNUM *mod);
  104. void BN_BLINDING_free(BN_BLINDING *b);
  105. int BN_BLINDING_update(BN_BLINDING *b,BN_CTX *ctx);
  106. int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
  107. int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
  108. int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b,
  109. BN_CTX *ctx);
  110. int BN_BLINDING_invert_ex(BIGNUM *n,const BIGNUM *r,BN_BLINDING *b,
  111. BN_CTX *ctx);
  112. unsigned long BN_BLINDING_get_thread_id(const BN_BLINDING *);
  113. void BN_BLINDING_set_thread_id(BN_BLINDING *, unsigned long);
  114. unsigned long BN_BLINDING_get_flags(const BN_BLINDING *);
  115. void BN_BLINDING_set_flags(BN_BLINDING *, unsigned long);
  116. BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,
  117. const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,
  118. int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  119. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx),
  120. BN_MONT_CTX *m_ctx);
  121. =head1 DESCRIPTION
  122. This library performs arithmetic operations on integers of arbitrary
  123. size. It was written for use in public key cryptography, such as RSA
  124. and Diffie-Hellman.
  125. It uses dynamic memory allocation for storing its data structures.
  126. That means that there is no limit on the size of the numbers
  127. manipulated by these functions, but return values must always be
  128. checked in case a memory allocation error has occurred.
  129. The basic object in this library is a B<BIGNUM>. It is used to hold a
  130. single large integer. This type should be considered opaque and fields
  131. should not be modified or accessed directly.
  132. The creation of B<BIGNUM> objects is described in L<BN_new(3)|BN_new(3)>;
  133. L<BN_add(3)|BN_add(3)> describes most of the arithmetic operations.
  134. Comparison is described in L<BN_cmp(3)|BN_cmp(3)>; L<BN_zero(3)|BN_zero(3)>
  135. describes certain assignments, L<BN_rand(3)|BN_rand(3)> the generation of
  136. random numbers, L<BN_generate_prime(3)|BN_generate_prime(3)> deals with prime
  137. numbers and L<BN_set_bit(3)|BN_set_bit(3)> with bit operations. The conversion
  138. of B<BIGNUM>s to external formats is described in L<BN_bn2bin(3)|BN_bn2bin(3)>.
  139. =head1 SEE ALSO
  140. L<bn_internal(3)|bn_internal(3)>,
  141. L<dh(3)|dh(3)>, L<err(3)|err(3)>, L<rand(3)|rand(3)>, L<rsa(3)|rsa(3)>,
  142. L<BN_new(3)|BN_new(3)>, L<BN_CTX_new(3)|BN_CTX_new(3)>,
  143. L<BN_copy(3)|BN_copy(3)>, L<BN_swap(3)|BN_swap(3)>, L<BN_num_bytes(3)|BN_num_bytes(3)>,
  144. L<BN_add(3)|BN_add(3)>, L<BN_add_word(3)|BN_add_word(3)>,
  145. L<BN_cmp(3)|BN_cmp(3)>, L<BN_zero(3)|BN_zero(3)>, L<BN_rand(3)|BN_rand(3)>,
  146. L<BN_generate_prime(3)|BN_generate_prime(3)>, L<BN_set_bit(3)|BN_set_bit(3)>,
  147. L<BN_bn2bin(3)|BN_bn2bin(3)>, L<BN_mod_inverse(3)|BN_mod_inverse(3)>,
  148. L<BN_mod_mul_reciprocal(3)|BN_mod_mul_reciprocal(3)>,
  149. L<BN_mod_mul_montgomery(3)|BN_mod_mul_montgomery(3)>,
  150. L<BN_BLINDING_new(3)|BN_BLINDING_new(3)>
  151. =cut