1 /** @file
2   RSA Asymmetric Cipher Wrapper Implementation over OpenSSL.
3 
4   This file implements following APIs which provide basic capabilities for RSA:
5   1) RsaNew
6   2) RsaFree
7   3) RsaSetKey
8   4) RsaPkcs1Verify
9 
10 Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
11 This program and the accompanying materials
12 are licensed and made available under the terms and conditions of the BSD License
13 which accompanies this distribution.  The full text of the license may be found at
14 http://opensource.org/licenses/bsd-license.php
15 
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 
19 **/
20 
21 #include "InternalCryptLib.h"
22 
23 #include <openssl/bn.h>
24 #include <openssl/rsa.h>
25 #include <openssl/objects.h>
26 
27 /**
28   Allocates and initializes one RSA context for subsequent use.
29 
30   @return  Pointer to the RSA context that has been initialized.
31            If the allocations fails, RsaNew() returns NULL.
32 
33 **/
34 VOID *
35 EFIAPI
RsaNew(VOID)36 RsaNew (
37   VOID
38   )
39 {
40   //
41   // Allocates & Initializes RSA Context by OpenSSL RSA_new()
42   //
43   return (VOID *) RSA_new ();
44 }
45 
46 /**
47   Release the specified RSA context.
48 
49   @param[in]  RsaContext  Pointer to the RSA context to be released.
50 
51 **/
52 VOID
53 EFIAPI
RsaFree(IN VOID * RsaContext)54 RsaFree (
55   IN  VOID  *RsaContext
56   )
57 {
58   //
59   // Free OpenSSL RSA Context
60   //
61   RSA_free ((RSA *) RsaContext);
62 }
63 
64 /**
65   Sets the tag-designated key component into the established RSA context.
66 
67   This function sets the tag-designated RSA key component into the established
68   RSA context from the user-specified non-negative integer (octet string format
69   represented in RSA PKCS#1).
70   If BigNumber is NULL, then the specified key component in RSA context is cleared.
71 
72   If RsaContext is NULL, then return FALSE.
73 
74   @param[in, out]  RsaContext  Pointer to RSA context being set.
75   @param[in]       KeyTag      Tag of RSA key component being set.
76   @param[in]       BigNumber   Pointer to octet integer buffer.
77                                If NULL, then the specified key component in RSA
78                                context is cleared.
79   @param[in]       BnSize      Size of big number buffer in bytes.
80                                If BigNumber is NULL, then it is ignored.
81 
82   @retval  TRUE   RSA key component was set successfully.
83   @retval  FALSE  Invalid RSA key component tag.
84 
85 **/
86 BOOLEAN
87 EFIAPI
RsaSetKey(IN OUT VOID * RsaContext,IN RSA_KEY_TAG KeyTag,IN CONST UINT8 * BigNumber,IN UINTN BnSize)88 RsaSetKey (
89   IN OUT  VOID         *RsaContext,
90   IN      RSA_KEY_TAG  KeyTag,
91   IN      CONST UINT8  *BigNumber,
92   IN      UINTN        BnSize
93   )
94 {
95   RSA  *RsaKey;
96 
97   //
98   // Check input parameters.
99   //
100   if (RsaContext == NULL || BnSize > INT_MAX) {
101     return FALSE;
102   }
103 
104   RsaKey = (RSA *) RsaContext;
105   //
106   // Set RSA Key Components by converting octet string to OpenSSL BN representation.
107   // NOTE: For RSA public key (used in signature verification), only public components
108   //       (N, e) are needed.
109   //
110   switch (KeyTag) {
111 
112   //
113   // RSA Public Modulus (N)
114   //
115   case RsaKeyN:
116     if (RsaKey->n != NULL) {
117       BN_free (RsaKey->n);
118     }
119     RsaKey->n = NULL;
120     if (BigNumber == NULL) {
121       break;
122     }
123     RsaKey->n = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->n);
124     if (RsaKey->n == NULL) {
125       return FALSE;
126     }
127 
128     break;
129 
130   //
131   // RSA Public Exponent (e)
132   //
133   case RsaKeyE:
134     if (RsaKey->e != NULL) {
135       BN_free (RsaKey->e);
136     }
137     RsaKey->e = NULL;
138     if (BigNumber == NULL) {
139       break;
140     }
141     RsaKey->e = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->e);
142     if (RsaKey->e == NULL) {
143       return FALSE;
144     }
145 
146     break;
147 
148   //
149   // RSA Private Exponent (d)
150   //
151   case RsaKeyD:
152     if (RsaKey->d != NULL) {
153       BN_free (RsaKey->d);
154     }
155     RsaKey->d = NULL;
156     if (BigNumber == NULL) {
157       break;
158     }
159     RsaKey->d = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->d);
160     if (RsaKey->d == NULL) {
161       return FALSE;
162     }
163 
164     break;
165 
166   //
167   // RSA Secret Prime Factor of Modulus (p)
168   //
169   case RsaKeyP:
170     if (RsaKey->p != NULL) {
171       BN_free (RsaKey->p);
172     }
173     RsaKey->p = NULL;
174     if (BigNumber == NULL) {
175       break;
176     }
177     RsaKey->p = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->p);
178     if (RsaKey->p == NULL) {
179       return FALSE;
180     }
181 
182     break;
183 
184   //
185   // RSA Secret Prime Factor of Modules (q)
186   //
187   case RsaKeyQ:
188     if (RsaKey->q != NULL) {
189       BN_free (RsaKey->q);
190     }
191     RsaKey->q = NULL;
192     if (BigNumber == NULL) {
193       break;
194     }
195     RsaKey->q = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->q);
196     if (RsaKey->q == NULL) {
197       return FALSE;
198     }
199 
200     break;
201 
202   //
203   // p's CRT Exponent (== d mod (p - 1))
204   //
205   case RsaKeyDp:
206     if (RsaKey->dmp1 != NULL) {
207       BN_free (RsaKey->dmp1);
208     }
209     RsaKey->dmp1 = NULL;
210     if (BigNumber == NULL) {
211       break;
212     }
213     RsaKey->dmp1 = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->dmp1);
214     if (RsaKey->dmp1 == NULL) {
215       return FALSE;
216     }
217 
218     break;
219 
220   //
221   // q's CRT Exponent (== d mod (q - 1))
222   //
223   case RsaKeyDq:
224     if (RsaKey->dmq1 != NULL) {
225       BN_free (RsaKey->dmq1);
226     }
227     RsaKey->dmq1 = NULL;
228     if (BigNumber == NULL) {
229       break;
230     }
231     RsaKey->dmq1 = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->dmq1);
232     if (RsaKey->dmq1 == NULL) {
233       return FALSE;
234     }
235 
236     break;
237 
238   //
239   // The CRT Coefficient (== 1/q mod p)
240   //
241   case RsaKeyQInv:
242     if (RsaKey->iqmp != NULL) {
243       BN_free (RsaKey->iqmp);
244     }
245     RsaKey->iqmp = NULL;
246     if (BigNumber == NULL) {
247       break;
248     }
249     RsaKey->iqmp = BN_bin2bn (BigNumber, (UINT32) BnSize, RsaKey->iqmp);
250     if (RsaKey->iqmp == NULL) {
251       return FALSE;
252     }
253 
254     break;
255 
256   default:
257     return FALSE;
258   }
259 
260   return TRUE;
261 }
262 
263 /**
264   Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
265   RSA PKCS#1.
266 
267   If RsaContext is NULL, then return FALSE.
268   If MessageHash is NULL, then return FALSE.
269   If Signature is NULL, then return FALSE.
270   If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.
271 
272   @param[in]  RsaContext   Pointer to RSA context for signature verification.
273   @param[in]  MessageHash  Pointer to octet message hash to be checked.
274   @param[in]  HashSize     Size of the message hash in bytes.
275   @param[in]  Signature    Pointer to RSA PKCS1-v1_5 signature to be verified.
276   @param[in]  SigSize      Size of signature in bytes.
277 
278   @retval  TRUE   Valid signature encoded in PKCS1-v1_5.
279   @retval  FALSE  Invalid signature or invalid RSA context.
280 
281 **/
282 BOOLEAN
283 EFIAPI
RsaPkcs1Verify(IN VOID * RsaContext,IN CONST UINT8 * MessageHash,IN UINTN HashSize,IN CONST UINT8 * Signature,IN UINTN SigSize)284 RsaPkcs1Verify (
285   IN  VOID         *RsaContext,
286   IN  CONST UINT8  *MessageHash,
287   IN  UINTN        HashSize,
288   IN  CONST UINT8  *Signature,
289   IN  UINTN        SigSize
290   )
291 {
292   INT32    DigestType;
293   UINT8    *SigBuf;
294 
295   //
296   // Check input parameters.
297   //
298   if (RsaContext == NULL || MessageHash == NULL || Signature == NULL) {
299     return FALSE;
300   }
301 
302   if (SigSize > INT_MAX || SigSize == 0) {
303     return FALSE;
304   }
305 
306   //
307   // Determine the message digest algorithm according to digest size.
308   //   Only MD5, SHA-1 or SHA-256 algorithm is supported.
309   //
310   switch (HashSize) {
311   case MD5_DIGEST_SIZE:
312     DigestType = NID_md5;
313     break;
314 
315   case SHA1_DIGEST_SIZE:
316     DigestType = NID_sha1;
317     break;
318 
319   case SHA256_DIGEST_SIZE:
320     DigestType = NID_sha256;
321     break;
322 
323   default:
324     return FALSE;
325   }
326 
327   SigBuf = (UINT8 *) Signature;
328   return (BOOLEAN) RSA_verify (
329                      DigestType,
330                      MessageHash,
331                      (UINT32) HashSize,
332                      SigBuf,
333                      (UINT32) SigSize,
334                      (RSA *) RsaContext
335                      );
336 }
337