TianoCore EDK2 master
Loading...
Searching...
No Matches
CryptRsaPss.c
Go to the documentation of this file.
1
12#include "InternalCryptLib.h"
13#include <mbedtls/rsa.h>
14#include <mbedtls/sha256.h>
15#include <mbedtls/sha512.h>
16
35BOOLEAN
36EFIAPI
38 IN VOID *RsaContext,
39 IN CONST UINT8 *Message,
40 IN UINTN MsgSize,
41 IN CONST UINT8 *Signature,
42 IN UINTN SigSize,
43 IN UINT16 DigestLen,
44 IN UINT16 SaltLen
45 )
46{
47 INT32 Ret;
48 mbedtls_md_type_t MdAlg;
49 UINT8 HashValue[SHA512_DIGEST_SIZE];
50 mbedtls_rsa_context *RsaKey;
51
52 if (RsaContext == NULL) {
53 return FALSE;
54 }
55
56 if ((Message == NULL) || (MsgSize == 0) || (MsgSize > INT_MAX)) {
57 return FALSE;
58 }
59
60 if (SaltLen != DigestLen) {
61 return FALSE;
62 }
63
64 if ((Signature == NULL) || (SigSize == 0) || (SigSize > INT_MAX)) {
65 return FALSE;
66 }
67
68 RsaKey = (mbedtls_rsa_context *)RsaContext;
69 if (mbedtls_rsa_complete (RsaKey) != 0) {
70 return FALSE;
71 }
72
73 ZeroMem (HashValue, DigestLen);
74
75 switch (DigestLen) {
77 MdAlg = MBEDTLS_MD_SHA256;
78 if (mbedtls_sha256 (Message, MsgSize, HashValue, FALSE) != 0) {
79 return FALSE;
80 }
81
82 break;
83
85 MdAlg = MBEDTLS_MD_SHA384;
86 if (mbedtls_sha512 (Message, MsgSize, HashValue, TRUE) != 0) {
87 return FALSE;
88 }
89
90 break;
91
93 MdAlg = MBEDTLS_MD_SHA512;
94 if (mbedtls_sha512 (Message, MsgSize, HashValue, FALSE) != 0) {
95 return FALSE;
96 }
97
98 break;
99
100 default:
101 return FALSE;
102 }
103
104 if (mbedtls_rsa_get_len (RsaContext) != SigSize) {
105 return FALSE;
106 }
107
108 mbedtls_rsa_set_padding (RsaContext, MBEDTLS_RSA_PKCS_V21, MdAlg);
109
110 Ret = mbedtls_rsa_rsassa_pss_verify (
111 RsaContext,
112 MdAlg,
113 (UINT32)DigestLen,
114 HashValue,
115 Signature
116 );
117 if (Ret != 0) {
118 return FALSE;
119 }
120
121 return TRUE;
122}
UINT64 UINTN
#define SHA512_DIGEST_SIZE
Definition: BaseCryptLib.h:54
#define SHA256_DIGEST_SIZE
Definition: BaseCryptLib.h:44
#define SHA384_DIGEST_SIZE
Definition: BaseCryptLib.h:49
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
BOOLEAN EFIAPI RsaPssVerify(IN VOID *RsaContext, IN CONST UINT8 *Message, IN UINTN MsgSize, IN CONST UINT8 *Signature, IN UINTN SigSize, IN UINT16 DigestLen, IN UINT16 SaltLen)
Definition: CryptRsaPss.c:67