NTRU_IoT/legacy-code/deb2/ntru_nino/EES593/URG_encrypt.c
2022-02-01 23:45:47 +07:00

95 lines
2.8 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "ntru_crypto.h"
#include <time.h>
uint32_t get_rand(uint8_t *out, uint32_t num_bytes)
{
int rng = 50;
int urnd = open("/dev/random", O_RDONLY);
read(urnd, &rng, sizeof(int));
*out = urnd;
close(urnd);
return 0;
}
double main(char user_input[], char *i )
{
uint8_t public_key[821]; /* sized for EES401EP2 */
uint16_t public_key_len; /* no. of octets in public key */
uint8_t private_key[891]; /* sized for EES401EP2 */
uint16_t private_key_len; /* no. of octets in private key */
uint16_t expected_private_key_len;
uint16_t expected_encoded_public_key_len;
uint8_t encoded_public_key[855]; /* sized for EES401EP2 */
uint16_t encoded_public_key_len; /* no. of octets in encoded public key */
uint8_t ciphertext[816]; /* sized fof EES401EP2 */
uint16_t ciphertext_len; /* no. of octets in ciphertext */
uint8_t plainrtext[86]; /* sized fof EES401EP2 */
uint16_t plaintext_len; /* no. of octets in ciphertext */
char *ret_str = 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[821];
char *c;
char *f_name = "/tmp/cipher_EES593_";
char *f_ext = ".dat";
char f_spec[strlen(f_name)+strlen(f_ext)+5];
int r;
double cpu_time_used;
clock_t time_s, time_e;
FILE *f = fopen("EES593/EES593-ntru-pub.raw", "rb");
r = fread(buffer, 1, 821, f);
fclose(f);
rc = ntru_crypto_drbg_external_instantiate(&get_rand, &drbg);
if (rc != DRBG_OK)
{
printf("Error 1");
goto error;
}
rc = ntru_crypto_ntru_encrypt(drbg, r, buffer, 16, user_input, &ciphertext_len, NULL);
if (rc != DRBG_OK)
{
printf("Error 1");
goto error;
}
time_s = clock();
rc = ntru_crypto_ntru_encrypt(drbg, r, buffer, 16, user_input, &ciphertext_len, ciphertext);
time_e = clock();
if (rc != DRBG_OK)
{
printf("Error 1");
goto error;
}
cpu_time_used = (float)(time_e - time_s) / CLOCKS_PER_SEC;
if (rc != DRBG_OK)
{
printf("Error 1");
goto error;
}
snprintf(f_spec, sizeof(f_spec), "%s%s%s", f_name, i, f_ext);
Handle=fopen(f_spec, "wb");
fwrite(ciphertext, ciphertext_len, 1, Handle);
fclose(Handle);
return cpu_time_used;
error:
printf("ERROR %x\n", rc);
return 0;
}