EVP_PKEY-EC.pod 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. =pod
  2. =head1 NAME
  3. EVP_PKEY-EC,
  4. EVP_KEYMGMT-EC
  5. - EVP_PKEY EC keytype and algorithm support
  6. =head1 DESCRIPTION
  7. The B<EC> keytype is implemented in OpenSSL's default provider.
  8. =head2 Common EC parameters
  9. The normal way of specifying domain parameters for an EC curve is via the
  10. curve name "group". For curves with no curve name, explicit parameters can be
  11. used that specify "field-type", "p", "a", "b", "generator" and "order".
  12. Explicit parameters are supported for backwards compatibility reasons, but they
  13. are not compliant with multiple standards (including RFC5915) which only allow
  14. named curves.
  15. The following KeyGen/Gettable/Import/Export types are available for the
  16. built-in EC algorithm:
  17. =over 4
  18. =item "group" (B<OSSL_PKEY_PARAM_GROUP_NAME>) <UTF8 string>
  19. The curve name.
  20. =item "field-type" (B<OSSL_PKEY_PARAM_EC_FIELD_TYPE>) <UTF8 string>
  21. The value should be either "prime-field" or "characteristic-two-field",
  22. which correspond to prime field Fp and binary field F2^m.
  23. =item "p" (B<OSSL_PKEY_PARAM_EC_P>) <unsigned integer>
  24. For a curve over Fp I<p> is the prime for the field. For a curve over F2^m I<p>
  25. represents the irreducible polynomial - each bit represents a term in the
  26. polynomial. Therefore, there will either be three or five bits set dependent on
  27. whether the polynomial is a trinomial or a pentanomial.
  28. =item "a" (B<OSSL_PKEY_PARAM_EC_A>) <unsigned integer>
  29. =item "b" (B<OSSL_PKEY_PARAM_EC_B>) <unsigned integer>
  30. =item "seed" (B<OSSL_PKEY_PARAM_EC_SEED>) <octet string>
  31. I<a> and I<b> represents the coefficients of the curve
  32. For Fp: y^2 mod p = x^3 +ax + b mod p OR
  33. For F2^m: y^2 + xy = x^3 + ax^2 + b
  34. I<seed> is an optional value that is for information purposes only.
  35. It represents the random number seed used to generate the coefficient I<b> from a
  36. random number.
  37. =item "generator" (B<OSSL_PKEY_PARAM_EC_GENERATOR>) <octet string>
  38. =item "order" (B<OSSL_PKEY_PARAM_EC_ORDER>) <unsigned integer>
  39. =item "cofactor" (B<OSSL_PKEY_PARAM_EC_COFACTOR>) <unsigned integer>
  40. The I<generator> is a well defined point on the curve chosen for cryptographic
  41. operations. The encoding conforms with Sec. 2.3.3 of the SECG SEC 1 ("Elliptic Curve
  42. Cryptography") standard. See EC_POINT_oct2point().
  43. Integers used for point multiplications will be between 0 and
  44. I<order> - 1.
  45. I<cofactor> is an optional value.
  46. I<order> multiplied by the I<cofactor> gives the number of points on the curve.
  47. =item "decoded-from-explicit" (B<OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS>) <integer>
  48. Gets a flag indicating whether the key or parameters were decoded from explicit
  49. curve parameters. Set to 1 if so or 0 if a named curve was used.
  50. =item "use-cofactor-flag" (B<OSSL_PKEY_PARAM_USE_COFACTOR_ECDH>) <integer>
  51. Enable Cofactor DH (ECC CDH) if this value is 1, otherwise it uses normal EC DH
  52. if the value is zero. The cofactor variant multiplies the shared secret by the
  53. EC curve's cofactor (note for some curves the cofactor is 1).
  54. See also L<EVP_KEYEXCH-ECDH(7)> for the related
  55. B<OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE> parameter that can be set on a
  56. per-operation basis.
  57. =item "encoding" (B<OSSL_PKEY_PARAM_EC_ENCODING>) <UTF8 string>
  58. Set the format used for serializing the EC group parameters.
  59. Valid values are "explicit" or "named_curve". The default value is "named_curve".
  60. =item "point-format" (B<OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT>) <UTF8 string>
  61. Sets or gets the point_conversion_form for the I<key>. For a description of
  62. point_conversion_forms please see L<EC_POINT_new(3)>. Valid values are
  63. "uncompressed" or "compressed". The default value is "uncompressed".
  64. =item "group-check" (B<OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE>) <UTF8 string>
  65. Sets or Gets the type of group check done when EVP_PKEY_param_check() is called.
  66. Valid values are "default", "named" and "named-nist".
  67. The "named" type checks that the domain parameters match the inbuilt curve parameters,
  68. "named-nist" is similar but also checks that the named curve is a nist curve.
  69. The "default" type does domain parameter validation for the OpenSSL default provider,
  70. but is equivalent to "named-nist" for the OpenSSL FIPS provider.
  71. =item "include-public" (B<OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC>) <integer>
  72. Setting this value to 0 indicates that the public key should not be included when
  73. encoding the private key. The default value of 1 will include the public key.
  74. =item "pub" (B<OSSL_PKEY_PARAM_PUB_KEY>) <octet string>
  75. The public key value in encoded EC point format conforming to Sec. 2.3.3 and
  76. 2.3.4 of the SECG SEC 1 ("Elliptic Curve Cryptography") standard.
  77. This parameter is used when importing or exporting the public key value with the
  78. EVP_PKEY_fromdata() and EVP_PKEY_todata() functions.
  79. Note, in particular, that the choice of point compression format used for
  80. encoding the exported value via EVP_PKEY_todata() depends on the underlying
  81. provider implementation.
  82. Before OpenSSL 3.0.8, the implementation of providers included with OpenSSL always
  83. opted for an encoding in compressed format, unconditionally.
  84. Since OpenSSL 3.0.8, the implementation has been changed to honor the
  85. B<OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT> parameter, if set, or to default
  86. to uncompressed format.
  87. =item "priv" (B<OSSL_PKEY_PARAM_PRIV_KEY>) <unsigned integer>
  88. The private key value.
  89. =item "encoded-pub-key" (B<OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY>) <octet string>
  90. Used for getting and setting the encoding of an EC public key. The public key
  91. is expected to be a point conforming to Sec. 2.3.4 of the SECG SEC 1 ("Elliptic
  92. Curve Cryptography") standard.
  93. =item "qx" (B<OSSL_PKEY_PARAM_EC_PUB_X>) <unsigned integer>
  94. Used for getting the EC public key X component.
  95. =item "qy" (B<OSSL_PKEY_PARAM_EC_PUB_Y>) <unsigned integer>
  96. Used for getting the EC public key Y component.
  97. =item "default-digest" (B<OSSL_PKEY_PARAM_DEFAULT_DIGEST>) <UTF8 string>
  98. Getter that returns the default digest name.
  99. (Currently returns "SHA256" as of OpenSSL 3.0).
  100. =item "dhkem-ikm" (B<OSSL_PKEY_PARAM_DHKEM_IKM>) <octet string>
  101. DHKEM requires the generation of a keypair using an input key material (seed).
  102. Use this to specify the key material used for generation of the private key.
  103. This value should not be reused for other purposes. It can only be used
  104. for the curves "P-256", "P-384" and "P-521" and should have a length of at least
  105. the size of the encoded private key (i.e. 32, 48 and 66 for the listed curves).
  106. =back
  107. The following Gettable types are also available for the built-in EC algorithm:
  108. =over 4
  109. =item "basis-type" (B<OSSL_PKEY_PARAM_EC_CHAR2_TYPE>) <UTF8 string>
  110. Supports the values "tpBasis" for a trinomial or "ppBasis" for a pentanomial.
  111. This field is only used for a binary field F2^m.
  112. =item "m" (B<OSSL_PKEY_PARAM_EC_CHAR2_M>) <integer>
  113. =item "tp" (B<OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS>) <integer>
  114. =item "k1" (B<OSSL_PKEY_PARAM_EC_CHAR2_PP_K1>) <integer>
  115. =item "k2" (B<OSSL_PKEY_PARAM_EC_CHAR2_PP_K2>) <integer>
  116. =item "k3" (B<OSSL_PKEY_PARAM_EC_CHAR2_PP_K3>) <integer>
  117. These fields are only used for a binary field F2^m.
  118. I<m> is the degree of the binary field.
  119. I<tp> is the middle bit of a trinomial so its value must be in the
  120. range m > tp > 0.
  121. I<k1>, I<k2> and I<k3> are used to get the middle bits of a pentanomial such
  122. that m > k3 > k2 > k1 > 0
  123. =back
  124. =head2 EC key validation
  125. For EC keys, L<EVP_PKEY_param_check(3)> behaves in the following way:
  126. For the OpenSSL default provider it uses either
  127. L<EC_GROUP_check(3)> or L<EC_GROUP_check_named_curve(3)> depending on the flag
  128. EC_FLAG_CHECK_NAMED_GROUP.
  129. The OpenSSL FIPS provider uses L<EC_GROUP_check_named_curve(3)> in order to
  130. conform to SP800-56Ar3 I<Assurances of Domain-Parameter Validity>.
  131. For EC keys, L<EVP_PKEY_param_check_quick(3)> is equivalent to
  132. L<EVP_PKEY_param_check(3)>.
  133. For EC keys, L<EVP_PKEY_public_check(3)> and L<EVP_PKEY_public_check_quick(3)>
  134. conform to SP800-56Ar3 I<ECC Full Public-Key Validation> and
  135. I<ECC Partial Public-Key Validation> respectively.
  136. For EC Keys, L<EVP_PKEY_private_check(3)> and L<EVP_PKEY_pairwise_check(3)>
  137. conform to SP800-56Ar3 I<Private key validity> and
  138. I<Owner Assurance of Pair-wise Consistency> respectively.
  139. =head1 EXAMPLES
  140. An B<EVP_PKEY> context can be obtained by calling:
  141. EVP_PKEY_CTX *pctx =
  142. EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
  143. An B<EVP_PKEY> ECDSA or ECDH key can be generated with a "P-256" named group by
  144. calling:
  145. pkey = EVP_EC_gen("P-256");
  146. or like this:
  147. EVP_PKEY *key = NULL;
  148. OSSL_PARAM params[2];
  149. EVP_PKEY_CTX *gctx =
  150. EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
  151. EVP_PKEY_keygen_init(gctx);
  152. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  153. "P-256", 0);
  154. params[1] = OSSL_PARAM_construct_end();
  155. EVP_PKEY_CTX_set_params(gctx, params);
  156. EVP_PKEY_generate(gctx, &key);
  157. EVP_PKEY_print_private(bio_out, key, 0, NULL);
  158. ...
  159. EVP_PKEY_free(key);
  160. EVP_PKEY_CTX_free(gctx);
  161. An B<EVP_PKEY> EC CDH (Cofactor Diffie-Hellman) key can be generated with a
  162. "K-571" named group by calling:
  163. int use_cdh = 1;
  164. EVP_PKEY *key = NULL;
  165. OSSL_PARAM params[3];
  166. EVP_PKEY_CTX *gctx =
  167. EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
  168. EVP_PKEY_keygen_init(gctx);
  169. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  170. "K-571", 0);
  171. /*
  172. * This curve has a cofactor that is not 1 - so setting CDH mode changes
  173. * the behaviour. For many curves the cofactor is 1 - so setting this has
  174. * no effect.
  175. */
  176. params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
  177. &use_cdh);
  178. params[2] = OSSL_PARAM_construct_end();
  179. EVP_PKEY_CTX_set_params(gctx, params);
  180. EVP_PKEY_generate(gctx, &key);
  181. EVP_PKEY_print_private(bio_out, key, 0, NULL);
  182. ...
  183. EVP_PKEY_free(key);
  184. EVP_PKEY_CTX_free(gctx);
  185. =head1 SEE ALSO
  186. L<EVP_EC_gen(3)>,
  187. L<EVP_KEYMGMT(3)>,
  188. L<EVP_PKEY(3)>,
  189. L<provider-keymgmt(7)>,
  190. L<EVP_SIGNATURE-ECDSA(7)>,
  191. L<EVP_KEYEXCH-ECDH(7)>
  192. =head1 COPYRIGHT
  193. Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
  194. Licensed under the Apache License 2.0 (the "License"). You may not use
  195. this file except in compliance with the License. You can obtain a copy
  196. in the file LICENSE in the source distribution or at
  197. L<https://www.openssl.org/source/license.html>.
  198. =cut