1 /*
2  * SPDX-License-Identifier: Apache-2.0
3  *
4  * Copyright (c) 2018-2019 JUUL Labs
5  * Copyright (c) 2019 Arm Limited
6  *
7  * Original license:
8  *
9  * Licensed to the Apache Software Foundation (ASF) under one
10  * or more contributor license agreements.  See the NOTICE file
11  * distributed with this work for additional information
12  * regarding copyright ownership.  The ASF licenses this file
13  * to you under the Apache License, Version 2.0 (the
14  * "License"); you may not use this file except in compliance
15  * with the License.  You may obtain a copy of the License at
16  *
17  *  http://www.apache.org/licenses/LICENSE-2.0
18  *
19  * Unless required by applicable law or agreed to in writing,
20  * software distributed under the License is distributed on an
21  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22  * KIND, either express or implied.  See the License for the
23  * specific language governing permissions and limitations
24  * under the License.
25  */
26 
27 #ifndef BOOTUTIL_ENC_KEY_H
28 #define BOOTUTIL_ENC_KEY_H
29 
30 #include <stdbool.h>
31 #include <stdint.h>
32 #include <flash_map_backend/flash_map_backend.h>
33 #include "bootutil/crypto/aes_ctr.h"
34 #include "bootutil/image.h"
35 #include "bootutil/enc_key_public.h"
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 #define BOOT_ENC_TLV_ALIGN_SIZE ALIGN_UP(BOOT_ENC_TLV_SIZE, BOOT_MAX_ALIGN)
42 
43 struct enc_key_data {
44     uint8_t valid;
45     bootutil_aes_ctr_context aes_ctr;
46 };
47 
48 extern const struct bootutil_key bootutil_enc_key;
49 struct boot_status;
50 
51 int boot_enc_init(struct enc_key_data *enc_state, uint8_t slot);
52 int boot_enc_drop(struct enc_key_data *enc_state, uint8_t slot);
53 int boot_enc_set_key(struct enc_key_data *enc_state, uint8_t slot,
54         const struct boot_status *bs);
55 int boot_enc_load(struct enc_key_data *enc_state, int image_index,
56         const struct image_header *hdr, const struct flash_area *fap,
57         struct boot_status *bs);
58 int boot_enc_decrypt(const uint8_t *buf, uint8_t *enckey);
59 bool boot_enc_valid(struct enc_key_data *enc_state, int image_index,
60         const struct flash_area *fap);
61 void boot_encrypt(struct enc_key_data *enc_state, int image_index,
62         const struct flash_area *fap, uint32_t off, uint32_t sz,
63         uint32_t blk_off, uint8_t *buf);
64 void boot_enc_zeroize(struct enc_key_data *enc_state);
65 
66 #ifdef __cplusplus
67 }
68 #endif
69 
70 #endif /* BOOTUTIL_ENC_KEY_H */
71