dh.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. from __future__ import absolute_import, division, print_function
  5. INCLUDES = """
  6. #include <openssl/dh.h>
  7. """
  8. TYPES = """
  9. typedef ... DH;
  10. const long DH_NOT_SUITABLE_GENERATOR;
  11. """
  12. FUNCTIONS = """
  13. DH *DH_new(void);
  14. void DH_free(DH *);
  15. int DH_size(const DH *);
  16. int DH_generate_key(DH *);
  17. int DH_compute_key(unsigned char *, const BIGNUM *, DH *);
  18. DH *DHparams_dup(DH *);
  19. /* added in 1.1.0 when the DH struct was opaqued */
  20. void DH_get0_pqg(const DH *, const BIGNUM **, const BIGNUM **,
  21. const BIGNUM **);
  22. int DH_set0_pqg(DH *, BIGNUM *, BIGNUM *, BIGNUM *);
  23. void DH_get0_key(const DH *, const BIGNUM **, const BIGNUM **);
  24. int DH_set0_key(DH *, BIGNUM *, BIGNUM *);
  25. int Cryptography_DH_check(const DH *, int *);
  26. int DH_generate_parameters_ex(DH *, int, int, BN_GENCB *);
  27. DH *d2i_DHparams_bio(BIO *, DH **);
  28. int i2d_DHparams_bio(BIO *, DH *);
  29. DH *Cryptography_d2i_DHxparams_bio(BIO *bp, DH **x);
  30. int Cryptography_i2d_DHxparams_bio(BIO *bp, DH *x);
  31. """
  32. CUSTOMIZATIONS = """
  33. #if CRYPTOGRAPHY_IS_LIBRESSL
  34. #ifndef DH_CHECK_Q_NOT_PRIME
  35. #define DH_CHECK_Q_NOT_PRIME 0x10
  36. #endif
  37. #ifndef DH_CHECK_INVALID_Q_VALUE
  38. #define DH_CHECK_INVALID_Q_VALUE 0x20
  39. #endif
  40. #ifndef DH_CHECK_INVALID_J_VALUE
  41. #define DH_CHECK_INVALID_J_VALUE 0x40
  42. #endif
  43. /* DH_check implementation taken from OpenSSL 1.1.0pre6 */
  44. /*-
  45. * Check that p is a safe prime and
  46. * if g is 2, 3 or 5, check that it is a suitable generator
  47. * where
  48. * for 2, p mod 24 == 11
  49. * for 3, p mod 12 == 5
  50. * for 5, p mod 10 == 3 or 7
  51. * should hold.
  52. */
  53. int Cryptography_DH_check(const DH *dh, int *ret)
  54. {
  55. int ok = 0, r;
  56. BN_CTX *ctx = NULL;
  57. BN_ULONG l;
  58. BIGNUM *t1 = NULL, *t2 = NULL;
  59. *ret = 0;
  60. ctx = BN_CTX_new();
  61. if (ctx == NULL)
  62. goto err;
  63. BN_CTX_start(ctx);
  64. t1 = BN_CTX_get(ctx);
  65. if (t1 == NULL)
  66. goto err;
  67. t2 = BN_CTX_get(ctx);
  68. if (t2 == NULL)
  69. goto err;
  70. if (dh->q) {
  71. if (BN_cmp(dh->g, BN_value_one()) <= 0)
  72. *ret |= DH_NOT_SUITABLE_GENERATOR;
  73. else if (BN_cmp(dh->g, dh->p) >= 0)
  74. *ret |= DH_NOT_SUITABLE_GENERATOR;
  75. else {
  76. /* Check g^q == 1 mod p */
  77. if (!BN_mod_exp(t1, dh->g, dh->q, dh->p, ctx))
  78. goto err;
  79. if (!BN_is_one(t1))
  80. *ret |= DH_NOT_SUITABLE_GENERATOR;
  81. }
  82. r = BN_is_prime_ex(dh->q, BN_prime_checks, ctx, NULL);
  83. if (r < 0)
  84. goto err;
  85. if (!r)
  86. *ret |= DH_CHECK_Q_NOT_PRIME;
  87. /* Check p == 1 mod q i.e. q divides p - 1 */
  88. if (!BN_div(t1, t2, dh->p, dh->q, ctx))
  89. goto err;
  90. if (!BN_is_one(t2))
  91. *ret |= DH_CHECK_INVALID_Q_VALUE;
  92. if (dh->j && BN_cmp(dh->j, t1))
  93. *ret |= DH_CHECK_INVALID_J_VALUE;
  94. } else if (BN_is_word(dh->g, DH_GENERATOR_2)) {
  95. l = BN_mod_word(dh->p, 24);
  96. if (l == (BN_ULONG)-1)
  97. goto err;
  98. if (l != 11)
  99. *ret |= DH_NOT_SUITABLE_GENERATOR;
  100. } else if (BN_is_word(dh->g, DH_GENERATOR_5)) {
  101. l = BN_mod_word(dh->p, 10);
  102. if (l == (BN_ULONG)-1)
  103. goto err;
  104. if ((l != 3) && (l != 7))
  105. *ret |= DH_NOT_SUITABLE_GENERATOR;
  106. } else
  107. *ret |= DH_UNABLE_TO_CHECK_GENERATOR;
  108. r = BN_is_prime_ex(dh->p, BN_prime_checks, ctx, NULL);
  109. if (r < 0)
  110. goto err;
  111. if (!r)
  112. *ret |= DH_CHECK_P_NOT_PRIME;
  113. else if (!dh->q) {
  114. if (!BN_rshift1(t1, dh->p))
  115. goto err;
  116. r = BN_is_prime_ex(t1, BN_prime_checks, ctx, NULL);
  117. if (r < 0)
  118. goto err;
  119. if (!r)
  120. *ret |= DH_CHECK_P_NOT_SAFE_PRIME;
  121. }
  122. ok = 1;
  123. err:
  124. if (ctx != NULL) {
  125. BN_CTX_end(ctx);
  126. BN_CTX_free(ctx);
  127. }
  128. return (ok);
  129. }
  130. #else
  131. int Cryptography_DH_check(const DH *dh, int *ret) {
  132. return DH_check(dh, ret);
  133. }
  134. #endif
  135. /* These functions were added in OpenSSL 1.1.0f commit d0c50e80a8 */
  136. /* Define our own to simplify support across all versions. */
  137. #if defined(EVP_PKEY_DHX) && EVP_PKEY_DHX != -1
  138. DH *Cryptography_d2i_DHxparams_bio(BIO *bp, DH **x) {
  139. return ASN1_d2i_bio_of(DH, DH_new, d2i_DHxparams, bp, x);
  140. }
  141. int Cryptography_i2d_DHxparams_bio(BIO *bp, DH *x) {
  142. return ASN1_i2d_bio_of_const(DH, i2d_DHxparams, bp, x);
  143. }
  144. #else
  145. DH *(*Cryptography_d2i_DHxparams_bio)(BIO *bp, DH **x) = NULL;
  146. int (*Cryptography_i2d_DHxparams_bio)(BIO *bp, DH *x) = NULL;
  147. #endif
  148. """