1e71b7053SJung-uk Kim=pod 2e71b7053SJung-uk Kim 3e71b7053SJung-uk Kim=head1 NAME 4e71b7053SJung-uk Kim 5*b077aed3SPierre ProncheryEVP_DigestSignInit_ex, EVP_DigestSignInit, EVP_DigestSignUpdate, 6*b077aed3SPierre ProncheryEVP_DigestSignFinal, EVP_DigestSign - EVP signing functions 7e71b7053SJung-uk Kim 8e71b7053SJung-uk Kim=head1 SYNOPSIS 9e71b7053SJung-uk Kim 10e71b7053SJung-uk Kim #include <openssl/evp.h> 11e71b7053SJung-uk Kim 12*b077aed3SPierre Pronchery int EVP_DigestSignInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, 13*b077aed3SPierre Pronchery const char *mdname, OSSL_LIB_CTX *libctx, 14*b077aed3SPierre Pronchery const char *props, EVP_PKEY *pkey, 15*b077aed3SPierre Pronchery const OSSL_PARAM params[]); 16e71b7053SJung-uk Kim int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, 17e71b7053SJung-uk Kim const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey); 18e71b7053SJung-uk Kim int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt); 19e71b7053SJung-uk Kim int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen); 20e71b7053SJung-uk Kim 21e71b7053SJung-uk Kim int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, 22e71b7053SJung-uk Kim size_t *siglen, const unsigned char *tbs, 23e71b7053SJung-uk Kim size_t tbslen); 24e71b7053SJung-uk Kim 25e71b7053SJung-uk Kim=head1 DESCRIPTION 26e71b7053SJung-uk Kim 2758f35182SJung-uk KimThe EVP signature routines are a high-level interface to digital signatures. 28*b077aed3SPierre ProncheryInput data is digested first before the signing takes place. 29e71b7053SJung-uk Kim 30*b077aed3SPierre ProncheryEVP_DigestSignInit_ex() sets up signing context I<ctx> to use a digest 31*b077aed3SPierre Proncherywith the name I<mdname> and private key I<pkey>. The name of the digest to be 32*b077aed3SPierre Proncheryused is passed to the provider of the signature algorithm in use. How that 33*b077aed3SPierre Proncheryprovider interprets the digest name is provider specific. The provider may 34*b077aed3SPierre Proncheryimplement that digest directly itself or it may (optionally) choose to fetch it 35*b077aed3SPierre Pronchery(which could result in a digest from a different provider being selected). If the 36*b077aed3SPierre Proncheryprovider supports fetching the digest then it may use the I<props> argument for 37*b077aed3SPierre Proncherythe properties to be used during the fetch. Finally, the passed parameters 38*b077aed3SPierre ProncheryI<params>, if not NULL, are set on the context before returning. 39e71b7053SJung-uk Kim 40*b077aed3SPierre ProncheryThe I<pkey> algorithm is used to fetch a B<EVP_SIGNATURE> method implicitly, to 41*b077aed3SPierre Proncherybe used for the actual signing. See L<provider(7)/Implicit fetch> for 42*b077aed3SPierre Proncherymore information about implicit fetches. 43e71b7053SJung-uk Kim 44*b077aed3SPierre ProncheryThe OpenSSL default and legacy providers support fetching digests and can fetch 45*b077aed3SPierre Proncherythose digests from any available provider. The OpenSSL FIPS provider also 46*b077aed3SPierre Proncherysupports fetching digests but will only fetch digests that are themselves 47*b077aed3SPierre Proncheryimplemented inside the FIPS provider. 48*b077aed3SPierre Pronchery 49*b077aed3SPierre ProncheryI<ctx> must be created with EVP_MD_CTX_new() before calling this function. If 50*b077aed3SPierre ProncheryI<pctx> is not NULL, the EVP_PKEY_CTX of the signing operation will be written 51*b077aed3SPierre Proncheryto I<*pctx>: this can be used to set alternative signing options. Note that any 52*b077aed3SPierre Proncheryexisting value in I<*pctx> is overwritten. The EVP_PKEY_CTX value returned must 53*b077aed3SPierre Proncherynot be freed directly by the application if I<ctx> is not assigned an 54*b077aed3SPierre ProncheryEVP_PKEY_CTX value before being passed to EVP_DigestSignInit_ex() 55*b077aed3SPierre Pronchery(which means the EVP_PKEY_CTX is created inside EVP_DigestSignInit_ex() 56*b077aed3SPierre Proncheryand it will be freed automatically when the EVP_MD_CTX is freed). If the 57*b077aed3SPierre ProncheryEVP_PKEY_CTX to be used is created by EVP_DigestSignInit_ex then it 58*b077aed3SPierre Proncherywill use the B<OSSL_LIB_CTX> specified in I<libctx> and the property query string 59*b077aed3SPierre Proncheryspecified in I<props>. 60*b077aed3SPierre Pronchery 61*b077aed3SPierre ProncheryThe digest I<mdname> may be NULL if the signing algorithm supports it. The 62*b077aed3SPierre ProncheryI<props> argument can always be NULL. 63*b077aed3SPierre Pronchery 64*b077aed3SPierre ProncheryNo B<EVP_PKEY_CTX> will be created by EVP_DigestSignInit_ex() if the 65*b077aed3SPierre Proncherypassed I<ctx> has already been assigned one via L<EVP_MD_CTX_set_pkey_ctx(3)>. 66*b077aed3SPierre ProncherySee also L<SM2(7)>. 67e71b7053SJung-uk Kim 68e71b7053SJung-uk KimOnly EVP_PKEY types that support signing can be used with these functions. This 69e71b7053SJung-uk Kimincludes MAC algorithms where the MAC generation is considered as a form of 70e71b7053SJung-uk Kim"signing". Built-in EVP_PKEY types supported by these functions are CMAC, 71e71b7053SJung-uk KimPoly1305, DSA, ECDSA, HMAC, RSA, SipHash, Ed25519 and Ed448. 72e71b7053SJung-uk Kim 73e71b7053SJung-uk KimNot all digests can be used for all key types. The following combinations apply. 74e71b7053SJung-uk Kim 75e71b7053SJung-uk Kim=over 4 76e71b7053SJung-uk Kim 77e71b7053SJung-uk Kim=item DSA 78e71b7053SJung-uk Kim 79e71b7053SJung-uk KimSupports SHA1, SHA224, SHA256, SHA384 and SHA512 80e71b7053SJung-uk Kim 81e71b7053SJung-uk Kim=item ECDSA 82e71b7053SJung-uk Kim 83e71b7053SJung-uk KimSupports SHA1, SHA224, SHA256, SHA384, SHA512 and SM3 84e71b7053SJung-uk Kim 85e71b7053SJung-uk Kim=item RSA with no padding 86e71b7053SJung-uk Kim 87*b077aed3SPierre ProncherySupports no digests (the digest I<type> must be NULL) 88e71b7053SJung-uk Kim 89e71b7053SJung-uk Kim=item RSA with X931 padding 90e71b7053SJung-uk Kim 91e71b7053SJung-uk KimSupports SHA1, SHA256, SHA384 and SHA512 92e71b7053SJung-uk Kim 93e71b7053SJung-uk Kim=item All other RSA padding types 94e71b7053SJung-uk Kim 95e71b7053SJung-uk KimSupport SHA1, SHA224, SHA256, SHA384, SHA512, MD5, MD5_SHA1, MD2, MD4, MDC2, 96e71b7053SJung-uk KimSHA3-224, SHA3-256, SHA3-384, SHA3-512 97e71b7053SJung-uk Kim 98e71b7053SJung-uk Kim=item Ed25519 and Ed448 99e71b7053SJung-uk Kim 100*b077aed3SPierre ProncherySupport no digests (the digest I<type> must be NULL) 101e71b7053SJung-uk Kim 102e71b7053SJung-uk Kim=item HMAC 103e71b7053SJung-uk Kim 104e71b7053SJung-uk KimSupports any digest 105e71b7053SJung-uk Kim 106e71b7053SJung-uk Kim=item CMAC, Poly1305 and SipHash 107e71b7053SJung-uk Kim 108e71b7053SJung-uk KimWill ignore any digest provided. 109e71b7053SJung-uk Kim 110e71b7053SJung-uk Kim=back 111e71b7053SJung-uk Kim 112e71b7053SJung-uk KimIf RSA-PSS is used and restrictions apply then the digest must match. 113e71b7053SJung-uk Kim 114*b077aed3SPierre ProncheryEVP_DigestSignInit() works in the same way as EVP_DigestSignInit_ex() 115*b077aed3SPierre Proncheryexcept that the I<mdname> parameter will be inferred from the supplied 116*b077aed3SPierre Proncherydigest I<type>, and I<props> will be NULL. Where supplied the ENGINE I<e> will 117*b077aed3SPierre Proncherybe used for the signing and digest algorithm implementations. I<e> may be NULL. 118e71b7053SJung-uk Kim 119*b077aed3SPierre ProncheryEVP_DigestSignUpdate() hashes I<cnt> bytes of data at I<d> into the 120*b077aed3SPierre Proncherysignature context I<ctx>. This function can be called several times on the 121*b077aed3SPierre Proncherysame I<ctx> to include additional data. 122e71b7053SJung-uk Kim 123*b077aed3SPierre ProncheryUnless I<sig> is NULL EVP_DigestSignFinal() signs the data in I<ctx> 124*b077aed3SPierre Proncheryand places the signature in I<sig>. 125*b077aed3SPierre ProncheryOtherwise the maximum necessary size of the output buffer is written to 126*b077aed3SPierre Proncherythe I<siglen> parameter. If I<sig> is not NULL then before the call the 127*b077aed3SPierre ProncheryI<siglen> parameter should contain the length of the I<sig> buffer. If the 128*b077aed3SPierre Proncherycall is successful the signature is written to I<sig> and the amount of data 129*b077aed3SPierre Proncherywritten to I<siglen>. 130*b077aed3SPierre Pronchery 131*b077aed3SPierre ProncheryEVP_DigestSign() signs I<tbslen> bytes of data at I<tbs> and places the 132*b077aed3SPierre Proncherysignature in I<sig> and its length in I<siglen> in a similar way to 133*b077aed3SPierre ProncheryEVP_DigestSignFinal(). In the event of a failure EVP_DigestSign() cannot be 134*b077aed3SPierre Proncherycalled again without reinitialising the EVP_MD_CTX. If I<sig> is NULL before the 135*b077aed3SPierre Proncherycall then I<siglen> will be populated with the required size for the I<sig> 136*b077aed3SPierre Proncherybuffer. If I<sig> is non-NULL before the call then I<siglen> should contain the 137*b077aed3SPierre Proncherylength of the I<sig> buffer. 138e71b7053SJung-uk Kim 139e71b7053SJung-uk Kim=head1 RETURN VALUES 140e71b7053SJung-uk Kim 14117f01e99SJung-uk KimEVP_DigestSignInit(), EVP_DigestSignUpdate(), EVP_DigestSignFinal() and 14217f01e99SJung-uk KimEVP_DigestSign() return 1 for success and 0 for failure. 143e71b7053SJung-uk Kim 144e71b7053SJung-uk KimThe error codes can be obtained from L<ERR_get_error(3)>. 145e71b7053SJung-uk Kim 146e71b7053SJung-uk Kim=head1 NOTES 147e71b7053SJung-uk Kim 148e71b7053SJung-uk KimThe B<EVP> interface to digital signatures should almost always be used in 14958f35182SJung-uk Kimpreference to the low-level interfaces. This is because the code then becomes 150e71b7053SJung-uk Kimtransparent to the algorithm used and much more flexible. 151e71b7053SJung-uk Kim 152e71b7053SJung-uk KimEVP_DigestSign() is a one shot operation which signs a single block of data 153e71b7053SJung-uk Kimin one function. For algorithms that support streaming it is equivalent to 154e71b7053SJung-uk Kimcalling EVP_DigestSignUpdate() and EVP_DigestSignFinal(). For algorithms which 155e71b7053SJung-uk Kimdo not support streaming (e.g. PureEdDSA) it is the only way to sign data. 156e71b7053SJung-uk Kim 157e71b7053SJung-uk KimIn previous versions of OpenSSL there was a link between message digest types 158e71b7053SJung-uk Kimand public key algorithms. This meant that "clone" digests such as EVP_dss1() 159e71b7053SJung-uk Kimneeded to be used to sign using SHA1 and DSA. This is no longer necessary and 160e71b7053SJung-uk Kimthe use of clone digest is now discouraged. 161e71b7053SJung-uk Kim 162da327cd2SJung-uk KimFor some key types and parameters the random number generator must be seeded. 163da327cd2SJung-uk KimIf the automatic seeding or reseeding of the OpenSSL CSPRNG fails due to 164da327cd2SJung-uk Kimexternal circumstances (see L<RAND(7)>), the operation will fail. 165e71b7053SJung-uk Kim 166e71b7053SJung-uk KimThe call to EVP_DigestSignFinal() internally finalizes a copy of the digest 167e71b7053SJung-uk Kimcontext. This means that calls to EVP_DigestSignUpdate() and 168e71b7053SJung-uk KimEVP_DigestSignFinal() can be called later to digest and sign additional data. 169e71b7053SJung-uk Kim 170*b077aed3SPierre ProncheryEVP_DigestSignInit() and EVP_DigestSignInit_ex() functions can be called 171*b077aed3SPierre Proncherymultiple times on a context and the parameters set by previous calls should be 172*b077aed3SPierre Proncherypreserved if the I<pkey> parameter is NULL. The call then just resets the state 173*b077aed3SPierre Proncheryof the I<ctx>. 174e71b7053SJung-uk Kim 175*b077aed3SPierre ProncheryIgnoring failure returns of EVP_DigestSignInit() and EVP_DigestSignInit_ex() 176*b077aed3SPierre Proncheryfunctions can lead to subsequent undefined behavior when calling 177*b077aed3SPierre ProncheryEVP_DigestSignUpdate(), EVP_DigestSignFinal(), or EVP_DigestSign(). 178*b077aed3SPierre Pronchery 179*b077aed3SPierre ProncheryThe use of EVP_PKEY_get_size() with these functions is discouraged because some 180e71b7053SJung-uk Kimsignature operations may have a signature length which depends on the 181*b077aed3SPierre Proncheryparameters set. As a result EVP_PKEY_get_size() would have to return a value 182e71b7053SJung-uk Kimwhich indicates the maximum possible signature for any set of parameters. 183e71b7053SJung-uk Kim 184e71b7053SJung-uk Kim=head1 SEE ALSO 185e71b7053SJung-uk Kim 186e71b7053SJung-uk KimL<EVP_DigestVerifyInit(3)>, 187e71b7053SJung-uk KimL<EVP_DigestInit(3)>, 188e71b7053SJung-uk KimL<evp(7)>, L<HMAC(3)>, L<MD2(3)>, 189e71b7053SJung-uk KimL<MD5(3)>, L<MDC2(3)>, L<RIPEMD160(3)>, 190*b077aed3SPierre ProncheryL<SHA1(3)>, L<openssl-dgst(1)>, 191da327cd2SJung-uk KimL<RAND(7)> 192e71b7053SJung-uk Kim 193e71b7053SJung-uk Kim=head1 HISTORY 194e71b7053SJung-uk Kim 195e71b7053SJung-uk KimEVP_DigestSignInit(), EVP_DigestSignUpdate() and EVP_DigestSignFinal() 1966935a639SJung-uk Kimwere added in OpenSSL 1.0.0. 197e71b7053SJung-uk Kim 198*b077aed3SPierre ProncheryEVP_DigestSignInit_ex() was added in OpenSSL 3.0. 199*b077aed3SPierre Pronchery 200*b077aed3SPierre ProncheryEVP_DigestSignUpdate() was converted from a macro to a function in OpenSSL 3.0. 201*b077aed3SPierre Pronchery 202e71b7053SJung-uk Kim=head1 COPYRIGHT 203e71b7053SJung-uk Kim 204*b077aed3SPierre ProncheryCopyright 2006-2023 The OpenSSL Project Authors. All Rights Reserved. 205e71b7053SJung-uk Kim 206*b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License"). You may not use 207e71b7053SJung-uk Kimthis file except in compliance with the License. You can obtain a copy 208e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at 209e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>. 210e71b7053SJung-uk Kim 211e71b7053SJung-uk Kim=cut 212