Home | History | Annotate | Download | only in Cipher
      1 /** @file
      2   ARC4 Wrapper Implementation over OpenSSL.
      3 
      4 Copyright (c) 2010 - 2012, 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 addition, it sets up all ARC4 key materials for subsequent encryption and decryption
     43   operations.
     44 
     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.
     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   //
     68   // Check input parameters.
     69   //
     70   if (Arc4Context == NULL || Key == NULL || (KeySize < 5 || KeySize > 256)) {
     71     return FALSE;
     72   }
     73 
     74   Rc4Key = (RC4_KEY *) Arc4Context;
     75 
     76   RC4_set_key (Rc4Key, (UINT32) KeySize, Key);
     77 
     78   CopyMem (Rc4Key +  1, Rc4Key, sizeof (RC4_KEY));
     79 
     80   return TRUE;
     81 }
     82 
     83 /**
     84   Performs ARC4 encryption on a data buffer of the specified size.
     85 
     86   This function performs ARC4 encryption on data buffer pointed by Input, of specified
     87   size of InputSize.
     88   Arc4Context should be already correctly initialized by Arc4Init(). Behavior with
     89   invalid ARC4 context is undefined.
     90 
     91   If Arc4Context is NULL, then return FALSE.
     92   If Input is NULL, then return FALSE.
     93   If Output is NULL, then return FALSE.
     94 
     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.
     99 
    100   @retval TRUE   ARC4 encryption succeeded.
    101   @retval FALSE  ARC4 encryption failed.
    102 
    103 **/
    104 BOOLEAN
    105 EFIAPI
    106 Arc4Encrypt (
    107   IN OUT  VOID         *Arc4Context,
    108   IN      CONST UINT8  *Input,
    109   IN      UINTN        InputSize,
    110   OUT     UINT8        *Output
    111   )
    112 {
    113   RC4_KEY  *Rc4Key;
    114 
    115   //
    116   // Check input parameters.
    117   //
    118   if (Arc4Context == NULL || Input == NULL || Output == NULL || InputSize > INT_MAX) {
    119     return FALSE;
    120   }
    121 
    122   Rc4Key = (RC4_KEY *) Arc4Context;
    123 
    124   RC4 (Rc4Key, (UINT32) InputSize, Input, Output);
    125 
    126   return TRUE;
    127 }
    128 
    129 /**
    130   Performs ARC4 decryption on a data buffer of the specified size.
    131 
    132   This function performs ARC4 decryption on data buffer pointed by Input, of specified
    133   size of InputSize.
    134   Arc4Context should be already correctly initialized by Arc4Init(). Behavior with
    135   invalid ARC4 context is undefined.
    136 
    137   If Arc4Context is NULL, then return FALSE.
    138   If Input is NULL, then return FALSE.
    139   If Output is NULL, then return FALSE.
    140 
    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.
    145 
    146   @retval TRUE   ARC4 decryption succeeded.
    147   @retval FALSE  ARC4 decryption failed.
    148 
    149 **/
    150 BOOLEAN
    151 EFIAPI
    152 Arc4Decrypt (
    153   IN OUT  VOID   *Arc4Context,
    154   IN      UINT8  *Input,
    155   IN      UINTN  InputSize,
    156   OUT     UINT8  *Output
    157   )
    158 {
    159   RC4_KEY  *Rc4Key;
    160 
    161   //
    162   // Check input parameters.
    163   //
    164   if (Arc4Context == NULL || Input == NULL || Output == NULL || InputSize > INT_MAX) {
    165     return FALSE;
    166   }
    167 
    168   Rc4Key = (RC4_KEY *) Arc4Context;
    169 
    170   RC4 (Rc4Key, (UINT32) InputSize, Input, Output);
    171 
    172   return TRUE;
    173 }
    174 
    175 /**
    176   Resets the ARC4 context to the initial state.
    177 
    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().
    182 
    183   If Arc4Context is NULL, then return FALSE.
    184 
    185   @param[in, out]  Arc4Context  Pointer to the ARC4 context.
    186 
    187   @retval TRUE   ARC4 reset succeeded.
    188   @retval FALSE  ARC4 reset failed.
    189 
    190 **/
    191 BOOLEAN
    192 EFIAPI
    193 Arc4Reset (
    194   IN OUT  VOID  *Arc4Context
    195   )
    196 {
    197   RC4_KEY  *Rc4Key;
    198 
    199   //
    200   // Check input parameters.
    201   //
    202   if (Arc4Context == NULL) {
    203     return FALSE;
    204   }
    205 
    206   Rc4Key = (RC4_KEY *) Arc4Context;
    207 
    208   CopyMem (Rc4Key, Rc4Key + 1, sizeof (RC4_KEY));
    209 
    210   return TRUE;
    211 }
    212