gd32f2xx_crypto_hash.c
Go to the documentation of this file.
1 /**
2  * @file gd32f2xx_crypto_hash.c
3  * @brief GD32F2 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.2
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "gd32f20x.h"
36 #include "gd32f20x_hau.h"
37 #include "core/crypto.h"
40 #include "hash/hash_algorithms.h"
41 #include "debug.h"
42 
43 //Check crypto library configuration
44 #if (GD32F2XX_CRYPTO_HASH_SUPPORT == ENABLED)
45 
46 
47 /**
48  * @brief HAU module initialization
49  * @return Error code
50  **/
51 
53 {
54  //Enable HAU peripheral clock
55  rcu_periph_clock_enable(RCU_HAU);
56 
57  //Successful processing
58  return NO_ERROR;
59 }
60 
61 
62 #if (MD5_SUPPORT == ENABLED)
63 
64 /**
65  * @brief Digest a message using MD5
66  * @param[in] data Pointer to the message being hashed
67  * @param[in] length Length of the message
68  * @param[out] digest Pointer to the calculated digest
69  * @return Error code
70  **/
71 
72 error_t md5Compute(const void *data, size_t length, uint8_t *digest)
73 {
74  ErrStatus status;
75 
76  //Acquire exclusive access to the HAU module
78  //Digest the message using MD5
79  status = hau_hash_md5((uint8_t *) data, length, digest);
80  //Release exclusive access to the HAU module
82 
83  //Return status code
84  return (status == SUCCESS) ? NO_ERROR : ERROR_FAILURE;
85 }
86 
87 #endif
88 #if (SHA1_SUPPORT == ENABLED)
89 
90 /**
91  * @brief Digest a message using SHA-1
92  * @param[in] data Pointer to the message being hashed
93  * @param[in] length Length of the message
94  * @param[out] digest Pointer to the calculated digest
95  * @return Error code
96  **/
97 
98 error_t sha1Compute(const void *data, size_t length, uint8_t *digest)
99 {
100  ErrStatus status;
101 
102  //Acquire exclusive access to the HAU module
104  //Digest the message using SHA-1
105  status = hau_hash_sha_1((uint8_t *) data, length, digest);
106  //Release exclusive access to the HAU module
108 
109  //Return status code
110  return (status == SUCCESS) ? NO_ERROR : ERROR_FAILURE;
111 }
112 
113 #endif
114 #if (SHA224_SUPPORT == ENABLED)
115 
116 /**
117  * @brief Digest a message using SHA-224
118  * @param[in] data Pointer to the message being hashed
119  * @param[in] length Length of the message
120  * @param[out] digest Pointer to the calculated digest
121  * @return Error code
122  **/
123 
124 error_t sha224Compute(const void *data, size_t length, uint8_t *digest)
125 {
126  ErrStatus status;
127 
128  //Acquire exclusive access to the HAU module
130  //Digest the message using SHA-224
131  status = hau_hash_sha_224((uint8_t *) data, length, digest);
132  //Release exclusive access to the HAU module
134 
135  //Return status code
136  return (status == SUCCESS) ? NO_ERROR : ERROR_FAILURE;
137 }
138 
139 #endif
140 #if (SHA256_SUPPORT == ENABLED)
141 
142 /**
143  * @brief Digest a message using SHA-256
144  * @param[in] data Pointer to the message being hashed
145  * @param[in] length Length of the message
146  * @param[out] digest Pointer to the calculated digest
147  * @return Error code
148  **/
149 
150 error_t sha256Compute(const void *data, size_t length, uint8_t *digest)
151 {
152  ErrStatus status;
153 
154  //Acquire exclusive access to the HAU module
156  //Digest the message using SHA-256
157  status = hau_hash_sha_256((uint8_t *) data, length, digest);
158  //Release exclusive access to the HAU module
160 
161  //Return status code
162  return (status == SUCCESS) ? NO_ERROR : ERROR_FAILURE;
163 }
164 
165 #endif
166 #endif
error_t sha256Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-256.
uint8_t data[]
Definition: ethernet.h:222
error_t
Error codes.
Definition: error.h:43
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
error_t md5Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using MD5.
error_t sha224Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-224.
General definitions for cryptographic algorithms.
uint8_t length
Definition: tcp.h:368
Collection of hash algorithms.
error_t hauInit(void)
HAU module initialization.
OsMutex gd32f2xxCryptoMutex
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
GD32F2 hash hardware accelerator.
GD32F2 hardware cryptographic accelerator.
error_t sha1Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-1.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.