Skip to content

Commit a39ea4b

Browse files
committed
Add SM4 IV length definition and update decryption logic in k230_boot.c
1 parent c48fa9e commit a39ea4b

2 files changed

Lines changed: 14 additions & 18 deletions

File tree

.github/workflows/build.yml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,6 @@ jobs:
124124
# Also copy artifacts to artifacts folder
125125
cp ${{ github.workspace }}/canmv_k230/output/${UBOOT_DEFCONFIG}/uboot/u-boot.bin ~/artifacts/${base_name}/u-boot.bin || { echo "u-boot.bin missing"; exit $?; }
126126
cp ${{ github.workspace }}/canmv_k230/output/${UBOOT_DEFCONFIG}/uboot/spl/u-boot-spl.bin ~/artifacts/${base_name}/u-boot-spl.bin || { echo "u-boot-spl.bin missing"; exit $?; }
127-
128-
# build fastboot spl
129-
make uboot-clean || exit $?
130-
echo "CONFIG_UBOOT_ENABLE_FAST_BOOT=y" >> ${{ github.workspace }}/canmv_k230/.config
131-
echo "CONFIG_UBOOT_FAST_BOOT_RTSMART=y" >> ${{ github.workspace }}/canmv_k230/.config
132-
# disable prebuilt
133-
sed -i 's/^CONFIG_UBOOT_USE_PREBUILT=y$/CONFIG_UBOOT_USE_PREBUILT=n/' ${{ github.workspace }}/canmv_k230/.config
134-
135-
make -j8 uboot || exit $?
136-
137-
cp ${{ github.workspace }}/canmv_k230/output/${UBOOT_DEFCONFIG}/uboot/spl/u-boot-spl.bin ~/artifacts/${base_name}/u-boot-spl-fastboot.bin || { echo "u-boot-spl-fastboot missing"; exit $?; }
138127
done
139128
140129
tree ~/artifacts

board/kendryte/common/k230_boot.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ static uint64_t rtapp_load_addr, rtapp_size, rttapp_loaded = 0;
9393
#define K230_DISABLE_NONE_SECURITY_MASK 0x1U
9494
#define K230_GCM_IV_LEN 12U
9595
#define K230_GCM_TAG_LEN 16U
96+
#define K230_SM4_IV_LEN 16U
9697
#define K230_GCM_UPDATE_CHUNK_SIZE 0x10000U
9798
#define K230_DOWNSTREAM_AES_KEY_SLOT OTPKEY_3
9899
#define K230_DOWNSTREAM_RSA_HASH_SLOT OTPKEY_8
@@ -126,11 +127,6 @@ unsigned long k230_get_rttapp_load_addr(void)
126127
return addr & ~(4096-1);
127128
}
128129

129-
static const uint8_t *k230_get_firmware_sm4_iv(void)
130-
{
131-
return k230_firmware_sm4_iv;
132-
}
133-
134130
static bool k230_calc_range_end(ulong start, ulong size, ulong *end)
135131
{
136132
ulong max_value = ~0UL;
@@ -388,7 +384,9 @@ static int k230_boot_check_and_get_plain_data(firmware_head_s *pfh,
388384
uint8_t puk_hash_otp[SHA256_SUM_LEN];
389385
pufs_ec_point_st puk = { .qlen = 32 };
390386
pufs_ecdsa_sig_st sig = { .qlen = 32 };
391-
const uint8_t *sm4_iv = k230_get_firmware_sm4_iv();
387+
const uint8_t *sm4_iv;
388+
const uint8_t *sm4_input;
389+
uint32_t sm4_input_len;
392390

393391
if (pufs_read_otp(puk_hash_otp, sizeof(puk_hash_otp),
394392
(K230_DOWNSTREAM_SM2_HASH_SLOT - OTPKEY_0) * OTP_KEY_LEN) != SUCCESS) {
@@ -423,9 +421,18 @@ static int k230_boot_check_and_get_plain_data(firmware_head_s *pfh,
423421
return 16;
424422
}
425423

424+
if (pfh->length < K230_SM4_IV_LEN) {
425+
printf("sm4 payload too short\n");
426+
return 17;
427+
}
428+
429+
sm4_iv = cipher_data;
430+
sm4_input = cipher_data + K230_SM4_IV_LEN;
431+
sm4_input_len = pfh->length - K230_SM4_IV_LEN;
432+
426433
plain_addr = k230_get_encrypted_image_decrypt_addr();
427434
if (cb_pufs_dec_cbc((uint8_t *)plain_addr, &outlen,
428-
cipher_data, pfh->length,
435+
sm4_input, sm4_input_len,
429436
SM4, OTPKEY, K230_DOWNSTREAM_SM4_KEY_SLOT, 128,
430437
sm4_iv, 0) != SUCCESS) {
431438
printf("sm4 decrypt error\n");

0 commit comments

Comments
 (0)