]>
git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Cipher/CryptArc4.c
2 ARC4 Wrapper Implementation over OpenSSL.
4 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "InternalCryptLib.h"
16 #include <openssl/rc4.h>
19 Retrieves the size, in bytes, of the context buffer required for ARC4 operations.
21 @return The size, in bytes, of the context buffer required for ARC4 operations.
31 // Memory for 2 copies of RC4_KEY is allocated, one for working copy, and the other
32 // for backup copy. When Arc4Reset() is called, we can use the backup copy to restore
33 // the working copy to the initial state.
35 return (UINTN
) (2 * sizeof (RC4_KEY
));
39 Initializes user-supplied memory as ARC4 context for subsequent use.
41 This function initializes user-supplied memory pointed by Arc4Context as ARC4 context.
42 In addition, it sets up all ARC4 key materials for subsequent encryption and decryption
45 If Arc4Context is NULL, then return FALSE.
46 If Key is NULL, then return FALSE.
47 If KeySize does not in the range of [5, 256] bytes, then return FALSE.
49 @param[out] Arc4Context Pointer to ARC4 context being initialized.
50 @param[in] Key Pointer to the user-supplied ARC4 key.
51 @param[in] KeySize Size of ARC4 key in bytes.
53 @retval TRUE ARC4 context initialization succeeded.
54 @retval FALSE ARC4 context initialization failed.
60 OUT VOID
*Arc4Context
,
68 // Check input parameters.
70 if (Arc4Context
== NULL
|| Key
== NULL
|| (KeySize
< 5 || KeySize
> 256)) {
74 Rc4Key
= (RC4_KEY
*) Arc4Context
;
76 RC4_set_key (Rc4Key
, (UINT32
) KeySize
, Key
);
78 CopyMem (Rc4Key
+ 1, Rc4Key
, sizeof (RC4_KEY
));
84 Performs ARC4 encryption on a data buffer of the specified size.
86 This function performs ARC4 encryption on data buffer pointed by Input, of specified
88 Arc4Context should be already correctly initialized by Arc4Init(). Behavior with
89 invalid ARC4 context is undefined.
91 If Arc4Context is NULL, then return FALSE.
92 If Input is NULL, then return FALSE.
93 If Output is NULL, then return FALSE.
95 @param[in, out] Arc4Context Pointer to the ARC4 context.
96 @param[in] Input Pointer to the buffer containing the data to be encrypted.
97 @param[in] InputSize Size of the Input buffer in bytes.
98 @param[out] Output Pointer to a buffer that receives the ARC4 encryption output.
100 @retval TRUE ARC4 encryption succeeded.
101 @retval FALSE ARC4 encryption failed.
107 IN OUT VOID
*Arc4Context
,
108 IN CONST UINT8
*Input
,
116 // Check input parameters.
118 if (Arc4Context
== NULL
|| Input
== NULL
|| Output
== NULL
|| InputSize
> INT_MAX
) {
122 Rc4Key
= (RC4_KEY
*) Arc4Context
;
124 RC4 (Rc4Key
, (UINT32
) InputSize
, Input
, Output
);
130 Performs ARC4 decryption on a data buffer of the specified size.
132 This function performs ARC4 decryption on data buffer pointed by Input, of specified
134 Arc4Context should be already correctly initialized by Arc4Init(). Behavior with
135 invalid ARC4 context is undefined.
137 If Arc4Context is NULL, then return FALSE.
138 If Input is NULL, then return FALSE.
139 If Output is NULL, then return FALSE.
141 @param[in, out] Arc4Context Pointer to the ARC4 context.
142 @param[in] Input Pointer to the buffer containing the data to be decrypted.
143 @param[in] InputSize Size of the Input buffer in bytes.
144 @param[out] Output Pointer to a buffer that receives the ARC4 decryption output.
146 @retval TRUE ARC4 decryption succeeded.
147 @retval FALSE ARC4 decryption failed.
153 IN OUT VOID
*Arc4Context
,
162 // Check input parameters.
164 if (Arc4Context
== NULL
|| Input
== NULL
|| Output
== NULL
|| InputSize
> INT_MAX
) {
168 Rc4Key
= (RC4_KEY
*) Arc4Context
;
170 RC4 (Rc4Key
, (UINT32
) InputSize
, Input
, Output
);
176 Resets the ARC4 context to the initial state.
178 The function resets the ARC4 context to the state it had immediately after the
179 ARC4Init() function call.
180 Contrary to ARC4Init(), Arc4Reset() requires no secret key as input, but ARC4 context
181 should be already correctly initialized by ARC4Init().
183 If Arc4Context is NULL, then return FALSE.
185 @param[in, out] Arc4Context Pointer to the ARC4 context.
187 @retval TRUE ARC4 reset succeeded.
188 @retval FALSE ARC4 reset failed.
194 IN OUT VOID
*Arc4Context
200 // Check input parameters.
202 if (Arc4Context
== NULL
) {
206 Rc4Key
= (RC4_KEY
*) Arc4Context
;
208 CopyMem (Rc4Key
, Rc4Key
+ 1, sizeof (RC4_KEY
));