From aaa225771694c5b63da1500536064c30bd877a28 Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Sat, 26 May 2018 11:36:55 -0400 Subject: [PATCH] start aes_gcm --- Makefile | 13 +++++++++++-- crypto.h | 10 +++++++--- crypto/aes_gcm.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ main.c | 3 ++- 4 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 crypto/aes_gcm.c diff --git a/Makefile b/Makefile index 4a92632..9c77936 100644 --- a/Makefile +++ b/Makefile @@ -13,15 +13,24 @@ src = $(wildcard *.c) $(wildcard crypto/*.c) crypto/tiny-AES-c/aes.c obj = $(src:.c=.o) uECC.o LDFLAGS = -Wl,--gc-sections ./tinycbor/lib/libtinycbor.a -CFLAGS = -O2 -fdata-sections -ffunction-sections -I./tinycbor/src -I./crypto -I./crypto/micro-ecc/ -Icrypto/tiny-AES-c/ +CFLAGS = -O2 -fdata-sections -ffunction-sections -I./tinycbor/src -I./crypto -I./crypto/micro-ecc/ -Icrypto/tiny-AES-c/ -I. name = main +all: main + +test: testgcm + $(name): $(obj) $(CC) $(LDFLAGS) -o $@ $(obj) $(LDFLAGS) +testgcm: $(obj) + $(CC) -c main.c $(CFLAGS) -DTEST -o main.o + $(CC) -c crypto/aes_gcm.c $(CFLAGS) -DTEST -o crypto/aes_gcm.o + $(CC) $(LDFLAGS) -o $@ $^ $(LDFLAGS) + uECC.o: ./crypto/micro-ecc/uECC.c $(CC) -c -o $@ $^ -O2 -fdata-sections -ffunction-sections -DuECC_PLATFORM=$(platform) -I./crypto/micro-ecc/ clean: - rm -f *.o main.exe main + rm -f *.o main.exe main crypto/tiny-AES-c/*.o crypto/*.o crypto/micro-ecc/*.o diff --git a/crypto.h b/crypto.h index 6ca99e5..ac7bed0 100644 --- a/crypto.h +++ b/crypto.h @@ -1,6 +1,7 @@ #ifndef _CRYPTO_H #define _CRYPTO_H +#include #define USE_SOFTWARE_IMPLEMENTATION @@ -16,7 +17,7 @@ void crypto_sha256_hmac_final(uint8_t * key, uint32_t klen, uint8_t * hmac); void crypto_ecc256_init(); void crypto_ecc256_derive_public_key(uint8_t * data, int len, uint8_t * x, uint8_t * y); -void crypto_ecc256_load_key(uint8_t * data, int len); +void crypto_ecc256_load_key(uint8_t * data, int len, uint8_t * data2, int len2); void crypto_ecc256_load_attestation_key(); void crypto_ecc256_sign(uint8_t * data, int len, uint8_t * sig); @@ -26,8 +27,11 @@ void crypto_ecc256_make_key_pair(uint8_t * pubkey, uint8_t * privkey); void crypto_ecc256_shared_secret(const uint8_t * pubkey, const uint8_t * privkey, uint8_t * shared_secret); // Key must be 32 bytes -void crypto_aes256_init(uint8_t * key); -void crypto_aes256_reset_iv(); +#define CRYPTO_TRANSPORT_KEY NULL +#define CRYPTO_MASTER_KEY NULL + +void crypto_aes256_init(uint8_t * key, uint8_t * nonce); +void crypto_aes256_reset_iv(uint8_t * nonce); // buf length must be multiple of 16 bytes void crypto_aes256_decrypt(uint8_t * buf, int lenth); diff --git a/crypto/aes_gcm.c b/crypto/aes_gcm.c new file mode 100644 index 0000000..74786dc --- /dev/null +++ b/crypto/aes_gcm.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +#include "aes.h" +#include "crypto.h" +#include "util.h" + +#define BLOCK_SIZE 16 + +static struct AES_ctx aes_ctx; + +// void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); + +int8_t crypto_aes256_gcm_encrypt(uint8_t * data, uint32_t length, uint8_t * authtag) +{ + memset(authtag, 0, BLOCK_SIZE); + AES_CTR_xcrypt_buffer(&aes_ctx, authtag, BLOCK_SIZE); + + return 0; +} + +#ifdef TEST + +int main(int argc, char * argv[]) +{ + uint8_t nonce[16]; + uint8_t key[32]; + uint8_t authtag[BLOCK_SIZE]; + + uint8_t * authtag1 = "\x53\x0f\x8a\xfb\xc7\x45\x36\xb9\xa9\x63\xb4\xf1\xc4\xcb\x73\x8b"; + + memset(nonce,0,16); + memset(key,0,16); + + AES_init_ctx_iv(&aes_ctx, key, nonce); + + crypto_aes256_gcm_encrypt(NULL, 0, authtag); + + printf("Auth tag: "); dump_hex(authtag, BLOCK_SIZE); + + + return 0; +} + +#endif diff --git a/main.c b/main.c index c76cf94..d73f835 100644 --- a/main.c +++ b/main.c @@ -19,7 +19,7 @@ static void check_ret(CborError ret) } } - +#ifndef TEST int main(int argc, char * argv[]) { set_logging_mask( @@ -61,3 +61,4 @@ int main(int argc, char * argv[]) printf("done\n"); return 0; } +#endif