Skip to content

Commit

Permalink
OvmfPkg: Add C runtime apis referenced by Boringssl
Browse files Browse the repository at this point in the history
BoringSSL is a fork of OpenSSL that is designed to meet Google's needs.
As documented at https://boringssl.googlesource.com/boringssl,
Boringssl manages Google's patches to openssl and is used in almost all of
Google products that rely on OpenSSL.
A fork of EDK2 forms the basis for the Uefi code in GCP compute and Google
would like to use Boringssl to work with it.
As part of that effort, there are many C runtime apis that get referenced
and are currently not implemented in EDK2.
This change adds those apis to edk2.
In particular, this change adds the following :
- Stubs for file functions added to CrtWrapper.c. These have been
implemented to return -1 rather than 0 since there is no file system
support. This will ensure that any calls into these apis will return
failure.
- Implementation of (strdup : string duplication function)
- Implementations of the following apis in CrtUtils.c, which are
referenced and used by the Boringssl code :
bsearch : binary search
getentropy : return entropy of requested length

Signed-off-by: Leena Soman <[email protected]>
  • Loading branch information
leenasoman committed Dec 19, 2024
1 parent c624788 commit bccd226
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 82 deletions.
1 change: 1 addition & 0 deletions CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
SysCall/CrtWrapper.c
SysCall/TimerWrapper.c
SysCall/BaseMemAllocation.c
SysCall/CrtUtils.c

[Sources.Ia32]
Rand/CryptRandTsc.c
Expand Down
1 change: 1 addition & 0 deletions CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
SysCall/CrtWrapper.c
SysCall/ConstantTimeClock.c
SysCall/BaseMemAllocation.c
SysCall/CrtUtils.c

[Packages]
MdePkg/MdePkg.dec
Expand Down
1 change: 1 addition & 0 deletions CryptoPkg/Library/BaseCryptLib/SecCryptLib.inf
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
SysCall/CrtWrapper.c
SysCall/ConstantTimeClock.c
SysCall/BaseMemAllocation.c
SysCall/CrtUtils.c

[Packages]
MdePkg/MdePkg.dec
Expand Down
1 change: 1 addition & 0 deletions CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
SysCall/CrtWrapper.c
SysCall/ConstantTimeClock.c
SysCall/BaseMemAllocation.c
SysCall/CrtUtils.c

[Sources.Ia32]
Rand/CryptRandTsc.c
Expand Down
92 changes: 92 additions & 0 deletions CryptoPkg/Library/BaseCryptLib/SysCall/CrtUtils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/** @file
C Run-Time Libraries (CRT) Utility apis for BoringSSL-based
Cryptographic Library.
Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <CrtLibSupport.h>
#include <Uefi/UefiBaseType.h>
#include <Library/RngLib.h>
#include <Library/SafeIntLib.h>

/* Performs a binary search */
void *
bsearch (
const void *key,
const void *base,
size_t nmemb,
size_t size,
int ( *compar )(const void *, const void *)
)
{
void *Mid;
int Sign;
RETURN_STATUS Status = RETURN_INVALID_PARAMETER;
size_t Result;

if (!key || !base || !nmemb || !size) {
return NULL;
}

Status = SafeUintnMult ((UINTN)size, (UINTN)(nmemb/2), (UINTN *)&Result);

if ((Status == RETURN_BUFFER_TOO_SMALL) ||
(Status == RETURN_INVALID_PARAMETER))
{
return NULL;
}

while (nmemb > 0) {
Mid = (char *)base + size * (nmemb/2);
Sign = compar (key, Mid);
if (Sign < 0) {
nmemb /= 2;
} else if (Sign > 0) {
base = (char *)Mid + size;
nmemb -= nmemb/2 + 1;
} else {
return Mid;
}
}

return NULL;
}

/* Returns entropy of requested length in provided buffer */
int
getentropy (
void *buffer,
size_t length
)
{
UINT8 *EntropyBuffer = (UINT8 *)buffer;
UINTN Index;
UINT64 RandNum;
UINTN CopyLength;

if (length > GETENTROPY_MAX) {
errno = EIO;
return -1;
}

if (EntropyBuffer == NULL) {
errno = EFAULT;
return -1;
}

for (Index = 0; Index < length; Index += sizeof (UINT64)) {
if (!GetRandomNumber64 (&RandNum)) {
errno = ENOSYS;
return -1;
}

CopyLength =
(length - Index >= sizeof (UINT64)) ? sizeof (UINT64) : (length - Index);
CopyMem (EntropyBuffer + Index, &RandNum, CopyLength);
}

return 0;
}
82 changes: 0 additions & 82 deletions CryptoPkg/Library/BaseCryptLib/SysCall/CrtWrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <CrtLibSupport.h>
#include <Uefi/UefiBaseType.h>
#include <Library/RngLib.h>
#include <Library/SafeIntLib.h>

int errno = 0;

Expand Down Expand Up @@ -676,85 +673,6 @@ strdup (
return Buffer;
}

/* Performs a binary search */
void *
bsearch (
const void *key,
const void *base,
size_t nmemb,
size_t size,
int ( *compar )(const void *, const void *)
)
{
void *Mid;
int Sign;
RETURN_STATUS Status = RETURN_INVALID_PARAMETER;
size_t Result;

if (!key || !base || !nmemb || !size) {
return NULL;
}

Status = SafeUintnMult ((UINTN)size, (UINTN)(nmemb/2), (UINTN *)&Result);

if ((Status == RETURN_BUFFER_TOO_SMALL) ||
(Status == RETURN_INVALID_PARAMETER))
{
return NULL;
}

while (nmemb > 0) {
Mid = (char *)base + size * (nmemb/2);
Sign = compar (key, Mid);
if (Sign < 0) {
nmemb /= 2;
} else if (Sign > 0) {
base = (char *)Mid + size;
nmemb -= nmemb/2 + 1;
} else {
return Mid;
}
}

return NULL;
}

/* Returns entropy of requested length in provided buffer */
int
getentropy (
void *buffer,
size_t length
)
{
UINT8 *EntropyBuffer = (UINT8 *)buffer;
UINTN Index;
UINT64 RandNum;
UINTN CopyLength;

if (length > GETENTROPY_MAX) {
errno = EIO;
return -1;
}

if (EntropyBuffer == NULL) {
errno = EFAULT;
return -1;
}

for (Index = 0; Index < length; Index += sizeof (UINT64)) {
if (!GetRandomNumber64 (&RandNum)) {
errno = ENOSYS;
return -1;
}

CopyLength =
(length - Index >= sizeof (UINT64)) ? sizeof (UINT64) : (length - Index);
CopyMem (EntropyBuffer + Index, &RandNum, CopyLength);
}

return 0;
}

uid_t
getuid (
void
Expand Down

0 comments on commit bccd226

Please sign in to comment.