Software APIs
aes_gcm_functest.c
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #include <stddef.h>
6 
8 #include "sw/device/lib/crypto/drivers/entropy.h"
11 #include "sw/device/tests/crypto/aes_gcm_testutils.h"
12 #include "sw/device/tests/crypto/aes_gcm_testvectors.h"
13 
14 // Global pointer to the current test vector.
15 const aes_gcm_test_t *current_test = NULL;
16 
17 static status_t encrypt_test(void) {
18  uint32_t cycles;
19  TRY(aes_gcm_testutils_encrypt(current_test, /*streaming=*/false, &cycles));
20  LOG_INFO("Encrypt cycles: %d", cycles);
21  return OK_STATUS();
22 }
23 
24 static status_t decrypt_test(void) {
25  uint32_t cycles;
26  hardened_bool_t tag_valid;
27  TRY(aes_gcm_testutils_decrypt(current_test, &tag_valid, /*streaming=*/false,
28  &cycles));
29  LOG_INFO("Decrypt cycles: %d", cycles);
30  TRY_CHECK(tag_valid == kHardenedBoolTrue);
31  return OK_STATUS();
32 }
33 
34 static status_t encrypt_streaming_test(void) {
35  uint32_t cycles;
36  TRY(aes_gcm_testutils_encrypt(current_test, /*streaming=*/true, &cycles));
37  LOG_INFO("Encrypt streaming cycles: %d", cycles);
38  return OK_STATUS();
39 }
40 
41 static status_t decrypt_streaming_test(void) {
42  uint32_t cycles;
43  hardened_bool_t tag_valid;
44  TRY(aes_gcm_testutils_decrypt(current_test, &tag_valid, /*streaming=*/true,
45  &cycles));
46  LOG_INFO("Decrypt streaming cycles: %d", cycles);
47  TRY_CHECK(tag_valid == kHardenedBoolTrue);
48  return OK_STATUS();
49 }
50 
51 OTTF_DEFINE_TEST_CONFIG();
52 
53 bool test_main(void) {
54  status_t result = OK_STATUS();
55 
56  CHECK_STATUS_OK(entropy_complex_init());
57 
58  for (size_t i = 0; i < ARRAYSIZE(kAesGcmTestvectors); i++) {
59  LOG_INFO("Starting AES-GCM test %d of %d...", i + 1,
60  ARRAYSIZE(kAesGcmTestvectors));
61  current_test = &kAesGcmTestvectors[i];
62  LOG_INFO("Key length = %d", current_test->key_len * sizeof(uint32_t));
63  LOG_INFO("Aad length = %d", current_test->aad_len);
64  LOG_INFO("Encrypted data length = %d", current_test->plaintext_len);
65  LOG_INFO("Tag length = %d", current_test->tag_len);
66  EXECUTE_TEST(result, encrypt_test);
67  EXECUTE_TEST(result, decrypt_test);
68  EXECUTE_TEST(result, encrypt_streaming_test);
69  EXECUTE_TEST(result, decrypt_streaming_test);
70  }
71 
72  return status_ok(result);
73 }