114 lines
3.5 KiB
C
114 lines
3.5 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include "ntru_crypto.h"
|
|
|
|
//typedef uint32_t (*urnd)(uint8_t *out, uint32_t num_bytes);
|
|
//static uint8_t const aes_key[] = "Decrypt me, WTF";
|
|
uint32_t get_rand(uint8_t *out, uint32_t num_bytes)
|
|
{
|
|
int rng = 112 ;
|
|
int urnd = open("/dev/random", O_RDONLY);
|
|
read(urnd, &rng, sizeof(int));
|
|
*out = urnd;
|
|
close(urnd);
|
|
return 0;
|
|
}
|
|
|
|
int main(void) {
|
|
|
|
|
|
//uint8_t public_key[557];
|
|
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[16]; /* 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 */
|
|
|
|
rc = ntru_crypto_drbg_external_instantiate(&get_rand, &drbg);
|
|
|
|
if (rc != DRBG_OK)
|
|
goto error;
|
|
|
|
rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2, &public_key_len, NULL, &private_key_len, NULL);
|
|
|
|
if (rc != NTRU_OK)
|
|
{
|
|
error = TRUE;
|
|
}
|
|
expected_private_key_len=private_key_len;
|
|
rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2, &public_key_len, public_key, &private_key_len, private_key);
|
|
|
|
if (rc != NTRU_OK)
|
|
error = TRUE;
|
|
|
|
if (expected_private_key_len!=private_key_len)
|
|
{
|
|
fprintf(stderr, "PRivate key length is different\n");
|
|
error = TRUE;
|
|
}
|
|
|
|
printf("Key sucessfully generated. \n");
|
|
|
|
rc = ntru_crypto_drbg_uninstantiate(drbg);
|
|
if ((rc != DRBG_OK) || error)
|
|
{
|
|
error = TRUE;
|
|
}
|
|
|
|
printf("KEY DRBG Success. \n");
|
|
|
|
Handle=fopen("Nino-ntru-key.raw","wb");
|
|
if (Handle!=NULL){
|
|
printf("Writing Pirvatkey\n");
|
|
fwrite(private_key, private_key_len, 1, Handle);
|
|
fclose(Handle);
|
|
}
|
|
Handle=fopen("Nino-ntru-pubkey.raw","wb");
|
|
if(Handle!=NULL){
|
|
printf("Writing Publickey\n");
|
|
fwrite(public_key, public_key_len, 1, Handle);
|
|
fclose(Handle);
|
|
}
|
|
|
|
rc = ntru_crypto_ntru_encrypt_publicKey2SubjectPublicKeyInfo(public_key_len, public_key, &encoded_public_key_len, NULL);
|
|
if (rc != NTRU_OK)
|
|
goto error;
|
|
printf("DER encoded, sized requierd %d . \n", encoded_public_ket_len);
|
|
expected_encoded_public_key_len = encoded_public_key_len;
|
|
rc = ntru_crypto_ntru_encrypt_publicKey2SubjectPublicKeyInfo(public_key_len, public_key, &encoded_public_key_len, encoded_public_key);
|
|
|
|
if (expected_encoded_public_key_len!=encoded_public_key_len)
|
|
{
|
|
fprintf(stderr, "Different encoded pub key detected\n");
|
|
error = TRUE;
|
|
}
|
|
|
|
printf("Pub DER Encoding success\n");
|
|
|
|
rc = ntru_crypto_ntru_encrypt(dbrg, public_key_len, public_key, sizeof(aes_key), aes_key, &ciphertext_len, ciphertext);
|
|
|
|
printf("%s\n", ciphertext);
|
|
printf("Done BLyat!!!!\n");
|
|
|
|
error:
|
|
printf("ERROR %x\n", rc);
|
|
return 1;
|
|
|
|
}
|