Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OvmfPkg: Add C runtime apis referenced by Boringssl #6539

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading