Select one of the symbols to view example projects that use it.
 
Outline
#define _HARDWARE_SHA256_H
#include "pico.h"
#include "hardware/structs/sha256.h"
#define PARAM_ASSERTIONS_ENABLED_HARDWARE_SHA256
#define PARAM_ASSERTIONS_ENABLED_HARDWARE_SHA256
#define SHA256_RESULT_BYTES
sha256_endianness
sha256_result_t
sha256_set_dma_size(uint)
sha256_set_bswap(bool)
sha256_start()
sha256_is_sum_valid()
sha256_is_ready()
sha256_wait_valid_blocking()
sha256_wait_ready_blocking()
sha256_get_result(sha256_result_t *, enum sha256_endianness);
sha256_err_not_ready()
sha256_err_not_ready_clear()
sha256_get_write_addr()
sha256_put_word(uint32_t)
sha256_put_byte(uint8_t)
Files
loading...
SourceVuRaspberry Pi Pico SDK and ExamplesPicoSDKsrc/rp2_common/hardware_sha256/include/hardware/sha256.h
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * Copyright (c) 2024 Raspberry Pi (Trading) Ltd. * * SPDX-License-Identifier: BSD-3-Clause *//* ... */ #ifndef _HARDWARE_SHA256_H #define _HARDWARE_SHA256_H #include "pico.h" #include "hardware/structs/sha256.h" /** \file hardware/sha256.h * \defgroup hardware_sha256 hardware_sha256 * * \brief Hardware SHA-256 Accelerator API * * RP2350 is equipped with an implementation of the SHA-256 hash algorithm. * The hardware should first be configured by calling the \ref sha256_set_dma_size and \ref sha256_set_bswap functions. * To generate a new hash the hardware should first be initialised by calling \ref sha256_start. * The hardware is ready to accept data when \ref sha256_is_ready returns true, * at which point the data to be hashed can be written to the address returned by \ref sha256_get_write_addr. * The hardware requires 64 bytes to be written in one go or else \ref sha256_err_not_ready will indicate an error and * the hashing process must be restarted. * \ref sha256_is_sum_valid will return true when there is a valid checksum result which can be retrieved by calling \ref sha256_get_result. *//* ... */ #ifdef __cplusplus extern "C" { #endif // PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_HARDWARE_SHA256, Enable/disable hardware_sha256 assertions, type=bool, default=0, group=hardware_sha256 #ifndef PARAM_ASSERTIONS_ENABLED_HARDWARE_SHA256 #ifdef PARAM_ASSERTIONS_ENABLED_SHA256 // backwards compatibility with SDK < 2.0.0 #define PARAM_ASSERTIONS_ENABLED_HARDWARE_SHA256 PARAM_ASSERTIONS_ENABLED_SHA256 #else #define PARAM_ASSERTIONS_ENABLED_HARDWARE_SHA256 0 #endif/* ... */ #endif /*! \brief Size of a sha256 result in bytes. * \ingroup hardware_sha256 *//* ... */ #define SHA256_RESULT_BYTES 32 /*! \brief SHA-256 endianness definition used in the API * \ingroup hardware_sha256 *//* ... */ enum sha256_endianness { SHA256_LITTLE_ENDIAN, ///< Little Endian SHA256_BIG_ENDIAN, ///< Big Endian ...}; /*! \brief SHA-256 result generated by the API * \ingroup hardware_sha256 *//* ... */ typedef union { uint32_t words[SHA256_RESULT_BYTES/4]; uint8_t bytes[SHA256_RESULT_BYTES]; ...} sha256_result_t; /*! \brief Configure the correct DMA data size. * \ingroup hardware_sha256 * * This must be configured before the DMA channel is triggered and ensures the correct number of transfers is requested per block. * * \param size_in_bytes Size of DMA transfers, either 1, 2 or 4 bytes only. *//* ... */ static inline void sha256_set_dma_size(uint size_in_bytes) { uint32_t val; invalid_params_if(HARDWARE_SHA256, size_in_bytes != 1 && size_in_bytes != 2 && size_in_bytes != 4); if (size_in_bytes == 1) { val = SHA256_CSR_DMA_SIZE_VALUE_8BIT; }if (size_in_bytes == 1) { ... } else if (size_in_bytes == 2) { val = SHA256_CSR_DMA_SIZE_VALUE_16BIT; }else if (size_in_bytes == 2) { ... } else { val = SHA256_CSR_DMA_SIZE_VALUE_32BIT; }else { ... } hw_write_masked(&sha256_hw->csr, val << SHA256_CSR_DMA_SIZE_LSB, SHA256_CSR_DMA_SIZE_BITS); }{ ... } /*! \brief Enable or disable byte swapping of 32-bit values. * \ingroup hardware_sha256 * * The SHA256 algorithm expects bytes in big endian order, but the system bus deals with little endian data, * so control is provided to convert little endian bus data to big endian internal data. This defaults to true * * \param swap false to disable byte swapping *//* ... */ static inline void sha256_set_bswap(bool swap) { if (swap) { hw_set_bits(&sha256_hw->csr, SHA256_CSR_BSWAP_BITS); }if (swap) { ... } else { hw_clear_bits(&sha256_hw->csr, SHA256_CSR_BSWAP_BITS); }else { ... } }{ ... } /*! \brief Prepare the hardware for a new checksum. * \ingroup hardware_sha256 * * Called to initialise the hardware before starting the checksum calculation *//* ... */ static inline void sha256_start(void) { hw_set_bits(&sha256_hw->csr, SHA256_CSR_START_BITS); }{ ... } /*! \brief Check if a valid checksum has been calculated * \ingroup hardware_sha256 * * The checksum result will be invalid when data is first written to the hardware, * and then once 64 bytes of data has been written it may take some time to complete the digest of the current block. * This function can be used to determine when the checksum is valid. * * \return True if \ref sha256_get_result would return a valid result *//* ... */ static inline bool sha256_is_sum_valid(void) { return sha256_hw->csr & SHA256_CSR_SUM_VLD_BITS; }{ ... } /*! \brief Check if a the hardware is ready to accept more data * \ingroup hardware_sha256 * * After writing 64 bytes of data to the hardware, it will be unable to accept more data for a time. * Call this to check if the hardware is ready for more data to be written. \see sha256_err_not_ready * * \return True if the hardware is ready to receive more data *//* ... */ static inline bool sha256_is_ready(void) { return sha256_hw->csr & SHA256_CSR_WDATA_RDY_BITS; }{ ... } /*! \brief Wait until the checksum is valid * \ingroup hardware_sha256 * * When a multiple of 64 bytes of data has been written to the hardware, * the checksum will be valid once the digest of the current block is complete. * This function waits until when the checksum result is valid. *//* ... */ static inline void sha256_wait_valid_blocking(void) { while (!sha256_is_sum_valid()) { tight_loop_contents(); }while (!sha256_is_sum_valid()) { ... } }{ ... } /*! \brief Wait until the hardware is ready to accept more data * \ingroup hardware_sha256 * * Before writing to the hardware, it's necessary to check it is ready to accept more data. * This function waits until the hardware is ready to accept more data *//* ... */ static inline void sha256_wait_ready_blocking(void) { while (!sha256_is_ready()) { tight_loop_contents(); }while (!sha256_is_ready()) { ... } }{ ... } /*! \brief Get the checksum result * \ingroup hardware_sha256 * * Read the 32 byte result calculated by the hardware. Only valid if \ref sha256_is_sum_valid is True * * \param out The checksum result *//* ... */ void sha256_get_result(sha256_result_t *out, enum sha256_endianness endianness); /*! \brief Check if data was written before the hardware was ready * \ingroup hardware_sha256 * * Indicates if an error has occurred due to data being written when the hardware is not ready. * * \return True if data was written before the hardware was ready *//* ... */ static inline bool sha256_err_not_ready(void) { return sha256_hw->csr & SHA256_CSR_ERR_WDATA_NOT_RDY_BITS; }{ ... } /*! \brief Clear the "not ready" error condition * \ingroup hardware_sha256 * * Resets the hardware if a "not ready" error condition is indicated. *//* ... */ static inline void sha256_err_not_ready_clear(void) { hw_clear_bits(&sha256_hw->csr, SHA256_CSR_ERR_WDATA_NOT_RDY_BITS); }{ ... } /*! \brief Address to write the data to be hashed * \ingroup hardware_sha256 * * Returns the hardware address where data to be hashed should be written * * \return Address to write data to be hashed *//* ... */ static inline volatile void *sha256_get_write_addr(void) { return &sha256_hw->wdata; }{ ... } /*! \brief Write one 32bit word of data to the SHA-256 hardware * \ingroup hardware_sha256 * * \param word data to write *//* ... */ static inline void sha256_put_word(uint32_t word) { sha256_hw->wdata = word; }{ ... } /*! \brief Write one byte of data to the SHA-256 hardware * \ingroup hardware_sha256 * * \param b data to write *//* ... */ static inline void sha256_put_byte(uint8_t b) { *((io_rw_8*)&sha256_hw->wdata) = b; }{ ... } #ifdef __cplusplus }extern "C" { ... } #endif /* ... */ #endif
Details