aboutsummaryrefslogtreecommitdiff
path: root/components/service/crypto/test/service/crypto_service_op_tests.cpp
blob: cd4bd04064339637cb7d7c7369318e8ced19ed8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 * Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <string>
#include <cstring>
#include <cstdint>
#include <cassert>
#include <service/crypto/client/cpp/crypto_client.h>
#include <service_locator.h>
#include <CppUTest/TestHarness.h>

/*
 * Service-level tests that focus on exercising each supported operation.
 * These are mainly valid behaviour tests with the goal of checking
 * that the number of operations supported is as expected.
 */
TEST_GROUP(CryptoServiceOpTests)
{
    void setup()
    {
        struct rpc_caller *caller;
        int status;

        m_rpc_session_handle = NULL;
        m_crypto_service_context = NULL;
        m_crypto_client = NULL;

        service_locator_init();

        m_crypto_service_context = service_locator_query("sn:trustedfirmware.org:crypto:0", &status);
        assert(m_crypto_service_context);

        m_rpc_session_handle = service_context_open(m_crypto_service_context, &caller);
        assert(m_rpc_session_handle);

        m_crypto_client = new crypto_client(caller);
    }

    void teardown()
    {
        delete m_crypto_client;
        m_crypto_client = NULL;

        service_context_close(m_crypto_service_context, m_rpc_session_handle);
        m_rpc_session_handle = NULL;

        service_context_relinquish(m_crypto_service_context);
        m_crypto_service_context = NULL;
    }

    rpc_session_handle m_rpc_session_handle;
    struct service_context *m_crypto_service_context;
    crypto_client *m_crypto_client;
};

TEST(CryptoServiceOpTests, generateVolatileKeys)
{
    psa_status_t status;
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;

    psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_VOLATILE);
    psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
    psa_set_key_algorithm(&attributes, PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256));
    psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_CURVE_SECP_R1));
    psa_set_key_bits(&attributes, 256);

    /* Generate first key */
     psa_key_handle_t key_handle_1;
    status = m_crypto_client->generate_key(&attributes, &key_handle_1);
    CHECK_EQUAL(PSA_SUCCESS, status);

    /* And another */
     psa_key_handle_t key_handle_2;
    status = m_crypto_client->generate_key(&attributes, &key_handle_2);
    CHECK_EQUAL(PSA_SUCCESS, status);

    /* Expect the key handles to be different */
    CHECK(key_handle_1 != key_handle_2);

    /* Remove the keys */
    status = m_crypto_client->destroy_key(key_handle_1);
    CHECK_EQUAL(PSA_SUCCESS, status);
    status = m_crypto_client->destroy_key(key_handle_2);
    CHECK_EQUAL(PSA_SUCCESS, status);

    psa_reset_key_attributes(&attributes);
}

TEST(CryptoServiceOpTests, generatePersistentKeys)
{
    psa_status_t status;
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;

    psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
    psa_set_key_algorithm(&attributes, PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256));
    psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_CURVE_SECP_R1));
    psa_set_key_bits(&attributes, 256);

    /* First try and generate a key with an invalid keu id */
    psa_key_id_t key_id_invalid = 0;
    psa_set_key_id(&attributes, key_id_invalid);
    psa_key_handle_t key_handle_invalid;
    status = m_crypto_client->generate_key(&attributes, &key_handle_invalid);
    CHECK_EQUAL(PSA_ERROR_INVALID_ARGUMENT, status);

    /* Generate first key */
    psa_key_id_t key_id_1 = 100000;
    psa_set_key_id(&attributes, key_id_1);
    psa_key_handle_t key_handle_1;
    status = m_crypto_client->generate_key(&attributes, &key_handle_1);
    CHECK_EQUAL(PSA_SUCCESS, status);

    /* And another */
    psa_key_id_t key_id_2 = 2;
    psa_set_key_id(&attributes, key_id_2);
    psa_key_handle_t key_handle_2;
    status = m_crypto_client->generate_key(&attributes, &key_handle_2);
    CHECK_EQUAL(PSA_SUCCESS, status);

    /* Expect the key handles to be different */
    CHECK(key_handle_1 != key_handle_2);

    /* Obtain more handles using key_open */
    psa_key_handle_t key_handle_3;
    status = m_crypto_client->open_key(key_id_1, &key_handle_3);
    CHECK_EQUAL(PSA_SUCCESS, status);

    psa_key_handle_t key_handle_4;
    status = m_crypto_client->open_key(key_id_1, &key_handle_4);
    CHECK_EQUAL(PSA_SUCCESS, status);

    /* Relinquish handles */
    status = m_crypto_client->close_key(key_handle_3);
    CHECK_EQUAL(PSA_SUCCESS, status);
    status = m_crypto_client->close_key(key_handle_4);
    CHECK_EQUAL(PSA_SUCCESS, status);

    /* Expect close handle to now be invalid */
    status = m_crypto_client->close_key(key_handle_4);
    CHECK_EQUAL(PSA_ERROR_INVALID_HANDLE, status);

    /* Remove the keys */
    status = m_crypto_client->destroy_key(key_handle_1);
    CHECK_EQUAL(PSA_SUCCESS, status);
    status = m_crypto_client->destroy_key(key_handle_2);
    CHECK_EQUAL(PSA_SUCCESS, status);

    psa_reset_key_attributes(&attributes);
}

TEST(CryptoServiceOpTests, exportPublicKey)
{
    psa_status_t status;
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
    psa_key_handle_t key_handle;

    psa_set_key_id(&attributes, 10);
    psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
    psa_set_key_algorithm(&attributes, PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256));
    psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_CURVE_SECP_R1));
    psa_set_key_bits(&attributes, 256);

    /* Generate a key */
    status = m_crypto_client->generate_key(&attributes, &key_handle);
    CHECK_EQUAL(PSA_SUCCESS, status);

    psa_reset_key_attributes(&attributes);

    /* Export the public key */
    uint8_t key_buf[PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(256)];
    size_t key_len = 0;

    status = m_crypto_client->export_public_key(key_handle, key_buf, sizeof(key_buf), &key_len);
    CHECK_EQUAL(PSA_SUCCESS, status);
    CHECK(key_len > 0);

    /* Remove the key */
    status = m_crypto_client->destroy_key(key_handle);
    CHECK_EQUAL(PSA_SUCCESS, status);
}

TEST(CryptoServiceOpTests, exportAndImportKeyPair)
{
    psa_status_t status;
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
    psa_key_handle_t key_handle_1;
    psa_key_handle_t key_handle_2;

    psa_set_key_id(&attributes, 11);
    psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_EXPORT);
    psa_set_key_algorithm(&attributes, PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256));
    psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_CURVE_SECP_R1));
    psa_set_key_bits(&attributes, 256);

    /* Generate a key */
    status = m_crypto_client->generate_key(&attributes, &key_handle_1);
    CHECK_EQUAL(PSA_SUCCESS, status);

    /* Export the key pair */
    uint8_t key_buf[PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(256)];
    size_t key_len = 0;

    status = m_crypto_client->export_key(key_handle_1, key_buf, sizeof(key_buf), &key_len);
    CHECK_EQUAL(PSA_SUCCESS, status);
    CHECK(key_len > 0);

    /* Import the key pair value with a different key id */
    psa_set_key_id(&attributes, 12);
    status = m_crypto_client->import_key(&attributes, key_buf, key_len, &key_handle_2);
    CHECK_EQUAL(PSA_SUCCESS, status);

    psa_reset_key_attributes(&attributes);

    /* Remove the keys */
    status = m_crypto_client->destroy_key(key_handle_1);
    CHECK_EQUAL(PSA_SUCCESS, status);
    status = m_crypto_client->destroy_key(key_handle_2);
    CHECK_EQUAL(PSA_SUCCESS, status);
}

TEST(CryptoServiceOpTests, signAndVerifyHash)
{
    psa_status_t status;
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
    psa_key_handle_t key_handle;

    psa_set_key_id(&attributes, 13);
    psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
    psa_set_key_algorithm(&attributes, PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256));
    psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_CURVE_SECP_R1));
    psa_set_key_bits(&attributes, 256);

    /* Generate a key */
    status = m_crypto_client->generate_key(&attributes, &key_handle);
    CHECK_EQUAL(PSA_SUCCESS, status);

    psa_reset_key_attributes(&attributes);

    /* Sign a hash */
    uint8_t hash[20];
    uint8_t signature[PSA_SIGNATURE_MAX_SIZE];
    size_t signature_length;

    memset(hash, 0x71, sizeof(hash));

    status = m_crypto_client->sign_hash(key_handle,
        PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256), hash, sizeof(hash),
        signature, sizeof(signature), &signature_length);

    CHECK_EQUAL(PSA_SUCCESS, status);
    CHECK(signature_length > 0);

    /* Verify the signature */
    status = m_crypto_client->verify_hash(key_handle,
        PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256), hash, sizeof(hash),
        signature, signature_length);
    CHECK_EQUAL(PSA_SUCCESS, status);

    /* Change the hash and expect verify to fail */
    hash[0] = 0x72;
    status = m_crypto_client->verify_hash(key_handle,
        PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256), hash, sizeof(hash),
        signature, signature_length);
    CHECK_EQUAL(PSA_ERROR_INVALID_SIGNATURE, status);

    /* Remove the key */
    status = m_crypto_client->destroy_key(key_handle);
    CHECK_EQUAL(PSA_SUCCESS, status);
}

TEST(CryptoServiceOpTests, asymEncryptDecrypt)
{
    psa_status_t status;
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
    psa_key_handle_t key_handle;

    psa_set_key_id(&attributes, 14);
    psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
    psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
    psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
    psa_set_key_bits(&attributes, 256);

    /* Generate a key */
    status = m_crypto_client->generate_key(&attributes, &key_handle);
    CHECK_EQUAL(PSA_SUCCESS, status);

    psa_reset_key_attributes(&attributes);

    /* Encrypt a message */
    uint8_t message[] = {'q','u','i','c','k','b','r','o','w','n','f','o','x'};
    uint8_t ciphertext[256];
    size_t ciphertext_len = 0;

    status = m_crypto_client->asymmetric_encrypt(key_handle, PSA_ALG_RSA_PKCS1V15_CRYPT,
                            message, sizeof(message), NULL, 0,
                            ciphertext, sizeof(ciphertext), &ciphertext_len);
    CHECK_EQUAL(PSA_SUCCESS, status);

    /* Decrypt it */
    uint8_t plaintext[256];
    size_t plaintext_len = 0;

    status = m_crypto_client->asymmetric_decrypt(key_handle, PSA_ALG_RSA_PKCS1V15_CRYPT,
                            ciphertext, ciphertext_len, NULL, 0,
                            plaintext, sizeof(plaintext), &plaintext_len);
    CHECK_EQUAL(PSA_SUCCESS, status);

    /* Expect the encrypted/decrypted message to match theh original */
    CHECK_EQUAL(sizeof(message), plaintext_len);
    CHECK(memcmp(message, plaintext, plaintext_len) == 0);

    /* Remove the key */
    status = m_crypto_client->destroy_key(key_handle);
    CHECK_EQUAL(PSA_SUCCESS, status);
}

TEST(CryptoServiceOpTests, generateRandomNumbers)
{
    psa_status_t status;
    uint8_t num1_8bit[1];
    uint8_t num2_8bit[1];
    uint8_t num3_16bit[2];
    uint8_t num4_16bit[2];
    uint8_t num5_24bit[3];
    uint8_t num6_24bit[3];
    uint8_t num7_32bit[4];
    uint8_t num8_32bit[4];
    uint8_t num9_64bit[8];
    uint8_t num10_64bit[8];
    uint8_t num11_128bit[16];
    uint8_t num12_128bit[16];

    /* Clear all buffers */
    memset(num1_8bit, 0, sizeof(num1_8bit));
    memset(num2_8bit, 0, sizeof(num2_8bit));
    memset(num3_16bit, 0, sizeof(num3_16bit));
    memset(num4_16bit, 0, sizeof(num4_16bit));
    memset(num5_24bit, 0, sizeof(num5_24bit));
    memset(num6_24bit, 0, sizeof(num6_24bit));
    memset(num7_32bit, 0, sizeof(num7_32bit));
    memset(num8_32bit, 0, sizeof(num8_32bit));
    memset(num9_64bit, 0, sizeof(num9_64bit));
    memset(num10_64bit, 0, sizeof(num10_64bit));
    memset(num11_128bit, 0, sizeof(num11_128bit));
    memset(num12_128bit, 0, sizeof(num12_128bit));

    /* Generate some different size random numbers */
    status = m_crypto_client->generate_random(num1_8bit, sizeof(num1_8bit));
    CHECK_EQUAL(PSA_SUCCESS, status);
    status = m_crypto_client->generate_random(num2_8bit, sizeof(num2_8bit));
    CHECK_EQUAL(PSA_SUCCESS, status);
    status = m_crypto_client->generate_random(num3_16bit, sizeof(num3_16bit));
    CHECK_EQUAL(PSA_SUCCESS, status);
    status = m_crypto_client->generate_random(num4_16bit, sizeof(num4_16bit));
    CHECK_EQUAL(PSA_SUCCESS, status);
    status = m_crypto_client->generate_random(num5_24bit, sizeof(num5_24bit));
    CHECK_EQUAL(PSA_SUCCESS, status);
    status = m_crypto_client->generate_random(num6_24bit, sizeof(num6_24bit));
    CHECK_EQUAL(PSA_SUCCESS, status);
    status = m_crypto_client->generate_random(num7_32bit, sizeof(num7_32bit));
    CHECK_EQUAL(PSA_SUCCESS, status);
    status = m_crypto_client->generate_random(num8_32bit, sizeof(num8_32bit));
    CHECK_EQUAL(PSA_SUCCESS, status);
    status = m_crypto_client->generate_random(num9_64bit, sizeof(num9_64bit));
    CHECK_EQUAL(PSA_SUCCESS, status);
    status = m_crypto_client->generate_random(num10_64bit, sizeof(num10_64bit));
    CHECK_EQUAL(PSA_SUCCESS, status);
    status = m_crypto_client->generate_random(num11_128bit, sizeof(num11_128bit));
    CHECK_EQUAL(PSA_SUCCESS, status);
    status = m_crypto_client->generate_random(num12_128bit, sizeof(num12_128bit));
    CHECK_EQUAL(PSA_SUCCESS, status);

    /* Expect different numbers to be generated */
    CHECK(memcmp(num1_8bit, num2_8bit, sizeof(num1_8bit)) != 0);
    CHECK(memcmp(num3_16bit, num4_16bit, sizeof(num3_16bit)) != 0);
    CHECK(memcmp(num5_24bit, num6_24bit, sizeof(num5_24bit)) != 0);
    CHECK(memcmp(num7_32bit, num8_32bit, sizeof(num7_32bit)) != 0);
    CHECK(memcmp(num9_64bit, num10_64bit, sizeof(num9_64bit)) != 0);
    CHECK(memcmp(num11_128bit, num12_128bit, sizeof(num11_128bit)) != 0);
}