2
0

ec_support.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright 2020-2022 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 <string.h>
  10. #include <openssl/ec.h>
  11. #include "crypto/ec.h"
  12. #include "internal/nelem.h"
  13. typedef struct ec_name2nid_st {
  14. const char *name;
  15. int nid;
  16. } EC_NAME2NID;
  17. static const EC_NAME2NID curve_list[] = {
  18. /* prime field curves */
  19. /* secg curves */
  20. {"secp112r1", NID_secp112r1 },
  21. {"secp112r2", NID_secp112r2 },
  22. {"secp128r1", NID_secp128r1 },
  23. {"secp128r2", NID_secp128r2 },
  24. {"secp160k1", NID_secp160k1 },
  25. {"secp160r1", NID_secp160r1 },
  26. {"secp160r2", NID_secp160r2 },
  27. {"secp192k1", NID_secp192k1 },
  28. {"secp224k1", NID_secp224k1 },
  29. {"secp224r1", NID_secp224r1 },
  30. {"secp256k1", NID_secp256k1 },
  31. {"secp384r1", NID_secp384r1 },
  32. {"secp521r1", NID_secp521r1 },
  33. /* X9.62 curves */
  34. {"prime192v1", NID_X9_62_prime192v1 },
  35. {"prime192v2", NID_X9_62_prime192v2 },
  36. {"prime192v3", NID_X9_62_prime192v3 },
  37. {"prime239v1", NID_X9_62_prime239v1 },
  38. {"prime239v2", NID_X9_62_prime239v2 },
  39. {"prime239v3", NID_X9_62_prime239v3 },
  40. {"prime256v1", NID_X9_62_prime256v1 },
  41. /* characteristic two field curves */
  42. /* NIST/SECG curves */
  43. {"sect113r1", NID_sect113r1 },
  44. {"sect113r2", NID_sect113r2 },
  45. {"sect131r1", NID_sect131r1 },
  46. {"sect131r2", NID_sect131r2 },
  47. {"sect163k1", NID_sect163k1 },
  48. {"sect163r1", NID_sect163r1 },
  49. {"sect163r2", NID_sect163r2 },
  50. {"sect193r1", NID_sect193r1 },
  51. {"sect193r2", NID_sect193r2 },
  52. {"sect233k1", NID_sect233k1 },
  53. {"sect233r1", NID_sect233r1 },
  54. {"sect239k1", NID_sect239k1 },
  55. {"sect283k1", NID_sect283k1 },
  56. {"sect283r1", NID_sect283r1 },
  57. {"sect409k1", NID_sect409k1 },
  58. {"sect409r1", NID_sect409r1 },
  59. {"sect571k1", NID_sect571k1 },
  60. {"sect571r1", NID_sect571r1 },
  61. /* X9.62 curves */
  62. {"c2pnb163v1", NID_X9_62_c2pnb163v1 },
  63. {"c2pnb163v2", NID_X9_62_c2pnb163v2 },
  64. {"c2pnb163v3", NID_X9_62_c2pnb163v3 },
  65. {"c2pnb176v1", NID_X9_62_c2pnb176v1 },
  66. {"c2tnb191v1", NID_X9_62_c2tnb191v1 },
  67. {"c2tnb191v2", NID_X9_62_c2tnb191v2 },
  68. {"c2tnb191v3", NID_X9_62_c2tnb191v3 },
  69. {"c2pnb208w1", NID_X9_62_c2pnb208w1 },
  70. {"c2tnb239v1", NID_X9_62_c2tnb239v1 },
  71. {"c2tnb239v2", NID_X9_62_c2tnb239v2 },
  72. {"c2tnb239v3", NID_X9_62_c2tnb239v3 },
  73. {"c2pnb272w1", NID_X9_62_c2pnb272w1 },
  74. {"c2pnb304w1", NID_X9_62_c2pnb304w1 },
  75. {"c2tnb359v1", NID_X9_62_c2tnb359v1 },
  76. {"c2pnb368w1", NID_X9_62_c2pnb368w1 },
  77. {"c2tnb431r1", NID_X9_62_c2tnb431r1 },
  78. /*
  79. * the WAP/WTLS curves [unlike SECG, spec has its own OIDs for curves
  80. * from X9.62]
  81. */
  82. {"wap-wsg-idm-ecid-wtls1", NID_wap_wsg_idm_ecid_wtls1 },
  83. {"wap-wsg-idm-ecid-wtls3", NID_wap_wsg_idm_ecid_wtls3 },
  84. {"wap-wsg-idm-ecid-wtls4", NID_wap_wsg_idm_ecid_wtls4 },
  85. {"wap-wsg-idm-ecid-wtls5", NID_wap_wsg_idm_ecid_wtls5 },
  86. {"wap-wsg-idm-ecid-wtls6", NID_wap_wsg_idm_ecid_wtls6 },
  87. {"wap-wsg-idm-ecid-wtls7", NID_wap_wsg_idm_ecid_wtls7 },
  88. {"wap-wsg-idm-ecid-wtls8", NID_wap_wsg_idm_ecid_wtls8 },
  89. {"wap-wsg-idm-ecid-wtls9", NID_wap_wsg_idm_ecid_wtls9 },
  90. {"wap-wsg-idm-ecid-wtls10", NID_wap_wsg_idm_ecid_wtls10 },
  91. {"wap-wsg-idm-ecid-wtls11", NID_wap_wsg_idm_ecid_wtls11 },
  92. {"wap-wsg-idm-ecid-wtls12", NID_wap_wsg_idm_ecid_wtls12 },
  93. /* IPSec curves */
  94. {"Oakley-EC2N-3", NID_ipsec3 },
  95. {"Oakley-EC2N-4", NID_ipsec4 },
  96. /* brainpool curves */
  97. {"brainpoolP160r1", NID_brainpoolP160r1 },
  98. {"brainpoolP160t1", NID_brainpoolP160t1 },
  99. {"brainpoolP192r1", NID_brainpoolP192r1 },
  100. {"brainpoolP192t1", NID_brainpoolP192t1 },
  101. {"brainpoolP224r1", NID_brainpoolP224r1 },
  102. {"brainpoolP224t1", NID_brainpoolP224t1 },
  103. {"brainpoolP256r1", NID_brainpoolP256r1 },
  104. {"brainpoolP256t1", NID_brainpoolP256t1 },
  105. {"brainpoolP320r1", NID_brainpoolP320r1 },
  106. {"brainpoolP320t1", NID_brainpoolP320t1 },
  107. {"brainpoolP384r1", NID_brainpoolP384r1 },
  108. {"brainpoolP384t1", NID_brainpoolP384t1 },
  109. {"brainpoolP512r1", NID_brainpoolP512r1 },
  110. {"brainpoolP512t1", NID_brainpoolP512t1 },
  111. /* SM2 curve */
  112. {"SM2", NID_sm2 },
  113. };
  114. const char *OSSL_EC_curve_nid2name(int nid)
  115. {
  116. size_t i;
  117. if (nid <= 0)
  118. return NULL;
  119. for (i = 0; i < OSSL_NELEM(curve_list); i++) {
  120. if (curve_list[i].nid == nid)
  121. return curve_list[i].name;
  122. }
  123. return NULL;
  124. }
  125. int ossl_ec_curve_name2nid(const char *name)
  126. {
  127. size_t i;
  128. int nid;
  129. if (name != NULL) {
  130. if ((nid = ossl_ec_curve_nist2nid_int(name)) != NID_undef)
  131. return nid;
  132. for (i = 0; i < OSSL_NELEM(curve_list); i++) {
  133. if (OPENSSL_strcasecmp(curve_list[i].name, name) == 0)
  134. return curve_list[i].nid;
  135. }
  136. }
  137. return NID_undef;
  138. }
  139. /* Functions to translate between common NIST curve names and NIDs */
  140. static const EC_NAME2NID nist_curves[] = {
  141. {"B-163", NID_sect163r2},
  142. {"B-233", NID_sect233r1},
  143. {"B-283", NID_sect283r1},
  144. {"B-409", NID_sect409r1},
  145. {"B-571", NID_sect571r1},
  146. {"K-163", NID_sect163k1},
  147. {"K-233", NID_sect233k1},
  148. {"K-283", NID_sect283k1},
  149. {"K-409", NID_sect409k1},
  150. {"K-571", NID_sect571k1},
  151. {"P-192", NID_X9_62_prime192v1},
  152. {"P-224", NID_secp224r1},
  153. {"P-256", NID_X9_62_prime256v1},
  154. {"P-384", NID_secp384r1},
  155. {"P-521", NID_secp521r1}
  156. };
  157. const char *ossl_ec_curve_nid2nist_int(int nid)
  158. {
  159. size_t i;
  160. for (i = 0; i < OSSL_NELEM(nist_curves); i++) {
  161. if (nist_curves[i].nid == nid)
  162. return nist_curves[i].name;
  163. }
  164. return NULL;
  165. }
  166. int ossl_ec_curve_nist2nid_int(const char *name)
  167. {
  168. size_t i;
  169. for (i = 0; i < OSSL_NELEM(nist_curves); i++) {
  170. if (strcmp(nist_curves[i].name, name) == 0)
  171. return nist_curves[i].nid;
  172. }
  173. return NID_undef;
  174. }