Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge aes.hpp into aes.h #209

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 2 additions & 2 deletions aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 9 additions & 1 deletion aes.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef _AES_H_
#define _AES_H_

#ifdef __cplusplus
extern "C" {
#endif //__cplusplus

#include <stdint.h>
#include <stddef.h>

Expand Down Expand Up @@ -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)
Expand All @@ -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_
12 changes: 0 additions & 12 deletions aes.hpp

This file was deleted.

3 changes: 1 addition & 2 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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")

Expand Down
10 changes: 5 additions & 5 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion test.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
#include "aes.hpp"
#include "test.c"