1 // Copyright (c) 2010, Atmel Corporation. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are met: 6 // * Redistributions of source code must retain the above copyright 7 // notice, this list of conditions and the following disclaimer. 8 // * Redistributions in binary form must reproduce the above copyright 9 // notice, this list of conditions and the following disclaimer in the 10 // documentation and/or other materials provided with the distribution. 11 // * Neither the name of Atmel nor the 12 // names of its contributors may be used to endorse or promote products 13 // derived from this software without specific prior written permission. 14 // 15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 26 #include <ctype.h> 27 #include <stdio.h> 28 #include <string.h> 29 #include "SHA_Comm.h" 30 #include "SHA_CommMarshalling.h" 31 #include "SHA_Status.h" 32 33 34 35 36 // Data definitions 37 uint8_t sendbuf[SENDBUF_SIZE]; 38 uint8_t receicebuf[RECEIVEBUF_SIZE]; 39 SHA_CommParameters commparms; 40 41 42 43 44 SHA_CommParameters* SHAC_GetData(void) { 45 return &commparms; 46 } 47 /** 48 * 49 * \brief Sends an MAC command to the device. 50 * 51 * \param[in] Mode 52 * \param[in] KeyID key id 53 * \param[in] Challenge 54 * \param[out] 32 bytes of response 55 * \return status of the operation 56 */ 57 uint8_t SHAC_Mac(uint8_t Mode, uint16_t KeyID, uint8_t *Challenge) { 58 sendbuf[COUNT_IDX] = MAC_COUNT_SHORT; 59 sendbuf[CMD_ORDINAL_IDX] = MAC; 60 sendbuf[MAC_MODE_IDX] = Mode; 61 memcpy(&sendbuf[MAC_KEYID_IDX], &KeyID, 2); 62 if ((Challenge != NULL) && ((Mode & 0x01) == 0)) 63 { 64 memcpy(&sendbuf[MAC_CHALL_IDX], Challenge, 32); 65 sendbuf[COUNT_IDX] = MAC_COUNT_LARGE; 66 } 67 68 commparms.txBuffer = &sendbuf[0]; 69 commparms.rxBuffer = &receicebuf[0]; 70 commparms.rxSize = 35; 71 commparms.executionDelay = MACDELAY; 72 // Transfer the command to the chip 73 // 74 return SHAC_SendAndReceive(&commparms); 75 76 } 77 78 79 80 /** 81 * 82 * \brief Sends an Read command to the device. 83 * 84 * \param[in] Zone 85 * \param[in] Address 86 * \param[out] 4 or 32 bytes of response 87 * \return status of the operation 88 */ 89 uint8_t SHAC_Read(uint8_t Zone, uint16_t Address) { 90 sendbuf[COUNT_IDX] = READ_COUNT; 91 sendbuf[CMD_ORDINAL_IDX] = READ; 92 sendbuf[READ_ZONE_IDX] = Zone; 93 memcpy(&sendbuf[READ_ADDR_IDX], &Address, 2); 94 95 commparms.txBuffer = &sendbuf[0]; 96 commparms.rxBuffer = &receicebuf[0]; 97 if (Zone & 0x80) // if bit 7 = 1, 32 bytes 98 commparms.rxSize = 35; 99 else 100 commparms.rxSize = 7; 101 // The execution delay will have to increased for clear text & enc data 102 commparms.executionDelay = GENERALCMDDELAY; 103 // Transfer the command to the chip 104 // 105 return SHAC_SendAndReceive(&commparms); 106 107 } 108 109