ota_signature.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /******************************************************************************
  2. Filename: ota_signature.c
  3. Revised: $Date: 2011-07-15 18:31:00 -0700 (Fri, 15 Jul 2011) $
  4. Revision: $Revision: 26808 $
  5. Description: This file contains code to calculate and verify OTA
  6. signatures based on teh MMO AES Hash function.
  7. Copyright 2010-2011 Texas Instruments Incorporated. All rights reserved.
  8. IMPORTANT: Your use of this Software is limited to those specific rights
  9. granted under the terms of a software license agreement between the user
  10. who downloaded the software, his/her employer (which must be your employer)
  11. and Texas Instruments Incorporated (the "License"). You may not use this
  12. Software unless you agree to abide by the terms of the License. The License
  13. limits your use, and you acknowledge, that the Software may not be modified,
  14. copied or distributed unless embedded on a Texas Instruments microcontroller
  15. or used solely and exclusively in conjunction with a Texas Instruments radio
  16. frequency transceiver, which is integrated into your product. Other than for
  17. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  18. works of, modify, distribute, perform, display or sell this Software and/or
  19. its documentation for any purpose.
  20. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  21. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  22. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  23. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  24. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  25. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  26. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  27. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  28. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  29. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  30. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  31. Should you have any questions regarding your right to use this Software,
  32. contact Texas Instruments Incorporated at www.TI.com.
  33. ******************************************************************************/
  34. #include "hal_types.h"
  35. #include "ota_common.h"
  36. #include "ota_signature.h"
  37. #include "eccapi.h"
  38. #ifdef _WIN32
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <stdlib.h>
  42. #define osal_memset memset
  43. #define osal_memcpy memcpy
  44. #define osal_strlen strlen
  45. #include "aes.h"
  46. #define ssp_HW_KeyInit(a)
  47. #else
  48. #include "osal.h"
  49. #include "hal_aes.h"
  50. #include "ssp_hash.h"
  51. #include "ZGlobals.h"
  52. #include "zcl.h"
  53. #include "OSAL_Nv.h"
  54. #endif
  55. static void OTA_AesHashBlock(uint8 *pHash, uint8 *pData);
  56. static void OTA_XorBlock(uint8 *pHash, uint8 *pData);
  57. static int OTA_ValidateHashFunc(uint8 *digest, uint32 len, uint8 *data);
  58. /******************************************************************************
  59. * @fn OTA_AesHashBlock
  60. *
  61. * @brief This function performs the AES MMO Hash on a block of data
  62. *
  63. * @param pHash - Pointer to hash
  64. * pData - pointer to data
  65. *
  66. * @return none
  67. */
  68. void OTA_AesHashBlock(uint8 *pHash, uint8 *pData)
  69. {
  70. uint8 key[OTA_MMO_HASH_SIZE];
  71. osal_memcpy(key, pHash, OTA_MMO_HASH_SIZE);
  72. osal_memcpy(pHash, pData, OTA_MMO_HASH_SIZE);
  73. ssp_HW_KeyInit(key);
  74. sspAesEncryptHW(key, pHash);
  75. OTA_XorBlock(pHash, pData);
  76. }
  77. /******************************************************************************
  78. * @fn OTA_XorBlock
  79. *
  80. * @brief This function exclusive ORs a block of hash and data and puts the
  81. * result into the hash.
  82. *
  83. * @param pHash - Pointer to hash
  84. * pData - pointer to data
  85. *
  86. * @return none
  87. */
  88. void OTA_XorBlock(uint8 *pHash, uint8 *pData)
  89. {
  90. uint8 i;
  91. for (i=0; i < OTA_MMO_HASH_SIZE; i++)
  92. {
  93. pHash[i] ^= pData[i];
  94. }
  95. }
  96. /******************************************************************************
  97. * @fn OTA_CalculateMmoR3
  98. *
  99. * @brief This function calcualtes a MMO (revision 3) Hash of an OTA Image
  100. * The hash must cover the entire image, but the data is received in
  101. * smaller blocks. State information about the hash is passed into
  102. * this function with each block of data.
  103. *
  104. * @param pCtrl - The control structure to calculate the MMO AES Hash
  105. * pData - A block of data (must be OTA_MMO_HASH_SIZE bytes except for last block)
  106. * len - The length of pData (ignored except when lastBlock = TRUE)
  107. * lastBlock - Indicates this is the last block of data to be hashed
  108. *
  109. * @return none
  110. */
  111. void OTA_CalculateMmoR3(OTA_MmoCtrl_t *pCtrl, uint8 *pData, uint8 len, uint8 lastBlock)
  112. {
  113. if (lastBlock)
  114. {
  115. uint32 m = (pCtrl->length + len) << 3;
  116. uint8 ending[OTA_MMO_HASH_SIZE];
  117. osal_memset(ending, 0, OTA_MMO_HASH_SIZE);
  118. if ( len >= OTA_MMO_HASH_SIZE )
  119. {
  120. len = OTA_MMO_HASH_SIZE - 1;
  121. }
  122. if (len)
  123. {
  124. osal_memcpy(ending, pData, len);
  125. }
  126. ending[len] = 0x80;
  127. // Different endings are required depending on total length
  128. if (m < 0x00010000)
  129. {
  130. if(len > 13)
  131. {
  132. OTA_AesHashBlock(pCtrl->hash, ending);
  133. osal_memset(ending, 0, OTA_MMO_HASH_SIZE);
  134. }
  135. ending[14] = (uint8)((m >> 8) &0xFF);
  136. ending[15] = (uint8)(m & 0xFF);
  137. OTA_AesHashBlock(pCtrl->hash, ending);
  138. }
  139. else
  140. {
  141. if (len > 9)
  142. {
  143. OTA_AesHashBlock(pCtrl->hash, ending);
  144. osal_memset(ending, 0, OTA_MMO_HASH_SIZE);
  145. }
  146. ending[10] = (uint8)((m >> 24) & 0xFF);
  147. ending[11] = (uint8)((m >> 16) & 0xFF);
  148. ending[12] = (uint8)((m >> 8) & 0xFF);
  149. ending[13] = (uint8)(m & 0xFF);
  150. OTA_AesHashBlock(pCtrl->hash, ending);
  151. }
  152. }
  153. else
  154. {
  155. OTA_AesHashBlock(pCtrl->hash, pData);
  156. pCtrl->length += OTA_MMO_HASH_SIZE;
  157. }
  158. }
  159. #if defined (ZCL_KEY_ESTABLISH)
  160. /******************************************************************************
  161. * @fn OTA_ValidateHashFunc
  162. *
  163. * @brief This function is a hash function used by the ZSE_ECDSAVerify.
  164. *
  165. * @param digest - Buffer to hold the digest
  166. * len - The length of the digest
  167. * data - Buffer with the data
  168. *
  169. * @return Status of the operation
  170. */
  171. static int OTA_ValidateHashFunc(uint8 *digest, uint32 len, uint8 *data)
  172. {
  173. len *= 8; // Convert to bit length
  174. sspMMOHash( NULL, 0, data, (uint16)len, digest );
  175. return MCE_SUCCESS;
  176. }
  177. #endif
  178. /******************************************************************************
  179. * @fn OTA_ValidateSignature
  180. *
  181. * @brief This function validates an ECDSA Signature.
  182. *
  183. * @param pHash - The digest created from the OTA Image
  184. * pCert - The Signer Certificate
  185. * pSig - The signature from the OTA Image
  186. * pIEEE - The Signer IEEE
  187. *
  188. * @return none
  189. */
  190. uint8 OTA_ValidateSignature(uint8 *pHash, uint8* pCert, uint8 *pSig, uint8 *pIEEE)
  191. {
  192. #if defined (ZCL_KEY_ESTABLISH)
  193. uint8 publicKey[SECT163K1_COMPRESSED_PUBLIC_KEY_SIZE];
  194. uint8 ret;
  195. uint8 *caPublicKey;
  196. if ((caPublicKey = osal_mem_alloc(ZCL_KE_CA_PUBLIC_KEY_LEN)) == NULL)
  197. {
  198. return ZCL_STATUS_SOFTWARE_FAILURE; // Memory allocation failure.
  199. }
  200. osal_nv_read(ZCD_NV_CA_PUBLIC_KEY, 0, ZCL_KE_CA_PUBLIC_KEY_LEN, caPublicKey);
  201. ret = ZSE_ECQVReconstructPublicKey(pCert, caPublicKey, publicKey,
  202. OTA_ValidateHashFunc, NULL, 0);
  203. osal_mem_free(caPublicKey);
  204. if ( ret == MCE_SUCCESS )
  205. {
  206. ret = ZSE_ECDSAVerify(publicKey, pHash, pSig,
  207. pSig + SECT163K1_POINT_ORDER_SIZE,
  208. NULL, 0 );
  209. if ( ret == MCE_SUCCESS )
  210. {
  211. return ZSuccess;
  212. }
  213. }
  214. return ZFailure;
  215. #else
  216. return ZSuccess;
  217. #endif
  218. }