unplugged-system/vendor/unplugged/proprietary/opensource/trng/jni/main.c

85 lines
1.9 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <read_rand.h>
#define MTK_SIP_KERNEL_GET_RND 0x8200026A
// Function to print a single line of the hexdump
static void print_hexdump_line(const unsigned char *buffer, int bytes_read, int offset)
{
// Print the offset
printf("%08x ", offset);
// Print the hex representation
for (int i = 0; i < 16; i++) {
if (i < bytes_read) {
printf("%02x ", buffer[i]);
} else {
printf(" ");
}
// Print an extra space between groups of 8 bytes
if (i == 7) {
printf(" ");
}
}
printf(" |");
// Print the ASCII representation
for (int i = 0; i < bytes_read; i++) {
if (buffer[i] >= 32 && buffer[i] <= 126) {
printf("%c", buffer[i]);
} else {
printf(".");
}
}
printf("|\n");
}
// Function to perform hexdump on a given buffer
static void hexdump(const unsigned char *buffer, size_t size)
{
int offset = 0;
while (size > 0) {
int bytes_to_print = size > 16 ? 16 : size;
print_hexdump_line(buffer, bytes_to_print, offset);
buffer += bytes_to_print;
offset += bytes_to_print;
size -= bytes_to_print;
}
}
int main(int argc, char *argv[])
{
int rc;
uint8_t buf[1024] = { };
int rand_len;
if (argc < 2) {
fprintf(stderr, "USAGE: %s <rand length>\n", argv[0]);
return 1;
}
rand_len = atoi(argv[1]);
if (rand_len < 0 || rand_len > (int) sizeof(buf)) {
fprintf(stderr, "rand length must be between 0 and 1024\n");
return 1;
}
rc = read_hwrandom(buf, (size_t) rand_len);
if (rc != 0) {
fprintf(stderr, "read hwrandom failed with %d\n", rc);
return 1;
}
hexdump(buf, (size_t) rand_len);
return 0;
}