EVP_PKEY_sign.pod 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. =pod
  2. =head1 NAME
  3. EVP_PKEY_sign_init, EVP_PKEY_sign_init_ex, EVP_PKEY_sign
  4. - sign using a public key algorithm
  5. =head1 SYNOPSIS
  6. #include <openssl/evp.h>
  7. int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx);
  8. int EVP_PKEY_sign_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]);
  9. int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
  10. unsigned char *sig, size_t *siglen,
  11. const unsigned char *tbs, size_t tbslen);
  12. =head1 DESCRIPTION
  13. EVP_PKEY_sign_init() initializes a public key algorithm context I<ctx> for
  14. signing using the algorithm given when the context was created
  15. using L<EVP_PKEY_CTX_new(3)> or variants thereof. The algorithm is used to
  16. fetch a B<EVP_SIGNATURE> method implicitly, see L<provider(7)/Implicit fetch>
  17. for more information about implicit fetches.
  18. EVP_PKEY_sign_init_ex() is the same as EVP_PKEY_sign_init() but additionally
  19. sets the passed parameters I<params> on the context before returning.
  20. The EVP_PKEY_sign() function performs a public key signing operation
  21. using I<ctx>. The data to be signed is specified using the I<tbs> and
  22. I<tbslen> parameters. If I<sig> is NULL then the maximum size of the output
  23. buffer is written to the I<siglen> parameter. If I<sig> is not NULL then
  24. before the call the I<siglen> parameter should contain the length of the
  25. I<sig> buffer, if the call is successful the signature is written to
  26. I<sig> and the amount of data written to I<siglen>.
  27. =head1 NOTES
  28. EVP_PKEY_sign() does not hash the data to be signed, and therefore is
  29. normally used to sign digests. For signing arbitrary messages, see the
  30. L<EVP_DigestSignInit(3)> and
  31. L<EVP_SignInit(3)> signing interfaces instead.
  32. After the call to EVP_PKEY_sign_init() algorithm specific control
  33. operations can be performed to set any appropriate parameters for the
  34. operation (see L<EVP_PKEY_CTX_ctrl(3)>).
  35. The function EVP_PKEY_sign() can be called more than once on the same
  36. context if several operations are performed using the same parameters.
  37. =head1 RETURN VALUES
  38. EVP_PKEY_sign_init() and EVP_PKEY_sign() return 1 for success and 0
  39. or a negative value for failure. In particular a return value of -2
  40. indicates the operation is not supported by the public key algorithm.
  41. =head1 EXAMPLES
  42. Sign data using RSA with PKCS#1 padding and SHA256 digest:
  43. #include <openssl/evp.h>
  44. #include <openssl/rsa.h>
  45. EVP_PKEY_CTX *ctx;
  46. /* md is a SHA-256 digest in this example. */
  47. unsigned char *md, *sig;
  48. size_t mdlen = 32, siglen;
  49. EVP_PKEY *signing_key;
  50. /*
  51. * NB: assumes signing_key and md are set up before the next
  52. * step. signing_key must be an RSA private key and md must
  53. * point to the SHA-256 digest to be signed.
  54. */
  55. ctx = EVP_PKEY_CTX_new(signing_key, NULL /* no engine */);
  56. if (!ctx)
  57. /* Error occurred */
  58. if (EVP_PKEY_sign_init(ctx) <= 0)
  59. /* Error */
  60. if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) <= 0)
  61. /* Error */
  62. if (EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()) <= 0)
  63. /* Error */
  64. /* Determine buffer length */
  65. if (EVP_PKEY_sign(ctx, NULL, &siglen, md, mdlen) <= 0)
  66. /* Error */
  67. sig = OPENSSL_malloc(siglen);
  68. if (!sig)
  69. /* malloc failure */
  70. if (EVP_PKEY_sign(ctx, sig, &siglen, md, mdlen) <= 0)
  71. /* Error */
  72. /* Signature is siglen bytes written to buffer sig */
  73. =head1 SEE ALSO
  74. L<EVP_PKEY_CTX_new(3)>,
  75. L<EVP_PKEY_CTX_ctrl(3)>,
  76. L<EVP_PKEY_encrypt(3)>,
  77. L<EVP_PKEY_decrypt(3)>,
  78. L<EVP_PKEY_verify(3)>,
  79. L<EVP_PKEY_verify_recover(3)>,
  80. L<EVP_PKEY_derive(3)>
  81. =head1 HISTORY
  82. The EVP_PKEY_sign_init() and EVP_PKEY_sign() functions were added in
  83. OpenSSL 1.0.0.
  84. The EVP_PKEY_sign_init_ex() function was added in OpenSSL 3.0.
  85. =head1 COPYRIGHT
  86. Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
  87. Licensed under the Apache License 2.0 (the "License"). You may not use
  88. this file except in compliance with the License. You can obtain a copy
  89. in the file LICENSE in the source distribution or at
  90. L<https://www.openssl.org/source/license.html>.
  91. =cut