]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | ARC4 Wrapper Implementation over OpenSSL.\r | |
3 | \r | |
4 | Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r | |
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 | |
35 | return (UINTN) (2 * sizeof(RC4_KEY));\r | |
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 | |
42 | In addtion, it sets up all ARC4 key materials for subsequent encryption and decryption\r | |
43 | operations.\r | |
44 | \r | |
45 | If Arc4Context is NULL, then ASSERT().\r | |
46 | If Key is NULL, then ASSERT().\r | |
47 | If KeySize does not in the range of [5, 256] bytes, then ASSERT().\r | |
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 | |
67 | ASSERT (Arc4Context != NULL);\r | |
68 | ASSERT (Key != NULL);\r | |
69 | ASSERT ((KeySize >= 5) && (KeySize <= 256));\r | |
70 | \r | |
71 | Rc4Key = (RC4_KEY *) Arc4Context;\r | |
72 | \r | |
73 | RC4_set_key (Rc4Key, (UINT32) KeySize, Key);\r | |
74 | \r | |
75 | CopyMem (Rc4Key + 1, Rc4Key, sizeof(RC4_KEY));\r | |
76 | \r | |
77 | return TRUE;\r | |
78 | }\r | |
79 | \r | |
80 | /**\r | |
81 | Performs ARC4 encryption on a data buffer of the specified size.\r | |
82 | \r | |
83 | This function performs ARC4 encryption on data buffer pointed by Input, of specified\r | |
84 | size of InputSize.\r | |
85 | Arc4Context should be already correctly initialized by Arc4Init(). Behavior with\r | |
86 | invalid ARC4 context is undefined.\r | |
87 | \r | |
88 | If Arc4Context is NULL, then ASSERT().\r | |
89 | If Input is NULL, then ASSERT().\r | |
90 | If Output is NULL, then ASSERT().\r | |
91 | \r | |
92 | @param[in, out] Arc4Context Pointer to the ARC4 context.\r | |
93 | @param[in] Input Pointer to the buffer containing the data to be encrypted.\r | |
94 | @param[in] InputSize Size of the Input buffer in bytes.\r | |
95 | @param[out] Output Pointer to a buffer that receives the ARC4 encryption output.\r | |
96 | \r | |
97 | @retval TRUE ARC4 encryption succeeded.\r | |
98 | @retval FALSE ARC4 encryption failed.\r | |
99 | \r | |
100 | **/\r | |
101 | BOOLEAN\r | |
102 | EFIAPI\r | |
103 | Arc4Encrypt (\r | |
104 | IN OUT VOID *Arc4Context,\r | |
105 | IN CONST UINT8 *Input,\r | |
106 | IN UINTN InputSize,\r | |
107 | OUT UINT8 *Output\r | |
108 | )\r | |
109 | {\r | |
110 | RC4_KEY *Rc4Key;\r | |
111 | \r | |
112 | ASSERT (Arc4Context != NULL);\r | |
113 | ASSERT (Input != NULL);\r | |
114 | ASSERT (Output != NULL);\r | |
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 | |
131 | If Arc4Context is NULL, then ASSERT().\r | |
132 | If Input is NULL, then ASSERT().\r | |
133 | If Output is NULL, then ASSERT().\r | |
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 | |
155 | ASSERT (Arc4Context != NULL);\r | |
156 | ASSERT (Input != NULL);\r | |
157 | ASSERT (Output != NULL);\r | |
158 | \r | |
159 | Rc4Key = (RC4_KEY *) Arc4Context;\r | |
160 | \r | |
161 | RC4 (Rc4Key, (UINT32) InputSize, Input, Output);\r | |
162 | \r | |
163 | return TRUE;\r | |
164 | }\r | |
165 | \r | |
166 | /**\r | |
167 | Resets the ARC4 context to the initial state.\r | |
168 | \r | |
169 | The function resets the ARC4 context to the state it had immediately after the\r | |
170 | ARC4Init() function call.\r | |
171 | Contrary to ARC4Init(), Arc4Reset() requires no secret key as input, but ARC4 context\r | |
172 | should be already correctly initialized by ARC4Init().\r | |
173 | \r | |
174 | If Arc4Context is NULL, then ASSERT().\r | |
175 | \r | |
176 | @param[in, out] Arc4Context Pointer to the ARC4 context.\r | |
177 | \r | |
178 | @retval TRUE ARC4 reset succeeded.\r | |
179 | @retval FALSE ARC4 reset failed.\r | |
180 | \r | |
181 | **/\r | |
182 | BOOLEAN\r | |
183 | EFIAPI\r | |
184 | Arc4Reset (\r | |
185 | IN OUT VOID *Arc4Context\r | |
186 | )\r | |
187 | {\r | |
188 | RC4_KEY *Rc4Key;\r | |
189 | \r | |
190 | ASSERT (Arc4Context != NULL);\r | |
191 | \r | |
192 | Rc4Key = (RC4_KEY *) Arc4Context;\r | |
193 | \r | |
194 | CopyMem (Rc4Key, Rc4Key + 1, sizeof(RC4_KEY));\r | |
195 | \r | |
196 | return TRUE;\r | |
197 | }\r |