genkat_aead.c (5289B)
1 // 2 // NIST-developed software is provided by NIST as a public service. 3 // You may use, copy and distribute copies of the software in any medium, 4 // provided that you keep intact this entire notice. You may improve, 5 // modify and create derivative works of the software or any portion of 6 // the software, and you may copy and distribute such modifications or 7 // works. Modified works should carry a notice stating that you changed 8 // the software and should note the date and nature of any such change. 9 // Please explicitly acknowledge the National Institute of Standards and 10 // Technology as the source of the software. 11 // 12 // NIST-developed software is expressly provided "AS IS." NIST MAKES NO 13 // WARRANTY OF ANY KIND, EXPRESS, IMPLIED, IN FACT OR ARISING BY OPERATION 14 // OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT AND DATA ACCURACY. NIST 16 // NEITHER REPRESENTS NOR WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE 17 // UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE CORRECTED. NIST 18 // DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE 19 // OR THE RESULTS THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, 20 // RELIABILITY, OR USEFULNESS OF THE SOFTWARE. 21 // 22 // You are solely responsible for determining the appropriateness of using and 23 // distributing the software and you assume all risks associated with its use, 24 // including but not limited to the risks and costs of program errors, compliance 25 // with applicable laws, damage to or loss of data, programs or equipment, and 26 // the unavailability or interruption of operation. This software is not intended 27 // to be used in any situation where a failure could cause risk of injury or 28 // damage to property. The software developed by NIST employees is not subject to 29 // copyright protection within the United States. 30 // 31 32 // disable deprecation for sprintf and fopen 33 #ifdef _MSC_VER 34 #define _CRT_SECURE_NO_WARNINGS 35 #endif 36 37 #include <stdio.h> 38 #include <string.h> 39 40 #include "crypto_aead.h" 41 #include "api.h" 42 43 #define KAT_SUCCESS 0 44 #define KAT_FILE_OPEN_ERROR -1 45 #define KAT_DATA_ERROR -3 46 #define KAT_CRYPTO_FAILURE -4 47 48 #define MAX_FILE_NAME 256 49 #define MAX_MESSAGE_LENGTH 32 50 #define MAX_ASSOCIATED_DATA_LENGTH 32 51 52 void init_buffer(unsigned char *buffer, unsigned long long numbytes); 53 54 void fprint_bstr(FILE *fp, const char *label, const unsigned char *data, unsigned long long length); 55 56 int generate_test_vectors(); 57 58 int main() 59 { 60 int ret = generate_test_vectors(); 61 62 if (ret != KAT_SUCCESS) { 63 fprintf(stderr, "test vector generation failed with code %d\n", ret); 64 } 65 66 return ret; 67 } 68 69 int generate_test_vectors() 70 { 71 FILE *fp; 72 char fileName[MAX_FILE_NAME]; 73 unsigned char key[CRYPTO_KEYBYTES]; 74 unsigned char nonce[CRYPTO_NPUBBYTES]; 75 unsigned char msg[MAX_MESSAGE_LENGTH]; 76 unsigned char msg2[MAX_MESSAGE_LENGTH]; 77 unsigned char ad[MAX_ASSOCIATED_DATA_LENGTH]; 78 unsigned char ct[MAX_MESSAGE_LENGTH + CRYPTO_ABYTES]; 79 unsigned long long clen, mlen2; 80 int count = 1; 81 int func_ret, ret_val = KAT_SUCCESS; 82 83 init_buffer(key, sizeof(key)); 84 init_buffer(nonce, sizeof(nonce)); 85 init_buffer(msg, sizeof(msg)); 86 init_buffer(ad, sizeof(ad)); 87 88 sprintf(fileName, "LWC_AEAD_KAT_%d_%d.txt", (CRYPTO_KEYBYTES * 8), (CRYPTO_NPUBBYTES * 8)); 89 90 if ((fp = fopen(fileName, "w")) == NULL) { 91 fprintf(stderr, "Couldn't open <%s> for write\n", fileName); 92 return KAT_FILE_OPEN_ERROR; 93 } 94 95 for (unsigned long long mlen = 0; (mlen <= MAX_MESSAGE_LENGTH) && (ret_val == KAT_SUCCESS); mlen++) { 96 97 for (unsigned long long adlen = 0; adlen <= MAX_ASSOCIATED_DATA_LENGTH; adlen++) { 98 99 fprintf(fp, "Count = %d\n", count++); 100 101 fprint_bstr(fp, "Key = ", key, CRYPTO_KEYBYTES); 102 103 fprint_bstr(fp, "Nonce = ", nonce, CRYPTO_NPUBBYTES); 104 105 fprint_bstr(fp, "PT = ", msg, mlen); 106 107 fprint_bstr(fp, "AD = ", ad, adlen); 108 109 if ((func_ret = crypto_aead_encrypt(ct, &clen, msg, mlen, ad, adlen, NULL, nonce, key)) != 0) { 110 fprintf(fp, "crypto_aead_encrypt returned <%d>\n", func_ret); 111 ret_val = KAT_CRYPTO_FAILURE; 112 break; 113 } 114 115 fprint_bstr(fp, "CT = ", ct, clen); 116 117 fprintf(fp, "\n"); 118 119 if ((func_ret = crypto_aead_decrypt(msg2, &mlen2, NULL, ct, clen, ad, adlen, nonce, key)) != 0) { 120 fprintf(fp, "crypto_aead_decrypt returned <%d>\n", func_ret); 121 ret_val = KAT_CRYPTO_FAILURE; 122 break; 123 } 124 125 if (mlen != mlen2) { 126 fprintf(fp, "crypto_aead_decrypt returned bad 'mlen': Got <%llu>, expected <%llu>\n", mlen2, mlen); 127 ret_val = KAT_CRYPTO_FAILURE; 128 break; 129 } 130 131 if (memcmp(msg, msg2, mlen)) { 132 fprintf(fp, "crypto_aead_decrypt did not recover the plaintext\n"); 133 ret_val = KAT_CRYPTO_FAILURE; 134 break; 135 } 136 } 137 } 138 139 fclose(fp); 140 141 return ret_val; 142 } 143 144 145 void fprint_bstr(FILE *fp, const char *label, const unsigned char *data, unsigned long long length) 146 { 147 fprintf(fp, "%s", label); 148 149 for (unsigned long long i = 0; i < length; i++) 150 fprintf(fp, "%02X", data[i]); 151 152 fprintf(fp, "\n"); 153 } 154 155 void init_buffer(unsigned char *buffer, unsigned long long numbytes) 156 { 157 for (unsigned long long i = 0; i < numbytes; i++) 158 buffer[i] = (unsigned char)i; 159 }