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