1 /** @file
2   The runtime cryptographic protocol.
3   Only limited crypto primitives (SHA-256 and RSA) are provided for runtime
4   authenticated variable service.
5 
6 Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution.  The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11 
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 
15 **/
16 
17 #ifndef __EFI_RUNTIME_CRYPT_PROTOCOL_H__
18 #define __EFI_RUNTIME_CRYPT_PROTOCOL_H__
19 
20 #include <Library/BaseCryptLib.h>
21 
22 ///
23 /// Runtime Cryptographic Protocol GUID.
24 ///
25 #define EFI_RUNTIME_CRYPT_PROTOCOL_GUID \
26   { \
27     0xe1475e0c, 0x1746, 0x4802, { 0x86, 0x2e, 0x1, 0x1c, 0x2c, 0x2d, 0x9d, 0x86 } \
28   }
29 
30 /**
31   Retrieves the size, in bytes, of the context buffer required for SHA-256 operations.
32 
33   @return  The size, in bytes, of the context buffer required for SHA-256 operations.
34 
35 **/
36 typedef
37 UINTN
38 (EFIAPI *EFI_RUNTIME_CRYPT_SHA256_GET_CONTEXT_SIZE) (
39   VOID
40   );
41 
42 
43 /**
44   Initializes user-supplied memory pointed by Sha256Context as SHA-256 hash context for
45   subsequent use.
46 
47   If Sha256Context is NULL, then return FALSE.
48 
49   @param[in, out]  Sha256Context  Pointer to SHA-256 Context being initialized.
50 
51   @retval TRUE   SHA-256 context initialization succeeded.
52   @retval FALSE  SHA-256 context initialization failed.
53 
54 **/
55 typedef
56 BOOLEAN
57 (EFIAPI *EFI_RUNTIME_CRYPT_SHA256_INIT) (
58   IN OUT  VOID  *Sha256Context
59   );
60 
61 
62 /**
63   Performs SHA-256 digest on a data buffer of the specified length. This function can
64   be called multiple times to compute the digest of long or discontinuous data streams.
65 
66   If Sha256Context is NULL, then return FALSE.
67 
68   @param[in, out]  Sha256Context  Pointer to the SHA-256 context.
69   @param[in]       Data           Pointer to the buffer containing the data to be hashed.
70   @param[in]       DataLength     Length of Data buffer in bytes.
71 
72   @retval TRUE   SHA-256 data digest succeeded.
73   @retval FALSE  Invalid SHA-256 context. After Sha256Final function has been called, the
74                  SHA-256 context cannot be reused.
75 
76 **/
77 typedef
78 BOOLEAN
79 (EFIAPI *EFI_RUNTIME_CRYPT_SHA256_UPDATE) (
80   IN OUT  VOID        *Sha256Context,
81   IN      CONST VOID  *Data,
82   IN      UINTN       DataLength
83   );
84 
85 
86 /**
87   Completes SHA-256 hash computation and retrieves the digest value into the specified
88   memory. After this function has been called, the SHA-256 context cannot be used again.
89 
90   If Sha256Context is NULL, then return FALSE.
91   If HashValue is NULL, then return FALSE.
92 
93   @param[in, out]  Sha256Context  Pointer to SHA-256 context
94   @param[out]      HashValue      Pointer to a buffer that receives the SHA-256 digest
95                                   value (32 bytes).
96 
97   @retval TRUE   SHA-256 digest computation succeeded.
98   @retval FALSE  SHA-256 digest computation failed.
99 
100 **/
101 typedef
102 BOOLEAN
103 (EFIAPI *EFI_RUNTIME_CRYPT_SHA256_FINAL) (
104   IN OUT  VOID   *Sha256Context,
105   OUT     UINT8  *HashValue
106   );
107 
108 
109 /**
110   Allocates and Initializes one RSA Context for subsequent use.
111 
112   @return  Pointer to the RSA Context that has been initialized.
113            If the allocations fails, RsaNew() returns NULL.
114 
115 **/
116 typedef
117 VOID *
118 (EFIAPI *EFI_RUNTIME_CRYPT_RSA_NEW) (
119   VOID
120   );
121 
122 /**
123   Release the specified RSA Context.
124 
125   @param[in]  RsaContext  Pointer to the RSA context to be released.
126 
127 **/
128 typedef
129 VOID
130 (EFIAPI *EFI_RUNTIME_CRYPT_RSA_FREE) (
131   IN  VOID  *RsaContext
132   );
133 
134 /**
135   Sets the tag-designated RSA key component into the established RSA context from
136   the user-specified nonnegative integer (octet string format represented in RSA
137   PKCS#1).
138 
139   If RsaContext is NULL, then return FALSE.
140 
141   @param[in, out]  RsaContext  Pointer to RSA context being set.
142   @param[in]       KeyTag      Tag of RSA key component being set.
143   @param[in]       BigNumber   Pointer to octet integer buffer.
144   @param[in]       BnLength    Length of big number buffer in bytes.
145 
146   @return  TRUE   RSA key component was set successfully.
147   @return  FALSE  Invalid RSA key component tag.
148 
149 **/
150 typedef
151 BOOLEAN
152 (EFIAPI *EFI_RUNTIME_CRYPT_RSA_SET_KEY) (
153   IN OUT VOID         *RsaContext,
154   IN     RSA_KEY_TAG  KeyTag,
155   IN     CONST UINT8  *BigNumber,
156   IN     UINTN        BnLength
157   );
158 
159 /**
160   Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in
161   RSA PKCS#1.
162 
163   If RsaContext is NULL, then return FALSE.
164   If MessageHash is NULL, then return FALSE.
165   If Signature is NULL, then return FALSE.
166   If HashLength is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.
167 
168   @param[in]  RsaContext   Pointer to RSA context for signature verification.
169   @param[in]  MessageHash  Pointer to octet message hash to be checked.
170   @param[in]  HashLength   Length of the message hash in bytes.
171   @param[in]  Signature    Pointer to RSA PKCS1-v1_5 signature to be verified.
172   @param[in]  SigLength    Length of signature in bytes.
173 
174   @return  TRUE   Valid signature encoded in PKCS1-v1_5.
175   @return  FALSE  Invalid signature or invalid RSA context.
176 
177 **/
178 typedef
179 BOOLEAN
180 (EFIAPI *EFI_RUNTIME_CRYPT_RSA_PKCS1_VERIFY) (
181   IN  VOID         *RsaContext,
182   IN  CONST UINT8  *MessageHash,
183   IN  UINTN        HashLength,
184   IN  CONST UINT8  *Signature,
185   IN  UINTN        SigLength
186   );
187 
188 ///
189 /// Runtime Cryptographic Protocol Structure.
190 ///
191 typedef struct {
192   EFI_RUNTIME_CRYPT_SHA256_GET_CONTEXT_SIZE  Sha256GetContextSize;
193   EFI_RUNTIME_CRYPT_SHA256_INIT              Sha256Init;
194   EFI_RUNTIME_CRYPT_SHA256_UPDATE            Sha256Update;
195   EFI_RUNTIME_CRYPT_SHA256_FINAL             Sha256Final;
196   EFI_RUNTIME_CRYPT_RSA_NEW                  RsaNew;
197   EFI_RUNTIME_CRYPT_RSA_FREE                 RsaFree;
198   EFI_RUNTIME_CRYPT_RSA_SET_KEY              RsaSetKey;
199   EFI_RUNTIME_CRYPT_RSA_PKCS1_VERIFY         RsaPkcs1Verify;
200 } EFI_RUNTIME_CRYPT_PROTOCOL;
201 
202 extern EFI_GUID gEfiRuntimeCryptProtocolGuid;
203 
204 #endif
205