intiial commit

Dependencies:   MAX8614X USBDevice max32630hsp_test

Committer:
phonemacro
Date:
Fri Aug 17 02:26:39 2018 +0000
Revision:
22:1be3a5f0223e
Parent:
21:0a4f90fb3451
Child:
23:4afef8872432
partial update

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 22:1be3a5f0223e 44 void xxx(uint32_t *inData);
phonemacro 7:5b7f7ec9efe0 45
phonemacro 16:d0f8cf9f160d 46 #define RESPONSE_LEN32 8
phonemacro 0:9e5a4f845510 47
phonemacro 0:9e5a4f845510 48 MAX32630HSP icarus(MAX32630HSP::VIO_1V8);
phonemacro 0:9e5a4f845510 49 // MAX32630FTHR mbed_board(MAX32630FTHR::VIO_1V8);
phonemacro 0:9e5a4f845510 50 SPI spi(P5_1, P5_2, P5_0); /* mosi, miso, sclk */
phonemacro 3:2272f89aad7e 51 DigitalOut cs(P5_3);
phonemacro 18:a0356b3e6ed9 52 PinName interrupt_pin = P3_2;
phonemacro 0:9e5a4f845510 53 // Virtual serial port over USB
phonemacro 0:9e5a4f845510 54 USBSerial microUSB;
phonemacro 0:9e5a4f845510 55
phonemacro 0:9e5a4f845510 56 DigitalOut rLED(LED1);
phonemacro 0:9e5a4f845510 57 DigitalOut gLED(LED2);
phonemacro 0:9e5a4f845510 58 DigitalOut bLED(LED3);
phonemacro 0:9e5a4f845510 59 int main()
phonemacro 0:9e5a4f845510 60 {
phonemacro 21:0a4f90fb3451 61 #define CHALLENGE8_LEN 20
phonemacro 21:0a4f90fb3451 62 uint8_t challenge_A[CHALLENGE8_LEN] = { // 160 bit, no ROM
phonemacro 15:2616528b4007 63 0x00, 0x00, 0x00, 0x01,
phonemacro 15:2616528b4007 64 0x00, 0x00, 0x00, 0x00,
phonemacro 15:2616528b4007 65 0x00, 0x00, 0x00, 0x00,
phonemacro 15:2616528b4007 66 0x00, 0x00, 0x00, 0x00,
phonemacro 15:2616528b4007 67 0x00, 0x00, 0x00, 0x00
phonemacro 15:2616528b4007 68 };
phonemacro 20:39857996a7c7 69 uint32_t expectedResponse_A_romID[RESPONSE_LEN32] = { // 160 bit, no ROM
phonemacro 20:39857996a7c7 70 0xf987d79a,
phonemacro 20:39857996a7c7 71 0xeb778ec7,
phonemacro 20:39857996a7c7 72 0x33861bc8,
phonemacro 20:39857996a7c7 73 0x745d4082,
phonemacro 20:39857996a7c7 74 0x921e02fe,
phonemacro 20:39857996a7c7 75 0x727d93c8,
phonemacro 20:39857996a7c7 76 0x218e53ee,
phonemacro 20:39857996a7c7 77 0x904e0c6b
phonemacro 20:39857996a7c7 78 };
phonemacro 21:0a4f90fb3451 79 uint8_t challenge_1[CHALLENGE8_LEN] = { // 160 bit
phonemacro 10:0b940aff3bb8 80 0x5e, 0x81, 0x35, 0x24,
phonemacro 10:0b940aff3bb8 81 0x56, 0x63, 0xd6, 0x09,
phonemacro 10:0b940aff3bb8 82 0x99, 0x8d, 0x7b, 0x0d,
phonemacro 10:0b940aff3bb8 83 0x52, 0x12, 0x84, 0x65,
phonemacro 10:0b940aff3bb8 84 0xcd, 0x0d, 0xe3, 0x01
phonemacro 4:46919eec2c28 85 };
phonemacro 20:39857996a7c7 86 uint32_t expectedResponse_1_romID[RESPONSE_LEN32] = { // 160 bit
phonemacro 20:39857996a7c7 87 0xfe3f805f,
phonemacro 20:39857996a7c7 88 0x0af066bc,
phonemacro 20:39857996a7c7 89 0xda0f55e2,
phonemacro 20:39857996a7c7 90 0x0020ac16,
phonemacro 20:39857996a7c7 91 0x13fa3406,
phonemacro 20:39857996a7c7 92 0x59d0e5ae,
phonemacro 20:39857996a7c7 93 0x79def309,
phonemacro 20:39857996a7c7 94 0x685981a3
phonemacro 20:39857996a7c7 95 };
phonemacro 4:46919eec2c28 96
phonemacro 21:0a4f90fb3451 97 uint8_t challenge_2[CHALLENGE8_LEN] = { // 160 bit, no ROM
phonemacro 13:97854f761347 98 0xd2, 0xaa, 0x84, 0xc5,
phonemacro 13:97854f761347 99 0x72, 0x77, 0xf7, 0xe5,
phonemacro 13:97854f761347 100 0xdb, 0x8f, 0xd6, 0x12,
phonemacro 13:97854f761347 101 0x96, 0xce, 0x69, 0xf2,
phonemacro 13:97854f761347 102 0x4e, 0xc5, 0x7a, 0xe8
phonemacro 13:97854f761347 103 };
phonemacro 14:1939758a03cf 104
phonemacro 17:e260080d3468 105 uint32_t expectedResponse_2_noRomID[RESPONSE_LEN32] = { // 160 bit, no ROM
phonemacro 17:e260080d3468 106 0x1a7135a2,
phonemacro 17:e260080d3468 107 0x51b99ca8,
phonemacro 17:e260080d3468 108 0xbdd9245e,
phonemacro 17:e260080d3468 109 0xb9c8e758,
phonemacro 17:e260080d3468 110 0x770ec1c4,
phonemacro 17:e260080d3468 111 0x474ffcc7,
phonemacro 17:e260080d3468 112 0x828afe4d,
phonemacro 17:e260080d3468 113 0x9cb3de6d
phonemacro 17:e260080d3468 114 };
phonemacro 21:0a4f90fb3451 115 uint8_t challenge_3[CHALLENGE8_LEN] = { // 160 bit
phonemacro 20:39857996a7c7 116 0x21, 0x20, 0x22, 0x80,
phonemacro 20:39857996a7c7 117 0xcc, 0x9d, 0x45, 0xaa,
phonemacro 20:39857996a7c7 118 0xb8, 0x13, 0x3e, 0x96,
phonemacro 20:39857996a7c7 119 0xd6, 0x53, 0x38, 0x0d,
phonemacro 20:39857996a7c7 120 0x2a, 0xd5, 0xdd, 0x6b
phonemacro 20:39857996a7c7 121 };
phonemacro 20:39857996a7c7 122 uint32_t expectedResponse_3_romID[RESPONSE_LEN32] = { // 160 bit
phonemacro 20:39857996a7c7 123 0xe7750b8d,
phonemacro 20:39857996a7c7 124 0xe29d9279,
phonemacro 20:39857996a7c7 125 0x7cdc7053,
phonemacro 20:39857996a7c7 126 0xa9f92519,
phonemacro 20:39857996a7c7 127 0xa1e59d93,
phonemacro 20:39857996a7c7 128 0x19cd930d,
phonemacro 20:39857996a7c7 129 0xfb0fc974,
phonemacro 20:39857996a7c7 130 0x2da0781e
phonemacro 20:39857996a7c7 131 };
phonemacro 22:1be3a5f0223e 132 uint32_t challenge_4[CHALLENGE8_LEN] = { // 160 bit, no ROM
phonemacro 22:1be3a5f0223e 133 0x4c3c0aca,
phonemacro 22:1be3a5f0223e 134 0x618abdf2,
phonemacro 22:1be3a5f0223e 135 0x34d8b341,
phonemacro 22:1be3a5f0223e 136 0x1289f378,
phonemacro 22:1be3a5f0223e 137 0x65b60deb
phonemacro 20:39857996a7c7 138 };
phonemacro 20:39857996a7c7 139 uint32_t expectedResponse_4_noRomID[RESPONSE_LEN32] = { // 160 bit, no ROM
phonemacro 20:39857996a7c7 140 0xae740f91,
phonemacro 20:39857996a7c7 141 0x8d6f2d58,
phonemacro 20:39857996a7c7 142 0x7486b0ba,
phonemacro 20:39857996a7c7 143 0xe9a84580,
phonemacro 20:39857996a7c7 144 0xfe9ce593,
phonemacro 20:39857996a7c7 145 0x58d66c7d,
phonemacro 20:39857996a7c7 146 0xc993d165,
phonemacro 20:39857996a7c7 147 0xe5ae5983
phonemacro 20:39857996a7c7 148 };
phonemacro 22:1be3a5f0223e 149
phonemacro 22:1be3a5f0223e 150 uint32_t challenge_14[CHALLENGE8_LEN] = { // 160 bit, no ROM
phonemacro 22:1be3a5f0223e 151 0x4c3c0aca,
phonemacro 22:1be3a5f0223e 152 0x618abdf2,
phonemacro 22:1be3a5f0223e 153 0x34d8b341,
phonemacro 22:1be3a5f0223e 154 0x1289f378,
phonemacro 22:1be3a5f0223e 155 0x65b60deb
phonemacro 22:1be3a5f0223e 156 };
phonemacro 16:d0f8cf9f160d 157 uint32_t chip_response[RESPONSE_LEN32];
phonemacro 16:d0f8cf9f160d 158 bool valid = 0;
phonemacro 16:d0f8cf9f160d 159
phonemacro 7:5b7f7ec9efe0 160
phonemacro 21:0a4f90fb3451 161 rLED = LED_OFF;
phonemacro 0:9e5a4f845510 162 gLED = LED_ON;
phonemacro 0:9e5a4f845510 163 bLED = LED_OFF;
phonemacro 0:9e5a4f845510 164
phonemacro 1:854f8a89a527 165 printf("\r\n\rmax86140 authenication software\r\n");
phonemacro 11:ec7998257ec2 166 MAX8614X m(spi,cs,interrupt_pin);
phonemacro 18:a0356b3e6ed9 167 m.init();
phonemacro 3:2272f89aad7e 168
phonemacro 17:e260080d3468 169 //● Compare MAC from MAX86140 wth Host's precalculated MAC.
phonemacro 22:1be3a5f0223e 170 // executeSha256(m, challenge_A, CHALLENGE8_LEN, 1, chip_response);
phonemacro 20:39857996a7c7 171 //● Check PASS or FAIL.
phonemacro 20:39857996a7c7 172 valid = isTheChipAuthenicated(chip_response, expectedResponse_A_romID);
phonemacro 20:39857996a7c7 173 if (valid)
phonemacro 20:39857996a7c7 174 printf("\r\n Challenge A passed\r\n\r\n");
phonemacro 20:39857996a7c7 175 else
phonemacro 20:39857996a7c7 176 printf("\r\n Challenge A failed\r\n\r\n");
phonemacro 17:e260080d3468 177
phonemacro 20:39857996a7c7 178 //● Compare MAC from MAX86140 wth Host's precalculated MAC.
phonemacro 22:1be3a5f0223e 179 // executeSha256(m, challenge_1, CHALLENGE8_LEN, 1, chip_response);
phonemacro 17:e260080d3468 180 //● Check PASS or FAIL.
phonemacro 16:d0f8cf9f160d 181 valid = isTheChipAuthenicated(chip_response, expectedResponse_1_romID);
phonemacro 16:d0f8cf9f160d 182 if (valid)
phonemacro 16:d0f8cf9f160d 183 printf("\r\n Challenge 1 passed\r\n\r\n");
phonemacro 16:d0f8cf9f160d 184 else
phonemacro 16:d0f8cf9f160d 185 printf("\r\n Challenge 1 failed\r\n\r\n");
phonemacro 17:e260080d3468 186
phonemacro 22:1be3a5f0223e 187 // executeSha256(m, challenge_2, CHALLENGE8_LEN, 0, chip_response);
phonemacro 17:e260080d3468 188 valid = isTheChipAuthenicated(chip_response, expectedResponse_2_noRomID);
phonemacro 17:e260080d3468 189 if (valid)
phonemacro 17:e260080d3468 190 printf("\r\n Challenge 2 passed\r\n\r\n");
phonemacro 17:e260080d3468 191 else
phonemacro 17:e260080d3468 192 printf("\r\n Challenge 2 failed\r\n\r\n");
phonemacro 20:39857996a7c7 193
phonemacro 22:1be3a5f0223e 194 // executeSha256(m, challenge_3, CHALLENGE8_LEN, 1, chip_response);
phonemacro 20:39857996a7c7 195 valid = isTheChipAuthenicated(chip_response, expectedResponse_3_romID);
phonemacro 20:39857996a7c7 196 if (valid)
phonemacro 20:39857996a7c7 197 printf("\r\n Challenge 3 passed\r\n\r\n");
phonemacro 20:39857996a7c7 198 else
phonemacro 20:39857996a7c7 199 printf("\r\n Challenge 3 failed\r\n\r\n");
phonemacro 20:39857996a7c7 200
phonemacro 21:0a4f90fb3451 201 executeSha256(m, challenge_4, CHALLENGE8_LEN, 0, chip_response);
phonemacro 20:39857996a7c7 202 valid = isTheChipAuthenicated(chip_response, expectedResponse_4_noRomID);
phonemacro 20:39857996a7c7 203 if (valid)
phonemacro 20:39857996a7c7 204 printf("\r\n Challenge 4 passed\r\n\r\n");
phonemacro 20:39857996a7c7 205 else
phonemacro 20:39857996a7c7 206 printf("\r\n Challenge 4 failed\r\n\r\n");
phonemacro 22:1be3a5f0223e 207 #if 0
phonemacro 22:1be3a5f0223e 208 xxx(challenge_14);
phonemacro 22:1be3a5f0223e 209 executeSha(m, challenge_14, CHALLENGE8_LEN, 0, chip_response);
phonemacro 22:1be3a5f0223e 210 valid = isTheChipAuthenicated(chip_response, expectedResponse_4_noRomID);
phonemacro 22:1be3a5f0223e 211 if (valid)
phonemacro 22:1be3a5f0223e 212 printf("\r\n Challenge 4 passed\r\n\r\n");
phonemacro 22:1be3a5f0223e 213 else
phonemacro 22:1be3a5f0223e 214 printf("\r\n Challenge 4 failed\r\n\r\n");
phonemacro 22:1be3a5f0223e 215 #endif
phonemacro 17:e260080d3468 216 //● Disable SHA_EN bit ( Write 0 to SHA_EN bit).
phonemacro 18:a0356b3e6ed9 217 m.writeRegister(MAX8614X::MAX8614X_SHA_CFG_REG, 0);
phonemacro 0:9e5a4f845510 218 while(1) {
phonemacro 1:854f8a89a527 219 gLED = !gLED;
phonemacro 0:9e5a4f845510 220 wait(1.0);
phonemacro 0:9e5a4f845510 221 }
phonemacro 0:9e5a4f845510 222 }
phonemacro 0:9e5a4f845510 223
phonemacro 16:d0f8cf9f160d 224 bool isTheChipAuthenicated(uint32_t *expectedResponse, uint32_t * chipResponse)
phonemacro 16:d0f8cf9f160d 225 {
phonemacro 16:d0f8cf9f160d 226 int i;
phonemacro 16:d0f8cf9f160d 227 for (i = 0; i < RESPONSE_LEN32; i++) {
phonemacro 16:d0f8cf9f160d 228 if (expectedResponse[i] != chipResponse[i])
phonemacro 16:d0f8cf9f160d 229 return 0;
phonemacro 16:d0f8cf9f160d 230 }
phonemacro 16:d0f8cf9f160d 231 return 1;
phonemacro 16:d0f8cf9f160d 232 }
phonemacro 16:d0f8cf9f160d 233
phonemacro 7:5b7f7ec9efe0 234 void transformData(uint8_t *inData, uint8_t *outData, unsigned int challengeLen)
phonemacro 7:5b7f7ec9efe0 235 {
phonemacro 7:5b7f7ec9efe0 236 int i, j, k;
phonemacro 7:5b7f7ec9efe0 237 k = 0;
phonemacro 7:5b7f7ec9efe0 238 for (i = 0; i < (challengeLen/4); i++) {
phonemacro 7:5b7f7ec9efe0 239 for (j = 3; j >= 0; j--) {
phonemacro 7:5b7f7ec9efe0 240 outData[j+(i*4)] = inData[k];
phonemacro 7:5b7f7ec9efe0 241 k++;
phonemacro 7:5b7f7ec9efe0 242 }
phonemacro 7:5b7f7ec9efe0 243 }
phonemacro 7:5b7f7ec9efe0 244 }
phonemacro 22:1be3a5f0223e 245 void xxx(uint32_t *inData)
phonemacro 22:1be3a5f0223e 246 {
phonemacro 22:1be3a5f0223e 247 int i, j, k;
phonemacro 22:1be3a5f0223e 248 uint8_t macData[256];
phonemacro 22:1be3a5f0223e 249 uint8_t xData[256];
phonemacro 22:1be3a5f0223e 250 uint32_t x32Data[64];
phonemacro 22:1be3a5f0223e 251 k = 0;
phonemacro 22:1be3a5f0223e 252 for (i = 0; i < 5; i++) {
phonemacro 22:1be3a5f0223e 253 printf("%08x\n\r", inData[i]);
phonemacro 22:1be3a5f0223e 254 x32Data[i] = inData[i];
phonemacro 22:1be3a5f0223e 255 for (j = 0; j <= 3; j++) {
phonemacro 22:1be3a5f0223e 256 xData[(i*4)+j] = x32Data[i] & 0xFF;
phonemacro 22:1be3a5f0223e 257 // printf("%02x %d \n\r", xData[k], k);
phonemacro 22:1be3a5f0223e 258 x32Data[i] = x32Data[i] >> 8;
phonemacro 22:1be3a5f0223e 259 //printf("%08x\n\r", x32Data[i]);
phonemacro 22:1be3a5f0223e 260 k++;
phonemacro 22:1be3a5f0223e 261 }
phonemacro 22:1be3a5f0223e 262 }
phonemacro 22:1be3a5f0223e 263 for (i = 0; i < 20; i++) {
phonemacro 22:1be3a5f0223e 264 if (!(i % 4))
phonemacro 22:1be3a5f0223e 265 printf("\r\n ");
phonemacro 22:1be3a5f0223e 266 printf("%02x", xData[i]);
phonemacro 22:1be3a5f0223e 267 }
phonemacro 22:1be3a5f0223e 268 printf("\r\n ");
phonemacro 22:1be3a5f0223e 269 }
phonemacro 22:1be3a5f0223e 270 void executeSha256(MAX8614X &m, uint32_t *challenge, unsigned int challengeLen, bool romID, uint32_t *response)
phonemacro 7:5b7f7ec9efe0 271 {
phonemacro 10:0b940aff3bb8 272 int i, j, k;
phonemacro 7:5b7f7ec9efe0 273 uint8_t macData[256];
phonemacro 7:5b7f7ec9efe0 274 uint8_t xData[256];
phonemacro 10:0b940aff3bb8 275 uint32_t x32Data[64];
phonemacro 7:5b7f7ec9efe0 276 uint32_t tmpData;
phonemacro 10:0b940aff3bb8 277 const unsigned int responseLen32 = 8;
phonemacro 7:5b7f7ec9efe0 278 uint8_t data[5];
phonemacro 11:ec7998257ec2 279
phonemacro 18:a0356b3e6ed9 280 //● Enable SHA_DONE Interrupt
phonemacro 7:5b7f7ec9efe0 281 m.writeRegister(MAX8614X::MAX8614X_INT_ENABLE2_REG, MAX8614X::MAX8614X_IE_SHA_DONE_EN);
phonemacro 7:5b7f7ec9efe0 282
phonemacro 7:5b7f7ec9efe0 283 m.writeRegister(MAX8614X::MAX8614X_INT_ENABLE1_REG, 0); // Disable all other interrupts
phonemacro 7:5b7f7ec9efe0 284
phonemacro 13:97854f761347 285 //- Enable SHA_EN bit.
phonemacro 7:5b7f7ec9efe0 286 m.writeRegister(MAX8614X::MAX8614X_SHA_CFG_REG,MAX8614X::MAX8614X_SHACFG_SHA_EN);
phonemacro 7:5b7f7ec9efe0 287
phonemacro 18:a0356b3e6ed9 288 //● Write 160-bit random challenge value to RAM using registers MEM_IDX and MEM_DATA.
phonemacro 7:5b7f7ec9efe0 289 // Enable Memory Write, Select Bank 0, address 0x00 to 0xFF
phonemacro 7:5b7f7ec9efe0 290 m.writeRegister(MAX8614X::MAX8614X_MEMORY_CONTROL_REG, MAX8614X::MAX8614X_MEMCNTRL_WR_EN_MASK | MAX8614X::MAX8614X_MEMCNTRL_BANK0_MASK);
phonemacro 7:5b7f7ec9efe0 291
phonemacro 22:1be3a5f0223e 292 for (i = 0; i < 5; i++) {
phonemacro 22:1be3a5f0223e 293 printf("%08x\n\r", challenge[i]);
phonemacro 22:1be3a5f0223e 294 x32Data[i] = challenge[i];
phonemacro 22:1be3a5f0223e 295 for (j = 0; j <= 3; j++) {
phonemacro 22:1be3a5f0223e 296 xData[(i*4)+j] = x32Data[i] & 0xFF;
phonemacro 22:1be3a5f0223e 297 // printf("%02x %d \n\r", xData[k], k);
phonemacro 22:1be3a5f0223e 298 x32Data[i] = x32Data[i] >> 8;
phonemacro 22:1be3a5f0223e 299 //printf("%08x\n\r", x32Data[i]);
phonemacro 22:1be3a5f0223e 300 k++;
phonemacro 22:1be3a5f0223e 301 }
phonemacro 22:1be3a5f0223e 302 }
phonemacro 22:1be3a5f0223e 303
phonemacro 22:1be3a5f0223e 304 #ifdef 0
phonemacro 7:5b7f7ec9efe0 305 printf("\r\n Raw Input Data\r\n\r\n");
phonemacro 7:5b7f7ec9efe0 306 for (i = 0; i < challengeLen; i++) {
phonemacro 10:0b940aff3bb8 307 if (!(i % 4))
phonemacro 10:0b940aff3bb8 308 printf("\r\n ");
phonemacro 10:0b940aff3bb8 309 printf("%02x", challenge[i]);
phonemacro 7:5b7f7ec9efe0 310 }
phonemacro 7:5b7f7ec9efe0 311 printf("\r\n");
phonemacro 10:0b940aff3bb8 312 #endif
phonemacro 10:0b940aff3bb8 313
phonemacro 22:1be3a5f0223e 314 //transformData(challenge, xData, challengeLen);
phonemacro 7:5b7f7ec9efe0 315
phonemacro 21:0a4f90fb3451 316 #ifdef DEBUG_ON
phonemacro 7:5b7f7ec9efe0 317 printf("\r\n Transformed Input Data\r\n\r\n");
phonemacro 10:0b940aff3bb8 318 for (i = 0; i < challengeLen; i++) {
phonemacro 7:5b7f7ec9efe0 319 if (!(i % 4))
phonemacro 7:5b7f7ec9efe0 320 printf("\r\n ");
phonemacro 7:5b7f7ec9efe0 321 printf("%02x", xData[i]);
phonemacro 7:5b7f7ec9efe0 322 }
phonemacro 7:5b7f7ec9efe0 323 printf("\r\n ");
phonemacro 21:0a4f90fb3451 324 #endif
phonemacro 7:5b7f7ec9efe0 325
phonemacro 10:0b940aff3bb8 326 for (i = 0; i < (challengeLen); i++) {
phonemacro 7:5b7f7ec9efe0 327 m.writeRegister(MAX8614X::MAX8614X_MEMORY_INDEX_REG, i);
phonemacro 7:5b7f7ec9efe0 328 m.writeRegister(MAX8614X::MAX8614X_MEMORY_DATA_REG, xData[i]);
phonemacro 10:0b940aff3bb8 329 //}
phonemacro 10:0b940aff3bb8 330 }
phonemacro 21:0a4f90fb3451 331
phonemacro 7:5b7f7ec9efe0 332 // 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 333 // 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 334 // bits of ROM ID are programmable–8 bits each in metal and 8 bits each in OTP bits
phonemacro 21:0a4f90fb3451 335 //● Write command, with ROM ID (0x35) or without ROM ID (0x36), to SHA_CMD register
phonemacro 21:0a4f90fb3451 336 if (romID) {
phonemacro 12:fc888315e5fd 337 m.writeRegister(MAX8614X::MAX8614X_SHA_CMD_REG, MAX8614X::MAX8614X_SHACMD_MAC_ROM_ID);
phonemacro 21:0a4f90fb3451 338 }
phonemacro 12:fc888315e5fd 339 else
phonemacro 21:0a4f90fb3451 340 {
phonemacro 21:0a4f90fb3451 341 m.writeRegister(MAX8614X::MAX8614X_SHA_CMD_REG,MAX8614X::MAX8614X_SHACMD_MAC_NO_ROM_ID);
phonemacro 21:0a4f90fb3451 342 }
phonemacro 7:5b7f7ec9efe0 343
phonemacro 10:0b940aff3bb8 344 m.readRegister(MAX8614X::MAX8614X_SHA_CMD_REG, data, 1);
phonemacro 7:5b7f7ec9efe0 345 //● Write 1 to SHA_START and 1 to SHA_EN bit.
phonemacro 7:5b7f7ec9efe0 346 m.writeRegister(MAX8614X::MAX8614X_SHA_CFG_REG,MAX8614X::MAX8614X_SHACFG_SHA_EN | MAX8614X::MAX8614X_SHACFG_SHA_START);
phonemacro 7:5b7f7ec9efe0 347
phonemacro 18:a0356b3e6ed9 348 //● Wait for SHA_DONE.
phonemacro 7:5b7f7ec9efe0 349 data[0] = 0;
phonemacro 17:e260080d3468 350 k = 0;
phonemacro 17:e260080d3468 351 while(!data[0] && k < 100) {
phonemacro 7:5b7f7ec9efe0 352 m.readRegister(MAX8614X::MAX8614X_INT_STATUS2_REG, data, 1);
phonemacro 17:e260080d3468 353 k++;
phonemacro 7:5b7f7ec9efe0 354 }
phonemacro 7:5b7f7ec9efe0 355 // ● Read 256 MAC value from RAM using registers MEM_IDX and MEM_DATA.
phonemacro 21:0a4f90fb3451 356 #ifdef DEBUG_ON
phonemacro 10:0b940aff3bb8 357 printf("\r\n Raw Output\r\n\r\n");
phonemacro 21:0a4f90fb3451 358 #endif
phonemacro 7:5b7f7ec9efe0 359 for (i = 64; i < 64+32; i++) {
phonemacro 21:0a4f90fb3451 360 #ifdef DEBUG_ON
phonemacro 7:5b7f7ec9efe0 361 if (!(i % 4))
phonemacro 7:5b7f7ec9efe0 362 printf("\r\n ");
phonemacro 21:0a4f90fb3451 363 #endif
phonemacro 7:5b7f7ec9efe0 364 m.writeRegister(MAX8614X::MAX8614X_MEMORY_INDEX_REG, i);
phonemacro 7:5b7f7ec9efe0 365 m.readRegister(MAX8614X::MAX8614X_MEMORY_DATA_REG, data, 1);
phonemacro 10:0b940aff3bb8 366 xData[i-64] = data[0];
phonemacro 21:0a4f90fb3451 367 #ifdef DEBUG_ON
phonemacro 7:5b7f7ec9efe0 368 printf("%02x", data[0]);
phonemacro 21:0a4f90fb3451 369 #endif
phonemacro 7:5b7f7ec9efe0 370 }
phonemacro 21:0a4f90fb3451 371 #ifdef DEBUG_ON
phonemacro 10:0b940aff3bb8 372 printf("%\r\n");
phonemacro 21:0a4f90fb3451 373 #endif
phonemacro 10:0b940aff3bb8 374
phonemacro 10:0b940aff3bb8 375 transformData(xData,macData, 32);
phonemacro 21:0a4f90fb3451 376 #ifdef DEBUG_ON
phonemacro 10:0b940aff3bb8 377 printf("\r\n Formatted Response\r\n\r\n");
phonemacro 10:0b940aff3bb8 378 for (i = 0; i < 32; i++) {
phonemacro 10:0b940aff3bb8 379 if (!(i % 4))
phonemacro 10:0b940aff3bb8 380 printf("\r\n ");
phonemacro 10:0b940aff3bb8 381 printf("%02x", macData[i]);
phonemacro 10:0b940aff3bb8 382 }
phonemacro 10:0b940aff3bb8 383 printf("\r\n\r\n");
phonemacro 10:0b940aff3bb8 384
phonemacro 10:0b940aff3bb8 385 #endif
phonemacro 10:0b940aff3bb8 386 // transform data to Little-Endian
phonemacro 10:0b940aff3bb8 387 k = 0;
phonemacro 10:0b940aff3bb8 388 for (i = 0; i < (responseLen32); i++) {
phonemacro 10:0b940aff3bb8 389 x32Data[i] = 0;
phonemacro 10:0b940aff3bb8 390 for (j = 3; j >= 0; j--) {
phonemacro 10:0b940aff3bb8 391 x32Data[i] = x32Data[i] << 8;
phonemacro 10:0b940aff3bb8 392 x32Data[i] |= macData[k] & 0xFF;
phonemacro 10:0b940aff3bb8 393 k++;
phonemacro 10:0b940aff3bb8 394 }
phonemacro 10:0b940aff3bb8 395 }
phonemacro 10:0b940aff3bb8 396
phonemacro 21:0a4f90fb3451 397 #ifdef DEBUG_ON
phonemacro 10:0b940aff3bb8 398 printf("\r\n Formatted Response\r\n\r\n");
phonemacro 21:0a4f90fb3451 399 #endif
phonemacro 10:0b940aff3bb8 400 for (i = 0; i < responseLen32; i++) {
phonemacro 21:0a4f90fb3451 401 #ifdef DEBUG_ON
phonemacro 10:0b940aff3bb8 402 printf(" %08X \r\n",x32Data[i]);
phonemacro 21:0a4f90fb3451 403 #endif
phonemacro 16:d0f8cf9f160d 404 response[i] = x32Data[i];
phonemacro 10:0b940aff3bb8 405 }
phonemacro 22:1be3a5f0223e 406 }