EVP_PKEY-EC.pod 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 compability 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 "use-cofactor-flag" (B<OSSL_PKEY_PARAM_USE_COFACTOR_ECDH>) <integer>
  48. Enable Cofactor DH (ECC CDH) if this value is 1, otherwise it uses normal EC DH
  49. if the value is zero. The cofactor variant multiplies the shared secret by the
  50. EC curve's cofactor (note for some curves the cofactor is 1).
  51. =item "encoding" (B<OSSL_PKEY_PARAM_EC_ENCODING>) <utf8 string>
  52. Set the format used for serializing the EC group parameters.
  53. Valid values are "explicit" or "named_curve". The default value is "named_curve".
  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 "pub" (B<OSSL_PKEY_PARAM_PUB_KEY>) <octet string>
  58. The public key value in EC point format.
  59. =item "priv" (B<OSSL_PKEY_PARAM_PRIV_KEY>) <unsigned integer>
  60. The private key value.
  61. =item "encoded-pub-key" (B<OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY>) <octet string>
  62. Used for getting and setting the encoding of an EC public key. The public key
  63. is expected to be a point conforming to Sec. 2.3.4 of the SECG SEC 1 ("Elliptic
  64. Curve Cryptography") standard.
  65. =back
  66. The following Gettable types are also available for the built-in EC algorithm:
  67. =over 4
  68. =item "basis-type" (B<OSSL_PKEY_PARAM_EC_CHAR2_TYPE>) <utf8 string>
  69. Supports the values "tpBasis" for a trinomial or "ppBasis" for a pentanomial.
  70. This field is only used for a binary field F2^m.
  71. =item "m" (B<OSSL_PKEY_PARAM_EC_CHAR2_M>) <integer>
  72. =item "tp" (B<OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS>) <integer>
  73. =item "k1" (B<OSSL_PKEY_PARAM_EC_CHAR2_PP_K1>) <integer>
  74. =item "k2" (B<OSSL_PKEY_PARAM_EC_CHAR2_PP_K2>) <integer>
  75. =item "k3" (B<OSSL_PKEY_PARAM_EC_CHAR2_PP_K3>) <integer>
  76. These fields are only used for a binary field F2^m.
  77. I<m> is the degree of the binary field.
  78. I<tp> is the middle bit of a trinomial so its value must be in the
  79. range m > tp > 0.
  80. I<k1>, I<k2> and I<k3> are used to get the middle bits of a pentanomial such
  81. that m > k3 > k2 > k1 > 0
  82. =back
  83. =head1 EXAMPLES
  84. An B<EVP_PKEY> context can be obtained by calling:
  85. EVP_PKEY_CTX *pctx =
  86. EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
  87. An B<EVP_PKEY> ECDSA or ECDH key can be generated with a "P-256" named group by
  88. calling:
  89. EVP_PKEY *key = NULL;
  90. OSSL_PARAM params[2];
  91. EVP_PKEY_CTX *gctx =
  92. EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
  93. EVP_PKEY_keygen_init(gctx);
  94. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  95. "P-256", 0);
  96. params[1] = OSSL_PARAM_construct_end();
  97. EVP_PKEY_CTX_set_params(gctx, params);
  98. EVP_PKEY_gen(gctx, &key);
  99. EVP_PKEY_print_private(bio_out, key, 0, NULL);
  100. ...
  101. EVP_PKEY_free(key);
  102. EVP_PKEY_CTX_free(gctx);
  103. An B<EVP_PKEY> EC CDH (Cofactor Diffie-Hellman) key can be generated with a
  104. "K-571" named group by calling:
  105. int use_cdh = 1;
  106. EVP_PKEY *key = NULL;
  107. OSSL_PARAM params[3];
  108. EVP_PKEY_CTX *gctx =
  109. EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
  110. EVP_PKEY_keygen_init(gctx);
  111. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  112. "K-571", 0);
  113. /*
  114. * This curve has a cofactor that is not 1 - so setting CDH mode changes
  115. * the behaviour. For many curves the cofactor is 1 - so setting this has
  116. * no effect.
  117. */
  118. params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
  119. &use_cdh);
  120. params[2] = OSSL_PARAM_construct_end();
  121. EVP_PKEY_CTX_set_params(gctx, params);
  122. EVP_PKEY_gen(gctx, &key);
  123. EVP_PKEY_print_private(bio_out, key, 0, NULL);
  124. ...
  125. EVP_PKEY_free(key);
  126. EVP_PKEY_CTX_free(gctx);
  127. =head1 SEE ALSO
  128. L<EVP_KEYMGMT(3)>,
  129. L<EVP_PKEY(3)>,
  130. L<provider-keymgmt(7)>,
  131. L<EVP_SIGNATURE-ECDSA(7)>,
  132. L<EVP_KEYEXCH-ECDH(7)>
  133. =head1 COPYRIGHT
  134. Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
  135. Licensed under the Apache License 2.0 (the "License"). You may not use
  136. this file except in compliance with the License. You can obtain a copy
  137. in the file LICENSE in the source distribution or at
  138. L<https://www.openssl.org/source/license.html>.
  139. =cut