1 /**@defgroup ota_hal_digest_api 2 * @{ 3 * This is an include file of OTA verify interface. 4 * 5 * Copyright (C) 2015-2021 Alibaba Group Holding Limited 6 */ 7 8 #ifndef OTA_HAL_DIGEST_H 9 #define OTA_HAL_DIGEST_H 10 #include "ota_agent.h" 11 12 #ifdef __cplusplus 13 extern "C" { 14 #endif 15 /*************************************************************** 16 *** OTA verify module: hash:md5/sha256 sign:RSA *** 17 ****************************************************************/ 18 /** 19 * Struct: MD5 Context. 20 */ 21 typedef struct { 22 unsigned int total[2]; 23 unsigned int state[4]; 24 unsigned char buffer[64]; 25 } ota_md5_context; 26 27 /** 28 * Struct: SHA256 Context. 29 */ 30 typedef struct { 31 unsigned int total[2]; 32 unsigned int state[8]; 33 unsigned char buffer[64]; 34 int is224; 35 } ota_sha256_context; 36 37 /** 38 * Struct: ota sign context. 39 */ 40 typedef struct { 41 char sign_enable; /* enable sign */ 42 unsigned char sign_value[256]; /* sign value */ 43 } ota_sign_t; 44 45 /** 46 * Struct: ota hash context. 47 */ 48 typedef struct { 49 unsigned char hash_method; /* hash method: md5, sha256 */ 50 union { 51 ota_md5_context md5_ctx; /* md5 hash context */ 52 ota_sha256_context sha256_ctx; /* sh256 hash context */ 53 }; 54 } ota_hash_ctx_t; 55 56 /** 57 * ota_hash_init ota hash init. 58 * 59 * @param[in] ota_hash_ctx_t *ctx OTA hash context 60 * @param[in] unsigned char type OTA hash type 61 * 62 * @return OTA_SUCCESS OTA success. 63 * @return OTA_VERIFY_MD5_FAIL OTA verfiy MD5 fail. 64 * @return OTA_VERIFY_SHA2_FAIL OTA verfiy SH256 fail. 65 * @return OTA_VERIFY_RSA_FAIL OTA verfiy RSA fail. 66 * @return OTA_VERIFY_IMAGE_FAIL OTA verfiy image fail. 67 */ 68 int ota_hash_init(ota_hash_ctx_t *ctx, unsigned char type); 69 70 /** 71 * ota_hash_update ota hash update. 72 * 73 * @param[in] ota_hash_ctx_t *ctx OTA hash context 74 * @param[in] const unsigned char *buf OTA hash buf 75 * @param[in] unsigned int len OTA hash len 76 * 77 * @return OTA_SUCCESS OTA success. 78 * @return OTA_VERIFY_MD5_FAIL OTA verfiy MD5 fail. 79 * @return OTA_VERIFY_SHA2_FAIL OTA verfiy SH256 fail. 80 * @return OTA_VERIFY_RSA_FAIL OTA verfiy RSA fail. 81 * @return OTA_VERIFY_IMAGE_FAIL OTA verfiy image fail. 82 */ 83 int ota_hash_update(ota_hash_ctx_t *ctx, const unsigned char *buf, unsigned int len); 84 85 /** 86 * ota_hash_final OTA final hash. 87 * 88 * @param[in] ota_hash_ctx_t *ctx OTA hash context 89 * @param[in] unsigned char *buf OTA hash digest 90 * 91 * @return OTA_SUCCESS OTA success. 92 * @return OTA_VERIFY_MD5_FAIL OTA verfiy MD5 fail. 93 * @return OTA_VERIFY_SHA2_FAIL OTA verfiy SH256 fail. 94 * @return OTA_VERIFY_RSA_FAIL OTA verfiy RSA fail. 95 * @return OTA_VERIFY_IMAGE_FAIL OTA verfiy image fail. 96 */ 97 int ota_hash_final(ota_hash_ctx_t *ctx, unsigned char *dgst); 98 99 /** 100 * ota_verify_rsa OTA verify RSA sign. 101 * 102 * @param[in] unsigned char *sign OTA firmware sign 103 * @param[in] const char *hash OTA firmware hash 104 * @param[in] unsigned char hash_type OTA hash type 105 * 106 * @return OTA_SUCCESS OTA success. 107 * @return OTA_VERIFY_MD5_FAIL OTA verfiy MD5 fail. 108 * @return OTA_VERIFY_SHA2_FAIL OTA verfiy SH256 fail. 109 * @return OTA_VERIFY_RSA_FAIL OTA verfiy RSA fail. 110 * @return OTA_VERIFY_IMAGE_FAIL OTA verfiy image fail. 111 */ 112 int ota_verify_rsa(unsigned char *sign, const char *hash, unsigned char hash_type); 113 114 /*Verify API*/ 115 /*SHA256*/ 116 void ota_sha256_free(ota_sha256_context *ctx); 117 void ota_sha256_init(ota_sha256_context *ctx); 118 void ota_sha256_starts(ota_sha256_context *ctx, int is224); 119 void ota_sha256_update(ota_sha256_context *ctx, const unsigned char *input, unsigned int ilen); 120 void ota_sha256_finish(ota_sha256_context *ctx, unsigned char output[32]); 121 /*MD5*/ 122 void ota_md5_free(ota_md5_context *ctx); 123 void ota_md5_init(ota_md5_context *ctx); 124 void ota_md5_starts(ota_md5_context *ctx); 125 void ota_md5_update(ota_md5_context *ctx, const unsigned char *input, unsigned int ilen); 126 void ota_md5_finish(ota_md5_context *ctx, unsigned char output[16]); 127 /*RSA*/ 128 const unsigned char *ota_rsa_pubkey_n(void); 129 const unsigned char *ota_rsa_pubkey_e(void); 130 unsigned int ota_rsa_pubkey_n_size(void); 131 unsigned int ota_rsa_pubkey_e_size(void); 132 int ota_rsa_pubkey_verify(const unsigned char *pubkey_n, 133 const unsigned char *pubkey_e, 134 unsigned int pubkey_n_size, 135 unsigned int pubkey_e_size, 136 const unsigned char *dig, 137 unsigned int dig_size, 138 const unsigned char *sig, 139 unsigned int sig_size); 140 /** 141 * @} 142 */ 143 #ifdef __cplusplus 144 } 145 #endif 146 #endif /*OTA_HAL_DIGEST_H*/ 147 148