Commit 75c36bef by Enrico Pozzobon

Merge branch 'master'

Conflicts:
	templates/f7/Inc/main.h
	templates/f7/Makefile
	templates/f7/Src/main.c
	templates/f7/f7.ioc
	templates/f7/middleware.py
parents 32b39542 fcb7d9c8
build/
measurements/
logs/
all-lwc-submission-files/
*.log
all-lwc-submission-files.zip
ace/
ascon/
bleep64/
cilipadi/
clae/
clx/
comet/
drygascon/
elephant/
estate/
flexaead/
forkae/
fountain/
gage-ingage/
gift-cofb/
gimli/
grain-128aead/
hern-heron/
hyena/
isap/
knot/
laem/
lilliput-ae/
limdolen/
lotus-locus/
mixfeed/
NOT ACCEPTED silmine/
orange/
oribatida/
photon-beetle/
pyjamask/
qameleon/
quartet/
remus/
romulus/
saeaes/
saturnin/
shamash-shamashash/
simple/
siv-rijndael256/
siv-tem-photon/
skinny/
sneik/
sparkle/
spix/
spoc/
spook/
subterranean/
sundae-gift/
sycon/
tgif/
tinyjambu/
triad/
trifle/
wage/
xoodyak/
yarara-coral/
../mbed_aes_gcm/
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
CC=gcc
#NISTGCCFLAGS=-std=c99 -Wall -Wextra -Wshadow -fsanitize=address,undefined -O2
NISTGCCFLAGS=-std=c99 -Wall -Wextra -Wshadow -Os
LFLAGS=-lm
all: nocrypt
nocrypt: nocrypt.c genkat_aead.c
$(CC) $(NISTGCCFLAGS) -o $@ $^ $(LFLAGS)
.PHONY: clean
clean:
rm -f *.o
rm -f nocrypt
#define CRYPTO_KEYBYTES 0
#define CRYPTO_NSECBYTES 0
#define CRYPTO_NPUBBYTES 0
#define CRYPTO_ABYTES 0
#define CRYPTO_NOOVERLAP 1
typedef unsigned long long u64;
int crypto_encrypt(
unsigned char *c,unsigned long long *clen,
const unsigned char *m,unsigned long long mlen,
const unsigned char *nsec,
const unsigned char *npub,
const unsigned char *k
);
int crypto_decrypt(
unsigned char *m,unsigned long long *mlen,
unsigned char *nsec,
const unsigned char *c,unsigned long long clen,
const unsigned char *npub,
const unsigned char *k
);
int crypto_aead_encrypt(
unsigned char *c, unsigned long long *clen,
const unsigned char *m, unsigned long long mlen,
const unsigned char *ad, unsigned long long adlen,
const unsigned char *nsec,
const unsigned char *npub,
const unsigned char *k
);
int crypto_aead_decrypt(
unsigned char *m, unsigned long long *mlen,
unsigned char *nsec,
const unsigned char *c, unsigned long long clen,
const unsigned char *ad, unsigned long long adlen,
const unsigned char *npub,
const unsigned char *k
);
//
// 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.
//
// 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.
//
// 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.
//
// disable deprecation for sprintf and fopen
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <string.h>
#include "crypto_aead.h"
#include "api.h"
#define KAT_SUCCESS 0
#define KAT_FILE_OPEN_ERROR -1
#define KAT_DATA_ERROR -3
#define KAT_CRYPTO_FAILURE -4
#define MAX_FILE_NAME 256
#define MAX_MESSAGE_LENGTH 32
#define MAX_ASSOCIATED_DATA_LENGTH 32
void init_buffer(unsigned char *buffer, unsigned long long numbytes);
void fprint_bstr(FILE *fp, const char *label, const unsigned char *data, unsigned long long length);
int generate_test_vectors();
int main()
{
int ret = generate_test_vectors();
if (ret != KAT_SUCCESS) {
fprintf(stderr, "test vector generation failed with code %d\n", ret);
}
return ret;
}
int generate_test_vectors()
{
FILE *fp;
char fileName[MAX_FILE_NAME];
unsigned char key[CRYPTO_KEYBYTES];
unsigned char nonce[CRYPTO_NPUBBYTES];
unsigned char msg[MAX_MESSAGE_LENGTH];
unsigned char msg2[MAX_MESSAGE_LENGTH];
unsigned char ad[MAX_ASSOCIATED_DATA_LENGTH];
unsigned char ct[MAX_MESSAGE_LENGTH + CRYPTO_ABYTES];
unsigned long long clen, mlen2;
int count = 1;
int func_ret, ret_val = KAT_SUCCESS;
init_buffer(key, sizeof(key));
init_buffer(nonce, sizeof(nonce));
init_buffer(msg, sizeof(msg));
init_buffer(ad, sizeof(ad));
sprintf(fileName, "LWC_AEAD_KAT_%d_%d.txt", (CRYPTO_KEYBYTES * 8), (CRYPTO_NPUBBYTES * 8));
if ((fp = fopen(fileName, "w")) == NULL) {
fprintf(stderr, "Couldn't open <%s> for write\n", fileName);
return KAT_FILE_OPEN_ERROR;
}
for (unsigned long long mlen = 0; (mlen <= MAX_MESSAGE_LENGTH) && (ret_val == KAT_SUCCESS); mlen++) {
//for (unsigned long long mlen = 0; (mlen <= 32) && (ret_val == KAT_SUCCESS); mlen++) {
for (unsigned long long adlen = 0; adlen <= MAX_ASSOCIATED_DATA_LENGTH; adlen++) {
//for (unsigned long long adlen = 0; adlen <= 32; adlen++) {
printf("%0d\n", (int)clen);
fprintf(fp, "Count = %d\n", count++);
printf("Count = %d\n", count - 1);
fprint_bstr(fp, "Key = ", key, CRYPTO_KEYBYTES);
fprint_bstr(fp, "Nonce = ", nonce, CRYPTO_NPUBBYTES);
fprint_bstr(fp, "PT = ", msg, mlen);
fprint_bstr(fp, "AD = ", ad, adlen);
if ((func_ret = crypto_aead_encrypt(ct, &clen, msg, mlen, ad, adlen, NULL, nonce, key)) != 0) {
fprintf(fp, "crypto_aead_encrypt returned <%d>\n", func_ret);
ret_val = KAT_CRYPTO_FAILURE;
break;
}
fprint_bstr(fp, "CT = ", ct, clen);
fprintf(fp, "\n");
if ((func_ret = crypto_aead_decrypt(msg2, &mlen2, NULL, ct, clen, ad, adlen, nonce, key)) != 0) {
fprintf(fp, "crypto_aead_decrypt returned <%d>\n", func_ret);
ret_val = KAT_CRYPTO_FAILURE;
break;
}
if (mlen != mlen2) {
fprintf(fp, "crypto_aead_decrypt returned bad 'mlen': Got <%llu>, expected <%llu>\n", mlen2, mlen);
ret_val = KAT_CRYPTO_FAILURE;
break;
}
if (memcmp(msg, msg2, mlen)) {
fprintf(fp, "crypto_aead_decrypt did not recover the plaintext\n");
ret_val = KAT_CRYPTO_FAILURE;
break;
}
}
}
fclose(fp);
return ret_val;
}
void fprint_bstr(FILE *fp, const char *label, const unsigned char *data, unsigned long long length)
{
fprintf(fp, "%s", label);
for (unsigned long long i = 0; i < length; i++)
fprintf(fp, "%02X", data[i]);
fprintf(fp, "\n");
}
void init_buffer(unsigned char *buffer, unsigned long long numbytes)
{
for (unsigned long long i = 0; i < numbytes; i++)
buffer[i] = (unsigned char)i;
}
#include "api.h"
#include "crypto_aead.h"
#include <string.h>
int crypto_aead_encrypt(
unsigned char *c,unsigned long long *clen,
const unsigned char *m,unsigned long long mlen,
const unsigned char *ad,unsigned long long adlen,
const unsigned char *nsec,
const unsigned char *npub,
const unsigned char *k
)
{
*clen = mlen + CRYPTO_ABYTES;
memcpy(c, m, mlen);
memset(c + mlen, 0, CRYPTO_ABYTES);
return 0;
}
int crypto_aead_decrypt(
unsigned char *m, unsigned long long *mlen,
unsigned char *nsec,
const unsigned char *c, unsigned long long clen,
const unsigned char *ad, unsigned long long adlen,
const unsigned char *npub,
const unsigned char *k
)
{
unsigned long long len = *mlen = clen - CRYPTO_ABYTES;
memcpy(m, c, len);
return 0;
}
......@@ -101,13 +101,19 @@ def find_test_vectors(d):
def main(argv):
submissions_dir = "all-lwc-submission-files"
template_dir = "templates/linux"
include_list = None
if len(argv) > 1:
template_dir = argv[1]
if len(argv) > 2:
with open(argv[2], 'r') as includes:
include_list = []
for line in includes.readlines():
include_list.append(line.strip())
print("Using template %s" % template_dir)
subs = os.listdir(submissions_dir)
# get all the submissions by looking for files named "api.h"
files = []
subfiles = []
for submission in subs:
implementations_dir = os.path.join(submissions_dir, submission, "Implementations", "crypto_aead")
......@@ -126,32 +132,45 @@ def main(argv):
for file in f:
if file == "api.h":
f = os.path.join(r, file)
subfiles.append(f)
c += 1
if c == 0:
raise Exception("No implementations found")
# Source directory d
d = os.path.split(f)[0]
assert os.path.isdir(d)
print(d)
if include_list is not None:
print("Include list has %d entries" % len(include_list))
# Test vectors file t
t = find_test_vectors(d)
print(t)
files = []
for f in subfiles:
# base name n
pieces = r.split(os.sep)
n = submission + "." + ".".join(pieces[4:])
print(n)
# Source directory d
d = os.path.split(f)[0]
assert os.path.isdir(d)
print(d)
# Put all in a tuple and count
files.append((t, d, n))
c += 1
# Test vectors file t
t = find_test_vectors(d)
print(t)
if c == 0:
raise Exception("No implementations found")
# base name n
pieces = f.split(os.sep)
n = pieces[1] + "." + ".".join(pieces[4:-1])
print(n)
# if include_list was provided, skip elements not in the list
if include_list is not None:
if not n in include_list:
continue
# Put all in a tuple and count
files.append((t, d, n))
# For testing, we only do the first 1
#files = files[:1]
print("%d algorithms will be compiled" % len(files))
# Clear the build directory as it is a leftover from the previous execution
if os.path.isdir('build'):
......
#!/bin/bash
mkdir all-lwc-submission-files || exit 1
mkdir -p all-lwc-submission-files
cd all-lwc-submission-files
wget https://csrc.nist.gov/CSRC/media/Projects/Lightweight-Cryptography/documents/round-1/submissions/all-lwc-submission-files.zip
unzip all-lwc-submission-files.zip
cp -r ../mbed_aes_gcm/ .
cd ..
mkdir -p all-lwc-submission-files/aes-gcm/Implementations/crypto_aead/aesgcm128-128
cp -r mbed_aes_gcm/ all-lwc-submission-files/aes-gcm/Implementations/crypto_aead/aesgcm128-128/ref
......@@ -41,11 +41,7 @@
#ifndef MBEDTLS_AES_H
#define MBEDTLS_AES_H
#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include <stddef.h>
#include <stdint.h>
......
......@@ -23,12 +23,8 @@
* This file is part of mbed TLS (https://tls.mbed.org)
*/
#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#include "platform_util.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#if defined(MBEDTLS_CIPHER_C)
......
......@@ -29,12 +29,8 @@
#ifndef MBEDTLS_CIPHER_H
#define MBEDTLS_CIPHER_H
#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#include "platform_util.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include <stddef.h>
......
......@@ -26,11 +26,7 @@
#ifndef MBEDTLS_CIPHER_WRAP_H
#define MBEDTLS_CIPHER_WRAP_H
#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include "cipher.h"
......
......@@ -23,11 +23,7 @@
* This file is part of mbed TLS (https://tls.mbed.org)
*/
#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#if defined(MBEDTLS_CIPHER_C)
......
#pragma once
#undef MBEDTLS_SELF_TEST
#define MBEDTLS_GCM_C
#define MBEDTLS_CIPHER_C
#define MBEDTLS_AES_C
......@@ -33,11 +33,7 @@
#ifndef MBEDTLS_GCM_H
#define MBEDTLS_GCM_H
#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include "cipher.h"
......
......@@ -28,11 +28,7 @@
#define _POSIX_C_SOURCE 200112L
#endif
#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include "platform_util.h"
......
......@@ -25,11 +25,7 @@
#ifndef MBEDTLS_PLATFORM_UTIL_H
#define MBEDTLS_PLATFORM_UTIL_H
#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include <stddef.h>
......@@ -37,99 +33,14 @@
extern "C" {
#endif
#if defined(MBEDTLS_CHECK_PARAMS)
#if defined(MBEDTLS_CHECK_PARAMS_ASSERT)
/* Allow the user to define MBEDTLS_PARAM_FAILED to something like assert
* (which is what our config.h suggests). */
#include <assert.h>
#endif /* MBEDTLS_CHECK_PARAMS_ASSERT */
#if defined(MBEDTLS_PARAM_FAILED)
/** An alternative definition of MBEDTLS_PARAM_FAILED has been set in config.h.
*
* This flag can be used to check whether it is safe to assume that
* MBEDTLS_PARAM_FAILED() will expand to a call to mbedtls_param_failed().
*/
#define MBEDTLS_PARAM_FAILED_ALT
#elif defined(MBEDTLS_CHECK_PARAMS_ASSERT)
#define MBEDTLS_PARAM_FAILED( cond ) assert( cond )
#define MBEDTLS_PARAM_FAILED_ALT
#else /* MBEDTLS_PARAM_FAILED */
#define MBEDTLS_PARAM_FAILED( cond ) \
mbedtls_param_failed( #cond, __FILE__, __LINE__ )
/**
* \brief User supplied callback function for parameter validation failure.
* See #MBEDTLS_CHECK_PARAMS for context.
*
* This function will be called unless an alternative treatement
* is defined through the #MBEDTLS_PARAM_FAILED macro.
*
* This function can return, and the operation will be aborted, or
* alternatively, through use of setjmp()/longjmp() can resume
* execution in the application code.
*
* \param failure_condition The assertion that didn't hold.
* \param file The file where the assertion failed.
* \param line The line in the file where the assertion failed.
*/
void mbedtls_param_failed( const char *failure_condition,
const char *file,
int line );
#endif /* MBEDTLS_PARAM_FAILED */
/* Internal macro meant to be called only from within the library. */
#define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) \
do { \
if( !(cond) ) \
{ \
MBEDTLS_PARAM_FAILED( cond ); \
return( ret ); \
} \
} while( 0 )
/* Internal macro meant to be called only from within the library. */
#define MBEDTLS_INTERNAL_VALIDATE( cond ) \
do { \
if( !(cond) ) \
{ \
MBEDTLS_PARAM_FAILED( cond ); \
return; \
} \
} while( 0 )
#else /* MBEDTLS_CHECK_PARAMS */
/* Internal macros meant to be called only from within the library. */
#define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) do { } while( 0 )
#define MBEDTLS_INTERNAL_VALIDATE( cond ) do { } while( 0 )
#endif /* MBEDTLS_CHECK_PARAMS */
/* Internal helper macros for deprecating API constants. */
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
#if defined(MBEDTLS_DEPRECATED_WARNING)
/* Deliberately don't (yet) export MBEDTLS_DEPRECATED here
* to avoid conflict with other headers which define and use
* it, too. We might want to move all these definitions here at
* some point for uniformity. */
#define MBEDTLS_DEPRECATED __attribute__((deprecated))
MBEDTLS_DEPRECATED typedef char const * mbedtls_deprecated_string_constant_t;
#define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) \
( (mbedtls_deprecated_string_constant_t) ( VAL ) )
MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t;
#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) \
( (mbedtls_deprecated_numeric_constant_t) ( VAL ) )
#undef MBEDTLS_DEPRECATED
#else /* MBEDTLS_DEPRECATED_WARNING */
#define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) VAL
#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) VAL
#endif /* MBEDTLS_DEPRECATED_WARNING */
#endif /* MBEDTLS_DEPRECATED_REMOVED */
/**
* \brief Securely zeroize a buffer
*
......
......@@ -34,7 +34,7 @@ unsigned long long nplen = NPUB_LENGTH;
unsigned char *k = NULL;
unsigned long long klen = KEY_LENGTH;
static void read_variable(unsigned char **target, unsigned long long *lenp) {
static void recv_variable(unsigned char **target, unsigned long long *lenp) {
if (*target != NULL) {
free(*target);
}
......@@ -46,26 +46,25 @@ static void read_variable(unsigned char **target, unsigned long long *lenp) {
}
*lenp = len;
if (*lenp == 0) {
*target = NULL;
return;
}
*target = malloc(*lenp);
*target = malloc(len);
if (NULL == *target) {
fprintf(stderr, "ERROR: couldn't malloc %llu bytes\r\n", *lenp);
fprintf(stderr, "ERROR: couldn't malloc %u bytes\r\n", len);
exit(2);
}
if (*lenp == 0) {
return;
}
if (1 != fread(*target, *lenp, 1, stdin)) {
fprintf(stderr, "ERROR: didn't read %llu bytes of data\r\n", *lenp);
fprintf(stderr, "ERROR: didn't read %u bytes of data\r\n", len);
exit(1);
}
}
static void write_variable(unsigned char *target, unsigned long long len) {
static void send_variable(unsigned char *target, unsigned long long len) {
uint32_t olen = len;
if (1 != fwrite(&olen, sizeof(olen), 1, stdout)) {
fprintf(stderr, "ERROR: didn't write length\r\n");
......@@ -92,22 +91,23 @@ int main() {
while (1) {
if (1 != fread(&action, sizeof(action), 1, stdin))
return 1;
fprintf(stderr, "DEBUG: received action 0x%02x\r\n", action);
switch (action) {
case 'c': read_variable(&c, &clen); break;
case 'm': read_variable(&m, &mlen); break;
case 'a': read_variable(&ad, &adlen); break;
case 'k': read_variable(&k, &klen); break;
case 'c': recv_variable(&c, &clen); break;
case 'm': recv_variable(&m, &mlen); break;
case 'a': recv_variable(&ad, &adlen); break;
case 'k': recv_variable(&k, &klen); break;
case 's': read_variable(&nsec, &nslen); break;
case 'p': read_variable(&npub, &nplen); break;
case 's': recv_variable(&nsec, &nslen); break;
case 'p': recv_variable(&npub, &nplen); break;
case 'C': write_variable(c, clen); break;
case 'M': write_variable(m, mlen); break;
case 'A': write_variable(ad, adlen); break;
case 'K': write_variable(k, klen); break;
case 'S': write_variable(nsec, nslen); break;
case 'P': write_variable(npub, nplen); break;
case 'C': send_variable(c, clen); break;
case 'M': send_variable(m, mlen); break;
case 'A': send_variable(ad, adlen); break;
case 'K': send_variable(k, klen); break;
case 'S': send_variable(nsec, nslen); break;
case 'P': send_variable(npub, nplen); break;
case 'e':
......@@ -124,6 +124,7 @@ int main() {
fprintf(stderr, "klen = %llu\r\n", klen);
fprintf(stderr, "k = "); FPRINTF_HEX(stderr, k, klen); fprintf(stderr, "\r\n");
*/
if (k == NULL) {
fprintf(stderr, "Missing key\r\n");
return 3;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment