| | #define _CRT_SECURE_NO_WARNINGS
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| |
|
| |
|
| | #include <stdio.h>
|
| | #include <stdlib.h>
|
| | #include <string.h>
|
| | #include <windows.h>
|
| | #include "ntru_crypto.h"
|
| | #pragma comment(lib, "ntruenc.lib")
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| |
|
| | static uint8_t
|
| | get_entropy(
|
| | ENTROPY_CMD cmd,
|
| | uint8_t* out)
|
| | {
|
| | |
| | |
| |
|
| | uint8_t seed[28];
|
| |
|
| | HCRYPTPROV hProv;
|
| | if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT) &&
|
| | !CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT | CRYPT_NEWKEYSET)) hProv = NULL;
|
| | if (hProv != 0) {
|
| | CryptGenRandom(hProv, 28, seed);
|
| | }
|
| | else {
|
| | printf("Can't initialize HCRYPTPROV, bye!\n");
|
| | ExitProcess(0);
|
| | }
|
| |
|
| | static size_t index;
|
| |
|
| | if (cmd == INIT) {
|
| |
|
| | index = 0;
|
| | return 1;
|
| | }
|
| |
|
| | if (out == NULL)
|
| | return 0;
|
| |
|
| | if (cmd == GET_NUM_BYTES_PER_BYTE_OF_ENTROPY) {
|
| | |
| | |
| |
|
| | *out = 1;
|
| | return 1;
|
| | }
|
| |
|
| | if (cmd == GET_BYTE_OF_ENTROPY) {
|
| | if (index == sizeof(seed))
|
| | return 0;
|
| |
|
| | *out = seed[index++];
|
| | return 1;
|
| | }
|
| | return 0;
|
| | }
|
| |
|
| |
|
| | |
| | |
| |
|
| | static uint8_t const pers_str[] = {
|
| | 'S', 'S', 'L', ' ', 'a', 'p', 'p', 'l', 'i', 'c', 'a', 't', 'i', 'o', 'n'
|
| | };
|
| |
|
| |
|
| |
|
| | static uint8_t const aes_key[] = {
|
| | 0xf3, 0xe9, 0x87, 0xbb, 0x18, 0x08, 0x3c, 0xaa,
|
| | 0x7b, 0x12, 0x49, 0x88, 0xaf, 0xb3, 0x22, 0xd8
|
| | };
|
| |
|
| |
|
| |
|
| | void
|
| | DumpHex(
|
| | const unsigned char* buf,
|
| | int len)
|
| | {
|
| | int i;
|
| | for (i = 0; i < len; i++)
|
| | {
|
| | if (i & 0x1f) printf(":");
|
| | printf("%02X", buf[i]);
|
| | if ((i & 0x1f) == 0x1f) printf("\n");
|
| | }
|
| | printf("\n");
|
| | }
|
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | int
|
| | main(void)
|
| | {
|
| | uint8_t public_key[557];
|
| | uint16_t public_key_len;
|
| | uint8_t private_key[607];
|
| | uint16_t private_key_len;
|
| | uint16_t expected_private_key_len;
|
| | uint16_t expected_encoded_public_key_len;
|
| | uint8_t encoded_public_key[593];
|
| | uint16_t encoded_public_key_len;
|
| | uint8_t ciphertext[552];
|
| | uint16_t ciphertext_len;
|
| | uint8_t plaintext[16];
|
| | uint16_t plaintext_len;
|
| | uint8_t* next = NULL;
|
| | uint32_t next_len;
|
| | DRBG_HANDLE drbg;
|
| | uint32_t rc;
|
| | bool error = FALSE;
|
| | FILE* Handle = NULL;
|
| |
|
| | |
| | |
| | |
| |
|
| | rc = ntru_crypto_drbg_instantiate(112, pers_str, sizeof(pers_str),
|
| | (ENTROPY_FN)&get_entropy, &drbg);
|
| | if (rc != DRBG_OK)
|
| |
|
| | goto error;
|
| | printf("DRBG at 112-bit security for key generation instantiated "
|
| | "successfully.\n");
|
| |
|
| |
|
| | |
| | |
| |
|
| | rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2, &public_key_len,
|
| | NULL, &private_key_len, NULL);
|
| | if (rc != NTRU_OK)
|
| |
|
| | goto error;
|
| | printf("Public-key buffer size required: %d octets.\n", public_key_len);
|
| | printf("Private-key buffer size required: %d octets.\n", private_key_len);
|
| |
|
| |
|
| | |
| | |
| | |
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| | 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 than expected\n");
|
| | error = TRUE;
|
| | }
|
| | printf("Key-pair for NTRU_EES401EP2 generated successfully.\n");
|
| |
|
| |
|
| |
|
| | rc = ntru_crypto_drbg_uninstantiate(drbg);
|
| | if ((rc != DRBG_OK) || error)
|
| |
|
| | goto error;
|
| | printf("Key-generation DRBG uninstantiated successfully.\n");
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | Handle = fopen("sample-ntru-key.raw", "wb");
|
| | if (Handle != NULL)
|
| | {
|
| | printf("Writing private key to ntru-key.raw\n");
|
| | fwrite(private_key, private_key_len, 1, Handle);
|
| | fclose(Handle);
|
| | }
|
| |
|
| | Handle = fopen("sample-ntru-pubkey.raw", "wb");
|
| | if (Handle != NULL)
|
| | {
|
| | printf("Writing public key to ntru-pubkey.raw\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 public-key buffer size required: %d octets.\n",
|
| | encoded_public_key_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, "encoded_public_key_len is different than expected\n");
|
| | error = TRUE;
|
| | }
|
| |
|
| | printf("Public key DER-encoded for SubjectPublicKeyInfo successfully.\n");
|
| |
|
| | printf("DER encoded public key in hex:\n");
|
| | DumpHex(encoded_public_key, encoded_public_key_len);
|
| |
|
| | Handle = fopen("sample-ntru-pubkey.der", "wb");
|
| | if (Handle != NULL)
|
| | {
|
| | printf("Writing DER encoded public key to ntru-pubkey.der\n");
|
| | fwrite(encoded_public_key, encoded_public_key_len, 1, Handle);
|
| | fclose(Handle);
|
| | }
|
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | next = encoded_public_key; |
| |
|
| | next_len = encoded_public_key_len;
|
| | rc = ntru_crypto_ntru_encrypt_subjectPublicKeyInfo2PublicKey(next,
|
| | &public_key_len, NULL, &next, &next_len);
|
| | if (rc != NTRU_OK)
|
| |
|
| | goto error;
|
| | printf("Public-key buffer size required: %d octets.\n", public_key_len);
|
| |
|
| |
|
| | |
| | |
| | |
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | |
| |
|
| | rc = ntru_crypto_ntru_encrypt_subjectPublicKeyInfo2PublicKey(next,
|
| | &public_key_len, public_key, &next, &next_len);
|
| | if (rc != NTRU_OK)
|
| | |
| | |
| | |
| |
|
| | goto error;
|
| | printf("Public key decoded from SubjectPublicKeyInfo successfully.\n");
|
| |
|
| |
|
| | |
| | |
| | |
| | |
| |
|
| | rc = ntru_crypto_drbg_instantiate(112, NULL, 0, (ENTROPY_FN)&get_entropy,
|
| | &drbg);
|
| | if (rc != DRBG_OK)
|
| |
|
| | goto error;
|
| | printf("DRBG at 112-bit security for encryption instantiated "
|
| | "successfully.\n");
|
| |
|
| |
|
| | |
| | |
| | |
| | |
| |
|
| | rc = ntru_crypto_ntru_encrypt(drbg, public_key_len, public_key,
|
| | sizeof(aes_key), aes_key, &ciphertext_len,
|
| | NULL);
|
| | if (rc != NTRU_OK)
|
| |
|
| | goto error;
|
| | printf("Ciphertext buffer size required: %d octets.\n", ciphertext_len);
|
| |
|
| |
|
| | |
| | |
| |
|
| |
|
| |
|
| | Handle = fopen("sample-original-plaintext.bin", "wb");
|
| | if (Handle != NULL)
|
| | {
|
| | printf("Writing original plaintext to original-plaintext.bin\n");
|
| | fwrite(aes_key, sizeof(aes_key), 1, Handle);
|
| | fclose(Handle);
|
| | }
|
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| | rc = ntru_crypto_ntru_encrypt(drbg, public_key_len, public_key,
|
| | sizeof(aes_key), aes_key, &ciphertext_len,
|
| | ciphertext);
|
| | if (rc != NTRU_OK)
|
| |
|
| | error = TRUE;
|
| | printf("AES-128 key encrypted successfully.\n");
|
| |
|
| |
|
| | Handle = fopen("sample-ciphertext.bin", "wb");
|
| | if (Handle != NULL)
|
| | {
|
| | printf("Writing ciphertext to ciphertext.bin\n");
|
| | fwrite(ciphertext, ciphertext_len, 1, Handle);
|
| | fclose(Handle);
|
| | }
|
| |
|
| |
|
| | rc = ntru_crypto_drbg_uninstantiate(drbg);
|
| | if ((rc != DRBG_OK) || error)
|
| | {
|
| | fprintf(stderr, "Error: An error occurred uninstantiating the DRBG, or encrypting.\n");
|
| | return 1;
|
| | }
|
| | printf("Encryption DRBG uninstantiated successfully.\n");
|
| |
|
| | printf("Plaintext:\n");
|
| | DumpHex(aes_key, sizeof(aes_key));
|
| |
|
| | printf("Ciphertext:\n");
|
| | DumpHex(ciphertext, ciphertext_len);
|
| |
|
| | |
| | |
| |
|
| | rc = ntru_crypto_ntru_decrypt(private_key_len, private_key, ciphertext_len,
|
| | ciphertext, &plaintext_len, NULL);
|
| | if (rc != NTRU_OK)
|
| |
|
| | goto error;
|
| | printf("Maximum plaintext buffer size required: %d octets.\n",
|
| | plaintext_len);
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | plaintext_len = sizeof(plaintext);
|
| | rc = ntru_crypto_ntru_decrypt(private_key_len, private_key, ciphertext_len,
|
| | ciphertext, &plaintext_len, plaintext);
|
| | if (rc != NTRU_OK)
|
| | {
|
| | fprintf(stderr, "Error: An error occurred decrypting the AES-128 key.\n");
|
| | return 1;
|
| | }
|
| | printf("AES-128 key decrypted successfully.\n");
|
| | printf("Decoded plaintext length: %d octets\n", plaintext_len);
|
| |
|
| | if (plaintext_len != sizeof(aes_key))
|
| | {
|
| | fprintf(stderr, "Error: Decrypted length does not match original plaintext length\n");
|
| | return 1;
|
| | }
|
| | if (memcmp(plaintext, aes_key, sizeof(aes_key)))
|
| | {
|
| | fprintf(stderr, "Error: Decrypted plaintext does not match original plaintext\n");
|
| | return 1;
|
| | }
|
| |
|
| | Handle = fopen("sample-decoded-plaintext.bin", "wb");
|
| | if (Handle != NULL)
|
| | {
|
| | printf("Writing decoded plaintext to decoded-plaintext.bin\n");
|
| | fwrite(plaintext, plaintext_len, 1, Handle);
|
| | fclose(Handle);
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | printf("Sample code completed successfully.\n");
|
| |
|
| |
|
| | return 0;
|
| |
|
| | error:
|
| | printf("Error (0x%x)\n", rc);
|
| | return 1;
|
| | }
|
| |
|