EVP_PKEY_sign.pod 3.4 KB

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