]> git.ipfire.org Git - thirdparty/openssl.git/blame - doc/man3/EVP_PKEY_sign.pod
EVP: Make the KEYEXCH implementation leaner
[thirdparty/openssl.git] / doc / man3 / EVP_PKEY_sign.pod
CommitLineData
ba544377
DSH
1=pod
2
3=head1 NAME
4
11031468
MC
5EVP_PKEY_sign_init_ex, EVP_PKEY_sign_init, EVP_PKEY_sign
6- sign using a public key algorithm
ba544377
DSH
7
8=head1 SYNOPSIS
9
10 #include <openssl/evp.h>
11
11031468 12 int EVP_PKEY_sign_init_ex(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature);
ba544377
DSH
13 int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx);
14 int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
e9b77246
BB
15 unsigned char *sig, size_t *siglen,
16 const unsigned char *tbs, size_t tbslen);
ba544377
DSH
17
18=head1 DESCRIPTION
19
11031468
MC
20The EVP_PKEY_sign_init_ex() function initializes a public key algorithm
21context for performing signing using the signature algorithm B<signature>.
22The signature algorithm B<signature> should be fetched using a call to
23L<EVP_SIGNATURE_fetch(3)>.
24The EVP_PKEY object associated with B<ctx> must be compatible with that
25algorithm.
26B<signature> may be NULL in which case the EVP_SIGNATURE algorithm is fetched
27implicitly based on the type of EVP_PKEY associated with B<ctx>.
28See L<provider(7)/Implicit fetch> for more information about implict fetches.
29
30The EVP_PKEY_sign_init() function is the same as EVP_PKEY_sign_init_ex() except
31that the EVP_SIGNATURE algorithm is always implicitly fetched.
ba544377
DSH
32
33The EVP_PKEY_sign() function performs a public key signing operation
34using B<ctx>. The data to be signed is specified using the B<tbs> and
35B<tbslen> parameters. If B<sig> is B<NULL> then the maximum size of the output
36buffer is written to the B<siglen> parameter. If B<sig> is not B<NULL> then
37before the call the B<siglen> parameter should contain the length of the
38B<sig> buffer, if the call is successful the signature is written to
39B<sig> and the amount of data written to B<siglen>.
40
41=head1 NOTES
42
d64c533a
EK
43EVP_PKEY_sign() does not hash the data to be signed, and therefore is
44normally used to sign digests. For signing arbitrary messages, see the
9b86974e
RS
45L<EVP_DigestSignInit(3)> and
46L<EVP_SignInit(3)> signing interfaces instead.
d64c533a 47
ba544377
DSH
48After the call to EVP_PKEY_sign_init() algorithm specific control
49operations can be performed to set any appropriate parameters for the
9b86974e 50operation (see L<EVP_PKEY_CTX_ctrl(3)>).
ba544377
DSH
51
52The function EVP_PKEY_sign() can be called more than once on the same
53context if several operations are performed using the same parameters.
54
55=head1 RETURN VALUES
56
57EVP_PKEY_sign_init() and EVP_PKEY_sign() return 1 for success and 0
58or a negative value for failure. In particular a return value of -2
59indicates the operation is not supported by the public key algorithm.
60
cda77422 61=head1 EXAMPLES
ba544377 62
43636910
DSH
63Sign data using RSA with PKCS#1 padding and SHA256 digest:
64
65 #include <openssl/evp.h>
66 #include <openssl/rsa.h>
67
68 EVP_PKEY_CTX *ctx;
d64c533a 69 /* md is a SHA-256 digest in this example. */
43636910 70 unsigned char *md, *sig;
d64c533a 71 size_t mdlen = 32, siglen;
43636910 72 EVP_PKEY *signing_key;
d64c533a
EK
73
74 /*
75 * NB: assumes signing_key and md are set up before the next
76 * step. signing_key must be an RSA private key and md must
77 * point to the SHA-256 digest to be signed.
43636910 78 */
d64c533a 79 ctx = EVP_PKEY_CTX_new(signing_key, NULL /* no engine */);
43636910 80 if (!ctx)
2947af32 81 /* Error occurred */
43636910 82 if (EVP_PKEY_sign_init(ctx) <= 0)
2947af32 83 /* Error */
43636910 84 if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) <= 0)
2947af32 85 /* Error */
43636910 86 if (EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()) <= 0)
2947af32 87 /* Error */
43636910
DSH
88
89 /* Determine buffer length */
90 if (EVP_PKEY_sign(ctx, NULL, &siglen, md, mdlen) <= 0)
2947af32 91 /* Error */
43636910
DSH
92
93 sig = OPENSSL_malloc(siglen);
94
95 if (!sig)
2947af32 96 /* malloc failure */
1bc74519 97
43636910 98 if (EVP_PKEY_sign(ctx, sig, &siglen, md, mdlen) <= 0)
2947af32 99 /* Error */
43636910
DSH
100
101 /* Signature is siglen bytes written to buffer sig */
ba544377 102
ba544377
DSH
103
104=head1 SEE ALSO
105
9b86974e
RS
106L<EVP_PKEY_CTX_new(3)>,
107L<EVP_PKEY_CTX_ctrl(3)>,
108L<EVP_PKEY_encrypt(3)>,
109L<EVP_PKEY_decrypt(3)>,
110L<EVP_PKEY_verify(3)>,
111L<EVP_PKEY_verify_recover(3)>,
1bc74519 112L<EVP_PKEY_derive(3)>
ba544377
DSH
113
114=head1 HISTORY
115
11031468 116EVP_PKEY_sign_init_ex() was added in OpenSSL 3.0.
fc5ecadd 117These functions were added in OpenSSL 1.0.0.
ba544377 118
e2f92610
RS
119=head1 COPYRIGHT
120
121Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
122
4746f25a 123Licensed under the Apache License 2.0 (the "License"). You may not use
e2f92610
RS
124this file except in compliance with the License. You can obtain a copy
125in the file LICENSE in the source distribution or at
126L<https://www.openssl.org/source/license.html>.
127
128=cut