aboutsummaryrefslogtreecommitdiff
path: root/components/service/crypto/test/service/crypto_service_limit_tests.cpp
blob: 392391a1001b808490f1da2c4d50da2375a4caf8 (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
/*
 * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <string>
#include <vector>
#include <cstring>
#include <cstdint>
#include <service/crypto/client/cpp/crypto_client.h>
#include <protocols/rpc/common/packed-c/encoding.h>
#include <service_locator.h>
#include <CppUTest/TestHarness.h>

/*
 * Service-level tests that focus on verifying that expected limits are met.
 * e.g. number of keys, key sizes etc.
 */
TEST_GROUP(CryptoServiceLimitTests)
{
    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);
        CHECK(m_crypto_service_context);

        m_rpc_session_handle = service_context_open(m_crypto_service_context, TS_RPC_ENCODING_PROTOBUF, &caller);
        CHECK(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;
    }

    psa_status_t generateVolatileEccKeyPair(std::vector<psa_key_handle_t> &key_handles)
    {
        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, 521);

        psa_key_handle_t key_handle;
        status = m_crypto_client->generate_key(&attributes, &key_handle);

        psa_reset_key_attributes(&attributes);

        if (status == PSA_SUCCESS) key_handles.push_back(key_handle);

        return status;
    }

    psa_status_t generateVolatileRsaKeyPair(std::vector<psa_key_handle_t> &key_handles)
    {
        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_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, 512);

        psa_key_handle_t key_handle;
        status = m_crypto_client->generate_key(&attributes, &key_handle);

        psa_reset_key_attributes(&attributes);

        if (status == PSA_SUCCESS) key_handles.push_back(key_handle);

        return status;
    }

    psa_status_t destroyKeys(const std::vector<psa_key_handle_t> &key_handles)
    {
        psa_status_t status = PSA_SUCCESS;
        size_t key_index = 0;

        while ((key_index < key_handles.size()) && (status == PSA_SUCCESS)) {

            status = m_crypto_client->destroy_key(key_handles[key_index]);
            ++key_index;
        }

        return status;
    }

    /*
     * Maximum number of key slots in mbedcrypto
     * is 32.  We would expect it to be possible to
     * generate keys up to that limit.
     */
    const size_t MAX_KEY_SLOTS = 32;

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

TEST(CryptoServiceLimitTests, volatileEccKeyPairLimit)
{
    size_t expected_limit = MAX_KEY_SLOTS;
    size_t actual_limit = 0;
    std::vector<psa_key_handle_t> key_handles;
    psa_status_t generate_status = PSA_SUCCESS;
    psa_status_t destroy_status;

    while (actual_limit < expected_limit) {

        generate_status = generateVolatileEccKeyPair(key_handles);

        if (generate_status == PSA_SUCCESS)
            ++actual_limit;
        else
            break;
    }

    destroy_status = destroyKeys(key_handles);

    CHECK_EQUAL(PSA_SUCCESS, generate_status);
    CHECK_EQUAL(PSA_SUCCESS, destroy_status);
    CHECK_EQUAL(expected_limit, actual_limit);
}

TEST(CryptoServiceLimitTests, volatileRsaKeyPairLimit)
{
    size_t expected_limit = MAX_KEY_SLOTS;
    size_t actual_limit = 0;
    std::vector<psa_key_handle_t> key_handles;
    psa_status_t generate_status = PSA_SUCCESS;
    psa_status_t destroy_status;

    while (actual_limit < expected_limit) {

        generate_status = generateVolatileRsaKeyPair(key_handles);

        if (generate_status == PSA_SUCCESS)
            ++actual_limit;
        else
            break;
    }

    destroy_status = destroyKeys(key_handles);

    CHECK_EQUAL(PSA_SUCCESS, generate_status);
    CHECK_EQUAL(PSA_SUCCESS, destroy_status);
    CHECK_EQUAL(expected_limit, actual_limit);
}