|
6 | 6 | * license that can be found in the LICENSE file. |
7 | 7 | */ |
8 | 8 |
|
9 | | -#include <assert.h> |
10 | | -#include <stdio.h> |
11 | | -#include <string.h> |
12 | | -#include <sdkconfig.h> |
13 | | - |
14 | | -#include <fido.h> |
15 | | - |
16 | | -#ifdef CONFIG_USE_HW_CRYPTO |
17 | | -#include <mbedtls/aes.h> |
18 | | -#include <mbedtls/gcm.h> |
19 | | -#include <mbedtls/sha256.h> |
20 | | -#include <mbedtls/sha512.h> |
21 | | -#endif |
22 | | - |
23 | | -#ifdef CONFIG_USE_HW_CRYPTO |
24 | | -int sha256(const uint8_t *data, size_t data_len, uint8_t *hash) { |
25 | | - int r = mbedtls_sha256(data, data_len, hash, 0); |
26 | | - if (r != 0) { |
27 | | - printf("sha256 failed with %d\n", r); |
28 | | - } |
29 | | - return r; |
30 | | -} |
31 | | - |
32 | | -int sha512(const uint8_t *data, size_t data_len, uint8_t *hash) { |
33 | | - int r = mbedtls_sha512(data, data_len, hash, 0); |
34 | | - if (r != 0) { |
35 | | - printf("sha512 failed with %d\n", r); |
36 | | - } |
37 | | - return r; |
38 | | -} |
39 | | - |
40 | | -int aes_gcm_encrypt( |
41 | | - const uint8_t *key, size_t key_len, |
42 | | - const uint8_t *iv, size_t iv_len, |
43 | | - const uint8_t *plaintext, size_t plaintext_len, |
44 | | - const uint8_t *aad, size_t aad_len, |
45 | | - uint8_t *ciphertext, uint8_t *tag |
46 | | -) { |
47 | | - mbedtls_gcm_context ctx; |
48 | | - int r; |
49 | | - |
50 | | - mbedtls_gcm_init(&ctx); |
51 | | - |
52 | | - r = mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key, key_len * 8); |
53 | | - if (r != 0) { |
54 | | - printf("[%s] mbedtls_gcm_setkey failed with %d\n", __func__, r); |
55 | | - return r; |
56 | | - } |
57 | | - |
58 | | - r = mbedtls_gcm_crypt_and_tag( |
59 | | - &ctx, |
60 | | - MBEDTLS_ENCRYPT, |
61 | | - plaintext_len, |
62 | | - iv, iv_len, |
63 | | - aad, aad_len, |
64 | | - ciphertext, plaintext, |
65 | | - 16, tag |
66 | | - ); |
67 | | - if (r != 0) { |
68 | | - printf("[%s] mbedtls_gcm_crypt_and_tag failed with %d\n", __func__, r); |
69 | | - return r; |
70 | | - } |
71 | | - |
72 | | - mbedtls_gcm_free(&ctx); |
73 | | - |
74 | | - return 0; |
75 | | -} |
76 | | - |
77 | | -int aes_gcm_decrypt( |
78 | | - const uint8_t *key, size_t key_len, |
79 | | - const uint8_t *iv, size_t iv_len, |
80 | | - const uint8_t *ciphertext, size_t ciphertext_len, |
81 | | - const uint8_t *aad, size_t aad_len, |
82 | | - const uint8_t *tag, |
83 | | - uint8_t *plaintext |
84 | | -) { |
85 | | - mbedtls_gcm_context ctx; |
86 | | - int r; |
87 | | - |
88 | | - mbedtls_gcm_init(&ctx); |
89 | | - |
90 | | - r = mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key, key_len * 8); |
91 | | - if (r != 0) { |
92 | | - printf("[%s] mbedtls_gcm_setkey failed with %d\n", __func__, r); |
93 | | - return r; |
94 | | - } |
95 | | - |
96 | | - r = mbedtls_gcm_crypt_and_tag( |
97 | | - &ctx, |
98 | | - MBEDTLS_DECRYPT, |
99 | | - ciphertext_len, |
100 | | - iv, iv_len, |
101 | | - aad, aad_len, |
102 | | - ciphertext, plaintext, |
103 | | - 16, tag |
104 | | - ); |
105 | | - if (r != 0) { |
106 | | - printf("[%s] mbedtls_gcm_crypt_and_tag failed with %d\n", __func__, r); |
107 | | - return r; |
108 | | - } |
109 | | - |
110 | | - mbedtls_gcm_free(&ctx); |
111 | | - |
112 | | - return 0; |
113 | | -} |
114 | | - |
115 | | -void init_crypto() { |
116 | | - fido_sha256 = &sha256; |
117 | | - fido_sha512 = &sha512; |
118 | | - fido_aes_gcm_encrypt = &aes_gcm_encrypt; |
119 | | - fido_aes_gcm_decrypt = &aes_gcm_decrypt; |
120 | | -} |
121 | | -#endif |
122 | | - |
123 | 9 | #include <fido.h> |
| 10 | +#include <stdio.h> |
| 11 | +#include "hw_crypto.h" |
| 12 | +#include "clock_cycles.h" |
124 | 13 | #include "stateless_rp/stateless_rp.h" |
125 | 14 | #include "stateless_rp/stateless_rp_nfc_simulator.h" |
126 | 15 |
|
127 | 16 | int app_main(void) { |
128 | | - #ifdef CONFIG_USE_HW_CRYPTO |
129 | | - init_crypto(); |
130 | | - #endif |
| 17 | + clock_init(); |
| 18 | + if (init_hw_crypto() != 0) { |
| 19 | + return -1; |
| 20 | + } else { |
| 21 | + printf("Initialized cryptography.\n"); |
| 22 | + } |
131 | 23 |
|
132 | 24 | fido_dev_t dev; |
133 | | - |
134 | 25 | if (prepare_stateless_rp_nfc_simulator_device(&dev) != 0) { |
135 | | - return 1; |
| 26 | + printf("Could not setup simulator device.\n"); |
| 27 | + return -1; |
136 | 28 | } |
137 | | - |
138 | 29 | const uint8_t updater_public_key[] = {0xA8, 0xEE, 0x4D, 0x2B, 0xD5, 0xAE, 0x09, 0x0A, 0xBC, 0xA9, 0x8A, 0x06, 0x6C, 0xA5, 0xB3, 0xA6, 0x22, 0x84, 0x89, 0xF5, 0x9E, 0x30, 0x90, 0x87, 0x65, 0x62, 0xB9, 0x79, 0x8A, 0xE7, 0x05, 0x15}; |
139 | | - return stateless_assert(&dev, "example.com", updater_public_key); |
| 30 | + clock_start_counting(); |
| 31 | + const int ret = stateless_assert(&dev, "example.com", updater_public_key); |
| 32 | + uint64_t elapsed_cycles = clock_stop_counting(); |
| 33 | + printf("Elapsed cycles for stateless assertion: %zu\n", elapsed_cycles); |
| 34 | + printf("Elapsed nanoseconds for stateless assertion: %zu\n", clock_cyles_to_ns(elapsed_cycles)); |
| 35 | + return ret; |
140 | 36 | } |
0 commit comments