NTRU_IoT/legacy-code/sample/URG_decrypt.c

81 lines
2.7 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 <time.h>
#include "ntru_crypto.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;
}
char * main(char user_input[])
{
uint8_t public_key[557]; /* sized for EES401EP2 */
uint16_t public_key_len; /* no. of octets in public key */
uint8_t private_key[607]; /* 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[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 plaintext[552]; /* size of AES-128 key */
uint16_t plaintext_len; /* no. of octets in plaintext */
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[607];
char *buffer2 = 0;
char in_buffer[255];
char plain_buffer[16];
char *c = malloc(4);
int r;
int s;
double cpu_time_used;
rc = ntru_crypto_drbg_uninstantiate(drbg);
FILE *f = fopen("Nino-ntru-key.raw", "r");
r = fread(buffer, 1, 607, f);
fclose(f);
private_key_len = 607;
printf("C Log DEC: %d bytes Private Key\n", r);
printf("C Log DEC: user_input Len %d\n", sizeof(user_input));
rc = ntru_crypto_ntru_decrypt(r, buffer, 552,user_input, &plaintext_len, NULL);
if (rc != NTRU_OK)
printf("ERROR 1\n");
rc = ntru_crypto_ntru_decrypt(r, buffer, 552,user_input, &plaintext_len, plaintext);
if (rc != NTRU_OK)
printf("ERROR 2\n");
printf("C Log DEC : your plain: %s\n", plaintext);
printf("C Log DEC : your plain LEN: %d\n", plaintext_len);
//printf("your time spent: %lf\n", cpu_time_used);
//snprintf(c, sizeof(c), "%s", plaintext);
strcpy(c, plaintext);
return c;
error:
printf("PROBLEM BUDDY %d\n", rc);
exit(EXIT_FAILURE);
return 0;
}