xref: /freebsd/crypto/openssl/doc/man3/EVP_PKEY_sign.pod (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1e71b7053SJung-uk Kim=pod
2e71b7053SJung-uk Kim
3e71b7053SJung-uk Kim=head1 NAME
4e71b7053SJung-uk Kim
5*b077aed3SPierre ProncheryEVP_PKEY_sign_init, EVP_PKEY_sign_init_ex, EVP_PKEY_sign
6*b077aed3SPierre Pronchery- sign using a public key algorithm
7e71b7053SJung-uk Kim
8e71b7053SJung-uk Kim=head1 SYNOPSIS
9e71b7053SJung-uk Kim
10e71b7053SJung-uk Kim #include <openssl/evp.h>
11e71b7053SJung-uk Kim
12e71b7053SJung-uk Kim int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx);
13*b077aed3SPierre Pronchery int EVP_PKEY_sign_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]);
14e71b7053SJung-uk Kim int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
15e71b7053SJung-uk Kim                   unsigned char *sig, size_t *siglen,
16e71b7053SJung-uk Kim                   const unsigned char *tbs, size_t tbslen);
17e71b7053SJung-uk Kim
18e71b7053SJung-uk Kim=head1 DESCRIPTION
19e71b7053SJung-uk Kim
20*b077aed3SPierre ProncheryEVP_PKEY_sign_init() initializes a public key algorithm context I<ctx> for
21*b077aed3SPierre Proncherysigning using the algorithm given when the context was created
22*b077aed3SPierre Proncheryusing L<EVP_PKEY_CTX_new(3)> or variants thereof.  The algorithm is used to
23*b077aed3SPierre Proncheryfetch a B<EVP_SIGNATURE> method implicitly, see L<provider(7)/Implicit fetch>
24*b077aed3SPierre Proncheryfor more information about implicit fetches.
25*b077aed3SPierre Pronchery
26*b077aed3SPierre ProncheryEVP_PKEY_sign_init_ex() is the same as EVP_PKEY_sign_init() but additionally
27*b077aed3SPierre Proncherysets the passed parameters I<params> on the context before returning.
28e71b7053SJung-uk Kim
29e71b7053SJung-uk KimThe EVP_PKEY_sign() function performs a public key signing operation
30*b077aed3SPierre Proncheryusing I<ctx>. The data to be signed is specified using the I<tbs> and
31*b077aed3SPierre ProncheryI<tbslen> parameters. If I<sig> is NULL then the maximum size of the output
32*b077aed3SPierre Proncherybuffer is written to the I<siglen> parameter. If I<sig> is not NULL then
33*b077aed3SPierre Proncherybefore the call the I<siglen> parameter should contain the length of the
34*b077aed3SPierre ProncheryI<sig> buffer, if the call is successful the signature is written to
35*b077aed3SPierre ProncheryI<sig> and the amount of data written to I<siglen>.
36e71b7053SJung-uk Kim
37e71b7053SJung-uk Kim=head1 NOTES
38e71b7053SJung-uk Kim
39e71b7053SJung-uk KimEVP_PKEY_sign() does not hash the data to be signed, and therefore is
40e71b7053SJung-uk Kimnormally used to sign digests. For signing arbitrary messages, see the
41e71b7053SJung-uk KimL<EVP_DigestSignInit(3)> and
42e71b7053SJung-uk KimL<EVP_SignInit(3)> signing interfaces instead.
43e71b7053SJung-uk Kim
44e71b7053SJung-uk KimAfter the call to EVP_PKEY_sign_init() algorithm specific control
45e71b7053SJung-uk Kimoperations can be performed to set any appropriate parameters for the
46e71b7053SJung-uk Kimoperation (see L<EVP_PKEY_CTX_ctrl(3)>).
47e71b7053SJung-uk Kim
48e71b7053SJung-uk KimThe function EVP_PKEY_sign() can be called more than once on the same
49e71b7053SJung-uk Kimcontext if several operations are performed using the same parameters.
50e71b7053SJung-uk Kim
51e71b7053SJung-uk Kim=head1 RETURN VALUES
52e71b7053SJung-uk Kim
53e71b7053SJung-uk KimEVP_PKEY_sign_init() and EVP_PKEY_sign() return 1 for success and 0
54e71b7053SJung-uk Kimor a negative value for failure. In particular a return value of -2
55e71b7053SJung-uk Kimindicates the operation is not supported by the public key algorithm.
56e71b7053SJung-uk Kim
57da327cd2SJung-uk Kim=head1 EXAMPLES
58e71b7053SJung-uk Kim
59e71b7053SJung-uk KimSign data using RSA with PKCS#1 padding and SHA256 digest:
60e71b7053SJung-uk Kim
61e71b7053SJung-uk Kim #include <openssl/evp.h>
62e71b7053SJung-uk Kim #include <openssl/rsa.h>
63e71b7053SJung-uk Kim
64e71b7053SJung-uk Kim EVP_PKEY_CTX *ctx;
65e71b7053SJung-uk Kim /* md is a SHA-256 digest in this example. */
66e71b7053SJung-uk Kim unsigned char *md, *sig;
67e71b7053SJung-uk Kim size_t mdlen = 32, siglen;
68e71b7053SJung-uk Kim EVP_PKEY *signing_key;
69e71b7053SJung-uk Kim
70e71b7053SJung-uk Kim /*
71e71b7053SJung-uk Kim  * NB: assumes signing_key and md are set up before the next
72e71b7053SJung-uk Kim  * step. signing_key must be an RSA private key and md must
73e71b7053SJung-uk Kim  * point to the SHA-256 digest to be signed.
74e71b7053SJung-uk Kim  */
75e71b7053SJung-uk Kim ctx = EVP_PKEY_CTX_new(signing_key, NULL /* no engine */);
76e71b7053SJung-uk Kim if (!ctx)
77e71b7053SJung-uk Kim     /* Error occurred */
78e71b7053SJung-uk Kim if (EVP_PKEY_sign_init(ctx) <= 0)
79e71b7053SJung-uk Kim     /* Error */
80e71b7053SJung-uk Kim if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) <= 0)
81e71b7053SJung-uk Kim     /* Error */
82e71b7053SJung-uk Kim if (EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()) <= 0)
83e71b7053SJung-uk Kim     /* Error */
84e71b7053SJung-uk Kim
85e71b7053SJung-uk Kim /* Determine buffer length */
86e71b7053SJung-uk Kim if (EVP_PKEY_sign(ctx, NULL, &siglen, md, mdlen) <= 0)
87e71b7053SJung-uk Kim     /* Error */
88e71b7053SJung-uk Kim
89e71b7053SJung-uk Kim sig = OPENSSL_malloc(siglen);
90e71b7053SJung-uk Kim
91e71b7053SJung-uk Kim if (!sig)
92e71b7053SJung-uk Kim     /* malloc failure */
93e71b7053SJung-uk Kim
94e71b7053SJung-uk Kim if (EVP_PKEY_sign(ctx, sig, &siglen, md, mdlen) <= 0)
95e71b7053SJung-uk Kim     /* Error */
96e71b7053SJung-uk Kim
97e71b7053SJung-uk Kim /* Signature is siglen bytes written to buffer sig */
98e71b7053SJung-uk Kim
99e71b7053SJung-uk Kim
100e71b7053SJung-uk Kim=head1 SEE ALSO
101e71b7053SJung-uk Kim
102e71b7053SJung-uk KimL<EVP_PKEY_CTX_new(3)>,
103e71b7053SJung-uk KimL<EVP_PKEY_CTX_ctrl(3)>,
104e71b7053SJung-uk KimL<EVP_PKEY_encrypt(3)>,
105e71b7053SJung-uk KimL<EVP_PKEY_decrypt(3)>,
106e71b7053SJung-uk KimL<EVP_PKEY_verify(3)>,
107e71b7053SJung-uk KimL<EVP_PKEY_verify_recover(3)>,
108e71b7053SJung-uk KimL<EVP_PKEY_derive(3)>
109e71b7053SJung-uk Kim
110e71b7053SJung-uk Kim=head1 HISTORY
111e71b7053SJung-uk Kim
112*b077aed3SPierre ProncheryThe EVP_PKEY_sign_init() and EVP_PKEY_sign() functions were added in
113*b077aed3SPierre ProncheryOpenSSL 1.0.0.
114*b077aed3SPierre Pronchery
115*b077aed3SPierre ProncheryThe EVP_PKEY_sign_init_ex() function was added in OpenSSL 3.0.
116e71b7053SJung-uk Kim
117e71b7053SJung-uk Kim=head1 COPYRIGHT
118e71b7053SJung-uk Kim
119*b077aed3SPierre ProncheryCopyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
120e71b7053SJung-uk Kim
121*b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License").  You may not use
122e71b7053SJung-uk Kimthis file except in compliance with the License.  You can obtain a copy
123e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at
124e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>.
125e71b7053SJung-uk Kim
126e71b7053SJung-uk Kim=cut
127