16.2. Usage - 5.2 English - 57404

AOCL User Guide (57404)

Document ID
57404
Release Date
2025-12-29
Version
5.2 English

The following source files are included in the AOCL-SecureRNG package:

  1. include/secrng.h : A header file that has declaration of all the library APIs.

  2. src_lib/secrng.c : Contains the implementation of the APIs.

  3. src_test/secrng_test.c : Test application to test all the library APIs.

  4. Makefile : To compile the library and test the application.

You can use the included Makefile to compile the source files and generate dynamic and static libraries. Then, you can link it to your application and invoke the required APIs.

The following code snippet shows a sample usage of the library APIs:

Listing 16.1 test_secrng.c#
 1/* Check for RDRAND instruction support */
 2int ret = is_RDRAND_supported();
 3int N = 1000;
 4
 5/* If RDRAND supported */
 6if (ret == SECRNG_SUPPORTED)
 7{
 8  uint64_t rng64;
 9
10  /* Get 64-bit random number */
11  ret = get_rdrand64u(&rng64, 0);
12
13  if (ret == SECRNG_SUCCESS)
14    printf("RDRAND rng 64-bit value %lu\n\n", rng64);
15  else
16    printf("Failure in retrieving random value using RDRAND!\n");
17
18  /* Get a range of 64-bit random values */
19  uint64_t* rng64_arr = (uint64_t*) malloc(sizeof(uint64_t) * N);
20
21  ret = get_rdrand64u_arr(rng64_arr, N, 0);
22  if (ret == SECRNG_SUCCESS)
23  {
24    printf("RDRAND for %u 64-bit random values succeeded!\n", N);
25    printf("First 10 values in the range : \n");
26    for (int i = 0; i < (N > 10? 10 : N); i++)
27      printf("%lu\n", rng64_arr[i]);
28  }
29  else
30    printf("Failure in retrieving array of random values using RDRAND!\n");
31}
32else
33{
34  printf("No support for RDRAND!\n");
35}

In the example, get_rdrand64u is invoked to return a single 64-bit random value and get_rdrand64u_arr is used to return an array of 1000, 64-bit random values.