CryptRsaBasic.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /** @file
  2. RSA Asymmetric Cipher Wrapper Implementation over OpenSSL.
  3. This file implements following APIs which provide basic capabilities for RSA:
  4. 1) RsaNew
  5. 2) RsaFree
  6. 3) RsaSetKey
  7. 4) RsaPkcs1Verify
  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/objects.h>
  15. /**
  16. Allocates and initializes one RSA context for subsequent use.
  17. @return Pointer to the RSA context that has been initialized.
  18. If the allocations fails, RsaNew() returns NULL.
  19. **/
  20. VOID *
  21. EFIAPI
  22. RsaNew (
  23. VOID
  24. )
  25. {
  26. //
  27. // Allocates & Initializes RSA Context by OpenSSL RSA_new()
  28. //
  29. return (VOID *) RSA_new ();
  30. }
  31. /**
  32. Release the specified RSA context.
  33. @param[in] RsaContext Pointer to the RSA context to be released.
  34. **/
  35. VOID
  36. EFIAPI
  37. RsaFree (
  38. IN VOID *RsaContext
  39. )
  40. {
  41. //
  42. // Free OpenSSL RSA Context
  43. //
  44. RSA_free ((RSA *) RsaContext);
  45. }
  46. /**
  47. Sets the tag-designated key component into the established RSA context.
  48. This function sets the tag-designated RSA key component into the established
  49. RSA context from the user-specified non-negative integer (octet string format
  50. represented in RSA PKCS#1).
  51. If BigNumber is NULL, then the specified key component in RSA context is cleared.
  52. If RsaContext is NULL, then return FALSE.
  53. @param[in, out] RsaContext Pointer to RSA context being set.
  54. @param[in] KeyTag Tag of RSA key component being set.
  55. @param[in] BigNumber Pointer to octet integer buffer.
  56. If NULL, then the specified key component in RSA
  57. context is cleared.
  58. @param[in] BnSize Size of big number buffer in bytes.
  59. If BigNumber is NULL, then it is ignored.
  60. @retval TRUE RSA key component was set successfully.
  61. @retval FALSE Invalid RSA key component tag.
  62. **/
  63. BOOLEAN
  64. EFIAPI
  65. RsaSetKey (
  66. IN OUT VOID *RsaContext,
  67. IN RSA_KEY_TAG KeyTag,
  68. IN CONST UINT8 *BigNumber,
  69. IN UINTN BnSize
  70. )
  71. {
  72. RSA *RsaKey;
  73. BIGNUM *BnN;
  74. BIGNUM *BnE;
  75. BIGNUM *BnD;
  76. BIGNUM *BnP;
  77. BIGNUM *BnQ;
  78. BIGNUM *BnDp;
  79. BIGNUM *BnDq;
  80. BIGNUM *BnQInv;
  81. //
  82. // Check input parameters.
  83. //
  84. if (RsaContext == NULL || BnSize > INT_MAX) {
  85. return FALSE;
  86. }
  87. BnN = NULL;
  88. BnE = NULL;
  89. BnD = NULL;
  90. BnP = NULL;
  91. BnQ = NULL;
  92. BnDp = NULL;
  93. BnDq = NULL;
  94. BnQInv = NULL;
  95. //
  96. // Retrieve the components from RSA object.
  97. //
  98. RsaKey = (RSA *) RsaContext;
  99. RSA_get0_key (RsaKey, (const BIGNUM **)&BnN, (const BIGNUM **)&BnE, (const BIGNUM **)&BnD);
  100. RSA_get0_factors (RsaKey, (const BIGNUM **)&BnP, (const BIGNUM **)&BnQ);
  101. RSA_get0_crt_params (RsaKey, (const BIGNUM **)&BnDp, (const BIGNUM **)&BnDq, (const BIGNUM **)&BnQInv);
  102. //
  103. // Set RSA Key Components by converting octet string to OpenSSL BN representation.
  104. // NOTE: For RSA public key (used in signature verification), only public components
  105. // (N, e) are needed.
  106. //
  107. switch (KeyTag) {
  108. //
  109. // RSA Public Modulus (N), Public Exponent (e) and Private Exponent (d)
  110. //
  111. case RsaKeyN:
  112. case RsaKeyE:
  113. case RsaKeyD:
  114. if (BnN == NULL) {
  115. BnN = BN_new ();
  116. }
  117. if (BnE == NULL) {
  118. BnE = BN_new ();
  119. }
  120. if (BnD == NULL) {
  121. BnD = BN_new ();
  122. }
  123. if ((BnN == NULL) || (BnE == NULL) || (BnD == NULL)) {
  124. return FALSE;
  125. }
  126. switch (KeyTag) {
  127. case RsaKeyN:
  128. BnN = BN_bin2bn (BigNumber, (UINT32)BnSize, BnN);
  129. break;
  130. case RsaKeyE:
  131. BnE = BN_bin2bn (BigNumber, (UINT32)BnSize, BnE);
  132. break;
  133. case RsaKeyD:
  134. BnD = BN_bin2bn (BigNumber, (UINT32)BnSize, BnD);
  135. break;
  136. default:
  137. return FALSE;
  138. }
  139. if (RSA_set0_key (RsaKey, BN_dup(BnN), BN_dup(BnE), BN_dup(BnD)) == 0) {
  140. return FALSE;
  141. }
  142. break;
  143. //
  144. // RSA Secret Prime Factor of Modulus (p and q)
  145. //
  146. case RsaKeyP:
  147. case RsaKeyQ:
  148. if (BnP == NULL) {
  149. BnP = BN_new ();
  150. }
  151. if (BnQ == NULL) {
  152. BnQ = BN_new ();
  153. }
  154. if ((BnP == NULL) || (BnQ == NULL)) {
  155. return FALSE;
  156. }
  157. switch (KeyTag) {
  158. case RsaKeyP:
  159. BnP = BN_bin2bn (BigNumber, (UINT32)BnSize, BnP);
  160. break;
  161. case RsaKeyQ:
  162. BnQ = BN_bin2bn (BigNumber, (UINT32)BnSize, BnQ);
  163. break;
  164. default:
  165. return FALSE;
  166. }
  167. if (RSA_set0_factors (RsaKey, BN_dup(BnP), BN_dup(BnQ)) == 0) {
  168. return FALSE;
  169. }
  170. break;
  171. //
  172. // p's CRT Exponent (== d mod (p - 1)), q's CRT Exponent (== d mod (q - 1)),
  173. // and CRT Coefficient (== 1/q mod p)
  174. //
  175. case RsaKeyDp:
  176. case RsaKeyDq:
  177. case RsaKeyQInv:
  178. if (BnDp == NULL) {
  179. BnDp = BN_new ();
  180. }
  181. if (BnDq == NULL) {
  182. BnDq = BN_new ();
  183. }
  184. if (BnQInv == NULL) {
  185. BnQInv = BN_new ();
  186. }
  187. if ((BnDp == NULL) || (BnDq == NULL) || (BnQInv == NULL)) {
  188. return FALSE;
  189. }
  190. switch (KeyTag) {
  191. case RsaKeyDp:
  192. BnDp = BN_bin2bn (BigNumber, (UINT32)BnSize, BnDp);
  193. break;
  194. case RsaKeyDq:
  195. BnDq = BN_bin2bn (BigNumber, (UINT32)BnSize, BnDq);
  196. break;
  197. case RsaKeyQInv:
  198. BnQInv = BN_bin2bn (BigNumber, (UINT32)BnSize, BnQInv);
  199. break;
  200. default:
  201. return FALSE;
  202. }
  203. if (RSA_set0_crt_params (RsaKey, BN_dup(BnDp), BN_dup(BnDq), BN_dup(BnQInv)) == 0) {
  204. return FALSE;
  205. }
  206. break;
  207. default:
  208. return FALSE;
  209. }
  210. return TRUE;
  211. }
  212. /**
  213. Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
  214. RSA PKCS#1.
  215. If RsaContext is NULL, then return FALSE.
  216. If MessageHash is NULL, then return FALSE.
  217. If Signature is NULL, then return FALSE.
  218. If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.
  219. @param[in] RsaContext Pointer to RSA context for signature verification.
  220. @param[in] MessageHash Pointer to octet message hash to be checked.
  221. @param[in] HashSize Size of the message hash in bytes.
  222. @param[in] Signature Pointer to RSA PKCS1-v1_5 signature to be verified.
  223. @param[in] SigSize Size of signature in bytes.
  224. @retval TRUE Valid signature encoded in PKCS1-v1_5.
  225. @retval FALSE Invalid signature or invalid RSA context.
  226. **/
  227. BOOLEAN
  228. EFIAPI
  229. RsaPkcs1Verify (
  230. IN VOID *RsaContext,
  231. IN CONST UINT8 *MessageHash,
  232. IN UINTN HashSize,
  233. IN CONST UINT8 *Signature,
  234. IN UINTN SigSize
  235. )
  236. {
  237. INT32 DigestType;
  238. UINT8 *SigBuf;
  239. //
  240. // Check input parameters.
  241. //
  242. if (RsaContext == NULL || MessageHash == NULL || Signature == NULL) {
  243. return FALSE;
  244. }
  245. if (SigSize > INT_MAX || SigSize == 0) {
  246. return FALSE;
  247. }
  248. //
  249. // Determine the message digest algorithm according to digest size.
  250. // Only MD5, SHA-1 or SHA-256 algorithm is supported.
  251. //
  252. switch (HashSize) {
  253. case MD5_DIGEST_SIZE:
  254. DigestType = NID_md5;
  255. break;
  256. case SHA1_DIGEST_SIZE:
  257. DigestType = NID_sha1;
  258. break;
  259. case SHA256_DIGEST_SIZE:
  260. DigestType = NID_sha256;
  261. break;
  262. default:
  263. return FALSE;
  264. }
  265. SigBuf = (UINT8 *) Signature;
  266. return (BOOLEAN) RSA_verify (
  267. DigestType,
  268. MessageHash,
  269. (UINT32) HashSize,
  270. SigBuf,
  271. (UINT32) SigSize,
  272. (RSA *) RsaContext
  273. );
  274. }