06187ff4baa72788629901a4d215acd215fbaa38
2 RSA PSS Asymmetric Cipher Wrapper Implementation over OpenSSL.
4 This file implements following APIs which provide basic capabilities for RSA:
7 Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include "InternalCryptLib.h"
14 #include <openssl/bn.h>
15 #include <openssl/rsa.h>
16 #include <openssl/objects.h>
17 #include <openssl/evp.h>
21 Retrieve a pointer to EVP message digest object.
23 @param[in] DigestLen Length of the message digest.
34 case SHA256_DIGEST_SIZE
:
37 case SHA384_DIGEST_SIZE
:
40 case SHA512_DIGEST_SIZE
:
50 Carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme.
52 This function carries out the RSA-SSA signature generation with EMSA-PSS encoding scheme defined in
54 Mask generation function is the same as the message digest algorithm.
55 If the Signature buffer is too small to hold the contents of signature, FALSE
56 is returned and SigSize is set to the required buffer size to obtain the signature.
58 If RsaContext is NULL, then return FALSE.
59 If Message is NULL, then return FALSE.
60 If MsgSize is zero or > INT_MAX, then return FALSE.
61 If DigestLen is NOT 32, 48 or 64, return FALSE.
62 If SaltLen is not equal to DigestLen, then return FALSE.
63 If SigSize is large enough but Signature is NULL, then return FALSE.
64 If this interface is not supported, then return FALSE.
66 @param[in] RsaContext Pointer to RSA context for signature generation.
67 @param[in] Message Pointer to octet message to be signed.
68 @param[in] MsgSize Size of the message in bytes.
69 @param[in] DigestLen Length of the digest in bytes to be used for RSA signature operation.
70 @param[in] SaltLen Length of the salt in bytes to be used for PSS encoding.
71 @param[out] Signature Pointer to buffer to receive RSA PSS signature.
72 @param[in, out] SigSize On input, the size of Signature buffer in bytes.
73 On output, the size of data returned in Signature buffer in bytes.
75 @retval TRUE Signature successfully generated in RSASSA-PSS.
76 @retval FALSE Signature generation failed.
77 @retval FALSE SigSize is too small.
78 @retval FALSE This interface is not supported.
85 IN CONST UINT8
*Message
,
96 EVP_MD_CTX
*EvpVerifyCtx
;
98 CONST EVP_MD
*HashAlg
;
106 if (RsaContext
== NULL
) {
109 if (Message
== NULL
|| MsgSize
== 0 || MsgSize
> INT_MAX
) {
113 RsaSigSize
= RSA_size (RsaContext
);
114 if (*SigSize
< RsaSigSize
) {
115 *SigSize
= RsaSigSize
;
119 if (Signature
== NULL
) {
123 if (SaltLen
!= DigestLen
) {
127 HashAlg
= GetEvpMD(DigestLen
);
129 if (HashAlg
== NULL
) {
133 EvpRsaKey
= EVP_PKEY_new();
134 if (EvpRsaKey
== NULL
) {
138 EVP_PKEY_set1_RSA(EvpRsaKey
, RsaContext
);
140 EvpVerifyCtx
= EVP_MD_CTX_create();
141 if (EvpVerifyCtx
== NULL
) {
145 Result
= EVP_DigestSignInit(EvpVerifyCtx
, &KeyCtx
, HashAlg
, NULL
, EvpRsaKey
) > 0;
146 if (KeyCtx
== NULL
) {
151 Result
= EVP_PKEY_CTX_set_rsa_padding(KeyCtx
, RSA_PKCS1_PSS_PADDING
) > 0;
154 Result
= EVP_PKEY_CTX_set_rsa_pss_saltlen(KeyCtx
, SaltLen
) > 0;
157 Result
= EVP_PKEY_CTX_set_rsa_mgf1_md(KeyCtx
, HashAlg
) > 0;
160 Result
= EVP_DigestSignUpdate(EvpVerifyCtx
, Message
, (UINT32
)MsgSize
) > 0;
163 Result
= EVP_DigestSignFinal(EvpVerifyCtx
, Signature
, SigSize
) > 0;
167 if (EvpRsaKey
!= NULL
) {
168 EVP_PKEY_free(EvpRsaKey
);
170 if (EvpVerifyCtx
!= NULL
) {
171 EVP_MD_CTX_destroy(EvpVerifyCtx
);