SEcube
SEcube Open Source Library - Device
se3c0.h
Go to the documentation of this file.
1 
7 #pragma once
8 #include <stdint.h>
9 #include <stdbool.h>
10 #include <stddef.h>
11 #include "sha256.h"
12 #include "aes256.h"
13 
14 #include "se3c0def.h"
15 #include "se3_common.h"
16 
17 #if defined(_MSC_VER)
18 #define SE3_ALIGN_16 __declspec(align(0x10))
19 #elif defined(__GNUC__)
20 #define SE3_ALIGN_16 __attribute__((aligned(0x10)))
21 #else
22 #define SE3_ALIGN_16
23 #endif
24 
25 void se3c0_init();
26 uint64_t se3c0_time_get();
27 void se3c0_time_set(uint64_t t);
28 void se3c0_time_inc();
29 
30 const uint8_t se3_hello[SE3_HELLO_SIZE];
31 
32 #define SE3_BMAP_MAKE(n) ((uint32_t)(0xFFFFFFFF >> (32 - (n))))
33 
38 typedef struct SE3_COMM_STATUS_ {
39  // magic
40  bool magic_ready;
41  uint32_t magic_bmap;
42 
43  // block map
44  uint32_t blocks[SE3_COMM_N];
45  uint32_t block_guess;
46  bool locked;
47 
48  // request
49  volatile bool req_ready;
50  uint32_t req_bmap;
51  uint8_t* req_data;
52  uint8_t* req_hdr;
53 
54  // response
55  volatile bool resp_ready;
56  uint32_t resp_bmap;
57  uint8_t* resp_data;
58  uint8_t* resp_hdr;
60 
62 typedef struct SE3_FLASH_INFO_ {
63  uint32_t sector;
64  const uint8_t* base;
65  const uint8_t* index;
66  const uint8_t* data;
67  size_t first_free_pos;
68  size_t used;
69  size_t allocated;
71 
73 typedef struct SE3_SERIAL_ {
74  uint8_t data[SE3_SERIAL_SIZE];
75  bool written;
76 } SE3_SERIAL;
77 
79 typedef struct se3c0_req_header_ {
80  uint16_t cmd;
81  uint16_t cmd_flags;
82  uint16_t len;
83 #if SE3_CONF_CRC
84  uint16_t crc;
85 #endif
86  uint32_t cmdtok[SE3_COMM_N - 1];
88 
90 typedef struct se3c0_resp_header_ {
91  uint16_t ready;
92  uint16_t status;
93  uint16_t len;
94 #if SE3_CONF_CRC
95  uint16_t crc;
96 #endif
97  uint32_t cmdtok[SE3_COMM_N - 1];
99 
101 typedef uint16_t(*se3_cmd_func)(uint16_t, const uint8_t*, uint16_t*, uint8_t*);
102 
104 typedef struct SE3_L0_GLOBALS_ {
105  SE3_SERIAL serial;
106  SE3_FLASH_INFO flash;
107  SE3_COMM_STATUS comm;
110  union {
111  B5_tSha256Ctx sha;
112  B5_tAesCtx aes;
113  } ctx;
114  uint16_t hwerror;
115 
116  uint64_t now;
119 
120 extern uint8_t se3_comm_request_buffer[SE3_COMM_N*SE3_COMM_BLOCK];
121 extern uint8_t se3_comm_response_buffer[SE3_COMM_N*SE3_COMM_BLOCK];
122 extern SE3_L0_GLOBALS se3c0;
123 
response header to be encoded
Definition: se3c0.h:90
This file contains defines and functions common for L0 and L1.
L0 globals structure.
Definition: se3c0.h:104
uint8_t * req_hdr
received header buffer
Definition: se3c0.h:52
uint32_t sector
active sector number
Definition: se3c0.h:63
uint32_t resp_bmap
map of sent response blocks
Definition: se3c0.h:56
uint32_t blocks[SE3_COMM_N]
map of blocks
Definition: se3c0.h:44
struct SE3_FLASH_INFO_ SE3_FLASH_INFO
Flash management structure.
uint32_t magic_bmap
bit map of written magic sectors
Definition: se3c0.h:41
bool magic_ready
magic written flag
Definition: se3c0.h:40
serial number data and state
Definition: se3c0.h:73
struct SE3_COMM_STATUS_ SE3_COMM_STATUS
structure holding host-device communication status and buffers
uint8_t * resp_hdr
buffer for header to be sent
Definition: se3c0.h:58
struct SE3_SERIAL_ SE3_SERIAL
serial number data and state
bool written
Indicates whether the serial number has been set (by FACTORY_INIT)
Definition: se3c0.h:75
uint16_t(* se3_cmd_func)(uint16_t, const uint8_t *, uint16_t *, uint8_t *)
Definition: se3c0.h:101
struct se3c0_req_header_ se3c0_req_header
decoded request header
uint8_t * req_data
received data buffer
Definition: se3c0.h:51
uint8_t * resp_data
buffer for data to be sent
Definition: se3c0.h:57
volatile bool resp_ready
response ready flag
Definition: se3c0.h:55
uint64_t now
current UNIX time in seconds
Definition: se3c0.h:116
Flash management structure.
Definition: se3c0.h:62
struct SE3_L0_GLOBALS_ SE3_L0_GLOBALS
L0 globals structure.
Definition: aes256.h:75
decoded request header
Definition: se3c0.h:79
bool now_initialized
time was initialized
Definition: se3c0.h:117
Definition: sha256.h:40
struct se3c0_resp_header_ se3c0_resp_header
response header to be encoded
uint32_t req_bmap
map of received request blocks
Definition: se3c0.h:50
volatile bool req_ready
request ready flag
Definition: se3c0.h:49
structure holding host-device communication status and buffers
Definition: se3c0.h:38
uint32_t block_guess
guess for next block that will be accessed
Definition: se3c0.h:45
bool locked
prevent magic initialization
Definition: se3c0.h:46