diff --git a/CMakeLists.txt b/CMakeLists.txt index 539cd68..519b2cb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,6 @@ target_sources(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/aes.c INTERFACE ${CMAKE_CURRENT_LIST_DIR}/aes.h - ${CMAKE_CURRENT_LIST_DIR}/aes.hpp ) target_include_directories(${PROJECT_NAME} diff --git a/README.md b/README.md index 6811e0e..e04368b 100644 --- a/README.md +++ b/README.md @@ -26,20 +26,18 @@ void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length); void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length); ``` -Important notes: +Important notes: * No padding is provided so for CBC and ECB all buffers should be multiples of 16 bytes. For padding [PKCS7](https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7) is recommendable. * ECB mode is considered unsafe for most uses and is not implemented in streaming mode. If you need this mode, call the function for every block of 16 bytes you need encrypted. See [wikipedia's article on ECB](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_Codebook_(ECB)) for more details. * This library is designed for small code size and simplicity, intended for cases where small binary size, low memory footprint and portability is more important than high performance. If speed is a concern, you can try more complex libraries, e.g. [Mbed TLS](https://tls.mbed.org/), [OpenSSL](https://www.openssl.org/) etc. You can choose to use any or all of the modes-of-operations, by defining the symbols CBC, CTR or ECB in [`aes.h`](https://github.com/kokke/tiny-AES-c/blob/master/aes.h) (read the comments for clarification). -C++ users should `#include` [aes.hpp](https://github.com/kokke/tiny-AES-c/blob/master/aes.hpp) instead of [aes.h](https://github.com/kokke/tiny-AES-c/blob/master/aes.h) - There is no built-in error checking or protection from out-of-bounds memory access errors as a result of malicious input. The module uses less than 200 bytes of RAM and 1-2K ROM when compiled for ARM, but YMMV depending on which modes are enabled. -It is one of the smallest implementations in C I've seen yet, but do contact me if you know of something smaller (or have improvements to the code here). +It is one of the smallest implementations in C I've seen yet, but do contact me if you know of something smaller (or have improvements to the code here). I've successfully used the code on 64bit x86, 32bit ARM and 8 bit AVR platforms. diff --git a/aes.c b/aes.c index 4481f7b..8e27c5b 100644 --- a/aes.c +++ b/aes.c @@ -552,9 +552,9 @@ void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length) /* Increment Iv and handle overflow */ for (bi = (AES_BLOCKLEN - 1); bi >= 0; --bi) { - /* inc will overflow */ + /* inc will overflow */ if (ctx->Iv[bi] == 255) - { + { ctx->Iv[bi] = 0; continue; } diff --git a/aes.h b/aes.h index b29b668..876d976 100644 --- a/aes.h +++ b/aes.h @@ -1,6 +1,10 @@ #ifndef _AES_H_ #define _AES_H_ +#ifdef __cplusplus +extern "C" { +#endif //__cplusplus + #include #include @@ -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,9 @@ void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length); #endif // #if defined(CTR) && (CTR == 1) +#ifdef __cplusplus +} +#endif //__cplusplus + #endif // _AES_H_ diff --git a/aes.hpp b/aes.hpp deleted file mode 100644 index ade1642..0000000 --- a/aes.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef _AES_HPP_ -#define _AES_HPP_ - -#ifndef __cplusplus -#error Do not include the hpp header in a c project! -#endif //__cplusplus - -extern "C" { -#include "aes.h" -} - -#endif //_AES_HPP_ diff --git a/conanfile.py b/conanfile.py index 8a75744..386d0f0 100644 --- a/conanfile.py +++ b/conanfile.py @@ -12,7 +12,7 @@ class TinyAesCConan(ConanFile): settings = "os", "compiler", "build_type", "arch" generators = "cmake" - exports_sources = ["CMakeLists.txt", "*.c", '*.h', '*.hpp'] + exports_sources = ["CMakeLists.txt", "*.c", '*.h'] exports = ["unlicense.txt"] _options_dict = { @@ -65,7 +65,6 @@ def build(self): def package(self): self.copy("*.h", dst="include") - self.copy("*.hpp", dst="include") self.copy("*.a", dst="lib", keep_path=False) self.copy("unlicense.txt") diff --git a/test.c b/test.c index 6796283..041eb68 100644 --- a/test.c +++ b/test.c @@ -98,8 +98,8 @@ static void test_encrypt_ecb_verbose(void) for (i = 0; i < 4; ++i) { - AES_ECB_encrypt(&ctx, plain_text + (i * 16)); - phex(plain_text + (i * 16)); + AES_ECB_encrypt(&ctx, plain_text + (i * 16)); + phex(plain_text + (i * 16)); } printf("\n"); } @@ -265,12 +265,12 @@ static int test_xcrypt_ctr(const char* xcrypt) 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_CTR_xcrypt_buffer(&ctx, in, 64); - + printf("CTR %s: ", xcrypt); - + if (0 == memcmp((char *) out, (char *) in, 64)) { printf("SUCCESS!\n"); return(0); diff --git a/test.cpp b/test.cpp index b7c8304..1873518 100644 --- a/test.cpp +++ b/test.cpp @@ -1,2 +1 @@ -#include "aes.hpp" #include "test.c"