Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
Tiny AES C
Commits
4b4b04b8
Unverified
Commit
4b4b04b8
authored
Feb 22, 2019
by
kokke
Committed by
GitHub
Feb 22, 2019
Browse files
Merge pull request #126 from torfinnberset/master
Const-qualify all read-only pointers
parents
e72b6eff
2fe22ab8
Changes
3
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
4b4b04b8
...
...
@@ -15,8 +15,8 @@ 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);
/* Then start encrypting and decrypting with the functions below: */
void AES_ECB_encrypt(struct AES_ctx* ctx, uint8_t* buf);
void AES_ECB_decrypt(struct AES_ctx* ctx, uint8_t* buf);
void AES_ECB_encrypt(
const
struct AES_ctx* ctx, uint8_t* buf);
void AES_ECB_decrypt(
const
struct AES_ctx* ctx, uint8_t* buf);
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* buf, uint32_t length);
...
...
@@ -47,21 +47,21 @@ GCC size output when only CTR mode is compiled for ARM:
$ arm-none-eabi-gcc -Os -DCBC=0 -DECB=0 -DCTR=1 -c aes.c
$ size aes.o
text data bss dec hex filename
1
20
3 0 0 1
20
3
4b3
aes.o
1
34
3 0 0 1
34
3
53f
aes.o
.. and when compiling for the THUMB instruction set, we end up just below 1K in code size.
$ arm-none-eabi-gcc -Os -mthumb -DCBC=0 -DECB=0 -DCTR=1 -c aes.c
$ size aes.o
text data bss dec hex filename
9
55
0 0 9
55
3
bb
aes.o
9
79
0 0 9
79
3
d3
aes.o
I am using the Free Software Foundation, ARM GCC compiler:
$ arm-none-eabi-gcc --version
arm-none-eabi-gcc (
4.8.4-1+11-1) 4.8.4
201
4
121
9
(release)
Copyright (C) 201
3
Free Software Foundation, Inc.
arm-none-eabi-gcc (
GNU Tools for Arm Embedded Processors 8-2018-q4-major) 8.2.1
201
8
121
3
(release)
Copyright (C) 201
8
Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
...
...
aes.c
View file @
4b4b04b8
...
...
@@ -240,7 +240,7 @@ void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv)
// This function adds the round key to state.
// The round key is added to the state by an XOR function.
static
void
AddRoundKey
(
uint8_t
round
,
state_t
*
state
,
uint8_t
*
RoundKey
)
static
void
AddRoundKey
(
uint8_t
round
,
state_t
*
state
,
const
uint8_t
*
RoundKey
)
{
uint8_t
i
,
j
;
for
(
i
=
0
;
i
<
4
;
++
i
)
...
...
@@ -408,7 +408,7 @@ static void InvShiftRows(state_t* state)
#endif // #if (defined(CBC) && CBC == 1) || (defined(ECB) && ECB == 1)
// Cipher is the main function that encrypts the PlainText.
static
void
Cipher
(
state_t
*
state
,
uint8_t
*
RoundKey
)
static
void
Cipher
(
state_t
*
state
,
const
uint8_t
*
RoundKey
)
{
uint8_t
round
=
0
;
...
...
@@ -434,7 +434,7 @@ static void Cipher(state_t* state, uint8_t* RoundKey)
}
#if (defined(CBC) && CBC == 1) || (defined(ECB) && ECB == 1)
static
void
InvCipher
(
state_t
*
state
,
uint8_t
*
RoundKey
)
static
void
InvCipher
(
state_t
*
state
,
const
uint8_t
*
RoundKey
)
{
uint8_t
round
=
0
;
...
...
@@ -466,13 +466,13 @@ static void InvCipher(state_t* state,uint8_t* RoundKey)
#if defined(ECB) && (ECB == 1)
void
AES_ECB_encrypt
(
struct
AES_ctx
*
ctx
,
uint8_t
*
buf
)
void
AES_ECB_encrypt
(
const
struct
AES_ctx
*
ctx
,
uint8_t
*
buf
)
{
// The next function call encrypts the PlainText with the Key using AES algorithm.
Cipher
((
state_t
*
)
buf
,
ctx
->
RoundKey
);
}
void
AES_ECB_decrypt
(
struct
AES_ctx
*
ctx
,
uint8_t
*
buf
)
void
AES_ECB_decrypt
(
const
struct
AES_ctx
*
ctx
,
uint8_t
*
buf
)
{
// The next function call decrypts the PlainText with the Key using AES algorithm.
InvCipher
((
state_t
*
)
buf
,
ctx
->
RoundKey
);
...
...
@@ -488,7 +488,7 @@ void AES_ECB_decrypt(struct AES_ctx* ctx, uint8_t* buf)
#if defined(CBC) && (CBC == 1)
static
void
XorWithIv
(
uint8_t
*
buf
,
uint8_t
*
Iv
)
static
void
XorWithIv
(
uint8_t
*
buf
,
const
uint8_t
*
Iv
)
{
uint8_t
i
;
for
(
i
=
0
;
i
<
AES_BLOCKLEN
;
++
i
)
// The block in AES is always 128bit no matter the key size
...
...
@@ -497,7 +497,7 @@ static void XorWithIv(uint8_t* buf, uint8_t* Iv)
}
}
void
AES_CBC_encrypt_buffer
(
struct
AES_ctx
*
ctx
,
uint8_t
*
buf
,
uint32_t
length
)
void
AES_CBC_encrypt_buffer
(
struct
AES_ctx
*
ctx
,
uint8_t
*
buf
,
uint32_t
length
)
{
uintptr_t
i
;
uint8_t
*
Iv
=
ctx
->
Iv
;
...
...
@@ -552,9 +552,9 @@ void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length)
/* Increment Iv and handle overflow */
for
(
bi
=
(
AES_BLOCKLEN
-
1
);
bi
>=
0
;
--
bi
)
{
/* inc will o
w
erflow */
/* inc will o
v
erflow */
if
(
ctx
->
Iv
[
bi
]
==
255
)
{
{
ctx
->
Iv
[
bi
]
=
0
;
continue
;
}
...
...
aes.h
View file @
4b4b04b8
...
...
@@ -58,8 +58,8 @@ void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv);
// buffer size is exactly AES_BLOCKLEN bytes;
// you need only AES_init_ctx as IV is not used in ECB
// NB: ECB is considered insecure for most uses
void
AES_ECB_encrypt
(
struct
AES_ctx
*
ctx
,
uint8_t
*
buf
);
void
AES_ECB_decrypt
(
struct
AES_ctx
*
ctx
,
uint8_t
*
buf
);
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 == !)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment