Home | History | Annotate | Download | only in Hash
      1 /** @file
      2   SHA-1 Digest Wrapper Implementation over OpenSSL.
      3 
      4 Copyright (c) 2009 - 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/sha.h>
     17 
     18 
     19 /**
     20   Retrieves the size, in bytes, of the context buffer required for SHA-1 hash operations.
     21 
     22   @return  The size, in bytes, of the context buffer required for SHA-1 hash operations.
     23 
     24 **/
     25 UINTN
     26 EFIAPI
     27 Sha1GetContextSize (
     28   VOID
     29   )
     30 {
     31   //
     32   // Retrieves OpenSSL SHA Context Size
     33   //
     34   return (UINTN) (sizeof (SHA_CTX));
     35 }
     36 
     37 /**
     38   Initializes user-supplied memory pointed by Sha1Context as SHA-1 hash context for
     39   subsequent use.
     40 
     41   If Sha1Context is NULL, then return FALSE.
     42 
     43   @param[out]  Sha1Context  Pointer to SHA-1 context being initialized.
     44 
     45   @retval TRUE   SHA-1 context initialization succeeded.
     46   @retval FALSE  SHA-1 context initialization failed.
     47 
     48 **/
     49 BOOLEAN
     50 EFIAPI
     51 Sha1Init (
     52   OUT  VOID  *Sha1Context
     53   )
     54 {
     55   //
     56   // Check input parameters.
     57   //
     58   if (Sha1Context == NULL) {
     59     return FALSE;
     60   }
     61 
     62   //
     63   // OpenSSL SHA-1 Context Initialization
     64   //
     65   return (BOOLEAN) (SHA1_Init ((SHA_CTX *) Sha1Context));
     66 }
     67 
     68 /**
     69   Makes a copy of an existing SHA-1 context.
     70 
     71   If Sha1Context is NULL, then return FALSE.
     72   If NewSha1Context is NULL, then return FALSE.
     73 
     74   @param[in]  Sha1Context     Pointer to SHA-1 context being copied.
     75   @param[out] NewSha1Context  Pointer to new SHA-1 context.
     76 
     77   @retval TRUE   SHA-1 context copy succeeded.
     78   @retval FALSE  SHA-1 context copy failed.
     79 
     80 **/
     81 BOOLEAN
     82 EFIAPI
     83 Sha1Duplicate (
     84   IN   CONST VOID  *Sha1Context,
     85   OUT  VOID        *NewSha1Context
     86   )
     87 {
     88   //
     89   // Check input parameters.
     90   //
     91   if (Sha1Context == NULL || NewSha1Context == NULL) {
     92     return FALSE;
     93   }
     94 
     95   CopyMem (NewSha1Context, Sha1Context, sizeof (SHA_CTX));
     96 
     97   return TRUE;
     98 }
     99 
    100 /**
    101   Digests the input data and updates SHA-1 context.
    102 
    103   This function performs SHA-1 digest on a data buffer of the specified size.
    104   It can be called multiple times to compute the digest of long or discontinuous data streams.
    105   SHA-1 context should be already correctly intialized by Sha1Init(), and should not be finalized
    106   by Sha1Final(). Behavior with invalid context is undefined.
    107 
    108   If Sha1Context is NULL, then return FALSE.
    109 
    110   @param[in, out]  Sha1Context  Pointer to the SHA-1 context.
    111   @param[in]       Data         Pointer to the buffer containing the data to be hashed.
    112   @param[in]       DataSize     Size of Data buffer in bytes.
    113 
    114   @retval TRUE   SHA-1 data digest succeeded.
    115   @retval FALSE  SHA-1 data digest failed.
    116 
    117 **/
    118 BOOLEAN
    119 EFIAPI
    120 Sha1Update (
    121   IN OUT  VOID        *Sha1Context,
    122   IN      CONST VOID  *Data,
    123   IN      UINTN       DataSize
    124   )
    125 {
    126   //
    127   // Check input parameters.
    128   //
    129   if (Sha1Context == NULL) {
    130     return FALSE;
    131   }
    132 
    133   //
    134   // Check invalid parameters, in case that only DataLength was checked in OpenSSL
    135   //
    136   if (Data == NULL && DataSize != 0) {
    137     return FALSE;
    138   }
    139 
    140   //
    141   // OpenSSL SHA-1 Hash Update
    142   //
    143   return (BOOLEAN) (SHA1_Update ((SHA_CTX *) Sha1Context, Data, DataSize));
    144 }
    145 
    146 /**
    147   Completes computation of the SHA-1 digest value.
    148 
    149   This function completes SHA-1 hash computation and retrieves the digest value into
    150   the specified memory. After this function has been called, the SHA-1 context cannot
    151   be used again.
    152   SHA-1 context should be already correctly intialized by Sha1Init(), and should not be
    153   finalized by Sha1Final(). Behavior with invalid SHA-1 context is undefined.
    154 
    155   If Sha1Context is NULL, then return FALSE.
    156   If HashValue is NULL, then return FALSE.
    157 
    158   @param[in, out]  Sha1Context  Pointer to the SHA-1 context.
    159   @param[out]      HashValue    Pointer to a buffer that receives the SHA-1 digest
    160                                 value (20 bytes).
    161 
    162   @retval TRUE   SHA-1 digest computation succeeded.
    163   @retval FALSE  SHA-1 digest computation failed.
    164 
    165 **/
    166 BOOLEAN
    167 EFIAPI
    168 Sha1Final (
    169   IN OUT  VOID   *Sha1Context,
    170   OUT     UINT8  *HashValue
    171   )
    172 {
    173   //
    174   // Check input parameters.
    175   //
    176   if (Sha1Context == NULL || HashValue == NULL) {
    177     return FALSE;
    178   }
    179 
    180   //
    181   // OpenSSL SHA-1 Hash Finalization
    182   //
    183   return (BOOLEAN) (SHA1_Final (HashValue, (SHA_CTX *) Sha1Context));
    184 }
    185