@@ -2499,9 +2499,24 @@ static int _HandleCmac(whServerContext* ctx, uint16_t magic, uint16_t seq,
24992499 len = sizeof (ctx -> crypto -> algoCtx .cmac );
25002500 keyId = wh_KeyId_TranslateFromClient (
25012501 WH_KEYTYPE_CRYPTO , ctx -> comm -> client_id , req .keyId );
2502- ret = wh_Server_KeystoreReadKey (
2503- ctx , keyId , NULL , (uint8_t * )ctx -> crypto -> algoCtx .cmac ,
2504- (uint32_t * )& len );
2502+
2503+ /* Validate key usage policy - CMAC accepts sign or verify */
2504+ if (!WH_KEYID_ISERASED (keyId )) {
2505+ ret = wh_Server_KeystoreFindEnforceKeyUsage (
2506+ ctx , keyId , WH_NVM_FLAGS_USAGE_SIGN );
2507+ if (ret == WH_ERROR_USAGE ) {
2508+ /* Sign not allowed, try verify */
2509+ ret = wh_Server_KeystoreFindEnforceKeyUsage (
2510+ ctx , keyId , WH_NVM_FLAGS_USAGE_VERIFY );
2511+ }
2512+ if (ret != WH_ERROR_OK ) {
2513+ return ret ;
2514+ }
2515+ }
2516+
2517+ ret = wh_Server_KeystoreReadKey (
2518+ ctx , keyId , meta , (uint8_t * )ctx -> crypto -> algoCtx .cmac ,
2519+ (uint32_t * )& len );
25052520 if (ret == WH_ERROR_OK ) {
25062521 /* if the key size is a multiple of aes, init the key and
25072522 * overwrite the existing key on exit */
@@ -2612,12 +2627,22 @@ static int _HandleCmac(whServerContext* ctx, uint16_t magic, uint16_t seq,
26122627 if (moveToBigCache == 1 ) {
26132628 ret = wh_Server_KeystoreEvictKey (ctx , keyId );
26142629 }
2615- if (ret == 0 ) {
2630+ if (ret == WH_ERROR_OK ) {
26162631 meta -> id = keyId ;
26172632 meta -> len = sizeof (ctx -> crypto -> algoCtx .cmac );
2618- ret = wh_Server_KeystoreCacheKey (
2633+ /* Nonzero key size means the client provided the key and
2634+ * wasn't able to provide flags, therefore we tag the key as
2635+ * useable for sign/verify operations. If the client instead
2636+ * wants to refer to the key by ID, meta->flags should
2637+ * already hold the flags set on the original AES key from
2638+ * the earlier read operation */
2639+ if (req .keySz != 0 ) {
2640+ meta -> flags =
2641+ WH_NVM_FLAGS_USAGE_SIGN | WH_NVM_FLAGS_USAGE_VERIFY ;
2642+ }
2643+ ret = wh_Server_KeystoreCacheKey (
26192644 ctx , meta , (uint8_t * )ctx -> crypto -> algoCtx .cmac );
2620- if (ret == 0 ) {
2645+ if (ret == WH_ERROR_OK ) {
26212646 res .keyId = wh_KeyId_TranslateToClient (keyId );
26222647 res .outSz = 0 ;
26232648 }
@@ -4692,6 +4717,22 @@ static int _HandleCmacDma(whServerContext* ctx, uint16_t magic, uint16_t seq,
46924717 keyId = wh_KeyId_TranslateFromClient (
46934718 WH_KEYTYPE_CRYPTO , ctx -> comm -> client_id ,
46944719 clientKeyId );
4720+
4721+ /* Validate key usage policy - CMAC accepts sign or
4722+ * verify */
4723+ if (!WH_KEYID_ISERASED (keyId )) {
4724+ ret = wh_Server_KeystoreFindEnforceKeyUsage (
4725+ ctx , keyId , WH_NVM_FLAGS_USAGE_SIGN );
4726+ if (ret == WH_ERROR_USAGE ) {
4727+ /* Sign not allowed, try verify */
4728+ ret = wh_Server_KeystoreFindEnforceKeyUsage (
4729+ ctx , keyId , WH_NVM_FLAGS_USAGE_VERIFY );
4730+ }
4731+ if (ret != WH_ERROR_OK ) {
4732+ return ret ;
4733+ }
4734+ }
4735+
46954736 keyLen = sizeof (tmpKey );
46964737
46974738 /* Load key from cache */
@@ -4756,6 +4797,22 @@ static int _HandleCmacDma(whServerContext* ctx, uint16_t magic, uint16_t seq,
47564797 /* Get key ID from CMAC context */
47574798 keyId = wh_KeyId_TranslateFromClient (
47584799 WH_KEYTYPE_CRYPTO , ctx -> comm -> client_id , nvmId );
4800+
4801+ /* Validate key usage policy - CMAC accepts sign or
4802+ * verify */
4803+ if (!WH_KEYID_ISERASED (keyId )) {
4804+ ret = wh_Server_KeystoreFindEnforceKeyUsage (
4805+ ctx , keyId , WH_NVM_FLAGS_USAGE_SIGN );
4806+ if (ret == WH_ERROR_USAGE ) {
4807+ /* Sign not allowed, try verify */
4808+ ret = wh_Server_KeystoreFindEnforceKeyUsage (
4809+ ctx , keyId , WH_NVM_FLAGS_USAGE_VERIFY );
4810+ }
4811+ if (ret != WH_ERROR_OK ) {
4812+ return ret ;
4813+ }
4814+ }
4815+
47594816 keyLen = sizeof (tmpKey );
47604817
47614818 /* Load key from cache */
0 commit comments