diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2015-06-24 01:16:40 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2015-06-24 01:16:40 +0300 |
commit | 724583d4ab12e520b29c00c36ffe2840b6962059 (patch) | |
tree | 6f62422ccd66afc60724c86a54a6bf374a203124 /include/aesni/mode.h | |
parent | test/toolkit.py: fix executable names (diff) | |
download | aes-tools-724583d4ab12e520b29c00c36ffe2840b6962059.tar.gz aes-tools-724583d4ab12e520b29c00c36ffe2840b6962059.zip |
use macros to get rid of repetitive code
Diffstat (limited to 'include/aesni/mode.h')
-rw-r--r-- | include/aesni/mode.h | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/include/aesni/mode.h b/include/aesni/mode.h index fc00e9c..122b842 100644 --- a/include/aesni/mode.h +++ b/include/aesni/mode.h @@ -8,6 +8,8 @@ #pragma once +#include <assert.h> + typedef enum { AESNI_ECB, @@ -17,3 +19,136 @@ typedef enum AESNI_CTR, } AesNI_Mode; + +#define AESNI_ENCRYPT_BLOCK_ECB(prefix, BlockT, KeyT) \ +static __inline BlockT __fastcall aesni_## prefix ##_encrypt_block_ecb( \ + BlockT plaintext, \ + const KeyT* key) \ +{ \ + assert(key); \ +\ + return aesni_## prefix ##_encrypt_block_(plaintext, key); \ +} + +#define AESNI_DECRYPT_BLOCK_ECB(prefix, BlockT, KeyT) \ +static __inline BlockT __fastcall aesni_## prefix ##_decrypt_block_ecb( \ + BlockT ciphertext, \ + const KeyT* key) \ +{ \ + assert(key); \ +\ + return aesni_## prefix ##_decrypt_block_(ciphertext, key); \ +} + +#define AESNI_ENCRYPT_BLOCK_CBC(prefix, BlockT, KeyT) \ +static __inline BlockT __fastcall aesni_## prefix ##_encrypt_block_cbc( \ + BlockT plaintext, \ + const KeyT* key, \ + BlockT init_vector, \ + BlockT* next_init_vector) \ +{ \ + assert(key); \ + assert(next_init_vector); \ +\ + return *next_init_vector = aesni_## prefix ##_encrypt_block_ecb( \ + aesni_## prefix ##_xor_blocks(plaintext, init_vector), key); \ +} + +#define AESNI_DECRYPT_BLOCK_CBC(prefix, BlockT, KeyT) \ +static __inline BlockT __fastcall aesni_## prefix ##_decrypt_block_cbc( \ + BlockT ciphertext, \ + const KeyT* key, \ + BlockT init_vector, \ + BlockT* next_init_vector) \ +{ \ + assert(key); \ + assert(next_init_vector); \ +\ + BlockT plaintext = aesni_## prefix ##_xor_blocks( \ + aesni_## prefix ##_decrypt_block_ecb(ciphertext, key), init_vector); \ + *next_init_vector = ciphertext; \ + return plaintext; \ +} + +#define AESNI_ENCRYPT_BLOCK_CFB(prefix, BlockT, KeyT) \ +static __inline BlockT __fastcall aesni_## prefix ##_encrypt_block_cfb( \ + BlockT plaintext, \ + const KeyT* key, \ + BlockT init_vector, \ + BlockT* next_init_vector) \ +{ \ + assert(key); \ + assert(next_init_vector); \ +\ + return *next_init_vector = aesni_## prefix ##_xor_blocks( \ + aesni_## prefix ##_encrypt_block_ecb(init_vector, key), plaintext); \ +} + +#define AESNI_DECRYPT_BLOCK_CFB(prefix, BlockT, KeyT) \ +static __inline BlockT __fastcall aesni_## prefix ##_decrypt_block_cfb( \ + BlockT ciphertext, \ + const KeyT* key, \ + BlockT init_vector, \ + BlockT* next_init_vector) \ +{ \ + assert(key); \ + assert(next_init_vector); \ +\ + BlockT plaintext = aesni_## prefix ##_xor_blocks( \ + aesni_## prefix ##_encrypt_block_ecb(init_vector, key), ciphertext); \ + *next_init_vector = ciphertext; \ + return plaintext; \ +} + +#define AESNI_ENCRYPT_BLOCK_OFB(prefix, BlockT, KeyT) \ +static __inline BlockT __fastcall aesni_## prefix ##_encrypt_block_ofb( \ + BlockT plaintext, \ + const KeyT* key, \ + BlockT init_vector, \ + BlockT* next_init_vector) \ +{ \ + assert(key); \ + assert(next_init_vector); \ +\ + BlockT tmp = aesni_## prefix ##_encrypt_block_ecb(init_vector, key); \ + *next_init_vector = tmp; \ + return aesni_## prefix ##_xor_blocks(tmp, plaintext); \ +} + +#define AESNI_DECRYPT_BLOCK_OFB(prefix, BlockT, KeyT) \ +static __inline BlockT __fastcall aesni_## prefix ##_decrypt_block_ofb( \ + BlockT ciphertext, \ + const KeyT* key, \ + BlockT init_vector, \ + BlockT* next_init_vector) \ +{ \ + return aesni_## prefix ##_encrypt_block_ofb( \ + ciphertext, key, init_vector, next_init_vector); \ +} + +#define AESNI_ENCRYPT_BLOCK_CTR(prefix, BlockT, KeyT) \ +static __inline BlockT __fastcall aesni_## prefix ##_encrypt_block_ctr( \ + BlockT plaintext, \ + const KeyT* key, \ + BlockT init_vector, \ + BlockT* next_init_vector) \ +{ \ + assert(key); \ + assert(next_init_vector); \ +\ + BlockT ciphertext = aesni_## prefix ##_xor_blocks( \ + plaintext, aesni_## prefix ##_encrypt_block_ecb(init_vector, key)); \ + *next_init_vector = aesni_## prefix ##_inc_block(init_vector); \ + return ciphertext; \ +} + +#define AESNI_DECRYPT_BLOCK_CTR(prefix, BlockT, KeyT) \ +static __inline BlockT __fastcall aesni_## prefix ##_decrypt_block_ctr( \ + BlockT ciphertext, \ + const KeyT* key, \ + BlockT init_vector, \ + BlockT* next_init_vector) \ +{ \ + return aesni_## prefix ##_encrypt_block_ctr( \ + ciphertext, key, init_vector, next_init_vector); \ +} |