]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Cipher/CryptArc4.c
Update CryptoPkg for new ciphers (HMAC, Block Cipher, etc) supports.
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Cipher / CryptArc4.c
1 /** @file
2 ARC4 Wrapper Implementation over OpenSSL.
3
4 Copyright (c) 2010, 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
9
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.
12
13 **/
14
15 #include "InternalCryptLib.h"
16 #include <openssl/rc4.h>
17
18 /**
19 Retrieves the size, in bytes, of the context buffer required for ARC4 operations.
20
21 @return The size, in bytes, of the context buffer required for ARC4 operations.
22
23 **/
24 UINTN
25 EFIAPI
26 Arc4GetContextSize (
27 VOID
28 )
29 {
30 //
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.
34 //
35 return (UINTN) (2 * sizeof(RC4_KEY));
36 }
37
38 /**
39 Initializes user-supplied memory as ARC4 context for subsequent use.
40
41 This function initializes user-supplied memory pointed by Arc4Context as ARC4 context.
42 In addtion, it sets up all ARC4 key materials for subsequent encryption and decryption
43 operations.
44
45 If Arc4Context is NULL, then ASSERT().
46 If Key is NULL, then ASSERT().
47 If KeySize does not in the range of [5, 256] bytes, then ASSERT().
48
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.
52
53 @retval TRUE ARC4 context initialization succeeded.
54 @retval FALSE ARC4 context initialization failed.
55
56 **/
57 BOOLEAN
58 EFIAPI
59 Arc4Init (
60 OUT VOID *Arc4Context,
61 IN CONST UINT8 *Key,
62 IN UINTN KeySize
63 )
64 {
65 RC4_KEY *Rc4Key;
66
67 ASSERT (Arc4Context != NULL);
68 ASSERT (Key != NULL);
69 ASSERT ((KeySize >= 5) && (KeySize <= 256));
70
71 Rc4Key = (RC4_KEY *) Arc4Context;
72
73 RC4_set_key (Rc4Key, (UINT32) KeySize, Key);
74
75 CopyMem (Rc4Key + 1, Rc4Key, sizeof(RC4_KEY));
76
77 return TRUE;
78 }
79
80 /**
81 Performs ARC4 encryption on a data buffer of the specified size.
82
83 This function performs ARC4 encryption on data buffer pointed by Input, of specified
84 size of InputSize.
85 Arc4Context should be already correctly initialized by Arc4Init(). Behavior with
86 invalid ARC4 context is undefined.
87
88 If Arc4Context is NULL, then ASSERT().
89 If Input is NULL, then ASSERT().
90 If Output is NULL, then ASSERT().
91
92 @param[in, out] Arc4Context Pointer to the ARC4 context.
93 @param[in] Input Pointer to the buffer containing the data to be encrypted.
94 @param[in] InputSize Size of the Input buffer in bytes.
95 @param[out] Output Pointer to a buffer that receives the ARC4 encryption output.
96
97 @retval TRUE ARC4 encryption succeeded.
98 @retval FALSE ARC4 encryption failed.
99
100 **/
101 BOOLEAN
102 EFIAPI
103 Arc4Encrypt (
104 IN OUT VOID *Arc4Context,
105 IN CONST UINT8 *Input,
106 IN UINTN InputSize,
107 OUT UINT8 *Output
108 )
109 {
110 RC4_KEY *Rc4Key;
111
112 ASSERT (Arc4Context != NULL);
113 ASSERT (Input != NULL);
114 ASSERT (Output != NULL);
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 ASSERT().
132 If Input is NULL, then ASSERT().
133 If Output is NULL, then ASSERT().
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 ASSERT (Arc4Context != NULL);
156 ASSERT (Input != NULL);
157 ASSERT (Output != NULL);
158
159 Rc4Key = (RC4_KEY *) Arc4Context;
160
161 RC4 (Rc4Key, (UINT32) InputSize, Input, Output);
162
163 return TRUE;
164 }
165
166 /**
167 Resets the ARC4 context to the initial state.
168
169 The function resets the ARC4 context to the state it had immediately after the
170 ARC4Init() function call.
171 Contrary to ARC4Init(), Arc4Reset() requires no secret key as input, but ARC4 context
172 should be already correctly initialized by ARC4Init().
173
174 If Arc4Context is NULL, then ASSERT().
175
176 @param[in, out] Arc4Context Pointer to the ARC4 context.
177
178 @retval TRUE ARC4 reset succeeded.
179 @retval FALSE ARC4 reset failed.
180
181 **/
182 BOOLEAN
183 EFIAPI
184 Arc4Reset (
185 IN OUT VOID *Arc4Context
186 )
187 {
188 RC4_KEY *Rc4Key;
189
190 ASSERT (Arc4Context != NULL);
191
192 Rc4Key = (RC4_KEY *) Arc4Context;
193
194 CopyMem (Rc4Key, Rc4Key + 1, sizeof(RC4_KEY));
195
196 return TRUE;
197 }