]>
Commit | Line | Data |
---|---|---|
ba544377 DSH |
1 | =pod |
2 | ||
3 | =head1 NAME | |
4 | ||
11031468 MC |
5 | EVP_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 |
20 | The EVP_PKEY_sign_init_ex() function initializes a public key algorithm |
21 | context for performing signing using the signature algorithm B<signature>. | |
22 | The signature algorithm B<signature> should be fetched using a call to | |
23 | L<EVP_SIGNATURE_fetch(3)>. | |
24 | The EVP_PKEY object associated with B<ctx> must be compatible with that | |
25 | algorithm. | |
26 | B<signature> may be NULL in which case the EVP_SIGNATURE algorithm is fetched | |
27 | implicitly based on the type of EVP_PKEY associated with B<ctx>. | |
28 | See L<provider(7)/Implicit fetch> for more information about implict fetches. | |
29 | ||
30 | The EVP_PKEY_sign_init() function is the same as EVP_PKEY_sign_init_ex() except | |
31 | that the EVP_SIGNATURE algorithm is always implicitly fetched. | |
ba544377 DSH |
32 | |
33 | The EVP_PKEY_sign() function performs a public key signing operation | |
34 | using B<ctx>. The data to be signed is specified using the B<tbs> and | |
35 | B<tbslen> parameters. If B<sig> is B<NULL> then the maximum size of the output | |
36 | buffer is written to the B<siglen> parameter. If B<sig> is not B<NULL> then | |
37 | before the call the B<siglen> parameter should contain the length of the | |
38 | B<sig> buffer, if the call is successful the signature is written to | |
39 | B<sig> and the amount of data written to B<siglen>. | |
40 | ||
41 | =head1 NOTES | |
42 | ||
d64c533a EK |
43 | EVP_PKEY_sign() does not hash the data to be signed, and therefore is |
44 | normally used to sign digests. For signing arbitrary messages, see the | |
9b86974e RS |
45 | L<EVP_DigestSignInit(3)> and |
46 | L<EVP_SignInit(3)> signing interfaces instead. | |
d64c533a | 47 | |
ba544377 DSH |
48 | After the call to EVP_PKEY_sign_init() algorithm specific control |
49 | operations can be performed to set any appropriate parameters for the | |
9b86974e | 50 | operation (see L<EVP_PKEY_CTX_ctrl(3)>). |
ba544377 DSH |
51 | |
52 | The function EVP_PKEY_sign() can be called more than once on the same | |
53 | context if several operations are performed using the same parameters. | |
54 | ||
55 | =head1 RETURN VALUES | |
56 | ||
57 | EVP_PKEY_sign_init() and EVP_PKEY_sign() return 1 for success and 0 | |
58 | or a negative value for failure. In particular a return value of -2 | |
59 | indicates the operation is not supported by the public key algorithm. | |
60 | ||
cda77422 | 61 | =head1 EXAMPLES |
ba544377 | 62 | |
43636910 DSH |
63 | Sign 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 |
106 | L<EVP_PKEY_CTX_new(3)>, |
107 | L<EVP_PKEY_CTX_ctrl(3)>, | |
108 | L<EVP_PKEY_encrypt(3)>, | |
109 | L<EVP_PKEY_decrypt(3)>, | |
110 | L<EVP_PKEY_verify(3)>, | |
111 | L<EVP_PKEY_verify_recover(3)>, | |
1bc74519 | 112 | L<EVP_PKEY_derive(3)> |
ba544377 DSH |
113 | |
114 | =head1 HISTORY | |
115 | ||
11031468 | 116 | EVP_PKEY_sign_init_ex() was added in OpenSSL 3.0. |
fc5ecadd | 117 | These functions were added in OpenSSL 1.0.0. |
ba544377 | 118 | |
e2f92610 RS |
119 | =head1 COPYRIGHT |
120 | ||
121 | Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved. | |
122 | ||
4746f25a | 123 | Licensed under the Apache License 2.0 (the "License"). You may not use |
e2f92610 RS |
124 | this file except in compliance with the License. You can obtain a copy |
125 | in the file LICENSE in the source distribution or at | |
126 | L<https://www.openssl.org/source/license.html>. | |
127 | ||
128 | =cut |