1 /*
2  *  Copyright Nordic Semiconductor ASA
3  *  SPDX-License-Identifier: Apache-2.0
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
6  *  not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 
18 #ifndef NRF_CC310_GLUE_H__
19 #define NRF_CC310_GLUE_H__
20 
21 #include <nrf.h>
22 #include <nrf_cc310_bl_init.h>
23 #include <nrf_cc310_bl_hash_sha256.h>
24 #include <nrf_cc310_bl_ecdsa_verify_secp256r1.h>
25 #include <devicetree.h>
26 #include <string.h>
27 
28 /*
29  * Name translation for peripherals with only one type of access available.
30  */
31 #if !defined(NRF_TRUSTZONE_NONSECURE) && defined(CONFIG_ARM_TRUSTZONE_M)
32 #define NRF_CRYPTOCELL   NRF_CRYPTOCELL_S
33 #endif
34 
35 typedef nrf_cc310_bl_hash_context_sha256_t bootutil_sha_context;
36 
37 int cc310_ecdsa_verify_secp256r1(uint8_t *hash,
38                                  uint8_t *public_key,
39                                  uint8_t *signature,
40                                  size_t hash_len);
41 
42 
43 int cc310_init(void);
44 
45 static inline void cc310_sha256_init(nrf_cc310_bl_hash_context_sha256_t *ctx);
46 
47 void cc310_sha256_update(nrf_cc310_bl_hash_context_sha256_t *ctx,
48                          const void *data,
49                          uint32_t data_len);
50 
nrf_cc310_enable(void)51 static inline void nrf_cc310_enable(void)
52 {
53     NRF_CRYPTOCELL->ENABLE=1;
54 }
55 
nrf_cc310_disable(void)56 static inline void nrf_cc310_disable(void)
57 {
58     NRF_CRYPTOCELL->ENABLE=0;
59 }
60 
61 /* Enable and disable cc310 to reduce power consumption */
cc310_sha256_init(nrf_cc310_bl_hash_context_sha256_t * ctx)62 static inline void cc310_sha256_init(nrf_cc310_bl_hash_context_sha256_t * ctx)
63 {
64     cc310_init();
65     nrf_cc310_enable();
66     nrf_cc310_bl_hash_sha256_init(ctx);
67 }
68 
cc310_sha256_finalize(nrf_cc310_bl_hash_context_sha256_t * ctx,uint8_t * output)69 static inline void cc310_sha256_finalize(nrf_cc310_bl_hash_context_sha256_t *ctx,
70                                           uint8_t *output)
71 {
72     nrf_cc310_bl_hash_sha256_finalize(ctx,
73                                       (nrf_cc310_bl_hash_digest_sha256_t *)output);
74     nrf_cc310_disable();
75 }
76 
77 #endif /* NRF_CC310_GLUE_H__ */
78