Skip to content

Commit b893aa9

Browse files
committed
Merged PR 10406838: Add OpenSSL implementation for XtsAes test
## Description: We add OpenSSL as submodule to 3rdparty and link symcryptunittest to it so we can compare the SymCrypt implementation. We add perf and functional test for XtsAes as well. ## Admin Checklist: - [ ] You have updated documentation in symcrypt.h to reflect any changes in behavior - [ ] You have updated CHANGELOG.md to reflect any changes in behavior - [ ] You have updated symcryptunittest to exercise any new functionality - [ ] If you have introduced any symbols in symcrypt.h you have updated production and test dynamic export symbols (exports.ver / exports.def / symcrypt.src) and tested the updated dynamic modules with symcryptunittest - [ ] If you have introduced functionality that varies based on CPU features, you have manually tested with and without relevant features - [ ] If you have made significant changes to a particular algorithm, you have checked that performance numbers reported by symcryptunittest are in line with expectations - [ ] If you have added new algorithms/modes, you have updated the status indicator text for the associated modules if necessary Related work items: #49347468
1 parent f70f82b commit b893aa9

25 files changed

+697
-108
lines changed

.pipelines/OneBranch.PullRequest.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ variables:
1616
WindowsContainerImage: 'onebranch.azurecr.io/windows/ltsc2019/vse2022@sha256:a3083215a4675bad774ec88005dcf57436b3423584d0db3e82b6de3f780213ba'
1717

1818
resources:
19-
repositories:
19+
repositories:
2020
- repository: templates
2121
type: git
2222
name: OneBranch.Pipelines/GovernedTemplates
@@ -55,7 +55,7 @@ extends:
5555
inputs:
5656
system: 'Custom'
5757
customVersion: '$(verStep.VER_MAJOR).$(verStep.VER_MINOR).$(verStep.VER_PATCH)-$(Build.BuildId)'
58-
58+
5959
- stage: Build_Windows
6060
displayName: Build Windows
6161
jobs:
@@ -103,44 +103,50 @@ extends:
103103
config: 'Debug'
104104
cc: 'gcc'
105105
cxx: 'g++'
106+
openssl: true
106107
- template: .pipelines/templates/build-linux.yml@self
107108
parameters:
108109
arch: 'AMD64'
109110
config: 'Sanitize'
110111
cc: 'gcc'
111112
cxx: 'g++'
113+
openssl: true
112114
- template: .pipelines/templates/build-linux.yml@self
113115
parameters:
114116
arch: 'AMD64'
115117
config: 'Release'
116118
cc: 'gcc'
117-
cxx: 'g++'
119+
cxx: 'g++'
120+
openssl: true
118121
- template: .pipelines/templates/build-linux.yml@self
119122
parameters:
120123
arch: 'AMD64'
121124
config: 'Debug'
122125
cc: 'clang'
123126
cxx: 'clang++'
127+
openssl: true
124128
- template: .pipelines/templates/build-linux.yml@self
125129
parameters:
126130
arch: 'AMD64'
127131
config: 'Sanitize'
128132
cc: 'clang'
129133
cxx: 'clang++'
134+
openssl: true
130135
- template: .pipelines/templates/build-linux.yml@self
131136
parameters:
132137
arch: 'AMD64'
133138
config: 'Release'
134139
cc: 'clang'
135140
cxx: 'clang++'
141+
openssl: true
136142
- template: .pipelines/templates/build-linux.yml@self
137143
parameters:
138144
arch: 'AMD64'
139145
config: 'Release'
140146
cc: 'gcc'
141147
cxx: 'g++'
142148
skipTests: true
143-
additionalArgs: '--no-asm'
149+
additionalArgs: '--no-asm --openssl'
144150
identifier: 'NoAsm'
145151
- template: .pipelines/templates/build-linux.yml@self
146152
parameters:
@@ -149,7 +155,7 @@ extends:
149155
cc: 'clang'
150156
cxx: 'clang++'
151157
skipTests: true
152-
additionalArgs: '--no-asm'
158+
additionalArgs: '--no-asm --openssl'
153159
identifier: 'NoAsm'
154160
- template: .pipelines/templates/build-linux.yml@self
155161
parameters:

.pipelines/templates/build-linux.yml

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ parameters:
3131
- name: identifier # Additional identifier for job name
3232
type: string
3333
default: ''
34+
- name: openssl # Build with OpenSSL
35+
type: boolean
36+
default: false
3437

3538
jobs:
3639
- job: Linux_${{ parameters.arch}}_${{ parameters.cc }}_${{parameters.config}}_${{parameters.identifier}}
@@ -46,39 +49,47 @@ jobs:
4649
verbose_build_flag: '--verbose'
4750
${{ else }}:
4851
verbose_build_flag: ''
52+
${{ if eq(parameters.openssl, true) }}:
53+
openssl_build_flag: '--openssl'
54+
${{ else }}:
55+
openssl_build_flag: ''
4956

5057
steps:
58+
- script: |
59+
apt-get update -y
60+
displayName: 'Update package manager'
5161
5262
- ${{ if eq(parameters.cc, 'clang') }}:
5363
# Install clang-11 and manually add symlinks so that it can be run via just "clang" rather
5464
# than clang-11. This is required because we can't explicitly specify clang-11 for
5565
# parameters.cc (it breaks the job name, because job names aren't allowed to have '-' in them)
5666
- script: |
57-
apt-get update
5867
apt-get install -y clang-11
5968
ln -s /usr/bin/clang-11 /usr/bin/clang
6069
ln -s /usr/bin/clang++-11 /usr/bin/clang++
6170
displayName: 'Install clang'
6271
6372
- ${{ if eq(parameters.arch, 'X86') }}:
6473
- script: |
65-
apt-get update
6674
apt-get install -y gcc-multilib g++-multilib
6775
displayName: 'Install x86 headers and libraries'
6876
6977
# Note: this assumes that the pipeline is always running on an AMD64 machine. When we have
7078
# native ARM64 pipelines, we'll need to change this.
7179
- ${{ if eq(parameters.arch, 'ARM64') }}:
7280
- script: |
73-
apt-get update
7481
apt-get install -y binutils-aarch64-linux-gnu gcc-aarch64-linux-gnu g++-aarch64-linux-gnu qemu-user
7582
displayName: 'Install arm64 cross-compilation tools'
7683
7784
- ${{ if eq(parameters.arch, 'ARM') }}:
7885
- script: |
79-
apt-get update
8086
apt-get install -y gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf qemu-user
81-
displayName: 'Install arm64 cross-compilation tools'
87+
displayName: 'Install arm cross-compilation tools'
88+
89+
- ${{ if eq(parameters.openssl, true) }}:
90+
- script: |
91+
apt-get install -y libssl-dev
92+
displayName: 'Install OpenSSL'
8293
8394
- task: PipAuthenticate@1
8495
inputs:
@@ -93,14 +104,14 @@ jobs:
93104
inputs:
94105
scriptSource: 'filePath'
95106
scriptPath: scripts/build.py
96-
arguments: 'cmake bin --arch ${{ parameters.arch }} --config ${{ parameters.config }} --cc ${{ parameters.cc }} --cxx ${{ parameters.cxx }} ${{ parameters.additionalArgs }} $(verbose_build_flag)'
107+
arguments: 'cmake bin --arch ${{ parameters.arch }} --config ${{ parameters.config }} --cc ${{ parameters.cc }} --cxx ${{ parameters.cxx }} ${{ parameters.additionalArgs }} $(verbose_build_flag) $(openssl_build_flag)'
97108
workingDirectory: $(Build.SourcesDirectory)
98-
109+
99110
# Overwrite default artifact publishing with our copy (enables publishing binaries for failed runs)
100111
- script: |
101112
cp .artifactignore "$(ob_outputDirectory)/.artifactignore"
102113
displayName: 'Overwrite .artifactignore'
103-
114+
104115
- ${{ if ne(parameters.skipTests, true) }}:
105116
- ${{ if ne(parameters.arch, 'ARM64') }}:
106117
- ${{ if ne(parameters.arch, 'ARM') }}:
@@ -146,7 +157,7 @@ jobs:
146157
scriptPath: scripts/test.py
147158
arguments: '--emulator qemu-aarch64 --emulator-lib-dir /usr/aarch64-linux-gnu/ bin dynamic:bin/module/generic/libsymcrypt.so noperftests +symcrypt -dh -dsa -rsa'
148159
workingDirectory: $(Build.SourcesDirectory)
149-
160+
150161
- ${{ if eq(parameters.arch, 'ARM') }}:
151162
- task: PythonScript@0
152163
displayName: 'Run unit tests'
@@ -170,4 +181,4 @@ jobs:
170181
scriptSource: 'filePath'
171182
scriptPath: scripts/package.py
172183
arguments: 'bin ${{ parameters.arch }} ${{ parameters.config }} generic bin'
173-
workingDirectory: $(Build.SourcesDirectory)
184+
workingDirectory: $(Build.SourcesDirectory)

BUILD.md

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Linux modules with FIPS integrity checks.
6060
* `-DSYMCRYPT_TARGET_ARCH=<AMD64|X86|ARM64>` to choose a target architecture. If not specified, it will default to the host system architecture.
6161
* To cross-compile for Windows X86 from Windows AMD64, you must also use `-A Win32`
6262
* To cross-compile for Linux ARM64, you must also use `--toolchain=cmake-configs/Toolchain-Clang-ARM64.cmake`
63-
* `-DSYMCRYPT_USE_ASM=<ON|OFF>` to choose whether to use assembly optimizations. Defaults to `ON`.
63+
* `-DSYMCRYPT_USE_ASM=<ON|OFF>` to choose whether to use assembly optimizations. Defaults to `ON`.
6464
* `-DSYMCRYPT_FIPS_BUILD=<ON|OFF>` to choose whether to enable FIPS self-tests in the SymCrypt shared object module. Defaults to `ON`. Currently only affects Linux builds.
6565
* For a release build, specify `-DCMAKE_BUILD_TYPE=RelWithDebInfo`
6666
1. `cmake --build bin`
@@ -85,19 +85,39 @@ outputs are placed in this directory.
8585

8686
Requires the following packages on debian-based systems to build:
8787
```
88-
apt-get -y install --no-install-recommends \
88+
apt update
89+
apt -y install --no-install-recommends \
8990
cmake \
90-
python3-pyelftools \ # integrity
91-
gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf # for arm
91+
python3-pyelftools \
92+
gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf
9293
```
93-
94+
`python3-pyelftools` is for integrity verification and `gcc-arm-linux-gnueabihf` `g++-arm-linux-gnueabihf` are for ARM cross compile
9495
And for running the test:
9596
```
96-
apt-get -y install --no-install-recommends qemu-user
97+
apt -y install --no-install-recommends qemu-user
9798
```
9899

99100
To build and test for example for arm:
100101
```
101102
python3 scripts/build.py cmake --arch arm --toolchain cmake-configs/Toolchain-GCC-ARM.cmake bin_arm
102103
qemu-arm -L /usr/arm-linux-gnueabihf/ ./bin_arm/exe/symcryptunittest -rsa -dsa -dh -ec -int -mod dynamic:bin_arm/module/generic/libsymcrypt.so
103-
```
104+
```
105+
106+
## Performance comparison with OpenSSL
107+
`symcryptunittest.exe` can be used to compare and measure performance of algorithms provided by SymCrypt and OpenSSL. On Windows `symcryptunittest.exe` would have to be compiled with OpenSSL. `nasm` and `strawberryperl` are prerequisites to building OpenSSL.
108+
```
109+
winget install nasm strawberryperl
110+
.\scripts\build.py cmake bin --config Release --openssl-build-from-source
111+
```
112+
113+
And on Linux we can use OpenSSL installed by system's package manager.
114+
```
115+
sudo apt install -y libssl-dev
116+
./scripts/build.py cmake bin --config Release --openssl
117+
```
118+
119+
To build OpenSSL on Linux we need to install following prerequisites.
120+
```
121+
sudo apt install -y nasm perl
122+
.\scripts\build.py cmake bin --config Release --openssl-build-from-source
123+
```

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ New changes will be listed here as they are developed. The version number is det
44
prior to the creation of a new release, based on the changes contained in that release.
55

66
- Add SymCryptEntropyAccumulator to Windows kernel module
7+
- Fix tweak lower 64 bit overflow calculation in SYMCRYPT_XtsAesXxx
8+
- Add OpenSSL implementation for XtsAes to symcryptunittest
79

810
# Version 103.4.1
911
- Add retpoline guard flags for undocked Windows build

CMakeLists.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ option(
5858
SYMCRYPT_TEST_LEGACY_IMPL
5959
"When enabled, the SymCrypt unit tests will be linked against and configured to run compatibility and performance tests on the legacy
6060
RSA32 and msbignum cryptographic implementations. This requires access to private static libraries which are not licensed
61-
for use outside of Windows, and thus can only be used by Microsoft employees building internally. It only affects the unit tests,
61+
for use outside of Windows, and thus can only be used by Microsoft employees building internally. It only affects the unit tests,
6262
and does not change the functionality of the SymCrypt library itself."
6363
OFF)
6464

@@ -74,10 +74,20 @@ option(
7474
ON, but may need to be disabled for specialized targets such as embedded systems."
7575
ON)
7676

77+
option(
78+
SYMCRYPT_TEST_WITH_OPENSSL
79+
"When enabled, the SymCrypt unit tests will be linked against and configured to run performance comparisons against OpenSSL
80+
implementations of certain algorithms."
81+
OFF)
82+
83+
84+
if (SYMCRYPT_TEST_WITH_OPENSSL)
85+
include(${CMAKE_SOURCE_DIR}/cmake-configs/OpenSSL.cmake)
86+
endif()
7787
include(${CMAKE_SOURCE_DIR}/cmake-configs/SymCrypt-Platforms.cmake)
7888

7989
if(NOT DEFINED CMAKE_BUILD_TYPE)
80-
set(CMAKE_BUILD_TYPE Debug)
90+
set(CMAKE_BUILD_TYPE Debug)
8191
endif()
8292

8393
set(CMAKE_C_STANDARD 11)

cmake-configs/OpenSSL.cmake

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
if(OPENSSL_BUILD_FROM_SOURCE)
2+
if(CMAKE_BUILD_TYPE MATCHES "Release|RelWithDebInfo")
3+
set(OPENSSL_BUILD_TYPE release)
4+
else()
5+
set(OPENSSL_BUILD_TYPE debug)
6+
endif()
7+
if(NOT DEFINED OPENSSL_BUILD_BRANCH)
8+
set(OPENSSL_BUILD_BRANCH_SUFFIX "")
9+
else()
10+
set(OPENSSL_BUILD_BRANCH_SUFFIX -${OPENSSL_BUILD_BRANCH})
11+
endif()
12+
13+
set(OPENSSL_BUILD_ROOT ${CMAKE_SOURCE_DIR}/3rdparty/openssl-${OPENSSL_BUILD_TYPE}${OPENSSL_BUILD_BRANCH_SUFFIX})
14+
15+
if(NOT IS_DIRECTORY "${OPENSSL_BUILD_ROOT}" OR NOT EXISTS "${OPENSSL_BUILD_ROOT}/Configure")
16+
execute_process(
17+
COMMAND git clone https://github.com/openssl/openssl.git ${OPENSSL_BUILD_ROOT}
18+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
19+
if(NOT OPENSSL_BUILD_BRANCH STREQUAL "")
20+
execute_process(
21+
COMMAND git checkout ${OPENSSL_BUILD_BRANCH}
22+
WORKING_DIRECTORY ${OPENSSL_BUILD_ROOT})
23+
endif()
24+
endif()
25+
26+
if(NOT EXISTS "${OPENSSL_BUILD_ROOT}/OpenSSLConfig.cmake")
27+
set(ENV{LANG} C)
28+
set(ENV{LC_ALL} C)
29+
set(ENV{CL} /MP)
30+
if(OPENSSL_BUILD_TYPE STREQUAL "release")
31+
execute_process(
32+
COMMAND perl Configure no-ssl no-tls1 no-tls1_1 --release
33+
WORKING_DIRECTORY ${OPENSSL_BUILD_ROOT}
34+
RESULT_VARIABLE result)
35+
else()
36+
execute_process(
37+
COMMAND perl Configure no-ssl no-tls1 no-tls1_1 --debug
38+
WORKING_DIRECTORY ${OPENSSL_BUILD_ROOT}
39+
RESULT_VARIABLE result)
40+
endif()
41+
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
42+
cmake_host_system_information(RESULT J
43+
QUERY NUMBER_OF_LOGICAL_CORES)
44+
45+
execute_process(
46+
COMMAND make -j ${J}
47+
WORKING_DIRECTORY ${OPENSSL_BUILD_ROOT}
48+
RESULT_VARIABLE result)
49+
elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
50+
execute_process(
51+
COMMAND nmake
52+
WORKING_DIRECTORY ${OPENSSL_BUILD_ROOT}
53+
RESULT_VARIABLE result)
54+
else()
55+
message(FATAL_ERROR "Unsupported platform")
56+
endif()
57+
endif()
58+
59+
# If you don't static link, symcryptunittest.exe may use C:\WINDOWS\SYSTEM32\libcrypto-3-x64.dll
60+
set(OPENSSL_USE_STATIC_LIBS True)
61+
62+
include(${OPENSSL_BUILD_ROOT}/OpenSSLConfig.cmake)
63+
else()
64+
find_package(OpenSSL REQUIRED)
65+
endif()
66+
67+
message("Found OpenSSL include directory ${OPENSSL_INCLUDE_DIR}")
68+
include_directories(${OPENSSL_INCLUDE_DIR})
69+
link_directories(${OPENSSL_LIBRARY_DIR})
70+
link_libraries(${OPENSSL_CRYPTO_LIBRARIES})
71+
add_compile_options(-DINCLUDE_IMPL_OPENSSL=1)

lib/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ function(process_cppasm filepath outformat archdefine)
163163
# assume masm => MSVC C compiler
164164
add_custom_command(
165165
OUTPUT ${output_asm}
166-
COMMAND "${CMAKE_C_COMPILER}" /EP /P /Fi${output_asm} ${filepath}
166+
COMMAND "${CMAKE_C_COMPILER}" /nologo /EP /P /Fi${output_asm} ${filepath}
167167
-I${CMAKE_CURRENT_SOURCE_DIR} -I${CMAKE_CURRENT_SOURCE_DIR}/${rootpath} -I${CMAKE_SOURCE_DIR}/inc
168168
-DSYMCRYPT_${outformatupper} -DSYMCRYPT_CPU_${archdefineupper} ${dbg_definition}
169169
MAIN_DEPENDENCY ${filepath}

lib/xtsaes_pattern.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ SYMCRYPT_XtsAesXxx(
5757
// bOverflow=FALSE allows backwards compatibility with old API which wrapped around at 64-bits
5858
SYMCRYPT_ASSERT( tweakLow64 < N_PARALLEL_TWEAKS );
5959

60-
// Increment tweakHigh64 and store new value in high half of the previous tweakLow64+1 tweaks
60+
// Increment tweakHigh64 and store new value in high half of the previous tweakLow64 tweaks
6161
tweakHigh64++;
62-
for( i=0; i<=tweakLow64; i++)
62+
for( i=0; i<tweakLow64; i++)
6363
{
6464
SYMCRYPT_STORE_LSBFIRST64(&tweakBuf[tweakBytes - (16*i) - 8], tweakHigh64);
6565
}

modules/linux/common/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ if (CMAKE_C_COMPILER_ID MATCHES "Clang" AND DEFINED CMAKE_C_COMPILER_TARGET)
1919
set(jitter_ldflags "--target=${CMAKE_C_COMPILER_TARGET}")
2020
endif()
2121

22+
if(NOT IS_DIRECTORY "${CMAKE_SOURCE_DIR}/3rdparty/jitterentropy-library" OR NOT EXISTS "${CMAKE_SOURCE_DIR}/3rdparty/jitterentropy-library/Makefile")
23+
execute_process(
24+
COMMAND git submodule update --init -- 3rdparty/jitterentropy-library
25+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
26+
endif()
27+
2228
add_custom_target(jitterentropy_lib ALL
2329
COMMAND make clean
2430
COMMAND ${CMAKE_COMMAND} -E
@@ -27,4 +33,4 @@ add_custom_target(jitterentropy_lib ALL
2733
env "LDFLAGS=${jitter_ldflags}"
2834
make
2935
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/3rdparty/jitterentropy-library
30-
)
36+
)

0 commit comments

Comments
 (0)