ali_genie_profile.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /*******************************************************************************
  29. * @file ali_genie_profile.c
  30. * @brief Contains all functions support for ali genie profile
  31. * @version 1.0
  32. * @date 28. Feb. 2019
  33. * @author Zhongqi Yang
  34. *
  35. * Copyright(C) 2016, PhyPlus Semiconductor
  36. * All rights reserved.
  37. *
  38. *******************************************************************************/
  39. #include "sha256.h"
  40. #include "flash.h"
  41. #include <string.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include "ali_genie_profile.h"
  45. #define ALI_GENIE_PID_LEN 4
  46. #define ALI_GENIE_SEC_LEN 16
  47. #define VENDOR_PRODUCT_ID_ADDR 0x4030
  48. #define VENDOR_PRODUCT_SECRERT_ADDR 0x4010
  49. #define VENDOR_PRODUCT_MAC_ADDR 0x4000
  50. unsigned char ali_genie_pid[4] ={0x00};
  51. unsigned char ali_genie_mac[6] ={0x00};
  52. unsigned char ali_genie_macStr[12];
  53. unsigned char ali_genie_sec[16] ={0x00};
  54. unsigned char ali_genie_auth[16]={0x00};
  55. void hex2Str( unsigned char* pIn,unsigned char*pOut ,int len ,int reverse)
  56. {
  57. int i;
  58. unsigned char hex[] = "0123456789abcdef";
  59. // Start from end of addr
  60. if(reverse)
  61. {
  62. pIn += len;
  63. for ( i = len; i > 0; i-- )
  64. {
  65. *pOut++ = hex[*--pIn >> 4];
  66. *pOut++ = hex[*pIn & 0x0F];
  67. }
  68. }
  69. else
  70. {
  71. for ( i = 0; i <len; i++ )
  72. {
  73. *pOut++ = hex[*pIn >> 4];
  74. *pOut++ = hex[*pIn++ & 0x0F];
  75. }
  76. }
  77. }
  78. int gen_aligenie_auth_val(void)
  79. {
  80. int i, buflen, ret = 0;
  81. //unsigned char buf[]="000293e2,abcdf0f1f2f3,53daed805bc534a4a93c825ed20a7063";
  82. unsigned char buf[54];
  83. // unsigned char pidStr[8];
  84. // unsigned char macAddrStr[12];
  85. // unsigned char secStr[32];
  86. unsigned char sha256sum[32];
  87. mbedtls_sha256_context ctx;
  88. //load ProductID
  89. for(i=0;i<4;i++)
  90. ali_genie_pid[i]=(uint8_t)ReadFlash(VENDOR_PRODUCT_ID_ADDR+i);
  91. hex2Str(ali_genie_pid,buf,4,0);
  92. printf("\n === PID === \n");
  93. for(i=0;i<4;i++)
  94. printf("%02X ",ali_genie_pid[i]);
  95. printf("\n");
  96. buf[8]=0x2c;
  97. //load MAC
  98. uint32 address = VENDOR_PRODUCT_MAC_ADDR;
  99. ali_genie_mac[3] = (uint8_t)ReadFlash(address ++);
  100. ali_genie_mac[2] = (uint8_t)ReadFlash(address ++);
  101. ali_genie_mac[1] = (uint8_t)ReadFlash(address ++);
  102. ali_genie_mac[0] = (uint8_t)ReadFlash(address ++);
  103. ali_genie_mac[5] = (uint8_t)ReadFlash(address ++);
  104. ali_genie_mac[4] = (uint8_t)ReadFlash(address);
  105. printf("\n === MAC === \n");
  106. for(i=0;i<6;i++)
  107. printf("%02X ",ali_genie_mac[5-i]);
  108. printf("\n");
  109. //hex2Str(ali_genie_mac,buf+9,6,1);
  110. hex2Str(ali_genie_mac,ali_genie_macStr,6,1);
  111. for(i=0;i<12;i++)
  112. buf[9+i]=ali_genie_macStr[i];
  113. buf[21]=0x2c;
  114. //load secert
  115. for(i=0;i<16;i++)
  116. ali_genie_sec[i]=(uint8_t)ReadFlash(VENDOR_PRODUCT_SECRERT_ADDR+i);
  117. hex2Str(ali_genie_sec,buf+22,16,0);
  118. mbedtls_sha256_init( &ctx );
  119. if( ( ret = mbedtls_sha256_starts_ret( &ctx, 0 ) ) != 0 )
  120. goto fail;
  121. buflen = 54;
  122. // printf("\n input %d ",buflen);
  123. // for(i=0;i<buflen;i++)
  124. // printf("%02x ",buf[i]);
  125. //
  126. // printf("\n");
  127. if( (ret = mbedtls_sha256_update_ret( &ctx, buf, buflen )) != 0 )
  128. goto fail;
  129. if( ( ret = mbedtls_sha256_finish_ret( &ctx, sha256sum ) ) != 0 )
  130. goto fail;
  131. // for(i=0;i<32;i++)
  132. // printf("%02x ",sha256sum[i]);
  133. goto exit;
  134. fail:
  135. printf( "failed\n" );
  136. exit:
  137. //mbedtls_sha256_free( &ctx );
  138. for(i=0;i<16;i++)
  139. {
  140. ali_genie_auth[i]=sha256sum[i];
  141. }
  142. return( ret );
  143. }