SEcube open-source SDK
L0_base.h
Go to the documentation of this file.
1 
10 #ifndef _L0_BASE_H
11 #define _L0_BASE_H
12 
13 #include <stdio.h> //miss
14 #include <stdint.h>
15 #include <vector>
16 #include <stddef.h>
17 #include <time.h>
18 #include <memory>
19 #ifdef _WIN32
20 //Windows -> include "Windows.h" only for windows 32 file
21 #include "Windows.h"
22 #else
23 //include for linux
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <malloc.h>
31 #include <errno.h>
32 
33 #endif
34 
35 #include "../L0_enumerations.h"
36 
37 //#define SE3_MAX_PATH 256
38 //#define SE3_SN_SIZE 32
39 #define SE3_DRIVE_BUF_MAX 1024
40 #define SE3_MAGIC_FILE_LEN 9
41 #define SE3C_MAGIC_TIMEOUT 1000
42 
43 //#define FSCTL_IS_VOLUME_MOUNTED 0x90028
44 //#define ERROR_FILE_CHECKED_OUT 220L
45 
46 #define SE3GET16(x, pos, val) do{ memcpy((void*)&(val), ((uint8_t*)(x))+pos, 2); }while(0)
47 #define SE3GET32(x, pos, val) do{ memcpy((void*)&(val), ((uint8_t*)(x))+pos, 4); }while(0)
48 #define SE3SET16(x, pos, val) do{ memcpy(((uint8_t*)(x))+pos, (void*)&(val), 2); }while(0)
49 #define SE3SET32(x, pos, val) do{ memcpy(((uint8_t*)(x))+pos, (void*)&(val), 4); }while(0)
50 
51 #ifdef _WIN32
52 //WINDOWS
53 #define SE3_OSSEP L'\\\\' //error #define SE3_OSSEP L'\\' -> only one backslash
54 #define SE3_MAGIC_FILE L".se3magic"
55 #define Se3Sleep() Sleep(0)
56 #else
57 #define SE3_OSSEP '/' //error #define SE3_OSSEP L'\' -> nop backshash miss '
58 #define SE3_MAGIC_FILE ".se3magic"
59 #define Se3Sleep() usleep(1000)
60 #endif
61 
62 #ifdef _DEBUG
63 //Debug mode
64 #define Se3Trace(msg) printf msg
65 #else
66 //Release mode
67 #define Se3Trace(msg)
68 #endif
69 
70 const uint8_t se3Magic[L0Communication::Size::MAGIC] = {
71  0x3c, 0xab, 0x78, 0xb6, 0x2, 0x64, 0x47, 0xe9, 0x30, 0x26, 0xd4, 0x1f, 0xad, 0x68, 0x22, 0x27,
72  0x41, 0xa4, 0x32, 0xba, 0xbe, 0x54, 0x83, 0xee, 0xab, 0x6b, 0x62, 0xce, 0xf0, 0x5c, 0x7, 0x91
73 };
74 
75 const uint16_t se3Crc16Table[0x100] = {
76  0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
77  0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
78  0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485, 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
79  0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4, 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
80  0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823, 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
81  0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12, 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
82  0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
83  0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70, 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
84  0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F, 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
85  0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
86  0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D, 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
87  0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C, 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
88  0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB, 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
89  0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A, 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
90  0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
91  0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0 };
92 
93 #ifdef _WIN32
94 //WINDOWS
95 typedef wchar_t se3Char;
96 #else
97 //UNIX
98 typedef char se3Char;
99 #endif
100 
101 #ifdef _WIN32
102 typedef BOOL(WINAPI *tADVAPI32_CryptAcquireContextW)(
103  ULONG_PTR *phProv,
104  LPCWSTR szContainer,
105  LPCWSTR szProvider,
106  DWORD dwProvType,
107  DWORD dwFlags);
108 
109 typedef BOOL(WINAPI *tADVAPI32_CryptGenRandom)(
110  ULONG_PTR hProv,
111  DWORD dwLen,
112  BYTE *pbBuffer);
113 
114 typedef BOOL(WINAPI *tADVAPI32_CryptReleaseContext)(
115  ULONG_PTR hProv,
116  DWORD dwFlags);
117 #endif
118 
119 typedef struct se3DiscoverInfo_ {
120  uint8_t serialno[L0Communication::Size::SERIAL];
121  uint8_t hello_msg[L0Communication::Size::HELLO];
122  uint16_t status;
123 } se3DiscoverInfo;
124 
125 typedef struct se3DeviceInfo_ {
126  se3Char path[L0Communication::Parameter::SE3_MAX_PATH];
127  uint8_t serialno[L0Communication::Size::SERIAL];
128  uint8_t helloMsg[L0Communication::Size::HELLO];
129  uint16_t status;
130 } se3DeviceInfo;
131 
132 typedef struct se3DriveIt_ {
133  se3Char* path;
134  se3Char buf[SE3_DRIVE_BUF_MAX + 1];
135  size_t bufLen;
136 #ifdef _WIN32
137 //WINDOWS
138  size_t pos;
139 #else
140 //UNIX
141  FILE* fp;
142 #endif
143 } se3DriveIt;
144 
145 typedef struct se3DiscoIt_ {
146  se3DeviceInfo deviceInfo;
147  se3DriveIt driveIt;
148 } se3DiscoIt;
149 
150 typedef struct {
151 #ifdef _WIN32
152 //WINDOWS
153  OVERLAPPED ol;
154  HANDLE h;
155 #else
156 //UNIX
157  int fd;
158  void* buf;
159  bool locked;
160 #endif
161 }se3File;
162 
163 typedef struct se3Device_ {
164  se3DeviceInfo info;
165  std::shared_ptr<uint8_t> request;
166  std::shared_ptr<uint8_t> response;
167  se3File f;
168  bool opened;
169 } se3Device;
170 
171 class L0Base {
172 private:
173  se3DiscoIt it;
174  std::vector<se3Device> dev;
175  uint8_t ptr;
176 
177 public:
178  //ctor
179  L0Base();
180  //dector
181  ~L0Base();
182 
183  //method to add a device to the array
184  void AddDevice();
185  //read the amount of devices connected
186  size_t GetNDevices();
187  //method to clear the device array
188  void ResetDeviceArray();
189 
191  //GET METHODS//
193  //Device GET methods
194  se3File GetDeviceFile(); //returns dev[this->ptr].f
195  uint8_t* GetDeviceHelloMsg(); //returns dev[this->ptr].info.helloMsg
196  se3Char* GetDeviceInfoPath(); //returns dev[this->ptr].info.path
197  uint8_t* GetDeviceInfoSerialNo(); //dev[this->ptr].info.serialno
198  bool GetDeviceOpened(); //dev[this->ptr].opened
199  uint8_t GetDevicePtr(); //returns ptr
200  uint8_t* GetDeviceRequest(); //returns dev[this->ptr].request
201  uint8_t* GetDeviceResponse(); //returns dev[this->ptr].response
202  //Iterator GET methods
203  uint8_t* GetDiscoDeviceHelloMsg(); //returns it.deviceInfo.helloMsg
204  se3Char* GetDiscoDevicePath(); //returns it.deviceInfo.path
205  uint8_t* GetDiscoDeviceSerialNo(); //returns it.deviceInfo.serialno
206  se3Char* GetDiscoDriveBuf(); //returns it.driveIt.buf
207  size_t GetDiscoDriveBufLen(); //returns it.driveIt.bufLen
208  se3Char* GetDiscoDrivePath(); //returns it.driveIt.path
209  FILE * GetDiscoDriveFile(); //returns it.driveIt.fp
211  //SET METHODS//
213  //buffer allocation/deallocation
214  void AllocateDeviceRequest(); //allocates dev[this->ptr].request
215  void AllocateDeviceResponse(); //allocates dev[this->ptr].response
216  void FreeDeviceRequest(); //free the allocation dev[this->ptr].request
217  void FreeDeviceResponse(); //free the allocation dev[this->ptr].response
218  //device SET methods
219  void SetDeviceFile(se3File file); //sets dev[this->ptr].f = file
220  void SetDeviceOpened(bool opened); //sets dev[this->ptr].opened = opened
221  bool SetDevicePtr(uint16_t newPtr); //sets ptr = newPtr
222  //iterator SET methods
223  void SetDiscoDeviceStatus(uint16_t status); //sets it.deviceInfo.status = status
224  void SetDiscoDriveBufLen(size_t bufLen); //sets it.driveIt.bufLen = bufLen
225  void SetDiscoDriveBufTermination(); //sets it.driveIt.buf[this->it.driveIt.bufLen] = L'\0'
226  void SetDiscoDrivePath(se3Char* path); //sets it.driveIt.path = path
227 
228  void SetDiscoDriveFile(FILE* fp); //set it.driverit.fp = fp
229 };
230 
231 class L0Support {
232 private:
233  L0Support() {};
234 
235 public:
236  static void Se3PathCopy(se3Char* dest, se3Char* src);
237  static bool Se3Win32DiskInDrive(wchar_t* path);
238  static uint64_t Se3Deadline(uint32_t timeout);
239  static uint64_t Se3Clock();
240  static bool Se3WriteMagic(se3File hFile);
241  static bool Se3Write(uint8_t* buf, se3File hfile, size_t block, size_t nBlocks, uint32_t timeout);
242  static bool Se3Read(uint8_t* buf, se3File hFile, size_t block, size_t nBlocks, uint32_t timeout);
243  static void Se3Close(se3File hFile);
244  static uint16_t Se3OpenExisting(se3Char* path, bool rw, uint64_t deadline, se3File* phFile);
245  static void Se3MakePath(se3Char* dest, se3Char* src);
246  static bool Se3MagicInit(se3Char* path, uint8_t* discoBuf, se3DiscoverInfo* info);
247  static bool Se3ReadInfo(uint8_t* buf, se3DiscoverInfo* info);
248  static void Se3Rand(size_t len, uint8_t* buf);
249  static uint16_t Se3ReqLenDataAndHeaders(uint16_t dataLen);
250  static uint16_t Se3RespLenData(uint16_t lenDataAndHeaders);
251  static uint16_t Se3NBlocks(uint16_t len);
252  static uint16_t Se3Crc16Update(size_t dataLen, const uint8_t* data, uint16_t crc);
253 
254  static bool se3UnixLock(int fd);
255  static void se3UnixUnlock(int fd);
256 
257  static void DebugFileCreation();
258 };
259 
260 #endif
L0_base.h
Prototypes of the L0Base library.