Skip to content

Commit 755c385

Browse files
committed
Liboqs: use WolfSSL RNG
Improve the interface to liboqs by properly configuring and using the RNG provided by WolfSSL from within liboqs. Signed-off-by: Tobias Frauenschläger <t.frauenschlaeger@me.com>
1 parent d31e2c3 commit 755c385

9 files changed

Lines changed: 203 additions & 3 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

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/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 */

wolfcrypt/src/wc_port.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@
127127
#include <wolfssl/wolfcrypt/port/psa/psa.h>
128128
#endif
129129

130+
#if defined(HAVE_LIBOQS)
131+
#include <wolfssl/wolfcrypt/port/liboqs/liboqs.h>
132+
#endif
133+
130134

131135
/* prevent multiple mutex initializations */
132136
static volatile int initRefCount = 0;
@@ -386,6 +390,12 @@ int wolfCrypt_Init(void)
386390
}
387391
rpcmem_init();
388392
#endif
393+
394+
#if defined(HAVE_LIBOQS)
395+
if ((ret = wolfSSL_liboqsInit()) != 0) {
396+
return ret;
397+
}
398+
#endif
389399
}
390400
initRefCount++;
391401

wolfssl/wolfcrypt/include.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ noinst_HEADERS+= \
115115
wolfssl/wolfcrypt/port/Renesas/renesas_sync.h \
116116
wolfssl/wolfcrypt/port/Renesas/renesas_cmn.h \
117117
wolfssl/wolfcrypt/port/Renesas/renesas_tsip_types.h \
118-
wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h
118+
wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h \
119+
wolfssl/wolfcrypt/port/liboqs/liboqs.h
119120

120121
if BUILD_CRYPTOAUTHLIB
121122
nobase_include_HEADERS+= wolfssl/wolfcrypt/port/atmel/atmel.h
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* liboqs.h
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+
\file wolfssl/wolfcrypt/port/liboqs/liboqs.h
24+
*/
25+
/*
26+
27+
DESCRIPTION
28+
This library provides the support interfaces to the liboqs library providing
29+
implementations for Post-Quantum cryptography algorithms.
30+
*/
31+
32+
#ifndef WOLF_CRYPT_LIBOQS_H
33+
#define WOLF_CRYPT_LIBOQS_H
34+
35+
#include <wolfssl/wolfcrypt/types.h>
36+
#include <wolfssl/wolfcrypt/random.h>
37+
38+
39+
#ifdef __cplusplus
40+
extern "C" {
41+
#endif
42+
43+
#if defined(HAVE_LIBOQS)
44+
45+
#include "oqs/oqs.h"
46+
47+
48+
int wolfSSL_liboqsInit(void);
49+
50+
int wolfSSL_liboqsRngMutexLock(WC_RNG* rng);
51+
52+
int wolfSSL_liboqsRngMutexUnlock(void);
53+
54+
#endif /* HAVE_LIBOQS */
55+
56+
#ifdef __cplusplus
57+
} /* extern "C" */
58+
#endif
59+
60+
#endif /* WOLF_CRYPT_LIBOQS_H */

zephyr/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ if(CONFIG_WOLFSSL)
109109
zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/wolfevent.c)
110110
zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/wolfmath.c)
111111

112+
zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/port/liboqs/liboqs.c)
112113
zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/port/psa/psa.c)
113114
zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/port/psa/psa_aes.c)
114115
zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/port/psa/psa_hash.c)

0 commit comments

Comments
 (0)