Commit b2d0e06f authored by Matthew Garrett's avatar Matthew Garrett

Add crypto libraries

parent 71d0b18b
This diff is collapsed.
/** @file
ARC4 Wrapper Implementation over OpenSSL.
Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "InternalCryptLib.h"
#include <openssl/rc4.h>
/**
Retrieves the size, in bytes, of the context buffer required for ARC4 operations.
@return The size, in bytes, of the context buffer required for ARC4 operations.
**/
UINTN
EFIAPI
Arc4GetContextSize (
VOID
)
{
//
// Memory for 2 copies of RC4_KEY is allocated, one for working copy, and the other
// for backup copy. When Arc4Reset() is called, we can use the backup copy to restore
// the working copy to the initial state.
//
return (UINTN) (2 * sizeof(RC4_KEY));
}
/**
Initializes user-supplied memory as ARC4 context for subsequent use.
This function initializes user-supplied memory pointed by Arc4Context as ARC4 context.
In addtion, it sets up all ARC4 key materials for subsequent encryption and decryption
operations.
If Arc4Context is NULL, then return FALSE.
If Key is NULL, then return FALSE.
If KeySize does not in the range of [5, 256] bytes, then return FALSE.
@param[out] Arc4Context Pointer to ARC4 context being initialized.
@param[in] Key Pointer to the user-supplied ARC4 key.
@param[in] KeySize Size of ARC4 key in bytes.
@retval TRUE ARC4 context initialization succeeded.
@retval FALSE ARC4 context initialization failed.
**/
BOOLEAN
EFIAPI
Arc4Init (
OUT VOID *Arc4Context,
IN CONST UINT8 *Key,
IN UINTN KeySize
)
{
RC4_KEY *Rc4Key;
//
// Check input parameters.
//
if (Arc4Context == NULL || Key == NULL || (KeySize < 5 || KeySize > 256)) {
return FALSE;
}
Rc4Key = (RC4_KEY *) Arc4Context;
RC4_set_key (Rc4Key, (UINT32) KeySize, Key);
CopyMem (Rc4Key + 1, Rc4Key, sizeof(RC4_KEY));
return TRUE;
}
/**
Performs ARC4 encryption on a data buffer of the specified size.
This function performs ARC4 encryption on data buffer pointed by Input, of specified
size of InputSize.
Arc4Context should be already correctly initialized by Arc4Init(). Behavior with
invalid ARC4 context is undefined.
If Arc4Context is NULL, then return FALSE.
If Input is NULL, then return FALSE.
If Output is NULL, then return FALSE.
@param[in, out] Arc4Context Pointer to the ARC4 context.
@param[in] Input Pointer to the buffer containing the data to be encrypted.
@param[in] InputSize Size of the Input buffer in bytes.
@param[out] Output Pointer to a buffer that receives the ARC4 encryption output.
@retval TRUE ARC4 encryption succeeded.
@retval FALSE ARC4 encryption failed.
**/
BOOLEAN
EFIAPI
Arc4Encrypt (
IN OUT VOID *Arc4Context,
IN CONST UINT8 *Input,
IN UINTN InputSize,
OUT UINT8 *Output
)
{
RC4_KEY *Rc4Key;
//
// Check input parameters.
//
if (Arc4Context == NULL || Input == NULL || Output == NULL) {
return FALSE;
}
Rc4Key = (RC4_KEY *) Arc4Context;
RC4 (Rc4Key, (UINT32) InputSize, Input, Output);
return TRUE;
}
/**
Performs ARC4 decryption on a data buffer of the specified size.
This function performs ARC4 decryption on data buffer pointed by Input, of specified
size of InputSize.
Arc4Context should be already correctly initialized by Arc4Init(). Behavior with
invalid ARC4 context is undefined.
If Arc4Context is NULL, then return FALSE.
If Input is NULL, then return FALSE.
If Output is NULL, then return FALSE.
@param[in, out] Arc4Context Pointer to the ARC4 context.
@param[in] Input Pointer to the buffer containing the data to be decrypted.
@param[in] InputSize Size of the Input buffer in bytes.
@param[out] Output Pointer to a buffer that receives the ARC4 decryption output.
@retval TRUE ARC4 decryption succeeded.
@retval FALSE ARC4 decryption failed.
**/
BOOLEAN
EFIAPI
Arc4Decrypt (
IN OUT VOID *Arc4Context,
IN UINT8 *Input,
IN UINTN InputSize,
OUT UINT8 *Output
)
{
RC4_KEY *Rc4Key;
//
// Check input parameters.
//
if (Arc4Context == NULL || Input == NULL || Output == NULL) {
return FALSE;
}
Rc4Key = (RC4_KEY *) Arc4Context;
RC4 (Rc4Key, (UINT32) InputSize, Input, Output);
return TRUE;
}
/**
Resets the ARC4 context to the initial state.
The function resets the ARC4 context to the state it had immediately after the
ARC4Init() function call.
Contrary to ARC4Init(), Arc4Reset() requires no secret key as input, but ARC4 context
should be already correctly initialized by ARC4Init().
If Arc4Context is NULL, then return FALSE.
@param[in, out] Arc4Context Pointer to the ARC4 context.
@retval TRUE ARC4 reset succeeded.
@retval FALSE ARC4 reset failed.
**/
BOOLEAN
EFIAPI
Arc4Reset (
IN OUT VOID *Arc4Context
)
{
RC4_KEY *Rc4Key;
//
// Check input parameters.
//
if (Arc4Context == NULL) {
return FALSE;
}
Rc4Key = (RC4_KEY *) Arc4Context;
CopyMem (Rc4Key, Rc4Key + 1, sizeof(RC4_KEY));
return TRUE;
}
This diff is collapsed.
/** @file
MD4 Digest Wrapper Implementation over OpenSSL.
Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "InternalCryptLib.h"
#include <openssl/md4.h>
/**
Retrieves the size, in bytes, of the context buffer required for MD4 hash operations.
@return The size, in bytes, of the context buffer required for MD4 hash operations.
**/
UINTN
EFIAPI
Md4GetContextSize (
VOID
)
{
//
// Retrieves the OpenSSL MD4 Context Size
//
return (UINTN)(sizeof (MD4_CTX));
}
/**
Initializes user-supplied memory pointed by Md4Context as MD4 hash context for
subsequent use.
If Md4Context is NULL, then return FALSE.
@param[out] Md4Context Pointer to MD4 context being initialized.
@retval TRUE MD4 context initialization succeeded.
@retval FALSE MD4 context initialization failed.
**/
BOOLEAN
EFIAPI
Md4Init (
OUT VOID *Md4Context
)
{
//
// Check input parameters.
//
if (Md4Context == NULL) {
return FALSE;
}
//
// OpenSSL MD4 Context Initialization
//
return (BOOLEAN) (MD4_Init ((MD4_CTX *)Md4Context));
}
/**
Makes a copy of an existing MD4 context.
If Md4Context is NULL, then return FALSE.
If NewMd4Context is NULL, then return FALSE.
@param[in] Md4Context Pointer to MD4 context being copied.
@param[out] NewMd4Context Pointer to new MD4 context.
@retval TRUE MD4 context copy succeeded.
@retval FALSE MD4 context copy failed.
**/
BOOLEAN
EFIAPI
Md4Duplicate (
IN CONST VOID *Md4Context,
OUT VOID *NewMd4Context
)
{
//
// Check input parameters.
//
if (Md4Context == NULL || NewMd4Context == NULL) {
return FALSE;
}
CopyMem (NewMd4Context, Md4Context, sizeof (MD4_CTX));
return TRUE;
}
/**
Digests the input data and updates MD4 context.
This function performs MD4 digest on a data buffer of the specified size.
It can be called multiple times to compute the digest of long or discontinuous data streams.
MD4 context should be already correctly intialized by Md4Init(), and should not be finalized
by Md4Final(). Behavior with invalid context is undefined.
If Md4Context is NULL, then return FALSE.
@param[in, out] Md4Context Pointer to the MD4 context.
@param[in] Data Pointer to the buffer containing the data to be hashed.
@param[in] DataSize Size of Data buffer in bytes.
@retval TRUE MD4 data digest succeeded.
@retval FALSE MD4 data digest failed.
**/
BOOLEAN
EFIAPI
Md4Update (
IN OUT VOID *Md4Context,
IN CONST VOID *Data,
IN UINTN DataSize
)
{
//
// Check input parameters.
//
if (Md4Context == NULL) {
return FALSE;
}
//
// Check invalid parameters, in case that only DataLength was checked in OpenSSL
//
if (Data == NULL && DataSize != 0) {
return FALSE;
}
//
// OpenSSL MD4 Hash Update
//
return (BOOLEAN) (MD4_Update ((MD4_CTX *)Md4Context, Data, DataSize));
}
/**
Completes computation of the MD4 digest value.
This function completes MD4 hash computation and retrieves the digest value into
the specified memory. After this function has been called, the MD4 context cannot
be used again.
MD4 context should be already correctly intialized by Md4Init(), and should not be
finalized by Md4Final(). Behavior with invalid MD4 context is undefined.
If Md4Context is NULL, then return FALSE.
If HashValue is NULL, then return FALSE.
@param[in, out] Md4Context Pointer to the MD4 context.
@param[out] HashValue Pointer to a buffer that receives the MD4 digest
value (16 bytes).
@retval TRUE MD4 digest computation succeeded.
@retval FALSE MD4 digest computation failed.
**/
BOOLEAN
EFIAPI
Md4Final (
IN OUT VOID *Md4Context,
OUT UINT8 *HashValue
)
{
//
// Check input parameters.
//
if (Md4Context == NULL || HashValue == NULL) {
return FALSE;
}
//
// OpenSSL MD4 Hash Finalization
//
return (BOOLEAN) (MD4_Final (HashValue, (MD4_CTX *)Md4Context));
}
/** @file
MD5 Digest Wrapper Implementation over OpenSSL.
Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "InternalCryptLib.h"
#include <openssl/md5.h>
/**
Retrieves the size, in bytes, of the context buffer required for MD5 hash operations.
@return The size, in bytes, of the context buffer required for MD5 hash operations.
**/
UINTN
EFIAPI
Md5GetContextSize (
VOID
)
{
//
// Retrieves the OpenSSL MD5 Context Size
//
return (UINTN)(sizeof (MD5_CTX));
}