@@ -2168,6 +2168,8 @@ exit:
2168
2168
/* BEGIN_CASE */
2169
2169
void mac_operation_init( )
2170
2170
{
2171
+ const uint8_t input[1] = { 0 };
2172
+
2171
2173
/* Test each valid way of initializing the object, except for `= {0}`, as
2172
2174
* Clang 5 complains when `-Wmissing-field-initializers` is used, even
2173
2175
* though it's OK by the C standard. We could test for this, but we'd need
@@ -2178,6 +2180,17 @@ void mac_operation_init( )
2178
2180
2179
2181
memset( &zero, 0, sizeof( zero ) );
2180
2182
2183
+ /* A default MAC operation should not be usable. */
2184
+ TEST_EQUAL( psa_mac_update( &func,
2185
+ input, sizeof( input ) ),
2186
+ PSA_ERROR_BAD_STATE );
2187
+ TEST_EQUAL( psa_mac_update( &init,
2188
+ input, sizeof( input ) ),
2189
+ PSA_ERROR_BAD_STATE );
2190
+ TEST_EQUAL( psa_mac_update( &zero,
2191
+ input, sizeof( input ) ),
2192
+ PSA_ERROR_BAD_STATE );
2193
+
2181
2194
/* A default MAC operation should be abortable without error. */
2182
2195
PSA_ASSERT( psa_mac_abort( &func ) );
2183
2196
PSA_ASSERT( psa_mac_abort( &init ) );
@@ -2220,6 +2233,122 @@ exit:
2220
2233
}
2221
2234
/* END_CASE */
2222
2235
2236
+ /* BEGIN_CASE */
2237
+ void mac_bad_order( )
2238
+ {
2239
+ psa_key_handle_t handle = 0;
2240
+ psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2241
+ psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2242
+ const uint8_t key[] = {
2243
+ 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2244
+ 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2245
+ 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
2246
+ psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
2247
+ psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2248
+ uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2249
+ size_t sign_mac_length = 0;
2250
+ const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2251
+ const uint8_t verify_mac[] = {
2252
+ 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2253
+ 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2254
+ 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2255
+
2256
+ PSA_ASSERT( psa_crypto_init( ) );
2257
+ PSA_ASSERT( psa_allocate_key( &handle ) );
2258
+ psa_key_policy_set_usage( &policy,
2259
+ PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2260
+ alg );
2261
+ PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
2262
+
2263
+ PSA_ASSERT( psa_import_key( handle, key_type,
2264
+ key, sizeof(key) ) );
2265
+
2266
+ /* Call update without calling setup beforehand. */
2267
+ TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2268
+ PSA_ERROR_BAD_STATE );
2269
+ PSA_ASSERT( psa_mac_abort( &operation ) );
2270
+
2271
+ /* Call sign finish without calling setup beforehand. */
2272
+ TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2273
+ &sign_mac_length),
2274
+ PSA_ERROR_BAD_STATE );
2275
+ PSA_ASSERT( psa_mac_abort( &operation ) );
2276
+
2277
+ /* Call verify finish without calling setup beforehand. */
2278
+ TEST_EQUAL( psa_mac_verify_finish( &operation,
2279
+ verify_mac, sizeof( verify_mac ) ),
2280
+ PSA_ERROR_BAD_STATE );
2281
+ PSA_ASSERT( psa_mac_abort( &operation ) );
2282
+
2283
+ /* Call update after sign finish. */
2284
+ PSA_ASSERT( psa_mac_sign_setup( &operation,
2285
+ handle, alg ) );
2286
+ PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2287
+ PSA_ASSERT( psa_mac_sign_finish( &operation,
2288
+ sign_mac, sizeof( sign_mac ),
2289
+ &sign_mac_length ) );
2290
+ TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2291
+ PSA_ERROR_BAD_STATE );
2292
+ PSA_ASSERT( psa_mac_abort( &operation ) );
2293
+
2294
+ /* Call update after verify finish. */
2295
+ PSA_ASSERT( psa_mac_verify_setup( &operation,
2296
+ handle, alg ) );
2297
+ PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2298
+ PSA_ASSERT( psa_mac_verify_finish( &operation,
2299
+ verify_mac, sizeof( verify_mac ) ) );
2300
+ TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2301
+ PSA_ERROR_BAD_STATE );
2302
+ PSA_ASSERT( psa_mac_abort( &operation ) );
2303
+
2304
+ /* Call sign finish twice in a row. */
2305
+ PSA_ASSERT( psa_mac_sign_setup( &operation,
2306
+ handle, alg ) );
2307
+ PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2308
+ PSA_ASSERT( psa_mac_sign_finish( &operation,
2309
+ sign_mac, sizeof( sign_mac ),
2310
+ &sign_mac_length ) );
2311
+ TEST_EQUAL( psa_mac_sign_finish( &operation,
2312
+ sign_mac, sizeof( sign_mac ),
2313
+ &sign_mac_length ),
2314
+ PSA_ERROR_BAD_STATE );
2315
+ PSA_ASSERT( psa_mac_abort( &operation ) );
2316
+
2317
+ /* Call verify finish twice in a row. */
2318
+ PSA_ASSERT( psa_mac_verify_setup( &operation,
2319
+ handle, alg ) );
2320
+ PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2321
+ PSA_ASSERT( psa_mac_verify_finish( &operation,
2322
+ verify_mac, sizeof( verify_mac ) ) );
2323
+ TEST_EQUAL( psa_mac_verify_finish( &operation,
2324
+ verify_mac, sizeof( verify_mac ) ),
2325
+ PSA_ERROR_BAD_STATE );
2326
+ PSA_ASSERT( psa_mac_abort( &operation ) );
2327
+
2328
+ /* Setup sign but try verify. */
2329
+ PSA_ASSERT( psa_mac_sign_setup( &operation,
2330
+ handle, alg ) );
2331
+ PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2332
+ TEST_EQUAL( psa_mac_verify_finish( &operation,
2333
+ verify_mac, sizeof( verify_mac ) ),
2334
+ PSA_ERROR_BAD_STATE );
2335
+ PSA_ASSERT( psa_mac_abort( &operation ) );
2336
+
2337
+ /* Setup verify but try sign. */
2338
+ PSA_ASSERT( psa_mac_verify_setup( &operation,
2339
+ handle, alg ) );
2340
+ PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2341
+ TEST_EQUAL( psa_mac_sign_finish( &operation,
2342
+ sign_mac, sizeof( sign_mac ),
2343
+ &sign_mac_length ),
2344
+ PSA_ERROR_BAD_STATE );
2345
+ PSA_ASSERT( psa_mac_abort( &operation ) );
2346
+
2347
+ exit:
2348
+ mbedtls_psa_crypto_free( );
2349
+ }
2350
+ /* END_CASE */
2351
+
2223
2352
/* BEGIN_CASE */
2224
2353
void mac_sign( int key_type_arg,
2225
2354
data_t *key,
@@ -2309,6 +2438,12 @@ void mac_verify( int key_type_arg,
2309
2438
expected_mac->x,
2310
2439
expected_mac->len ) );
2311
2440
2441
+ /* Ensure double verify fails properly. */
2442
+ TEST_EQUAL( psa_mac_verify_finish( &operation,
2443
+ expected_mac->x,
2444
+ expected_mac->len ),
2445
+ PSA_ERROR_BAD_STATE );
2446
+
2312
2447
exit:
2313
2448
psa_destroy_key( handle );
2314
2449
mbedtls_psa_crypto_free( );
0 commit comments