ra8_crypto_hash.c
Go to the documentation of this file.
1 /**
2  * @file ra8_crypto_hash.c
3  * @brief RA8 hash hardware accelerator
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "hw_sce_hash_private.h"
36 #include "core/crypto.h"
39 #include "hash/hash_algorithms.h"
40 #include "debug.h"
41 
42 //Check crypto library configuration
43 #if (RA8_CRYPTO_HASH_SUPPORT == ENABLED)
44 
45 
46 /**
47  * @brief Update hash value
48  * @param[in] algo Hash algorithm
49  * @param[in] data Pointer to the input buffer
50  * @param[in] length Length of the input buffer
51  * @param[in,out] h Hash value
52  **/
53 
54 void hashProcessData(uint32_t algo, const uint8_t *data, size_t length,
55  uint32_t *h)
56 {
57  uint32_t type;
58  uint32_t command;
59  uint32_t state[20];
60  uint32_t digest[16];
61 
62  //Hash type
63  type = htobe32(algo);
64  //Command
65  command = HTOBE32(SCE_OEM_CMD_HASH_RESUME_TO_SUSPEND);
66 
67  //Clear state
68  osMemset(state, 0, sizeof(state));
69 
70  //Acquire exclusive access to the RSIP7 module
72 
73  //Restore hash state
74  if(algo == SCE_OEM_CMD_HASH_TYPE_SHA1)
75  {
76  state[0] = h[0];
77  state[2] = h[1];
78  state[4] = h[2];
79  state[6] = h[3];
80  state[14] = h[4];
81  state[17] = HTOBE32(0x80000000);
82  state[18] = HTOBE32(0x7F000000);
83  state[19] = HTOBE32(0x00FEFFFF);
84  }
85  else if(algo == SCE_OEM_CMD_HASH_TYPE_SHA256)
86  {
87  state[0] = h[0];
88  state[2] = h[1];
89  state[4] = h[2];
90  state[6] = h[3];
91  state[8] = h[4];
92  state[10] = h[5];
93  state[12] = h[6];
94  state[14] = h[7];
95  state[17] = HTOBE32(0x80000000);
96  state[18] = HTOBE32(0x7F000000);
97  state[19] = HTOBE32(0x00FEFFFF);
98  }
99  else
100  {
101  state[0] = h[0];
102  state[1] = h[1];
103  state[2] = h[2];
104  state[3] = h[3];
105  state[4] = h[4];
106  state[5] = h[5];
107  state[6] = h[6];
108  state[7] = h[7];
109  state[8] = h[8];
110  state[9] = h[9];
111  state[10] = h[10];
112  state[11] = h[11];
113  state[12] = h[12];
114  state[13] = h[13];
115  state[14] = h[14];
116  state[15] = h[15];
117  state[17] = HTOBE32(0x80000000);
118  state[18] = HTOBE32(0x7F000000);
119  state[19] = HTOBE32(0x00FCFFFF);
120  }
121 
122  //Accelerate inner compression loop
123  HW_SCE_ShaGenerateMessageDigestSub(&type, &command, (uint32_t *) data, NULL,
124  state, digest, state, length / 4);
125 
126  //Save intermediate hash value
127  if(algo == SCE_OEM_CMD_HASH_TYPE_SHA1)
128  {
129  h[0] = state[0];
130  h[1] = state[2];
131  h[2] = state[4];
132  h[3] = state[6];
133  h[4] = state[14];
134  }
135  else if(algo == SCE_OEM_CMD_HASH_TYPE_SHA256)
136  {
137  h[0] = state[0];
138  h[1] = state[2];
139  h[2] = state[4];
140  h[3] = state[6];
141  h[4] = state[8];
142  h[5] = state[10];
143  h[6] = state[12];
144  h[7] = state[14];
145  }
146  else
147  {
148  h[0] = state[0];
149  h[1] = state[1];
150  h[2] = state[2];
151  h[3] = state[3];
152  h[4] = state[4];
153  h[5] = state[5];
154  h[6] = state[6];
155  h[7] = state[7];
156  h[8] = state[8];
157  h[9] = state[9];
158  h[10] = state[10];
159  h[11] = state[11];
160  h[12] = state[12];
161  h[13] = state[13];
162  h[14] = state[14];
163  h[15] = state[15];
164  }
165 
166  //Release exclusive access to the RSIP7 module
168 }
169 
170 
171 #if (SHA1_SUPPORT == ENABLED)
172 
173 /**
174  * @brief Update the SHA-1 context with a portion of the message being hashed
175  * @param[in] context Pointer to the SHA-1 context
176  * @param[in] data Pointer to the buffer being hashed
177  * @param[in] length Length of the buffer
178  **/
179 
180 void sha1Update(Sha1Context *context, const void *data, size_t length)
181 {
182  size_t n;
183 
184  //Process the incoming data
185  while(length > 0)
186  {
187  //Check whether some data is pending in the buffer
188  if(context->size == 0 && length >= 64)
189  {
190  //The length must be a multiple of 64 bytes
191  n = length - (length % 64);
192 
193  //Update hash value
194  hashProcessData(SCE_OEM_CMD_HASH_TYPE_SHA1, data, n, context->h);
195 
196  //Update the SHA-1 context
197  context->totalSize += n;
198  //Advance the data pointer
199  data = (uint8_t *) data + n;
200  //Remaining bytes to process
201  length -= n;
202  }
203  else
204  {
205  //The buffer can hold at most 64 bytes
206  n = MIN(length, 64 - context->size);
207 
208  //Copy the data to the buffer
209  osMemcpy(context->buffer + context->size, data, n);
210 
211  //Update the SHA-1 context
212  context->size += n;
213  context->totalSize += n;
214  //Advance the data pointer
215  data = (uint8_t *) data + n;
216  //Remaining bytes to process
217  length -= n;
218 
219  //Check whether the buffer is full
220  if(context->size == 64)
221  {
222  //Update hash value
223  hashProcessData(SCE_OEM_CMD_HASH_TYPE_SHA1, context->buffer,
224  context->size, context->h);
225 
226  //Empty the buffer
227  context->size = 0;
228  }
229  }
230  }
231 }
232 
233 
234 /**
235  * @brief Process message in 16-word blocks
236  * @param[in] context Pointer to the SHA-1 context
237  **/
238 
240 {
241  //Update hash value
242  hashProcessData(SCE_OEM_CMD_HASH_TYPE_SHA1, context->buffer, 64,
243  context->h);
244 }
245 
246 #endif
247 #if (SHA256_SUPPORT == ENABLED)
248 
249 /**
250  * @brief Update the SHA-256 context with a portion of the message being hashed
251  * @param[in] context Pointer to the SHA-256 context
252  * @param[in] data Pointer to the buffer being hashed
253  * @param[in] length Length of the buffer
254  **/
255 
256 void sha256Update(Sha256Context *context, const void *data, size_t length)
257 {
258  size_t n;
259 
260  //Process the incoming data
261  while(length > 0)
262  {
263  //Check whether some data is pending in the buffer
264  if(context->size == 0 && length >= 64)
265  {
266  //The length must be a multiple of 64 bytes
267  n = length - (length % 64);
268 
269  //Update hash value
270  hashProcessData(SCE_OEM_CMD_HASH_TYPE_SHA256, data, n, context->h);
271 
272  //Update the SHA-256 context
273  context->totalSize += n;
274  //Advance the data pointer
275  data = (uint8_t *) data + n;
276  //Remaining bytes to process
277  length -= n;
278  }
279  else
280  {
281  //The buffer can hold at most 64 bytes
282  n = MIN(length, 64 - context->size);
283 
284  //Copy the data to the buffer
285  osMemcpy(context->buffer + context->size, data, n);
286 
287  //Update the SHA-256 context
288  context->size += n;
289  context->totalSize += n;
290  //Advance the data pointer
291  data = (uint8_t *) data + n;
292  //Remaining bytes to process
293  length -= n;
294 
295  //Check whether the buffer is full
296  if(context->size == 64)
297  {
298  //Update hash value
299  hashProcessData(SCE_OEM_CMD_HASH_TYPE_SHA256, context->buffer,
300  context->size, context->h);
301 
302  //Empty the buffer
303  context->size = 0;
304  }
305  }
306  }
307 }
308 
309 
310 /**
311  * @brief Process message in 16-word blocks
312  * @param[in] context Pointer to the SHA-256 context
313  **/
314 
316 {
317  //Update hash value
318  hashProcessData(SCE_OEM_CMD_HASH_TYPE_SHA256, context->buffer, 64,
319  context->h);
320 }
321 
322 #endif
323 #if (SHA512_SUPPORT == ENABLED)
324 
325 /**
326  * @brief Update the SHA-512 context with a portion of the message being hashed
327  * @param[in] context Pointer to the SHA-512 context
328  * @param[in] data Pointer to the buffer being hashed
329  * @param[in] length Length of the buffer
330  **/
331 
332 void sha512Update(Sha512Context *context, const void *data, size_t length)
333 {
334  size_t n;
335 
336  //Process the incoming data
337  while(length > 0)
338  {
339  //Check whether some data is pending in the buffer
340  if(context->size == 0 && length >= 128)
341  {
342  //The length must be a multiple of 128 bytes
343  n = length - (length % 128);
344 
345  //Update hash value
346  hashProcessData(SCE_OEM_CMD_HASH_TYPE_SHA512, data, n,
347  (uint32_t *) context->h);
348 
349  //Update the SHA-512 context
350  context->totalSize += n;
351  //Advance the data pointer
352  data = (uint8_t *) data + n;
353  //Remaining bytes to process
354  length -= n;
355  }
356  else
357  {
358  //The buffer can hold at most 128 bytes
359  n = MIN(length, 128 - context->size);
360 
361  //Copy the data to the buffer
362  osMemcpy(context->buffer + context->size, data, n);
363 
364  //Update the SHA-512 context
365  context->size += n;
366  context->totalSize += n;
367  //Advance the data pointer
368  data = (uint8_t *) data + n;
369  //Remaining bytes to process
370  length -= n;
371 
372  //Check whether the buffer is full
373  if(context->size == 128)
374  {
375  //Update hash value
376  hashProcessData(SCE_OEM_CMD_HASH_TYPE_SHA512, context->buffer,
377  context->size, (uint32_t *) context->h);
378 
379  //Empty the buffer
380  context->size = 0;
381  }
382  }
383  }
384 }
385 
386 
387 /**
388  * @brief Process message in 16-word blocks
389  * @param[in] context Pointer to the SHA-512 context
390  **/
391 
393 {
394  //Update hash value
395  hashProcessData(SCE_OEM_CMD_HASH_TYPE_SHA512, context->buffer, 128,
396  (uint32_t *) context->h);
397 }
398 
399 #endif
400 #endif
uint8_t type
Definition: coap_common.h:176
#define HTOBE32(value)
Definition: cpu_endian.h:443
#define htobe32(value)
Definition: cpu_endian.h:446
General definitions for cryptographic algorithms.
Debugging facilities.
uint8_t n
uint8_t data[]
Definition: ethernet.h:222
Collection of hash algorithms.
uint8_t h
Definition: ndp.h:302
#define osMemset(p, value, length)
Definition: os_port.h:135
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
#define MIN(a, b)
Definition: os_port.h:63
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
OsMutex ra8CryptoMutex
Definition: ra8_crypto.c:41
RA8 hardware cryptographic accelerator (RSIP7)
void sha512ProcessBlock(Sha512Context *context)
Process message in 16-word blocks.
void sha1ProcessBlock(Sha1Context *context)
Process message in 16-word blocks.
void sha256Update(Sha256Context *context, const void *data, size_t length)
Update the SHA-256 context with a portion of the message being hashed.
void sha1Update(Sha1Context *context, const void *data, size_t length)
Update the SHA-1 context with a portion of the message being hashed.
void sha256ProcessBlock(Sha256Context *context)
Process message in 16-word blocks.
void sha512Update(Sha512Context *context, const void *data, size_t length)
Update the SHA-512 context with a portion of the message being hashed.
void hashProcessData(uint32_t algo, const uint8_t *data, size_t length, uint32_t *h)
Update hash value.
RA8 hash hardware accelerator.
SHA-1 algorithm context.
Definition: sha1.h:62
uint64_t totalSize
Definition: sha1.h:74
size_t size
Definition: sha1.h:73
uint32_t h[5]
Definition: sha1.h:65
uint8_t buffer[64]
Definition: sha1.h:71
SHA-256 algorithm context.
Definition: sha256.h:62
uint64_t totalSize
Definition: sha256.h:74
size_t size
Definition: sha256.h:73
uint8_t buffer[64]
Definition: sha256.h:71
uint32_t h[8]
Definition: sha256.h:65
SHA-512 algorithm context.
Definition: sha512.h:62
uint64_t h[8]
Definition: sha512.h:65
uint64_t totalSize
Definition: sha512.h:74
size_t size
Definition: sha512.h:73
uint8_t buffer[128]
Definition: sha512.h:71
uint8_t length
Definition: tcp.h:368