CryptRsaExt.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /** @file
  2. RSA Asymmetric Cipher Wrapper Implementation over OpenSSL.
  3. This file implements following APIs which provide more capabilities for RSA:
  4. 1) RsaGetKey
  5. 2) RsaGenerateKey
  6. 3) RsaCheckKey
  7. 4) RsaPkcs1Sign
  8. Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
  9. SPDX-License-Identifier: BSD-2-Clause-Patent
  10. **/
  11. #include "InternalCryptLib.h"
  12. #include <openssl/bn.h>
  13. #include <openssl/rsa.h>
  14. #include <openssl/err.h>
  15. #include <openssl/objects.h>
  16. /**
  17. Gets the tag-designated RSA key component from the established RSA context.
  18. This function retrieves the tag-designated RSA key component from the
  19. established RSA context as a non-negative integer (octet string format
  20. represented in RSA PKCS#1).
  21. If specified key component has not been set or has been cleared, then returned
  22. BnSize is set to 0.
  23. If the BigNumber buffer is too small to hold the contents of the key, FALSE
  24. is returned and BnSize is set to the required buffer size to obtain the key.
  25. If RsaContext is NULL, then return FALSE.
  26. If BnSize is NULL, then return FALSE.
  27. If BnSize is large enough but BigNumber is NULL, then return FALSE.
  28. @param[in, out] RsaContext Pointer to RSA context being set.
  29. @param[in] KeyTag Tag of RSA key component being set.
  30. @param[out] BigNumber Pointer to octet integer buffer.
  31. @param[in, out] BnSize On input, the size of big number buffer in bytes.
  32. On output, the size of data returned in big number buffer in bytes.
  33. @retval TRUE RSA key component was retrieved successfully.
  34. @retval FALSE Invalid RSA key component tag.
  35. @retval FALSE BnSize is too small.
  36. **/
  37. BOOLEAN
  38. EFIAPI
  39. RsaGetKey (
  40. IN OUT VOID *RsaContext,
  41. IN RSA_KEY_TAG KeyTag,
  42. OUT UINT8 *BigNumber,
  43. IN OUT UINTN *BnSize
  44. )
  45. {
  46. RSA *RsaKey;
  47. BIGNUM *BnKey;
  48. UINTN Size;
  49. //
  50. // Check input parameters.
  51. //
  52. if (RsaContext == NULL || BnSize == NULL) {
  53. return FALSE;
  54. }
  55. RsaKey = (RSA *) RsaContext;
  56. Size = *BnSize;
  57. *BnSize = 0;
  58. BnKey = NULL;
  59. switch (KeyTag) {
  60. //
  61. // RSA Public Modulus (N)
  62. //
  63. case RsaKeyN:
  64. RSA_get0_key (RsaKey, (const BIGNUM **)&BnKey, NULL, NULL);
  65. break;
  66. //
  67. // RSA Public Exponent (e)
  68. //
  69. case RsaKeyE:
  70. RSA_get0_key (RsaKey, NULL, (const BIGNUM **)&BnKey, NULL);
  71. break;
  72. //
  73. // RSA Private Exponent (d)
  74. //
  75. case RsaKeyD:
  76. RSA_get0_key (RsaKey, NULL, NULL, (const BIGNUM **)&BnKey);
  77. break;
  78. //
  79. // RSA Secret Prime Factor of Modulus (p)
  80. //
  81. case RsaKeyP:
  82. RSA_get0_factors (RsaKey, (const BIGNUM **)&BnKey, NULL);
  83. break;
  84. //
  85. // RSA Secret Prime Factor of Modules (q)
  86. //
  87. case RsaKeyQ:
  88. RSA_get0_factors (RsaKey, NULL, (const BIGNUM **)&BnKey);
  89. break;
  90. //
  91. // p's CRT Exponent (== d mod (p - 1))
  92. //
  93. case RsaKeyDp:
  94. RSA_get0_crt_params (RsaKey, (const BIGNUM **)&BnKey, NULL, NULL);
  95. break;
  96. //
  97. // q's CRT Exponent (== d mod (q - 1))
  98. //
  99. case RsaKeyDq:
  100. RSA_get0_crt_params (RsaKey, NULL, (const BIGNUM **)&BnKey, NULL);
  101. break;
  102. //
  103. // The CRT Coefficient (== 1/q mod p)
  104. //
  105. case RsaKeyQInv:
  106. RSA_get0_crt_params (RsaKey, NULL, NULL, (const BIGNUM **)&BnKey);
  107. break;
  108. default:
  109. return FALSE;
  110. }
  111. if (BnKey == NULL) {
  112. return FALSE;
  113. }
  114. *BnSize = Size;
  115. Size = BN_num_bytes (BnKey);
  116. if (*BnSize < Size) {
  117. *BnSize = Size;
  118. return FALSE;
  119. }
  120. if (BigNumber == NULL) {
  121. *BnSize = Size;
  122. return TRUE;
  123. }
  124. *BnSize = BN_bn2bin (BnKey, BigNumber) ;
  125. return TRUE;
  126. }
  127. /**
  128. Generates RSA key components.
  129. This function generates RSA key components. It takes RSA public exponent E and
  130. length in bits of RSA modulus N as input, and generates all key components.
  131. If PublicExponent is NULL, the default RSA public exponent (0x10001) will be used.
  132. Before this function can be invoked, pseudorandom number generator must be correctly
  133. initialized by RandomSeed().
  134. If RsaContext is NULL, then return FALSE.
  135. @param[in, out] RsaContext Pointer to RSA context being set.
  136. @param[in] ModulusLength Length of RSA modulus N in bits.
  137. @param[in] PublicExponent Pointer to RSA public exponent.
  138. @param[in] PublicExponentSize Size of RSA public exponent buffer in bytes.
  139. @retval TRUE RSA key component was generated successfully.
  140. @retval FALSE Invalid RSA key component tag.
  141. **/
  142. BOOLEAN
  143. EFIAPI
  144. RsaGenerateKey (
  145. IN OUT VOID *RsaContext,
  146. IN UINTN ModulusLength,
  147. IN CONST UINT8 *PublicExponent,
  148. IN UINTN PublicExponentSize
  149. )
  150. {
  151. BIGNUM *KeyE;
  152. BOOLEAN RetVal;
  153. //
  154. // Check input parameters.
  155. //
  156. if (RsaContext == NULL || ModulusLength > INT_MAX || PublicExponentSize > INT_MAX) {
  157. return FALSE;
  158. }
  159. KeyE = BN_new ();
  160. if (KeyE == NULL) {
  161. return FALSE;
  162. }
  163. RetVal = FALSE;
  164. if (PublicExponent == NULL) {
  165. if (BN_set_word (KeyE, 0x10001) == 0) {
  166. goto _Exit;
  167. }
  168. } else {
  169. if (BN_bin2bn (PublicExponent, (UINT32) PublicExponentSize, KeyE) == NULL) {
  170. goto _Exit;
  171. }
  172. }
  173. if (RSA_generate_key_ex ((RSA *) RsaContext, (UINT32) ModulusLength, KeyE, NULL) == 1) {
  174. RetVal = TRUE;
  175. }
  176. _Exit:
  177. BN_free (KeyE);
  178. return RetVal;
  179. }
  180. /**
  181. Validates key components of RSA context.
  182. NOTE: This function performs integrity checks on all the RSA key material, so
  183. the RSA key structure must contain all the private key data.
  184. This function validates key components of RSA context in following aspects:
  185. - Whether p is a prime
  186. - Whether q is a prime
  187. - Whether n = p * q
  188. - Whether d*e = 1 mod lcm(p-1,q-1)
  189. If RsaContext is NULL, then return FALSE.
  190. @param[in] RsaContext Pointer to RSA context to check.
  191. @retval TRUE RSA key components are valid.
  192. @retval FALSE RSA key components are not valid.
  193. **/
  194. BOOLEAN
  195. EFIAPI
  196. RsaCheckKey (
  197. IN VOID *RsaContext
  198. )
  199. {
  200. UINTN Reason;
  201. //
  202. // Check input parameters.
  203. //
  204. if (RsaContext == NULL) {
  205. return FALSE;
  206. }
  207. if (RSA_check_key ((RSA *) RsaContext) != 1) {
  208. Reason = ERR_GET_REASON (ERR_peek_last_error ());
  209. if (Reason == RSA_R_P_NOT_PRIME ||
  210. Reason == RSA_R_Q_NOT_PRIME ||
  211. Reason == RSA_R_N_DOES_NOT_EQUAL_P_Q ||
  212. Reason == RSA_R_D_E_NOT_CONGRUENT_TO_1) {
  213. return FALSE;
  214. }
  215. }
  216. return TRUE;
  217. }
  218. /**
  219. Carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme.
  220. This function carries out the RSA-SSA signature generation with EMSA-PKCS1-v1_5 encoding scheme defined in
  221. RSA PKCS#1.
  222. If the Signature buffer is too small to hold the contents of signature, FALSE
  223. is returned and SigSize is set to the required buffer size to obtain the signature.
  224. If RsaContext is NULL, then return FALSE.
  225. If MessageHash is NULL, then return FALSE.
  226. If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.
  227. If SigSize is large enough but Signature is NULL, then return FALSE.
  228. @param[in] RsaContext Pointer to RSA context for signature generation.
  229. @param[in] MessageHash Pointer to octet message hash to be signed.
  230. @param[in] HashSize Size of the message hash in bytes.
  231. @param[out] Signature Pointer to buffer to receive RSA PKCS1-v1_5 signature.
  232. @param[in, out] SigSize On input, the size of Signature buffer in bytes.
  233. On output, the size of data returned in Signature buffer in bytes.
  234. @retval TRUE Signature successfully generated in PKCS1-v1_5.
  235. @retval FALSE Signature generation failed.
  236. @retval FALSE SigSize is too small.
  237. **/
  238. BOOLEAN
  239. EFIAPI
  240. RsaPkcs1Sign (
  241. IN VOID *RsaContext,
  242. IN CONST UINT8 *MessageHash,
  243. IN UINTN HashSize,
  244. OUT UINT8 *Signature,
  245. IN OUT UINTN *SigSize
  246. )
  247. {
  248. RSA *Rsa;
  249. UINTN Size;
  250. INT32 DigestType;
  251. //
  252. // Check input parameters.
  253. //
  254. if (RsaContext == NULL || MessageHash == NULL) {
  255. return FALSE;
  256. }
  257. Rsa = (RSA *) RsaContext;
  258. Size = RSA_size (Rsa);
  259. if (*SigSize < Size) {
  260. *SigSize = Size;
  261. return FALSE;
  262. }
  263. if (Signature == NULL) {
  264. return FALSE;
  265. }
  266. //
  267. // Determine the message digest algorithm according to digest size.
  268. // Only MD5, SHA-1 or SHA-256 algorithm is supported.
  269. //
  270. switch (HashSize) {
  271. case MD5_DIGEST_SIZE:
  272. DigestType = NID_md5;
  273. break;
  274. case SHA1_DIGEST_SIZE:
  275. DigestType = NID_sha1;
  276. break;
  277. case SHA256_DIGEST_SIZE:
  278. DigestType = NID_sha256;
  279. break;
  280. default:
  281. return FALSE;
  282. }
  283. return (BOOLEAN) RSA_sign (
  284. DigestType,
  285. MessageHash,
  286. (UINT32) HashSize,
  287. Signature,
  288. (UINT32 *) SigSize,
  289. (RSA *) RsaContext
  290. );
  291. }