]>
Commit | Line | Data |
---|---|---|
a8c44645 | 1 | /** @file\r |
2 | ARC4 Wrapper Implementation over OpenSSL.\r | |
3 | \r | |
16d2c32c | 4 | Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r |
a8c44645 | 5 | This program and the accompanying materials\r |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | #include "InternalCryptLib.h"\r | |
16 | #include <openssl/rc4.h>\r | |
17 | \r | |
18 | /**\r | |
19 | Retrieves the size, in bytes, of the context buffer required for ARC4 operations.\r | |
20 | \r | |
21 | @return The size, in bytes, of the context buffer required for ARC4 operations.\r | |
22 | \r | |
23 | **/\r | |
24 | UINTN\r | |
25 | EFIAPI\r | |
26 | Arc4GetContextSize (\r | |
27 | VOID\r | |
28 | )\r | |
29 | {\r | |
30 | //\r | |
31 | // Memory for 2 copies of RC4_KEY is allocated, one for working copy, and the other\r | |
32 | // for backup copy. When Arc4Reset() is called, we can use the backup copy to restore\r | |
33 | // the working copy to the initial state.\r | |
34 | //\r | |
6b8ebcb8 | 35 | return (UINTN) (2 * sizeof (RC4_KEY));\r |
a8c44645 | 36 | }\r |
37 | \r | |
38 | /**\r | |
39 | Initializes user-supplied memory as ARC4 context for subsequent use.\r | |
40 | \r | |
41 | This function initializes user-supplied memory pointed by Arc4Context as ARC4 context.\r | |
6b8ebcb8 | 42 | In addition, it sets up all ARC4 key materials for subsequent encryption and decryption\r |
a8c44645 | 43 | operations.\r |
44 | \r | |
16d2c32c | 45 | If Arc4Context is NULL, then return FALSE.\r |
46 | If Key is NULL, then return FALSE.\r | |
47 | If KeySize does not in the range of [5, 256] bytes, then return FALSE.\r | |
a8c44645 | 48 | \r |
49 | @param[out] Arc4Context Pointer to ARC4 context being initialized.\r | |
50 | @param[in] Key Pointer to the user-supplied ARC4 key.\r | |
51 | @param[in] KeySize Size of ARC4 key in bytes.\r | |
52 | \r | |
53 | @retval TRUE ARC4 context initialization succeeded.\r | |
54 | @retval FALSE ARC4 context initialization failed.\r | |
55 | \r | |
56 | **/\r | |
57 | BOOLEAN\r | |
58 | EFIAPI\r | |
59 | Arc4Init (\r | |
60 | OUT VOID *Arc4Context,\r | |
61 | IN CONST UINT8 *Key,\r | |
62 | IN UINTN KeySize\r | |
63 | )\r | |
64 | {\r | |
65 | RC4_KEY *Rc4Key;\r | |
66 | \r | |
16d2c32c | 67 | //\r |
68 | // Check input parameters.\r | |
69 | // \r | |
70 | if (Arc4Context == NULL || Key == NULL || (KeySize < 5 || KeySize > 256)) {\r | |
71 | return FALSE;\r | |
72 | }\r | |
a8c44645 | 73 | \r |
74 | Rc4Key = (RC4_KEY *) Arc4Context;\r | |
75 | \r | |
76 | RC4_set_key (Rc4Key, (UINT32) KeySize, Key);\r | |
77 | \r | |
6b8ebcb8 | 78 | CopyMem (Rc4Key + 1, Rc4Key, sizeof (RC4_KEY));\r |
a8c44645 | 79 | \r |
80 | return TRUE;\r | |
81 | }\r | |
82 | \r | |
83 | /**\r | |
84 | Performs ARC4 encryption on a data buffer of the specified size.\r | |
85 | \r | |
86 | This function performs ARC4 encryption on data buffer pointed by Input, of specified\r | |
87 | size of InputSize.\r | |
88 | Arc4Context should be already correctly initialized by Arc4Init(). Behavior with\r | |
89 | invalid ARC4 context is undefined.\r | |
90 | \r | |
16d2c32c | 91 | If Arc4Context is NULL, then return FALSE.\r |
92 | If Input is NULL, then return FALSE.\r | |
93 | If Output is NULL, then return FALSE.\r | |
a8c44645 | 94 | \r |
95 | @param[in, out] Arc4Context Pointer to the ARC4 context.\r | |
96 | @param[in] Input Pointer to the buffer containing the data to be encrypted.\r | |
97 | @param[in] InputSize Size of the Input buffer in bytes.\r | |
98 | @param[out] Output Pointer to a buffer that receives the ARC4 encryption output.\r | |
99 | \r | |
100 | @retval TRUE ARC4 encryption succeeded.\r | |
101 | @retval FALSE ARC4 encryption failed.\r | |
102 | \r | |
103 | **/\r | |
104 | BOOLEAN\r | |
105 | EFIAPI\r | |
106 | Arc4Encrypt (\r | |
107 | IN OUT VOID *Arc4Context,\r | |
108 | IN CONST UINT8 *Input,\r | |
109 | IN UINTN InputSize,\r | |
110 | OUT UINT8 *Output\r | |
111 | )\r | |
112 | {\r | |
113 | RC4_KEY *Rc4Key;\r | |
114 | \r | |
16d2c32c | 115 | //\r |
116 | // Check input parameters.\r | |
117 | //\r | |
dda39f3a | 118 | if (Arc4Context == NULL || Input == NULL || Output == NULL || InputSize > INT_MAX) {\r |
16d2c32c | 119 | return FALSE;\r |
120 | }\r | |
a8c44645 | 121 | \r |
122 | Rc4Key = (RC4_KEY *) Arc4Context;\r | |
123 | \r | |
124 | RC4 (Rc4Key, (UINT32) InputSize, Input, Output);\r | |
125 | \r | |
126 | return TRUE;\r | |
127 | }\r | |
128 | \r | |
129 | /**\r | |
130 | Performs ARC4 decryption on a data buffer of the specified size.\r | |
131 | \r | |
132 | This function performs ARC4 decryption on data buffer pointed by Input, of specified\r | |
133 | size of InputSize.\r | |
134 | Arc4Context should be already correctly initialized by Arc4Init(). Behavior with\r | |
135 | invalid ARC4 context is undefined.\r | |
136 | \r | |
16d2c32c | 137 | If Arc4Context is NULL, then return FALSE.\r |
138 | If Input is NULL, then return FALSE.\r | |
139 | If Output is NULL, then return FALSE.\r | |
a8c44645 | 140 | \r |
141 | @param[in, out] Arc4Context Pointer to the ARC4 context.\r | |
142 | @param[in] Input Pointer to the buffer containing the data to be decrypted.\r | |
143 | @param[in] InputSize Size of the Input buffer in bytes.\r | |
144 | @param[out] Output Pointer to a buffer that receives the ARC4 decryption output.\r | |
145 | \r | |
146 | @retval TRUE ARC4 decryption succeeded.\r | |
147 | @retval FALSE ARC4 decryption failed.\r | |
148 | \r | |
149 | **/\r | |
150 | BOOLEAN\r | |
151 | EFIAPI\r | |
152 | Arc4Decrypt (\r | |
153 | IN OUT VOID *Arc4Context,\r | |
154 | IN UINT8 *Input,\r | |
155 | IN UINTN InputSize,\r | |
156 | OUT UINT8 *Output\r | |
157 | )\r | |
158 | {\r | |
159 | RC4_KEY *Rc4Key;\r | |
160 | \r | |
16d2c32c | 161 | //\r |
162 | // Check input parameters.\r | |
163 | //\r | |
dda39f3a | 164 | if (Arc4Context == NULL || Input == NULL || Output == NULL || InputSize > INT_MAX) {\r |
16d2c32c | 165 | return FALSE;\r |
166 | }\r | |
a8c44645 | 167 | \r |
168 | Rc4Key = (RC4_KEY *) Arc4Context;\r | |
169 | \r | |
170 | RC4 (Rc4Key, (UINT32) InputSize, Input, Output);\r | |
171 | \r | |
172 | return TRUE;\r | |
173 | }\r | |
174 | \r | |
175 | /**\r | |
176 | Resets the ARC4 context to the initial state.\r | |
177 | \r | |
178 | The function resets the ARC4 context to the state it had immediately after the\r | |
179 | ARC4Init() function call.\r | |
180 | Contrary to ARC4Init(), Arc4Reset() requires no secret key as input, but ARC4 context\r | |
181 | should be already correctly initialized by ARC4Init().\r | |
182 | \r | |
16d2c32c | 183 | If Arc4Context is NULL, then return FALSE.\r |
a8c44645 | 184 | \r |
185 | @param[in, out] Arc4Context Pointer to the ARC4 context.\r | |
186 | \r | |
187 | @retval TRUE ARC4 reset succeeded.\r | |
188 | @retval FALSE ARC4 reset failed.\r | |
189 | \r | |
190 | **/\r | |
191 | BOOLEAN\r | |
192 | EFIAPI\r | |
193 | Arc4Reset (\r | |
194 | IN OUT VOID *Arc4Context\r | |
195 | )\r | |
196 | {\r | |
197 | RC4_KEY *Rc4Key;\r | |
198 | \r | |
16d2c32c | 199 | //\r |
200 | // Check input parameters.\r | |
201 | //\r | |
202 | if (Arc4Context == NULL) {\r | |
203 | return FALSE;\r | |
204 | }\r | |
205 | \r | |
a8c44645 | 206 | Rc4Key = (RC4_KEY *) Arc4Context;\r |
207 | \r | |
6b8ebcb8 | 208 | CopyMem (Rc4Key, Rc4Key + 1, sizeof (RC4_KEY));\r |
a8c44645 | 209 | \r |
210 | return TRUE;\r | |
211 | }\r |