1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
/** @file
RSA Asymmetric Cipher Wrapper Implementation over MbedTLS.
This file implements following APIs which provide basic capabilities for RSA:
1) RsaNew
2) RsaFree
3) RsaSetKey
4) RsaPkcs1Verify
RFC 8017 - PKCS #1: RSA Cryptography Specifications Version 2.2
Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "InternalCryptLib.h"
#include <mbedtls/rsa.h>
/**
Allocates and initializes one RSA context for subsequent use.
@return Pointer to the RSA context that has been initialized.
If the allocations fails, RsaNew() returns NULL.
**/
VOID *
EFIAPI
RsaNew (
VOID
)
{
VOID *RsaContext;
RsaContext = AllocateZeroPool (sizeof (mbedtls_rsa_context));
if (RsaContext == NULL) {
return RsaContext;
}
mbedtls_rsa_init (RsaContext);
if (mbedtls_rsa_set_padding (RsaContext, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE) != 0) {
return NULL;
}
return RsaContext;
}
/**
Release the specified RSA context.
@param[in] RsaContext Pointer to the RSA context to be released.
**/
VOID
EFIAPI
RsaFree (
IN VOID *RsaContext
)
{
mbedtls_rsa_free (RsaContext);
if (RsaContext != NULL) {
FreePool (RsaContext);
}
}
/**
Sets the tag-designated key component into the established RSA context.
This function sets the tag-designated RSA key component into the established
RSA context from the user-specified non-negative integer (octet string format
represented in RSA PKCS#1).
If BigNumber is NULL, then the specified key component in RSA context is cleared.
If RsaContext is NULL, then return FALSE.
@param[in, out] RsaContext Pointer to RSA context being set.
@param[in] KeyTag Tag of RSA key component being set.
@param[in] BigNumber Pointer to octet integer buffer.
If NULL, then the specified key component in RSA
context is cleared.
@param[in] BnSize Size of big number buffer in bytes.
If BigNumber is NULL, then it is ignored.
@retval TRUE RSA key component was set successfully.
@retval FALSE Invalid RSA key component tag.
**/
BOOLEAN
EFIAPI
RsaSetKey (
IN OUT VOID *RsaContext,
IN RSA_KEY_TAG KeyTag,
IN CONST UINT8 *BigNumber,
IN UINTN BnSize
)
{
mbedtls_rsa_context *RsaKey;
INT32 Ret;
mbedtls_mpi Value;
//
// Check input parameters.
//
if ((RsaContext == NULL) || (BnSize > INT_MAX)) {
return FALSE;
}
mbedtls_mpi_init (&Value);
RsaKey = (mbedtls_rsa_context *)RsaContext;
// if BigNumber is Null clear
if (BigNumber != NULL) {
Ret = mbedtls_mpi_read_binary (&Value, BigNumber, BnSize);
if (Ret != 0) {
mbedtls_mpi_free (&Value);
return FALSE;
}
}
switch (KeyTag) {
case RsaKeyN:
Ret = mbedtls_rsa_import (
RsaKey,
&Value,
NULL,
NULL,
NULL,
NULL
);
break;
case RsaKeyE:
Ret = mbedtls_rsa_import (
RsaKey,
NULL,
NULL,
NULL,
NULL,
&Value
);
break;
case RsaKeyD:
Ret = mbedtls_rsa_import (
RsaKey,
NULL,
NULL,
NULL,
&Value,
NULL
);
break;
case RsaKeyQ:
Ret = mbedtls_rsa_import (
RsaKey,
NULL,
NULL,
&Value,
NULL,
NULL
);
break;
case RsaKeyP:
Ret = mbedtls_rsa_import (
RsaKey,
NULL,
&Value,
NULL,
NULL,
NULL
);
break;
case RsaKeyDp:
case RsaKeyDq:
case RsaKeyQInv:
default:
Ret = -1;
break;
}
mbedtls_mpi_free (&Value);
return Ret == 0;
}
/**
Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
RSA PKCS#1.
If RsaContext is NULL, then return FALSE.
If MessageHash is NULL, then return FALSE.
If Signature is NULL, then return FALSE.
If HashSize is not equal to the size of MD5, SHA-1, SHA-256, SHA-384 or SHA-512 digest, then return FALSE.
@param[in] RsaContext Pointer to RSA context for signature verification.
@param[in] MessageHash Pointer to octet message hash to be checked.
@param[in] HashSize Size of the message hash in bytes.
@param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.
@param[in] SigSize Size of signature in bytes.
@retval TRUE Valid signature encoded in PKCS1-v1_5.
@retval FALSE Invalid signature or invalid RSA context.
**/
BOOLEAN
EFIAPI
RsaPkcs1Verify (
IN VOID *RsaContext,
IN CONST UINT8 *MessageHash,
IN UINTN HashSize,
IN CONST UINT8 *Signature,
IN UINTN SigSize
)
{
INT32 Ret;
mbedtls_md_type_t md_alg;
mbedtls_rsa_context *RsaKey;
if ((RsaContext == NULL) || (MessageHash == NULL) || (Signature == NULL)) {
return FALSE;
}
if ((SigSize > INT_MAX) || (SigSize == 0)) {
return FALSE;
}
RsaKey = (mbedtls_rsa_context *)RsaContext;
if (mbedtls_rsa_complete (RsaKey) != 0) {
return FALSE;
}
switch (HashSize) {
#ifdef ENABLE_MD5_DEPRECATED_INTERFACES
case MD5_DIGEST_SIZE:
md_alg = MBEDTLS_MD_MD5;
break;
#endif
#ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
case SHA1_DIGEST_SIZE:
md_alg = MBEDTLS_MD_SHA1;
break;
#endif
case SHA256_DIGEST_SIZE:
md_alg = MBEDTLS_MD_SHA256;
break;
case SHA384_DIGEST_SIZE:
md_alg = MBEDTLS_MD_SHA384;
break;
case SHA512_DIGEST_SIZE:
md_alg = MBEDTLS_MD_SHA512;
break;
default:
return FALSE;
}
if (mbedtls_rsa_get_len (RsaContext) != SigSize) {
return FALSE;
}
mbedtls_rsa_set_padding (RsaContext, MBEDTLS_RSA_PKCS_V15, md_alg);
Ret = mbedtls_rsa_pkcs1_verify (
RsaContext,
md_alg,
(UINT32)HashSize,
MessageHash,
Signature
);
if (Ret != 0) {
return FALSE;
}
return TRUE;
}
|