HASH Example

Source code path: example/hal/hash

Supported Platforms

  • sf32lb52-lcd_n16r8

Overview

This example demonstrates using the HASH engine for HASH calculations, including:

  • Single HASH calculation (data not split, synchronous polling mode)

    • SHA1

    • SHA224

    • SHA256

    • SM3

  • Single HASH calculation (data not split, asynchronous interrupt mode)

    • SHA1

  • Multiple calculations (data split, for scenarios with large data)

    • SHA1

Example Usage

Hardware Requirements

Before running this example, you need to prepare a development board supported by this example

Compilation and Programming

Switch to the example project directory and run the scons command to compile:

scons --board=sf32lb52-lcd_n16r8 -j32

Run build_sf32lb52-lcd_n16r8_hcpu\uart_download.bat, select the port as prompted to download:

$ ./uart_download.bat

     Uart Download

please input the serial port num:5

For detailed steps on compilation and downloading, please refer to the relevant introduction in Getting Started Guide.

Expected Results

After the example starts, the serial port outputs the following:

  1. Print raw data:

10-19 22:13:46:066    Input raw data:
10-19 22:13:46:067     0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
10-19 22:13:46:069     0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x64,
10-19 22:13:46:071     0x61, 0x74, 0x61, 0x20, 0x75, 0x73, 0x65, 0x64,
10-19 22:13:46:072     0x20, 0x66, 0x6f, 0x72, 0x20, 0x74, 0x65, 0x73,
10-19 22:13:46:074     0x74, 0x69, 0x6e, 0x67, 0x20, 0x68, 0x61, 0x73,
10-19 22:13:46:076     0x68, 0x20, 0x61, 0x6c, 0x67, 0x6f, 0x72, 0x69,
10-19 22:13:46:078     0x74, 0x68, 0x6d, 0x20, 0x77, 0x69, 0x74, 0x68,
10-19 22:13:46:124     0x20, 0x74, 0x68, 0x65, 0x20, 0x68, 0x61, 0x73,
10-19 22:13:46:129     0x68, 0x20, 0x68, 0x61, 0x72, 0x64, 0x77, 0x61,
10-19 22:13:46:133     0x72, 0x65, 0x20, 0x62, 0x6c, 0x6f, 0x63, 0x6b,
10-19 22:13:46:139     0x2e, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
10-19 22:13:46:144     0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20,
10-19 22:13:46:149     0x64, 0x61, 0x74, 0x61, 0x20, 0x75, 0x73, 0x65,
10-19 22:13:46:154     0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x74, 0x65,
10-19 22:13:46:158     0x73, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x68, 0x61,
10-19 22:13:46:163     0x73, 0x68, 0x20, 0x61, 0x6c, 0x67, 0x6f, 0x72,
10-19 22:13:46:168     0x69, 0x74, 0x68, 0x6d, 0x20, 0x77, 0x69, 0x74,
10-19 22:13:46:174     0x68, 0x20, 0x74, 0x68, 0x65, 0x20, 0x68, 0x61,
10-19 22:13:46:179     0x73, 0x68, 0x20, 0x68, 0x61, 0x72, 0x64, 0x77,
10-19 22:13:46:184     0x61, 0x72, 0x65, 0x20, 0x62, 0x6c, 0x6f, 0x63,
10-19 22:13:46:189     0x6b, 0x2e, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69,
10-19 22:13:46:194     0x73, 0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74,
10-19 22:13:46:198     0x20, 0x64, 0x61, 0x74, 0x61, 0x20, 0x75, 0x73,
10-19 22:13:46:204     0x65, 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x74,
10-19 22:13:46:209     0x65, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x68,
10-19 22:13:46:214     0x61, 0x73, 0x68, 0x20, 0x61, 0x6c, 0x67, 0x6f,
10-19 22:13:46:219     0x72, 0x69, 0x74, 0x68, 0x6d, 0x20, 0x77, 0x69,
10-19 22:13:46:224     0x74, 0x68, 0x20, 0x74, 0x68, 0x65, 0x20, 0x68,
10-19 22:13:46:229     0x61, 0x73, 0x68, 0x20, 0x68, 0x61, 0x72, 0x64,
10-19 22:13:46:234     0x77, 0x61, 0x72, 0x65, 0x20, 0x62, 0x6c, 0x6f,
10-19 22:13:46:238     0x63, 0x6b, 0x2e, 0x00,
  1. SHA1 algorithm calculation (synchronous mode):

10-19 22:13:46:243    SHA1:
10-19 22:13:46:247    HASH Result:
10-19 22:13:46:251     0x8a, 0x50, 0x60, 0xae, 0x38, 0xfd, 0x7c, 0x73,
10-19 22:13:46:255     0x5b, 0x85, 0x53, 0x41, 0x51, 0x99, 0x77, 0xb7,
10-19 22:13:46:260     0x8f, 0x91, 0xee, 0x70,
  1. SHA224 algorithm calculation (synchronous mode):

10-19 22:13:46:265    SHA224:
10-19 22:13:46:270    HASH Result:
10-19 22:13:46:274     0x08, 0xef, 0x97, 0x98, 0x25, 0x90, 0xc4, 0xa2,
10-19 22:13:46:278     0x01, 0xb7, 0xb1, 0x82, 0x4b, 0x6f, 0x24, 0xfc,
10-19 22:13:46:282     0x5d, 0x0a, 0xab, 0x8b, 0x96, 0xcc, 0xd2, 0x61,
10-19 22:13:46:287     0xeb, 0xb8, 0x10, 0x94,
  1. SHA256 algorithm calculation (synchronous mode):

10-19 22:13:46:292    SHA256:
10-19 22:13:46:296    HASH Result:
10-19 22:13:46:300     0xea, 0x72, 0x91, 0xbd, 0xbe, 0xb1, 0x6b, 0xe7,
10-19 22:13:46:305     0x9c, 0xe2, 0xbf, 0xf4, 0xfa, 0x40, 0x12, 0x35,
10-19 22:13:46:311     0xfc, 0xbd, 0x44, 0x24, 0xb9, 0x5c, 0x09, 0xb0,
10-19 22:13:46:316     0x10, 0x67, 0x62, 0x76, 0x8e, 0xdb, 0xc5, 0x41,
  1. SM3 algorithm calculation (synchronous mode):

10-19 22:13:46:321    SM3:
10-19 22:13:46:324    HASH Result:
10-19 22:13:46:328     0xfc, 0xf6, 0x99, 0x0c, 0x78, 0xb2, 0x0c, 0x4f,
10-19 22:13:46:333     0xf2, 0x54, 0x5e, 0xf0, 0xdd, 0x64, 0xd3, 0x36,
10-19 22:13:46:339     0x2a, 0x98, 0xdb, 0x91, 0x74, 0x7d, 0x29, 0xaa,
10-19 22:13:46:344     0x33, 0x53, 0xa7, 0x9d, 0xae, 0xa6, 0x6b, 0x65,
  1. SHA1 algorithm calculation (interrupt mode):

10-19 22:13:46:349    SHA1(IT):
10-19 22:13:46:353    HASH Result(IT):
10-19 22:13:46:357     0x8a, 0x50, 0x60, 0xae, 0x38, 0xfd, 0x7c, 0x73,
10-19 22:13:46:362     0x5b, 0x85, 0x53, 0x41, 0x51, 0x99, 0x77, 0xb7,
10-19 22:13:46:368     0x8f, 0x91, 0xee, 0x70,
  1. SHA1 algorithm calculation (multiple calculation mode with data splitting):

10-19 22:13:46:373    SHA1(multiple):
10-19 22:13:46:377    HASH Result(multiple):
10-19 22:13:46:382     0x8a, 0x50, 0x60, 0xae, 0x38, 0xfd, 0x7c, 0x73,
10-19 22:13:46:386     0x5b, 0x85, 0x53, 0x41, 0x51, 0x99, 0x77, 0xb7,
10-19 22:13:46:391     0x8f, 0x91, 0xee, 0x70,

Exception Diagnosis

Warning

If HASH input and output memory uses PSRAM, attention must be paid to cache synchronization:

  1. Before executing HASH run, cache synchronization is needed for input memory to ensure HASH reads the required data.

  2. After HASH completion, cache synchronization may be needed for output memory to ensure correct data is read.

Reference Documents

Update Log

Version

Date

Release Notes

0.0.1

10/2024

Initial version