Skip to content

Commit a207794

Browse files
committed
Bring in support for MAXQ1065
1 parent 7808688 commit a207794

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

src/crypto.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2554,7 +2554,7 @@ CK_RV C_DigestFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pDigest,
25542554
CK_RV C_SignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
25552555
CK_OBJECT_HANDLE hKey)
25562556
{
2557-
int ret;
2557+
int ret = 0;
25582558
WP11_Session* session;
25592559
WP11_Object* obj = NULL;
25602560
CK_KEY_TYPE type;
@@ -2568,10 +2568,27 @@ CK_RV C_SignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
25682568
return CKR_ARGUMENTS_BAD;
25692569

25702570
ret = WP11_Object_Find(session, hKey, &obj);
2571-
if (ret != 0)
2571+
#ifdef WOLFSSL_MAXQ10XX_CRYPTO
2572+
if ((ret != 0) && (hKey == 0) && (pMechanism->mechanism == CKM_ECDSA)) {
2573+
if (pMechanism->pParameter != NULL || pMechanism->ulParameterLen != 0) {
2574+
return CKR_MECHANISM_PARAM_INVALID;
2575+
}
2576+
2577+
/* Do not worry; the private key is pre-provisioned, but note there is
2578+
* no object to set. */
2579+
init = WP11_INIT_ECDSA_SIGN;
2580+
WP11_Session_SetMechanism(session, pMechanism->mechanism);
2581+
WP11_Session_SetOpInitialized(session, init);
2582+
2583+
return CKR_OK;
2584+
} else
2585+
#endif
2586+
if (ret != 0) {
25722587
return CKR_OBJECT_HANDLE_INVALID;
2588+
}
25732589

25742590
type = WP11_Object_GetType(obj);
2591+
25752592
switch (pMechanism->mechanism) {
25762593
#ifndef NO_RSA
25772594
case CKM_RSA_X_509:
@@ -2770,8 +2787,9 @@ CK_RV C_Sign(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData,
27702787
#endif
27712788
#ifdef HAVE_ECC
27722789
case CKM_ECDSA:
2773-
if (!WP11_Session_IsOpInitialized(session, WP11_INIT_ECDSA_SIGN))
2790+
if (!WP11_Session_IsOpInitialized(session, WP11_INIT_ECDSA_SIGN)) {
27742791
return CKR_OPERATION_NOT_INITIALIZED;
2792+
}
27752793

27762794
sigLen = WP11_Ec_SigLen(obj);
27772795
if (pSignature == NULL) {

src/internal.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@
5959
#define WOLFPKCS11_NEED_RSA_RNG
6060
#endif
6161

62+
#if defined(WOLFPKCS11_TPM) && defined(WOLFSSL_MAXQ10XX_CRYPTO)
63+
#error "wolfTPM and MAXQ10XX are incompatable with each other."
64+
#endif
65+
6266
/* Helper to get size of struct field */
6367
#define FIELD_SIZE(type, field) (sizeof(((type *)0)->field))
6468

@@ -3538,8 +3542,10 @@ static int wp11_Slot_Init(WP11_Slot* slot, int id)
35383542

35393543
ret = WP11_Lock_Init(&slot->lock);
35403544
if (ret == 0) {
3541-
#ifdef WOLFPKCS11_TPM
3545+
#if defined(WOLFPKCS11_TPM)
35423546
ret = wp11_TpmInit(slot);
3547+
#elif defined (WOLFSSL_MAXQ10XX_CRYPTO)
3548+
slot->devId = MAXQ_DEVICE_ID;
35433549
#endif
35443550
/* Create the minimum number of unused sessions. */
35453551
for (i = 0; ret == 0 && i < WP11_SESSION_CNT_MIN; i++) {
@@ -3605,8 +3611,16 @@ int WP11_Library_Init(void)
36053611

36063612
if (libraryInitCount == 0) {
36073613
ret = WP11_Lock_Init(&globalLock);
3608-
if (ret == 0)
3614+
if (ret == 0) {
3615+
#ifdef WOLFSSL_MAXQ10XX_CRYPTO
3616+
ret = wolfCrypt_Init();
3617+
if (ret == 0) {
3618+
ret = wc_InitRng_ex(&globalRandom, NULL, MAXQ_DEVICE_ID);
3619+
}
3620+
#else
36093621
ret = wc_InitRng(&globalRandom);
3622+
#endif
3623+
}
36103624
for (i = 0; (ret == 0) && (i < slotCnt); i++) {
36113625
ret = wp11_Slot_Init(&slotList[i], i + 1);
36123626
}
@@ -4725,7 +4739,7 @@ int WP11_Session_SetCbcParams(WP11_Session* session, unsigned char* iv,
47254739
WP11_Data* key;
47264740

47274741
/* AES object on session. */
4728-
ret = wc_AesInit(&cbc->aes, NULL, INVALID_DEVID);
4742+
ret = wc_AesInit(&cbc->aes, NULL, session->devId);
47294743
if (ret == 0) {
47304744
if (object->onToken)
47314745
WP11_Lock_LockRO(object->lock);
@@ -7659,7 +7673,7 @@ int WP11_EC_Derive(unsigned char* point, word32 pointLen, unsigned char* key,
76597673
WC_RNG rng;
76607674
#endif
76617675

7662-
ret = wc_ecc_init_ex(&pubKey, NULL, INVALID_DEVID);
7676+
ret = wc_ecc_init_ex(&pubKey, NULL, priv->slot->devId);
76637677
if (ret == 0) {
76647678
ret = wc_ecc_import_x963(point, pointLen, &pubKey);
76657679
}
@@ -8280,7 +8294,7 @@ int WP11_AesGcm_Encrypt(unsigned char* plain, word32 plainSz,
82808294
word32 authTagSz = gcm->tagBits / 8;
82818295
unsigned char* authTag = enc + plainSz;
82828296

8283-
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
8297+
ret = wc_AesInit(&aes, NULL, session->devId);
82848298
if (ret == 0) {
82858299
if (secret->onToken)
82868300
WP11_Lock_LockRO(secret->lock);
@@ -8332,7 +8346,7 @@ int WP11_AesGcm_EncryptUpdate(unsigned char* plain, word32 plainSz,
83328346
word32 authTagSz = gcm->tagBits / 8;
83338347
unsigned char* authTag = gcm->authTag;
83348348

8335-
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
8349+
ret = wc_AesInit(&aes, NULL, session->devId);
83368350
if (ret == 0) {
83378351
if (secret->onToken)
83388352
WP11_Lock_LockRO(secret->lock);
@@ -8412,7 +8426,7 @@ int WP11_AesGcm_Decrypt(unsigned char* enc, word32 encSz, unsigned char* dec,
84128426
word32 authTagSz = gcm->tagBits / 8;
84138427
unsigned char* authTag = enc + encSz - authTagSz;
84148428

8415-
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
8429+
ret = wc_AesInit(&aes, NULL, session->devId);
84168430
if (ret == 0) {
84178431
if (secret->onToken) {
84188432
WP11_Lock_LockRO(secret->lock);
@@ -8737,7 +8751,7 @@ int WP11_Hmac_Init(CK_MECHANISM_TYPE mechanism, WP11_Object* secret,
87378751
if (ret == 0)
87388752
hmac->hmacSz = wc_HmacSizeByType(hashType);
87398753
if (ret == 0)
8740-
ret = wc_HmacInit(&hmac->hmac, NULL, INVALID_DEVID);
8754+
ret = wc_HmacInit(&hmac->hmac, NULL, secret->slot->devId);
87418755
if (ret == 0) {
87428756
if (secret->onToken)
87438757
WP11_Lock_LockRO(secret->lock);

tests/pkcs11test.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2930,8 +2930,8 @@ static CK_RV test_pubkey_sig_fail(CK_SESSION_HANDLE session, CK_MECHANISM* mech,
29302930
CHECK_CKR_FAIL(ret, CKR_OPERATION_NOT_INITIALIZED, "Verify wrong init");
29312931
}
29322932
if (ret == CKR_OK) {
2933-
ret = funcList->C_VerifyInit(session, mech, pub);
2934-
CHECK_CKR(ret, "Verify Init");
2933+
ret = funcList->C_VerifyInit(session, mech, pub);
2934+
CHECK_CKR(ret, "Verify Init");
29352935
}
29362936
if (ret == CKR_OK) {
29372937
ret = funcList->C_Sign(session, hash, hashSz, out, &outSz);
@@ -3794,11 +3794,13 @@ static CK_RV test_rsa_fixed_keys_oaep(void* args)
37943794
0);
37953795
CHECK_CKR(ret, "SHA1 No AAD");
37963796
}
3797+
#ifdef WOLFSSL_SHA224
37973798
if (ret == CKR_OK) {
37983799
ret = rsa_oaep_test(session, priv, pub, CKM_SHA224, CKG_MGF1_SHA224,
37993800
NULL, 0);
38003801
CHECK_CKR(ret, "SHA224 No AAD");
38013802
}
3803+
#endif
38023804
if (ret == CKR_OK) {
38033805
ret = rsa_oaep_test(session, priv, pub, CKM_SHA384, CKG_MGF1_SHA384,
38043806
NULL, 0);
@@ -3893,10 +3895,12 @@ static CK_RV test_rsa_fixed_keys_pss(void* args)
38933895
ret = rsa_pss_test(session, priv, pub, CKM_SHA1, CKG_MGF1_SHA1, 20);
38943896
CHECK_CKR(ret, "RSA PKCS#1 PSS - SHA1");
38953897
}
3898+
#ifdef WOLFSSL_SHA224
38963899
if (ret == CKR_OK) {
38973900
ret = rsa_pss_test(session, priv, pub, CKM_SHA224, CKG_MGF1_SHA224, 28);
38983901
CHECK_CKR(ret, "RSA PKCS#1 PSS - SHA224");
38993902
}
3903+
#endif
39003904
if (ret == CKR_OK) {
39013905
ret = rsa_pss_test(session, priv, pub, CKM_SHA384, CKG_MGF1_SHA384, 48);
39023906
CHECK_CKR(ret, "RSA PKCS#1 PSS - SHA384");
@@ -4819,10 +4823,15 @@ static CK_RV ecdsa_test(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE privKey,
48194823
ret = funcList->C_VerifyInit(session, &mech, pubKey);
48204824
CHECK_CKR(ret, "ECDSA Verify Init");
48214825
}
4826+
#ifndef WOLFSSL_MAXQ10XX_CRYPTO
4827+
/* In the case of MAXQ1065 it will be signed by the pre-provisioned private
4828+
* key so verify operation will fail as this is NOT the corresponding
4829+
* public key. */
48224830
if (ret == CKR_OK) {
48234831
ret = funcList->C_Verify(session, hash, hashSz, out, outSz);
48244832
CHECK_CKR(ret, "ECDSA Verify");
48254833
}
4834+
#endif
48264835
if (ret == CKR_OK) {
48274836
ret = funcList->C_Verify(session, hash, hashSz - 1, out, outSz);
48284837
CHECK_CKR_FAIL(ret, CKR_SIGNATURE_INVALID, "ECDSA Verify bad hash");

0 commit comments

Comments
 (0)