ffc_dh.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2020-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. #include "internal/ffc.h"
  10. #include "internal/nelem.h"
  11. #include "crypto/bn_dh.h"
  12. #include "e_os.h" /* strcasecmp */
  13. #ifndef OPENSSL_NO_DH
  14. # define FFDHE(sz) { \
  15. SN_ffdhe##sz, NID_ffdhe##sz, \
  16. sz, \
  17. &ossl_bignum_ffdhe##sz##_p, &ossl_bignum_ffdhe##sz##_q, \
  18. &ossl_bignum_const_2, \
  19. }
  20. # define MODP(sz) { \
  21. SN_modp_##sz, NID_modp_##sz, \
  22. sz, \
  23. &ossl_bignum_modp_##sz##_p, &ossl_bignum_modp_##sz##_q, \
  24. &ossl_bignum_const_2 \
  25. }
  26. # define RFC5114(name, uid, sz, tag) { \
  27. name, uid, \
  28. sz, \
  29. &ossl_bignum_dh##tag##_p, &ossl_bignum_dh##tag##_q, \
  30. &ossl_bignum_dh##tag##_g \
  31. }
  32. #else
  33. # define FFDHE(sz) { SN_ffdhe##sz, NID_ffdhe##sz }
  34. # define MODP(sz) { SN_modp_##sz, NID_modp_##sz }
  35. # define RFC5114(name, uid, sz, tag) { name, uid }
  36. #endif
  37. struct dh_named_group_st {
  38. const char *name;
  39. int uid;
  40. #ifndef OPENSSL_NO_DH
  41. int32_t nbits;
  42. const BIGNUM *p;
  43. const BIGNUM *q;
  44. const BIGNUM *g;
  45. #endif
  46. };
  47. static const DH_NAMED_GROUP dh_named_groups[] = {
  48. FFDHE(2048),
  49. FFDHE(3072),
  50. FFDHE(4096),
  51. FFDHE(6144),
  52. FFDHE(8192),
  53. #ifndef FIPS_MODULE
  54. MODP(1536),
  55. #endif
  56. MODP(2048),
  57. MODP(3072),
  58. MODP(4096),
  59. MODP(6144),
  60. MODP(8192),
  61. /*
  62. * Additional dh named groups from RFC 5114 that have a different g.
  63. * The uid can be any unique identifier.
  64. */
  65. #ifndef FIPS_MODULE
  66. RFC5114("dh_1024_160", 1, 1024, 1024_160),
  67. RFC5114("dh_2048_224", 2, 2048, 2048_224),
  68. RFC5114("dh_2048_256", 3, 2048, 2048_256),
  69. #endif
  70. };
  71. const DH_NAMED_GROUP *ossl_ffc_name_to_dh_named_group(const char *name)
  72. {
  73. size_t i;
  74. for (i = 0; i < OSSL_NELEM(dh_named_groups); ++i) {
  75. if (strcasecmp(dh_named_groups[i].name, name) == 0)
  76. return &dh_named_groups[i];
  77. }
  78. return NULL;
  79. }
  80. const DH_NAMED_GROUP *ossl_ffc_uid_to_dh_named_group(int uid)
  81. {
  82. size_t i;
  83. for (i = 0; i < OSSL_NELEM(dh_named_groups); ++i) {
  84. if (dh_named_groups[i].uid == uid)
  85. return &dh_named_groups[i];
  86. }
  87. return NULL;
  88. }
  89. #ifndef OPENSSL_NO_DH
  90. const DH_NAMED_GROUP *ossl_ffc_numbers_to_dh_named_group(const BIGNUM *p,
  91. const BIGNUM *q,
  92. const BIGNUM *g)
  93. {
  94. size_t i;
  95. for (i = 0; i < OSSL_NELEM(dh_named_groups); ++i) {
  96. /* Keep searching until a matching p and g is found */
  97. if (BN_cmp(p, dh_named_groups[i].p) == 0
  98. && BN_cmp(g, dh_named_groups[i].g) == 0
  99. /* Verify q is correct if it exists */
  100. && (q == NULL || BN_cmp(q, dh_named_groups[i].q) == 0))
  101. return &dh_named_groups[i];
  102. }
  103. return NULL;
  104. }
  105. #endif
  106. int ossl_ffc_named_group_get_uid(const DH_NAMED_GROUP *group)
  107. {
  108. if (group == NULL)
  109. return NID_undef;
  110. return group->uid;
  111. }
  112. const char *ossl_ffc_named_group_get_name(const DH_NAMED_GROUP *group)
  113. {
  114. if (group == NULL)
  115. return NULL;
  116. return group->name;
  117. }
  118. #ifndef OPENSSL_NO_DH
  119. const BIGNUM *ossl_ffc_named_group_get_q(const DH_NAMED_GROUP *group)
  120. {
  121. if (group == NULL)
  122. return NULL;
  123. return group->q;
  124. }
  125. int ossl_ffc_named_group_set_pqg(FFC_PARAMS *ffc, const DH_NAMED_GROUP *group)
  126. {
  127. if (ffc == NULL || group == NULL)
  128. return 0;
  129. ossl_ffc_params_set0_pqg(ffc, (BIGNUM *)group->p, (BIGNUM *)group->q,
  130. (BIGNUM *)group->g);
  131. /* flush the cached nid, The DH layer is responsible for caching */
  132. ffc->nid = NID_undef;
  133. return 1;
  134. }
  135. #endif