12.13. SHA-256 accelerator

RP2350 is equipped with an implementation of the SHA-256 hash algorithm, as defined in the FIPS 180-4 standard available from NIST publications. A hash algorithm digests an arbitrary-length stream of data, known as the message , and produces a fixed-size result, known as a hash . In the case of SHA-256, the result is always 256 bits in size. Hash algorithms are designed such that:

These properties make hash algorithms useful for checking the integrity of data, in the face of both accidental bit flips and deliberate tampering.

To compute a SHA-256 with the RP2350 SHA-256 accelerator:

  1. 1. Initialise the algorithm state by writing a 1 to CSR.START.
  2. 2. Write the message to the WDATA register, polling CSR.WDATA_RDY in between writes.
  1. 3. Write additional trailer and padding data to WDATA , as described in Section 12.13.1 below.
  2. 4. Poll CSR.SUM_VLD to wait for the last block to be digested.
  3. 5. Read the 256-bit result from the 8 read-only result registers starting at SUM0 .

12.13.1. Message padding

Pad message content according to the standard SHA-256 method as described in the FIPS 180-4 Secure Hash Standard : append the message with single bit 1 , then a number of 0 bits, then a 64-bit count of the number of message bits. So for a message M with length L bits the padded message should be:

  1. 1. message M
  2. 2. 1
  3. 3. k zero bits, where k is the smallest non-negative solution to the equation: \( L + 1 + k = 448 \bmod 512 \)
  4. 4. a 64-bit block indicating L (the length of the message) in binary

For example, the 8 bit ASCII message abc has a length of 24 bits. This is padded with 1 , then \( 448 - (24 + 1) = 423 \) 0 bits, and then the message length as a 64-bit value as follows:

01100001 01100010 01100011 1 00000000 000...0 00000000 000...0 00011000
|-----message-----| 1 |--423 0 bits--| |-----64 bit len-----|

12.13.2. Throughput

SHA-256 processes data one 512-bit block at a time. This requires 16 32-bit writes, 32 16-bit writes, or 64 8-bit writes to the WDATA register. An APB register write costs 4 cycles, so it takes at least 64 system clock cycles to write a data block.

Once a full block is transferred, the SHA core takes a further 57 cycles to complete the block digest. CSR.WDATA_RDY goes low, and you must not write to WDATA during this time.

The maximum throughput is therefore one block per 121 system clock cycles, or 0.53 bytes per cycle. At a 150 MHz system clock this is 79.3 MB/s. This throughput is achieved when you use 32-bit transfers from the DMA. Using narrower transfers result in lower throughput, as does polling the CSR.WDATA_RDY flag when transferring data from the processor.

12.13.3. Data size and endianness

Data is sent in message blocks of 512 bits, padded as described in Section 12.13.1 . The SHA-256 accelerator updates its 256-bit output state for each input block. The SHA-256 algorithm is defined in terms of big-endian message words, but this accelerator provides a byte swap function via CSR.BSWAP to support little-endian data. BSWAP is set by default. For more information, see the register descriptions.

WDATA supports 8-bit, 16-bit and 32-bit writes. The bus interface accumulates 8 and 16-bit writes in a 32-bit shift register before passing them into the SHA-256 algorithm core. This means you must take care when mixing writes of different sizes, because taking the shift register level from less than to greater than 32 bits in a single write will silently drop data. You can avoid this issue by not mixing WDATA write sizes within a single SHA-256 message block (64 bytes).

12.13.4. DMA DREQ interface

The block can request the DMA controller to send entire blocks of data at once. Configure transfer size using

CSR.DMA_SIZE so that the DMA controller requests the correct number of transfers.

The DREQ always requests one full SHA block of data at a time. Do not start a DMA on a non-block boundary.

12.13.5. List of registers

The SHA-256 registers start at a base address of 0x400f8000(defined as SHA256_BASE in SDK).

Table 1280. List of SHA256 registers

OffsetNameInfo
0x00CSRControl and status register
0x04WDATAWrite data register
0x08SUM0256-bit checksum result. Contents are undefined when CSR_SUM_VLD is 0.
0x0cSUM1256-bit checksum result. Contents are undefined when CSR_SUM_VLD is 0.
0x10SUM2256-bit checksum result. Contents are undefined when CSR_SUM_VLD is 0.
0x14SUM3256-bit checksum result. Contents are undefined when CSR_SUM_VLD is 0.
0x18SUM4256-bit checksum result. Contents are undefined when CSR_SUM_VLD is 0.
0x1cSUM5256-bit checksum result. Contents are undefined when CSR_SUM_VLD is 0.
0x20SUM6256-bit checksum result. Contents are undefined when CSR_SUM_VLD is 0.
0x24SUM7256-bit checksum result. Contents are undefined when CSR_SUM_VLD is 0.

SHA256: CSR Register

Offset: 0x00

Description

Control and status register

Table 1281. CSR Register

BitsDescriptionTypeReset
31:13Reserved.--
Bits 14:10 9:5 4:0 BitsDescription SHIFT DescriptionMASK_MSB : The most-significant bit allowed to pass by the mask (inclusive) MASK_LSB : The least-significant bit allowed to pass by the mask (inclusive) : Right-rotate applied to accumulator before masking. By appropriatelyType RW RW RW TypeReset 0x00 0x00 0x00 Reset
11:10Reserved.--
9:8DMA_SIZE: Configure DREQ logic for the correct DMA data size. Must be configured before the DMA channel is triggered.RW0x2
7:5Reserved.--
4ERR_WDATA_NOT_RDY: Set when a write occurs whilst the SHA-256 core is not ready for data (WDATA_RDY is low). Write one to clear.WC0x0
3Reserved.--
2SUM_VLD: If 1, the SHA-256 checksum presented in registers SUM0 through SUM7 is currently valid.RO0x1
1completed. WDATA_RDYbeen written and the digest of the current 512-bit block has subsequently : If 1, the SHA-256 core is ready to accept more data through the WDATA register.RO0x1
BitsDescriptionTypeReset
0

START: Write 1 to prepare the SHA-256 core for a new checksum.

The SUMx registers are initialised to the proper values (fractional bits of square roots of first 8 primes) and internal counters are cleared. This immediately forces WDATA_RDY and SUM_VLD high.

START must be written before initiating a DMA transfer to the SHA-256 core, because the core will always request 16 transfers at a time (1 512-bit block). Additionally, the DMA channel should be configured for a multiple of 16 32-bit transfers.

SC0x0

SHA256: WDATA Register

Offset: 0x04

Description

Write data register

Table 1282. WDATA Register

BitsDescriptionTypeReset
31:0

After pulsing START and writing 16 words of data to this register, WDATA_RDY will go low and the SHA-256 core will complete the digest of the current 512-bit block.

Software is responsible for ensuring the data is correctly padded and terminated to a whole number of 512-bit blocks.

After this, WDATA_RDY will return high, and more data can be written (if any).

This register supports word, halfword and byte writes, so that DMA from non-word-aligned buffers can be supported. The total amount of data per block remains the same (16 words, 32 halfwords or 64 bytes) and byte/halfword transfers must not be mixed within a block.

WF0x00000000

SHA256: SUM0 Register

Offset: 0x08

Table 1283. SUM0 Register

BitsDescriptionTypeReset
31:0256-bit checksum result. Contents are undefined when CSR_SUM_VLD is 0.RO0x00000000

SHA256: SUM1 Register

Offset: 0x0c

Table 1284. SUM1 Register

BitsDescriptionTypeReset
31:0256-bit checksum result. Contents are undefined when CSR_SUM_VLD is 0.RO0x00000000

SHA256: SUM2 Register

Offset: 0x10

Table 1285. SUM2 Register

BitsDescriptionTypeReset
31:0256-bit checksum result. Contents are undefined when CSR_SUM_VLD is 0.RO0x00000000

SHA256: SUM3 Register

Offset: 0x14

Table 1286. SUM3 Register

BitsDescriptionTypeReset
31:0256-bit checksum result. Contents are undefined when CSR_SUM_VLD is 0.RO0x00000000

SHA256: SUM4 Register

Offset: 0x18

Table 1287. SUM4 Register

BitsDescriptionTypeReset
31:0256-bit checksum result. Contents are undefined when CSR_SUM_VLD is 0.RO0x00000000

SHA256: SUM5 Register

Offset: 0x1c

Table 1288. SUM5 Register

BitsDescriptionTypeReset
31:0256-bit checksum result. Contents are undefined when CSR_SUM_VLD is 0.RO0x00000000

SHA256: SUM6 Register

Offset: 0x20

Table 1289. SUM6 Register

BitsDescriptionTypeReset
31:0256-bit checksum result. Contents are undefined when CSR_SUM_VLD is 0.RO0x00000000

SHA256: SUM7 Register

Offset: 0x24

Table 1290. SUM7 Register

BitsDescriptionTypeReset
31:0256-bit checksum result. Contents are undefined when CSR_SUM_VLD is 0.RO0x00000000