1 /***************************************************************************//**
2 * \file cy_cryptolite_vu.h
3 * \version 2.30
4 *
5 * \brief
6 *  This file provides provides constant and parameters
7 *  and helper functions of the VU in the Cryptolite driver.
8 *
9 ********************************************************************************
10 * Copyright 2020-2021 Cypress Semiconductor Corporation
11 * SPDX-License-Identifier: Apache-2.0
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * 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, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *******************************************************************************/
25 #include "cy_device.h"
26 
27 #if defined (CY_IP_MXCRYPTOLITE)
28 
29 #include "cy_cryptolite_common.h"
30 #include "cy_cryptolite_vu.h"
31 
32 #if defined(__cplusplus)
33 extern "C" {
34 #endif
35 
36 #if (CRYPTOLITE_VU_PRESENT == 1)
37 
Cy_Cryptolite_Vu_test_zero(CRYPTOLITE_Type * base,cy_stc_cryptolite_descr_t * p_struct,uint8_t * psrc,uint16_t bitsize)38 bool Cy_Cryptolite_Vu_test_zero(CRYPTOLITE_Type *base, cy_stc_cryptolite_descr_t* p_struct, uint8_t *psrc, uint16_t bitsize)
39 {
40     uint32_t* p_a_uint32  = (uint32_t *) ((void *)psrc);
41     uint32_t  word;
42     (void)p_struct;
43     (void)base;
44 
45     while (p_a_uint32 < (((uint32_t *) ((void *)psrc)) + VU_BITS_TO_WORDS(bitsize)))
46     {
47         word  = *p_a_uint32++;
48         if (word != 0U){
49           return false;
50         }
51     }
52     return true;
53 }
54 
55 // returns TRUE if rsrc0 contains the same value as rsrc1. FALSE otherwise
Cy_Cryptolite_Vu_test_equal(CRYPTOLITE_Type * base,cy_stc_cryptolite_descr_t * p_struct,uint8_t * rsrc0,uint8_t * rsrc1,uint16_t bitsize)56 bool Cy_Cryptolite_Vu_test_equal(CRYPTOLITE_Type *base, cy_stc_cryptolite_descr_t* p_struct, uint8_t *rsrc0, uint8_t *rsrc1, uint16_t bitsize)
57 {
58   CY_ALIGN(4) uint8_t temp[VU_BITS_TO_BYTES(VU_TEST_EQUAL_LESS_SIZE)];
59 
60   (void)Cy_Cryptolite_Vu_sub_hw(base, p_struct, temp, VU_BITS_TO_WORDS((uint32_t)bitsize), rsrc0, (uint32_t)VU_BITS_TO_WORDS((uint32_t)bitsize), rsrc1, (uint32_t)VU_BITS_TO_WORDS((uint32_t)bitsize));
61   Cy_Cryptolite_Vu_wait_hw(base);
62 
63   if (Cy_Cryptolite_Vu_test_zero(base, p_struct, temp, VU_BITS_TO_WORDS(bitsize))) {
64     return true;
65   }
66   else {
67     return false;
68   }
69 }
70 
71 // returns TRUE if rsrc0 contains the value less than value of rsrc1. FALSE otherwise
Cy_Cryptolite_Vu_test_less_than(CRYPTOLITE_Type * base,cy_stc_cryptolite_descr_t * p_struct,uint8_t * rsrc0,uint8_t * rsrc1,uint16_t bitsize)72 bool Cy_Cryptolite_Vu_test_less_than(CRYPTOLITE_Type *base, cy_stc_cryptolite_descr_t* p_struct, uint8_t *rsrc0, uint8_t *rsrc1, uint16_t bitsize)
73 {
74   CY_ALIGN(4) uint8_t temp[VU_BITS_TO_BYTES(VU_TEST_EQUAL_LESS_SIZE+1U)]={0};
75   uint32_t  sign;
76 
77   (void)Cy_Cryptolite_Vu_sub_hw (base, p_struct, temp, VU_BITS_TO_WORDS((uint32_t)bitsize+1U), rsrc0, VU_BITS_TO_WORDS((uint32_t)bitsize), rsrc1, VU_BITS_TO_WORDS((uint32_t)bitsize));
78   Cy_Cryptolite_Vu_wait_hw (base);
79 
80   sign = Cy_Cryptolite_Vu_get_bit(temp, bitsize);
81 
82   if (!(bool)sign) {
83     return false;
84   }
85   else {
86     return true;
87   }
88 }
89 
Cy_Cryptolite_Vu_lsl(uint8_t * p_z,uint32_t word_size_z,uint8_t * p_a,uint32_t word_size_a,uint32_t bit_sh)90 void Cy_Cryptolite_Vu_lsl (uint8_t* p_z, uint32_t word_size_z, uint8_t* p_a, uint32_t word_size_a, uint32_t bit_sh)
91 {
92    uint32_t  word_sh     = bit_sh >> 5;
93    uint32_t  bit_offset  = bit_sh & 31U;
94    uint32_t* p_z_uint32  = (uint32_t *) (void *)p_z;
95    uint32_t* p_a_uint32  = (uint32_t *) (void *)p_a;
96    uint32_t  saved = 0;
97    uint32_t  word;
98    (void)word_size_a;
99    // To be done.
100 
101    while (p_z_uint32 < (((uint32_t *) (void *)p_z) + word_sh)) {
102       *p_z_uint32++ = 0;
103    }
104 
105    while (p_z_uint32 < (((uint32_t *) (void *)p_z) + word_size_z)) {
106       word = *p_a_uint32++;
107       *p_z_uint32++ = (word <<  bit_offset) | saved;
108       saved = (word >> (32U-word_sh));
109    }
110 }
111 
112 // "p_z"       must be 32-bit word aligned.
113 // "word_size" must be not equal to "0"
Cy_Cryptolite_Vu_clr(uint8_t * p_z,uint32_t word_size)114 void Cy_Cryptolite_Vu_clr (uint8_t* p_z, uint32_t word_size)
115 {
116    uint32_t* p_z_uint32 = (uint32_t *) (void *)p_z;
117    uint32_t  i          = 0;
118 
119    do {
120       *p_z_uint32++ = 0;
121 
122       i++;
123    } while (i < word_size);
124 }
125 
Cy_Cryptolite_Vu_RunInstr(CRYPTOLITE_Type * base,cy_stc_cryptolite_descr_t * p_struct)126 cy_en_cryptolite_status_t Cy_Cryptolite_Vu_RunInstr(CRYPTOLITE_Type *base,
127                             cy_stc_cryptolite_descr_t *p_struct)
128 {
129 
130     REG_CRYPTOLITE_VU_DESCR(base) = (uint32_t)p_struct;
131     while((REG_CRYPTOLITE_STATUS(base) & CRYPTOLITE_STATUS_BUSY_Msk) != 0U){};
132 
133     if((REG_CRYPTOLITE_INTR_ERROR(base) & CRYPTOLITE_INTR_ERROR_BUS_ERROR_Msk) != 0U)
134     {
135         REG_CRYPTOLITE_INTR_ERROR(base) = CRYPTOLITE_INTR_ERROR_BUS_ERROR_Msk;
136         return CY_CRYPTOLITE_BUS_ERROR;
137     }
138 
139     return CY_CRYPTOLITE_SUCCESS;
140 }
141 #endif /* CPUSS_CRYPTOLITE_VU */
142 
143 #if defined(__cplusplus)
144 }
145 #endif
146 
147 #endif /* CY_IP_MXCRYPTOLITE */
148