Commit 8f759f2e authored by Dominik Ermel's avatar Dominik Ermel Committed by Dominik Ermel
Browse files

boot: Replace boot_encrypt by boot_enc_encrypt and boot_enc_decrypt



To be able to implement encryption with API that requires different
calls for encryption and encryption, the boot_encrypt
needs to be replaced with encryption/decryption specific functions.

Signed-off-by: default avatarDominik Ermel <dominik.ermel@nordicsemi.no>
parent c894d047
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -171,7 +171,7 @@ decrypt_region_inplace(struct boot_loader_state *state,
                    blk_sz = tlv_off - (off + bytes_copied);
                }
            }
            boot_encrypt(BOOT_CURR_ENC(state), slot,
            boot_enc_decrypt(BOOT_CURR_ENC(state), slot,
                    (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
                    blk_off, &buf[idx]);
        }
+3 −1
Original line number Diff line number Diff line
@@ -70,7 +70,9 @@ int boot_enc_load(struct enc_key_data *enc_state, int slot,
        const struct image_header *hdr, const struct flash_area *fap,
        struct boot_status *bs);
bool boot_enc_valid(struct enc_key_data *enc_state, int slot);
void boot_encrypt(struct enc_key_data *enc_state, int slot,
void boot_enc_encrypt(struct enc_key_data *enc_state, int slot,
        uint32_t off, uint32_t sz, uint32_t blk_off, uint8_t *buf);
void boot_enc_decrypt(struct enc_key_data *enc_state, int slot,
        uint32_t off, uint32_t sz, uint32_t blk_off, uint8_t *buf);
void boot_enc_zeroize(struct enc_key_data *enc_state);

+26 −5
Original line number Diff line number Diff line
@@ -688,14 +688,13 @@ boot_enc_valid(struct enc_key_data *enc_state, int slot)
}

void
boot_encrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
boot_enc_encrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
             uint32_t sz, uint32_t blk_off, uint8_t *buf)
{
    struct enc_key_data *enc;
    struct enc_key_data *enc = &enc_state[slot];
    uint8_t nonce[16];

    /* boot_copy_region will call boot_encrypt with sz = 0 when skipping over
       the TLVs. */
    /* Nothing to do with size == 0 */
    if (sz == 0) {
       return;
    }
@@ -707,11 +706,33 @@ boot_encrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
    nonce[14] = (uint8_t)(off >> 8);
    nonce[15] = (uint8_t)off;

    enc = &enc_state[slot];
    assert(enc->valid == 1);
    bootutil_aes_ctr_encrypt(&enc->aes_ctr, nonce, buf, sz, blk_off, buf);
}

void
boot_enc_decrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
             uint32_t sz, uint32_t blk_off, uint8_t *buf)
{
    struct enc_key_data *enc = &enc_state[slot];
    uint8_t nonce[16];

    /* Nothing to do with size == 0 */
    if (sz == 0) {
       return;
    }

    memset(nonce, 0, 12);
    off >>= 4;
    nonce[12] = (uint8_t)(off >> 24);
    nonce[13] = (uint8_t)(off >> 16);
    nonce[14] = (uint8_t)(off >> 8);
    nonce[15] = (uint8_t)off;

    assert(enc->valid == 1);
    bootutil_aes_ctr_decrypt(&enc->aes_ctr, nonce, buf, sz, blk_off, buf);
}

/**
 * Clears encrypted state after use.
 */
+2 −2
Original line number Diff line number Diff line
@@ -153,7 +153,7 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,

            if (off >= hdr_size && off < tlv_off) {
                blk_off = (off - hdr_size) & 0xf;
                boot_encrypt(enc_state, slot, off - hdr_size,
                boot_enc_decrypt(enc_state, slot, off - hdr_size,
                                 blk_sz, blk_off, tmp_buf);
            }
        }
+12 −7
Original line number Diff line number Diff line
@@ -1391,11 +1391,17 @@ boot_copy_region(struct boot_loader_state *state,
                        blk_sz = tlv_off - abs_off;
                    }
                }
                boot_encrypt(BOOT_CURR_ENC(state), source_slot,
                if (source_slot == 0) {
                    boot_enc_encrypt(BOOT_CURR_ENC(state), source_slot,
                            (abs_off + idx) - hdr->ih_hdr_size, blk_sz,
                            blk_off, &buf[idx]);
                } else {
                    boot_enc_decrypt(BOOT_CURR_ENC(state), source_slot,
                            (abs_off + idx) - hdr->ih_hdr_size, blk_sz,
                            blk_off, &buf[idx]);
                }
            }
        }
#endif

        rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
@@ -2897,10 +2903,9 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
             * Part of the chunk is encrypted payload */
            blk_sz = tlv_off - (bytes_copied);
        }
        boot_encrypt(BOOT_CURR_ENC(state), slot,
        boot_enc_decrypt(BOOT_CURR_ENC(state), slot,
                (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
                blk_off, cur_dst);

        bytes_copied += chunk_sz;
    }
    rc = 0;