Cybersecurity Project / Mbed 2 deprecated sd_duplicator

Dependencies:   mbed-rtos mbed wolfSSL

Fork of sd_duplicator by Titi Asrat

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 #include "SDBlock.h"
00004 #include <wolfssl/wolfcrypt/ecc.h>
00005 
00006 DigitalOut led1(LED1);
00007 DigitalOut led4(LED4);
00008 
00009 uint8_t hash[32];
00010 uint8_t buffer[512];
00011 uint8_t signature[5];
00012 Sha256 h; 
00013 ecc_key key;
00014 RNG rng;
00015 
00016 byte sig[512]; // will hold generated signature
00017 
00018 
00019 int main() 
00020 {   
00021     SDBlock original(p5, p6, p7, p22);
00022     const int x = original.disk_initialize();
00023     printf("************\r\n");
00024     printf("%d\r\n", x);
00025     SDBlock duplicate(p11, p12, p13, p21);   
00026     const int z = duplicate.disk_initialize();
00027     printf("%d\r\n", z);
00028     const int y = original.disk_status();
00029     printf("original status: %d\r\n", y);
00030     const int t = duplicate.disk_status();
00031     printf("duplicate status: %d\r\n", t);
00032     //s is sector 
00033     const uint32_t s = original.disk_sectors();
00034     printf("sector size %d\r\n", s);
00035     printf("buffer %d\r\n", buffer);
00036     
00037     wc_InitSha256(&h);
00038     
00039     
00040     //start time
00041     //time_t time_start = time(NULL);
00042     for (int i = 0; i <= 99; ++i)
00043     {
00044         const int r = original.disk_read(buffer, i, 1);
00045         if (r !=0)
00046         {
00047             printf("%d\r\n", r);
00048         }   
00049         printf("disk read ");
00050        /* const int rd2 = duplicate.disk_write(buffer, i, 1);
00051         if (rd2 != 0)
00052         {
00053            printf("%d\n", rd2);
00054         }*/
00055         //update hash
00056         wc_Sha256Update(&h, buffer, 512);
00057         printf("digest updated, block %d  \r\n",i);
00058         
00059       /*  if(i % 1000 == 0)
00060         {
00061             status update
00062             double percentage = (i/s) * 100;
00063             printf("percent copied: %d\n", percentage);
00064             time elapsed
00065             time_t time_current = time(NULL);
00066             int time_working = time_start - time_current;
00067             double speed = (i * 512) / time_working;
00068             
00069             printf("speed (bytes/s): %d\n", speed);
00070         } */
00071     }//end of for 
00072     wc_Sha256Final(&h,hash);
00073     //sign the hash
00074     int ret;
00075     uint32_t sigSz; 
00076     wc_InitRng(&rng);
00077     wc_ecc_init(&key);
00078 
00079        
00080     //print the hash
00081     printf("hash: ");
00082     for(int i = 0; i < 32; ++i)
00083         printf("%02x", hash[i]);
00084     printf("\r\n");
00085     
00086     //print signature
00087     printf("hash: ");
00088     //byte digest[] = hash;
00089     //int wc_ecc_sign_hash(const byte* hash, uint32_t len(hash), byte* sig, word32 *outlen, RNG* rng, 
00090 //ecc_key* key);
00091     uint32_t hashsize = 8; 
00092     wc_InitRng(&rng); // initialize rng
00093     wc_ecc_init(&key); // initialize key
00094     wc_ecc_make_key(&rng, 32, &key); // make public/private key pair
00095     sigSz = sizeof(sig);
00096     ret = wc_ecc_sign_hash(hash, hashsize, sig, &sigSz,&rng, &key);
00097     
00098     printf("ret %i  \r\n",ret);
00099     
00100     if ( ret != 0 ) 
00101     {
00102     // error generating message signature
00103         printf("Error generating signature/r/n");
00104     }
00105     printf("finished\r\n");
00106     
00107     for(int i = 0; i < 512; ++i)
00108         printf("%02x", sig[i]);
00109     printf("\r\n");
00110  
00111     //when finished do this light pattern   
00112     while(true)
00113     {
00114         led1=0;
00115         led4=1;
00116         wait(.5);
00117         led1=1;
00118         led4=0;
00119         wait(.5);
00120     } 
00121 }//end main