Skip to content

Commit bcfaf03

Browse files
authored
Merge pull request #7026 from Frauschi/liboqs
Improve liboqs integration
2 parents 52db533 + 7e60b02 commit bcfaf03

19 files changed

Lines changed: 242 additions & 18 deletions

File tree

cmake/functions.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ function(generate_build_flags)
198198
set(BUILD_SPHINCS "yes" PARENT_SCOPE)
199199
set(BUILD_DILITHIUM "yes" PARENT_SCOPE)
200200
set(BUILD_EXT_KYBER "yes" PARENT_SCOPE)
201+
set(BUILD_OQS_HELPER "yes" PARENT_SCOPE)
201202
endif()
202203
if(WOLFSSL_ARIA OR WOLFSSL_USER_SETTINGS)
203204
message(STATUS "ARIA functions.cmake found WOLFSSL_ARIA")
@@ -587,6 +588,11 @@ function(generate_lib_src_list LIB_SOURCES)
587588
wolfcrypt/src/wc_port.c
588589
wolfcrypt/src/error.c)
589590

591+
if(BUILD_OQS_HELPER)
592+
list(APPEND LIB_SOURCES
593+
wolfcrypt/src/port/liboqs/liboqs.c)
594+
endif()
595+
590596
if(BUILD_ARIA)
591597
list(APPEND LIB_SOURCES
592598
wolfcrypt/src/port/aria/aria-crypt.c

src/include.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@ src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/falcon.c
835835
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/dilithium.c
836836
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/sphincs.c
837837
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/ext_kyber.c
838+
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/port/liboqs/liboqs.c
838839
endif
839840

840841
if BUILD_LIBLMS

src/tls13.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8911,7 +8911,7 @@ static int SendTls13CertificateVerify(WOLFSSL* ssl)
89118911
ret = wc_falcon_sign_msg(args->sigData, args->sigDataSz,
89128912
args->verify + HASH_SIG_SIZE +
89138913
VERIFY_HEADER, (word32*)&sig->length,
8914-
(falcon_key*)ssl->hsKey);
8914+
(falcon_key*)ssl->hsKey, ssl->rng);
89158915
args->length = (word16)sig->length;
89168916
}
89178917
#endif
@@ -8920,7 +8920,7 @@ static int SendTls13CertificateVerify(WOLFSSL* ssl)
89208920
ret = wc_dilithium_sign_msg(args->sigData, args->sigDataSz,
89218921
args->verify + HASH_SIG_SIZE +
89228922
VERIFY_HEADER, (word32*)&sig->length,
8923-
(dilithium_key*)ssl->hsKey);
8923+
(dilithium_key*)ssl->hsKey, ssl->rng);
89248924
args->length = (word16)sig->length;
89258925
}
89268926
#endif

wolfcrypt/benchmark/benchmark.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11791,7 +11791,7 @@ void bench_falconKeySign(byte level)
1179111791
x = FALCON_LEVEL5_SIG_SIZE;
1179211792
}
1179311793

11794-
ret = wc_falcon_sign_msg(msg, sizeof(msg), sig, &x, &key);
11794+
ret = wc_falcon_sign_msg(msg, sizeof(msg), sig, &x, &key, GLOBAL_RNG);
1179511795
if (ret != 0) {
1179611796
printf("wc_falcon_sign_msg failed\n");
1179711797
}
@@ -11912,7 +11912,7 @@ void bench_dilithiumKeySign(byte level)
1191211912
x = DILITHIUM_LEVEL5_SIG_SIZE;
1191311913
}
1191411914

11915-
ret = wc_dilithium_sign_msg(msg, sizeof(msg), sig, &x, &key);
11915+
ret = wc_dilithium_sign_msg(msg, sizeof(msg), sig, &x, &key, GLOBAL_RNG);
1191611916
if (ret != 0) {
1191711917
printf("wc_dilithium_sign_msg failed\n");
1191811918
}
@@ -12058,7 +12058,7 @@ void bench_sphincsKeySign(byte level, byte optim)
1205812058
x = SPHINCS_SMALL_LEVEL5_SIG_SIZE;
1205912059
}
1206012060

12061-
ret = wc_sphincs_sign_msg(msg, sizeof(msg), sig, &x, &key);
12061+
ret = wc_sphincs_sign_msg(msg, sizeof(msg), sig, &x, &key, GLOBAL_RNG);
1206212062
if (ret != 0) {
1206312063
printf("wc_sphincs_sign_msg failed\n");
1206412064
}

wolfcrypt/src/asn.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28901,7 +28901,7 @@ static int MakeSignature(CertSignCtx* certSignCtx, const byte* buf, word32 sz,
2890128901
#if defined(HAVE_FALCON)
2890228902
if (!rsaKey && !eccKey && !ed25519Key && !ed448Key && falconKey) {
2890328903
word32 outSz = sigSz;
28904-
ret = wc_falcon_sign_msg(buf, sz, sig, &outSz, falconKey);
28904+
ret = wc_falcon_sign_msg(buf, sz, sig, &outSz, falconKey, rng);
2890528905
if (ret == 0)
2890628906
ret = outSz;
2890728907
}
@@ -28910,7 +28910,7 @@ static int MakeSignature(CertSignCtx* certSignCtx, const byte* buf, word32 sz,
2891028910
if (!rsaKey && !eccKey && !ed25519Key && !ed448Key && !falconKey &&
2891128911
dilithiumKey) {
2891228912
word32 outSz = sigSz;
28913-
ret = wc_dilithium_sign_msg(buf, sz, sig, &outSz, dilithiumKey);
28913+
ret = wc_dilithium_sign_msg(buf, sz, sig, &outSz, dilithiumKey, rng);
2891428914
if (ret == 0)
2891528915
ret = outSz;
2891628916
}
@@ -28919,7 +28919,7 @@ static int MakeSignature(CertSignCtx* certSignCtx, const byte* buf, word32 sz,
2891928919
if (!rsaKey && !eccKey && !ed25519Key && !ed448Key && !falconKey &&
2892028920
!dilithiumKey && sphincsKey) {
2892128921
word32 outSz = sigSz;
28922-
ret = wc_sphincs_sign_msg(buf, sz, sig, &outSz, sphincsKey);
28922+
ret = wc_sphincs_sign_msg(buf, sz, sig, &outSz, sphincsKey, rng);
2892328923
if (ret == 0)
2892428924
ret = outSz;
2892528925
}

wolfcrypt/src/dilithium.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
*/
6060
int wc_dilithium_sign_msg(const byte* in, word32 inLen,
6161
byte* out, word32 *outLen,
62-
dilithium_key* key)
62+
dilithium_key* key, WC_RNG* rng)
6363
{
6464
int ret = 0;
6565
#ifdef HAVE_LIBOQS
@@ -107,6 +107,10 @@ int wc_dilithium_sign_msg(const byte* in, word32 inLen,
107107
localOutLen = *outLen;
108108
}
109109

110+
if (ret == 0) {
111+
ret = wolfSSL_liboqsRngMutexLock(rng);
112+
}
113+
110114
if ((ret == 0) &&
111115
(OQS_SIG_sign(oqssig, out, &localOutLen, in, inLen, key->k)
112116
== OQS_ERROR)) {
@@ -117,6 +121,8 @@ int wc_dilithium_sign_msg(const byte* in, word32 inLen,
117121
*outLen = (word32)localOutLen;
118122
}
119123

124+
wolfSSL_liboqsRngMutexUnlock();
125+
120126
if (oqssig != NULL) {
121127
OQS_SIG_free(oqssig);
122128
}

wolfcrypt/src/ext_kyber.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939

4040
#if defined (HAVE_LIBOQS)
4141

42+
#include <wolfssl/wolfcrypt/port/liboqs/liboqs.h>
43+
4244
static const char* OQS_ID2name(int id) {
4345
switch (id) {
4446
case KYBER_LEVEL1: return OQS_KEM_alg_kyber_512;
@@ -337,12 +339,16 @@ int wc_KyberKey_MakeKey(KyberKey* key, WC_RNG* rng)
337339
ret = BAD_FUNC_ARG;
338340
}
339341
}
342+
if (ret == 0) {
343+
ret = wolfSSL_liboqsRngMutexLock(rng);
344+
}
340345
if (ret == 0) {
341346
if (OQS_KEM_keypair(kem, key->pub, key->priv) !=
342347
OQS_SUCCESS) {
343348
ret = BAD_FUNC_ARG;
344349
}
345350
}
351+
wolfSSL_liboqsRngMutexUnlock();
346352
OQS_KEM_free(kem);
347353
#endif /* HAVE_LIBOQS */
348354
#ifdef HAVE_PQM4
@@ -422,12 +428,15 @@ int wc_KyberKey_Encapsulate(KyberKey* key, unsigned char* ct, unsigned char* ss,
422428
ret = BAD_FUNC_ARG;
423429
}
424430
}
431+
if (ret == 0) {
432+
ret = wolfSSL_liboqsRngMutexLock(rng);
433+
}
425434
if (ret == 0) {
426435
if (OQS_KEM_encaps(kem, ct, ss, key->pub) != OQS_SUCCESS) {
427436
ret = BAD_FUNC_ARG;
428437
}
429438
}
430-
439+
wolfSSL_liboqsRngMutexUnlock();
431440
OQS_KEM_free(kem);
432441
#endif /* HAVE_LIBOQS */
433442
#ifdef HAVE_PQM4

wolfcrypt/src/falcon.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
*/
6060
int wc_falcon_sign_msg(const byte* in, word32 inLen,
6161
byte* out, word32 *outLen,
62-
falcon_key* key)
62+
falcon_key* key, WC_RNG* rng)
6363
{
6464
int ret = 0;
6565
#ifdef HAVE_LIBOQS
@@ -101,6 +101,10 @@ int wc_falcon_sign_msg(const byte* in, word32 inLen,
101101
localOutLen = *outLen;
102102
}
103103

104+
if (ret == 0) {
105+
ret = wolfSSL_liboqsRngMutexLock(rng);
106+
}
107+
104108
if ((ret == 0) &&
105109
(OQS_SIG_sign(oqssig, out, &localOutLen, in, inLen, key->k)
106110
== OQS_ERROR)) {
@@ -111,6 +115,8 @@ int wc_falcon_sign_msg(const byte* in, word32 inLen,
111115
*outLen = (word32)localOutLen;
112116
}
113117

118+
wolfSSL_liboqsRngMutexUnlock();
119+
114120
if (oqssig != NULL) {
115121
OQS_SIG_free(oqssig);
116122
}

wolfcrypt/src/include.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ EXTRA_DIST += wolfcrypt/src/port/ti/ti-aes.c \
132132
wolfcrypt/src/port/Renesas/renesas_rx64_hw_sha.c \
133133
wolfcrypt/src/port/Renesas/renesas_rx64_hw_util.c \
134134
wolfcrypt/src/port/Renesas/README.md \
135-
wolfcrypt/src/port/cypress/psoc6_crypto.c
135+
wolfcrypt/src/port/cypress/psoc6_crypto.c \
136+
wolfcrypt/src/port/liboqs/liboqs.c
136137

137138
$(ASYNC_FILES):
138139
$(AM_V_at)touch $(srcdir)/$@

wolfcrypt/src/port/liboqs/liboqs.c

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/* liboqs.c
2+
*
3+
* Copyright (C) 2006-2023 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL.
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
22+
/*
23+
24+
DESCRIPTION
25+
This library provides the support interfaces to the liboqs library providing
26+
implementations for Post-Quantum cryptography algorithms.
27+
28+
*/
29+
30+
#ifdef HAVE_CONFIG_H
31+
#include <config.h>
32+
#endif
33+
34+
#include <wolfssl/wolfcrypt/settings.h>
35+
#include <wolfssl/wolfcrypt/types.h>
36+
#include <wolfssl/wolfcrypt/error-crypt.h>
37+
38+
#include <wolfssl/wolfcrypt/port/liboqs/liboqs.h>
39+
40+
#if defined(HAVE_LIBOQS)
41+
42+
/* RNG for liboqs */
43+
static WC_RNG liboqsDefaultRNG;
44+
static WC_RNG* liboqsCurrentRNG;
45+
46+
static wolfSSL_Mutex liboqsRNGMutex;
47+
48+
static int liboqs_init = 0;
49+
50+
51+
static void wolfSSL_liboqsGetRandomData(uint8_t* buffer, size_t numOfBytes)
52+
{
53+
int ret = wc_RNG_GenerateBlock(liboqsCurrentRNG, buffer, numOfBytes);
54+
if (ret != 0) {
55+
// ToDo: liboqs exits programm if RNG fails, not sure what to do here
56+
}
57+
}
58+
59+
int wolfSSL_liboqsInit(void)
60+
{
61+
int ret = 0;
62+
63+
if (liboqs_init == 0) {
64+
ret = wc_InitMutex(&liboqsRNGMutex);
65+
if (ret != 0) {
66+
return ret;
67+
}
68+
ret = wc_LockMutex(&liboqsRNGMutex);
69+
if (ret != 0) {
70+
return ret;
71+
}
72+
ret = wc_InitRng(&liboqsDefaultRNG);
73+
if (ret == 0) {
74+
OQS_init();
75+
liboqs_init = 1;
76+
}
77+
liboqsCurrentRNG = &liboqsDefaultRNG;
78+
wc_UnLockMutex(&liboqsRNGMutex);
79+
80+
OQS_randombytes_custom_algorithm(wolfSSL_liboqsGetRandomData);
81+
}
82+
83+
return ret;
84+
}
85+
86+
int wolfSSL_liboqsRngMutexLock(WC_RNG* rng)
87+
{
88+
int ret = wolfSSL_liboqsInit();
89+
if (ret == 0) {
90+
ret = wc_LockMutex(&liboqsRNGMutex);
91+
}
92+
if (ret == 0 && rng != NULL) {
93+
/* Update the pointer with the RNG to use. This is safe as we locked the mutex */
94+
liboqsCurrentRNG = rng;
95+
}
96+
return ret;
97+
}
98+
99+
int wolfSSL_liboqsRngMutexUnlock(void)
100+
{
101+
int ret = BAD_MUTEX_E;
102+
103+
liboqsCurrentRNG = &liboqsDefaultRNG;
104+
105+
if (liboqs_init) {
106+
ret = wc_UnLockMutex(&liboqsRNGMutex);
107+
}
108+
return ret;
109+
}
110+
111+
#endif /* HAVE_LIBOQS */

0 commit comments

Comments
 (0)