intiial commit

Dependencies:   MAX8614X USBDevice max32630hsp_test

Committer:
phonemacro
Date:
Fri Aug 17 05:37:08 2018 +0000
Revision:
24:3981488a0abd
Parent:
23:4afef8872432
Added comments to main.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phonemacro 0:9e5a4f845510 1 /**********************************************************************
phonemacro 1:854f8a89a527 2 * Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved.
phonemacro 0:9e5a4f845510 3 *
phonemacro 0:9e5a4f845510 4 * Permission is hereby granted, free of charge, to any person obtaining a
phonemacro 0:9e5a4f845510 5 * copy of this software and associated documentation files (the "Software"),
phonemacro 0:9e5a4f845510 6 * to deal in the Software without restriction, including without limitation
phonemacro 0:9e5a4f845510 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
phonemacro 0:9e5a4f845510 8 * and/or sell copies of the Software, and to permit persons to whom the
phonemacro 0:9e5a4f845510 9 * Software is furnished to do so, subject to the following conditions:
phonemacro 0:9e5a4f845510 10 *
phonemacro 0:9e5a4f845510 11 * The above copyright notice and this permission notice shall be included
phonemacro 0:9e5a4f845510 12 * in all copies or substantial portions of the Software.
phonemacro 0:9e5a4f845510 13 *
phonemacro 0:9e5a4f845510 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
phonemacro 0:9e5a4f845510 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
phonemacro 0:9e5a4f845510 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
phonemacro 0:9e5a4f845510 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
phonemacro 0:9e5a4f845510 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
phonemacro 0:9e5a4f845510 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
phonemacro 0:9e5a4f845510 20 * OTHER DEALINGS IN THE SOFTWARE.
phonemacro 0:9e5a4f845510 21 *
phonemacro 0:9e5a4f845510 22 * Except as contained in this notice, the name of Maxim Integrated
phonemacro 0:9e5a4f845510 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
phonemacro 0:9e5a4f845510 24 * Products, Inc. Branding Policy.
phonemacro 0:9e5a4f845510 25 *
phonemacro 0:9e5a4f845510 26 * The mere transfer of this software does not imply any licenses
phonemacro 0:9e5a4f845510 27 * of trade secrets, proprietary technology, copyrights, patents,
phonemacro 0:9e5a4f845510 28 * trademarks, maskwork rights, or any other form of intellectual
phonemacro 0:9e5a4f845510 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
phonemacro 0:9e5a4f845510 30 * ownership rights.
phonemacro 0:9e5a4f845510 31 **********************************************************************/
phonemacro 0:9e5a4f845510 32
phonemacro 0:9e5a4f845510 33
phonemacro 0:9e5a4f845510 34 #include "mbed.h"
phonemacro 0:9e5a4f845510 35 //#include "max32630fthr.h"
phonemacro 0:9e5a4f845510 36 #include "max32630hsp.h"
phonemacro 0:9e5a4f845510 37 #include "MAX8614X.h"
phonemacro 0:9e5a4f845510 38 #include "USBSerial.h"
phonemacro 21:0a4f90fb3451 39
phonemacro 21:0a4f90fb3451 40 //#define DEBUG_ON 1
phonemacro 21:0a4f90fb3451 41
phonemacro 22:1be3a5f0223e 42 void executeSha256(MAX8614X &m, uint32_t *challenge, unsigned int challengeLen, bool romID, uint32_t *response);
phonemacro 16:d0f8cf9f160d 43 bool isTheChipAuthenicated(uint32_t *expectedResponse, uint32_t *chipResponse);
phonemacro 7:5b7f7ec9efe0 44
phonemacro 16:d0f8cf9f160d 45 #define RESPONSE_LEN32 8
phonemacro 0:9e5a4f845510 46
phonemacro 0:9e5a4f845510 47 MAX32630HSP icarus(MAX32630HSP::VIO_1V8);
phonemacro 0:9e5a4f845510 48 // MAX32630FTHR mbed_board(MAX32630FTHR::VIO_1V8);
phonemacro 0:9e5a4f845510 49 SPI spi(P5_1, P5_2, P5_0); /* mosi, miso, sclk */
phonemacro 3:2272f89aad7e 50 DigitalOut cs(P5_3);
phonemacro 18:a0356b3e6ed9 51 PinName interrupt_pin = P3_2;
phonemacro 0:9e5a4f845510 52 // Virtual serial port over USB
phonemacro 0:9e5a4f845510 53 USBSerial microUSB;
phonemacro 0:9e5a4f845510 54
phonemacro 0:9e5a4f845510 55 DigitalOut rLED(LED1);
phonemacro 0:9e5a4f845510 56 DigitalOut gLED(LED2);
phonemacro 0:9e5a4f845510 57 DigitalOut bLED(LED3);
phonemacro 24:3981488a0abd 58 // main() runs in its own thread in the OS
phonemacro 24:3981488a0abd 59 // (note the calls to Thread::wait below for delays)
phonemacro 24:3981488a0abd 60 /**
phonemacro 24:3981488a0abd 61 * @brief Sample main program for SHA256 Authenication using the MAX86140
phonemacro 24:3981488a0abd 62 * @version 1.0000.0
phonemacro 24:3981488a0abd 63 *
phonemacro 24:3981488a0abd 64 * @details Sample main program for MAX86140 authenication.
phonemacro 24:3981488a0abd 65 * The prints are sent to the terminal window (9600, 8n1).
phonemacro 24:3981488a0abd 66 * The program issues challenges to the MAX86140 SHA256 athenicator.
phonemacro 24:3981488a0abd 67 * The responses are compared to the expected responses.
phonemacro 24:3981488a0abd 68 * To run the program, drag and drop the .bin file into the
phonemacro 24:3981488a0abd 69 * DAPLINK folder. After it finishes flashing, cycle the power or
phonemacro 24:3981488a0abd 70 * reset the board.
phonemacro 24:3981488a0abd 71 */
phonemacro 24:3981488a0abd 72
phonemacro 0:9e5a4f845510 73 int main()
phonemacro 0:9e5a4f845510 74 {
phonemacro 23:4afef8872432 75 #define CHALLENGE32_LEN 5
phonemacro 23:4afef8872432 76 uint32_t challenge_A[CHALLENGE32_LEN] = { // 160 bit, no ROM
phonemacro 23:4afef8872432 77 0x00000001,
phonemacro 23:4afef8872432 78 0x00000000,
phonemacro 23:4afef8872432 79 0x00000000,
phonemacro 23:4afef8872432 80 0x00000000,
phonemacro 23:4afef8872432 81 0x00000000
phonemacro 15:2616528b4007 82 };
phonemacro 20:39857996a7c7 83 uint32_t expectedResponse_A_romID[RESPONSE_LEN32] = { // 160 bit, no ROM
phonemacro 20:39857996a7c7 84 0xf987d79a,
phonemacro 20:39857996a7c7 85 0xeb778ec7,
phonemacro 20:39857996a7c7 86 0x33861bc8,
phonemacro 20:39857996a7c7 87 0x745d4082,
phonemacro 20:39857996a7c7 88 0x921e02fe,
phonemacro 20:39857996a7c7 89 0x727d93c8,
phonemacro 20:39857996a7c7 90 0x218e53ee,
phonemacro 20:39857996a7c7 91 0x904e0c6b
phonemacro 20:39857996a7c7 92 };
phonemacro 23:4afef8872432 93 uint32_t challenge_1[CHALLENGE32_LEN] = { // 160 bit
phonemacro 23:4afef8872432 94 0x5e813524,
phonemacro 23:4afef8872432 95 0x5663d609,
phonemacro 23:4afef8872432 96 0x998d7b0d,
phonemacro 23:4afef8872432 97 0x52128465,
phonemacro 23:4afef8872432 98 0xcd0de301
phonemacro 4:46919eec2c28 99 };
phonemacro 20:39857996a7c7 100 uint32_t expectedResponse_1_romID[RESPONSE_LEN32] = { // 160 bit
phonemacro 20:39857996a7c7 101 0xfe3f805f,
phonemacro 20:39857996a7c7 102 0x0af066bc,
phonemacro 20:39857996a7c7 103 0xda0f55e2,
phonemacro 20:39857996a7c7 104 0x0020ac16,
phonemacro 20:39857996a7c7 105 0x13fa3406,
phonemacro 20:39857996a7c7 106 0x59d0e5ae,
phonemacro 20:39857996a7c7 107 0x79def309,
phonemacro 20:39857996a7c7 108 0x685981a3
phonemacro 20:39857996a7c7 109 };
phonemacro 4:46919eec2c28 110
phonemacro 23:4afef8872432 111 uint32_t challenge_2[CHALLENGE32_LEN] = { // 160 bit, no ROM
phonemacro 23:4afef8872432 112 0xd2aa84c5,
phonemacro 23:4afef8872432 113 0x7277f7e5,
phonemacro 23:4afef8872432 114 0xdb8fd612,
phonemacro 23:4afef8872432 115 0x96ce69f2,
phonemacro 23:4afef8872432 116 0x4ec57ae8
phonemacro 13:97854f761347 117 };
phonemacro 14:1939758a03cf 118
phonemacro 17:e260080d3468 119 uint32_t expectedResponse_2_noRomID[RESPONSE_LEN32] = { // 160 bit, no ROM
phonemacro 17:e260080d3468 120 0x1a7135a2,
phonemacro 17:e260080d3468 121 0x51b99ca8,
phonemacro 17:e260080d3468 122 0xbdd9245e,
phonemacro 17:e260080d3468 123 0xb9c8e758,
phonemacro 17:e260080d3468 124 0x770ec1c4,
phonemacro 17:e260080d3468 125 0x474ffcc7,
phonemacro 17:e260080d3468 126 0x828afe4d,
phonemacro 17:e260080d3468 127 0x9cb3de6d
phonemacro 17:e260080d3468 128 };
phonemacro 23:4afef8872432 129 uint32_t challenge_3[CHALLENGE32_LEN] = { // 160 bit
phonemacro 23:4afef8872432 130 0x21202280,
phonemacro 23:4afef8872432 131 0xcc9d45aa,
phonemacro 23:4afef8872432 132 0xb8133e96,
phonemacro 23:4afef8872432 133 0xd653380d,
phonemacro 23:4afef8872432 134 0x2ad5dd6b
phonemacro 20:39857996a7c7 135 };
phonemacro 20:39857996a7c7 136 uint32_t expectedResponse_3_romID[RESPONSE_LEN32] = { // 160 bit
phonemacro 20:39857996a7c7 137 0xe7750b8d,
phonemacro 20:39857996a7c7 138 0xe29d9279,
phonemacro 20:39857996a7c7 139 0x7cdc7053,
phonemacro 20:39857996a7c7 140 0xa9f92519,
phonemacro 20:39857996a7c7 141 0xa1e59d93,
phonemacro 20:39857996a7c7 142 0x19cd930d,
phonemacro 20:39857996a7c7 143 0xfb0fc974,
phonemacro 20:39857996a7c7 144 0x2da0781e
phonemacro 20:39857996a7c7 145 };
phonemacro 23:4afef8872432 146 uint32_t challenge_4[CHALLENGE32_LEN] = { // 160 bit, no ROM
phonemacro 22:1be3a5f0223e 147 0x4c3c0aca,
phonemacro 22:1be3a5f0223e 148 0x618abdf2,
phonemacro 22:1be3a5f0223e 149 0x34d8b341,
phonemacro 22:1be3a5f0223e 150 0x1289f378,
phonemacro 22:1be3a5f0223e 151 0x65b60deb
phonemacro 20:39857996a7c7 152 };
phonemacro 20:39857996a7c7 153 uint32_t expectedResponse_4_noRomID[RESPONSE_LEN32] = { // 160 bit, no ROM
phonemacro 20:39857996a7c7 154 0xae740f91,
phonemacro 20:39857996a7c7 155 0x8d6f2d58,
phonemacro 20:39857996a7c7 156 0x7486b0ba,
phonemacro 20:39857996a7c7 157 0xe9a84580,
phonemacro 20:39857996a7c7 158 0xfe9ce593,
phonemacro 20:39857996a7c7 159 0x58d66c7d,
phonemacro 20:39857996a7c7 160 0xc993d165,
phonemacro 20:39857996a7c7 161 0xe5ae5983
phonemacro 20:39857996a7c7 162 };
phonemacro 22:1be3a5f0223e 163
phonemacro 16:d0f8cf9f160d 164 uint32_t chip_response[RESPONSE_LEN32];
phonemacro 16:d0f8cf9f160d 165 bool valid = 0;
phonemacro 16:d0f8cf9f160d 166
phonemacro 21:0a4f90fb3451 167 rLED = LED_OFF;
phonemacro 0:9e5a4f845510 168 gLED = LED_ON;
phonemacro 0:9e5a4f845510 169 bLED = LED_OFF;
phonemacro 0:9e5a4f845510 170
phonemacro 1:854f8a89a527 171 printf("\r\n\rmax86140 authenication software\r\n");
phonemacro 11:ec7998257ec2 172 MAX8614X m(spi,cs,interrupt_pin);
phonemacro 18:a0356b3e6ed9 173 m.init();
phonemacro 3:2272f89aad7e 174
phonemacro 17:e260080d3468 175 //● Compare MAC from MAX86140 wth Host's precalculated MAC.
phonemacro 23:4afef8872432 176 executeSha256(m, challenge_A, CHALLENGE32_LEN, 1, chip_response);
phonemacro 20:39857996a7c7 177 //● Check PASS or FAIL.
phonemacro 20:39857996a7c7 178 valid = isTheChipAuthenicated(chip_response, expectedResponse_A_romID);
phonemacro 20:39857996a7c7 179 if (valid)
phonemacro 20:39857996a7c7 180 printf("\r\n Challenge A passed\r\n\r\n");
phonemacro 20:39857996a7c7 181 else
phonemacro 20:39857996a7c7 182 printf("\r\n Challenge A failed\r\n\r\n");
phonemacro 17:e260080d3468 183
phonemacro 20:39857996a7c7 184 //● Compare MAC from MAX86140 wth Host's precalculated MAC.
phonemacro 23:4afef8872432 185 executeSha256(m, challenge_1, CHALLENGE32_LEN, 1, chip_response);
phonemacro 17:e260080d3468 186 //● Check PASS or FAIL.
phonemacro 16:d0f8cf9f160d 187 valid = isTheChipAuthenicated(chip_response, expectedResponse_1_romID);
phonemacro 16:d0f8cf9f160d 188 if (valid)
phonemacro 16:d0f8cf9f160d 189 printf("\r\n Challenge 1 passed\r\n\r\n");
phonemacro 16:d0f8cf9f160d 190 else
phonemacro 16:d0f8cf9f160d 191 printf("\r\n Challenge 1 failed\r\n\r\n");
phonemacro 17:e260080d3468 192
phonemacro 23:4afef8872432 193 executeSha256(m, challenge_2, CHALLENGE32_LEN, 0, chip_response);
phonemacro 17:e260080d3468 194 valid = isTheChipAuthenicated(chip_response, expectedResponse_2_noRomID);
phonemacro 17:e260080d3468 195 if (valid)
phonemacro 17:e260080d3468 196 printf("\r\n Challenge 2 passed\r\n\r\n");
phonemacro 17:e260080d3468 197 else
phonemacro 17:e260080d3468 198 printf("\r\n Challenge 2 failed\r\n\r\n");
phonemacro 20:39857996a7c7 199
phonemacro 23:4afef8872432 200 executeSha256(m, challenge_3, CHALLENGE32_LEN, 1, chip_response);
phonemacro 20:39857996a7c7 201 valid = isTheChipAuthenicated(chip_response, expectedResponse_3_romID);
phonemacro 20:39857996a7c7 202 if (valid)
phonemacro 20:39857996a7c7 203 printf("\r\n Challenge 3 passed\r\n\r\n");
phonemacro 20:39857996a7c7 204 else
phonemacro 20:39857996a7c7 205 printf("\r\n Challenge 3 failed\r\n\r\n");
phonemacro 20:39857996a7c7 206
phonemacro 23:4afef8872432 207 executeSha256(m, challenge_4, CHALLENGE32_LEN, 0, chip_response);
phonemacro 20:39857996a7c7 208 valid = isTheChipAuthenicated(chip_response, expectedResponse_4_noRomID);
phonemacro 20:39857996a7c7 209 if (valid)
phonemacro 20:39857996a7c7 210 printf("\r\n Challenge 4 passed\r\n\r\n");
phonemacro 20:39857996a7c7 211 else
phonemacro 20:39857996a7c7 212 printf("\r\n Challenge 4 failed\r\n\r\n");
phonemacro 23:4afef8872432 213
phonemacro 17:e260080d3468 214 //● Disable SHA_EN bit ( Write 0 to SHA_EN bit).
phonemacro 18:a0356b3e6ed9 215 m.writeRegister(MAX8614X::MAX8614X_SHA_CFG_REG, 0);
phonemacro 0:9e5a4f845510 216 while(1) {
phonemacro 1:854f8a89a527 217 gLED = !gLED;
phonemacro 0:9e5a4f845510 218 wait(1.0);
phonemacro 0:9e5a4f845510 219 }
phonemacro 0:9e5a4f845510 220 }
phonemacro 0:9e5a4f845510 221
phonemacro 16:d0f8cf9f160d 222 bool isTheChipAuthenicated(uint32_t *expectedResponse, uint32_t * chipResponse)
phonemacro 16:d0f8cf9f160d 223 {
phonemacro 16:d0f8cf9f160d 224 int i;
phonemacro 16:d0f8cf9f160d 225 for (i = 0; i < RESPONSE_LEN32; i++) {
phonemacro 16:d0f8cf9f160d 226 if (expectedResponse[i] != chipResponse[i])
phonemacro 16:d0f8cf9f160d 227 return 0;
phonemacro 16:d0f8cf9f160d 228 }
phonemacro 16:d0f8cf9f160d 229 return 1;
phonemacro 16:d0f8cf9f160d 230 }
phonemacro 16:d0f8cf9f160d 231
phonemacro 7:5b7f7ec9efe0 232 void transformData(uint8_t *inData, uint8_t *outData, unsigned int challengeLen)
phonemacro 7:5b7f7ec9efe0 233 {
phonemacro 7:5b7f7ec9efe0 234 int i, j, k;
phonemacro 7:5b7f7ec9efe0 235 k = 0;
phonemacro 7:5b7f7ec9efe0 236 for (i = 0; i < (challengeLen/4); i++) {
phonemacro 7:5b7f7ec9efe0 237 for (j = 3; j >= 0; j--) {
phonemacro 7:5b7f7ec9efe0 238 outData[j+(i*4)] = inData[k];
phonemacro 7:5b7f7ec9efe0 239 k++;
phonemacro 7:5b7f7ec9efe0 240 }
phonemacro 7:5b7f7ec9efe0 241 }
phonemacro 7:5b7f7ec9efe0 242 }
phonemacro 22:1be3a5f0223e 243 void executeSha256(MAX8614X &m, uint32_t *challenge, unsigned int challengeLen, bool romID, uint32_t *response)
phonemacro 7:5b7f7ec9efe0 244 {
phonemacro 10:0b940aff3bb8 245 int i, j, k;
phonemacro 7:5b7f7ec9efe0 246 uint8_t macData[256];
phonemacro 7:5b7f7ec9efe0 247 uint8_t xData[256];
phonemacro 10:0b940aff3bb8 248 uint32_t x32Data[64];
phonemacro 7:5b7f7ec9efe0 249 uint32_t tmpData;
phonemacro 10:0b940aff3bb8 250 const unsigned int responseLen32 = 8;
phonemacro 7:5b7f7ec9efe0 251 uint8_t data[5];
phonemacro 11:ec7998257ec2 252
phonemacro 18:a0356b3e6ed9 253 //● Enable SHA_DONE Interrupt
phonemacro 7:5b7f7ec9efe0 254 m.writeRegister(MAX8614X::MAX8614X_INT_ENABLE2_REG, MAX8614X::MAX8614X_IE_SHA_DONE_EN);
phonemacro 7:5b7f7ec9efe0 255
phonemacro 7:5b7f7ec9efe0 256 m.writeRegister(MAX8614X::MAX8614X_INT_ENABLE1_REG, 0); // Disable all other interrupts
phonemacro 7:5b7f7ec9efe0 257
phonemacro 13:97854f761347 258 //- Enable SHA_EN bit.
phonemacro 7:5b7f7ec9efe0 259 m.writeRegister(MAX8614X::MAX8614X_SHA_CFG_REG,MAX8614X::MAX8614X_SHACFG_SHA_EN);
phonemacro 7:5b7f7ec9efe0 260
phonemacro 18:a0356b3e6ed9 261 //● Write 160-bit random challenge value to RAM using registers MEM_IDX and MEM_DATA.
phonemacro 7:5b7f7ec9efe0 262 // Enable Memory Write, Select Bank 0, address 0x00 to 0xFF
phonemacro 7:5b7f7ec9efe0 263 m.writeRegister(MAX8614X::MAX8614X_MEMORY_CONTROL_REG, MAX8614X::MAX8614X_MEMCNTRL_WR_EN_MASK | MAX8614X::MAX8614X_MEMCNTRL_BANK0_MASK);
phonemacro 7:5b7f7ec9efe0 264
phonemacro 22:1be3a5f0223e 265 for (i = 0; i < 5; i++) {
phonemacro 22:1be3a5f0223e 266 x32Data[i] = challenge[i];
phonemacro 22:1be3a5f0223e 267 for (j = 0; j <= 3; j++) {
phonemacro 22:1be3a5f0223e 268 xData[(i*4)+j] = x32Data[i] & 0xFF;
phonemacro 22:1be3a5f0223e 269 x32Data[i] = x32Data[i] >> 8;
phonemacro 22:1be3a5f0223e 270 k++;
phonemacro 22:1be3a5f0223e 271 }
phonemacro 22:1be3a5f0223e 272 }
phonemacro 22:1be3a5f0223e 273
phonemacro 23:4afef8872432 274 #ifdef DEBUG_ON
phonemacro 7:5b7f7ec9efe0 275 printf("\r\n Raw Input Data\r\n\r\n");
phonemacro 7:5b7f7ec9efe0 276 for (i = 0; i < challengeLen; i++) {
phonemacro 23:4afef8872432 277 printf("%08x\r\n", challenge[i]);
phonemacro 7:5b7f7ec9efe0 278 }
phonemacro 7:5b7f7ec9efe0 279 printf("\r\n");
phonemacro 10:0b940aff3bb8 280 #endif
phonemacro 10:0b940aff3bb8 281
phonemacro 22:1be3a5f0223e 282 //transformData(challenge, xData, challengeLen);
phonemacro 7:5b7f7ec9efe0 283
phonemacro 21:0a4f90fb3451 284 #ifdef DEBUG_ON
phonemacro 7:5b7f7ec9efe0 285 printf("\r\n Transformed Input Data\r\n\r\n");
phonemacro 23:4afef8872432 286 for (i = 0; i < challengeLen*4; i++) {
phonemacro 7:5b7f7ec9efe0 287 if (!(i % 4))
phonemacro 7:5b7f7ec9efe0 288 printf("\r\n ");
phonemacro 7:5b7f7ec9efe0 289 printf("%02x", xData[i]);
phonemacro 7:5b7f7ec9efe0 290 }
phonemacro 7:5b7f7ec9efe0 291 printf("\r\n ");
phonemacro 21:0a4f90fb3451 292 #endif
phonemacro 7:5b7f7ec9efe0 293
phonemacro 23:4afef8872432 294 for (i = 0; i < (challengeLen*4); i++) {
phonemacro 7:5b7f7ec9efe0 295 m.writeRegister(MAX8614X::MAX8614X_MEMORY_INDEX_REG, i);
phonemacro 7:5b7f7ec9efe0 296 m.writeRegister(MAX8614X::MAX8614X_MEMORY_DATA_REG, xData[i]);
phonemacro 10:0b940aff3bb8 297 //}
phonemacro 10:0b940aff3bb8 298 }
phonemacro 21:0a4f90fb3451 299
phonemacro 7:5b7f7ec9efe0 300 // The message block consists of a 160-bit secret, a 160-bit challenge and 192 bits of constant data. Optionally, the 64-bit
phonemacro 7:5b7f7ec9efe0 301 // 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
phonemacro 7:5b7f7ec9efe0 302 // bits of ROM ID are programmable–8 bits each in metal and 8 bits each in OTP bits
phonemacro 21:0a4f90fb3451 303 //● Write command, with ROM ID (0x35) or without ROM ID (0x36), to SHA_CMD register
phonemacro 21:0a4f90fb3451 304 if (romID) {
phonemacro 12:fc888315e5fd 305 m.writeRegister(MAX8614X::MAX8614X_SHA_CMD_REG, MAX8614X::MAX8614X_SHACMD_MAC_ROM_ID);
phonemacro 21:0a4f90fb3451 306 }
phonemacro 12:fc888315e5fd 307 else
phonemacro 21:0a4f90fb3451 308 {
phonemacro 21:0a4f90fb3451 309 m.writeRegister(MAX8614X::MAX8614X_SHA_CMD_REG,MAX8614X::MAX8614X_SHACMD_MAC_NO_ROM_ID);
phonemacro 21:0a4f90fb3451 310 }
phonemacro 7:5b7f7ec9efe0 311
phonemacro 10:0b940aff3bb8 312 m.readRegister(MAX8614X::MAX8614X_SHA_CMD_REG, data, 1);
phonemacro 7:5b7f7ec9efe0 313 //● Write 1 to SHA_START and 1 to SHA_EN bit.
phonemacro 7:5b7f7ec9efe0 314 m.writeRegister(MAX8614X::MAX8614X_SHA_CFG_REG,MAX8614X::MAX8614X_SHACFG_SHA_EN | MAX8614X::MAX8614X_SHACFG_SHA_START);
phonemacro 7:5b7f7ec9efe0 315
phonemacro 18:a0356b3e6ed9 316 //● Wait for SHA_DONE.
phonemacro 7:5b7f7ec9efe0 317 data[0] = 0;
phonemacro 17:e260080d3468 318 k = 0;
phonemacro 17:e260080d3468 319 while(!data[0] && k < 100) {
phonemacro 7:5b7f7ec9efe0 320 m.readRegister(MAX8614X::MAX8614X_INT_STATUS2_REG, data, 1);
phonemacro 17:e260080d3468 321 k++;
phonemacro 7:5b7f7ec9efe0 322 }
phonemacro 7:5b7f7ec9efe0 323 // ● Read 256 MAC value from RAM using registers MEM_IDX and MEM_DATA.
phonemacro 21:0a4f90fb3451 324 #ifdef DEBUG_ON
phonemacro 10:0b940aff3bb8 325 printf("\r\n Raw Output\r\n\r\n");
phonemacro 21:0a4f90fb3451 326 #endif
phonemacro 7:5b7f7ec9efe0 327 for (i = 64; i < 64+32; i++) {
phonemacro 21:0a4f90fb3451 328 #ifdef DEBUG_ON
phonemacro 7:5b7f7ec9efe0 329 if (!(i % 4))
phonemacro 7:5b7f7ec9efe0 330 printf("\r\n ");
phonemacro 21:0a4f90fb3451 331 #endif
phonemacro 7:5b7f7ec9efe0 332 m.writeRegister(MAX8614X::MAX8614X_MEMORY_INDEX_REG, i);
phonemacro 7:5b7f7ec9efe0 333 m.readRegister(MAX8614X::MAX8614X_MEMORY_DATA_REG, data, 1);
phonemacro 10:0b940aff3bb8 334 xData[i-64] = data[0];
phonemacro 21:0a4f90fb3451 335 #ifdef DEBUG_ON
phonemacro 7:5b7f7ec9efe0 336 printf("%02x", data[0]);
phonemacro 21:0a4f90fb3451 337 #endif
phonemacro 7:5b7f7ec9efe0 338 }
phonemacro 21:0a4f90fb3451 339 #ifdef DEBUG_ON
phonemacro 10:0b940aff3bb8 340 printf("%\r\n");
phonemacro 21:0a4f90fb3451 341 #endif
phonemacro 10:0b940aff3bb8 342
phonemacro 10:0b940aff3bb8 343 transformData(xData,macData, 32);
phonemacro 21:0a4f90fb3451 344 #ifdef DEBUG_ON
phonemacro 10:0b940aff3bb8 345 printf("\r\n Formatted Response\r\n\r\n");
phonemacro 23:4afef8872432 346 for (i = 0; i < responseLen32*4; i++) {
phonemacro 10:0b940aff3bb8 347 if (!(i % 4))
phonemacro 10:0b940aff3bb8 348 printf("\r\n ");
phonemacro 10:0b940aff3bb8 349 printf("%02x", macData[i]);
phonemacro 10:0b940aff3bb8 350 }
phonemacro 10:0b940aff3bb8 351 printf("\r\n\r\n");
phonemacro 10:0b940aff3bb8 352
phonemacro 10:0b940aff3bb8 353 #endif
phonemacro 10:0b940aff3bb8 354 // transform data to Little-Endian
phonemacro 10:0b940aff3bb8 355 k = 0;
phonemacro 10:0b940aff3bb8 356 for (i = 0; i < (responseLen32); i++) {
phonemacro 10:0b940aff3bb8 357 x32Data[i] = 0;
phonemacro 10:0b940aff3bb8 358 for (j = 3; j >= 0; j--) {
phonemacro 10:0b940aff3bb8 359 x32Data[i] = x32Data[i] << 8;
phonemacro 10:0b940aff3bb8 360 x32Data[i] |= macData[k] & 0xFF;
phonemacro 10:0b940aff3bb8 361 k++;
phonemacro 10:0b940aff3bb8 362 }
phonemacro 10:0b940aff3bb8 363 }
phonemacro 10:0b940aff3bb8 364
phonemacro 21:0a4f90fb3451 365 #ifdef DEBUG_ON
phonemacro 10:0b940aff3bb8 366 printf("\r\n Formatted Response\r\n\r\n");
phonemacro 21:0a4f90fb3451 367 #endif
phonemacro 10:0b940aff3bb8 368 for (i = 0; i < responseLen32; i++) {
phonemacro 21:0a4f90fb3451 369 #ifdef DEBUG_ON
phonemacro 10:0b940aff3bb8 370 printf(" %08X \r\n",x32Data[i]);
phonemacro 21:0a4f90fb3451 371 #endif
phonemacro 16:d0f8cf9f160d 372 response[i] = x32Data[i];
phonemacro 10:0b940aff3bb8 373 }
phonemacro 22:1be3a5f0223e 374 }