5.2 ALGO Guide
This chapter covers the files and the interface source of the N32H49x crypto algorithm library (ALGO), and the ALGO_DEMO example. The library is built on the hardware crypto algorithm accelerator (SAC); read it together with the “Crypto algorithm hardware accelerator (SAC)” chapter of the user manual for the supported algorithm list, key lengths, and working modes.
5.2.1 Related files
| Relative path | Purpose |
|---|---|
firmware/n32h49x_algo_lib/keil/inc/ |
Keil header directory: n32h49x_algo_common.h, n32h49x_aes.h, n32h49x_des.h, n32h49x_hash.h, n32h49x_rng.h, and n32h49x_sm4.h, declaring the common helper, AES, DES/3DES, hash, random-number, and SM4 interfaces |
firmware/n32h49x_algo_lib/keil/lib/ |
Keil precompiled libraries: n32h49x_common.lib, n32h49x_aes.lib, n32h49x_des.lib, n32h49x_hash.lib, n32h49x_rng.lib, and n32h49x_sm4.lib |
firmware/n32h49x_algo_lib/gcc/ |
GCC directory: the same 6 headers under inc/, and Common.a, AES.a, DES.a, HASH.a, RNG.a, and SM4.a under lib/ |
projects/n32h49x_EVAL/examples/ALGO/ |
ALGO example root, containing the single ALGO_DEMO example |
projects/n32h49x_EVAL/examples/ALGO/ALGO_DEMO/ |
Project layout of the ALGO_DEMO example: MDK-ARM/, EWARM/, inc/, src/, and readme.txt |
projects/n32h49x_EVAL/bsp/ |
Board support files used by the example: log initialization and print UART remapping |
5.2.2 API reference
ALGO is a precompiled crypto algorithm library shipped with the SDK. It has no inc/src source files as a standard peripheral driver does; its interfaces are provided only as headers plus precompiled libraries, all under firmware/n32h49x_algo_lib/. The library comes in two toolchain sets: the Keil set has its headers under keil/inc/ and its libraries under keil/lib/ (n32h49x_common.lib, n32h49x_aes.lib, n32h49x_des.lib, n32h49x_hash.lib, n32h49x_rng.lib, and n32h49x_sm4.lib); the GCC set has its headers under gcc/inc/ and its libraries under gcc/lib/ (Common.a, AES.a, DES.a, HASH.a, RNG.a, and SM4.a). The two sets use the same header names, so a project only needs to add the matching inc/ to the header search path and the matching lib/ to the library path for the toolchain in use. The public entry points are grouped by header below:
n32h49x_algo_common.h:RandomSort,Cpy_U8,Cpy_U32,XOR_U8,XOR_U32,SetZero_U8,SetZero_U32,ReverseBytesInWord_U32,Cmp_U32, andCmp_U8, plus the return-value enumerationCpy_OK,SetZero_OK,XOR_OK,Reverse_OK,Cmp_EQUAL, andCmp_UNEQUAL.n32h49x_sm4.h:SM4_Init,SM4_Crypto,SM4_Close, andSM4_Version, plus the parameter structureSM4_PARMand the working-mode and direction macrosSM4_ECB,SM4_CBC,SM4_ENC, andSM4_DEC.n32h49x_aes.h:AES_Init,AES_Crypto,AES_Close, andAES_Version, plus the parameter structureAES_PARMand the working-mode and direction macrosAES_ECB,AES_CBC,AES_CTR,AES_ENC, andAES_DEC.n32h49x_des.h:DES_Init,DES_Crypto,DES_Close, andDES_Version, plus the parameter structureDES_PARM.n32h49x_hash.h: the streaming interfacesHASH_Init,HASH_Start,HASH_Update,HASH_Complete, andHASH_Close, the one-shot interfacesSM3_Hash,SHA1_Hash,SHA224_Hash,SHA256_Hash, andMD5_Hash, plusHASH_Version, the context structureHASH_CTX, and the algorithm descriptorsHASH_ALG_SHA1,HASH_ALG_SHA224,HASH_ALG_SHA256,HASH_ALG_MD5, andHASH_ALG_SM3.n32h49x_rng.h:GetPseudoRand_U32,GetTrueRand_U32, andRNG_Version.
Every symmetric algorithm is used in three steps, initialize, crypto, and close: first XXX_Init fills the key, plaintext or ciphertext, and working mode into the parameter structure, then XXX_Crypto performs one encryption or decryption, and finally XXX_Close releases the resources; changing only the direction field of the same parameter structure reuses it for the opposite direction. A hash algorithm can either feed data in pieces through HASH_Init/HASH_Start/HASH_Update/HASH_Complete/HASH_Close, or use a one-shot interface such as SM3_Hash or MD5_Hash. The library version of each module is read through SM4_Version, AES_Version, DES_Version, HASH_Version, and RNG_Version.
5.2.3 Demo descriptions
This section covers the single ALGO example: ALGO_DEMO runs every algorithm in the library with a fixed set of test vectors and compares each result against the expected value.
5.2.3.1 ALGO_DEMO combined algorithm library demo
Example directory: projects/n32h49x_EVAL/examples/ALGO/ALGO_DEMO/
Initialize the log UART and divide the interface and algorithm work clocks to 120 MHz→Run the SM4, AES, and DES symmetric algorithms and the digest algorithms→Compare each result against the expected vector and print success or fail→Obtain pseudo-random and true random numbers and print them→Print the final notice and stay in the main loop after everything passes
| Item | Source analysis |
|---|---|
| Function | Demonstrates the full call flow of the crypto algorithm library with a fixed set of test vectors; it runs SM4, AES128/192/256, DES, and 2KEY/3KEY 3DES encryption and decryption together with SHA, SM3, and MD5 digests and random-number calls, and compares each result against the expected value. |
| Source files | src/main.c: main and the declarations of the algorithm test functions;src/algo_demo.c: the implementations of SM4_test, AES_128_test, DES_test, HASH_test, MD5_test, and the other test functions plus TestRand;inc/algo_demo.h and inc/n32h47x_48x_it.h: function declarations and the interrupt header;readme.txt: function, environment, and usage;MDK-ARM/ and EWARM/: Keil and IAR projects. |
| Key configuration | The clock source is HSI+PLL; both the interface clock and the algorithm work clock are divided by 2 and set to 120 MHz, because the algorithm library does not support a 240 MHz main frequency; the print UART is PA9 at 115200 baud. |
| Program and data path | main first calls log_init, then divides the interface clock and the algorithm work clock to 120 MHz, and then calls each algorithm test function and TestRand in turn;each test function fills the input, key, and working mode into a parameter structure, obtains the result through the init and crypto interfaces, and compares it with the expected array through Cmp_U32. |
| Main APIs | SM4_Init, SM4_Crypto, SM4_Close, AES_Init, AES_Crypto, DES_Init, DES_Crypto, HASH_Init, HASH_Update, HASH_Complete, GetPseudoRand_U32, GetTrueRand_U32 |
| Verification | After reset the UART prints Algorithm demo start.;each algorithm prints its ...-Test success when it passes, or ...-Test fail and returns 0x5A5A5A5A when it fails;after all tests pass it prints ALGO demo all test OK!. |
| Use note | The precompiled algorithm libraries come in a Keil set and a GCC set, so the Keil project uses n32h49x_*.lib while the IAR/GCC project uses *.a;the algorithm library does not support a 240 MHz main frequency, so the system clock must be divided by 2 to 120 MHz before calling it; each test function returns 0x5A5A5A5A when a test fails.Toolchains are KEIL MDK-ARM V5.34 and IAR EWARM 8.50.1. |
Core-code walkthrough and porting
After adding the library files for the toolchain, the example verifies every algorithm in the library with a fixed set of test vectors and compares each result against the expected value.
Data path: main configures the interface clock and the algorithm work clock → each test function fills the key, plaintext, and working mode into a parameter structure → the init and crypto interfaces finish the encryption, decryption, or digest → Cmp_U32 compares against the expected array and reports success or failure → TestRand obtains pseudo-random and true random numbers
1. Divide the clocks first, then call the algorithms in order. The library requires both the interface clock and the algorithm work clock to stay at or below 120 MHz, so the example divides both by 2 before any algorithm call.
int main(void)
{
log_init();
log_info("-----------------------\nAlgorithm demo start.\n");
/* config interface clock is 120MHz */
RCC_ConfigPeriphClk(RCC_PERIPH_HCLK_DIV2);
/* config work clock is 120MHz(must not exceed interface clock) */
RCC_ConfigRngcClk(RCC_RNGCCLK_SYSCLK_DIV2);
RCC_ConfigPeriphClk(RCC_PERIPH_HCLK_DIV2) halves the interface clock, and RCC_ConfigRngcClk(RCC_RNGCCLK_SYSCLK_DIV2) halves the algorithm work clock.
2. Fill a parameter structure, then call the symmetric algorithm in three steps. The plaintext is first copied into the output buffer for in-place crypto, and the key, length, working mode, and direction fields are then filled in one by one.
Cpy_U32(out, in1, 32);
SM4_Parm.in = out;
SM4_Parm.key = key1;
SM4_Parm.out = out;
SM4_Parm.inWordLen = 32;
SM4_Parm.workingMode = SM4_ECB;
SM4_Parm.EnDeMode = SM4_ENC;
ret = SM4_Init(&SM4_Parm);
Once the structure is filled, SM4_Init, SM4_Crypto, and SM4_Close are called in turn to finish one operation; decryption only needs EnDeMode changed to SM4_DEC and, for CBC mode, an iv added, so the same structure is reused.
3. Verify the result with a known test vector. Both the return value and the expected array are checked: a non-success return or a mismatch against the expected vector sets the flag to 0x5A5A5A5A.
if(ret != SM4_Crypto_OK)
{
flag1 = 0x5A5A5A5A;
}
else
{
if(Cmp_U32(SM4_ECB_EN, 32, out, 32))
{
flag1 = 0x5A5A5A5A;
}
else
{
flag1 = 0;
}
}
One flagN is produced per mode, and if (flag1 | flag2 | flag3 | flag4) combines them at the end, so any failure prints SM4-Test fail and returns 0x5A5A5A5A.
4. Get pseudo-random and true random numbers. A pseudo-random call takes two seed words, and a true random call uses the hardware entropy source directly; both report success by returning RNG_OK.
void TestRand(void)
{
uint32_t buf[8];
uint32_t seed[2] = {1, 2};
uint8_t i;
printf("\n");
if (RNG_OK != GetPseudoRand_U32(buf, 8, seed))
{
printf("GetPseudoRand_U32 failed.\n");
}
else
{
printf("Pseudo random with seed 1,2 success: ");
printf("\n");
}
The third argument of GetPseudoRand_U32 is a two-word seed array, and changing the seed yields a different sequence; GetTrueRand_U32 is called with the number of words needed and is used where a true random source is required.
Corresponding source files: src/main.c, src/algo_demo.c
5.2.4 Notes and troubleshooting
| Scenario | Check & action | Source |
|---|---|---|
| Symmetric algorithms supported by hardware | The crypto algorithm hardware accelerator supports DES/3DES, AES, and SM4 as its symmetric algorithms; the library only provides crypto interfaces within that set. |
User manual |
| Working modes of the symmetric algorithms | AES supports ECB, CBC, and CTR; DES/3DES and SM4 support ECB and CBC. |
User manual |
| AES key length | AES supports 128-bit, 192-bit, and 256-bit key lengths; the key must be prepared for the chosen length before the call. |
User manual |
| Key modes of 3DES | TDES supports both the 2KEY and the 3KEY key modes. | User manual |
| Hash and digest algorithms supported by hardware | The hardware supports the SHA1, SHA224, and SHA256 hash algorithms together with the MD5 digest algorithm and the SM3 hash algorithm. | User manual |
| Random-number generation | The hardware accelerator supports random-number generation. | User manual |
| Algorithm performance and usage support | Performance data and specific usage support for the crypto algorithms require contacting the vendor’s sales representative; this manual only lists the supported algorithms and modes and gives no throughput figures. |
User manual |