Skip to content

Commit 40aabc2

Browse files
authored
Merge pull request #729 from ejohnstown/new-kdf
Use wolfCrypt SSHv2 KDF
2 parents 06dc40d + 6e151e4 commit 40aabc2

5 files changed

Lines changed: 48 additions & 2 deletions

File tree

src/internal.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@
4848
#include <wolfssl/wolfcrypt/hmac.h>
4949
#include <wolfssl/wolfcrypt/signature.h>
5050

51+
#if (LIBWOLFSSL_VERSION_HEX >= WOLFSSL_V5_0_0) \
52+
&& ((defined(HAVE_FIPS) && FIPS_VERSION_GE(5,2)) \
53+
|| defined(WOLFSSH_NO_ECDH_NISTP256_KYBER_LEVEL1_SHA256))
54+
#include <wolfssl/wolfcrypt/kdf.h>
55+
#endif
56+
5157
#ifdef WOLFSSH_HAVE_LIBOQS
5258
#include <oqs/kem.h>
5359
#endif
@@ -456,6 +462,9 @@ const char* GetErrorString(int err)
456462
case WS_AUTH_PENDING:
457463
return "userauth is still pending (callback would block)";
458464

465+
case WS_KDF_E:
466+
return "KDF error";
467+
459468
default:
460469
return "Unknown error code";
461470
}
@@ -2164,6 +2173,32 @@ int GenerateKey(byte hashId, byte keyId,
21642173
const byte* h, word32 hSz,
21652174
const byte* sessionId, word32 sessionIdSz,
21662175
byte doKeyPad)
2176+
#if (LIBWOLFSSL_VERSION_HEX >= WOLFSSL_V5_0_0) \
2177+
&& ((defined(HAVE_FIPS) && FIPS_VERSION_GE(5,2)) \
2178+
|| defined(WOLFSSH_NO_ECDH_NISTP256_KYBER_LEVEL1_SHA256))
2179+
/* Cannot use the SSH KDF with Kyber. With Kyber, doKeyPad must be false,
2180+
* and the FIPS SSH KDF doesn't handle no-padding. Also, the Kyber algorithm
2181+
* isn't in our FIPS boundary. */
2182+
{
2183+
int ret = WS_SUCCESS;
2184+
2185+
if (!doKeyPad) {
2186+
WLOG(WS_LOG_ERROR, "cannot use FIPS KDF with Kyber");
2187+
ret = WS_INVALID_ALGO_ID;
2188+
}
2189+
else {
2190+
PRIVATE_KEY_UNLOCK();
2191+
ret = wc_SSH_KDF(hashId, keyId, key, keySz,
2192+
k, kSz, h, hSz, sessionId, sessionIdSz);
2193+
PRIVATE_KEY_LOCK();
2194+
if (ret != 0) {
2195+
WLOG(WS_LOG_ERROR, "SSH KDF failed (%d)", ret);
2196+
ret = WS_KDF_E;
2197+
}
2198+
}
2199+
return ret;
2200+
}
2201+
#else
21672202
{
21682203
word32 blocks, remainder;
21692204
wc_HashAlg hash;
@@ -2174,12 +2209,13 @@ int GenerateKey(byte hashId, byte keyId,
21742209
int digestSz;
21752210
int ret;
21762211

2212+
WLOG(WS_LOG_DEBUG, "Entering GenerateKey()");
2213+
21772214
if (key == NULL || keySz == 0 ||
21782215
k == NULL || kSz == 0 ||
21792216
h == NULL || hSz == 0 ||
21802217
sessionId == NULL || sessionIdSz == 0) {
21812218

2182-
WLOG(WS_LOG_DEBUG, "GK: bad argument");
21832219
return WS_BAD_ARGUMENT;
21842220
}
21852221

@@ -2274,6 +2310,7 @@ int GenerateKey(byte hashId, byte keyId,
22742310

22752311
return ret;
22762312
}
2313+
#endif /* HAVE_FIPS && LIBWOLFSSL_VERSION_HEX >= WOLFSSL_V5_7_2 */
22772314

22782315

22792316
static int GenerateKeys(WOLFSSH* ssh, byte hashId, byte doKeyPad)

wolfssh/error.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ enum WS_ErrorCodes {
135135
WS_MSGID_NOT_ALLOWED_E = -1094, /* Message not allowed before userauth */
136136
WS_ED25519_E = -1095, /* Ed25519 failure */
137137
WS_AUTH_PENDING = -1096, /* User authentication still pending */
138+
WS_KDF_E = -1097, /* KDF error*/
138139

139-
WS_LAST_E = -1096 /* Update this to indicate last error */
140+
WS_LAST_E = -1097 /* Update this to indicate last error */
140141
};
141142

142143

wolfssh/internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,7 +1352,9 @@ enum TerminalModes {
13521352
#endif /* WOLFSSH_TERM */
13531353

13541354

1355+
#define WOLFSSL_V5_0_0 0x05000000
13551356
#define WOLFSSL_V5_7_0 0x05007000
1357+
#define WOLFSSL_V5_7_2 0x05007002
13561358

13571359

13581360
#ifdef __cplusplus

zephyr/samples/tests/wolfssl_user_settings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ extern "C" {
2828
#undef WOLFSSL_ZEPHYR
2929
#define WOLFSSL_ZEPHYR
3030

31+
#undef WOLFSSL_WOLFSSH
32+
#define WOLFSSL_WOLFSSH
33+
3134
#undef TFM_TIMING_RESISTANT
3235
#define TFM_TIMING_RESISTANT
3336

zephyr/samples/tests/wolfssl_user_settings_nofs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ extern "C" {
2828
#undef WOLFSSL_ZEPHYR
2929
#define WOLFSSL_ZEPHYR
3030

31+
#undef WOLFSSL_WOLFSSH
32+
#define WOLFSSL_WOLFSSH
33+
3134
#undef TFM_TIMING_RESISTANT
3235
#define TFM_TIMING_RESISTANT
3336

0 commit comments

Comments
 (0)