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