Skip to content
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
38 changes: 35 additions & 3 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -17250,6 +17250,38 @@ int WP11_AesKeyWrap_Decrypt(unsigned char* enc, word32 encSz,
return 0;
}

/* Single-block AES encrypt/decrypt. FIPS modules before v5.3 declare
* wc_AesEncryptDirect/wc_AesDecryptDirect as returning void, so no result is
* available there. The v5.2.3 and v5.2.4 modules return int for ARM assembly
* builds. */
#if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3))
#define WP11_AES_DIRECT_RETURNS_INT
#elif defined(WOLFSSL_ARMASM) && defined(FIPS_VERSION3_GE)
#if FIPS_VERSION3_GE(5, 2, 3)
#define WP11_AES_DIRECT_RETURNS_INT
#endif
#endif

static int wp11_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
{
#ifdef WP11_AES_DIRECT_RETURNS_INT
return wc_AesEncryptDirect(aes, out, in);
#else
wc_AesEncryptDirect(aes, out, in);
return 0;
#endif
}

static int wp11_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
{
#ifdef WP11_AES_DIRECT_RETURNS_INT
return wc_AesDecryptDirect(aes, out, in);
#else
wc_AesDecryptDirect(aes, out, in);
return 0;
#endif
}

/**
* RFC 3394 key unwrap core that returns the recovered integrity register A
* instead of verifying it, so RFC 5649 can inspect the AIV (which encodes the
Expand Down Expand Up @@ -17293,7 +17325,7 @@ static int wp11_AesKeyUnwrapRaw(Aes* aes, const unsigned char* in, word32 inSz,
r = out + (i - 1) * KEYWRAP_BLOCK_SIZE;
XMEMCPY(tmp, a, KEYWRAP_BLOCK_SIZE);
XMEMCPY(tmp + KEYWRAP_BLOCK_SIZE, r, KEYWRAP_BLOCK_SIZE);
ret = wc_AesDecryptDirect(aes, tmp, tmp);
ret = wp11_AesDecryptDirect(aes, tmp, tmp);
if (ret != 0)
break;
XMEMCPY(a, tmp, KEYWRAP_BLOCK_SIZE);
Expand Down Expand Up @@ -17352,7 +17384,7 @@ int WP11_AesKeyWrapPad_Encrypt(unsigned char* plain, word32 plainSz,
unsigned char block[2 * KEYWRAP_BLOCK_SIZE];
XMEMCPY(block, aiv, KEYWRAP_BLOCK_SIZE);
XMEMCPY(block + KEYWRAP_BLOCK_SIZE, buf, KEYWRAP_BLOCK_SIZE);
ret = wc_AesEncryptDirect(&wrap->aes, enc, block);
ret = wp11_AesEncryptDirect(&wrap->aes, enc, block);
wc_ForceZero(block, sizeof(block));
}
else {
Expand Down Expand Up @@ -17401,7 +17433,7 @@ int WP11_AesKeyWrapPad_Decrypt(unsigned char* enc, word32 encSz,
if (encSz == 2 * KEYWRAP_BLOCK_SIZE) {
/* Single semiblock: ECB-decrypt to AIV || padded plaintext. */
unsigned char block[2 * KEYWRAP_BLOCK_SIZE];
ret = wc_AesDecryptDirect(&wrap->aes, block, enc);
ret = wp11_AesDecryptDirect(&wrap->aes, block, enc);
if (ret == 0) {
XMEMCPY(aiv, block, KEYWRAP_BLOCK_SIZE);
XMEMCPY(padBuf, block + KEYWRAP_BLOCK_SIZE, KEYWRAP_BLOCK_SIZE);
Expand Down
20 changes: 20 additions & 0 deletions tests/copyobject_token_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,21 @@ static int test_copy_token_findable(CK_SESSION_HANDLE session)
/* A failed persistence attempt must roll the new token object out of the
* in-memory list before C_CopyObject frees it. */
#ifndef WOLFPKCS11_NO_STORE
#define COPY_TOKEN_PROBE_FILE COPY_TOKEN_TEST_DIR "/.write_probe"

/* Returns 1 if a new file can still be created in the token storage
* directory. Privileged users (e.g. root) bypass directory permissions. */
static int test_store_dir_writable(void)
{
FILE* f = fopen(COPY_TOKEN_PROBE_FILE, "wb");

if (f == NULL)
return 0;
fclose(f);
(void)remove(COPY_TOKEN_PROBE_FILE);
return 1;
}

static int test_copy_token_store_failure(CK_SESSION_HANDLE session)
{
CK_RV ret;
Expand All @@ -486,6 +501,11 @@ static int test_copy_token_store_failure(CK_SESSION_HANDLE session)
CHECK_COND(ret == 0, "Test6: make token storage read-only");
storeReadOnly = 1;

if (test_store_dir_writable()) {
printf("SKIP: Test6: token storage still writable (running as root?)\n");
goto cleanup;
}

ret = funcList->C_CopyObject(session, src, copyTmpl, 1, &copy);
(void)TEST_SET_WRITABLE(COPY_TOKEN_TEST_DIR);
storeReadOnly = 0;
Expand Down
Loading