Skip to content

Commit

Permalink
Restore IntrinsicLib to CryptoPkg (microsoft#712)
Browse files Browse the repository at this point in the history
## Description

Some silicon code is making use of the CryptoPkg's Intrinsic lib. 

Intrinsiclib was removed in afd1f31.

Adding the functionality back to allow silicon code to use the library
while a better long term solution is created.

- [ ] Impacts functionality?
- **Functionality** - Does the change ultimately impact how firmware
functions?
- Examples: Add a new library, publish a new PPI, update an algorithm,
...
- [ ] Impacts security?
- **Security** - Does the change have a direct security impact on an
application,
    flow, or firmware?
  - Examples: Crypto algorithm change, buffer overflow fix, parameter
    validation improvement, ...
- [ ] Breaking change?
- **Breaking change** - Will anyone consuming this change experience a
break
    in build or boot behavior?
- Examples: Add a new library class, move a module to a different repo,
call
    a function in a new library class in a pre-existing module, ...
- [ ] Includes tests?
  - **Tests** - Does the change include any explicit test code?
  - Examples: Unit tests, integration tests, robot tests, ...
- [ ] Includes documentation?
- **Documentation** - Does the change contain explicit documentation
additions
    outside direct code modifications (and comments)?
- Examples: Update readme file, add feature readme file, link to
documentation
    on an a separate Web page, ...

## How This Was Tested

## Integration Instructions
N/A
  • Loading branch information
apop5 authored and kenlautner committed Feb 14, 2024
1 parent d4cc801 commit 6884dc0
Show file tree
Hide file tree
Showing 20 changed files with 1,411 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CryptoPkg/CryptoPkg.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
UefiApplicationEntryPoint|MdePkg/Library/UefiApplicationEntryPoint/UefiApplicationEntryPoint.inf
PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf
MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf
IntrinsicLib|CryptoPkg/Library/IntrinsicLib/IntrinsicLib.inf
SynchronizationLib|MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf
TimerLib|MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.inf
SafeIntLib|MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf
Expand Down Expand Up @@ -197,6 +198,7 @@

CryptoPkg/Library/HmacSha1Lib/HmacSha1LibNull.inf
CryptoPkg/Library/BaseCryptLibNull/BaseCryptLibNull.inf
CryptoPkg/Library/IntrinsicLib/IntrinsicLib.inf
CryptoPkg/Library/TlsLibNull/TlsLibNull.inf
CryptoPkg/Library/BaseHashApiLib/BaseHashApiLib.inf

Expand Down
16 changes: 16 additions & 0 deletions CryptoPkg/Library/IntrinsicLib/BaseIntrinsicLib.uni
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// /** @file
// Intrinsic Routines Wrapper Library Instance.
//
// This module is intrinsic routines wrapper library instance.
//
// Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
//
// SPDX-License-Identifier: BSD-2-Clause-Patent
//
// **/


#string STR_MODULE_ABSTRACT #language en-US "Intrinsic Routines Wrapper Library Instance"

#string STR_MODULE_DESCRIPTION #language en-US "This module is intrinsic routines wrapper library instance."

47 changes: 47 additions & 0 deletions CryptoPkg/Library/IntrinsicLib/CopyMem.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/** @file
Intrinsic Memory Routines Wrapper Implementation for OpenSSL-based
Cryptographic Library.
Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <Base.h>
#include <Library/BaseMemoryLib.h>

#if defined (__clang__) && !defined (__APPLE__)

/* Copies bytes between buffers */
static __attribute__ ((__used__))
void *
__memcpy (
void *dest,
const void *src,
unsigned int count
)
{
return CopyMem (dest, src, (UINTN)count);
}

__attribute__ ((__alias__ ("__memcpy")))
void *
memcpy (
void *dest,
const void *src,
unsigned int count
);

#else
/* Copies bytes between buffers */
void *
memcpy (
void *dest,
const void *src,
unsigned int count
)
{
return CopyMem (dest, src, (UINTN)count);
}

#endif
23 changes: 23 additions & 0 deletions CryptoPkg/Library/IntrinsicLib/Ia32/MathDivModU64x64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/** @file
64-bit Math Worker Function.
The 32-bit versions of C compiler generate calls to library routines
to handle 64-bit math. These functions use non-standard calling conventions.
Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <Library/BaseLib.h>

/* https://gcc.gnu.org/onlinedocs/gccint/Integer-library-routines.html */
__attribute__ ((__used__))
unsigned long long
__udivmoddi4 (
unsigned long long A,
unsigned long long B,
unsigned long long *C
)
{
return DivU64x64Remainder ((UINT64)A, (UINT64)B, (UINT64 *)C);
}
22 changes: 22 additions & 0 deletions CryptoPkg/Library/IntrinsicLib/Ia32/MathDivS64x64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/** @file
64-bit Math Worker Function.
The 32-bit versions of C compiler generate calls to library routines
to handle 64-bit math. These functions use non-standard calling conventions.
Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <Library/BaseLib.h>

/* https://gcc.gnu.org/onlinedocs/gccint/Integer-library-routines.html */
__attribute__ ((__used__))
long long
__divdi3 (
long long A,
long long B
)
{
return DivS64x64Remainder ((INT64)A, (INT64)B, NULL);
}
22 changes: 22 additions & 0 deletions CryptoPkg/Library/IntrinsicLib/Ia32/MathDivU64x64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/** @file
64-bit Math Worker Function.
The 32-bit versions of C compiler generate calls to library routines
to handle 64-bit math. These functions use non-standard calling conventions.
Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <Library/BaseLib.h>

/* https://gcc.gnu.org/onlinedocs/gccint/Integer-library-routines.html */
__attribute__ ((__used__))
unsigned long long
__udivdi3 (
unsigned long long A,
unsigned long long B
)
{
return DivU64x64Remainder ((UINT64)A, (UINT64)B, NULL);
}
37 changes: 37 additions & 0 deletions CryptoPkg/Library/IntrinsicLib/Ia32/MathFtol.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/** @file
64-bit Math Worker Function.
The 32-bit versions of C compiler generate calls to library routines
to handle 64-bit math. These functions use non-standard calling conventions.
Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

/*
* Floating point to integer conversion.
*/
__declspec(naked) void
_ftol2 (
void
)
{
_asm {
fistp qword ptr [esp-8]
mov edx, [esp-4]
mov eax, [esp-8]
ret
}
}

__declspec(naked) void
_ftol2_sse (
void
)
{
_asm {
fistp dword ptr [esp-4]
mov eax,[esp-4]
ret
}
}
51 changes: 51 additions & 0 deletions CryptoPkg/Library/IntrinsicLib/Ia32/MathLShiftS64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/** @file
64-bit Math Worker Function.
The 32-bit versions of C compiler generate calls to library routines
to handle 64-bit math. These functions use non-standard calling conventions.
Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

/*
* Shifts a 64-bit signed value left by a particular number of bits.
*/
__declspec(naked) void __cdecl
_allshl (
void
)
{
_asm {
;
; Handle shifting of 64 or more bits (return 0)
;

cmp cl, 64
jae short ReturnZero

;
; Handle shifting of between 0 and 31 bits
;
cmp cl, 32
jae short More32
shld edx, eax, cl
shl eax, cl
ret

;
; Handle shifting of between 32 and 63 bits
;
More32:
mov edx, eax
xor eax, eax
and cl, 31
shl edx, cl
ret

ReturnZero:
xor eax,eax
xor edx,edx
ret
}
}
42 changes: 42 additions & 0 deletions CryptoPkg/Library/IntrinsicLib/Ia32/MathLShiftS64.nasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
;------------------------------------------------------------------------------
;
; Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
; SPDX-License-Identifier: BSD-2-Clause-Patent
;
; Module Name:
;
; MathLShiftS64.nasm
;
; Abstract:
;
; 64-bit Math Worker Function.
; Shifts a 64-bit signed value left by a certain number of bits.
;
;------------------------------------------------------------------------------

SECTION .text

global ASM_PFX(__ashldi3)
;------------------------------------------------------------------------------
;
; void __cdecl __ashldi3 (void)
;
;------------------------------------------------------------------------------
ASM_PFX(__ashldi3):
cmp cl,0x40
jnc ReturnZero
cmp cl,0x20
jnc More32
shld edx,eax,cl
shl eax,cl
ret
More32:
mov edx,eax
xor eax,eax
and cl,0x1f
shl edx,cl
ret
ReturnZero:
xor eax,eax
xor edx,edx
ret
Loading

0 comments on commit 6884dc0

Please sign in to comment.