EVP_DigestSignInit.pod 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. =pod
  2. =head1 NAME
  3. EVP_DigestSignInit_ex, EVP_DigestSignInit, EVP_DigestSignUpdate,
  4. EVP_DigestSignFinal, EVP_DigestSign - EVP signing functions
  5. =head1 SYNOPSIS
  6. #include <openssl/evp.h>
  7. int EVP_DigestSignInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
  8. const char *mdname, const char *props,
  9. EVP_PKEY *pkey, OPENSSL_CTX *libctx);
  10. int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
  11. const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey);
  12. int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
  13. int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen);
  14. int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret,
  15. size_t *siglen, const unsigned char *tbs,
  16. size_t tbslen);
  17. =head1 DESCRIPTION
  18. The EVP signature routines are a high-level interface to digital signatures.
  19. Input data is digested first before the signing takes place.
  20. EVP_DigestSignInit_ex() sets up signing context I<ctx> to use a digest with the
  21. name I<mdname> and private key I<pkey>. The name of the digest to be used is
  22. passed to the provider of the signature algorithm in use. How that provider
  23. interprets the digest name is provider specific. The provider may implement
  24. that digest directly itself or it may (optionally) choose to fetch it (which
  25. could result in a digest from a different provider being selected). If the
  26. provider supports fetching the digest then it may use the I<props> argument for
  27. the properties to be used during the fetch.
  28. The I<pkey> algorithm is used to fetch a B<EVP_SIGNATURE> method implicitly, to
  29. be used for the actual signing. See L<provider(7)/Implicit fetch> for
  30. more information about implicit fetches.
  31. The OpenSSL default and legacy providers support fetching digests and can fetch
  32. those digests from any available provider. The OpenSSL fips provider also
  33. supports fetching digests but will only fetch digests that are themselves
  34. implemented inside the fips provider.
  35. I<ctx> must be created with EVP_MD_CTX_new() before calling this function. If
  36. I<pctx> is not NULL, the EVP_PKEY_CTX of the signing operation will be written
  37. to I<*pctx>: this can be used to set alternative signing options. Note that any
  38. existing value in I<*pctx> is overwritten. The EVP_PKEY_CTX value returned must
  39. not be freed directly by the application if I<ctx> is not assigned an
  40. EVP_PKEY_CTX value before being passed to EVP_DigestSignInit_ex() (which means
  41. the EVP_PKEY_CTX is created inside EVP_DigestSignInit_ex() and it will be freed
  42. automatically when the EVP_MD_CTX is freed). If the EVP_PKEY_CTX to be used is
  43. created by EVP_DigestSignInit_ex then it will use the B<OPENSSL_CTX> specified
  44. in I<libctx> and the property query string specified in I<props>.
  45. The digest I<mdname> may be NULL if the signing algorithm supports it. The
  46. I<props> argument can always be NULL.
  47. No B<EVP_PKEY_CTX> will be created by EVP_DigestSignInit_ex() if the passed
  48. I<ctx> has already been assigned one via L<EVP_MD_CTX_set_pkey_ctx(3)>. See also
  49. L<SM2(7)>.
  50. Only EVP_PKEY types that support signing can be used with these functions. This
  51. includes MAC algorithms where the MAC generation is considered as a form of
  52. "signing". Built-in EVP_PKEY types supported by these functions are CMAC,
  53. Poly1305, DSA, ECDSA, HMAC, RSA, SipHash, Ed25519 and Ed448.
  54. Not all digests can be used for all key types. The following combinations apply.
  55. =over 4
  56. =item DSA
  57. Supports SHA1, SHA224, SHA256, SHA384 and SHA512
  58. =item ECDSA
  59. Supports SHA1, SHA224, SHA256, SHA384, SHA512 and SM3
  60. =item RSA with no padding
  61. Supports no digests (the digest I<type> must be NULL)
  62. =item RSA with X931 padding
  63. Supports SHA1, SHA256, SHA384 and SHA512
  64. =item All other RSA padding types
  65. Support SHA1, SHA224, SHA256, SHA384, SHA512, MD5, MD5_SHA1, MD2, MD4, MDC2,
  66. SHA3-224, SHA3-256, SHA3-384, SHA3-512
  67. =item Ed25519 and Ed448
  68. Support no digests (the digest I<type> must be NULL)
  69. =item HMAC
  70. Supports any digest
  71. =item CMAC, Poly1305 and SipHash
  72. Will ignore any digest provided.
  73. =back
  74. If RSA-PSS is used and restrictions apply then the digest must match.
  75. EVP_DigestSignInit() works in the same way as EVP_DigestSignInit_ex() except
  76. that the I<mdname> parameter will be inferred from the supplied digest I<type>,
  77. and I<props> will be NULL. Where supplied the ENGINE I<e> will be used for the
  78. signing and digest algorithm implementations. I<e> may be NULL.
  79. EVP_DigestSignUpdate() hashes I<cnt> bytes of data at I<d> into the
  80. signature context I<ctx>. This function can be called several times on the
  81. same I<ctx> to include additional data.
  82. EVP_DigestSignFinal() signs the data in I<ctx> and places the signature in I<sig>.
  83. If I<sig> is NULL then the maximum size of the output buffer is written to
  84. the I<siglen> parameter. If I<sig> is not NULL then before the call the
  85. I<siglen> parameter should contain the length of the I<sig> buffer. If the
  86. call is successful the signature is written to I<sig> and the amount of data
  87. written to I<siglen>.
  88. EVP_DigestSign() signs I<tbslen> bytes of data at I<tbs> and places the
  89. signature in I<sig> and its length in I<siglen> in a similar way to
  90. EVP_DigestSignFinal().
  91. =head1 RETURN VALUES
  92. EVP_DigestSignInit(), EVP_DigestSignUpdate(), EVP_DigestSignFinal() and
  93. EVP_DigestSign() return 1 for success and 0 for failure.
  94. The error codes can be obtained from L<ERR_get_error(3)>.
  95. =head1 NOTES
  96. The B<EVP> interface to digital signatures should almost always be used in
  97. preference to the low-level interfaces. This is because the code then becomes
  98. transparent to the algorithm used and much more flexible.
  99. EVP_DigestSign() is a one shot operation which signs a single block of data
  100. in one function. For algorithms that support streaming it is equivalent to
  101. calling EVP_DigestSignUpdate() and EVP_DigestSignFinal(). For algorithms which
  102. do not support streaming (e.g. PureEdDSA) it is the only way to sign data.
  103. In previous versions of OpenSSL there was a link between message digest types
  104. and public key algorithms. This meant that "clone" digests such as EVP_dss1()
  105. needed to be used to sign using SHA1 and DSA. This is no longer necessary and
  106. the use of clone digest is now discouraged.
  107. For some key types and parameters the random number generator must be seeded.
  108. If the automatic seeding or reseeding of the OpenSSL CSPRNG fails due to
  109. external circumstances (see L<RAND(7)>), the operation will fail.
  110. The call to EVP_DigestSignFinal() internally finalizes a copy of the digest
  111. context. This means that calls to EVP_DigestSignUpdate() and
  112. EVP_DigestSignFinal() can be called later to digest and sign additional data.
  113. Since only a copy of the digest context is ever finalized, the context must
  114. be cleaned up after use by calling EVP_MD_CTX_free() or a memory leak
  115. will occur.
  116. The use of EVP_PKEY_size() with these functions is discouraged because some
  117. signature operations may have a signature length which depends on the
  118. parameters set. As a result EVP_PKEY_size() would have to return a value
  119. which indicates the maximum possible signature for any set of parameters.
  120. =head1 SEE ALSO
  121. L<EVP_DigestVerifyInit(3)>,
  122. L<EVP_DigestInit(3)>,
  123. L<evp(7)>, L<HMAC(3)>, L<MD2(3)>,
  124. L<MD5(3)>, L<MDC2(3)>, L<RIPEMD160(3)>,
  125. L<SHA1(3)>, L<openssl-dgst(1)>,
  126. L<RAND(7)>
  127. =head1 HISTORY
  128. EVP_DigestSignInit(), EVP_DigestSignUpdate() and EVP_DigestSignFinal()
  129. were added in OpenSSL 1.0.0.
  130. EVP_DigestSignInit_ex() was added in OpenSSL 3.0.
  131. EVP_DigestSignUpdate() was converted from a macro to a function in OpenSSL 3.0.
  132. =head1 COPYRIGHT
  133. Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
  134. Licensed under the Apache License 2.0 (the "License"). You may not use
  135. this file except in compliance with the License. You can obtain a copy
  136. in the file LICENSE in the source distribution or at
  137. L<https://www.openssl.org/source/license.html>.
  138. =cut