1 /*
2 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3 */
4
5 #include <stdlib.h>
6 #include <string.h>
7 #include "ota_log.h"
8 #include "ota_import.h"
9 #include "ota_hal_digest.h"
10 #include "mbedtls/sha256.h"
11 #include "mbedtls/md5.h"
12 #include "mbedtls/bignum.h"
13 #include "mbedtls/rsa.h"
14
15 /* RSA Public Key:User needs sign in alibaba cloud to get and replace them. */
16 static unsigned char ota_pubn_buf[256] = {0x00};
17
18 static unsigned char ota_pube_buf[3] = {0x01, 0x00, 0x01};
19
20 /* SHA256 */
ota_sha256_free(ota_sha256_context * ctx)21 void ota_sha256_free(ota_sha256_context *ctx)
22 {
23 mbedtls_sha256_free((mbedtls_sha256_context *)ctx);
24 }
25
ota_sha256_init(ota_sha256_context * ctx)26 void ota_sha256_init(ota_sha256_context *ctx)
27 {
28 mbedtls_sha256_init((mbedtls_sha256_context *)ctx);
29 }
30
ota_sha256_starts(ota_sha256_context * ctx,int is224)31 void ota_sha256_starts(ota_sha256_context *ctx, int is224)
32 {
33 mbedtls_sha256_starts((mbedtls_sha256_context *)ctx, is224);
34 }
35
ota_sha256_update(ota_sha256_context * ctx,const unsigned char * input,unsigned int ilen)36 void ota_sha256_update(ota_sha256_context *ctx, const unsigned char *input, unsigned int ilen)
37 {
38 mbedtls_sha256_update((mbedtls_sha256_context *)ctx, input, ilen);
39 }
40
ota_sha256_finish(ota_sha256_context * ctx,unsigned char output[32])41 void ota_sha256_finish(ota_sha256_context *ctx, unsigned char output[32])
42 {
43 mbedtls_sha256_finish((mbedtls_sha256_context *)ctx, output);
44 }
45
46 /*MD5*/
ota_md5_free(ota_md5_context * ctx)47 void ota_md5_free(ota_md5_context *ctx)
48 {
49 mbedtls_md5_free((mbedtls_md5_context *)ctx);
50 }
51
ota_md5_init(ota_md5_context * ctx)52 void ota_md5_init(ota_md5_context *ctx)
53 {
54 mbedtls_md5_init((mbedtls_md5_context *)ctx);
55 }
56
ota_md5_starts(ota_md5_context * ctx)57 void ota_md5_starts(ota_md5_context *ctx)
58 {
59 mbedtls_md5_starts((mbedtls_md5_context *)ctx);
60 }
61
ota_md5_update(ota_md5_context * ctx,const unsigned char * input,unsigned int ilen)62 void ota_md5_update(ota_md5_context *ctx, const unsigned char *input, unsigned int ilen)
63 {
64 mbedtls_md5_update((mbedtls_md5_context *)ctx, input, ilen);
65 }
66
ota_md5_finish(ota_md5_context * ctx,unsigned char output[16])67 void ota_md5_finish(ota_md5_context *ctx, unsigned char output[16])
68 {
69 mbedtls_md5_finish((mbedtls_md5_context *)ctx, output);
70 }
71
72 /*RSA*/
ota_rsa_pubkey_n(void)73 const unsigned char *ota_rsa_pubkey_n(void)
74 {
75 return ota_pubn_buf;
76 }
77
ota_rsa_pubkey_e(void)78 const unsigned char *ota_rsa_pubkey_e(void)
79 {
80 return ota_pube_buf;
81 }
82
ota_rsa_pubkey_n_size(void)83 unsigned int ota_rsa_pubkey_n_size(void)
84 {
85 return sizeof(ota_pubn_buf);
86 }
87
ota_rsa_pubkey_e_size(void)88 unsigned int ota_rsa_pubkey_e_size(void)
89 {
90 return sizeof(ota_pube_buf);
91 }
92
ota_rsa_pubkey_verify(const unsigned char * pubkey_n,const unsigned char * pubkey_e,unsigned int pubkey_n_size,unsigned int pubkey_e_size,const unsigned char * dig,unsigned int dig_size,const unsigned char * sig,unsigned int sig_size)93 int ota_rsa_pubkey_verify(const unsigned char *pubkey_n,
94 const unsigned char *pubkey_e,
95 unsigned int pubkey_n_size,
96 unsigned int pubkey_e_size,
97 const unsigned char *dig,
98 unsigned int dig_size,
99 const unsigned char *sig,
100 unsigned int sig_size)
101 {
102 int ret = 0;
103 mbedtls_rsa_context ctx;
104
105 if (pubkey_n == NULL || pubkey_n == NULL || dig == NULL || sig == NULL) {
106 ret = OTA_VERIFY_RSA_FAIL;
107 goto EXIT;
108 }
109 if (pubkey_n_size == 0 || pubkey_e_size == 0 || sig_size == 0 || dig_size != OTA_SHA256_HASH_SIZE) {
110 ret = OTA_VERIFY_RSA_FAIL;
111 goto EXIT;
112 }
113 mbedtls_rsa_init(&ctx, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_SHA256);
114 ret = mbedtls_mpi_read_binary(&ctx.N, pubkey_n, pubkey_n_size);
115 if (0 != ret) {
116 goto EXIT;
117 }
118 ret = mbedtls_mpi_read_binary(&ctx.E, pubkey_e, pubkey_e_size);
119 if (0 != ret) {
120 goto EXIT;
121 }
122 ctx.len = pubkey_n_size;
123 ret = mbedtls_rsa_check_pubkey(&ctx);
124 if (0 != ret) {
125 goto EXIT;
126 }
127 ret = mbedtls_rsa_pkcs1_verify(&ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA256, (unsigned int)0, (const unsigned char *)dig, (const unsigned char *)sig);
128 if (0 != ret) {
129 goto EXIT;
130 }
131 EXIT:
132 if (ret != 0) {
133 OTA_LOG_E("rsa verify ret: 0x%x", ret);
134 }
135 mbedtls_rsa_free(&ctx);
136 return ret;
137 }
138