EVP_PKEY_sign.pod 3.5 KB

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