119c402afSSimon Glass /*
219c402afSSimon Glass * Copyright (c) 2013, Google Inc.
319c402afSSimon Glass *
41a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+
519c402afSSimon Glass */
619c402afSSimon Glass
729a23f9dSHeiko Schocher #ifndef USE_HOSTCC
819c402afSSimon Glass #include <common.h>
9008ec9b4SJoseph Chen #include <crypto.h>
1019c402afSSimon Glass #include <fdtdec.h>
1178263d89SJason Zhu #include <misc.h>
1229a23f9dSHeiko Schocher #include <asm/types.h>
1329a23f9dSHeiko Schocher #include <asm/byteorder.h>
141221ce45SMasahiro Yamada #include <linux/errno.h>
1529a23f9dSHeiko Schocher #include <asm/types.h>
1629a23f9dSHeiko Schocher #include <asm/unaligned.h>
17c937ff6dSRuchika Gupta #include <dm.h>
18f649b885SJason Zhu #include <asm/arch/rk_atags.h>
1929a23f9dSHeiko Schocher #else
2029a23f9dSHeiko Schocher #include "fdt_host.h"
2129a23f9dSHeiko Schocher #include "mkimage.h"
2229a23f9dSHeiko Schocher #include <fdt_support.h>
2329a23f9dSHeiko Schocher #endif
24fc2f4246SRuchika Gupta #include <u-boot/rsa-mod-exp.h>
252b9912e6SJeroen Hofstee #include <u-boot/rsa.h>
2629a23f9dSHeiko Schocher
27e0f2f155SMichael van der Westhuizen /* Default public exponent for backward compatibility */
28e0f2f155SMichael van der Westhuizen #define RSA_DEFAULT_PUBEXP 65537
29e0f2f155SMichael van der Westhuizen
30f8e13adfSXuhui Lin /* Default otp value for enable rsa4096 */
31f8e13adfSXuhui Lin #ifndef OTP_RSA4096_ENABLE_VALUE
32f8e13adfSXuhui Lin #define OTP_RSA4096_ENABLE_VALUE 0x30
33f8e13adfSXuhui Lin #endif
34f8e13adfSXuhui Lin
35f8e13adfSXuhui Lin /* Default otp value for enable secureboot */
36f8e13adfSXuhui Lin #ifndef OTP_SECURE_BOOT_ENABLE_VALUE
37f8e13adfSXuhui Lin #define OTP_SECURE_BOOT_ENABLE_VALUE 0xff
38f8e13adfSXuhui Lin #endif
39f8e13adfSXuhui Lin
4019c402afSSimon Glass /**
41da29f299SAndrew Duda * rsa_verify_padding() - Verify RSA message padding is valid
42da29f299SAndrew Duda *
43da29f299SAndrew Duda * Verify a RSA message's padding is consistent with PKCS1.5
44da29f299SAndrew Duda * padding as described in the RSA PKCS#1 v2.1 standard.
45da29f299SAndrew Duda *
46da29f299SAndrew Duda * @msg: Padded message
47da29f299SAndrew Duda * @pad_len: Number of expected padding bytes
48da29f299SAndrew Duda * @algo: Checksum algo structure having information on DER encoding etc.
49da29f299SAndrew Duda * @return 0 on success, != 0 on failure
50da29f299SAndrew Duda */
rsa_verify_padding(const uint8_t * msg,const int pad_len,struct checksum_algo * algo)51da29f299SAndrew Duda static int rsa_verify_padding(const uint8_t *msg, const int pad_len,
52da29f299SAndrew Duda struct checksum_algo *algo)
53da29f299SAndrew Duda {
54da29f299SAndrew Duda int ff_len;
55da29f299SAndrew Duda int ret;
56da29f299SAndrew Duda
57da29f299SAndrew Duda /* first byte must be 0x00 */
58da29f299SAndrew Duda ret = *msg++;
59da29f299SAndrew Duda /* second byte must be 0x01 */
60da29f299SAndrew Duda ret |= *msg++ ^ 0x01;
61da29f299SAndrew Duda /* next ff_len bytes must be 0xff */
62da29f299SAndrew Duda ff_len = pad_len - algo->der_len - 3;
63da29f299SAndrew Duda ret |= *msg ^ 0xff;
64da29f299SAndrew Duda ret |= memcmp(msg, msg+1, ff_len-1);
65da29f299SAndrew Duda msg += ff_len;
66da29f299SAndrew Duda /* next byte must be 0x00 */
67da29f299SAndrew Duda ret |= *msg++;
68da29f299SAndrew Duda /* next der_len bytes must match der_prefix */
69da29f299SAndrew Duda ret |= memcmp(msg, algo->der_prefix, algo->der_len);
70da29f299SAndrew Duda
71da29f299SAndrew Duda return ret;
72da29f299SAndrew Duda }
73da29f299SAndrew Duda
74008ec9b4SJoseph Chen #if !defined(USE_HOSTCC)
75008ec9b4SJoseph Chen #if CONFIG_IS_ENABLED(FIT_HW_CRYPTO)
rsa_convert_big_endian(uint32_t * dst,const uint32_t * src,int total_len,int convert_len)769c63859fSJason Zhu static void rsa_convert_big_endian(uint32_t *dst, const uint32_t *src,
779c63859fSJason Zhu int total_len, int convert_len)
78008ec9b4SJoseph Chen {
799c63859fSJason Zhu int total_wd, convert_wd, i;
80008ec9b4SJoseph Chen
819c63859fSJason Zhu if (total_len < convert_len)
829c63859fSJason Zhu convert_len = total_len;
839c63859fSJason Zhu
849c63859fSJason Zhu total_wd = total_len / sizeof(uint32_t);
859c63859fSJason Zhu convert_wd = convert_len / sizeof(uint32_t);
869c63859fSJason Zhu for (i = 0; i < convert_wd; i++)
879c63859fSJason Zhu dst[i] = fdt32_to_cpu(src[total_wd - 1 - i]);
88008ec9b4SJoseph Chen }
89008ec9b4SJoseph Chen
rsa_mod_exp_hw(struct key_prop * prop,const uint8_t * sig,const uint32_t sig_len,const uint32_t key_len,uint8_t * output)900fb93272SJoseph Chen static int rsa_mod_exp_hw(struct key_prop *prop, const uint8_t *sig,
91008ec9b4SJoseph Chen const uint32_t sig_len, const uint32_t key_len,
92008ec9b4SJoseph Chen uint8_t *output)
93008ec9b4SJoseph Chen {
94008ec9b4SJoseph Chen struct udevice *dev;
95008ec9b4SJoseph Chen uint8_t sig_reverse[sig_len];
96008ec9b4SJoseph Chen uint8_t buf[sig_len];
97008ec9b4SJoseph Chen rsa_key rsa_key;
98008ec9b4SJoseph Chen int i, ret;
99c9e2e133SXuhui Lin #ifdef CONFIG_FIT_ENABLE_RSA4096_SUPPORT
100c9e2e133SXuhui Lin if (key_len != RSA4096_BYTES)
101c9e2e133SXuhui Lin return -EINVAL;
102008ec9b4SJoseph Chen
103c9e2e133SXuhui Lin rsa_key.algo = CRYPTO_RSA4096;
104c9e2e133SXuhui Lin #else
105008ec9b4SJoseph Chen if (key_len != RSA2048_BYTES)
106008ec9b4SJoseph Chen return -EINVAL;
107008ec9b4SJoseph Chen
108008ec9b4SJoseph Chen rsa_key.algo = CRYPTO_RSA2048;
109c9e2e133SXuhui Lin #endif
110008ec9b4SJoseph Chen rsa_key.n = malloc(key_len);
111008ec9b4SJoseph Chen rsa_key.e = malloc(key_len);
112008ec9b4SJoseph Chen rsa_key.c = malloc(key_len);
113008ec9b4SJoseph Chen if (!rsa_key.n || !rsa_key.e || !rsa_key.c)
114008ec9b4SJoseph Chen return -ENOMEM;
115008ec9b4SJoseph Chen
116008ec9b4SJoseph Chen rsa_convert_big_endian(rsa_key.n, (uint32_t *)prop->modulus,
1179c63859fSJason Zhu key_len, key_len);
118008ec9b4SJoseph Chen rsa_convert_big_endian(rsa_key.e, (uint32_t *)prop->public_exponent_BN,
1199c63859fSJason Zhu key_len, key_len);
120008ec9b4SJoseph Chen #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
121008ec9b4SJoseph Chen rsa_convert_big_endian(rsa_key.c, (uint32_t *)prop->factor_c,
1229c63859fSJason Zhu key_len, key_len);
123008ec9b4SJoseph Chen #else
124008ec9b4SJoseph Chen rsa_convert_big_endian(rsa_key.c, (uint32_t *)prop->factor_np,
1259c63859fSJason Zhu key_len, key_len);
126008ec9b4SJoseph Chen #endif
127f649b885SJason Zhu #if defined(CONFIG_ROCKCHIP_PRELOADER_ATAGS) && defined(CONFIG_SPL_BUILD)
128f649b885SJason Zhu char *rsa_key_data = malloc(3 * key_len);
129f649b885SJason Zhu int flag = 0;
130f649b885SJason Zhu
131f649b885SJason Zhu if (rsa_key_data) {
132f649b885SJason Zhu memcpy(rsa_key_data, rsa_key.n, key_len);
133f649b885SJason Zhu memcpy(rsa_key_data + key_len, rsa_key.e, key_len);
134f649b885SJason Zhu memcpy(rsa_key_data + 2 * key_len, rsa_key.c, key_len);
135f649b885SJason Zhu if (fit_board_verify_required_sigs())
136f649b885SJason Zhu flag = PUBKEY_FUSE_PROGRAMMED;
137f649b885SJason Zhu
138f649b885SJason Zhu if (atags_set_pub_key(rsa_key_data, 3 * key_len, flag))
139f649b885SJason Zhu printf("Send public key through atags fail.");
140f649b885SJason Zhu }
141f649b885SJason Zhu #endif
142008ec9b4SJoseph Chen for (i = 0; i < sig_len; i++)
143008ec9b4SJoseph Chen sig_reverse[sig_len-1-i] = sig[i];
144008ec9b4SJoseph Chen
145008ec9b4SJoseph Chen dev = crypto_get_device(rsa_key.algo);
146008ec9b4SJoseph Chen if (!dev) {
147008ec9b4SJoseph Chen printf("No crypto device for expected RSA\n");
148008ec9b4SJoseph Chen return -ENODEV;
149008ec9b4SJoseph Chen }
150008ec9b4SJoseph Chen
151008ec9b4SJoseph Chen ret = crypto_rsa_verify(dev, &rsa_key, (u8 *)sig_reverse, buf);
152008ec9b4SJoseph Chen if (ret)
153008ec9b4SJoseph Chen goto out;
154008ec9b4SJoseph Chen
155008ec9b4SJoseph Chen for (i = 0; i < sig_len; i++)
156008ec9b4SJoseph Chen sig_reverse[sig_len-1-i] = buf[i];
157008ec9b4SJoseph Chen
158008ec9b4SJoseph Chen memcpy(output, sig_reverse, sig_len);
159008ec9b4SJoseph Chen out:
160008ec9b4SJoseph Chen free(rsa_key.n);
161008ec9b4SJoseph Chen free(rsa_key.e);
162008ec9b4SJoseph Chen free(rsa_key.c);
163008ec9b4SJoseph Chen
164008ec9b4SJoseph Chen return ret;
165008ec9b4SJoseph Chen }
166008ec9b4SJoseph Chen #endif
167008ec9b4SJoseph Chen #endif
168008ec9b4SJoseph Chen
padding_pkcs_15_verify(struct image_sign_info * info,uint8_t * msg,int msg_len,const uint8_t * hash,int hash_len)169219050bfSPhilippe Reynes int padding_pkcs_15_verify(struct image_sign_info *info,
170219050bfSPhilippe Reynes uint8_t *msg, int msg_len,
171219050bfSPhilippe Reynes const uint8_t *hash, int hash_len)
172219050bfSPhilippe Reynes {
173219050bfSPhilippe Reynes struct checksum_algo *checksum = info->checksum;
174219050bfSPhilippe Reynes int ret, pad_len = msg_len - checksum->checksum_len;
175219050bfSPhilippe Reynes
176219050bfSPhilippe Reynes /* Check pkcs1.5 padding bytes. */
177219050bfSPhilippe Reynes ret = rsa_verify_padding(msg, pad_len, checksum);
178219050bfSPhilippe Reynes if (ret) {
179219050bfSPhilippe Reynes debug("In RSAVerify(): Padding check failed!\n");
180219050bfSPhilippe Reynes return -EINVAL;
181219050bfSPhilippe Reynes }
182219050bfSPhilippe Reynes
183219050bfSPhilippe Reynes /* Check hash. */
184219050bfSPhilippe Reynes if (memcmp((uint8_t *)msg + pad_len, hash, msg_len - pad_len)) {
185219050bfSPhilippe Reynes debug("In RSAVerify(): Hash check failed!\n");
186219050bfSPhilippe Reynes return -EACCES;
187219050bfSPhilippe Reynes }
188219050bfSPhilippe Reynes
189219050bfSPhilippe Reynes return 0;
190219050bfSPhilippe Reynes }
191219050bfSPhilippe Reynes
19285289e9dSPhilippe Reynes #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
u32_i2osp(uint32_t val,uint8_t * buf)19385289e9dSPhilippe Reynes static void u32_i2osp(uint32_t val, uint8_t *buf)
19485289e9dSPhilippe Reynes {
19585289e9dSPhilippe Reynes buf[0] = (uint8_t)((val >> 24) & 0xff);
19685289e9dSPhilippe Reynes buf[1] = (uint8_t)((val >> 16) & 0xff);
19785289e9dSPhilippe Reynes buf[2] = (uint8_t)((val >> 8) & 0xff);
19885289e9dSPhilippe Reynes buf[3] = (uint8_t)((val >> 0) & 0xff);
19985289e9dSPhilippe Reynes }
20085289e9dSPhilippe Reynes
20185289e9dSPhilippe Reynes /**
20285289e9dSPhilippe Reynes * mask_generation_function1() - generate an octet string
20385289e9dSPhilippe Reynes *
20485289e9dSPhilippe Reynes * Generate an octet string used to check rsa signature.
20585289e9dSPhilippe Reynes * It use an input octet string and a hash function.
20685289e9dSPhilippe Reynes *
20785289e9dSPhilippe Reynes * @checksum: A Hash function
20885289e9dSPhilippe Reynes * @seed: Specifies an input variable octet string
20985289e9dSPhilippe Reynes * @seed_len: Size of the input octet string
21085289e9dSPhilippe Reynes * @output: Specifies the output octet string
21185289e9dSPhilippe Reynes * @output_len: Size of the output octet string
21285289e9dSPhilippe Reynes * @return 0 if the octet string was correctly generated, others on error
21385289e9dSPhilippe Reynes */
mask_generation_function1(struct checksum_algo * checksum,uint8_t * seed,int seed_len,uint8_t * output,int output_len)21485289e9dSPhilippe Reynes static int mask_generation_function1(struct checksum_algo *checksum,
21585289e9dSPhilippe Reynes uint8_t *seed, int seed_len,
21685289e9dSPhilippe Reynes uint8_t *output, int output_len)
21785289e9dSPhilippe Reynes {
21885289e9dSPhilippe Reynes struct image_region region[2];
21985289e9dSPhilippe Reynes int ret = 0, i, i_output = 0, region_count = 2;
22085289e9dSPhilippe Reynes uint32_t counter = 0;
22185289e9dSPhilippe Reynes uint8_t buf_counter[4], *tmp;
22285289e9dSPhilippe Reynes int hash_len = checksum->checksum_len;
22385289e9dSPhilippe Reynes
22485289e9dSPhilippe Reynes memset(output, 0, output_len);
22585289e9dSPhilippe Reynes
22685289e9dSPhilippe Reynes region[0].data = seed;
22785289e9dSPhilippe Reynes region[0].size = seed_len;
22885289e9dSPhilippe Reynes region[1].data = &buf_counter[0];
22985289e9dSPhilippe Reynes region[1].size = 4;
23085289e9dSPhilippe Reynes
23185289e9dSPhilippe Reynes tmp = malloc(hash_len);
23285289e9dSPhilippe Reynes if (!tmp) {
23385289e9dSPhilippe Reynes debug("%s: can't allocate array tmp\n", __func__);
23485289e9dSPhilippe Reynes ret = -ENOMEM;
23585289e9dSPhilippe Reynes goto out;
23685289e9dSPhilippe Reynes }
23785289e9dSPhilippe Reynes
23885289e9dSPhilippe Reynes while (i_output < output_len) {
23985289e9dSPhilippe Reynes u32_i2osp(counter, &buf_counter[0]);
24085289e9dSPhilippe Reynes
24185289e9dSPhilippe Reynes ret = checksum->calculate(checksum->name,
24285289e9dSPhilippe Reynes region, region_count,
24385289e9dSPhilippe Reynes tmp);
24485289e9dSPhilippe Reynes if (ret < 0) {
24585289e9dSPhilippe Reynes debug("%s: Error in checksum calculation\n", __func__);
24685289e9dSPhilippe Reynes goto out;
24785289e9dSPhilippe Reynes }
24885289e9dSPhilippe Reynes
24985289e9dSPhilippe Reynes i = 0;
25085289e9dSPhilippe Reynes while ((i_output < output_len) && (i < hash_len)) {
25185289e9dSPhilippe Reynes output[i_output] = tmp[i];
25285289e9dSPhilippe Reynes i_output++;
25385289e9dSPhilippe Reynes i++;
25485289e9dSPhilippe Reynes }
25585289e9dSPhilippe Reynes
25685289e9dSPhilippe Reynes counter++;
25785289e9dSPhilippe Reynes }
25885289e9dSPhilippe Reynes
25985289e9dSPhilippe Reynes out:
26085289e9dSPhilippe Reynes free(tmp);
26185289e9dSPhilippe Reynes
26285289e9dSPhilippe Reynes return ret;
26385289e9dSPhilippe Reynes }
26485289e9dSPhilippe Reynes
compute_hash_prime(struct checksum_algo * checksum,uint8_t * pad,int pad_len,uint8_t * hash,int hash_len,uint8_t * salt,int salt_len,uint8_t * hprime)26585289e9dSPhilippe Reynes static int compute_hash_prime(struct checksum_algo *checksum,
26685289e9dSPhilippe Reynes uint8_t *pad, int pad_len,
26785289e9dSPhilippe Reynes uint8_t *hash, int hash_len,
26885289e9dSPhilippe Reynes uint8_t *salt, int salt_len,
26985289e9dSPhilippe Reynes uint8_t *hprime)
27085289e9dSPhilippe Reynes {
27185289e9dSPhilippe Reynes struct image_region region[3];
27285289e9dSPhilippe Reynes int ret, region_count = 3;
27385289e9dSPhilippe Reynes
27485289e9dSPhilippe Reynes region[0].data = pad;
27585289e9dSPhilippe Reynes region[0].size = pad_len;
27685289e9dSPhilippe Reynes region[1].data = hash;
27785289e9dSPhilippe Reynes region[1].size = hash_len;
27885289e9dSPhilippe Reynes region[2].data = salt;
27985289e9dSPhilippe Reynes region[2].size = salt_len;
28085289e9dSPhilippe Reynes
28185289e9dSPhilippe Reynes ret = checksum->calculate(checksum->name, region, region_count, hprime);
28285289e9dSPhilippe Reynes if (ret < 0) {
28385289e9dSPhilippe Reynes debug("%s: Error in checksum calculation\n", __func__);
28485289e9dSPhilippe Reynes goto out;
28585289e9dSPhilippe Reynes }
28685289e9dSPhilippe Reynes
28785289e9dSPhilippe Reynes out:
28885289e9dSPhilippe Reynes return ret;
28985289e9dSPhilippe Reynes }
29085289e9dSPhilippe Reynes
padding_pss_verify(struct image_sign_info * info,uint8_t * msg,int msg_len,const uint8_t * hash,int hash_len)29185289e9dSPhilippe Reynes int padding_pss_verify(struct image_sign_info *info,
29285289e9dSPhilippe Reynes uint8_t *msg, int msg_len,
29385289e9dSPhilippe Reynes const uint8_t *hash, int hash_len)
29485289e9dSPhilippe Reynes {
29585289e9dSPhilippe Reynes uint8_t *masked_db = NULL;
29685289e9dSPhilippe Reynes int masked_db_len = msg_len - hash_len - 1;
29785289e9dSPhilippe Reynes uint8_t *h = NULL, *hprime = NULL;
29885289e9dSPhilippe Reynes int h_len = hash_len;
29985289e9dSPhilippe Reynes uint8_t *db_mask = NULL;
30085289e9dSPhilippe Reynes int db_mask_len = masked_db_len;
30185289e9dSPhilippe Reynes uint8_t *db = NULL, *salt = NULL;
30285289e9dSPhilippe Reynes int db_len = masked_db_len, salt_len = msg_len - hash_len - 2;
30385289e9dSPhilippe Reynes uint8_t pad_zero[8] = { 0 };
30485289e9dSPhilippe Reynes int ret, i, leftmost_bits = 1;
30585289e9dSPhilippe Reynes uint8_t leftmost_mask;
30685289e9dSPhilippe Reynes struct checksum_algo *checksum = info->checksum;
30785289e9dSPhilippe Reynes
30885289e9dSPhilippe Reynes /* first, allocate everything */
30985289e9dSPhilippe Reynes masked_db = malloc(masked_db_len);
31085289e9dSPhilippe Reynes h = malloc(h_len);
31185289e9dSPhilippe Reynes db_mask = malloc(db_mask_len);
31285289e9dSPhilippe Reynes db = malloc(db_len);
31385289e9dSPhilippe Reynes salt = malloc(salt_len);
31485289e9dSPhilippe Reynes hprime = malloc(hash_len);
31585289e9dSPhilippe Reynes if (!masked_db || !h || !db_mask || !db || !salt || !hprime) {
31685289e9dSPhilippe Reynes printf("%s: can't allocate some buffer\n", __func__);
31785289e9dSPhilippe Reynes ret = -ENOMEM;
31885289e9dSPhilippe Reynes goto out;
31985289e9dSPhilippe Reynes }
32085289e9dSPhilippe Reynes
32185289e9dSPhilippe Reynes /* step 4: check if the last byte is 0xbc */
32285289e9dSPhilippe Reynes if (msg[msg_len - 1] != 0xbc) {
32385289e9dSPhilippe Reynes printf("%s: invalid pss padding (0xbc is missing)\n", __func__);
32485289e9dSPhilippe Reynes ret = -EINVAL;
32585289e9dSPhilippe Reynes goto out;
32685289e9dSPhilippe Reynes }
32785289e9dSPhilippe Reynes
32885289e9dSPhilippe Reynes /* step 5 */
32985289e9dSPhilippe Reynes memcpy(masked_db, msg, masked_db_len);
33085289e9dSPhilippe Reynes memcpy(h, msg + masked_db_len, h_len);
33185289e9dSPhilippe Reynes
33285289e9dSPhilippe Reynes /* step 6 */
33385289e9dSPhilippe Reynes leftmost_mask = (0xff >> (8 - leftmost_bits)) << (8 - leftmost_bits);
33485289e9dSPhilippe Reynes if (masked_db[0] & leftmost_mask) {
33585289e9dSPhilippe Reynes printf("%s: invalid pss padding ", __func__);
33685289e9dSPhilippe Reynes printf("(leftmost bit of maskedDB not zero)\n");
33785289e9dSPhilippe Reynes ret = -EINVAL;
33885289e9dSPhilippe Reynes goto out;
33985289e9dSPhilippe Reynes }
34085289e9dSPhilippe Reynes
34185289e9dSPhilippe Reynes /* step 7 */
34285289e9dSPhilippe Reynes mask_generation_function1(checksum, h, h_len, db_mask, db_mask_len);
34385289e9dSPhilippe Reynes
34485289e9dSPhilippe Reynes /* step 8 */
34585289e9dSPhilippe Reynes for (i = 0; i < db_len; i++)
34685289e9dSPhilippe Reynes db[i] = masked_db[i] ^ db_mask[i];
34785289e9dSPhilippe Reynes
34885289e9dSPhilippe Reynes /* step 9 */
34985289e9dSPhilippe Reynes db[0] &= 0xff >> leftmost_bits;
35085289e9dSPhilippe Reynes
35185289e9dSPhilippe Reynes /* step 10 */
35285289e9dSPhilippe Reynes if (db[0] != 0x01) {
35385289e9dSPhilippe Reynes printf("%s: invalid pss padding ", __func__);
35485289e9dSPhilippe Reynes printf("(leftmost byte of db isn't 0x01)\n");
35585289e9dSPhilippe Reynes ret = EINVAL;
35685289e9dSPhilippe Reynes goto out;
35785289e9dSPhilippe Reynes }
35885289e9dSPhilippe Reynes
35985289e9dSPhilippe Reynes /* step 11 */
36085289e9dSPhilippe Reynes memcpy(salt, &db[1], salt_len);
36185289e9dSPhilippe Reynes
36285289e9dSPhilippe Reynes /* step 12 & 13 */
36385289e9dSPhilippe Reynes compute_hash_prime(checksum, pad_zero, 8,
36485289e9dSPhilippe Reynes (uint8_t *)hash, hash_len,
36585289e9dSPhilippe Reynes salt, salt_len, hprime);
36685289e9dSPhilippe Reynes
36785289e9dSPhilippe Reynes /* step 14 */
36885289e9dSPhilippe Reynes ret = memcmp(h, hprime, hash_len);
36985289e9dSPhilippe Reynes
37085289e9dSPhilippe Reynes out:
37185289e9dSPhilippe Reynes free(hprime);
37285289e9dSPhilippe Reynes free(salt);
37385289e9dSPhilippe Reynes free(db);
37485289e9dSPhilippe Reynes free(db_mask);
37585289e9dSPhilippe Reynes free(h);
37685289e9dSPhilippe Reynes free(masked_db);
37785289e9dSPhilippe Reynes
37885289e9dSPhilippe Reynes return ret;
37985289e9dSPhilippe Reynes }
38085289e9dSPhilippe Reynes #endif
38185289e9dSPhilippe Reynes
382da29f299SAndrew Duda /**
383fc2f4246SRuchika Gupta * rsa_verify_key() - Verify a signature against some data using RSA Key
38419c402afSSimon Glass *
385fc2f4246SRuchika Gupta * Verify a RSA PKCS1.5 signature against an expected hash using
386fc2f4246SRuchika Gupta * the RSA Key properties in prop structure.
387fc2f4246SRuchika Gupta *
388219050bfSPhilippe Reynes * @info: Specifies key and FIT information
389fc2f4246SRuchika Gupta * @prop: Specifies key
390fc2f4246SRuchika Gupta * @sig: Signature
391fc2f4246SRuchika Gupta * @sig_len: Number of bytes in signature
392fc2f4246SRuchika Gupta * @hash: Pointer to the expected hash
3930c1d74fdSAndrew Duda * @key_len: Number of bytes in rsa key
394fc2f4246SRuchika Gupta * @return 0 if verified, -ve on error
39519c402afSSimon Glass */
rsa_verify_key(struct image_sign_info * info,struct key_prop * prop,const uint8_t * sig,const uint32_t sig_len,const uint8_t * hash,const uint32_t key_len)396219050bfSPhilippe Reynes static int rsa_verify_key(struct image_sign_info *info,
397219050bfSPhilippe Reynes struct key_prop *prop, const uint8_t *sig,
398646257d1SHeiko Schocher const uint32_t sig_len, const uint8_t *hash,
399219050bfSPhilippe Reynes const uint32_t key_len)
40019c402afSSimon Glass {
40119c402afSSimon Glass int ret;
402219050bfSPhilippe Reynes struct checksum_algo *checksum = info->checksum;
403219050bfSPhilippe Reynes struct padding_algo *padding = info->padding;
404219050bfSPhilippe Reynes int hash_len = checksum->checksum_len;
40519c402afSSimon Glass
406219050bfSPhilippe Reynes if (!prop || !sig || !hash || !checksum)
40719c402afSSimon Glass return -EIO;
40819c402afSSimon Glass
409fc2f4246SRuchika Gupta if (sig_len != (prop->num_bits / 8)) {
41019c402afSSimon Glass debug("Signature is of incorrect length %d\n", sig_len);
41119c402afSSimon Glass return -EINVAL;
41219c402afSSimon Glass }
41319c402afSSimon Glass
414219050bfSPhilippe Reynes debug("Checksum algorithm: %s", checksum->name);
415646257d1SHeiko Schocher
41619c402afSSimon Glass /* Sanity check for stack size */
41719c402afSSimon Glass if (sig_len > RSA_MAX_SIG_BITS / 8) {
41819c402afSSimon Glass debug("Signature length %u exceeds maximum %d\n", sig_len,
41919c402afSSimon Glass RSA_MAX_SIG_BITS / 8);
42019c402afSSimon Glass return -EINVAL;
42119c402afSSimon Glass }
42219c402afSSimon Glass
423fc2f4246SRuchika Gupta uint8_t buf[sig_len];
42419c402afSSimon Glass
425c937ff6dSRuchika Gupta #if !defined(USE_HOSTCC)
426008ec9b4SJoseph Chen #if CONFIG_IS_ENABLED(FIT_HW_CRYPTO)
4270fb93272SJoseph Chen ret = rsa_mod_exp_hw(prop, sig, sig_len, key_len, buf);
428008ec9b4SJoseph Chen #else
429008ec9b4SJoseph Chen struct udevice *mod_exp_dev;
430008ec9b4SJoseph Chen
431c937ff6dSRuchika Gupta ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
432c937ff6dSRuchika Gupta if (ret) {
433c937ff6dSRuchika Gupta printf("RSA: Can't find Modular Exp implementation\n");
434c937ff6dSRuchika Gupta return -EINVAL;
435c937ff6dSRuchika Gupta }
436c937ff6dSRuchika Gupta
437c937ff6dSRuchika Gupta ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf);
438008ec9b4SJoseph Chen #endif
439c937ff6dSRuchika Gupta #else
440fc2f4246SRuchika Gupta ret = rsa_mod_exp_sw(sig, sig_len, prop, buf);
441c937ff6dSRuchika Gupta #endif
442fc2f4246SRuchika Gupta if (ret) {
443fc2f4246SRuchika Gupta debug("Error in Modular exponentation\n");
44419c402afSSimon Glass return ret;
445fc2f4246SRuchika Gupta }
44619c402afSSimon Glass
447219050bfSPhilippe Reynes ret = padding->verify(info, buf, key_len, hash, hash_len);
448da29f299SAndrew Duda if (ret) {
449219050bfSPhilippe Reynes debug("In RSAVerify(): padding check failed!\n");
450219050bfSPhilippe Reynes return ret;
45119c402afSSimon Glass }
45219c402afSSimon Glass
45319c402afSSimon Glass return 0;
45419c402afSSimon Glass }
45519c402afSSimon Glass
rsa_get_key_prop(struct key_prop * prop,struct image_sign_info * info,int node)45678263d89SJason Zhu static int rsa_get_key_prop(struct key_prop *prop, struct image_sign_info *info, int node)
45778263d89SJason Zhu {
45878263d89SJason Zhu const void *blob = info->fdt_blob;
45978263d89SJason Zhu int length;
46078263d89SJason Zhu int hash_node;
46178263d89SJason Zhu
46278263d89SJason Zhu if (node < 0) {
46378263d89SJason Zhu debug("%s: Skipping invalid node", __func__);
46478263d89SJason Zhu return -EBADF;
46578263d89SJason Zhu }
46678263d89SJason Zhu
46778263d89SJason Zhu if (!prop) {
46878263d89SJason Zhu debug("%s: The prop is NULL", __func__);
46978263d89SJason Zhu return -EBADF;
47078263d89SJason Zhu }
47178263d89SJason Zhu
47278263d89SJason Zhu prop->burn_key = fdtdec_get_int(blob, node, "burn-key-hash", 0);
47378263d89SJason Zhu
47478263d89SJason Zhu prop->num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
47578263d89SJason Zhu
47678263d89SJason Zhu prop->n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
47778263d89SJason Zhu
47878263d89SJason Zhu prop->public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length);
47978263d89SJason Zhu if (!prop->public_exponent || length < sizeof(uint64_t))
48078263d89SJason Zhu prop->public_exponent = NULL;
48178263d89SJason Zhu
48278263d89SJason Zhu prop->exp_len = sizeof(uint64_t);
48378263d89SJason Zhu prop->modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
48478263d89SJason Zhu prop->public_exponent_BN = fdt_getprop(blob, node, "rsa,exponent-BN", NULL);
48578263d89SJason Zhu prop->rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
48678263d89SJason Zhu #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
48778263d89SJason Zhu hash_node = fdt_subnode_offset(blob, node, "hash@c");
48878263d89SJason Zhu #else
48978263d89SJason Zhu hash_node = fdt_subnode_offset(blob, node, "hash@np");
49078263d89SJason Zhu #endif
49178263d89SJason Zhu if (hash_node >= 0)
49278263d89SJason Zhu prop->hash = fdt_getprop(blob, hash_node, "value", NULL);
49378263d89SJason Zhu
49478263d89SJason Zhu if (!prop->num_bits || !prop->modulus) {
49578263d89SJason Zhu debug("%s: Missing RSA key info", __func__);
49678263d89SJason Zhu return -EFAULT;
49778263d89SJason Zhu }
49878263d89SJason Zhu
49978263d89SJason Zhu #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
50078263d89SJason Zhu prop->factor_c = fdt_getprop(blob, node, "rsa,c", NULL);
50178263d89SJason Zhu if (!prop.factor_c)
50278263d89SJason Zhu return -EFAULT;
50378263d89SJason Zhu #else
50478263d89SJason Zhu prop->factor_np = fdt_getprop(blob, node, "rsa,np", NULL);
50578263d89SJason Zhu if (!prop->factor_np)
50678263d89SJason Zhu return -EFAULT;
50778263d89SJason Zhu #endif
50878263d89SJason Zhu
50978263d89SJason Zhu return 0;
51078263d89SJason Zhu }
51178263d89SJason Zhu
512fc2f4246SRuchika Gupta /**
513fc2f4246SRuchika Gupta * rsa_verify_with_keynode() - Verify a signature against some data using
514fc2f4246SRuchika Gupta * information in node with prperties of RSA Key like modulus, exponent etc.
515fc2f4246SRuchika Gupta *
516fc2f4246SRuchika Gupta * Parse sign-node and fill a key_prop structure with properties of the
517fc2f4246SRuchika Gupta * key. Verify a RSA PKCS1.5 signature against an expected hash using
518fc2f4246SRuchika Gupta * the properties parsed
519fc2f4246SRuchika Gupta *
520fc2f4246SRuchika Gupta * @info: Specifies key and FIT information
521fc2f4246SRuchika Gupta * @hash: Pointer to the expected hash
522fc2f4246SRuchika Gupta * @sig: Signature
523fc2f4246SRuchika Gupta * @sig_len: Number of bytes in signature
524fc2f4246SRuchika Gupta * @node: Node having the RSA Key properties
525fc2f4246SRuchika Gupta * @return 0 if verified, -ve on error
526fc2f4246SRuchika Gupta */
rsa_verify_with_keynode(struct image_sign_info * info,const void * hash,uint8_t * sig,uint sig_len,int node)52719c402afSSimon Glass static int rsa_verify_with_keynode(struct image_sign_info *info,
528fc2f4246SRuchika Gupta const void *hash, uint8_t *sig,
529fc2f4246SRuchika Gupta uint sig_len, int node)
53019c402afSSimon Glass {
531fc2f4246SRuchika Gupta struct key_prop prop;
53219c402afSSimon Glass
53378263d89SJason Zhu if (rsa_get_key_prop(&prop, info, node))
53419c402afSSimon Glass return -EFAULT;
53519c402afSSimon Glass
53678263d89SJason Zhu return rsa_verify_key(info, &prop, sig, sig_len, hash,
537219050bfSPhilippe Reynes info->crypto->key_len);
53819c402afSSimon Glass }
53919c402afSSimon Glass
rsa_verify(struct image_sign_info * info,const struct image_region region[],int region_count,uint8_t * sig,uint sig_len)54019c402afSSimon Glass int rsa_verify(struct image_sign_info *info,
54119c402afSSimon Glass const struct image_region region[], int region_count,
54219c402afSSimon Glass uint8_t *sig, uint sig_len)
54319c402afSSimon Glass {
54419c402afSSimon Glass const void *blob = info->fdt_blob;
545646257d1SHeiko Schocher /* Reserve memory for maximum checksum-length */
54683dd98e0SAndrew Duda uint8_t hash[info->crypto->key_len];
54719c402afSSimon Glass int ndepth, noffset;
54819c402afSSimon Glass int sig_node, node;
54919c402afSSimon Glass char name[100];
550646257d1SHeiko Schocher int ret;
551646257d1SHeiko Schocher
552646257d1SHeiko Schocher /*
553646257d1SHeiko Schocher * Verify that the checksum-length does not exceed the
554646257d1SHeiko Schocher * rsa-signature-length
555646257d1SHeiko Schocher */
55683dd98e0SAndrew Duda if (info->checksum->checksum_len >
55783dd98e0SAndrew Duda info->crypto->key_len) {
558db1b5f3dSHeiko Schocher debug("%s: invlaid checksum-algorithm %s for %s\n",
55983dd98e0SAndrew Duda __func__, info->checksum->name, info->crypto->name);
560646257d1SHeiko Schocher return -EINVAL;
561646257d1SHeiko Schocher }
56219c402afSSimon Glass
56319c402afSSimon Glass sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
56419c402afSSimon Glass if (sig_node < 0) {
56519c402afSSimon Glass debug("%s: No signature node found\n", __func__);
56619c402afSSimon Glass return -ENOENT;
56719c402afSSimon Glass }
56819c402afSSimon Glass
569646257d1SHeiko Schocher /* Calculate checksum with checksum-algorithm */
57083dd98e0SAndrew Duda ret = info->checksum->calculate(info->checksum->name,
571b37b46f0SRuchika Gupta region, region_count, hash);
572b37b46f0SRuchika Gupta if (ret < 0) {
573b37b46f0SRuchika Gupta debug("%s: Error in checksum calculation\n", __func__);
574b37b46f0SRuchika Gupta return -EINVAL;
575b37b46f0SRuchika Gupta }
57619c402afSSimon Glass
57719c402afSSimon Glass /* See if we must use a particular key */
57819c402afSSimon Glass if (info->required_keynode != -1) {
57919c402afSSimon Glass ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
58019c402afSSimon Glass info->required_keynode);
58119c402afSSimon Glass if (!ret)
58219c402afSSimon Glass return ret;
58319c402afSSimon Glass }
58419c402afSSimon Glass
58519c402afSSimon Glass /* Look for a key that matches our hint */
58619c402afSSimon Glass snprintf(name, sizeof(name), "key-%s", info->keyname);
58719c402afSSimon Glass node = fdt_subnode_offset(blob, sig_node, name);
58819c402afSSimon Glass ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
58919c402afSSimon Glass if (!ret)
59019c402afSSimon Glass return ret;
59119c402afSSimon Glass
59219c402afSSimon Glass /* No luck, so try each of the keys in turn */
59319c402afSSimon Glass for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth);
59419c402afSSimon Glass (noffset >= 0) && (ndepth > 0);
59519c402afSSimon Glass noffset = fdt_next_node(info->fit, noffset, &ndepth)) {
59619c402afSSimon Glass if (ndepth == 1 && noffset != node) {
59719c402afSSimon Glass ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
59819c402afSSimon Glass noffset);
59919c402afSSimon Glass if (!ret)
60019c402afSSimon Glass break;
60119c402afSSimon Glass }
60219c402afSSimon Glass }
60319c402afSSimon Glass
60419c402afSSimon Glass return ret;
60519c402afSSimon Glass }
60678263d89SJason Zhu
60778263d89SJason Zhu #if !defined(USE_HOSTCC)
608c2b76562SJoseph Chen #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_FIT_HW_CRYPTO) && \
609c2b76562SJoseph Chen defined(CONFIG_SPL_ROCKCHIP_SECURE_OTP)
rsa_burn_key_hash(struct image_sign_info * info)61078263d89SJason Zhu int rsa_burn_key_hash(struct image_sign_info *info)
61178263d89SJason Zhu {
61278263d89SJason Zhu char *rsa_key;
61378263d89SJason Zhu void *n, *e, *c;
61478263d89SJason Zhu uint32_t key_len;
61578263d89SJason Zhu struct udevice *dev;
61678263d89SJason Zhu struct key_prop prop;
61778263d89SJason Zhu char name[100] = {0};
61859992cdcSXuhui Lin uint8_t otp_write;
61978263d89SJason Zhu const void *blob = info->fdt_blob;
620f8e13adfSXuhui Lin uint8_t digest_write[FIT_MAX_HASH_LEN];
62159992cdcSXuhui Lin int sig_node, node, digest_len;
62259992cdcSXuhui Lin int ret = 0;
62378263d89SJason Zhu
6242285b68dSXuhui Lin /* Check burn-key-hash flag in itb first */
62578263d89SJason Zhu sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
62678263d89SJason Zhu if (sig_node < 0) {
62778263d89SJason Zhu debug("%s: No signature node found\n", __func__);
62878263d89SJason Zhu return -ENOENT;
62978263d89SJason Zhu }
63078263d89SJason Zhu
63178263d89SJason Zhu snprintf(name, sizeof(name), "key-%s", info->keyname);
63278263d89SJason Zhu node = fdt_subnode_offset(blob, sig_node, name);
63378263d89SJason Zhu
63478263d89SJason Zhu if (rsa_get_key_prop(&prop, info, node))
63578263d89SJason Zhu return -1;
63678263d89SJason Zhu
63778263d89SJason Zhu if (!(prop.burn_key))
6382285b68dSXuhui Lin return 0;
6392285b68dSXuhui Lin
6402285b68dSXuhui Lin /* Handle burn_key_hash process from now on */
6412285b68dSXuhui Lin dev = misc_otp_get_device(OTP_S);
6422285b68dSXuhui Lin if (!dev)
6432285b68dSXuhui Lin return -ENODEV;
6442285b68dSXuhui Lin
645*60bee396SXuhui Lin #if !defined(CONFIG_SPL_REVOKE_PUB_KEY)
646*60bee396SXuhui Lin u16 secure_flags_read;
647*60bee396SXuhui Lin
6482285b68dSXuhui Lin ret = misc_otp_read(dev, OTP_SECURE_BOOT_ENABLE_ADDR,
649f8e13adfSXuhui Lin &secure_flags_read, OTP_SECURE_BOOT_ENABLE_SIZE);
6502285b68dSXuhui Lin if (ret)
6512285b68dSXuhui Lin return ret;
6522285b68dSXuhui Lin
653f8e13adfSXuhui Lin if ((secure_flags_read & 0xff) == 0xff)
6542285b68dSXuhui Lin return 0;
655*60bee396SXuhui Lin #endif
65678263d89SJason Zhu
65778263d89SJason Zhu if (!prop.hash || !prop.modulus || !prop.public_exponent_BN)
65878263d89SJason Zhu return -ENOENT;
65978263d89SJason Zhu #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
66078263d89SJason Zhu if (!prop.factor_c)
66178263d89SJason Zhu return -ENOENT;
66278263d89SJason Zhu #else
66378263d89SJason Zhu if (!prop.factor_np)
66478263d89SJason Zhu return -ENOENT;
66578263d89SJason Zhu #endif
66678263d89SJason Zhu key_len = info->crypto->key_len;
667f8e13adfSXuhui Lin
668f8e13adfSXuhui Lin if ((key_len != RSA4096_BYTES) && (key_len != RSA2048_BYTES))
66978263d89SJason Zhu return -EINVAL;
67078263d89SJason Zhu
6719c63859fSJason Zhu rsa_key = calloc(key_len * 3, sizeof(char));
67278263d89SJason Zhu if (!rsa_key)
67378263d89SJason Zhu return -ENOMEM;
67478263d89SJason Zhu
67578263d89SJason Zhu n = rsa_key;
6769c63859fSJason Zhu e = rsa_key + CONFIG_RSA_N_SIZE;
6779c63859fSJason Zhu c = rsa_key + CONFIG_RSA_N_SIZE + CONFIG_RSA_E_SIZE;
67878263d89SJason Zhu rsa_convert_big_endian(n, (uint32_t *)prop.modulus,
6799c63859fSJason Zhu key_len, CONFIG_RSA_N_SIZE);
68078263d89SJason Zhu rsa_convert_big_endian(e, (uint32_t *)prop.public_exponent_BN,
6819c63859fSJason Zhu key_len, CONFIG_RSA_E_SIZE);
68278263d89SJason Zhu #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
68378263d89SJason Zhu rsa_convert_big_endian(c, (uint32_t *)prop.factor_c,
6849c63859fSJason Zhu key_len, CONFIG_RSA_C_SIZE);
68578263d89SJason Zhu #else
68678263d89SJason Zhu rsa_convert_big_endian(c, (uint32_t *)prop.factor_np,
6879c63859fSJason Zhu key_len, CONFIG_RSA_C_SIZE);
68878263d89SJason Zhu #endif
68978263d89SJason Zhu
690f8e13adfSXuhui Lin /* Calculate and compare key hash */
6919c63859fSJason Zhu ret = calculate_hash(rsa_key, CONFIG_RSA_N_SIZE + CONFIG_RSA_E_SIZE + CONFIG_RSA_C_SIZE,
692f8e13adfSXuhui Lin info->checksum->name, digest_write, &digest_len);
69378263d89SJason Zhu if (ret)
69478263d89SJason Zhu goto error;
69578263d89SJason Zhu
696f8e13adfSXuhui Lin if (memcmp(digest_write, prop.hash, digest_len) != 0) {
697bd2c27ccSJason Zhu printf("RSA: Compare public key hash fail.\n");
698f8e13adfSXuhui Lin ret = -EINVAL;
69978263d89SJason Zhu goto error;
70078263d89SJason Zhu }
70178263d89SJason Zhu
702cfb52818SXuhui Lin /* Delay 3 seconds to make sure power supply is steady. */
703cfb52818SXuhui Lin printf("Waiting for power supply steady for OTP write. Please don't turn off the device\n");
704cfb52818SXuhui Lin mdelay(3000);
705cfb52818SXuhui Lin
706*60bee396SXuhui Lin #if defined(CONFIG_SPL_REVOKE_PUB_KEY)
707*60bee396SXuhui Lin /* Burn next key hash here */
708*60bee396SXuhui Lin if (misc_otp_write_verify(dev, OTP_NEXT_RSA_HASH_ADDR, digest_write,
709*60bee396SXuhui Lin OTP_NEXT_RSA_HASH_SIZE)) {
710*60bee396SXuhui Lin printf("RSA: Write next public key hash fail.\n");
711*60bee396SXuhui Lin ret = -EIO;
712*60bee396SXuhui Lin goto error;
713*60bee396SXuhui Lin } else {
714*60bee396SXuhui Lin printf("RSA: Write next RSA key hash successfully.\n");
715*60bee396SXuhui Lin }
716*60bee396SXuhui Lin #else
717f8e13adfSXuhui Lin /* Burn key hash here */
71859992cdcSXuhui Lin if (misc_otp_write_verify(dev, OTP_RSA_HASH_ADDR, digest_write, OTP_RSA_HASH_SIZE)) {
71959992cdcSXuhui Lin printf("RSA: Write public key hash fail.\n");
72078263d89SJason Zhu ret = -EIO;
72178263d89SJason Zhu goto error;
72259992cdcSXuhui Lin } else {
723b77c257eSXuhui Lin printf("RSA: Write RSA key hash successfully.\n");
72459992cdcSXuhui Lin }
725*60bee396SXuhui Lin #endif
726f8e13adfSXuhui Lin /*
727f8e13adfSXuhui Lin * For some chips, rsa4096 flag and secureboot flag should be burned together
728f8e13adfSXuhui Lin * because of ecc enable. OTP_RSA4096_ENABLE_ADDR won't defined for burning
729f8e13adfSXuhui Lin * these two flags only once.
730f8e13adfSXuhui Lin */
731f8e13adfSXuhui Lin #if defined(CONFIG_FIT_ENABLE_RSA4096_SUPPORT) && defined(OTP_RSA4096_ENABLE_ADDR)
732f8e13adfSXuhui Lin /* Burn rsa4096 flag here */
73359992cdcSXuhui Lin otp_write = OTP_RSA4096_ENABLE_VALUE;
73459992cdcSXuhui Lin if (misc_otp_write_verify(dev, OTP_RSA4096_ENABLE_ADDR, &otp_write,
73559992cdcSXuhui Lin OTP_RSA4096_ENABLE_SIZE)) {
736f8e13adfSXuhui Lin printf("RSA: Write rsa4096 flag fail.\n");
73759992cdcSXuhui Lin ret = -EIO;
738f8e13adfSXuhui Lin goto error;
73959992cdcSXuhui Lin } else {
740f8e13adfSXuhui Lin printf("RSA: Write rsa4096 flag successfully.\n");
74159992cdcSXuhui Lin }
742f8e13adfSXuhui Lin #endif
743f8e13adfSXuhui Lin
744*60bee396SXuhui Lin #if defined(CONFIG_SPL_REVOKE_PUB_KEY)
745*60bee396SXuhui Lin /* Burn revoke key config here */
746*60bee396SXuhui Lin otp_write = OTP_RSA_HASH_REVOKE_VAL;
747*60bee396SXuhui Lin if (misc_otp_write_verify(dev, OTP_RSA_HASH_REVOKE_ADDR, &otp_write,
748*60bee396SXuhui Lin OTP_RSA_HASH_REVOKE_SIZE)) {
749*60bee396SXuhui Lin printf("RSA: Write revoke key config fail.\n");
750*60bee396SXuhui Lin ret = -EIO;
751*60bee396SXuhui Lin goto error;
752*60bee396SXuhui Lin } else {
753*60bee396SXuhui Lin printf("RSA: Write revoke key config successfully.\n");
754*60bee396SXuhui Lin }
755*60bee396SXuhui Lin #else
756f8e13adfSXuhui Lin /* Burn secure flag here */
75759992cdcSXuhui Lin otp_write = OTP_SECURE_BOOT_ENABLE_VALUE;
75859992cdcSXuhui Lin if (misc_otp_write_verify(dev, OTP_SECURE_BOOT_ENABLE_ADDR, &otp_write,
75959992cdcSXuhui Lin OTP_SECURE_BOOT_ENABLE_SIZE)) {
760f8e13adfSXuhui Lin printf("RSA: Write secure flag fail.\n");
76159992cdcSXuhui Lin ret = -EIO;
762f8e13adfSXuhui Lin goto error;
76359992cdcSXuhui Lin } else {
764f8e13adfSXuhui Lin printf("RSA: Write secure flag successfully.\n");
76559992cdcSXuhui Lin }
766*60bee396SXuhui Lin #endif
767f8e13adfSXuhui Lin
76878263d89SJason Zhu error:
76978263d89SJason Zhu free(rsa_key);
77078263d89SJason Zhu
77178263d89SJason Zhu return ret;
77278263d89SJason Zhu }
7738e2679f6SXuhui Lin
7748e2679f6SXuhui Lin #if defined(CONFIG_SPL_OTP_DISABLE_SD) || defined(CONFIG_SPL_OTP_DISABLE_USB) || \
7758e2679f6SXuhui Lin defined(CONFIG_SPL_OTP_DISABLE_UART) || defined(CONFIG_SPL_OTP_DISABLE_SPI2APB)
7768e2679f6SXuhui Lin typedef struct {
7778e2679f6SXuhui Lin unsigned long addr;
7788e2679f6SXuhui Lin uint8_t value;
7798e2679f6SXuhui Lin char *name;
7808e2679f6SXuhui Lin } OtpUpgrade;
7818e2679f6SXuhui Lin
rsa_burn_disable_upgrade(void)7828e2679f6SXuhui Lin int rsa_burn_disable_upgrade(void)
7838e2679f6SXuhui Lin {
7848e2679f6SXuhui Lin OtpUpgrade upgrade[] = {
7858e2679f6SXuhui Lin #if defined(CONFIG_SPL_OTP_DISABLE_USB)
7868e2679f6SXuhui Lin {OTP_DISABLE_UPGRADE_ADDR, OTP_DISABLE_USB_VAL, "usb"},
7878e2679f6SXuhui Lin #endif
7888e2679f6SXuhui Lin #if defined(CONFIG_SPL_OTP_DISABLE_SD)
7898e2679f6SXuhui Lin {OTP_DISABLE_UPGRADE_ADDR, OTP_DISABLE_SD_VAL, "sd"},
7908e2679f6SXuhui Lin #endif
7918e2679f6SXuhui Lin #if defined(CONFIG_SPL_OTP_DISABLE_UART)
7928e2679f6SXuhui Lin {OTP_DISABLE_UPGRADE_ADDR, OTP_DISABLE_UART_VAL, "uart"},
7938e2679f6SXuhui Lin #endif
7948e2679f6SXuhui Lin #if defined(CONFIG_SPL_OTP_DISABLE_SPI2APB)
7958e2679f6SXuhui Lin {OTP_DISABLE_UPGRADE_ADDR, OTP_DISABLE_SPI2APB_VAL, "spi2apb"},
7968e2679f6SXuhui Lin #endif
7978e2679f6SXuhui Lin };
7988e2679f6SXuhui Lin struct udevice *dev;
7998e2679f6SXuhui Lin uint8_t otp_write;
8008e2679f6SXuhui Lin int ret = 0, i;
8018e2679f6SXuhui Lin
8028e2679f6SXuhui Lin dev = misc_otp_get_device(OTP_S);
8038e2679f6SXuhui Lin if (!dev) {
8048e2679f6SXuhui Lin printf("OTP: No available device\n");
8058e2679f6SXuhui Lin ret = -ENODEV;
8068e2679f6SXuhui Lin goto fail;
8078e2679f6SXuhui Lin }
8088e2679f6SXuhui Lin
8098e2679f6SXuhui Lin for (i = 0; i < sizeof(upgrade)/sizeof(upgrade[0]); i++) {
8108e2679f6SXuhui Lin otp_write = upgrade[i].value;
8118e2679f6SXuhui Lin if (misc_otp_write_verify(dev, upgrade[i].addr, &otp_write, 1)) {
8128e2679f6SXuhui Lin printf("Write OTP to disable %s upgrade failed.\n", upgrade[i].name);
8138e2679f6SXuhui Lin goto fail;
8148e2679f6SXuhui Lin }
8158e2679f6SXuhui Lin printf("Write OTP to disable %s upgrade successfully.\n", upgrade[i].name);
8168e2679f6SXuhui Lin }
8178e2679f6SXuhui Lin
8188e2679f6SXuhui Lin fail:
8198e2679f6SXuhui Lin return ret;
8208e2679f6SXuhui Lin }
8218e2679f6SXuhui Lin #endif
82278263d89SJason Zhu #endif
82378263d89SJason Zhu #endif
824