intiial commit

Dependencies:   MAX8614X USBDevice max32630hsp_test

Revision:
7:5b7f7ec9efe0
Parent:
6:0ec9dd0e8eb0
Child:
8:166e2b836050
--- a/main.cpp	Sun Aug 12 00:02:14 2018 +0000
+++ b/main.cpp	Tue Aug 14 05:22:46 2018 +0000
@@ -36,6 +36,9 @@
 #include "max32630hsp.h"
 #include "MAX8614X.h"
 #include "USBSerial.h"
+void executeSha256(uint8_t *challenge, unsigned int challengeLen);
+void executeSha(uint32_t *challenge, unsigned int challengeLen);
+
 
 MAX32630HSP icarus(MAX32630HSP::VIO_1V8);
 //    MAX32630FTHR mbed_board(MAX32630FTHR::VIO_1V8);
@@ -57,19 +60,18 @@
 DigitalOut bLED(LED3);
 int main()
 {
-    #define CHALLENGE_SZ 20
-    static const uint8_t challenge_1[CHALLENGE_SZ] = {  // 160 bit
-        0x5e, 0x81, 0x35, 0x24,
-        0x56, 0x63, 0xd6, 0x09,
-        0x99, 0x8d, 0x7b, 0x0d,
-        0x52, 0x12, 0x84, 0x65,
-        0xcd, 0x0d, 0xe3, 0x01
+    #define CHALLENGE_SZ_NO_SECRET 20
+    uint32_t challenge_1[5] = {  // 160 bit = 5*32 bits
+      0x5e813524,
+      0x5663d609,
+      0x998d7b0d,
+      0x52128465,
+      0xcd0de301
     };
 
+
     #define MAC_SZ 32
-    uint8_t macData[MAC_SZ];
-    uint8_t data[5];
-    int i;
+
 
 //    daplink.printf("daplink serial port\r\n");
 //    microUSB.printf("micro USB serial port\r\n");
@@ -79,72 +81,12 @@
 
     rLED = LED_OFF;
     printf("\r\n\rmax86140 authenication software\r\n");
-    MAX8614X m(spi,cs,interrupt_pin);
-    m.readRegister(MAX8614X::MAX8614X_PART_ID_REG, data, 1);
-    printf("device id should be 0x24, reg %02X = %02X\r\n", MAX8614X::MAX8614X_PART_ID_REG, data[0]);
-
-    //● Enable SHA_DONE Interrupt
-    m.writeRegister(MAX8614X::MAX8614X_INT_ENABLE2_REG, MAX8614X::MAX8614X_IE_SHA_DONE_EN);
-
-    m.readRegister(MAX8614X::MAX8614X_INT_ENABLE2_REG, data, 1);
-    printf("enable sha done interrupt, reg %02X = %02X\r\n", MAX8614X::MAX8614X_INT_ENABLE2_REG, data[0]);
-
-
-    //● Enable SHA_EN bit.
-    m.writeRegister(MAX8614X::MAX8614X_SHA_CFG_REG,MAX8614X::MAX8614X_SHACFG_SHA_EN);
-
-    m.readRegister(MAX8614X::MAX8614X_SHA_CFG_REG, data, 1);
-    printf("enable sha CFG,  reg %02X = %02X\r\n", MAX8614X::MAX8614X_SHA_CFG_REG, data[0]);
-    
-    //● Write 160-bit random challenge value to RAM using registers MEM_IDX and MEM_DATA.
-    // Enable Memory Write, Select Bank 0, address 0x00 to 0xFF
-    m.writeRegister(MAX8614X::MAX8614X_MEMORY_CONTROL_REG, MAX8614X::MAX8614X_MEMCNTRL_WR_EN_MASK | MAX8614X::MAX8614X_MEMCNTRL_BANK0_MASK);
-       m.readRegister(MAX8614X::MAX8614X_MEMORY_CONTROL_REG, data, 1);
-    printf("MEM CNTRL REG should be 2,  reg %02X = = %02X\r\n", MAX8614X::MAX8614X_MEMORY_CONTROL_REG, data[0]);
- 
-    for (i = 0; i < CHALLENGE_SZ; i++) {
-        m.writeRegister(MAX8614X::MAX8614X_MEMORY_INDEX_REG, i);
-        m.writeRegister(MAX8614X::MAX8614X_MEMORY_DATA_REG, challenge_1[i]);
-    } 
+//    MAX8614X m(spi,cs,interrupt_pin);
+//    m.readRegister(MAX8614X::MAX8614X_PART_ID_REG, data, 1);
+//    printf("device id should be 0x24, reg %02X = %02X\r\n", MAX8614X::MAX8614X_PART_ID_REG, data[0]);
 
-    for (i = 0; i < CHALLENGE_SZ; i++) {
-        m.writeRegister(MAX8614X::MAX8614X_MEMORY_INDEX_REG, i);
-        m.readRegister(MAX8614X::MAX8614X_MEMORY_DATA_REG, data, 1);
-        printf("%02x ", data[0]);
-    } 
-    printf("%\r\n");
-    // The message block consists of a 160-bit secret, a 160-bit challenge and 192 bits of constant data. Optionally, the 64-bit
-    // ROM ID replaces 64 of the 192 bits of constant data used in the hash operation. 16 bits out of the 160-bit secret and 16
-    // bits of ROM ID are programmable–8 bits each in metal and 8 bits each in OTP bits
-    //● Write command, with ROM ID (0x35) or without ROM ID (0x36), to SHA_CMD register
-    m.writeRegister(MAX8614X::MAX8614X_SHA_CMD_REG,MAX8614X::MAX8614X_SHACMD_MAC_ROM_ID);
-
-    m.readRegister(MAX8614X::MAX8614X_SHA_CMD_REG, data, 1);
-    printf("sha cmd reg should be 0x35 mac rom id,  reg %02X = %02X\r\n", MAX8614X::MAX8614X_SHA_CMD_REG, data[0]);
-        m.readRegister(MAX8614X::MAX8614X_INT_STATUS2_REG, data, 1);
-        printf("int status2, reg %02X = %X\r\n", MAX8614X::MAX8614X_INT_STATUS2_REG, data[0]);
-    //● Write 1 to SHA_START and 1 to SHA_EN bit.
-    m.writeRegister(MAX8614X::MAX8614X_SHA_CFG_REG,MAX8614X::MAX8614X_SHACFG_SHA_EN | MAX8614X::MAX8614X_SHACFG_SHA_START);
-    m.readRegister(MAX8614X::MAX8614X_SHA_CFG_REG, data, 1);
-    printf("sha CFG reg should be 0x3,  reg %02X = %02X\r\n", MAX8614X::MAX8614X_SHA_CFG_REG, data[0]);
-    
-    //● Wait for SHA_DONE interrupt.
-    m.clearShaComplete();
-    data[0] = 0;
-//  while(!m.isShaComplete()) {
-    while(!m.isShaComplete() && !data[0] ) {
-        m.readRegister(MAX8614X::MAX8614X_INT_STATUS2_REG, data, 1);
-        printf("int status2, reg %02X = %X\r\n", MAX8614X::MAX8614X_INT_STATUS2_REG, data[0]);
-        
-    }   
-    // ● Read 256 MAC value from RAM using registers MEM_IDX and MEM_DATA.
-    for (i = 0; i < MAC_SZ; i++) {
-        m.writeRegister(MAX8614X::MAX8614X_MEMORY_INDEX_REG, i);
-        m.readRegister(MAX8614X::MAX8614X_MEMORY_DATA_REG, data, 1);
-        macData[i] = data[0];
-        printf("%02x ", data[0]);
-    } 
-     printf("%\r\n");
+//    executeSha256(challenge_1, CHALLENGE_SZ_NO_SECRET);
+    executeSha(challenge_1, 5);
    //● Compare MAC from MAX86140 wth Host's precalculated MAC.
     //● Check PASS or FAIL.
     //● Disable SHA_EN bit ( Write 0 to SHA_EN bit).     
@@ -154,3 +96,106 @@
     }
 }
 
+void transformData(uint8_t *inData, uint8_t *outData, unsigned int challengeLen)
+{
+    int i, j, k;
+    k = 0;
+    for (i = 0; i < (challengeLen/4); i++) {
+        for (j = 3; j >= 0; j--) {
+            outData[j+(i*4)] = inData[k];
+            k++;
+        }
+    }
+}
+
+
+
+//******************************************************************
+void executeSha(uint32_t *challenge, unsigned int challengeLen)
+{
+    int i, j, k, ctr;;
+    uint8_t macData[256];
+    uint8_t xData[256];
+    uint32_t tmpData;
+    uint8_t data[5];
+    uint8_t romID;
+    MAX8614X m(spi,cs,interrupt_pin);
+    //● Enable SHA_DONE Interrupt
+    m.writeRegister(MAX8614X::MAX8614X_INT_ENABLE2_REG, MAX8614X::MAX8614X_IE_SHA_DONE_EN);
+
+    m.writeRegister(MAX8614X::MAX8614X_INT_ENABLE1_REG, 0);  // Disable all other interrupts
+
+    //● Enable SHA_EN bit.
+    m.writeRegister(MAX8614X::MAX8614X_SHA_CFG_REG,MAX8614X::MAX8614X_SHACFG_SHA_EN);
+
+    //● Write 160-bit random challenge value to RAM using registers MEM_IDX and MEM_DATA.
+    // Enable Memory Write, Select Bank 0, address 0x00 to 0xFF
+    m.writeRegister(MAX8614X::MAX8614X_MEMORY_CONTROL_REG, MAX8614X::MAX8614X_MEMCNTRL_WR_EN_MASK | MAX8614X::MAX8614X_MEMCNTRL_BANK0_MASK);
+
+    printf("\r\n Raw Input Data\r\n\r\n");
+    for (i = 0; i < challengeLen; i++) {
+        printf("% 08x\r\n", challenge[i]);
+    }
+    printf("\r\n");
+    k = 0;
+    for (i = 0; i < (challengeLen); i++) {
+        tmpData = challenge[i];
+        for (j = 3; j >= 0; j--) {
+            xData[k] = tmpData & 0xFF;
+            tmpData = tmpData >> 8;
+            k++;
+        }
+    }
+
+    printf("\r\n Transformed Input Data\r\n\r\n");
+    for (i = 0; i < challengeLen*4; i++) {
+        if (!(i % 4))
+            printf("\r\n ");
+            
+        printf("%02x", xData[i]);
+    }
+        printf("\r\n ");
+        printf("\r\n ");
+        
+    for (i = 0; i < (challengeLen*4); i++) {
+        m.writeRegister(MAX8614X::MAX8614X_MEMORY_INDEX_REG, i);
+        m.writeRegister(MAX8614X::MAX8614X_MEMORY_DATA_REG, xData[i]);
+    }
+    printf("%\r\n");
+
+    // The message block consists of a 160-bit secret, a 160-bit challenge and 192 bits of constant data. Optionally, the 64-bit
+    // ROM ID replaces 64 of the 192 bits of constant data used in the hash operation. 16 bits out of the 160-bit secret and 16
+    // bits of ROM ID are programmable–8 bits each in metal and 8 bits each in OTP bits
+    //● Write command, with ROM ID (0x35) or without ROM ID (0x36), to SHA_CMD register
+    m.writeRegister(MAX8614X::MAX8614X_SHA_CMD_REG,MAX8614X::MAX8614X_SHACMD_MAC_ROM_ID);
+//    m.writeRegister(MAX8614X::MAX8614X_SHA_CMD_REG,MAX8614X::MAX8614X_SHACMD_MAC_NO_ROM_ID);
+
+
+    m.readRegister(MAX8614X::MAX8614X_INT_STATUS2_REG, data, 1);  // clear the status register
+    //● Write 1 to SHA_START and 1 to SHA_EN bit.
+    m.writeRegister(MAX8614X::MAX8614X_SHA_CFG_REG,MAX8614X::MAX8614X_SHACFG_SHA_EN | MAX8614X::MAX8614X_SHACFG_SHA_START);
+    
+    //● Pole the SHA_DONE interrupt.
+    data[0] = 0;
+    ctr = 0;
+    while(!data[0] && ctr++ < 1000 ) {
+        m.readRegister(MAX8614X::MAX8614X_INT_STATUS2_REG, data, 1);
+    }
+    // ● Read 256 MAC value from RAM using registers MEM_IDX and MEM_DATA.
+    printf("\r\n Response Data ");
+    if (romID == MAX8614X::MAX8614X_SHACMD_MAC_ROM_ID)
+        printf("with ROM ID\r\n");
+    else if (romID == MAX8614X::MAX8614X_SHACMD_MAC_NO_ROM_ID)
+        printf("with no ROM ID\r\n");
+    else
+        printf(" The SHA CMD register is incorrect!\r\n");
+    for (i = 64; i < 64+32; i++) {
+        if (!(i % 4))
+            printf("\r\n ");
+        m.writeRegister(MAX8614X::MAX8614X_MEMORY_INDEX_REG, i);
+        m.readRegister(MAX8614X::MAX8614X_MEMORY_DATA_REG, data, 1);
+        macData[i] = data[0];
+        printf("%02x", data[0]);
+    } 
+     printf("%\r\n");
+}
\ No newline at end of file