Skip to content

Commit d9485cb

Browse files
authored
Merge pull request #196 from JinkaiYang/branch-dev/ehsm
Add backup schema for machine-binding-Rootkey
2 parents 95e900b + e1637dc commit d9485cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+7386
-0
lines changed

cczoo/sgx_kms/.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
rkeyserver/App/enclave_u.*
2+
rkeyserver/Enclave/enclave_t.*
3+
rkeyserver/App/*.o
4+
rkeyserver/Enclave/*.o
5+
6+
utils/tkey_exchange/*.o
7+
utils/tkey_exchange/sgx_tkey_exchange_t.*
8+
utils/tkey_exchange/sgx_tkey_exchange_u.*
9+
utils/ukey_exchange/*.o
10+
11+
out/
12+
13+
.history/
14+
.vscode/
15+
16+
rkeyserver/App/auto_version.h

cczoo/sgx_kms/Makefile

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#
2+
# Copyright (c) 2022 Intel Corporation
3+
# SPDX-License-Identifier: Apache-2.0
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
include buildenv.mk
17+
18+
SUB_DIR := utils/tkey_exchange utils/ukey_exchange rkeyserver
19+
20+
.PHONY: all clean
21+
22+
all:
23+
for dir in $(SUB_DIR); do \
24+
$(MAKE) -C $$dir; \
25+
done
26+
27+
ifeq ($(Build_Mode), HW_DEBUG)
28+
@echo "The project has been built in hardware debug mode."
29+
else ifeq ($(Build_Mode), HW_RELEAESE)
30+
@echo "The project has been built in hardware release mode."
31+
else ifeq ($(Build_Mode), HW_PRERELEAESE)
32+
@echo "The project has been built in hardware pre-release mode."
33+
else ifeq ($(Build_Mode), SIM_DEBUG)
34+
@echo "The project has been built in simulation debug mode."
35+
else ifeq ($(Build_Mode), SIM_RELEAESE)
36+
@echo "The project has been built in simulation release mode."
37+
else ifeq ($(Build_Mode), SIM_PRERELEAESE)
38+
@echo "The project has been built in simulation pre-release mode."
39+
endif
40+
41+
clean:
42+
@rm -rf $(OUTDIR)
43+
for dir in $(SUB_DIR); do \
44+
$(MAKE) -C $$dir clean; \
45+
done

cczoo/sgx_kms/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## Overview of this backup schema of sgx_kms
2+
3+
In the design architecture of sgx_kms, the TCP/IP socket server is used to provide the root key, the remote secure channel provided by SGX is used to transmit the root key in the enclave, and the SGX Keys are used to encrypt the root key and store it in a local file or database, so that it can be safely stored outside the enclave environment. In this way, the root key is also bound with the unique SGX Keys of the processor, reducing the correlation between the root key and the maintenance personnel. Considering that the server may fail, we have designed a backup architecture. When we start the standby server, we will ask the primary server for the root key and keep it securely. When the primary server goes down, the backup server can play a role as the primary server.
4+
5+
The root key stored in the keystore needs to be decrypted by all root key providing service instances, while other platforms cannot decrypt the root key encrypted through the SGX sealing mechanism. Therefore, in the SGX security enhanced root key providing service design, the Root Key is deployed in each backup instance by using SGX remote authentication to establish a secure session. The scheme design is shown in the figure. This scheme is stored on the platform through SGX sealed encryption, and the root key is only used for decryption in Enclave, so that every instance has the same root key, and the attacker is prevented from directly obtaining the master key from memory.
6+
7+
<img src="./docs/arch-of-back-sgxkms.png" alt="arch-of-back-sgxkms" style="zoom:50%;" />
8+
9+
## Build Instructions
10+
11+
- If you need to synchronize the domain key between the host machine (the machine that has the domain key) and the backup machine (the machine that requests the domain key from the host machine), then you can compile first.
12+
13+
``` bash
14+
make
15+
```
16+
17+
- In this way, the directory `out/` will be generated, and in this directory, there will be the following files
18+
19+
```
20+
rkeyserver
21+
22+
lib
23+
```
24+
25+
- Enter the folder rkeyserver, there will be the following file rkeyserver
26+
27+
```
28+
libenclave-rkeyserver.signed.so
29+
30+
libenclave-rkeyserver.so
31+
```
32+
33+
- On the host side, execute
34+
35+
``` bash
36+
./rkeyserver
37+
```
38+
39+
- on the backup machine, execute
40+
41+
``` bash
42+
./rkeyserver -i 10.23.100.2 -p 8888
43+
```
44+
45+
`-i` is followed by the ip address of the host (`10.23.100.2` is used here as an example), `-p` is the port number of the host, the default is `8888`.
46+
47+
- If the following message is displayed
48+
49+
```
50+
INFO [App/ra_getkey.cpp(454) -> start_getkey]: Successfully received the DomainKey from deploy server.
51+
```
52+
53+
It means that the domain key is successfully obtained on the backup machine. By default, the domain key is stored in the directory `/etc/rkey.bin` in encrypted form.
54+
55+
It is worth mentioning that when the `/etc/rkey.bin` file already exists, when the backup machine continues to request the domain key from the host, the original `rkey.bin` file will be replaced by the new `rkey.bin` file and the following information is output on the screen
56+
57+
```
58+
file already exist, substitute by new file
59+
```
60+
61+
> ***Many source codes in this project come from [intel/ehsm](https://github.com/intel/ehsm/tree/t-multi-dkeyserver-reference)***

cczoo/sgx_kms/buildenv.mk

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#
2+
# Copyright (c) 2022 Intel Corporation
3+
# SPDX-License-Identifier: Apache-2.0
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
######## auto_version Settings ########
17+
DATE_STRING := `date "+20%y.%m.%d %k:%M"`
18+
KMS_GIT_SHA=$(shell git rev-parse --short=7 --verify HEAD)
19+
20+
# -------------------------------------------------------------------
21+
# Function : parent-dir
22+
# Arguments: 1: path
23+
# Returns : Parent dir or path of $1, with final separator removed.
24+
# -------------------------------------------------------------------
25+
parent-dir = $(patsubst %/,%,$(dir $(1:%/=%)))
26+
27+
# ------------------------------------------------------------------
28+
# Macro : my-dir
29+
# Returns : the directory of the current Makefile
30+
# Usage : $(my-dir)
31+
# ------------------------------------------------------------------
32+
my-dir = $(realpath $(call parent-dir,$(lastword $(MAKEFILE_LIST))))
33+
34+
ROOT_DIR := $(call my-dir)
35+
ifneq ($(words $(subst :, ,$(ROOT_DIR))), 1)
36+
$(error main directory cannot contain spaces nor colons)
37+
endif
38+
39+
######## Output Settings ########
40+
TOPDIR = $(ROOT_DIR)
41+
OUTDIR := out
42+
OUTLIB_DIR := $(OUTDIR)/lib
43+
44+
######## Compiler Settings ########
45+
CP = cp
46+
CC ?= gcc
47+
CXX ?= g++
48+
RM = rm -f
49+
50+
######## SGX SDK Settings ########
51+
52+
SGX_SDK ?= /opt/intel/sgxsdk
53+
SGX_MODE ?= HW
54+
SGX_ARCH ?= x64
55+
SGX_DEBUG ?= 1
56+
#SUPPLIED_KEY_DERIVATION ?= 1
57+
58+
include $(SGX_SDK)/buildenv.mk
59+
60+
ifeq ($(shell getconf LONG_BIT), 32)
61+
SGX_ARCH := x86
62+
else ifeq ($(findstring -m32, $(CXXFLAGS)), -m32)
63+
SGX_ARCH := x86
64+
endif
65+
66+
ifeq ($(SGX_ARCH), x86)
67+
SGX_COMMON_FLAGS := -m32
68+
SGX_LIBRARY_PATH := $(SGX_SDK)/lib
69+
SGX_ENCLAVE_SIGNER := $(SGX_SDK)/bin/x86/sgx_sign
70+
SGX_EDGER8R := $(SGX_SDK)/bin/x86/sgx_edger8r
71+
else
72+
SGX_COMMON_FLAGS := -m64
73+
SGX_LIBRARY_PATH := $(SGX_SDK)/lib64
74+
SGX_ENCLAVE_SIGNER := $(SGX_SDK)/bin/x64/sgx_sign
75+
SGX_EDGER8R := $(SGX_SDK)/bin/x64/sgx_edger8r
76+
endif
77+
78+
ifeq ($(SGX_DEBUG), 1)
79+
ifeq ($(SGX_PRERELEASE), 1)
80+
$(error Cannot set SGX_DEBUG and SGX_PRERELEASE at the same time!!)
81+
endif
82+
endif
83+
84+
ifeq ($(SUPPLIED_KEY_DERIVATION), 1)
85+
SGX_COMMON_FLAGS += -DSUPPLIED_KEY_DERIVATION
86+
endif
87+
88+
ifeq ($(SGX_DEBUG), 1)
89+
SGX_COMMON_FLAGS += -O0 -ggdb3
90+
else
91+
SGX_COMMON_FLAGS += -O2
92+
endif
93+
94+
SGX_COMMON_FLAGS += -Wall -Wextra -Winit-self -Wpointer-arith -Wreturn-type \
95+
-Waddress -Wsequence-point -Wformat-security \
96+
-Wmissing-include-dirs -Wfloat-equal -Wundef -Wshadow \
97+
-Wcast-align -Wredundant-decls
98+
99+
#SGX_COMMON_CFLAGS += $(SGX_COMMON_FLAGS) -Wstrict-prototypes -Wunsuffixed-float-constants -Wcast-qual
100+
101+
SGX_COMMON_CXXFLAGS := $(SGX_COMMON_FLAGS) -Wnon-virtual-dtor -std=c++11
102+
103+
######## BUILD Settings ########
104+
ifeq ($(SGX_MODE), HW)
105+
ifeq ($(SGX_DEBUG), 1)
106+
Build_Mode = HW_DEBUG
107+
else ifeq ($(SGX_PRERELEASE), 1)
108+
Build_Mode = HW_PRERELEASE
109+
else
110+
Build_Mode = HW_RELEASE
111+
endif
112+
else
113+
ifeq ($(SGX_DEBUG), 1)
114+
Build_Mode = SIM_DEBUG
115+
else ifeq ($(SGX_PRERELEASE), 1)
116+
Build_Mode = SIM_PRERELEASE
117+
else
118+
Build_Mode = SIM_RELEASE
119+
endif
120+
endif
121+
122+
ifneq ($(SGX_MODE), HW)
123+
Urts_Library_Name := sgx_urts_sim
124+
else
125+
Urts_Library_Name := sgx_urts
126+
endif
127+
128+
ifneq ($(SGX_MODE), HW)
129+
Trts_Library_Name := sgx_trts_sim
130+
Service_Library_Name := sgx_tservice_sim
131+
else
132+
Trts_Library_Name := sgx_trts
133+
Service_Library_Name := sgx_tservice
134+
endif
135+
97.3 KB
Loading

cczoo/sgx_kms/include/datatypes.h

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
*
3+
* Copyright (c) 2022 Intel Corporation
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+
19+
#include "sgx_report.h"
20+
#include "sgx_eid.h"
21+
#include "sgx_ecp_types.h"
22+
#include "sgx_dh.h"
23+
#include "sgx_tseal.h"
24+
25+
#ifndef DATATYPES_H_
26+
#define DATATYPES_H_
27+
28+
#define DH_KEY_SIZE 20
29+
#define NONCE_SIZE 16
30+
#define MAC_SIZE 16
31+
#define MAC_KEY_SIZE 16
32+
#define PADDING_SIZE 16
33+
34+
#define EH_API_KEY_SIZE 32
35+
#define UUID_STR_LEN 37
36+
37+
#define TAG_SIZE 16
38+
#define IV_SIZE 12
39+
40+
#define DERIVE_MAC_KEY 0x0
41+
#define DERIVE_SESSION_KEY 0x1
42+
#define DERIVE_VK1_KEY 0x3
43+
#define DERIVE_VK2_KEY 0x4
44+
45+
#define CLOSED 0x0
46+
#define IN_PROGRESS 0x1
47+
#define ACTIVE 0x2
48+
49+
#define SGX_DOMAIN_KEY_SIZE 16
50+
51+
#define MESSAGE_EXCHANGE 0x0
52+
53+
#define MESSAGE_EXCHANGE_CMD_DK 0x1
54+
55+
#define ENCLAVE_TO_ENCLAVE_CALL 0x1
56+
57+
#define INVALID_ARGUMENT -2 ///< Invalid function argument
58+
#define LOGIC_ERROR -3 ///< Functional logic error
59+
#define FILE_NOT_FOUND -4 ///< File not found
60+
61+
#define VMC_ATTRIBUTE_MASK 0xFFFFFFFFFFFFFFCB
62+
63+
#define _T(x) x
64+
65+
#define UNUSED(val) (void)(val)
66+
67+
#define TCHAR char
68+
69+
#define _TCHAR char
70+
71+
#define scanf_s scanf
72+
73+
#define _tmain main
74+
75+
#ifndef INT_MAX
76+
#define INT_MAX 0x7fffffff
77+
#endif
78+
79+
#ifndef SAFE_FREE
80+
#define SAFE_FREE(ptr) {if (NULL != (ptr)) {free(ptr); (ptr) = NULL;}}
81+
#endif
82+
83+
#ifndef _ERRNO_T_DEFINED
84+
#define _ERRNO_T_DEFINED
85+
typedef int errno_t;
86+
#endif
87+
88+
89+
typedef uint8_t dh_nonce[NONCE_SIZE];
90+
typedef uint8_t cmac_128[MAC_SIZE];
91+
92+
#pragma pack(push, 1)
93+
94+
//Format of the AES-GCM message being exchanged between the source and the destination enclaves
95+
typedef struct _secure_message_t
96+
{
97+
uint32_t session_id; //Session ID identifyting the session to which the message belongs
98+
sgx_aes_gcm_data_t message_aes_gcm_data;
99+
} secure_message_t;
100+
101+
//Format of the input function parameter structure
102+
typedef struct _ms_in_msg_exchange_t {
103+
uint32_t msg_type; //Type of Call E2E or general message exchange
104+
uint32_t target_fn_id; //Function Id to be called in Destination. Is valid only when msg_type=ENCLAVE_TO_ENCLAVE_CALL
105+
uint32_t inparam_buff_len; //Length of the serialized input parameters
106+
uint8_t inparam_buff[1]; //Serialized input parameters
107+
} ms_in_msg_exchange_t;
108+
109+
//Format of the return value and output function parameter structure
110+
typedef struct _ms_out_msg_exchange_t {
111+
uint32_t retval_len; //Length of the return value
112+
uint32_t ret_outparam_buff_len; //Length of the serialized return value and output parameters
113+
uint8_t ret_outparam_buff[1]; //Serialized return value and output parameters
114+
} ms_out_msg_exchange_t;
115+
116+
//Session Tracker to generate session ids
117+
typedef struct _session_id_tracker_t
118+
{
119+
uint32_t session_id;
120+
} session_id_tracker_t;
121+
122+
#pragma pack(pop)
123+
124+
125+
#endif

0 commit comments

Comments
 (0)