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