1 /*
2 *
3 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19 #include <string.h>
20
21 #include "../../cipher/v2/api/hi_mpi_cipher.h"
22
23 #include "../update_log.h"
24 #include "update_public_key.h"
25
update_sign_rsa_verify(const unsigned char * data,unsigned int data_len,unsigned char * sign,unsigned int sign_len)26 int update_sign_rsa_verify(const unsigned char *data, unsigned int data_len,
27 unsigned char *sign, unsigned int sign_len)
28 {
29 int ret;
30 hi_cipher_rsa_verify rsa;
31 hi_cipher_verify_data verify_data;
32
33 if (!data || data_len == 0 || !sign || sign_len == 0) {
34 UPD_LOGE("invalid");
35 return -1;
36 }
37
38 memset(&rsa, 0, sizeof(rsa));
39 memset(&verify_data, 0, sizeof(hi_cipher_verify_data));
40
41 verify_data.in = data;
42 verify_data.in_len = data_len;
43 verify_data.sign = sign;
44 verify_data.sign_len = sign_len;
45
46 rsa.scheme = HI_CIPHER_RSA_SIGN_SCHEME_RSASSA_PKCS1_PSS_SHA256;
47 rsa.pub_key.n = get_ota_pub_key_N(&rsa.pub_key.n_len);
48 rsa.pub_key.e = get_ota_pub_key_E(&rsa.pub_key.e_len);
49 if (!rsa.pub_key.n || !rsa.pub_key.e) {
50 UPD_LOGE("public key invalid");
51 return -1;
52 }
53
54 UPD_LOGI("verify sign start...");
55 ret = hi_mpi_cipher_rsa_verify(&rsa, &verify_data);
56 if (ret != HI_SUCCESS) {
57 UPD_LOGE("verify sign fail!");
58 return -1;
59 }
60 UPD_LOGI("verify sign ok");
61 return 0;
62 }
63
update_rsa_init(void)64 int update_rsa_init(void)
65 {
66 return hi_mpi_cipher_init() == HI_SUCCESS ? 0 : -1;
67 }
68
update_rsa_free(void)69 void update_rsa_free(void)
70 {
71 (void)hi_mpi_cipher_deinit();
72 }
73