@@ -2168,6 +2168,8 @@ exit:
2168
2168
/* BEGIN_CASE */
2169
2169
void mac_operation_init( )
2170
2170
{
2171
+ const uint8_t input[1];
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,119 @@ 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
+
2270
+ /* Call finish without calling setup beforehand. */
2271
+ memset( &operation, 0, sizeof( operation ) );
2272
+ TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2273
+ &sign_mac_length),
2274
+ PSA_ERROR_BAD_STATE );
2275
+ memset( &operation, 0, sizeof( operation ) );
2276
+ TEST_EQUAL( psa_mac_verify_finish( &operation,
2277
+ verify_mac, sizeof( verify_mac ) ),
2278
+ PSA_ERROR_BAD_STATE );
2279
+
2280
+ /* Call update after sign finish. */
2281
+ memset( &operation, 0, sizeof( operation ) );
2282
+ PSA_ASSERT( psa_mac_sign_setup( &operation,
2283
+ handle, alg ) );
2284
+ PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2285
+ PSA_ASSERT( psa_mac_sign_finish( &operation,
2286
+ sign_mac, sizeof( sign_mac ),
2287
+ &sign_mac_length ) );
2288
+ TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2289
+ PSA_ERROR_BAD_STATE );
2290
+
2291
+ /* Call update after verify finish. */
2292
+ memset( &operation, 0, sizeof( operation ) );
2293
+ PSA_ASSERT( psa_mac_verify_setup( &operation,
2294
+ handle, alg ) );
2295
+ PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2296
+ PSA_ASSERT( psa_mac_verify_finish( &operation,
2297
+ verify_mac, sizeof( verify_mac ) ) );
2298
+ TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2299
+ PSA_ERROR_BAD_STATE );
2300
+
2301
+ /* Call sign finish twice in a row. */
2302
+ memset( &operation, 0, sizeof( operation ) );
2303
+ PSA_ASSERT( psa_mac_sign_setup( &operation,
2304
+ handle, alg ) );
2305
+ PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2306
+ PSA_ASSERT( psa_mac_sign_finish( &operation,
2307
+ sign_mac, sizeof( sign_mac ),
2308
+ &sign_mac_length ) );
2309
+ TEST_EQUAL( psa_mac_sign_finish( &operation,
2310
+ sign_mac, sizeof( sign_mac ),
2311
+ &sign_mac_length ),
2312
+ PSA_ERROR_BAD_STATE );
2313
+
2314
+ /* Call verify finish twice in a row. */
2315
+ memset( &operation, 0, sizeof( operation ) );
2316
+ PSA_ASSERT( psa_mac_verify_setup( &operation,
2317
+ handle, alg ) );
2318
+ PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2319
+ PSA_ASSERT( psa_mac_verify_finish( &operation,
2320
+ verify_mac, sizeof( verify_mac ) ) );
2321
+ TEST_EQUAL( psa_mac_verify_finish( &operation,
2322
+ verify_mac, sizeof( verify_mac ) ),
2323
+ PSA_ERROR_BAD_STATE );
2324
+
2325
+ /* Setup sign but try verify. */
2326
+ memset( &operation, 0, sizeof( operation ) );
2327
+ PSA_ASSERT( psa_mac_sign_setup( &operation,
2328
+ handle, alg ) );
2329
+ PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2330
+ TEST_EQUAL( psa_mac_verify_finish( &operation,
2331
+ verify_mac, sizeof( verify_mac ) ),
2332
+ PSA_ERROR_BAD_STATE );
2333
+
2334
+ /* Setup verify but try sign. */
2335
+ memset( &operation, 0, sizeof( operation ) );
2336
+ PSA_ASSERT( psa_mac_verify_setup( &operation,
2337
+ handle, alg ) );
2338
+ PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2339
+ TEST_EQUAL( psa_mac_sign_finish( &operation,
2340
+ sign_mac, sizeof( sign_mac ),
2341
+ &sign_mac_length ),
2342
+ PSA_ERROR_BAD_STATE );
2343
+
2344
+ exit:
2345
+ mbedtls_psa_crypto_free( );
2346
+ }
2347
+ /* END_CASE */
2348
+
2223
2349
/* BEGIN_CASE */
2224
2350
void mac_sign( int key_type_arg,
2225
2351
data_t *key,
@@ -2262,6 +2388,8 @@ void mac_sign( int key_type_arg,
2262
2388
actual_mac, mac_buffer_size,
2263
2389
&mac_length ) );
2264
2390
2391
+ /* Ensure double sign fails properly. */
2392
+
2265
2393
/* Compare with the expected value. */
2266
2394
ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2267
2395
actual_mac, mac_length );
@@ -2309,6 +2437,12 @@ void mac_verify( int key_type_arg,
2309
2437
expected_mac->x,
2310
2438
expected_mac->len ) );
2311
2439
2440
+ /* Ensure double verify fails properly. */
2441
+ TEST_EQUAL( psa_mac_verify_finish( &operation,
2442
+ expected_mac->x,
2443
+ expected_mac->len ),
2444
+ PSA_ERROR_BAD_STATE );
2445
+
2312
2446
exit:
2313
2447
psa_destroy_key( handle );
2314
2448
mbedtls_psa_crypto_free( );
0 commit comments