Skip to content

Commit 288d2f7

Browse files
authored
Merge pull request #25 from dgarske/portability_realloc
Portability improvements for XREALLOC, XGETENV and pin hashing
2 parents b18d011 + fd6c4b4 commit 288d2f7

File tree

6 files changed

+46
-14
lines changed

6 files changed

+46
-14
lines changed

src/internal.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <wolfssl/version.h>
3434
#include <wolfssl/wolfcrypt/pwdbased.h>
3535
#include <wolfssl/wolfcrypt/asn.h>
36+
#include <wolfssl/wolfcrypt/hash.h>
3637
#include <wolfssl/wolfcrypt/hmac.h>
3738
#include <wolfssl/wolfcrypt/ecc.h>
3839
#include <wolfssl/wolfcrypt/rsa.h>
@@ -695,7 +696,7 @@ int wolfPKCS11_Store_Open(int type, CK_ULONG id1, CK_ULONG id2, int read,
695696
void** store)
696697
{
697698
int ret = 0;
698-
#if defined(XGETENV) || !defined(WOLFPKCS11_TPM_STORE)
699+
#ifndef WOLFPKCS11_NO_ENV
699700
const char* str = NULL;
700701
#endif
701702
#ifdef WOLFPKCS11_TPM_STORE
@@ -715,7 +716,7 @@ int wolfPKCS11_Store_Open(int type, CK_ULONG id1, CK_ULONG id2, int read,
715716
type, id1, id2, read);
716717
#endif
717718

718-
#ifdef XGETENV
719+
#ifndef WOLFPKCS11_NO_ENV
719720
str = XGETENV("WOLFPKCS11_NO_STORE");
720721
if (str != NULL) {
721722
return NOT_AVAILABLE_E;
@@ -764,7 +765,7 @@ int wolfPKCS11_Store_Open(int type, CK_ULONG id1, CK_ULONG id2, int read,
764765
#endif
765766

766767
#else
767-
#ifdef XGETENV
768+
#ifndef WOLFPKCS11_NO_ENV
768769
str = XGETENV("WOLFPKCS11_TOKEN_PATH");
769770
#endif
770771
if (str == NULL) {
@@ -3639,6 +3640,12 @@ static int HashPIN(char* pin, int pinLen, byte* seed, int seedLen, byte* hash,
36393640
return wc_scrypt(hash, (byte*)pin, pinLen, seed, seedLen,
36403641
WP11_HASH_PIN_COST, WP11_HASH_PIN_BLOCKSIZE,
36413642
WP11_HASH_PIN_PARALLEL, hashLen);
3643+
#elif !defined(NO_SHA256)
3644+
/* fallback to simple SHA2-256 hash of pin */
3645+
(void)seed;
3646+
(void)seedLen;
3647+
XMEMSET(hash, 0, hashLen);
3648+
return wc_Sha256Hash((const byte*)pin, pinLen, hash);
36423649
#else
36433650
(void)pin;
36443651
(void)pinLen;
@@ -8056,6 +8063,7 @@ int WP11_AesGcm_DecryptUpdate(unsigned char* enc, word32 encSz,
80568063
unsigned char* newEnc;
80578064
WP11_GcmParams* gcm = &session->params.gcm;
80588065

8066+
#ifdef XREALLOC
80598067
newEnc = (unsigned char*)XREALLOC(gcm->enc, gcm->encSz + encSz, NULL,
80608068
DYNAMIC_TYPE_TMP_BUFFER);
80618069
if (newEnc == NULL)
@@ -8065,6 +8073,20 @@ int WP11_AesGcm_DecryptUpdate(unsigned char* enc, word32 encSz,
80658073
XMEMCPY(gcm->enc + gcm->encSz, enc, encSz);
80668074
gcm->encSz += encSz;
80678075
}
8076+
#else
8077+
newEnc = (unsigned char*)XMALLOC(gcm->encSz + encSz, NULL,
8078+
DYNAMIC_TYPE_TMP_BUFFER);
8079+
if (newEnc == NULL)
8080+
ret = MEMORY_E;
8081+
if (ret == 0) {
8082+
if (gcm->enc != NULL)
8083+
XMEMCPY(newEnc, gcm->enc, gcm->encSz);
8084+
XFREE(gcm->enc, NULL, DYNAMIC_TYPE_TMP_BUFFER);
8085+
gcm->enc = newEnc;
8086+
XMEMCPY(gcm->enc + gcm->encSz, enc, encSz);
8087+
gcm->encSz += encSz;
8088+
}
8089+
#endif /* !XREALLOC */
80688090

80698091
return ret;
80708092
}

tests/pkcs11mtt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6499,7 +6499,7 @@ int pkcs11test_mtt(int argc, char* argv[])
64996499
int i;
65006500

65016501
#ifndef WOLFPKCS11_NO_ENV
6502-
setenv("WOLFPKCS11_NO_STORE", "1", 1);
6502+
XSETENV("WOLFPKCS11_NO_STORE", "1", 1);
65036503
#endif
65046504

65056505
argc--;

tests/pkcs11str.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -923,8 +923,8 @@ int pkcs11test_str(int argc, char* argv[])
923923
int closeDl = 1;
924924

925925
#ifndef WOLFPKCS11_NO_ENV
926-
if (!getenv("WOLFPKCS11_TOKEN_PATH")) {
927-
setenv("WOLFPKCS11_TOKEN_PATH", "./tests", 1);
926+
if (!XGETENV("WOLFPKCS11_TOKEN_PATH")) {
927+
XSETENV("WOLFPKCS11_TOKEN_PATH", "./tests", 1);
928928
}
929929
#endif
930930

tests/pkcs11test.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7791,8 +7791,10 @@ static CK_RV pkcs11_test(int slotId, int setPin, int onlySet, int closeDl)
77917791
ret = pkcs11_lib_init();
77927792

77937793
/* Do tests after library initialization but without SO PIN. */
7794-
if (ret == CKR_OK)
7794+
if (ret == CKR_OK) {
7795+
inited = 1;
77957796
ret = run_tests(testFunc, testFuncCnt, onlySet, TEST_FLAG_INIT);
7797+
}
77967798

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

78067808
/* Set user PIN. */
78077809
if (ret == CKR_OK) {
7808-
inited = 1;
78097810
if (setPin)
78107811
ret = pkcs11_set_user_pin(slotId);
78117812
}
@@ -7913,7 +7914,7 @@ int pkcs11test_test(int argc, char* argv[])
79137914
int i;
79147915

79157916
#ifndef WOLFPKCS11_NO_ENV
7916-
setenv("WOLFPKCS11_NO_STORE", "1", 1);
7917+
XSETENV("WOLFPKCS11_NO_STORE", "1", 1);
79177918
#endif
79187919

79197920
argc--;

wolfpkcs11/internal.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,17 @@
2424
#define WOLFPKCS11_INTERNAL_H
2525

2626
#ifndef WOLFSSL_USER_SETTINGS
27-
#include <wolfssl/options.h>
28-
#else
29-
#include "user_settings.h"
27+
#include <wolfssl/options.h>
3028
#endif
29+
#include <wolfssl/wolfcrypt/settings.h>
3130
#include <wolfssl/wolfcrypt/rsa.h>
3231
#include <wolfssl/wolfcrypt/ecc.h>
3332
#include <wolfssl/wolfcrypt/error-crypt.h>
3433
#include <wolfssl/wolfcrypt/wc_encrypt.h>
3534

3635
#ifndef WOLFPKCS11_USER_SETTINGS
37-
#include <wolfpkcs11/options.h>
36+
#include <wolfpkcs11/options.h>
3837
#endif
39-
4038
#include <wolfpkcs11/pkcs11.h>
4139
#include <wolfpkcs11/version.h>
4240

wolfpkcs11/pkcs11.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@
2828
extern "C" {
2929
#endif
3030

31+
/* Helpers for setenv/getenv */
32+
#if !defined(WOLFPKCS11_USER_ENV) && !defined(WOLFPKCS11_NO_ENV)
33+
#include <stdlib.h>
34+
#ifndef XSETENV
35+
#define XSETENV setenv
36+
#endif
37+
#ifndef XGETENV
38+
#define XGETENV getenv
39+
#endif
40+
#endif
41+
3142
#ifndef NULL_PTR
3243
#define NULL_PTR 0
3344
#endif

0 commit comments

Comments
 (0)