Skip to content

Commit 029b06b

Browse files
authored
Initial implementation of SSHKDF for wolfprovider (#382)
* Initial implementation of SSHKDF for wolfprovider * Add private key lock/unlock for FIPS * Extract new logic to helper function, unify into internal.c, add new test for MSB
1 parent 5e4c33f commit 029b06b

14 files changed

Lines changed: 1012 additions & 16 deletions

File tree

include/wolfprovider/alg_funcs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ typedef void (*DFUNC)(void);
149149
#define WP_NAMES_TLS1_PRF "TLS1-PRF"
150150
#define WP_NAMES_KBKDF "KBKDF"
151151
#define WP_NAMES_KRB5KDF "KRB5KDF"
152+
#define WP_NAMES_SSHKDF "SSHKDF"
152153

153154
/* Signature names. */
154155
#define WP_NAMES_RSA "RSA:rsaEncryption:1.2.840.113549.1.1.1"
@@ -315,6 +316,7 @@ extern const OSSL_DISPATCH wp_kdf_tls1_3_kdf_functions[];
315316
extern const OSSL_DISPATCH wp_kdf_tls1_prf_functions[];
316317
extern const OSSL_DISPATCH wp_kdf_kbkdf_functions[];
317318
extern const OSSL_DISPATCH wp_kdf_krb5kdf_functions[];
319+
extern const OSSL_DISPATCH wp_kdf_sshkdf_functions[];
318320

319321
/* Signature implementations. */
320322
extern const OSSL_DISPATCH wp_rsa_signature_functions[];

include/wolfprovider/internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,7 @@ byte wp_ct_int_mask_eq(int a, int b);
249249
byte wp_ct_int_mask_lt(int a, int b);
250250
byte wp_ct_byte_mask_sel(byte mask, byte a, byte b);
251251

252+
void wp_c32toa(word32 wc_u32, byte* c);
253+
word32 wp_atoc32(const byte* c);
254+
252255
#endif /* WP_INTERNAL_H */

include/wolfprovider/settings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@
116116
#ifndef NO_PWDBASED
117117
#define WP_HAVE_PBE
118118
#endif
119+
#ifdef WOLFSSL_WOLFSSH
120+
#define WP_HAVE_SSHKDF
121+
#endif
119122

120123
#ifndef NO_DH
121124
#define WP_HAVE_DH

include/wolfprovider/wp_logging.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@
186186
#define WP_LOG_COMP_X448 0x4000000 /* X448 operations */
187187
#define WP_LOG_COMP_QUERY 0x8000000 /* wolfprov_query operations */
188188
#define WP_LOG_COMP_TLS1_PRF 0x10000000 /* TLS1 PRF operations */
189+
#define WP_LOG_COMP_SSHKDF 0x20000000 /* SSHKDF operations */
189190

190191
/* log all components */
191192
#define WP_LOG_COMP_ALL ( \
@@ -217,7 +218,8 @@
217218
WP_LOG_COMP_X25519 | \
218219
WP_LOG_COMP_X448 | \
219220
WP_LOG_COMP_QUERY | \
220-
WP_LOG_COMP_TLS1_PRF )
221+
WP_LOG_COMP_TLS1_PRF | \
222+
WP_LOG_COMP_SSHKDF )
221223

222224
/* default components logged */
223225
#define WP_LOG_COMP_DEFAULT WP_LOG_COMP_ALL

src/include.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ libwolfprov_la_SOURCES += src/wp_kdf_exch.c
2323
libwolfprov_la_SOURCES += src/wp_pbkdf2.c
2424
libwolfprov_la_SOURCES += src/wp_kbkdf.c
2525
libwolfprov_la_SOURCES += src/wp_krb5kdf.c
26+
libwolfprov_la_SOURCES += src/wp_sshkdf.c
2627
libwolfprov_la_SOURCES += src/wp_rsa_kmgmt.c
2728
libwolfprov_la_SOURCES += src/wp_rsa_sig.c
2829
libwolfprov_la_SOURCES += src/wp_rsa_asym.c

src/wp_internal.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,3 +1231,32 @@ byte wp_ct_byte_mask_sel(byte mask, byte a, byte b)
12311231
return (byte)((mask & a) | (~mask & b));
12321232
}
12331233

1234+
/* Big-endian word32 <-> byte[4] conversions shared across KDF sources.
1235+
* We are not guaranteed to have these available from wolfssl, so implement
1236+
* them here. Consumed by SSHKDF (mpint length decode) and KBKDF
1237+
* (counter / length encode). */
1238+
1239+
void wp_c32toa(word32 wc_u32, byte* c) {
1240+
#ifdef WOLFSSL_USE_ALIGN
1241+
c[0] = (byte)((wc_u32 >> 24) & 0xff);
1242+
c[1] = (byte)((wc_u32 >> 16) & 0xff);
1243+
c[2] = (byte)((wc_u32 >> 8) & 0xff);
1244+
c[3] = (byte) (wc_u32 & 0xff);
1245+
#elif defined(LITTLE_ENDIAN_ORDER)
1246+
*(word32*)c = ByteReverseWord32(wc_u32);
1247+
#else
1248+
*(word32*)c = wc_u32;
1249+
#endif
1250+
}
1251+
1252+
word32 wp_atoc32(const byte* c) {
1253+
#ifdef WOLFSSL_USE_ALIGN
1254+
return ((word32)c[0] << 24) | ((word32)c[1] << 16)
1255+
| ((word32)c[2] << 8) | (word32)c[3];
1256+
#elif defined(LITTLE_ENDIAN_ORDER)
1257+
return ByteReverseWord32(*(const word32*)c);
1258+
#else
1259+
return *(const word32*)c;
1260+
#endif
1261+
}
1262+

src/wp_kbkdf.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -354,21 +354,6 @@ static const OSSL_PARAM* wp_kdf_kbkdf_gettable_ctx_params(wp_KbkdfCtx* ctx,
354354
return wp_kbkdf_supported_gettable_ctx_params;
355355
}
356356

357-
/* We are not guaranteed to have these available from wolfssl, so implement
358-
* them here */
359-
static void wp_c32toa(word32 wc_u32, byte* c) {
360-
#ifdef WOLFSSL_USE_ALIGN
361-
c[0] = (byte)((wc_u32 >> 24) & 0xff);
362-
c[1] = (byte)((wc_u32 >> 16) & 0xff);
363-
c[2] = (byte)((wc_u32 >> 8) & 0xff);
364-
c[3] = (byte) (wc_u32 & 0xff);
365-
#elif defined(LITTLE_ENDIAN_ORDER)
366-
*(word32*)c = ByteReverseWord32(wc_u32);
367-
#else
368-
*(word32*)c = wc_u32;
369-
#endif
370-
}
371-
372357
#ifdef WP_HAVE_HMAC
373358
#define WP_MAX_HASH_BLOCK_SIZE 128
374359

src/wp_logging.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ static void wolfProv_LogComponentToMask(const char* level, size_t len, void* ctx
531531
{ "WP_LOG_COMP_X448", XSTRLEN("WP_LOG_COMP_X448"), WP_LOG_COMP_X448 },
532532
{ "WP_LOG_COMP_QUERY", XSTRLEN("WP_LOG_COMP_QUERY"), WP_LOG_COMP_QUERY },
533533
{ "WP_LOG_COMP_TLS1_PRF", XSTRLEN("WP_LOG_COMP_TLS1_PRF"), WP_LOG_COMP_TLS1_PRF },
534+
{ "WP_LOG_COMP_SSHKDF", XSTRLEN("WP_LOG_COMP_SSHKDF"), WP_LOG_COMP_SSHKDF },
534535
{ "WP_LOG_COMP_ALL",
535536
XSTRLEN("WP_LOG_COMP_ALL"),
536537
WP_LOG_COMP_ALL },

0 commit comments

Comments
 (0)