CryptArc4.c 5.3 KB

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