summaryrefslogtreecommitdiff
path: root/Include/BaseCryptLib.h
blob: 34bf640c590b398c738b2bbad3ccdf4109875212 (plain)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2011, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**        5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093         **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************
//**********************************************************************
// $Header: /Alaska/BIN/Modules/CryptoPkg/Lib/Include/BaseCryptLib.h 1     6/13/11 5:19p Alexp $
//
// $Revision: 1 $
//
// $Date: 6/13/11 5:19p $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/BIN/Modules/CryptoPkg/Lib/Include/BaseCryptLib.h $
// 
// 1     6/13/11 5:19p Alexp
// 
// 1     5/06/11 6:12p Alexp
// initial module release
// 
// 2     5/05/11 3:39p Alexp
// add Help hdrs
// 
//**********************************************************************

//<AMI_FHDR_START>
//----------------------------------------------------------------------------
//
// Name:		BaseCryptLib.h
//
// Description:	Defines base cryptographic library APIs compatible with EDK2 BaseCryptLib.
//  The Base Cryptographic Library provides implementations of basic cryptography
//  primitives (SHA-1, SHA-256, RSA, etc) for UEFI security functionality enabling.
//
//----------------------------------------------------------------------------
//<AMI_FHDR_END>

#ifndef __BASE_CRYPT_LIB_H__
#define __BASE_CRYPT_LIB_H__

#include <Efi.h>
//=====================================================================================
//    One-Way Cryptographic Hash Primitives
//=====================================================================================

///
/// SHA-1 digest size in bytes.
///
#define SHA1_DIGEST_SIZE    20
///
/// SHA-256 digest size in bytes
///
#define SHA256_DIGEST_SIZE  32

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   Sha1GetContextSize
//
// Description: Retrieves the size, in bytes, of the context buffer required for SHA-1 hash operations.
//
// Input:none
//
// Output: The size, in bytes, of the context buffer required for SHA-1 hash operations.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
UINTN
Sha1GetContextSize (
  VOID
  );


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   Sha1Init
//
// Description: Initializes user-supplied memory pointed by Sha1Context as the SHA-1 hash context for
//  subsequent use. If Sha1Context is NULL, then ASSERT().
//
// Input:
//      param[in, out]  Sha1Context  Pointer to the SHA-1 Context being initialized.
//
// Output: 
//      retval TRUE  SHA-1 initialization succeeded.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN
Sha1Init (
  IN OUT  VOID  *Sha1Context
  );


/**
  Performs SHA-1 digest on a data buffer of the specified length. This function can
  be called multiple times to compute the digest of long or discontinuous data streams.

  If Sha1Context is NULL, then ASSERT().

  @param[in, out]  Sha1Context  Pointer to the SHA-1 context.
  @param[in]       Data         Pointer to the buffer containing the data to be hashed.
  @param[in]       DataLength   Length of Data buffer in bytes.

  @retval TRUE   SHA-1 data digest succeeded.
  @retval FALSE  Invalid SHA-1 context. After Sha1Final function has been called, the
                 SHA-1 context cannot be reused.

**/
BOOLEAN
Sha1Update (
  IN OUT  VOID        *Sha1Context,
  IN      CONST VOID  *Data,
  IN      UINTN       DataLength
  );


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   Sha1Final
//
// Description:   Completes SHA-1 hash computation and retrieves the digest value into the specified
//                memory. After this function has been called, the SHA-1 context cannot be used again.
//  If Sha1Context is NULL, then ASSERT().
//  If HashValue is NULL, then ASSERT().
//
// Input:
//  param[in, out]  Sha1Context  Pointer to the SHA-1 context
//  param[out]      HashValue    Pointer to a buffer that receives the SHA-1 digest
//                               value (20 bytes).
// Output: TRUE  SHA-1 digest computation succeeded.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN
Sha1Final (
  IN OUT  VOID   *Sha1Context,
  OUT     UINT8  *HashValue
  );


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   Sha256GetContextSize
//
// Description: Retrieves the size, in bytes, of the context buffer required for SHA256 hash operations.
//
// Input:none
// Output: The size, in bytes, of the context buffer required for SHA256 hash operations.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
UINTN
Sha256GetContextSize (
  VOID
  );


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   Sha256Init
//
// Description: Initializes user-supplied memory pointed by Sha256Context as SHA-256 hash context for
//  subsequent use. If Sha256Context is NULL, then ASSERT().
//
// Input:
//      param[in, out]  Sha256Context  Pointer to the SHA-1 Context being initialized.
//
// Output: 
//      retval TRUE  SHA-256 context initialization succeeded.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN
Sha256Init (
  IN OUT  VOID  *Sha256Context
  );


/**
  Performs SHA-256 digest on a data buffer of the specified length. This function can
  be called multiple times to compute the digest of long or discontinuous data streams.

  If Sha256Context is NULL, then ASSERT().

  @param[in, out]  Sha256Context  Pointer to the SHA-256 context.
  @param[in]       Data           Pointer to the buffer containing the data to be hashed.
  @param[in]       DataLength     Length of Data buffer in bytes.

  @retval TRUE   SHA-256 data digest succeeded.
  @retval FALSE  Invalid SHA-256 context. After Sha256Final function has been called, the
                 SHA-256 context cannot be reused.

**/
BOOLEAN
Sha256Update (
  IN OUT  VOID        *Sha256Context,
  IN      CONST VOID  *Data,
  IN      UINTN       DataLength
  );


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   Sha256Final
//
// Description:  Completes SHA-256 hash computation and retrieves the digest value into the specified
//               memory. After this function has been called, the SHA-256 context cannot be used again.
//  If Sha256Context is NULL, then ASSERT().
//  If HashValue is NULL, then ASSERT().
//
// Input:
//  param[in, out]  Sha256Context  Pointer to SHA-256 context
//  param[out]      HashValue      Pointer to a buffer that receives the SHA-256 digest
//                                 value (32 bytes).
// Output: TRUE  SHA-256 digest computation succeeded.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN
Sha256Final (
  IN OUT  VOID   *Sha256Context,
  OUT     UINT8  *HashValue
  );


//=====================================================================================
//    MAC (Message Authentication Code) Primitive
//=====================================================================================

///
/// No MAC supports for minimum scope required by UEFI
///


//=====================================================================================
//    Symmetric Cryptography Primitive
//=====================================================================================

///
/// No symmetric cryptographic supports for minimum scope required by UEFI
///


//=====================================================================================
//    Asymmetric Cryptography Primitive
//=====================================================================================
#define DEFAULT_RSA_KEY_MODULUS_LEN 256 // 2048 bits
#define DEFAULT_RSA_SIG_LEN DEFAULT_RSA_KEY_MODULUS_LEN // This is true as long as > data

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   RsaNew
//
// Description:  Allocates and Initializes one RSA Context for subsequent use.
//
// Input:
//
// Output: Pointer to the RSA Context that has been initialized.
//         If the allocations fails, RsaNew() returns NULL.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID *
RsaNew (
  VOID
  );

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   RsaFree
//
// Description:  Release the specified RSA Context.
//
// Input:
//         param[in]  RsaContext  Pointer to the RSA context to be released.
//
// Output: 
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
EFIAPI
RsaFree (
  IN  VOID  *RsaContext
  );


///
/// RSA Key Tags Definition used in RsaSetKey() function for key component identification.
///
typedef enum _RSA_KEY_TAG {
  RsaKeyN,      ///< RSA public Modulus (N)
  RsaKeyE,      ///< RSA Public exponent (e)
  RsaKeyD,      ///< RSA Private exponent (d)
  RsaKeyP,      ///< RSA secret prime factor of Modulus (p)
  RsaKeyQ,      ///< RSA secret prime factor of Modules (q)
  RsaKeyDp,     ///< p's CRT exponent (== d mod (p - 1))
  RsaKeyDq,     ///< q's CRT exponent (== d mod (q - 1))
  RsaKeyQInv    ///< The CRT coefficient (== 1/q mod p)
} RSA_KEY_TAG;

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   RsaSetKey
//
// Description:  Sets the tag-designated RSA key component into the established RSA context from
//  the user-specified nonnegative integer (octet string format represented in RSA
//  PKCS#1).   If RsaContext is NULL, then ASSERT().
//
// Input:
//  @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.
//  @param[in]       BnLength    Length of big number buffer in bytes.
//
// Output: 
//  @return  TRUE   RSA key component was set successfully.
//  @return  FALSE  Invalid RSA key component tag.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN
RsaSetKey (
  IN OUT VOID         *RsaContext,
  IN     RSA_KEY_TAG  KeyTag,
  IN     CONST UINT8  *BigNumber,
  IN     UINTN        BnLength
  );

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   RsaPkcs1Verify
//
// Description:    Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
//  RSA PKCS#1.
//  If RsaContext is NULL, then ASSERT().
//  If MessageHash is NULL, then ASSERT().
//  If Signature is NULL, then ASSERT().
//  If HashLength is not equal to the size of SHA-1 or SHA-256 digest, then ASSERT().
//
// Input:
//  @param[in]  RsaContext   Pointer to RSA context for signature verification.
//  @param[in]  MessageHash  Pointer to octet message hash to be checked.
//  @param[in]  HashLength   Length of the message hash in bytes.
//  @param[in]  Signature    Pointer to RSA PKCS1-v1_5 signature to be verified.
//  @param[in]  SigLength    Length of signature in bytes.
//
// Output: 
//  @return  TRUE   Valid signature encoded in PKCS1-v1_5.
//  @return  FALSE  Invalid signature or invalid RSA context.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN
RsaPkcs1Verify (
  IN  VOID         *RsaContext,
  IN  CONST UINT8  *MessageHash,
  IN  UINTN        HashLength,
  IN  UINT8        *Signature,
  IN  UINTN        SigLength
  );

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   Pkcs7Verify
//
// Description: Verifies the validility of a PKCS#7 signed data as described in 
//              "PKCS #7: Cryptographic Message Syntax Standard".
//  If P7Data is NULL, then ASSERT().
//
// Input:
//  @param[in]  P7Data       Pointer to the PKCS#7 message to verify.
//  @param[in]  P7Length     Length of the PKCS#7 message in bytes.
//  @param[in]  TrustedCert  Pointer to a trusted/root certificate encoded in DER, which
//                           is used for certificate chain verification.
//  @param[in]  CertLength   Length of the trusted certificate in bytes.
//  @param[in]  InData       Pointer to the content to be verified.
//  @param[in]  DataLength   Length of InData in bytes.
//
// Output: 
//  @return  TRUE  The specified PKCS#7 signed data is valid.
//  @return  FALSE Invalid PKCS#7 signed data.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN
Pkcs7Verify (
  IN  CONST UINT8  *P7Data,
  IN  UINTN        P7Length,
  IN  CONST UINT8  *TrustedCert,
  IN  UINTN        CertLength,
  IN  CONST UINT8  *InData,
  IN  UINTN        DataLength
  );


#endif // __BASE_CRYPT_LIB_H__