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 20, 2024
1 parent 4af5849 commit 3c66dcf
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 27 deletions.
2 changes: 2 additions & 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 Expand Up @@ -99,6 +100,7 @@
PrintLib
UefiBootServicesTableLib
SynchronizationLib
SafeIntLib

[Protocols]
gEfiMpServiceProtocolGuid
Expand Down
2 changes: 2 additions & 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 All @@ -87,6 +88,7 @@
PeiServicesTablePointerLib
PeiServicesLib
SynchronizationLib
SafeIntLib

[Ppis]
gEfiPeiMpServicesPpiGuid
Expand Down
2 changes: 2 additions & 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 All @@ -73,6 +74,7 @@
OpensslLib
IntrinsicLib
PrintLib
SafeIntLib

#
# Remove these [BuildOptions] after this library is cleaned up
Expand Down
2 changes: 2 additions & 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 All @@ -94,6 +95,7 @@
PrintLib
MmServicesTableLib
SynchronizationLib
SafeIntLib

#
# Remove these [BuildOptions] after this library is cleaned up
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;
}
95 changes: 91 additions & 4 deletions CryptoPkg/Library/BaseCryptLib/SysCall/CrtWrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ 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 @@ -580,15 +583,99 @@ fopen (

size_t
fread (
void *b,
size_t c,
size_t i,
FILE *f
void *ptr,
size_t size,
size_t nmemb,
FILE *stream
)
{
return 0;
}

int
fputs (
const char *s,
FILE *stream
)
{
return -1;
}

int
fflush (
FILE *stream
)
{
return -1;
}

int
ferror (
FILE *stream
)
{
return -1;
}

int
fseek (
FILE *stream,
long offset,
int whence
)
{
return -1;
}

int
feof (
FILE *stream
)
{
return -1;
}

int
ftell (
FILE *stream
)
{
return -1;
}

char *
fgets (
char *s,
int size,
FILE *stream
)
{
return NULL;
}

char *
strdup (
char *s
)
{
UINTN Length;
VOID *Buffer;

if (!s) {
return NULL;
}

Length = strlen (s);

Buffer = malloc (Length);
if (Buffer == NULL) {
return NULL;
}

strncpy (Buffer, s, Length);
return Buffer;
}

uid_t
getuid (
void
Expand Down
Loading

0 comments on commit 3c66dcf

Please sign in to comment.