fota_verify.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (C) 2019-2020 Alibaba Group Holding Limited
  3. */
  4. #include <errno.h>
  5. #include <string.h>
  6. #include "yoc/fota.h"
  7. #include <ulog/ulog.h>
  8. #define TAG "fotav"
  9. #ifndef __linux__
  10. #include <yoc/partition.h>
  11. #include <verify.h>
  12. #include <verify_wrapper.h>
  13. #define DUMP_DATA_EN 0
  14. typedef struct {
  15. uint32_t magic;
  16. uint8_t ver;
  17. uint8_t image_sum;
  18. uint8_t digest_type;
  19. uint8_t signature_type;
  20. uint32_t mnft_off;
  21. uint8_t head_len;
  22. uint8_t pad_type;
  23. uint8_t rsvd[2];
  24. } fota_head_info_t;
  25. #if DUMP_DATA_EN
  26. static void dump_data(uint8_t *data, int32_t len)
  27. {
  28. int32_t i;
  29. for (i = 0; i < len; i++) {
  30. if (i % 16 == 0) {
  31. printf("\n");
  32. }
  33. printf("%02x ", data[i]);
  34. }
  35. printf("\n");
  36. }
  37. #endif
  38. #if defined(CONFIG_FOTA_DATA_IN_RAM) && CONFIG_FOTA_DATA_IN_RAM > 0
  39. /**
  40. * @brief 获取存储fota数据的ram地址
  41. * @return address
  42. */
  43. __attribute__((weak)) unsigned long fota_data_address_get(void)
  44. {
  45. return 0;
  46. }
  47. #endif
  48. __attribute__((weak)) int fota_data_verify(void)
  49. {
  50. #define FOTA_DATA_MAGIC 0x45474d49
  51. #define BUF_SIZE 512
  52. int ret;
  53. uint8_t *buffer;
  54. uint8_t *hash_out;
  55. uint32_t olen;
  56. int fota_data_offset, image_size;
  57. int signature_len, hash_len;
  58. int signature_offset, hash_offset;
  59. digest_sch_e digest_type;
  60. signature_sch_e sign_type;
  61. partition_t partition;
  62. partition_info_t *partition_info;
  63. fota_head_info_t *head;
  64. unsigned long data_address_start;
  65. LOGD(TAG, "start fota verify...");
  66. ret = 0;
  67. buffer = NULL;
  68. hash_out = NULL;
  69. #if CONFIG_FOTA_DATA_IN_RAM > 0
  70. (void)partition;
  71. (void)partition_info;
  72. data_address_start = fota_data_address_get();
  73. if (data_address_start == 0) {
  74. LOGE(TAG, "fota data address[0x%x] error.", data_address_start);
  75. return -1;
  76. }
  77. head = (fota_head_info_t *)data_address_start;
  78. fota_data_offset = 0;
  79. buffer = aos_malloc(BUF_SIZE);
  80. #else
  81. (void)data_address_start;
  82. partition = partition_open("misc");
  83. if (partition < 0) {
  84. LOGE(TAG, "flash open e.");
  85. return -1;
  86. }
  87. partition_info = hal_flash_get_info(partition);
  88. if (partition_info == NULL) {
  89. ret = -EIO;
  90. goto out;
  91. }
  92. fota_data_offset = partition_info->sector_size << 1;
  93. buffer = aos_malloc(BUF_SIZE);
  94. if (buffer == NULL) {
  95. ret = -ENOMEM;
  96. goto out;
  97. }
  98. if (partition_read(partition, fota_data_offset, buffer, BUF_SIZE) < 0) {
  99. ret = -EIO;
  100. goto out;
  101. }
  102. head = (fota_head_info_t *)buffer;
  103. #endif
  104. if (head->magic != FOTA_DATA_MAGIC) {
  105. LOGE(TAG, "app fota data magic e");
  106. ret = -1;
  107. goto out;
  108. }
  109. image_size = head->mnft_off;
  110. digest_type = head->digest_type;
  111. sign_type = head->signature_type;
  112. hash_len = get_length_with_digest_type(digest_type);
  113. signature_len = get_length_with_signature_type(sign_type);
  114. signature_offset = image_size + fota_data_offset;
  115. hash_offset = signature_offset + 256;
  116. LOGD(TAG, "image_size:%d", image_size);
  117. LOGD(TAG, "digest_type:%d", digest_type);
  118. LOGD(TAG, "sign_type:%d", sign_type);
  119. LOGD(TAG, "hash_len:%d", hash_len);
  120. LOGD(TAG, "signature_len:%d", signature_len);
  121. LOGD(TAG, "signature_offset:%d", signature_offset);
  122. LOGD(TAG, "hash_offset:%d", hash_offset);
  123. hash_out = aos_malloc(hash_len);
  124. if (hash_out == NULL) {
  125. ret = -ENOMEM;
  126. goto out;
  127. }
  128. #if CONFIG_FOTA_DATA_IN_RAM > 0
  129. ret = hash_calc_start(digest_type, (const uint8_t *)(fota_data_offset + data_address_start), image_size, hash_out, &olen, 1);
  130. if (ret != 0) {
  131. LOGE(TAG, "hash calc failed.");
  132. goto out;
  133. }
  134. memcpy(buffer, (void *)(hash_offset + data_address_start), hash_len);
  135. #else
  136. ret = hash_calc_start(digest_type, (const uint8_t *)fota_data_offset + partition_info->base_addr + partition_info->start_addr, image_size, hash_out, &olen, 0);
  137. if (ret != 0) {
  138. LOGE(TAG, "hash calc failed.");
  139. goto out;
  140. }
  141. if (partition_read(partition, hash_offset, buffer, hash_len) < 0) {
  142. ret = -EIO;
  143. goto out;
  144. }
  145. #endif
  146. #if CONFIG_FOTA_IMG_AUTHENTICITY_NOT_CHECK == 0
  147. // TODO: not support yet
  148. uint8_t *key;
  149. int key_len = 0;
  150. if (partition_read(partition, signature_offset, buffer, signature_len) < 0) {
  151. ret = -EIO;
  152. goto out;
  153. }
  154. #if DUMP_DATA_EN
  155. dump_data(buffer, signature_len);
  156. #endif
  157. ret = signature_verify_start(digest_type, sign_type, key, ken_len, hash_out, hash_len, buffer, signature_len);
  158. if (ret != 0) {
  159. LOGE(TAG, "signature verify failed.");
  160. goto out;
  161. }
  162. LOGD(TAG, "fota data verify ok...");
  163. #else
  164. (void)sign_type;
  165. (void)signature_len;
  166. #if DUMP_DATA_EN
  167. dump_data(buffer, hash_len);
  168. dump_data(hash_out, hash_len);
  169. #endif
  170. if (memcmp(hash_out, buffer, hash_len) != 0) {
  171. LOGE(TAG, "!!!fota data hash v e..");
  172. ret = -EIO;
  173. goto out;
  174. }
  175. LOGI(TAG, "###fota data hash v ok.");
  176. #endif /*CONFIG_FOTA_IMG_AUTHENTICITY_NOT_CHECK*/
  177. out:
  178. if (buffer)
  179. aos_free(buffer);
  180. if (hash_out)
  181. aos_free(hash_out);
  182. #if CONFIG_FOTA_DATA_IN_RAM == 0
  183. partition_close(partition);
  184. #endif
  185. return ret;
  186. }
  187. #else
  188. __attribute__((weak)) int fota_data_verify(void)
  189. {
  190. return 0;
  191. }
  192. #endif /*__linux__*/