Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Portability improvements for XREALLOC, XGETENV and pin hashing #25

Merged
merged 4 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <wolfssl/version.h>
#include <wolfssl/wolfcrypt/pwdbased.h>
#include <wolfssl/wolfcrypt/asn.h>
#include <wolfssl/wolfcrypt/hash.h>
#include <wolfssl/wolfcrypt/hmac.h>
#include <wolfssl/wolfcrypt/ecc.h>
#include <wolfssl/wolfcrypt/rsa.h>
Expand Down Expand Up @@ -695,7 +696,7 @@ int wolfPKCS11_Store_Open(int type, CK_ULONG id1, CK_ULONG id2, int read,
void** store)
{
int ret = 0;
#if defined(XGETENV) || !defined(WOLFPKCS11_TPM_STORE)
#ifndef WOLFPKCS11_NO_ENV
const char* str = NULL;
#endif
#ifdef WOLFPKCS11_TPM_STORE
Expand All @@ -715,7 +716,7 @@ int wolfPKCS11_Store_Open(int type, CK_ULONG id1, CK_ULONG id2, int read,
type, id1, id2, read);
#endif

#ifdef XGETENV
#ifndef WOLFPKCS11_NO_ENV
str = XGETENV("WOLFPKCS11_NO_STORE");
if (str != NULL) {
return NOT_AVAILABLE_E;
Expand Down Expand Up @@ -764,7 +765,7 @@ int wolfPKCS11_Store_Open(int type, CK_ULONG id1, CK_ULONG id2, int read,
#endif

#else
#ifdef XGETENV
#ifndef WOLFPKCS11_NO_ENV
str = XGETENV("WOLFPKCS11_TOKEN_PATH");
#endif
if (str == NULL) {
Expand Down Expand Up @@ -3639,6 +3640,12 @@ static int HashPIN(char* pin, int pinLen, byte* seed, int seedLen, byte* hash,
return wc_scrypt(hash, (byte*)pin, pinLen, seed, seedLen,
WP11_HASH_PIN_COST, WP11_HASH_PIN_BLOCKSIZE,
WP11_HASH_PIN_PARALLEL, hashLen);
#elif !defined(NO_SHA256)
/* fallback to simple SHA2-256 hash of pin */
(void)seed;
(void)seedLen;
XMEMSET(hash, 0, hashLen);
return wc_Sha256Hash((const byte*)pin, pinLen, hash);
#else
(void)pin;
(void)pinLen;
Expand Down Expand Up @@ -8056,6 +8063,7 @@ int WP11_AesGcm_DecryptUpdate(unsigned char* enc, word32 encSz,
unsigned char* newEnc;
WP11_GcmParams* gcm = &session->params.gcm;

#ifdef XREALLOC
newEnc = (unsigned char*)XREALLOC(gcm->enc, gcm->encSz + encSz, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (newEnc == NULL)
Expand All @@ -8065,6 +8073,20 @@ int WP11_AesGcm_DecryptUpdate(unsigned char* enc, word32 encSz,
XMEMCPY(gcm->enc + gcm->encSz, enc, encSz);
gcm->encSz += encSz;
}
#else
newEnc = (unsigned char*)XMALLOC(gcm->encSz + encSz, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (newEnc == NULL)
ret = MEMORY_E;
if (ret == 0) {
if (gcm->enc != NULL)
XMEMCPY(newEnc, gcm->enc, gcm->encSz);
XFREE(gcm->enc, NULL, DYNAMIC_TYPE_TMP_BUFFER);
gcm->enc = newEnc;
XMEMCPY(gcm->enc + gcm->encSz, enc, encSz);
gcm->encSz += encSz;
}
#endif /* !XREALLOC */

return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/pkcs11mtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -6499,7 +6499,7 @@ int pkcs11test_mtt(int argc, char* argv[])
int i;

#ifndef WOLFPKCS11_NO_ENV
setenv("WOLFPKCS11_NO_STORE", "1", 1);
XSETENV("WOLFPKCS11_NO_STORE", "1", 1);
#endif

argc--;
Expand Down
4 changes: 2 additions & 2 deletions tests/pkcs11str.c
Original file line number Diff line number Diff line change
Expand Up @@ -923,8 +923,8 @@ int pkcs11test_str(int argc, char* argv[])
int closeDl = 1;

#ifndef WOLFPKCS11_NO_ENV
if (!getenv("WOLFPKCS11_TOKEN_PATH")) {
setenv("WOLFPKCS11_TOKEN_PATH", "./tests", 1);
if (!XGETENV("WOLFPKCS11_TOKEN_PATH")) {
XSETENV("WOLFPKCS11_TOKEN_PATH", "./tests", 1);
}
#endif

Expand Down
7 changes: 4 additions & 3 deletions tests/pkcs11test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7791,8 +7791,10 @@ static CK_RV pkcs11_test(int slotId, int setPin, int onlySet, int closeDl)
ret = pkcs11_lib_init();

/* Do tests after library initialization but without SO PIN. */
if (ret == CKR_OK)
if (ret == CKR_OK) {
inited = 1;
ret = run_tests(testFunc, testFuncCnt, onlySet, TEST_FLAG_INIT);
}

if (ret == CKR_OK)
ret = pkcs11_init_token();
Expand All @@ -7805,7 +7807,6 @@ static CK_RV pkcs11_test(int slotId, int setPin, int onlySet, int closeDl)

/* Set user PIN. */
if (ret == CKR_OK) {
inited = 1;
if (setPin)
ret = pkcs11_set_user_pin(slotId);
}
Expand Down Expand Up @@ -7913,7 +7914,7 @@ int pkcs11test_test(int argc, char* argv[])
int i;

#ifndef WOLFPKCS11_NO_ENV
setenv("WOLFPKCS11_NO_STORE", "1", 1);
XSETENV("WOLFPKCS11_NO_STORE", "1", 1);
#endif

argc--;
Expand Down
8 changes: 3 additions & 5 deletions wolfpkcs11/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,17 @@
#define WOLFPKCS11_INTERNAL_H

#ifndef WOLFSSL_USER_SETTINGS
#include <wolfssl/options.h>
#else
#include "user_settings.h"
#include <wolfssl/options.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/wolfcrypt/rsa.h>
#include <wolfssl/wolfcrypt/ecc.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/wc_encrypt.h>

#ifndef WOLFPKCS11_USER_SETTINGS
#include <wolfpkcs11/options.h>
#include <wolfpkcs11/options.h>
#endif

#include <wolfpkcs11/pkcs11.h>
#include <wolfpkcs11/version.h>

Expand Down
11 changes: 11 additions & 0 deletions wolfpkcs11/pkcs11.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@
extern "C" {
#endif

/* Helpers for setenv/getenv */
#if !defined(WOLFPKCS11_USER_ENV) && !defined(WOLFPKCS11_NO_ENV)
#include <stdlib.h>
#ifndef XSETENV
#define XSETENV setenv
#endif
#ifndef XGETENV
#define XGETENV getenv
#endif
#endif

#ifndef NULL_PTR
#define NULL_PTR 0
#endif
Expand Down