Skip to content

Commit 0ecd2c5

Browse files
author
itayzafrir
committed
crypto service: Crypto access control
Implement crypto keys access control in crypto service: - Only the key owner (the partition which created the key) is allowed to manage (import/export/open/close/destroy/etc.) the key. - Only the key owner (the partition which created the key) is allowed to use the key handle for crypto operations which require a key handle.
1 parent e03b3b6 commit 0ecd2c5

File tree

3 files changed

+263
-6
lines changed

3 files changed

+263
-6
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2019, Arm Limited and affiliates
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include <string.h>
19+
20+
#include "psa_crypto_access_control.h"
21+
#include "psa_crypto_slot_management.h"
22+
#include "spm_panic.h"
23+
24+
typedef struct psa_crypto_access_control_s {
25+
psa_key_handle_t key_handle;
26+
int32_t partition_id;
27+
} psa_crypto_access_control_t;
28+
29+
static psa_crypto_access_control_t crypto_access_control_arr[PSA_KEY_SLOT_COUNT];
30+
31+
#define PSA_CRYPTO_ACCESS_CONTROL_RESET() (memset(crypto_access_control_arr, 0, sizeof(crypto_access_control_arr)))
32+
33+
void psa_crypto_access_control_init(void)
34+
{
35+
PSA_CRYPTO_ACCESS_CONTROL_RESET();
36+
}
37+
38+
void psa_crypto_access_control_destroy(void)
39+
{
40+
PSA_CRYPTO_ACCESS_CONTROL_RESET();
41+
}
42+
43+
void psa_crypto_access_control_register_handle(psa_key_handle_t key_handle, int32_t partition_id)
44+
{
45+
for (size_t i = 0; i < PSA_KEY_SLOT_COUNT; i++) {
46+
if (crypto_access_control_arr[i].key_handle == 0 &&
47+
crypto_access_control_arr[i].partition_id == 0) {
48+
crypto_access_control_arr[i].key_handle = key_handle;
49+
crypto_access_control_arr[i].partition_id = partition_id;
50+
return;
51+
}
52+
}
53+
54+
SPM_PANIC("psa_crypto_access_control_register_handle failed");
55+
}
56+
57+
void psa_crypto_access_control_unregister_handle(psa_key_handle_t key_handle)
58+
{
59+
for (size_t i = 0; i < PSA_KEY_SLOT_COUNT; i++) {
60+
if (crypto_access_control_arr[i].key_handle == key_handle) {
61+
crypto_access_control_arr[i].key_handle = 0;
62+
crypto_access_control_arr[i].partition_id = 0;
63+
return;
64+
}
65+
}
66+
67+
SPM_PANIC("psa_crypto_access_control_unregister_handle failed");
68+
}
69+
70+
uint8_t psa_crypto_access_control_is_handle_permitted(psa_key_handle_t key_handle, int32_t partition_id)
71+
{
72+
for (size_t i = 0; i < PSA_KEY_SLOT_COUNT; i++) {
73+
if (crypto_access_control_arr[i].key_handle == key_handle &&
74+
crypto_access_control_arr[i].partition_id == partition_id) {
75+
return 1;
76+
}
77+
}
78+
79+
return 0;
80+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2019, Arm Limited and affiliates
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef PSA_CRYPTO_ACCESS_CONTROL_H
19+
#define PSA_CRYPTO_ACCESS_CONTROL_H
20+
21+
#include <stdint.h>
22+
23+
#include "psa_crypto_core.h"
24+
#include "crypto_platform.h"
25+
26+
void psa_crypto_access_control_init(void);
27+
28+
void psa_crypto_access_control_destroy(void);
29+
30+
void psa_crypto_access_control_register_handle(psa_key_handle_t key_handle, int32_t partition_id);
31+
32+
void psa_crypto_access_control_unregister_handle(psa_key_handle_t key_handle);
33+
34+
uint8_t psa_crypto_access_control_is_handle_permitted(psa_key_handle_t key_handle, int32_t partition_id);
35+
36+
#endif /* PSA_CRYPTO_ACCESS_CONTROL_H */

0 commit comments

Comments
 (0)