diff --git a/aes.c b/aes.c index 4481f7b..695e5f4 100644 --- a/aes.c +++ b/aes.c @@ -486,7 +486,7 @@ void AES_ECB_decrypt(const struct AES_ctx* ctx, uint8_t* buf) -#if defined(CBC) && (CBC == 1) +#if defined(CBC) && (CBC == 1) || (defined(OFB) && (OFB == 1)) static void XorWithIv(uint8_t* buf, const uint8_t* Iv) @@ -497,7 +497,9 @@ static void XorWithIv(uint8_t* buf, const uint8_t* Iv) buf[i] ^= Iv[i]; } } +#endif // #if defined(CBC) && (CBC == 1) || (defined(OFB) && (OFB == 1)) +#if defined(CBC) && (CBC == 1) void AES_CBC_encrypt_buffer(struct AES_ctx *ctx, uint8_t* buf, size_t length) { size_t i; @@ -570,3 +572,23 @@ void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length) #endif // #if defined(CTR) && (CTR == 1) +#if defined(OFB) && (OFB == 1) + +// Same function for encrypting as for decrypting. +// NOTES: you need to set IV in ctx with AES_init_ctx_iv() or AES_ctx_set_iv() +// no IV should ever be reused with the same key +void AES_OFB_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length) +{ + size_t i; + uint8_t *Iv = ctx->Iv; + for (i = 0; i < length; i += AES_BLOCKLEN) + { + Cipher((state_t*)Iv, ctx->RoundKey); + XorWithIv(buf, Iv); + buf += AES_BLOCKLEN; + } + /* store Iv in ctx for next call */ + memcpy(ctx->Iv, Iv, AES_BLOCKLEN); +} + +#endif // #if defined(OFB) && (OFB == 1) diff --git a/aes.h b/aes.h index b29b668..7b9866a 100644 --- a/aes.h +++ b/aes.h @@ -8,6 +8,7 @@ // // CBC enables AES encryption in CBC-mode of operation. // CTR enables encryption in counter-mode. +// OFB enables encryption in output feedback mode // ECB enables the basic ECB 16-byte block algorithm. All can be enabled simultaneously. // The #ifndef-guard allows it to be configured before #include'ing or at compile time. @@ -23,6 +24,9 @@ #define CTR 1 #endif +#ifndef OFB + #define OFB 1 +#endif #define AES128 1 //#define AES192 1 @@ -44,13 +48,13 @@ struct AES_ctx { uint8_t RoundKey[AES_keyExpSize]; -#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1)) +#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1)) || (defined(OFB) && (OFB == 1)) uint8_t Iv[AES_BLOCKLEN]; #endif }; void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key); -#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1)) +#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1)) || (defined(OFB) && (OFB == 1)) void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv); void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv); #endif @@ -62,7 +66,7 @@ void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv); void AES_ECB_encrypt(const struct AES_ctx* ctx, uint8_t* buf); void AES_ECB_decrypt(const struct AES_ctx* ctx, uint8_t* buf); -#endif // #if defined(ECB) && (ECB == !) +#endif // #if defined(ECB) && (ECB == 1) #if defined(CBC) && (CBC == 1) @@ -87,5 +91,14 @@ void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length); #endif // #if defined(CTR) && (CTR == 1) +#if defined(OFB) && (OFB == 1) + +// Same function for encrypting as for decrypting. +// NOTES: you need to set IV in ctx with AES_init_ctx_iv() or AES_ctx_set_iv() +// no IV should ever be reused with the same key +void AES_OFB_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length); + +#endif // #if defined(OFB) && (OFB == 1) + #endif // _AES_H_ diff --git a/test.c b/test.c index 6796283..d0961f2 100644 --- a/test.c +++ b/test.c @@ -7,6 +7,7 @@ #define CBC 1 #define CTR 1 #define ECB 1 +#define OFB 1 #include "aes.h" @@ -19,6 +20,8 @@ static int test_decrypt_ctr(void); static int test_encrypt_ecb(void); static int test_decrypt_ecb(void); static void test_encrypt_ecb_verbose(void); +static int test_encrypt_ofb(void); +static int test_decrypt_ofb(void); int main(void) @@ -38,6 +41,7 @@ int main(void) exit = test_encrypt_cbc() + test_decrypt_cbc() + test_encrypt_ctr() + test_decrypt_ctr() + + test_encrypt_ofb() + test_decrypt_ofb() + test_decrypt_ecb() + test_encrypt_ecb(); test_encrypt_ecb_verbose(); @@ -280,6 +284,60 @@ static int test_xcrypt_ctr(const char* xcrypt) } } +static int test_xcrypt_ofb(const char* xcrypt); +static int test_encrypt_ofb(void) +{ + return test_xcrypt_ofb("encrypt"); +} + +static int test_decrypt_ofb(void) +{ + return test_xcrypt_ofb("decrypt"); +} + +static int test_xcrypt_ofb(const char* xcrypt) +{ +#if defined(AES256) + uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, + 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 }; + uint8_t in[64] = { 0xDC, 0x7E, 0x84, 0xBF, 0xDA, 0x79, 0x16, 0x4B, 0x7E, 0xCD, 0x84, 0x86, 0x98, 0x5D, 0x38, 0x60, + 0x4F, 0xEB, 0xDC, 0x67, 0x40, 0xD2, 0x0B, 0x3A, 0xC8, 0x8F, 0x6A, 0xD8, 0x2A, 0x4F, 0xB0, 0x8D, + 0x71, 0xAB, 0x47, 0xA0, 0x86, 0xE8, 0x6E, 0xED, 0xF3, 0x9D, 0x1C, 0x5B, 0xBA, 0x97, 0xC4, 0x08, + 0x01, 0x26, 0x14, 0x1D, 0x67, 0xF3, 0x7B, 0xE8, 0x53, 0x8F, 0x5A, 0x8B, 0xE7, 0x40, 0xE4, 0x84 }; +#elif defined(AES192) + uint8_t key[24] = { 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5, + 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b }; + uint8_t in[64] = { 0xCD, 0xC8, 0x0D, 0x6F, 0xDD, 0xF1, 0x8C, 0xAB, 0x34, 0xC2, 0x59, 0x09, 0xC9, 0x9A, 0x41, 0x74, + 0xFC, 0xC2, 0x8B, 0x8D, 0x4C, 0x63, 0x83, 0x7C, 0x09, 0xE8, 0x17, 0x00, 0xC1, 0x10, 0x04, 0x01, + 0x8D, 0x9A, 0x9A, 0xEA, 0xC0, 0xF6, 0x59, 0x6F, 0x55, 0x9C, 0x6D, 0x4D, 0xAF, 0x59, 0xA5, 0xF2, + 0x6D, 0x9F, 0x20, 0x08, 0x57, 0xCA, 0x6C, 0x3E, 0x9C, 0xAC, 0x52, 0x4B, 0xD9, 0xAC, 0xC9, 0x2A }; +#elif defined(AES128) + uint8_t key[16] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }; + uint8_t in[64] = { 0x3B, 0x3F, 0xD9, 0x2E, 0xB7, 0x2D, 0xAD, 0x20, 0x33, 0x34, 0x49, 0xF8, 0xE8, 0x3C, 0xFB, 0x4A, + 0x77, 0x89, 0x50, 0x8D, 0x16, 0x91, 0x8F, 0x03, 0xF5, 0x3C, 0x52, 0xDA, 0xC5, 0x4E, 0xD8, 0x25, + 0x97, 0x40, 0x05, 0x1E, 0x9C, 0x5F, 0xEC, 0xF6, 0x43, 0x44, 0xF7, 0xA8, 0x22, 0x60, 0xED, 0xCC, + 0x30, 0x4C, 0x65, 0x28, 0xF6, 0x59, 0xC7, 0x78, 0x66, 0xA5, 0x10, 0xD9, 0xC1, 0xD6, 0xAE, 0x5E }; +#endif + uint8_t iv[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; + uint8_t out[64] = { 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A, + 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, + 0xF6, 0x9F, 0x24, 0x45, 0xDF, 0x4F, 0x9B, 0x17, 0xAD, 0x2B, 0x41, 0x7B, 0xE6, 0x6C, 0x37, 0x10 }; + struct AES_ctx ctx; + + AES_init_ctx_iv(&ctx, key, iv); + AES_OFB_xcrypt_buffer(&ctx, in, 64); + + printf("OFB %s: ", xcrypt); + + if (0 == memcmp((char *) out, (char *) in, 64)) { + printf("SUCCESS!\n"); + return(0); + } else { + printf("FAILURE!\n"); + return(1); + } +} static int test_decrypt_ecb(void) {