Skip to content

Commit

Permalink
Update opensbi version to opensbi-1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Fly0307 committed Oct 7, 2023
1 parent ed6b034 commit 28cf6b5
Show file tree
Hide file tree
Showing 69 changed files with 19,071 additions and 20 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,16 @@ copy-files/*
*.hex
*.swp
tags
backup/
.vscode/

-append
-bios
-device
-drive
-kernel
-netdev
-object
-smp
host.txt
Image
1 change: 1 addition & 0 deletions opensbi-1.2/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#Build & install directories
build/
build-oe/
install/

# Development friendly files
Expand Down
19 changes: 19 additions & 0 deletions opensbi-1.2/include/sbi/riscv_encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,22 @@
#define PMP_ADDR_MASK _UL(0xFFFFFFFF)
#endif

/* page table entry (PTE) fields */
#define PTE_V _UL(0x001) /* Valid */
#define PTE_R _UL(0x002) /* Read */
#define PTE_W _UL(0x004) /* Write */
#define PTE_X _UL(0x008) /* Execute */
#define PTE_U _UL(0x010) /* User */
#define PTE_G _UL(0x020) /* Global */
#define PTE_A _UL(0x040) /* Accessed */
#define PTE_D _UL(0x080) /* Dirty */
#define PTE_SOFT _UL(0x300) /* Reserved for Software */

#define PTE_PPN_SHIFT 10

#define PTE_TABLE(PTE) \
(((PTE) & (PTE_V | PTE_R | PTE_W | PTE_X)) == PTE_V)

#if __riscv_xlen == 64
#define MSTATUS_SD MSTATUS64_SD
#define SSTATUS_SD SSTATUS64_SD
Expand All @@ -177,6 +193,9 @@
#define HGATP_MODE_SHIFT HGATP32_MODE_SHIFT
#endif

#define RISCV_PGSHIFT 12
#define RISCV_PGSIZE (1 << RISCV_PGSHIFT)

#define TOPI_IID_SHIFT 16
#define TOPI_IID_MASK 0xfff
#define TOPI_IPRIO_MASK 0xff
Expand Down
12 changes: 12 additions & 0 deletions opensbi-1.2/include/sbi/sbi_ecall.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ struct sbi_ecall_extension {
struct sbi_trap_info *out_trap);
};

extern struct sbi_ecall_extension ecall_base;
extern struct sbi_ecall_extension ecall_legacy;
extern struct sbi_ecall_extension ecall_time;
extern struct sbi_ecall_extension ecall_rfence;
extern struct sbi_ecall_extension ecall_ipi;
extern struct sbi_ecall_extension ecall_vendor;
extern struct sbi_ecall_extension ecall_hsm;
extern struct sbi_ecall_extension ecall_srst;
extern struct sbi_ecall_extension ecall_pmu;
extern struct sbi_ecall_extension ecall_penglai_host;
extern struct sbi_ecall_extension ecall_penglai_enclave;

u16 sbi_ecall_version_major(void);

u16 sbi_ecall_version_minor(void);
Expand Down
4 changes: 4 additions & 0 deletions opensbi-1.2/include/sbi/sbi_ecall_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
#define SBI_EXT_SRST 0x53525354
#define SBI_EXT_PMU 0x504D55

//Penglai
#define SBI_EXT_PENGLAI_HOST 0x100100
#define SBI_EXT_PENGLAI_ENCLAVE 0x100101

/* SBI function IDs for BASE extension*/
#define SBI_EXT_BASE_GET_SPEC_VERSION 0x0
#define SBI_EXT_BASE_GET_IMP_ID 0x1
Expand Down
2 changes: 1 addition & 1 deletion opensbi-1.2/include/sbi/sbi_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ struct sbi_platform_operations {
};

/** Platform default per-HART stack size for exception/interrupt handling */
#define SBI_PLATFORM_DEFAULT_HART_STACK_SIZE 8192
#define SBI_PLATFORM_DEFAULT_HART_STACK_SIZE 8192*8

/** Representation of a platform */
struct sbi_platform {
Expand Down
10 changes: 10 additions & 0 deletions opensbi-1.2/include/sbi/sbi_pmp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef __SBI_PMP_H__
#define __SBI_PMP_H__

#include <sm/pmp.h>
#include <sbi/sbi_types.h>
#include <sbi/sbi_hartmask.h>
struct sbi_scratch;
int sbi_pmp_init(struct sbi_scratch *scratch, bool cold_boot);
int sbi_send_pmp(ulong hmask, ulong hbase, struct pmp_data_t* pmp_data);
#endif
16 changes: 16 additions & 0 deletions opensbi-1.2/include/sm/attest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef _ATTEST_H
#define _ATTEST_H

#include "sm/enclave.h"

void attest_init();

void hash_enclave(struct enclave_t* enclave, void* hash, uintptr_t nonce);

void update_enclave_hash(char *output, void* hash, uintptr_t nonce_arg);

void sign_enclave(void* signature, unsigned char *message, int len);

int verify_enclave(void* signature, unsigned char *message, int len);

#endif /* _ATTEST_H */
112 changes: 112 additions & 0 deletions opensbi-1.2/include/sm/enclave.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#ifndef _ENCLAVE_H
#define _ENCLAVE_H

#include <sbi/riscv_asm.h>
#include <sm/vm.h>
#include <sbi/riscv_encoding.h>
#include <sm/enclave_args.h>
#include <sbi/riscv_atomic.h>
#include <sm/thread.h>
#include <stdint.h>
#include <stddef.h>

#define ENCLAVES_PER_METADATA_REGION 128
#define ENCLAVE_METADATA_REGION_SIZE ((sizeof(struct enclave_t)) * ENCLAVES_PER_METADATA_REGION)

#define ENCLAVE_MODE 1

// define the time slice for an enclave
#define ENCLAVE_TIME_CREDITS 100000

struct link_mem_t
{
unsigned long mem_size;
unsigned long slab_size;
unsigned long slab_num;
char* addr;
struct link_mem_t* next_link_mem;
};

typedef enum
{
DESTROYED = -1,
INVALID = 0,
FRESH = 1,
RUNNABLE,
RUNNING,
STOPPED,
} enclave_state_t;

/*
* enclave memory [paddr, paddr + size]
* free_mem @ unused memory address in enclave mem
*/
struct enclave_t
{
unsigned int eid;
enclave_state_t state;

//memory region of enclave
unsigned long paddr;
unsigned long size;

//address of left available memory in memory region
unsigned long free_mem;

//TODO: dynamically allocated memory
unsigned long* enclave_mem_metadata_page;

//root page table of enclave
unsigned long* root_page_table;
//root page table register for host
unsigned long host_ptbr;
//entry point of enclave
unsigned long entry_point;

//shared mem with kernel
unsigned long kbuffer;
unsigned long kbuffer_size;

unsigned long* ocall_func_id;
unsigned long* ocall_arg0;
unsigned long* ocall_arg1;
unsigned long* ocall_syscall_num;

//shared memory with host
unsigned long untrusted_ptr;
unsigned long untrusted_size;
// enclave measurement
unsigned char hash[HASH_SIZE];
// hash of enclave developer's public key
unsigned char signer[HASH_SIZE];

//enclave thread context
//TODO: support multiple threads
struct thread_state_t thread_context;
};

struct cpu_state_t
{
int in_enclave;
int eid;
};

uintptr_t create_enclave(struct enclave_sbi_param_t create_args);
uintptr_t run_enclave(uintptr_t* regs, unsigned int eid);
uintptr_t stop_enclave(uintptr_t* regs, unsigned int eid);
uintptr_t destroy_enclave(uintptr_t* regs, unsigned int eid);
uintptr_t resume_enclave(uintptr_t* regs, unsigned int eid);
uintptr_t resume_from_stop(uintptr_t* regs, unsigned int eid);
uintptr_t attest_enclave(uintptr_t eid, uintptr_t report_ptr, uintptr_t nonce);
uintptr_t exit_enclave(uintptr_t* regs, unsigned long retval);
uintptr_t do_timer_irq(uintptr_t* regs, uintptr_t mcause, uintptr_t mepc);

uintptr_t resume_from_ocall(uintptr_t* regs, unsigned int eid);
uintptr_t enclave_sys_write(uintptr_t *regs);
uintptr_t enclave_user_defined_ocall(uintptr_t *regs, uintptr_t ocall_buf_size);
uintptr_t enclave_derive_seal_key(uintptr_t* regs, uintptr_t salt_va,
uintptr_t salt_len, uintptr_t key_buf_va, uintptr_t key_buf_len);

int check_in_enclave_world();

#endif /* _ENCLAVE_H */
85 changes: 85 additions & 0 deletions opensbi-1.2/include/sm/enclave_args.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#ifndef _ENCLAVE_ARGS_H
#define _ENCLAVE_ARGS_H
#include "thread.h"
#define HASH_SIZE 32
#define PRIVATE_KEY_SIZE 32
#define PUBLIC_KEY_SIZE 64
#define SIGNATURE_SIZE 64

#define MANU_PUB_KEY (void*)((unsigned long)0x801ff000)
#define DEV_PUB_KEY (MANU_PUB_KEY + PUBLIC_KEY_SIZE)
#define DEV_PRI_KEY (DEV_PUB_KEY + PUBLIC_KEY_SIZE)
#define SM_PUB_KEY (DEV_PRI_KEY + PRIVATE_KEY_SIZE)
#define SM_PRI_KEY (SM_PUB_KEY + PUBLIC_KEY_SIZE)
#define SM_HASH (SM_PRI_KEY + PRIVATE_KEY_SIZE)
#define SM_SIGNATURE (SM_HASH + HASH_SIZE)

struct mm_alloc_arg_t
{
unsigned long req_size;
uintptr_t resp_addr;
unsigned long resp_size;
};

// Attestation-related report
struct sm_report_t
{
unsigned char hash[HASH_SIZE];
unsigned char signature[SIGNATURE_SIZE];
unsigned char sm_pub_key[PUBLIC_KEY_SIZE];
};

struct enclave_report_t
{
unsigned char hash[HASH_SIZE];
unsigned char signature[SIGNATURE_SIZE];
uintptr_t nonce;
};

struct report_t
{
struct sm_report_t sm;
struct enclave_report_t enclave;
unsigned char dev_pub_key[PUBLIC_KEY_SIZE];
};

struct prikey_t
{
unsigned char dA[PRIVATE_KEY_SIZE];
};

struct pubkey_t
{
unsigned char xA[PUBLIC_KEY_SIZE/2];
unsigned char yA[PUBLIC_KEY_SIZE/2];
};

struct signature_t
{
unsigned char r[PUBLIC_KEY_SIZE/2];
unsigned char s[PUBLIC_KEY_SIZE/2];
};

/*
* enclave memory [paddr, paddr + size]
* free_mem @ unused memory address in enclave mem
*/
struct enclave_sbi_param_t
{
unsigned int *eid_ptr;
unsigned long paddr;
unsigned long size;
unsigned long entry_point;
unsigned long untrusted_ptr;
unsigned long untrusted_size;
unsigned long free_mem;
//enclave shared mem with kernel
unsigned long kbuffer;
unsigned long kbuffer_size;
unsigned long *ecall_arg0;
unsigned long *ecall_arg1;
unsigned long *ecall_arg2;
unsigned long *ecall_arg3;
};

#endif /* _ENCLAVE_ARGS_H */
62 changes: 62 additions & 0 deletions opensbi-1.2/include/sm/gm/SM2_sv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/************************************************************************
Copyright (c) IPADS@SJTU 2021. Modification to support Penglai (RISC-V TEE)
This file contains GM/T SM2 standard implementation, provided by the Commercial
Cryptography Testing Center, see <http://www.scctc.org.cn> for more infomation.
File name: SM2_sv.c
Version: SM2_sv_V1.0
Date: Sep 27,2016
Description: implementation of SM2 signature algorithm and verification algorithm
Function List:
1.SM2_Init //initiate SM2 curve
2.Test_Point //test if the given point is on SM2 curve
3.Test_PubKey //test if the given public key is valid
4.Test_Zero //test if the big x equals zero
5.Test_n //test if the big x equals n
6.Test_Range //test if the big x belong to the range[1,n-1]
7.SM2_KeyGeneration //generate SM2 key pair
8.SM2_Sign //SM2 signature algorithm
9.SM2_Verify //SM2 verification
10.SM2_SelfCheck() //SM2 self-check
11.SM3_256() //this function can be found in SM3.c and SM3.h
Additional Functions Added By PENGLAI Enclave:
1.MIRACL_Init //init miracl system
2.SM2_make_prikey //generate a SM2 private key
3.SM2_make_pubkey //generate a SM2 public Key out of a private Key
4.SM2_gen_random //generate a random number K lies in [1,n-1]
5.SM2_compute_ZA //compute ZA out of a given pubkey
**************************************************************************/

#pragma once

#include "sm/gm/miracl/miracl.h"

#define SM2_WORDSIZE 8
#define SM2_NUMBITS 256
#define SM2_NUMWORD (SM2_NUMBITS / SM2_WORDSIZE)

#define ERR_ECURVE_INIT 0x00000001
#define ERR_INFINITY_POINT 0x00000002
#define ERR_NOT_VALID_POINT 0x00000003
#define ERR_ORDER 0x00000004
#define ERR_NOT_VALID_ELEMENT 0x00000005
#define ERR_GENERATE_R 0x00000006
#define ERR_GENERATE_S 0x00000007
#define ERR_OUTRANGE_R 0x00000008
#define ERR_OUTRANGE_S 0x00000009
#define ERR_GENERATE_T 0x0000000A
#define ERR_PUBKEY_INIT 0x0000000B
#define ERR_DATA_MEMCMP 0x0000000C

int SM2_Init();
int Test_Point(epoint *point);
int Test_PubKey(epoint *pubKey);
int Test_Zero(big x);
int Test_n(big x);
int Test_Range(big x);
int SM2_KeyGeneration(unsigned char PriKey[], unsigned char Px[], unsigned char Py[]);
int SM2_Sign(unsigned char *message, int len, unsigned char d[], unsigned char R[], unsigned char S[]);
int SM2_Verify(unsigned char *message, int len, unsigned char Px[], unsigned char Py[], unsigned char R[], unsigned char S[]);
int SM2_SelfCheck();
Loading

0 comments on commit 28cf6b5

Please sign in to comment.