forked from keystone-enclave/sm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsm.c
192 lines (163 loc) · 5.04 KB
/
sm.c
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
//******************************************************************************
// Copyright (c) 2018, The Regents of the University of California (Regents).
// All Rights Reserved. See LICENSE for license details.
//------------------------------------------------------------------------------
#include "ipi.h"
#include "sm.h"
#include "pmp.h"
#include "crypto.h"
#include "enclave.h"
#include "platform-hook.h"
#include "sm-sbi-opensbi.h"
#include <sbi/sbi_string.h>
#include <sbi/riscv_locks.h>
#include <sbi/riscv_barrier.h>
#include <sbi/sbi_console.h>
#include <sbi/sbi_hart.h>
#include <sifive/sifive_pmc.h>
#include <sifive/sifive_smc.h>
#include <sifive/sifive_cmc.h>
#include <sifive/sifive_tmc.h>
static int sm_init_done = 0;
static int sm_region_id = 0, os_region_id = 0;
/* from Sanctum BootROM */
extern byte sanctum_sm_hash[MDSIZE];
extern byte sanctum_sm_signature[SIGNATURE_SIZE];
extern byte sanctum_sm_secret_key[PRIVATE_KEY_SIZE];
extern byte sanctum_sm_public_key[PUBLIC_KEY_SIZE];
extern byte sanctum_dev_public_key[PUBLIC_KEY_SIZE];
byte sm_hash[MDSIZE] = { 0, };
byte sm_signature[SIGNATURE_SIZE] = { 0, };
byte sm_public_key[PUBLIC_KEY_SIZE] = { 0, };
byte sm_private_key[PRIVATE_KEY_SIZE] = { 0, };
byte dev_public_key[PUBLIC_KEY_SIZE] = { 0, };
int osm_pmp_set(uint8_t perm)
{
/* in case of OSM, PMP cfg is exactly the opposite.*/
return pmp_set_keystone(os_region_id, perm);
}
int smm_init()
{
int region = -1;
int ret = pmp_region_init_atomic(SMM_BASE, SMM_SIZE, PMP_PRI_TOP, ®ion, 0);
if(ret)
return -1;
return region;
}
int osm_init()
{
int region = -1;
int ret = pmp_region_init_atomic(0, -1UL, PMP_PRI_BOTTOM, ®ion, 1);
if(ret)
return -1;
return region;
}
void sm_sign(void* signature, const void* data, size_t len)
{
sign(signature, data, len, sm_public_key, sm_private_key);
}
int sm_derive_sealing_key(unsigned char *key, const unsigned char *key_ident,
size_t key_ident_size,
const unsigned char *enclave_hash)
{
unsigned char info[MDSIZE + key_ident_size];
sbi_memcpy(info, enclave_hash, MDSIZE);
sbi_memcpy(info + MDSIZE, key_ident, key_ident_size);
/*
* The key is derived without a salt because we have no entropy source
* available to generate the salt.
*/
return kdf(NULL, 0,
(const unsigned char *)sm_private_key, PRIVATE_KEY_SIZE,
info, MDSIZE + key_ident_size, key, SEALING_KEY_SIZE);
}
void sm_copy_key()
{
sbi_memcpy(sm_hash, sanctum_sm_hash, MDSIZE);
sbi_memcpy(sm_signature, sanctum_sm_signature, SIGNATURE_SIZE);
sbi_memcpy(sm_public_key, sanctum_sm_public_key, PUBLIC_KEY_SIZE);
sbi_memcpy(sm_private_key, sanctum_sm_secret_key, PRIVATE_KEY_SIZE);
sbi_memcpy(dev_public_key, sanctum_dev_public_key, PUBLIC_KEY_SIZE);
}
void sm_print_hash()
{
for (int i=0; i<MDSIZE; i++)
{
sbi_printf("%02x", (char) sm_hash[i]);
}
sbi_printf("\n");
}
/*
void sm_print_cert()
{
int i;
printm("Booting from Security Monitor\n");
printm("Size: %d\n", sanctum_sm_size[0]);
printm("============ PUBKEY =============\n");
for(i=0; i<8; i+=1)
{
printm("%x",*((int*)sanctum_dev_public_key+i));
if(i%4==3) printm("\n");
}
printm("=================================\n");
printm("=========== SIGNATURE ===========\n");
for(i=0; i<16; i+=1)
{
printm("%x",*((int*)sanctum_sm_signature+i));
if(i%4==3) printm("\n");
}
printm("=================================\n");
}
*/
void sm_init(bool cold_boot)
{
// initialize SMM
if (cold_boot) {
/* only the cold-booting hart will execute these */
sbi_printf("[SM] Initializing ... hart [%lx]\n", csr_read(mhartid));
pmp_reg_bitmap_init();
sbi_ecall_register_extension(&ecall_keystone_enclave);
sm_region_id = smm_init();
if(sm_region_id < 0) {
sbi_printf("[SM] intolerable error - failed to initialize SM memory");
sbi_hart_hang();
}
os_region_id = osm_init();
if(os_region_id < 0) {
sbi_printf("[SM] intolerable error - failed to initialize OS memory");
sbi_hart_hang();
}
if (platform_init_global_once() != SBI_ERR_SM_ENCLAVE_SUCCESS) {
sbi_printf("[SM] platform global init fatal error");
sbi_hart_hang();
}
// Copy the keypair from the root of trust
sm_copy_key();
// Init the enclave metadata
enclave_init_metadata();
sm_init_done = 1;
mb();
}
/* wait until cold-boot hart finishes */
while (!sm_init_done)
{
mb();
}
/* below are executed by all harts */
pmp_init();
pmp_set_keystone_dump(sm_region_id, PMP_NO_PERM, cold_boot);
pmp_set_keystone_dump(os_region_id, PMP_ALL_PERM, cold_boot);
if (platform_init_global() != SBI_ERR_SM_ENCLAVE_SUCCESS) {
sbi_printf("[SM] platform global init fatal error");
sbi_hart_hang();
}
if (!(sifive_tmc_check_pg_wake() || sifive_cmc_check_pg_wake() ||
sifive_smc_check_pg_wake() || sifive_pmc_check_pg_wake())) {
/* Fire platform specific global init */
sbi_printf("[SM] Keystone security monitor has been initialized!\n");
sm_print_hash();
}
return;
// for debug
// sm_print_cert();
}