wallet
Dependencies: mbed HTTPClient SHA256 uECC MMA8451Q W5500Interface
main.cpp
00001 #include "mbed.h" 00002 #include "uECC.h" 00003 #include "MMA8451Q.h" 00004 #include "SHA256.h" 00005 #include <string> 00006 #include <stdlib.h> 00007 #include "W5500Interface/EthernetInterface.h" 00008 #include "HTTPClient.h" 00009 using namespace std; 00010 00011 00012 char privateKey[43]; 00013 char publicKey[81]; 00014 00015 char myDevID[6]; 00016 00017 PinName const SDA = PTE25; 00018 PinName const SCL = PTE24; 00019 bool isHashed = false; 00020 00021 #define MMA8451_I2C_ADDRESS (0x1d<<1) 00022 00023 int count = 0; 00024 int blockNo = 0; 00025 SHA256 hash; 00026 unsigned int mac_addr[6] = {0x00, 0x08, 0xdc, 0x1c, 0xb8, 0x4a}; 00027 00028 char* DEV_ID; 00029 00030 static int RNG(uint8_t *dest, unsigned size) { 00031 //TODO: Add Random number according to mac addr 00032 return 1; 00033 } 00034 00035 /* 00036 Generate Keys using uECC, private and public key. secp256k1 00037 */ 00038 void getKeys(){ 00039 const struct uECC_Curve_t * curve = uECC_secp256k1(); 00040 uint8_t privKey[21]; 00041 00042 uint8_t pubKey[40]; 00043 00044 int key_status = uECC_make_key(pubKey, privKey, curve); 00045 if(key_status) 00046 { 00047 for(int i = 0; i < 21; ++i) { 00048 sprintf(privateKey+2*i,"%02x", privKey[i]); 00049 } 00050 for(int j = 0; j < 40; ++j) { 00051 sprintf(publicKey+2*j,"%02x", pubKey[j]); 00052 } 00053 } 00054 00055 wait(1); 00056 } 00057 00058 /* 00059 Send data to the REST API 00060 */ 00061 char* sendData(int block, char* data, char* hash) 00062 { 00063 char sendIP[100]= "192.168.0.110"; 00064 char str[512]; 00065 char get_msg[1024]; 00066 //data?dev_id=123456&block=1&data=1,2,3,4,5,6,7,8,9,10&hash=123456abcdef... 00067 00068 sprintf(get_msg, "http://%s:5000/data?dev_id=%s&block=%d&data=%s&hash=%s", sendIP,DEV_ID,block,data,hash); 00069 00070 HTTPClient http; 00071 00072 printf("msg : %s\r\n",get_msg); 00073 int ret = http.get(get_msg, str, sizeof(str)); 00074 if(!ret) 00075 { 00076 printf("\r\nPage fetched successfully - read %d characters\r\n", strlen(str)); 00077 printf("Result: %s\r\n", str); 00078 memset(&myDevID,'\0',6); 00079 strcat(myDevID,str); 00080 return myDevID; 00081 } 00082 else 00083 { 00084 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); 00085 return "Error"; 00086 } 00087 } 00088 00089 /* 00090 Function to get the hash of the data 00091 Input- SHA256 Object 00092 Output- char* 00093 */ 00094 char* hashData(SHA256 data) 00095 { 00096 return (char*)hash.hexString().c_str(); 00097 } 00098 00099 /* 00100 Function to sign the data 00101 Input- char* 00102 Output- char* 00103 */ 00104 char* signData(char* data) 00105 { 00106 const struct uECC_Curve_t * curve = uECC_secp256k1(); 00107 return (char*)uECC_shared_secret((uint8_t*)publicKey, (uint8_t*)privateKey, (uint8_t*)data, curve); 00108 } 00109 /* 00110 Register the device, public key using REST API 00111 */ 00112 char* registerDevice(char* pubKey) 00113 { 00114 char sendIP[100]= "192.168.0.110"; 00115 char str[512]; 00116 char get_msg[512]; 00117 sprintf(get_msg, "http://%s:5000/register?PublicKey=%s", sendIP,pubKey); 00118 //"http://192.168.0.110:5000/register?PublicKey=7834783543874"; 00119 00120 HTTPClient http; 00121 00122 printf("msg : %s\r\n",get_msg); 00123 int ret = http.get(get_msg, str, sizeof(str)); 00124 if(!ret) 00125 { 00126 printf("\r\nPage fetched successfully - read %d characters\r\n", strlen(str)); 00127 printf("Result: %s\r\n", str); 00128 memset(&myDevID,'\0',6); 00129 strcat(myDevID,str); 00130 return myDevID; 00131 } 00132 else 00133 { 00134 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); 00135 return "Error"; 00136 } 00137 } 00138 00139 int main(void) 00140 { 00141 char data[30]; 00142 memset(data,'\0',strlen(data)); 00143 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); 00144 PwmOut rled(LED1); 00145 PwmOut gled(LED2); 00146 PwmOut bled(LED3); 00147 uECC_set_rng(&RNG); 00148 printf("My Keys are\r\n"); 00149 getKeys(); 00150 printf("Public Key %s\r\n", publicKey); 00151 printf("Private Key %s\r\n", privateKey); 00152 printf("MMA8451 ID: %d\r\n", acc.getWhoAmI()); 00153 uint8_t myPos = acc.getOrientation(); 00154 uint8_t myOldPos = myPos; 00155 00156 //Ethernet enable 00157 printf("spi init\r\n"); 00158 SPI spi(D11, D12, D13); // mosi, miso, sclk 00159 wait(1); // 1 second for stable state 00160 EthernetInterface eth(&spi, D10, D9);//scs(D10), nRESET(PTA20) 00161 printf("App Start\r\n"); 00162 wait(1); // 1 second for stable state 00163 00164 uint8_t mac_addr[6] = {0x22, 0x12, 0x34, 0x1c, 0xb8, 0x4a}; // for example 00165 00166 eth.init((uint8_t *)mac_addr); //Use DHCP 00167 eth.connect(); 00168 printf("Initialized, MAC: %s\r\n", eth.getMACAddress()); 00169 printf("Connected, IP: %s, MASK: %s, GW: %s\r\n", 00170 eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); 00171 00172 DEV_ID = (char*)registerDevice(publicKey); 00173 if(DEV_ID == "Error") 00174 { 00175 printf("ERROR IN REGISTRATION\r\n"); 00176 while(1); 00177 } 00178 while(true) 00179 { 00180 myPos = acc.getOrientation(); 00181 if(abs((int)myPos - (int)myOldPos) > 1) 00182 { 00183 printf("Count "); 00184 count++; 00185 printf("%d\r\n",count); 00186 char temp[3]; 00187 sprintf(temp,"%d",count); 00188 strcat(data,temp); 00189 strcat(data,","); 00190 myOldPos = myPos; 00191 isHashed = false; 00192 } 00193 if(count % 10 == 0 && count !=0 && isHashed == false) 00194 { 00195 blockNo++; 00196 data[strlen(data)-1] = '\0'; 00197 printf("Data %s\r\n", data); 00198 hash.append(data); 00199 hash.finalize(); 00200 printf("hash for block %d ", blockNo); 00201 printf("%s\r\n", hashData(hash)); 00202 sendData(blockNo, data, hashData(hash)); 00203 hash.reset(); 00204 memset(data,'\0',strlen(data)); 00205 isHashed = true; 00206 } 00207 wait(1); 00208 } 00209 } 00210 00211
Generated on Thu Jul 28 2022 15:47:26 by 1.7.2