1 /***************************************************************************//**
2 * \file cy_cryptolite_utils.c
3 * \version 2.50
4 *
5 * \brief
6 *  Provides utility functions.
7 *
8 ********************************************************************************
9 * Copyright 2022 Cypress Semiconductor Corporation
10 * SPDX-License-Identifier: Apache-2.0
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 *    http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *******************************************************************************/
24 #include "cy_device.h"
25 
26 #if defined(CY_IP_MXCRYPTOLITE)
27 
28 #include "cy_cryptolite_vu.h"
29 #include "cy_cryptolite_utils.h"
30 #include <string.h>
31 #include <stdlib.h>
32 
Cy_Cryptolite_String2ByteArray(uint8_t * p_dst,int8_t * p_number_string,int byte_size)33 void Cy_Cryptolite_String2ByteArray ( uint8_t* p_dst,
34                             int8_t*  p_number_string,
35                             int      byte_size)
36 {
37    int      i;
38    int      len  = (int)strlen ((char const *)p_number_string);
39    uint8_t  character;
40    uint8_t  nibble;
41    uint8_t  byte = 0;
42    uint32_t nibble_pos = 0;
43    int      byte_idx;
44    bool     valid;
45    for (byte_idx = byte_size - 1; byte_idx > 0; byte_idx--){
46       p_dst[byte_idx] = 0x00;
47    }
48    p_dst[byte_idx] = 0x00;
49    for (i = 0 ; i <= len - 1; i++) {
50       character = (uint8_t)p_number_string[i];
51       if ((character >= (uint8_t)'0') && (character <= (uint8_t)'9')) {
52          nibble = (uint8_t) character - (uint8_t)'0';
53          valid  = true;
54       }
55       else if ((character >= (uint8_t)'a') && (character <= (uint8_t)'f')) {
56          nibble = (uint8_t) (character - (uint8_t)'a') + (uint8_t)10;
57          valid  = true;
58       }
59       else if ((character >= (uint8_t)'A') && (character <= (uint8_t)'F')) {
60          nibble = (uint8_t) (character - (uint8_t)'A') + (uint8_t)10;
61          valid  = true;
62       }
63       else {
64          valid  = false;
65       }
66       if (valid) {
67         if(nibble_pos != 0u)
68         {
69             byte |= nibble;
70         }
71         else
72         {
73             byte |= nibble << 4U;
74         }
75          nibble_pos++;
76       }
77       if (nibble_pos == 2U) {
78          p_dst[byte_idx] = byte;
79          byte_idx   += 1;
80          nibble_pos = 0;
81          byte       = 0;
82       }
83    }
84 
85 }
86 
Cy_Cryptolite_Memcpy(CRYPTOLITE_Type * base,uint8_t * dst,uint8_t * src,uint32_t size)87 cy_en_cryptolite_status_t Cy_Cryptolite_Memcpy(CRYPTOLITE_Type *base, uint8_t* dst,
88                         uint8_t* src,
89                         uint32_t size)
90 {
91     uint32_t i;
92     cy_en_cryptolite_status_t status = CY_CRYPTOLITE_SUCCESS;
93 
94     #if (CRYPTOLITE_VU_PRESENT == 1)
95     cy_stc_cryptolite_descr_t p_struct;
96     uint8_t *srcRemap;
97 
98     srcRemap =  (uint8_t *)CY_REMAP_ADDRESS_CRYPTOLITE(src);
99 
100     if((((uint32_t)dst & 0x3UL) == 0UL) && (((uint32_t)srcRemap & 0x3UL) == 0UL))
101     {
102         status = Cy_Cryptolite_Vu_mov_hw(base, &p_struct, dst, CY_CRYPTOLITE_WORD_SIZE_OF_BYTES(size) , srcRemap, CY_CRYPTOLITE_WORD_SIZE_OF_BYTES(size));
103     }
104     else
105     #endif
106     {
107         (void)base;
108         for(i=0; i < size; i++)
109         {
110             dst[i] = src[i];
111         }
112     }
113 
114     return status;
115 }
116 
117 
Cy_Cryptolite_Memset(void * dest,uint8_t data,uint32_t size)118 void Cy_Cryptolite_Memset (void  *dest, uint8_t data, uint32_t size)
119 {
120    uint32_t i;
121    uint8_t  *dest_P = (uint8_t *)dest;
122 
123    for(i=0u; i < size; i++)
124    {
125       dest_P[i] = data;
126    }
127 
128 }
129 
Cy_Cryptolite_Setnumber(uint8_t * rdst,uint8_t * p_number,uint32_t size)130 void Cy_Cryptolite_Setnumber( uint8_t* rdst,
131                         uint8_t* p_number,
132                         uint32_t size)
133 {
134   uint32_t i;
135 
136   for(i=0; i < size; i++)
137   {
138       rdst[i] = p_number[i];
139   }
140 }
141 
Cy_Cryptolite_InvertEndianness(void * inArrPtr,uint32_t byteSize)142 void Cy_Cryptolite_InvertEndianness(void *inArrPtr, uint32_t byteSize)
143 {
144     int32_t limit;
145     int32_t i;
146     int32_t j = 0;
147     uint8_t temp;
148     uint8_t *tempPtr = NULL;
149 
150 
151     if((inArrPtr == NULL) && (byteSize > 0u))
152     {
153       return;
154     }
155 
156     tempPtr = (uint8_t*)inArrPtr;
157 
158     if (byteSize > 1u)
159     {
160         limit = (int32_t)byteSize / 2;
161         if (0u == (byteSize % 2u))
162         {
163             --limit;
164         }
165 
166         j = 0;
167         i = (int32_t)byteSize - 1;
168         while ( i > limit)
169         {
170             temp = tempPtr[j];
171             tempPtr[j] = tempPtr[i];
172             tempPtr[i] = temp;
173 
174             --i;
175             ++j;
176         }
177     }
178 }
179 #endif /*defined(CY_IP_MXCRYPTOLITE)*/
180