NTRU_IoT/infidel-code/EES401/URG_encrypt.c

117 lines
3.3 KiB
C
Raw Permalink Normal View History

2022-02-01 11:45:47 -05:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "ntru_crypto.h"
#include <time.h>
#include <sys/stat.h>
#include "ntru_crypto_drbg.h"
#include "test_common.h"
uint8_t new_rand(uint8_t *out, uint32_t num_bytes)
{
int rng;
FILE *fpointer;
fpointer = fopen("/dev/random", "rb");
fread(&rng, sizeof(int), 1, fpointer);
*out = rng;
fclose(fpointer);
return 0;
}
uint8_t get_rand(uint8_t *out, uint32_t num_bytes)
{
int rng = 10;
int urnd = open("/dev/random", "rb");
read(urnd, &rng, sizeof(int));
*out = urnd;
close(urnd);
return 0;
}
double main(char *user_input, char *i )
{
uint8_t public_key[557]; /* sized for EES401EP2 */
uint16_t public_key_len; /* no. of octets in public key */
uint16_t private_key_len; /* no. of octets in private key */
uint8_t private_key[607]; /* sized for EES401EP2 */
uint8_t encoded_public_key[593]; /* sized for EES401EP2 */
uint16_t encoded_public_key_len; /* no. of octets in encoded public key */
uint8_t ciphertext[552]; /* sized fof EES401EP2 */
uint16_t ciphertext_len; /* no. of octets in ciphertext */
uint8_t *next = NULL; /* points to next cert field to parse */
uint32_t next_len; /* no. of octets it next */
DRBG_HANDLE drbg; /* handle for instantiated DRBG */
uint32_t rc; /* return code */
bool error = FALSE; /* records if error occurred */
FILE *Handle=NULL; /* File Handle for writing NTRU key to file */
char *filename[33];
char **ptr = filename;
char buffer[557];
char *c;
char *f_name = ".cipher/ees401/cipher_";
char *f_ext = ".dat";
char *f_fin;
char f_spec[strlen(f_name)+strlen(f_ext)+5];
int r;
double cpu_time_used;
clock_t time_s, time_e;
struct stat st = {0}; /* Dir Handler */
NTRU_ENCRYPT_PARAM_SET_ID param_set_id;
param_set_id = PARAM_SET_IDS[1]; /* 0 : 401; 1 : 449; 14 : 593; */
FILE *f = fopen("keys/key-401.pub", "r");
r = fread(buffer, 1, 557, f);
fclose(f);
if (stat(".cipher/ees401", &st) == -1) {
mkdir(".cipher/ees401", 0700);
printf("Cipher directory created...\n");
}
// rc = ntru_crypto_drbg_external_instantiate(&new_rand, &drbg);
fprintf(stderr, "Testing parameter set \t: %s\n", ntru_encrypt_get_param_set_name(param_set_id));
rc = ntru_crypto_drbg_external_instantiate(
(RANDOM_BYTES_FN) &randombytes, &drbg);
if (rc != DRBG_OK)
{
printf("Error 1\n");
goto error;
}
rc = ntru_crypto_ntru_encrypt(drbg, r, buffer, 32, user_input, &ciphertext_len, NULL);
if (rc != DRBG_OK)
{
printf("Error 2\n");
goto error;
}
time_s = clock();
rc = ntru_crypto_ntru_encrypt(drbg, r, buffer, 32, user_input, &ciphertext_len, ciphertext);
time_e = clock();
cpu_time_used = (float)(time_e - time_s) / CLOCKS_PER_SEC;
if (rc != DRBG_OK)
{
printf("Error 3\n");
goto error;
}
snprintf(f_spec, sizeof(f_spec), "%s%s%s", f_name, i, f_ext);
printf("File destination : ");
printf("%s \n",f_spec);
Handle=fopen(f_spec, "wb");
fwrite(ciphertext, sizeof(ciphertext), 1, Handle);
fclose(Handle);
// free(f_spec);
// strcpy(ret_str, ciphertext);
return cpu_time_used;
error:
printf("ERROR %x\n", rc);
return 0;
}