Modify providers that keep track of underlying algorithms
[openssl.git] / doc / man3 / EVP_PKEY_sign.pod
1 =pod
2
3 =head1 NAME
4
5 EVP_PKEY_sign_init_ex, EVP_PKEY_sign_init, EVP_PKEY_sign
6 - sign using a public key algorithm
7
8 =head1 SYNOPSIS
9
10  #include <openssl/evp.h>
11
12  int EVP_PKEY_sign_init_ex(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature);
13  int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx);
14  int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
15                    unsigned char *sig, size_t *siglen,
16                    const unsigned char *tbs, size_t tbslen);
17
18 =head1 DESCRIPTION
19
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.
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
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
45 L<EVP_DigestSignInit(3)> and
46 L<EVP_SignInit(3)> signing interfaces instead.
47
48 After the call to EVP_PKEY_sign_init() algorithm specific control
49 operations can be performed to set any appropriate parameters for the
50 operation (see L<EVP_PKEY_CTX_ctrl(3)>).
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
61 =head1 EXAMPLES
62
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;
69  /* md is a SHA-256 digest in this example. */
70  unsigned char *md, *sig;
71  size_t mdlen = 32, siglen;
72  EVP_PKEY *signing_key;
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.
78   */
79  ctx = EVP_PKEY_CTX_new(signing_key, NULL /* no engine */);
80  if (!ctx)
81      /* Error occurred */
82  if (EVP_PKEY_sign_init(ctx) <= 0)
83      /* Error */
84  if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) <= 0)
85      /* Error */
86  if (EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()) <= 0)
87      /* Error */
88
89  /* Determine buffer length */
90  if (EVP_PKEY_sign(ctx, NULL, &siglen, md, mdlen) <= 0)
91      /* Error */
92
93  sig = OPENSSL_malloc(siglen);
94
95  if (!sig)
96      /* malloc failure */
97
98  if (EVP_PKEY_sign(ctx, sig, &siglen, md, mdlen) <= 0)
99      /* Error */
100
101  /* Signature is siglen bytes written to buffer sig */
102
103
104 =head1 SEE ALSO
105
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)>,
112 L<EVP_PKEY_derive(3)>
113
114 =head1 HISTORY
115
116 EVP_PKEY_sign_init_ex() was added in OpenSSL 3.0.
117 These functions were added in OpenSSL 1.0.0.
118
119 =head1 COPYRIGHT
120
121 Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
122
123 Licensed under the Apache License 2.0 (the "License").  You may not use
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