Skip to content
This repository was archived by the owner on Nov 6, 2024. It is now read-only.

Commit 5a1be2d

Browse files
committed
Use latest published nistkat package
Signed-off-by: Nigel Jones <[email protected]>
1 parent 6467b60 commit 5a1be2d

File tree

3 files changed

+60
-57
lines changed

3 files changed

+60
-57
lines changed

src/nistkat/PQCgenKAT_kem.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
/*
2+
NIST-developed software is provided by NIST as a public service. You may use, copy, and distribute copies of the software in any medium, provided that you keep intact this entire notice. You may improve, modify, and create derivative works of the software or any portion of the software, and you may copy and distribute such modifications or works. Modified works should carry a notice stating that you changed the software and should note the date and nature of any such change. Please explicitly acknowledge the National Institute of Standards and Technology as the source of the software.
3+
4+
NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED, IN FACT, OR ARISING BY OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE CORRECTED. NIST DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE SOFTWARE.
5+
6+
You are solely responsible for determining the appropriateness of using and distributing the software and you assume all risks associated with its use, including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and the unavailability or interruption of operation. This software is not intended to be used in any situation where a failure could cause risk of injury or damage to property. The software developed by NIST employees is not subject to copyright protection within the United States.
7+
*/
18

2-
//
3-
// PQCgenKAT_kem.c
4-
//
5-
// Created by Bassham, Lawrence E (Fed) on 8/29/17.
6-
// Copyright © 2017 Bassham, Lawrence E (Fed). All rights reserved.
7-
//
89
#include <stdio.h>
910
#include <stdlib.h>
1011
#include <string.h>
1112
#include <ctype.h>
1213
#include "rng.h"
13-
#include "../kem.h"
14+
#include "api.h"
1415

1516
#define MAX_MARKER_LEN 50
1617
#define KAT_SUCCESS 0
@@ -34,7 +35,7 @@ main()
3435
int done;
3536
unsigned char pk[CRYPTO_PUBLICKEYBYTES], sk[CRYPTO_SECRETKEYBYTES];
3637
int ret_val;
37-
38+
3839
// Create the REQUEST file
3940
sprintf(fn_req, "PQCkemKAT_%d.req", CRYPTO_SECRETKEYBYTES);
4041
if ( (fp_req = fopen(fn_req, "w")) == NULL ) {
@@ -46,7 +47,7 @@ main()
4647
printf("Couldn't open <%s> for write\n", fn_rsp);
4748
return KAT_FILE_OPEN_ERROR;
4849
}
49-
50+
5051
for (int i=0; i<48; i++)
5152
entropy_input[i] = i;
5253

@@ -61,13 +62,13 @@ main()
6162
fprintf(fp_req, "ss =\n\n");
6263
}
6364
fclose(fp_req);
64-
65+
6566
//Create the RESPONSE file based on what's in the REQUEST file
6667
if ( (fp_req = fopen(fn_req, "r")) == NULL ) {
6768
printf("Couldn't open <%s> for read\n", fn_req);
6869
return KAT_FILE_OPEN_ERROR;
6970
}
70-
71+
7172
fprintf(fp_rsp, "# %s\n\n", CRYPTO_ALGNAME);
7273
done = 0;
7374
do {
@@ -78,44 +79,44 @@ main()
7879
break;
7980
}
8081
fprintf(fp_rsp, "count = %d\n", count);
81-
82+
8283
if ( !ReadHex(fp_req, seed, 48, "seed = ") ) {
8384
printf("ERROR: unable to read 'seed' from <%s>\n", fn_req);
8485
return KAT_DATA_ERROR;
8586
}
8687
fprintBstr(fp_rsp, "seed = ", seed, 48);
87-
88+
8889
randombytes_init(seed, NULL, 256);
89-
90+
9091
// Generate the public/private keypair
9192
if ( (ret_val = crypto_kem_keypair(pk, sk)) != 0) {
9293
printf("crypto_kem_keypair returned <%d>\n", ret_val);
9394
return KAT_CRYPTO_FAILURE;
9495
}
9596
fprintBstr(fp_rsp, "pk = ", pk, CRYPTO_PUBLICKEYBYTES);
9697
fprintBstr(fp_rsp, "sk = ", sk, CRYPTO_SECRETKEYBYTES);
97-
98+
9899
if ( (ret_val = crypto_kem_enc(ct, ss, pk)) != 0) {
99100
printf("crypto_kem_enc returned <%d>\n", ret_val);
100101
return KAT_CRYPTO_FAILURE;
101102
}
102103
fprintBstr(fp_rsp, "ct = ", ct, CRYPTO_CIPHERTEXTBYTES);
103104
fprintBstr(fp_rsp, "ss = ", ss, CRYPTO_BYTES);
104-
105+
105106
fprintf(fp_rsp, "\n");
106-
107+
107108
if ( (ret_val = crypto_kem_dec(ss1, ct, sk)) != 0) {
108109
printf("crypto_kem_dec returned <%d>\n", ret_val);
109110
return KAT_CRYPTO_FAILURE;
110111
}
111-
112+
112113
if ( memcmp(ss, ss1, CRYPTO_BYTES) ) {
113114
printf("crypto_kem_dec returned bad 'ss' value\n");
114115
return KAT_CRYPTO_FAILURE;
115116
}
116117

117118
} while ( !done );
118-
119+
119120
fclose(fp_req);
120121
fclose(fp_rsp);
121122

@@ -203,7 +204,7 @@ ReadHex(FILE *infile, unsigned char *A, int Length, char *str)
203204
ich = ch - 'a' + 10;
204205
else // shouldn't ever get here
205206
ich = 0;
206-
207+
207208
for ( i=0; i<Length-1; i++ )
208209
A[i] = (A[i] << 4) | (A[i+1] >> 4);
209210
A[Length-1] = (A[Length-1] << 4) | ich;

src/nistkat/rng.c

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
//
2-
// rng.c
3-
//
4-
// Created by Bassham, Lawrence E (Fed) on 8/29/17.
5-
// Copyright © 2017 Bassham, Lawrence E (Fed). All rights reserved.
6-
//
1+
/*
2+
NIST-developed software is provided by NIST as a public service. You may use, copy, and distribute copies of the software in any medium, provided that you keep intact this entire notice. You may improve, modify, and create derivative works of the software or any portion of the software, and you may copy and distribute such modifications or works. Modified works should carry a notice stating that you changed the software and should note the date and nature of any such change. Please explicitly acknowledge the National Institute of Standards and Technology as the source of the software.
3+
4+
NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED, IN FACT, OR ARISING BY OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE CORRECTED. NIST DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE SOFTWARE.
5+
6+
You are solely responsible for determining the appropriateness of using and distributing the software and you assume all risks associated with its use, including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and the unavailability or interruption of operation. This software is not intended to be used in any situation where a failure could cause risk of injury or damage to property. The software developed by NIST employees is not subject to copyright protection within the United States.
7+
*/
78

89
#include <string.h>
910
#include "rng.h"
@@ -30,11 +31,11 @@ seedexpander_init(AES_XOF_struct *ctx,
3031
{
3132
if ( maxlen >= 0x100000000 )
3233
return RNG_BAD_MAXLEN;
33-
34+
3435
ctx->length_remaining = maxlen;
35-
36+
3637
memcpy(ctx->key, seed, 32);
37-
38+
3839
memcpy(ctx->ctr, diversifier, 8);
3940
ctx->ctr[11] = maxlen % 256;
4041
maxlen >>= 8;
@@ -44,10 +45,10 @@ seedexpander_init(AES_XOF_struct *ctx,
4445
maxlen >>= 8;
4546
ctx->ctr[8] = maxlen % 256;
4647
memset(ctx->ctr+12, 0x00, 4);
47-
48+
4849
ctx->buffer_pos = 16;
4950
memset(ctx->buffer, 0x00, 16);
50-
51+
5152
return RNG_SUCCESS;
5253
}
5354

@@ -61,31 +62,31 @@ int
6162
seedexpander(AES_XOF_struct *ctx, unsigned char *x, unsigned long xlen)
6263
{
6364
unsigned long offset;
64-
65+
6566
if ( x == NULL )
6667
return RNG_BAD_OUTBUF;
6768
if ( xlen >= ctx->length_remaining )
6869
return RNG_BAD_REQ_LEN;
69-
70+
7071
ctx->length_remaining -= xlen;
71-
72+
7273
offset = 0;
7374
while ( xlen > 0 ) {
7475
if ( xlen <= (16-ctx->buffer_pos) ) { // buffer has what we need
7576
memcpy(x+offset, ctx->buffer+ctx->buffer_pos, xlen);
7677
ctx->buffer_pos += xlen;
77-
78+
7879
return RNG_SUCCESS;
7980
}
80-
81+
8182
// take what's in the buffer
8283
memcpy(x+offset, ctx->buffer+ctx->buffer_pos, 16-ctx->buffer_pos);
8384
xlen -= 16-ctx->buffer_pos;
8485
offset += 16-ctx->buffer_pos;
85-
86+
8687
AES256_ECB(ctx->key, ctx->ctr, ctx->buffer);
8788
ctx->buffer_pos = 0;
88-
89+
8990
//increment the counter
9091
for (int i=15; i>=12; i--) {
9192
if ( ctx->ctr[i] == 0xff )
@@ -95,9 +96,9 @@ seedexpander(AES_XOF_struct *ctx, unsigned char *x, unsigned long xlen)
9596
break;
9697
}
9798
}
98-
99+
99100
}
100-
101+
101102
return RNG_SUCCESS;
102103
}
103104

@@ -116,21 +117,21 @@ void
116117
AES256_ECB(unsigned char *key, unsigned char *ctr, unsigned char *buffer)
117118
{
118119
EVP_CIPHER_CTX *ctx;
119-
120+
120121
int len;
121-
122+
122123
int ciphertext_len;
123-
124+
124125
/* Create and initialise the context */
125126
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
126-
127+
127128
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_ecb(), NULL, key, NULL))
128129
handleErrors();
129-
130+
130131
if(1 != EVP_EncryptUpdate(ctx, buffer, &len, ctr, 16))
131132
handleErrors();
132133
ciphertext_len = len;
133-
134+
134135
/* Clean up */
135136
EVP_CIPHER_CTX_free(ctx);
136137
}
@@ -141,7 +142,7 @@ randombytes_init(unsigned char *entropy_input,
141142
int security_strength)
142143
{
143144
unsigned char seed_material[48];
144-
145+
145146
memcpy(seed_material, entropy_input, 48);
146147
if (personalization_string)
147148
for (int i=0; i<48; i++)
@@ -157,7 +158,7 @@ randombytes(unsigned char *x, unsigned long long xlen)
157158
{
158159
unsigned char block[16];
159160
int i = 0;
160-
161+
161162
while ( xlen > 0 ) {
162163
//increment V
163164
for (int j=15; j>=0; j--) {
@@ -181,7 +182,7 @@ randombytes(unsigned char *x, unsigned long long xlen)
181182
}
182183
AES256_CTR_DRBG_Update(NULL, DRBG_ctx.Key, DRBG_ctx.V);
183184
DRBG_ctx.reseed_counter++;
184-
185+
185186
return RNG_SUCCESS;
186187
}
187188

@@ -191,7 +192,7 @@ AES256_CTR_DRBG_Update(unsigned char *provided_data,
191192
unsigned char *V)
192193
{
193194
unsigned char temp[48];
194-
195+
195196
for (int i=0; i<3; i++) {
196197
//increment V
197198
for (int j=15; j>=0; j--) {
@@ -202,7 +203,7 @@ AES256_CTR_DRBG_Update(unsigned char *provided_data,
202203
break;
203204
}
204205
}
205-
206+
206207
AES256_ECB(Key, V, temp+16*i);
207208
}
208209
if ( provided_data != NULL )

src/nistkat/rng.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
//
2-
// rng.h
3-
//
4-
// Created by Bassham, Lawrence E (Fed) on 8/29/17.
5-
// Copyright © 2017 Bassham, Lawrence E (Fed). All rights reserved.
6-
//
1+
/*
2+
NIST-developed software is provided by NIST as a public service. You may use, copy, and distribute copies of the software in any medium, provided that you keep intact this entire notice. You may improve, modify, and create derivative works of the software or any portion of the software, and you may copy and distribute such modifications or works. Modified works should carry a notice stating that you changed the software and should note the date and nature of any such change. Please explicitly acknowledge the National Institute of Standards and Technology as the source of the software.
3+
4+
NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED, IN FACT, OR ARISING BY OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE CORRECTED. NIST DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE SOFTWARE.
5+
6+
You are solely responsible for determining the appropriateness of using and distributing the software and you assume all risks associated with its use, including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and the unavailability or interruption of operation. This software is not intended to be used in any situation where a failure could cause risk of injury or damage to property. The software developed by NIST employees is not subject to copyright protection within the United States.
7+
*/
78

89
#ifndef rng_h
910
#define rng_h

0 commit comments

Comments
 (0)