efm32gg11_crypto_hash.c
Go to the documentation of this file.
1 /**
2  * @file efm32gg11_crypto_hash.c
3  * @brief EFM32 Giant Gecko 11 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.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "em_device.h"
36 #include "em_crypto.h"
37 #include "core/crypto.h"
40 #include "hash/hash_algorithms.h"
41 #include "debug.h"
42 
43 //Check crypto library configuration
44 #if (EFM32GG11_CRYPTO_HASH_SUPPORT == ENABLED)
45 #if (SHA1_SUPPORT == ENABLED)
46 
47 /**
48  * @brief Digest a message using SHA-1
49  * @param[in] data Pointer to the message being hashed
50  * @param[in] length Length of the message
51  * @param[out] digest Pointer to the calculated digest
52  * @return Error code
53  **/
54 
55 error_t sha1Compute(const void *data, size_t length, uint8_t *digest)
56 {
57  //Acquire exclusive access to the CRYPTO module
59 
60  //Digest the message
61  CRYPTO_SHA_1(CRYPTO0, data, length, digest);
62 
63  //Release exclusive access to the CRYPTO module
65 
66  //Successful processing
67  return NO_ERROR;
68 }
69 
70 
71 /**
72  * @brief Process message in 16-word blocks
73  * @param[in] context Pointer to the SHA-1 context
74  **/
75 
77 {
78  uint32_t temp[8];
79 
80  //Initialize the 8 working registers
81  temp[0] = context->h[0];
82  temp[1] = context->h[1];
83  temp[2] = context->h[2];
84  temp[3] = context->h[3];
85  temp[4] = context->h[4];
86  temp[5] = 0;
87  temp[6] = 0;
88  temp[7] = 0;
89 
90  //Acquire exclusive access to the CRYPTO module
92 
93  //Configure the CRYPTO module for SHA-1 operation
94  CRYPTO0->CTRL = CRYPTO_CTRL_SHA_SHA1;
95  CRYPTO0->SEQCTRL = 0;
96  CRYPTO0->SEQCTRLB = 0;
97 
98  //Set the result width for MADD32 operation
99  CRYPTO_ResultWidthSet(CRYPTO0, cryptoResult256Bits);
100 
101  //Write the state to DDATA1
102  CRYPTO_DDataWrite(&CRYPTO0->DDATA1, temp);
103 
104  //Copy data to DDATA0 and select DDATA0 and DDATA1 for SHA operation
105  CRYPTO_EXECUTE_2(CRYPTO0,
106  CRYPTO_CMD_INSTR_DDATA1TODDATA0,
107  CRYPTO_CMD_INSTR_SELDDATA0DDATA1);
108 
109  //Write the 16-word block to QDATA1BIG
110  CRYPTO_QDataWrite(&CRYPTO0->QDATA1BIG, context->w);
111 
112  //Accelerate SHA-1 inner compression loop
113  CRYPTO_EXECUTE_3(CRYPTO0,
114  CRYPTO_CMD_INSTR_SHA,
115  CRYPTO_CMD_INSTR_MADD32,
116  CRYPTO_CMD_INSTR_DDATA0TODDATA1);
117 
118  //Read the resulting digest from DDATA0BIG
119  CRYPTO_DDataRead(&CRYPTO0->DDATA0BIG, temp);
120 
121  //Release exclusive access to the CRYPTO module
123 
124  //Convert from big-endian byte order to host byte order
125  context->h[0] = betoh32(temp[0]);
126  context->h[1] = betoh32(temp[1]);
127  context->h[2] = betoh32(temp[2]);
128  context->h[3] = betoh32(temp[3]);
129  context->h[4] = betoh32(temp[4]);
130 }
131 
132 #endif
133 #if (SHA256_SUPPORT == ENABLED)
134 
135 /**
136  * @brief Digest a message using SHA-256
137  * @param[in] data Pointer to the message being hashed
138  * @param[in] length Length of the message
139  * @param[out] digest Pointer to the calculated digest
140  * @return Error code
141  **/
142 
143 error_t sha256Compute(const void *data, size_t length, uint8_t *digest)
144 {
145  //Acquire exclusive access to the CRYPTO module
147 
148  //Digest the message
149  CRYPTO_SHA_256(CRYPTO0, data, length, digest);
150 
151  //Release exclusive access to the CRYPTO module
153 
154  //Successful processing
155  return NO_ERROR;
156 }
157 
158 
159 /**
160  * @brief Process message in 16-word blocks
161  * @param[in] context Pointer to the SHA-256 context
162  **/
163 
165 {
166  //Acquire exclusive access to the CRYPTO module
168 
169  //Configure the CRYPTO module for SHA-256 operation
170  CRYPTO0->CTRL = CRYPTO_CTRL_SHA_SHA2;
171  CRYPTO0->SEQCTRL = 0;
172  CRYPTO0->SEQCTRLB = 0;
173 
174  //Set the result width for MADD32 operation
175  CRYPTO_ResultWidthSet(CRYPTO0, cryptoResult256Bits);
176 
177  //Write the state to DDATA1
178  CRYPTO_DDataWrite(&CRYPTO0->DDATA1, context->h);
179 
180  //Copy data to DDATA0 and select DDATA0 and DDATA1 for SHA operation
181  CRYPTO_EXECUTE_2(CRYPTO0,
182  CRYPTO_CMD_INSTR_DDATA1TODDATA0,
183  CRYPTO_CMD_INSTR_SELDDATA0DDATA1);
184 
185  //Write the 16-word block to QDATA1BIG
186  CRYPTO_QDataWrite(&CRYPTO0->QDATA1BIG, context->w);
187 
188  //Accelerate SHA-256 inner compression loop
189  CRYPTO_EXECUTE_3(CRYPTO0,
190  CRYPTO_CMD_INSTR_SHA,
191  CRYPTO_CMD_INSTR_MADD32,
192  CRYPTO_CMD_INSTR_DDATA0TODDATA1);
193 
194  //Read the resulting digest from DDATA0BIG
195  CRYPTO_DDataRead(&CRYPTO0->DDATA0BIG, context->h);
196 
197  //Release exclusive access to the CRYPTO module
199 
200  //Convert from big-endian byte order to host byte order
201  context->h[0] = betoh32(context->h[0]);
202  context->h[1] = betoh32(context->h[1]);
203  context->h[2] = betoh32(context->h[2]);
204  context->h[3] = betoh32(context->h[3]);
205  context->h[4] = betoh32(context->h[4]);
206  context->h[5] = betoh32(context->h[5]);
207  context->h[6] = betoh32(context->h[6]);
208  context->h[7] = betoh32(context->h[7]);
209 }
210 
211 #endif
212 #endif
#define betoh32(value)
Definition: cpu_endian.h:454
SHA-256 algorithm context.
Definition: sha256.h:62
uint8_t data[]
Definition: ethernet.h:222
uint32_t h[8]
Definition: sha256.h:65
EFM32 Giant Gecko 11 hash hardware accelerator.
error_t sha1Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-1.
error_t
Error codes.
Definition: error.h:43
uint32_t w[16]
Definition: sha256.h:70
EFM32 Giant Gecko 11 hardware cryptographic accelerator.
uint32_t h[5]
Definition: sha1.h:65
General definitions for cryptographic algorithms.
uint8_t length
Definition: tcp.h:368
Collection of hash algorithms.
void sha256ProcessBlock(Sha256Context *context)
Process message in 16-word blocks.
void sha1ProcessBlock(Sha1Context *context)
Process message in 16-word blocks.
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
OsMutex efm32gg11CryptoMutex
error_t sha256Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-256.
uint32_t w[16]
Definition: sha1.h:70
SHA-1 algorithm context.
Definition: sha1.h:62
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.