1238384Sjkim=pod
2238384Sjkim
3238384Sjkim=head1 NAME
4238384Sjkim
5238384SjkimEVP_PKEY_sign_init, EVP_PKEY_sign - sign using a public key algorithm
6238384Sjkim
7238384Sjkim=head1 SYNOPSIS
8238384Sjkim
9238384Sjkim #include <openssl/evp.h>
10238384Sjkim
11238384Sjkim int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx);
12238384Sjkim int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
13238384Sjkim			unsigned char *sig, size_t *siglen,
14238384Sjkim			const unsigned char *tbs, size_t tbslen);
15238384Sjkim
16238384Sjkim=head1 DESCRIPTION
17238384Sjkim
18238384SjkimThe EVP_PKEY_sign_init() function initializes a public key algorithm
19238384Sjkimcontext using key B<pkey> for a signing operation.
20238384Sjkim
21238384SjkimThe EVP_PKEY_sign() function performs a public key signing operation
22238384Sjkimusing B<ctx>. The data to be signed is specified using the B<tbs> and
23238384SjkimB<tbslen> parameters. If B<sig> is B<NULL> then the maximum size of the output
24238384Sjkimbuffer is written to the B<siglen> parameter. If B<sig> is not B<NULL> then
25238384Sjkimbefore the call the B<siglen> parameter should contain the length of the
26238384SjkimB<sig> buffer, if the call is successful the signature is written to
27238384SjkimB<sig> and the amount of data written to B<siglen>.
28238384Sjkim
29238384Sjkim=head1 NOTES
30238384Sjkim
31273149SjkimEVP_PKEY_sign() does not hash the data to be signed, and therefore is
32273149Sjkimnormally used to sign digests. For signing arbitrary messages, see the
33273149SjkimL<EVP_DigestSignInit(3)|EVP_DigestSignInit(3)> and
34273149SjkimL<EVP_SignInit(3)|EVP_SignInit(3)> signing interfaces instead.
35273149Sjkim
36238384SjkimAfter the call to EVP_PKEY_sign_init() algorithm specific control
37238384Sjkimoperations can be performed to set any appropriate parameters for the
38273149Sjkimoperation (see L<EVP_PKEY_CTX_ctrl(3)|EVP_PKEY_CTX_ctrl(3)>).
39238384Sjkim
40238384SjkimThe function EVP_PKEY_sign() can be called more than once on the same
41238384Sjkimcontext if several operations are performed using the same parameters.
42238384Sjkim
43238384Sjkim=head1 RETURN VALUES
44238384Sjkim
45238384SjkimEVP_PKEY_sign_init() and EVP_PKEY_sign() return 1 for success and 0
46238384Sjkimor a negative value for failure. In particular a return value of -2
47238384Sjkimindicates the operation is not supported by the public key algorithm.
48238384Sjkim
49238384Sjkim=head1 EXAMPLE
50238384Sjkim
51238384SjkimSign data using RSA with PKCS#1 padding and SHA256 digest:
52238384Sjkim
53238384Sjkim #include <openssl/evp.h>
54238384Sjkim #include <openssl/rsa.h>
55238384Sjkim
56238384Sjkim EVP_PKEY_CTX *ctx;
57273149Sjkim /* md is a SHA-256 digest in this example. */
58238384Sjkim unsigned char *md, *sig;
59273149Sjkim size_t mdlen = 32, siglen;
60238384Sjkim EVP_PKEY *signing_key;
61273149Sjkim
62273149Sjkim /*
63273149Sjkim  * NB: assumes signing_key and md are set up before the next
64273149Sjkim  * step. signing_key must be an RSA private key and md must
65273149Sjkim  * point to the SHA-256 digest to be signed.
66238384Sjkim  */
67273149Sjkim ctx = EVP_PKEY_CTX_new(signing_key, NULL /* no engine */);
68238384Sjkim if (!ctx)
69238384Sjkim	/* Error occurred */
70238384Sjkim if (EVP_PKEY_sign_init(ctx) <= 0)
71238384Sjkim	/* Error */
72238384Sjkim if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) <= 0)
73238384Sjkim	/* Error */
74238384Sjkim if (EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()) <= 0)
75238384Sjkim	/* Error */
76238384Sjkim
77238384Sjkim /* Determine buffer length */
78238384Sjkim if (EVP_PKEY_sign(ctx, NULL, &siglen, md, mdlen) <= 0)
79238384Sjkim	/* Error */
80238384Sjkim
81238384Sjkim sig = OPENSSL_malloc(siglen);
82238384Sjkim
83238384Sjkim if (!sig)
84238384Sjkim	/* malloc failure */
85238384Sjkim 
86238384Sjkim if (EVP_PKEY_sign(ctx, sig, &siglen, md, mdlen) <= 0)
87238384Sjkim	/* Error */
88238384Sjkim
89238384Sjkim /* Signature is siglen bytes written to buffer sig */
90238384Sjkim
91238384Sjkim
92238384Sjkim=head1 SEE ALSO
93238384Sjkim
94238384SjkimL<EVP_PKEY_CTX_new(3)|EVP_PKEY_CTX_new(3)>,
95273149SjkimL<EVP_PKEY_CTX_ctrl(3)|EVP_PKEY_CTX_ctrl(3)>,
96238384SjkimL<EVP_PKEY_encrypt(3)|EVP_PKEY_encrypt(3)>,
97238384SjkimL<EVP_PKEY_decrypt(3)|EVP_PKEY_decrypt(3)>,
98238384SjkimL<EVP_PKEY_verify(3)|EVP_PKEY_verify(3)>,
99246772SjkimL<EVP_PKEY_verify_recover(3)|EVP_PKEY_verify_recover(3)>,
100238384SjkimL<EVP_PKEY_derive(3)|EVP_PKEY_derive(3)> 
101238384Sjkim
102238384Sjkim=head1 HISTORY
103238384Sjkim
104238384SjkimThese functions were first added to OpenSSL 1.0.0.
105238384Sjkim
106238384Sjkim=cut
107