Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
afDursun committed Jun 30, 2024
0 parents commit 37de13d
Show file tree
Hide file tree
Showing 55 changed files with 5,946 additions and 0 deletions.
64 changes: 64 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
CC ?= /usr/bin/cc
CFLAGS += -Wall -Wextra -Wpedantic -Wmissing-prototypes -Wredundant-decls \
-Wshadow -Wpointer-arith -O3 -fomit-frame-pointer -I/usr/include/openssl
NISTFLAGS += -Wno-unused-result -O3 -fomit-frame-pointer
LDFLAGS += -L/usr/lib -lcrypto
RM = /bin/rm

SOURCES = kyber-pake.c indcpa.c polyvec.c poly.c ntt.c cbd.c reduce.c verify.c aead.c permutations.c printstate.c
SOURCESKECCAK = $(SOURCES) fips202.c symmetric-shake.c
SOURCESNINETIES = $(SOURCES) sha256.c sha512.c aes256ctr.c symmetric-aes.c
HEADERS = params.h kyber-pake.h indcpa.h polyvec.h poly.h ntt.h cbd.h reduce.c verify.h symmetric.h permutations.h printstate.h crypto_aead.h config.h constants.h round.h forceinline.h word.h bendian.h
HEADERSKECCAK = $(HEADERS) fips202.h
HEADERSNINETIES = $(HEADERS) aes256ctr.h sha2.h

.PHONY: all speed shared clean

all: \
test_kyber512 \
test_kyber768 \
test_kyber1024 \
test_speed512 \
test_speed768 \
test_speed1024 \

kyber_pake: \
test_kyber512 \
test_kyber768 \
test_kyber1024 \

kyber_pake_speed: \
test_speed512 \
test_speed768 \
test_speed1024 \



test_kyber512: $(SOURCESKECCAK) $(HEADERSKECCAK) test_kyber.c randombytes.c
$(CC) $(CFLAGS) $(LDFLAGS) -DKYBER_K=2 $(SOURCESKECCAK) randombytes.c test_kyber.c -o pake_kyber512


test_kyber768: $(SOURCESKECCAK) $(HEADERSKECCAK) test_kyber.c randombytes.c
$(CC) $(CFLAGS) $(LDFLAGS) -DKYBER_K=3 $(SOURCESKECCAK) randombytes.c test_kyber.c -o pake_kyber768

test_kyber1024: $(SOURCESKECCAK) $(HEADERSKECCAK) test_kyber.c randombytes.c
$(CC) $(CFLAGS) $(LDFLAGS) -DKYBER_K=4 $(SOURCESKECCAK) randombytes.c test_kyber.c -o pake_kyber1024

test_speed512: $(SOURCESKECCAK) $(HEADERSKECCAK) cpucycles.h cpucycles.c speed_print.h speed_print.c test_speed.c randombytes.c
$(CC) $(CFLAGS) $(LDFLAGS) -DKYBER_K=2 $(SOURCESKECCAK) randombytes.c cpucycles.c speed_print.c test_speed.c -o test_speed512

test_speed768: $(SOURCESKECCAK) $(HEADERSKECCAK) cpucycles.h cpucycles.c speed_print.h speed_print.c test_speed.c randombytes.c
$(CC) $(CFLAGS) $(LDFLAGS) -DKYBER_K=3 $(SOURCESKECCAK) randombytes.c cpucycles.c speed_print.c test_speed.c -o test_speed768

test_speed1024: $(SOURCESKECCAK) $(HEADERSKECCAK) cpucycles.h cpucycles.c speed_print.h speed_print.c test_speed.c randombytes.c
$(CC) $(CFLAGS) $(LDFLAGS) -DKYBER_K=4 $(SOURCESKECCAK) randombytes.c cpucycles.c speed_print.c test_speed.c -o test_speed1024

clean:
-$(RM) -rf *.gcno *.gcda *.lcov *.o *.so
-$(RM) -rf pake_kyber512
-$(RM) -rf pake_kyber768
-$(RM) -rf pake_kyber1024
-$(RM) -rf test_speed512
-$(RM) -rf test_speed768
-$(RM) -rf test_speed1024

232 changes: 232 additions & 0 deletions PQCgenKAT_kem.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@

//
// PQCgenKAT_kem.c
//
// Created by Bassham, Lawrence E (Fed) on 8/29/17.
// Copyright © 2017 Bassham, Lawrence E (Fed). All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "rng.h"
#include "kem.h"

#define MAX_MARKER_LEN 50
#define KAT_SUCCESS 0
#define KAT_FILE_OPEN_ERROR -1
#define KAT_DATA_ERROR -3
#define KAT_CRYPTO_FAILURE -4

int FindMarker(FILE *infile, const char *marker);
int ReadHex(FILE *infile, unsigned char *A, int Length, char *str);
void fprintBstr(FILE *fp, char *S, unsigned char *A, unsigned long long L);

int
main()
{
char fn_req[32], fn_rsp[32];
FILE *fp_req, *fp_rsp;
unsigned char seed[48];
unsigned char entropy_input[48];
unsigned char ct[CRYPTO_CIPHERTEXTBYTES], ss[CRYPTO_BYTES], ss1[CRYPTO_BYTES];
int count;
int done;
unsigned char pk[CRYPTO_PUBLICKEYBYTES], sk[CRYPTO_SECRETKEYBYTES];
int ret_val;

// Create the REQUEST file
sprintf(fn_req, "PQCkemKAT_%d.req", CRYPTO_SECRETKEYBYTES);
if ( (fp_req = fopen(fn_req, "w")) == NULL ) {
printf("Couldn't open <%s> for write\n", fn_req);
return KAT_FILE_OPEN_ERROR;
}
sprintf(fn_rsp, "PQCkemKAT_%d.rsp", CRYPTO_SECRETKEYBYTES);
if ( (fp_rsp = fopen(fn_rsp, "w")) == NULL ) {
printf("Couldn't open <%s> for write\n", fn_rsp);
return KAT_FILE_OPEN_ERROR;
}

for (int i=0; i<48; i++)
entropy_input[i] = i;

randombytes_init(entropy_input, NULL, 256);
for (int i=0; i<100; i++) {
fprintf(fp_req, "count = %d\n", i);
randombytes(seed, 48);
fprintBstr(fp_req, "seed = ", seed, 48);
fprintf(fp_req, "pk =\n");
fprintf(fp_req, "sk =\n");
fprintf(fp_req, "ct =\n");
fprintf(fp_req, "ss =\n\n");
}
fclose(fp_req);

//Create the RESPONSE file based on what's in the REQUEST file
if ( (fp_req = fopen(fn_req, "r")) == NULL ) {
printf("Couldn't open <%s> for read\n", fn_req);
return KAT_FILE_OPEN_ERROR;
}

fprintf(fp_rsp, "# %s\n\n", CRYPTO_ALGNAME);
done = 0;
do {
if ( FindMarker(fp_req, "count = ") )
fscanf(fp_req, "%d", &count);
else {
done = 1;
break;
}
fprintf(fp_rsp, "count = %d\n", count);

if ( !ReadHex(fp_req, seed, 48, "seed = ") ) {
printf("ERROR: unable to read 'seed' from <%s>\n", fn_req);
return KAT_DATA_ERROR;
}
fprintBstr(fp_rsp, "seed = ", seed, 48);

randombytes_init(seed, NULL, 256);

// Generate the public/private keypair
if ( (ret_val = crypto_kem_keypair(pk, sk)) != 0) {
printf("crypto_kem_keypair returned <%d>\n", ret_val);
return KAT_CRYPTO_FAILURE;
}
fprintBstr(fp_rsp, "pk = ", pk, CRYPTO_PUBLICKEYBYTES);
fprintBstr(fp_rsp, "sk = ", sk, CRYPTO_SECRETKEYBYTES);

if ( (ret_val = crypto_kem_enc(ct, ss, pk)) != 0) {
printf("crypto_kem_enc returned <%d>\n", ret_val);
return KAT_CRYPTO_FAILURE;
}
fprintBstr(fp_rsp, "ct = ", ct, CRYPTO_CIPHERTEXTBYTES);
fprintBstr(fp_rsp, "ss = ", ss, CRYPTO_BYTES);

fprintf(fp_rsp, "\n");

if ( (ret_val = crypto_kem_dec(ss1, ct, sk)) != 0) {
printf("crypto_kem_dec returned <%d>\n", ret_val);
return KAT_CRYPTO_FAILURE;
}

if ( memcmp(ss, ss1, CRYPTO_BYTES) ) {
printf("crypto_kem_dec returned bad 'ss' value\n");
return KAT_CRYPTO_FAILURE;
}

} while ( !done );

fclose(fp_req);
fclose(fp_rsp);

return KAT_SUCCESS;
}



//
// ALLOW TO READ HEXADECIMAL ENTRY (KEYS, DATA, TEXT, etc.)
//
//
// ALLOW TO READ HEXADECIMAL ENTRY (KEYS, DATA, TEXT, etc.)
//
int
FindMarker(FILE *infile, const char *marker)
{
char line[MAX_MARKER_LEN];
int i, len;
int curr_line;

len = (int)strlen(marker);
if ( len > MAX_MARKER_LEN-1 )
len = MAX_MARKER_LEN-1;

for ( i=0; i<len; i++ )
{
curr_line = fgetc(infile);
line[i] = curr_line;
if (curr_line == EOF )
return 0;
}
line[len] = '\0';

while ( 1 ) {
if ( !strncmp(line, marker, len) )
return 1;

for ( i=0; i<len-1; i++ )
line[i] = line[i+1];
curr_line = fgetc(infile);
line[len-1] = curr_line;
if (curr_line == EOF )
return 0;
line[len] = '\0';
}

// shouldn't get here
return 0;
}

//
// ALLOW TO READ HEXADECIMAL ENTRY (KEYS, DATA, TEXT, etc.)
//
int
ReadHex(FILE *infile, unsigned char *A, int Length, char *str)
{
int i, ch, started;
unsigned char ich;

if ( Length == 0 ) {
A[0] = 0x00;
return 1;
}
memset(A, 0x00, Length);
started = 0;
if ( FindMarker(infile, str) )
while ( (ch = fgetc(infile)) != EOF ) {
if ( !isxdigit(ch) ) {
if ( !started ) {
if ( ch == '\n' )
break;
else
continue;
}
else
break;
}
started = 1;
if ( (ch >= '0') && (ch <= '9') )
ich = ch - '0';
else if ( (ch >= 'A') && (ch <= 'F') )
ich = ch - 'A' + 10;
else if ( (ch >= 'a') && (ch <= 'f') )
ich = ch - 'a' + 10;
else // shouldn't ever get here
ich = 0;

for ( i=0; i<Length-1; i++ )
A[i] = (A[i] << 4) | (A[i+1] >> 4);
A[Length-1] = (A[Length-1] << 4) | ich;
}
else
return 0;

return 1;
}

void
fprintBstr(FILE *fp, char *S, unsigned char *A, unsigned long long L)
{
unsigned long long i;

fprintf(fp, "%s", S);

for ( i=0; i<L; i++ )
fprintf(fp, "%02X", A[i]);

if ( L == 0 )
fprintf(fp, "00");

fprintf(fp, "\n");
}

25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generic Kyber.PAKE ASCON

[![GitHub release (latest by date including pre-releases)](https://img.shields.io/github/v/release/afDursun/generic-kyber-pake-ascon?include_prereleases)](https://img.shields.io/github/v/release/afDursun/generic-kyber-pake-ascon?include_prereleases)
[![GitHub last commit](https://img.shields.io/github/last-commit/afDursun/generic-kyber-pake-ascon)](https://img.shields.io/github/last-commit/afDursun/generic-kyber-pake-ascon)
[![GitHub issues](https://img.shields.io/github/issues-raw/afDursun/generic-kyber-pake-ascon)](https://img.shields.io/github/issues-raw/afDursun/generic-kyber-pake-ascon)
[![GitHub pull requests](https://img.shields.io/github/issues-pr/afDursun/generic-kyber-pake-ascon)](https://img.shields.io/github/issues-pr/afDursun/generic-kyber-pake-ascon)
[![GitHub](https://img.shields.io/github/license/afDursun/generic-kyber-pake-ascon)](https://img.shields.io/github/license/afDursun/generic-kyber-pake-ascon)


## Usage

To build all applications
```
make all
```

To build only the main application
```
make kyber_pake
```

To build only the performance result
```
make kyber_pake_speed
```
Loading

0 comments on commit 37de13d

Please sign in to comment.