Home | History | Annotate | Line # | Download | only in man3
      1  1.1  christos =pod
      2  1.1  christos 
      3  1.1  christos =head1 NAME
      4  1.1  christos 
      5  1.1  christos EVP_PKEY_sign_init, EVP_PKEY_sign_init_ex, EVP_PKEY_sign_init_ex2,
      6  1.1  christos EVP_PKEY_sign, EVP_PKEY_sign_message_init, EVP_PKEY_sign_message_update,
      7  1.1  christos EVP_PKEY_sign_message_final - sign using a public key algorithm
      8  1.1  christos 
      9  1.1  christos =head1 SYNOPSIS
     10  1.1  christos 
     11  1.1  christos  #include <openssl/evp.h>
     12  1.1  christos 
     13  1.1  christos  int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx);
     14  1.1  christos  int EVP_PKEY_sign_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]);
     15  1.1  christos  int EVP_PKEY_sign_init_ex2(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *algo,
     16  1.1  christos                             const OSSL_PARAM params[]);
     17  1.1  christos  int EVP_PKEY_sign_message_init(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *algo,
     18  1.1  christos                                 const OSSL_PARAM params[]);
     19  1.1  christos  int EVP_PKEY_sign_message_update(EVP_PKEY_CTX *ctx,
     20  1.1  christos                                   unsigned char *in, size_t inlen);
     21  1.1  christos  int EVP_PKEY_sign_message_final(EVP_PKEY_CTX *ctx, unsigned char *sig,
     22  1.1  christos                                  size_t *siglen, size_t sigsize);
     23  1.1  christos  int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
     24  1.1  christos                    unsigned char *sig, size_t *siglen,
     25  1.1  christos                    const unsigned char *tbs, size_t tbslen);
     26  1.1  christos 
     27  1.1  christos =head1 DESCRIPTION
     28  1.1  christos 
     29  1.1  christos EVP_PKEY_sign_init() initializes a public key algorithm context I<ctx> for
     30  1.1  christos signing using the algorithm given when the context was created
     31  1.1  christos using L<EVP_PKEY_CTX_new(3)> or variants thereof.  The algorithm is used to
     32  1.1  christos fetch a B<EVP_SIGNATURE> method implicitly, see L<provider(7)/Implicit fetch>
     33  1.1  christos for more information about implicit fetches.
     34  1.1  christos 
     35  1.1  christos EVP_PKEY_sign_init_ex() is the same as EVP_PKEY_sign_init() but additionally
     36  1.1  christos sets the passed parameters I<params> on the context before returning.
     37  1.1  christos 
     38  1.1  christos EVP_PKEY_sign_init_ex2() initializes a public key algorithm context I<ctx> for
     39  1.1  christos signing a pre-computed message digest using the algorithm given by I<algo> and
     40  1.1  christos the key given through L<EVP_PKEY_CTX_new(3)> or L<EVP_PKEY_CTX_new_from_pkey(3)>.
     41  1.1  christos A context I<ctx> without a pre-loaded key cannot be used with this function.
     42  1.1  christos This function provides almost the same functionality as EVP_PKEY_sign_init_ex(),
     43  1.1  christos but is uniquely intended to be used with a pre-computed message digest, and
     44  1.1  christos allows pre-determining the exact conditions for that message digest, if a
     45  1.1  christos composite signature algorithm (such as RSA-SHA256) was fetched.
     46  1.1  christos Following a call to this function, setting parameters that modifies the digest
     47  1.1  christos implementation or padding is not normally supported.
     48  1.1  christos 
     49  1.1  christos EVP_PKEY_sign_message_init() initializes a public key algorithm context I<ctx>
     50  1.1  christos for signing an unlimited size message using the algorithm given by I<algo> and
     51  1.1  christos the key given through L<EVP_PKEY_CTX_new(3)> or L<EVP_PKEY_CTX_new_from_pkey(3)>.
     52  1.1  christos Passing the message is supported both in a one-shot fashion using
     53  1.1  christos EVP_PKEY_sign(), and through the combination of EVP_PKEY_sign_message_update()
     54  1.1  christos and EVP_PKEY_sign_message_final().
     55  1.1  christos This function enables using algorithms that can process input of arbitrary
     56  1.1  christos length, such as ED25519, RSA-SHA256 and similar.
     57  1.1  christos 
     58  1.1  christos EVP_PKEY_sign_message_update() adds I<inlen> bytes from I<in> to the data to be
     59  1.1  christos processed for signature.  The signature algorithm specification and
     60  1.1  christos implementation determine how the input bytes are processed and if there's a
     61  1.1  christos limit on the total size of the input.  See L</NOTES> below for a deeper
     62  1.1  christos explanation.
     63  1.1  christos 
     64  1.1  christos EVP_PKEY_sign_message_final() signs the processed data and places the data in
     65  1.1  christos I<sig>, and the number of signature bytes in I<*siglen>, if the number of
     66  1.1  christos bytes doesn't surpass the size given by I<sigsize>.
     67  1.1  christos I<sig> may be NULL, and in that case, only I<*siglen> is updated with the
     68  1.1  christos number of signature bytes.
     69  1.1  christos 
     70  1.1  christos EVP_PKEY_sign() is a one-shot function that can be used with all the init
     71  1.1  christos functions above.
     72  1.1  christos When initialization was done with EVP_PKEY_sign_init(), EVP_PKEY_sign_init_ex()
     73  1.1  christos or EVP_PKEY_sign_init_ex2(), the data specified by I<tbs> and I<tbslen> is
     74  1.1  christos signed after appropriate padding.
     75  1.1  christos When initialization was done with EVP_PKEY_sign_message_init(), the data
     76  1.1  christos specified by I<tbs> and I<tbslen> is digested by the implied message digest
     77  1.1  christos algorithm, and the result is signed after appropriate padding.
     78  1.1  christos If I<sig> is NULL then the maximum size of the output buffer is written to the
     79  1.1  christos I<siglen> parameter.
     80  1.1  christos If I<sig> is not NULL, then before the call the I<siglen> parameter should
     81  1.1  christos contain the length of the I<sig> buffer, and if the call is successful the
     82  1.1  christos signature is written to I<sig> and the amount of data written to I<siglen>.
     83  1.1  christos 
     84  1.1  christos =head1 NOTES
     85  1.1  christos 
     86  1.1  christos =begin comment
     87  1.1  christos 
     88  1.1  christos These notes are largely replicated in EVP_PKEY_verify.pod, please keep them
     89  1.1  christos in sync.
     90  1.1  christos 
     91  1.1  christos =end comment
     92  1.1  christos 
     93  1.1  christos =head2 General
     94  1.1  christos 
     95  1.1  christos Some signature implementations only accumulate the input data and do no
     96  1.1  christos further processing before signing it (they expect the input to be a digest),
     97  1.1  christos while others compress the data, typically by internally producing a digest,
     98  1.1  christos and signing the result.
     99  1.1  christos Some of them support both modes of operation at the same time.
    100  1.1  christos The caller is expected to know how the chosen algorithm is supposed to behave
    101  1.1  christos and under what conditions.
    102  1.1  christos 
    103  1.1  christos For example, an RSA implementation can be expected to only expect a message
    104  1.1  christos digest as input, while ED25519 can be expected to process the input with a hash,
    105  1.1  christos i.e. to produce the message digest internally, and while RSA-SHA256 can be
    106  1.1  christos expected to handle either mode of operation, depending on if the operation was
    107  1.1  christos initialized with EVP_PKEY_sign_init_ex2() or with EVP_PKEY_sign_message_init(). 
    108  1.1  christos 
    109  1.1  christos Similarly, an RSA implementation usually expects additional details to be set,
    110  1.1  christos like the message digest algorithm that the input is supposed to be digested
    111  1.1  christos with, as well as the padding mode (see L<EVP_PKEY_CTX_set_signature_md(3)> and
    112  1.1  christos L<EVP_PKEY_CTX_set_rsa_padding(3)> and similar others), while an RSA-SHA256
    113  1.1  christos implementation usually has these details pre-set and immutable.
    114  1.1  christos 
    115  1.1  christos The functions described here can't be used to combine separate algorithms.  In
    116  1.1  christos particular, neither L<EVP_PKEY_CTX_set_signature_md(3)> nor the B<OSSL_PARAM>
    117  1.1  christos parameter "digest" (B<OSSL_SIGNATURE_PARAM_DIGEST>) can be used to combine a
    118  1.1  christos signature algorithm with a hash algorithm to process the input.  In other
    119  1.1  christos words, it's not possible to specify a I<ctx> pre-loaded with an RSA pkey, or
    120  1.1  christos an I<algo> that fetched C<RSA> and try to specify SHA256 separately to get the
    121  1.1  christos functionality of RSA-SHA256.  If combining algorithms in that manner is
    122  1.1  christos desired, please use L<EVP_DigestSignInit(3)> and associated functions.
    123  1.1  christos 
    124  1.1  christos =head2 Performing multiple signatures
    125  1.1  christos 
    126  1.1  christos When initialized using EVP_PKEY_sign_init_ex() or  EVP_PKEY_sign_init_ex2(),
    127  1.1  christos EVP_PKEY_sign() can be called more than once on the same context to have
    128  1.1  christos several one-shot operations performed using the same parameters.
    129  1.1  christos 
    130  1.1  christos When initialized using EVP_PKEY_sign_message_init(), it's not possible to
    131  1.1  christos call EVP_PKEY_sign() multiple times.
    132  1.1  christos 
    133  1.1  christos =head1 RETURN VALUES
    134  1.1  christos 
    135  1.1  christos All functions return 1 for success and 0 or a negative value for failure.
    136  1.1  christos 
    137  1.1  christos In particular, EVP_PKEY_sign_init() and its other variants may return -2 to
    138  1.1  christos indicate that the operation is not supported by the public key algorithm.
    139  1.1  christos 
    140  1.1  christos =head1 EXAMPLES
    141  1.1  christos 
    142  1.1  christos =begin comment
    143  1.1  christos 
    144  1.1  christos These examples are largely replicated in EVP_PKEY_verify.pod, please keep them
    145  1.1  christos in sync.
    146  1.1  christos 
    147  1.1  christos =end comment
    148  1.1  christos 
    149  1.1  christos =head2 RSA with PKCS#1 padding for SHA256
    150  1.1  christos 
    151  1.1  christos Sign data using RSA with PKCS#1 padding and a SHA256 digest as input:
    152  1.1  christos 
    153  1.1  christos  #include <openssl/evp.h>
    154  1.1  christos  #include <openssl/rsa.h>
    155  1.1  christos 
    156  1.1  christos  EVP_PKEY_CTX *ctx;
    157  1.1  christos  /* md is a SHA-256 digest in this example. */
    158  1.1  christos  unsigned char *md, *sig;
    159  1.1  christos  size_t mdlen = 32, siglen;
    160  1.1  christos  EVP_PKEY *signing_key;
    161  1.1  christos 
    162  1.1  christos  /*
    163  1.1  christos   * NB: assumes signing_key and md are set up before the next
    164  1.1  christos   * step. signing_key must be an RSA private key and md must
    165  1.1  christos   * point to the SHA-256 digest to be signed.
    166  1.1  christos   */
    167  1.1  christos  ctx = EVP_PKEY_CTX_new(signing_key, NULL /* no engine */);
    168  1.1  christos  if (ctx == NULL)
    169  1.1  christos      /* Error occurred */
    170  1.1  christos  if (EVP_PKEY_sign_init(ctx) <= 0)
    171  1.1  christos      /* Error */
    172  1.1  christos  if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) <= 0)
    173  1.1  christos      /* Error */
    174  1.1  christos  if (EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()) <= 0)
    175  1.1  christos      /* Error */
    176  1.1  christos 
    177  1.1  christos  /* Determine buffer length */
    178  1.1  christos  if (EVP_PKEY_sign(ctx, NULL, &siglen, md, mdlen) <= 0)
    179  1.1  christos      /* Error */
    180  1.1  christos 
    181  1.1  christos  sig = OPENSSL_malloc(siglen);
    182  1.1  christos 
    183  1.1  christos  if (sig == NULL)
    184  1.1  christos      /* malloc failure */
    185  1.1  christos 
    186  1.1  christos  if (EVP_PKEY_sign(ctx, sig, &siglen, md, mdlen) <= 0)
    187  1.1  christos      /* Error */
    188  1.1  christos 
    189  1.1  christos  /* Signature is siglen bytes written to buffer sig */
    190  1.1  christos 
    191  1.1  christos =head2 RSA-SHA256 with a pre-computed digest
    192  1.1  christos 
    193  1.1  christos Sign a digest with RSA-SHA256 using one-shot functions.  To be noted is that
    194  1.1  christos RSA-SHA256 is assumed to be an implementation of C<sha256WithRSAEncryption>,
    195  1.1  christos for which the padding is pre-determined to be B<RSA_PKCS1_PADDING>, and the
    196  1.1  christos input digest is assumed to have been computed using SHA256.
    197  1.1  christos 
    198  1.1  christos  #include <openssl/evp.h>
    199  1.1  christos  #include <openssl/rsa.h>
    200  1.1  christos 
    201  1.1  christos  EVP_PKEY_CTX *ctx;
    202  1.1  christos  /* md is a SHA-256 digest in this example. */
    203  1.1  christos  unsigned char *md, *sig;
    204  1.1  christos  size_t mdlen = 32, siglen;
    205  1.1  christos  EVP_PKEY *signing_key;
    206  1.1  christos 
    207  1.1  christos  /*
    208  1.1  christos   * NB: assumes signing_key and md are set up before the next
    209  1.1  christos   * step. signing_key must be an RSA private key and md must
    210  1.1  christos   * point to the SHA-256 digest to be signed.
    211  1.1  christos   */
    212  1.1  christos  ctx = EVP_PKEY_CTX_new(signing_key, NULL /* no engine */);
    213  1.1  christos  alg = EVP_SIGNATURE_fetch(NULL, "RSA-SHA256", NULL);
    214  1.1  christos 
    215  1.1  christos  if (ctx == NULL)
    216  1.1  christos      /* Error occurred */
    217  1.1  christos  if (EVP_PKEY_sign_init_ex2(ctx, alg, NULL) <= 0)
    218  1.1  christos      /* Error */
    219  1.1  christos 
    220  1.1  christos  /* Determine buffer length */
    221  1.1  christos  if (EVP_PKEY_sign(ctx, NULL, &siglen, md, mdlen) <= 0)
    222  1.1  christos      /* Error */
    223  1.1  christos 
    224  1.1  christos  sig = OPENSSL_malloc(siglen);
    225  1.1  christos 
    226  1.1  christos  if (sig == NULL)
    227  1.1  christos      /* malloc failure */
    228  1.1  christos 
    229  1.1  christos  if (EVP_PKEY_sign(ctx, sig, &siglen, md, mdlen) <= 0)
    230  1.1  christos      /* Error */
    231  1.1  christos 
    232  1.1  christos  /* Signature is siglen bytes written to buffer sig */
    233  1.1  christos 
    234  1.1  christos 
    235  1.1  christos =head2 RSA-SHA256, one-shot
    236  1.1  christos 
    237  1.1  christos Sign a document with RSA-SHA256 using one-shot functions.
    238  1.1  christos To be noted is that RSA-SHA256 is assumed to be an implementation of
    239  1.1  christos C<sha256WithRSAEncryption>, for which the padding is pre-determined to be
    240  1.1  christos B<RSA_PKCS1_PADDING>.
    241  1.1  christos 
    242  1.1  christos  #include <openssl/evp.h>
    243  1.1  christos  #include <openssl/rsa.h>
    244  1.1  christos 
    245  1.1  christos  EVP_PKEY_CTX *ctx;
    246  1.1  christos  /* in is the input in this example. */
    247  1.1  christos  unsigned char *in, *sig;
    248  1.1  christos  /* inlen is the length of the input in this example. */
    249  1.1  christos  size_t inlen, siglen;
    250  1.1  christos  EVP_PKEY *signing_key;
    251  1.1  christos  EVP_SIGNATURE *alg;
    252  1.1  christos 
    253  1.1  christos  /*
    254  1.1  christos   * NB: assumes signing_key, in and inlen are set up before
    255  1.1  christos   * the next step. signing_key must be an RSA private key,
    256  1.1  christos   * in must point to data to be digested and signed, and
    257  1.1  christos   * inlen must be the size of the data in bytes.
    258  1.1  christos   */
    259  1.1  christos  ctx = EVP_PKEY_CTX_new(signing_key, NULL /* no engine */);
    260  1.1  christos  alg = EVP_SIGNATURE_fetch(NULL, "RSA-SHA256", NULL);
    261  1.1  christos 
    262  1.1  christos  if (ctx == NULL || alg == NULL)
    263  1.1  christos      /* Error occurred */
    264  1.1  christos  if (EVP_PKEY_sign_message_init(ctx, alg, NULL) <= 0)
    265  1.1  christos      /* Error */
    266  1.1  christos 
    267  1.1  christos  /* Determine sig buffer length */
    268  1.1  christos  if (EVP_PKEY_sign(ctx, NULL, &siglen, in, inlen) <= 0)
    269  1.1  christos      /* Error */
    270  1.1  christos 
    271  1.1  christos  sig = OPENSSL_malloc(siglen);
    272  1.1  christos 
    273  1.1  christos  if (sig == NULL)
    274  1.1  christos      /* malloc failure */
    275  1.1  christos 
    276  1.1  christos  if (EVP_PKEY_sign(ctx, sig, &siglen, in, inlen) <= 0)
    277  1.1  christos      /* Error */
    278  1.1  christos 
    279  1.1  christos  /* Signature is siglen bytes written to buffer sig */
    280  1.1  christos 
    281  1.1  christos 
    282  1.1  christos =head2 RSA-SHA256, using update and final
    283  1.1  christos 
    284  1.1  christos This is the same as the previous example, but allowing stream-like
    285  1.1  christos functionality.
    286  1.1  christos 
    287  1.1  christos  #include <openssl/evp.h>
    288  1.1  christos  #include <openssl/rsa.h>
    289  1.1  christos 
    290  1.1  christos  EVP_PKEY_CTX *ctx;
    291  1.1  christos  /* in is the input in this example. */
    292  1.1  christos  unsigned char *in, *sig;
    293  1.1  christos  /* inlen is the length of the input in this example. */
    294  1.1  christos  size_t inlen, siglen;
    295  1.1  christos  EVP_PKEY *signing_key;
    296  1.1  christos  EVP_SIGNATURE *alg;
    297  1.1  christos 
    298  1.1  christos  /*
    299  1.1  christos   * NB: assumes signing_key, in and inlen are set up before
    300  1.1  christos   * the next step. signing_key must be an RSA private key,
    301  1.1  christos   * in must point to data to be digested and signed, and
    302  1.1  christos   * inlen must be the size of the data in bytes.
    303  1.1  christos   */
    304  1.1  christos  ctx = EVP_PKEY_CTX_new(signing_key, NULL /* no engine */);
    305  1.1  christos  alg = EVP_SIGNATURE_fetch(NULL, "RSA-SHA256", NULL);
    306  1.1  christos 
    307  1.1  christos  if (ctx == NULL || alg == NULL)
    308  1.1  christos      /* Error occurred */
    309  1.1  christos  if (EVP_PKEY_sign_message_init(ctx, alg, NULL) <= 0)
    310  1.1  christos      /* Error */
    311  1.1  christos 
    312  1.1  christos  while (inlen > 0) {
    313  1.1  christos      if (EVP_PKEY_sign_message_update(ctx, in, inlen)) <= 0)
    314  1.1  christos          /* Error */
    315  1.1  christos      if (inlen > 256) {
    316  1.1  christos          inlen -= 256;
    317  1.1  christos          in += 256;
    318  1.1  christos      } else {
    319  1.1  christos          inlen = 0;
    320  1.1  christos      }
    321  1.1  christos  }
    322  1.1  christos 
    323  1.1  christos  /* Determine sig buffer length */
    324  1.1  christos  if (EVP_PKEY_sign_message_final(ctx, NULL, &siglen) <= 0)
    325  1.1  christos      /* Error */
    326  1.1  christos 
    327  1.1  christos  sig = OPENSSL_malloc(siglen);
    328  1.1  christos 
    329  1.1  christos  if (sig == NULL)
    330  1.1  christos      /* malloc failure */
    331  1.1  christos 
    332  1.1  christos  if (EVP_PKEY_sign_message_final(ctx, sig, &siglen) <= 0)
    333  1.1  christos      /* Error */
    334  1.1  christos 
    335  1.1  christos  /* Signature is siglen bytes written to buffer sig */
    336  1.1  christos 
    337  1.1  christos 
    338  1.1  christos =head1 SEE ALSO
    339  1.1  christos 
    340  1.1  christos L<EVP_PKEY_CTX_new(3)>,
    341  1.1  christos L<EVP_PKEY_CTX_ctrl(3)>,
    342  1.1  christos L<EVP_PKEY_encrypt(3)>,
    343  1.1  christos L<EVP_PKEY_decrypt(3)>,
    344  1.1  christos L<EVP_PKEY_verify(3)>,
    345  1.1  christos L<EVP_PKEY_verify_recover(3)>,
    346  1.1  christos L<EVP_PKEY_derive(3)>
    347  1.1  christos 
    348  1.1  christos =head1 HISTORY
    349  1.1  christos 
    350  1.1  christos The EVP_PKEY_sign_init() and EVP_PKEY_sign() functions were added in
    351  1.1  christos OpenSSL 1.0.0.
    352  1.1  christos 
    353  1.1  christos The EVP_PKEY_sign_init_ex() function was added in OpenSSL 3.0.
    354  1.1  christos 
    355  1.1  christos The EVP_PKEY_sign_init_ex2(), EVP_PKEY_sign_message_init(),
    356  1.1  christos EVP_PKEY_sign_message_update() and EVP_PKEY_sign_message_final() functions
    357  1.1  christos where added in OpenSSL 3.4.
    358  1.1  christos 
    359  1.1  christos =head1 COPYRIGHT
    360  1.1  christos 
    361  1.1  christos Copyright 2006-2025 The OpenSSL Project Authors. All Rights Reserved.
    362  1.1  christos 
    363  1.1  christos Licensed under the Apache License 2.0 (the "License").  You may not use
    364  1.1  christos this file except in compliance with the License.  You can obtain a copy
    365  1.1  christos in the file LICENSE in the source distribution or at
    366  1.1  christos L<https://www.openssl.org/source/license.html>.
    367  1.1  christos 
    368  1.1  christos =cut
    369