Commit 1e86fddb authored by Stoian Ivanov's avatar Stoian Ivanov
Browse files

inplace api and test, Makefile update

parent 88f25887
...@@ -2,7 +2,11 @@ ...@@ -2,7 +2,11 @@
#CFLAGS = -Wall -mmcu=atmega16 -Os -Wl,-Map,test.map #CFLAGS = -Wall -mmcu=atmega16 -Os -Wl,-Map,test.map
#OBJCOPY = avr-objcopy #OBJCOPY = avr-objcopy
CC = gcc CC = gcc
CFLAGS = -Wall -Os -Wl,-Map,test.map LD = gcc
CFLAGS = -Wall -Os -c
LDFLAGS = -Wall -Os -Wl,-Map,test.map
OBJCOPYFLAFS = -j .text -O ihex
OBJCOPY = objcopy OBJCOPY = objcopy
# include path to AVR library # include path to AVR library
...@@ -10,28 +14,27 @@ INCLUDE_PATH = /usr/lib/avr/include ...@@ -10,28 +14,27 @@ INCLUDE_PATH = /usr/lib/avr/include
# splint static check # splint static check
SPLINT = splint test.c aes.c -I$(INCLUDE_PATH) +charindex -unrecog SPLINT = splint test.c aes.c -I$(INCLUDE_PATH) +charindex -unrecog
default: test.elf
.SILENT: .SILENT:
.PHONY: lint clean .PHONY: lint clean
test.hex : test.elf
echo copy object-code to new image and format in hex
$(OBJCOPY) ${OBJCOPYFLAFS} $< $@
rom.hex : test.out test.o : test.c aes.h aes.o
# copy object-code to new image and format in hex echo [CC] $@
$(OBJCOPY) -j .text -O ihex test.out rom.hex $(CC) $(CFLAGS) -o $@ $<
test.o : test.c
# compiling test.c
$(CC) $(CFLAGS) -c test.c -o test.o
aes.o : aes.h aes.c aes.o : aes.c aes.h
# compiling aes.c echo [CC] $@
$(CC) $(CFLAGS) -c aes.c -o aes.o $(CC) $(CFLAGS) -o $@ $<
test.out : aes.o test.o test.elf : aes.o test.o
# linking object code to binary echo [LD] $@
$(CC) $(CFLAGS) aes.o test.o -o test.out $(LD) $(LDFLAGS) -o $@ $^
small: test.out
$(OBJCOPY) -j .text -O ihex test.out rom.hex
clean: clean:
rm -f *.OBJ *.LST *.o *.gch *.out *.hex *.map rm -f *.OBJ *.LST *.o *.gch *.out *.hex *.map
......
...@@ -482,21 +482,16 @@ static void InvCipher(state_t *state,uint8_t*RoundKey) ...@@ -482,21 +482,16 @@ static void InvCipher(state_t *state,uint8_t*RoundKey)
#if defined(ECB) && (ECB == 1) #if defined(ECB) && (ECB == 1)
void AES_ECB_encrypt(struct AES_ctx *ctx,const uint8_t* input, uint8_t* output) void AES_ECB_encrypt(struct AES_ctx *ctx,const uint8_t* buf)
{ {
// Copy input to output, and work in-memory on output
memcpy(output, input, AES_BLOCKLEN);
// The next function call encrypts the PlainText with the Key using AES algorithm. // The next function call encrypts the PlainText with the Key using AES algorithm.
Cipher((state_t*)output,ctx->RoundKey); Cipher((state_t*)buf,ctx->RoundKey);
} }
void AES_ECB_decrypt(struct AES_ctx *ctx,const uint8_t* input, uint8_t *output) void AES_ECB_decrypt(struct AES_ctx *ctx,const uint8_t* buf)
{ {
// Copy input to output, and work in-memory on output // The next function call decrypts the PlainText with the Key using AES algorithm.
memcpy(output, input, AES_BLOCKLEN); InvCipher((state_t*)buf,ctx->RoundKey);
InvCipher((state_t*)output,ctx->RoundKey);
} }
...@@ -518,35 +513,33 @@ static void XorWithIv(uint8_t* buf,uint8_t*Iv) ...@@ -518,35 +513,33 @@ static void XorWithIv(uint8_t* buf,uint8_t*Iv)
} }
} }
void AES_CBC_encrypt_buffer(struct AES_ctx *ctx,uint8_t* output, uint8_t* input, uint32_t length) void AES_CBC_encrypt_buffer(struct AES_ctx *ctx,uint8_t* buf, uint32_t length)
{ {
uintptr_t i; uintptr_t i;
uint8_t *Iv=ctx->Iv; uint8_t *Iv=ctx->Iv;
memcpy(output, input, length);
for (i = 0; i < length; i += AES_BLOCKLEN) for (i = 0; i < length; i += AES_BLOCKLEN)
{ {
XorWithIv(output,Iv); XorWithIv(buf,Iv);
Cipher((state_t*)output,ctx->RoundKey); Cipher((state_t*)buf,ctx->RoundKey);
Iv = output; Iv = buf;
output += AES_BLOCKLEN; buf += AES_BLOCKLEN;
//printf("Step %d - %d", i/16, i); //printf("Step %d - %d", i/16, i);
} }
//store Iv in ctx for next call //store Iv in ctx for next call
memcpy(ctx->Iv,Iv,AES_BLOCKLEN); memcpy(ctx->Iv,Iv,AES_BLOCKLEN);
} }
void AES_CBC_decrypt_buffer(struct AES_ctx *ctx, uint8_t* output, uint8_t* input, uint32_t length) void AES_CBC_decrypt_buffer(struct AES_ctx *ctx, uint8_t* buf, uint32_t length)
{ {
uintptr_t i; uintptr_t i;
uint8_t *Iv=ctx->Iv; uint8_t storeNextIv[AES_BLOCKLEN];
memcpy(output, input, length);
for (i = 0; i < length; i += AES_BLOCKLEN) for (i = 0; i < length; i += AES_BLOCKLEN)
{ {
InvCipher((state_t*)output,ctx->RoundKey); memcpy(storeNextIv, buf, AES_BLOCKLEN);
XorWithIv(output,Iv); InvCipher((state_t*)buf,ctx->RoundKey);
Iv = input; //we DO need original input stored here XorWithIv(buf,ctx->Iv);
input += AES_BLOCKLEN; memcpy(ctx->Iv, storeNextIv, AES_BLOCKLEN);
output += AES_BLOCKLEN; buf += AES_BLOCKLEN;
} }
} }
...@@ -558,34 +551,35 @@ void AES_CBC_decrypt_buffer(struct AES_ctx *ctx, uint8_t* output, uint8_t* input ...@@ -558,34 +551,35 @@ void AES_CBC_decrypt_buffer(struct AES_ctx *ctx, uint8_t* output, uint8_t* input
#if defined(CTR) && (CTR == 1) #if defined(CTR) && (CTR == 1)
/* Symmetrical operation: same function for encrypting as for decrypting. Note any IV/nonce should never be reused with the same key */ /* Symmetrical operation: same function for encrypting as for decrypting. Note any IV/nonce should never be reused with the same key */
void AES_CTR_xcrypt_buffer(struct AES_ctx *ctx,uint8_t* output, uint8_t* input, uint32_t length) void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length)
{ {
uint8_t buffer[AES_BLOCKLEN]; uint8_t buffer[AES_BLOCKLEN];
int j;
unsigned i; unsigned i;
for (i = 0; i < length; ++i) int bi;
for (i = 0,bi=AES_BLOCKLEN; i < length; ++i,bi++)
{ {
if ((i & (AES_BLOCKLEN - 1)) == 0) //we need to regen xor compliment in buff if (bi == AES_BLOCKLEN) //we need to regen xor compliment in buffer
{ {
memcpy(buffer, ctx->Iv, AES_BLOCKLEN); memcpy(buffer, ctx->Iv, AES_BLOCKLEN);
Cipher((state_t*)buffer,ctx->RoundKey); Cipher((state_t*)buffer,ctx->RoundKey);
/* Increment counter and handle overflow */ /* Increment Iv and handle overflow */
for (j = (AES_BLOCKLEN - 1); j >= 0; --j) for (bi = (AES_BLOCKLEN - 1); bi >= 0; --bi)
{
ctx->Iv[j] += 1;
/* Break if no overflow, keep going otherwise */
if (ctx->Iv[j] != 0)
{ {
break; if (ctx->Iv[bi] == 255) { //inc will owerflow
ctx->Iv[bi]=0;
continue;
} }
ctx->Iv[bi] += 1;
break;
} }
bi=0;
} }
output[i] = (input[i] ^ buffer[(i & (AES_BLOCKLEN - 1))]); buf[i] = (buf[i] ^ buffer[bi]);
} }
} }
......
...@@ -58,8 +58,8 @@ void AES_ctx_set_iv(struct AES_ctx *ctx,const uint8_t* iv); ...@@ -58,8 +58,8 @@ void AES_ctx_set_iv(struct AES_ctx *ctx,const uint8_t* iv);
// buffer size is exactly AES_BLOCKLEN bytes; // buffer size is exactly AES_BLOCKLEN bytes;
// you need only AES_init_ctx as Iv is not used in ECB // you need only AES_init_ctx as Iv is not used in ECB
// NB: ECB s considered insecure // NB: ECB s considered insecure
void AES_ECB_encrypt(struct AES_ctx *ctx, const uint8_t* input, uint8_t *output); void AES_ECB_encrypt(struct AES_ctx *ctx, const uint8_t* buf);
void AES_ECB_decrypt(struct AES_ctx *ctx, const uint8_t* input, uint8_t *output); void AES_ECB_decrypt(struct AES_ctx *ctx, const uint8_t* buf);
#endif // #if defined(ECB) && (ECB == !) #endif // #if defined(ECB) && (ECB == !)
...@@ -69,8 +69,8 @@ void AES_ECB_decrypt(struct AES_ctx *ctx, const uint8_t* input, uint8_t *output) ...@@ -69,8 +69,8 @@ void AES_ECB_decrypt(struct AES_ctx *ctx, const uint8_t* input, uint8_t *output)
// We suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 if you need one // We suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 if you need one
// you need to set iv in ctx via AES_init_ctx_iv or AES_ctx_set_iv // you need to set iv in ctx via AES_init_ctx_iv or AES_ctx_set_iv
// NB: no IV should ever be reused with the same key // NB: no IV should ever be reused with the same key
void AES_CBC_encrypt_buffer(struct AES_ctx *ctx, uint8_t* output, uint8_t* input, uint32_t length); void AES_CBC_encrypt_buffer(struct AES_ctx *ctx, uint8_t* buf, uint32_t length);
void AES_CBC_decrypt_buffer(struct AES_ctx *ctx, uint8_t* output, uint8_t* input, uint32_t length); void AES_CBC_decrypt_buffer(struct AES_ctx *ctx, uint8_t* buf, uint32_t length);
#endif // #if defined(CBC) && (CBC == 1) #endif // #if defined(CBC) && (CBC == 1)
...@@ -83,7 +83,7 @@ void AES_CBC_decrypt_buffer(struct AES_ctx *ctx, uint8_t* output, uint8_t* input ...@@ -83,7 +83,7 @@ void AES_CBC_decrypt_buffer(struct AES_ctx *ctx, uint8_t* output, uint8_t* input
// We suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 if you need one // We suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 if you need one
// you need to set iv in ctx via AES_init_ctx_iv or AES_ctx_set_iv // you need to set iv in ctx via AES_init_ctx_iv or AES_ctx_set_iv
// NB: no IV should ever be reused with the same key // NB: no IV should ever be reused with the same key
void AES_CTR_xcrypt_buffer(struct AES_ctx *ctx, uint8_t* output, uint8_t* input, uint32_t length); void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length);
#endif // #if defined(CTR) && (CTR == 1) #endif // #if defined(CTR) && (CTR == 1)
......
...@@ -101,8 +101,8 @@ static void test_encrypt_ecb_verbose(void) ...@@ -101,8 +101,8 @@ static void test_encrypt_ecb_verbose(void)
AES_init_ctx(&ctx,key); AES_init_ctx(&ctx,key);
for(i = 0; i < 4; ++i) for(i = 0; i < 4; ++i)
{ {
AES_ECB_encrypt(&ctx,plain_text + (i*16), buf+(i*16)); AES_ECB_encrypt(&ctx,plain_text + (i*16));
phex(buf + (i*16)); phex(plain_text + (i*16));
} }
printf("\n"); printf("\n");
} }
...@@ -124,14 +124,13 @@ static void test_encrypt_ecb(void) ...@@ -124,14 +124,13 @@ static void test_encrypt_ecb(void)
#endif #endif
uint8_t in[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a}; uint8_t in[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
uint8_t buffer[16];
struct AES_ctx ctx; AES_init_ctx(&ctx,key); struct AES_ctx ctx; AES_init_ctx(&ctx,key);
AES_ECB_encrypt(&ctx,in, buffer); AES_ECB_encrypt(&ctx,in);
printf("ECB encrypt: "); printf("ECB encrypt: ");
if(0 == memcmp((char*) out, (char*) buffer, 16)) if(0 == memcmp((char*) out, (char*) in, 16))
{ {
printf("SUCCESS!\n"); printf("SUCCESS!\n");
} }
...@@ -169,14 +168,14 @@ static void test_decrypt_cbc(void) ...@@ -169,14 +168,14 @@ static void test_decrypt_cbc(void)
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 }; 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
uint8_t buffer[64]; // uint8_t buffer[64];
struct AES_ctx ctx; struct AES_ctx ctx;
AES_init_ctx_iv(&ctx,key,iv); AES_init_ctx_iv(&ctx,key,iv);
AES_CBC_decrypt_buffer(&ctx,buffer, in, 64); AES_CBC_decrypt_buffer(&ctx,in, 64);
printf("CBC decrypt: "); printf("CBC decrypt: ");
if(0 == memcmp((char*) out, (char*) buffer, 64)) if(0 == memcmp((char*) out, (char*) in, 64))
{ {
printf("SUCCESS!\n"); printf("SUCCESS!\n");
} }
...@@ -214,14 +213,13 @@ static void test_encrypt_cbc(void) ...@@ -214,14 +213,13 @@ static void test_encrypt_cbc(void)
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 }; 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
uint8_t buffer[64];
struct AES_ctx ctx; struct AES_ctx ctx;
AES_init_ctx_iv(&ctx,key,iv); AES_init_ctx_iv(&ctx,key,iv);
AES_CBC_encrypt_buffer(&ctx,buffer, in, 64); AES_CBC_encrypt_buffer(&ctx, in, 64);
printf("CBC encrypt: "); printf("CBC encrypt: ");
if(0 == memcmp((char*) out, (char*) buffer, 64)) if(0 == memcmp((char*) out, (char*) in, 64))
{ {
printf("SUCCESS!\n"); printf("SUCCESS!\n");
} }
...@@ -270,16 +268,15 @@ static void test_xcrypt_ctr(const char* xcrypt) ...@@ -270,16 +268,15 @@ static void test_xcrypt_ctr(const char* xcrypt)
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 }; 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
uint8_t buffer[64];
struct AES_ctx ctx; struct AES_ctx ctx;
AES_init_ctx_iv(&ctx,key,iv); AES_init_ctx_iv(&ctx,key,iv);
AES_CTR_xcrypt_buffer(&ctx,buffer, in, 64); AES_CTR_xcrypt_buffer(&ctx, in, 64);
printf("CTR %s: ", xcrypt); printf("CTR %s: ", xcrypt);
if (0 == memcmp((char *) out, (char *) buffer, 64)) if (0 == memcmp((char *) out, (char *) in, 64))
{ {
printf("SUCCESS!\n"); printf("SUCCESS!\n");
} }
...@@ -306,14 +303,14 @@ static void test_decrypt_ecb(void) ...@@ -306,14 +303,14 @@ static void test_decrypt_ecb(void)
#endif #endif
uint8_t out[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a}; uint8_t out[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
uint8_t buffer[16];
struct AES_ctx ctx; struct AES_ctx ctx;
AES_init_ctx(&ctx,key); AES_init_ctx(&ctx,key);
AES_ECB_decrypt(&ctx,in, buffer); AES_ECB_decrypt(&ctx,in);
printf("ECB decrypt: "); printf("ECB decrypt: ");
if(0 == memcmp((char*) out, (char*) buffer, 16)) if(0 == memcmp((char*) out, (char*) in, 16))
{ {
printf("SUCCESS!\n"); printf("SUCCESS!\n");
} }
......
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 to comment