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
7fbaf552
Commit
7fbaf552
authored
Jun 06, 2017
by
Matteo Brichese
Committed by
GitHub
Jun 06, 2017
Browse files
Merge pull request #1 from bricke/aes
Aes
parents
8592c76b
c1c5fb19
Changes
3
Hide whitespace changes
Inline
Side-by-side
aes.c
View file @
7fbaf552
...
...
@@ -42,6 +42,7 @@ NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0)
/*****************************************************************************/
// The number of columns comprising a state in AES. This is a constant in AES. Value=4
#define Nb 4
#define BLOCKLEN 16 //Block length in bytes AES is 128b block only
#ifdef AES256
#define Nk 8
...
...
@@ -177,7 +178,7 @@ static void KeyExpansion(void)
// All other round keys are found from the previous round keys.
//i == Nk
for
(
i
=
Nk
;
i
<
Nb
*
(
Nr
+
1
);
++
i
)
for
(;
i
<
Nb
*
(
Nr
+
1
);
++
i
)
{
{
tempa
[
0
]
=
RoundKey
[(
i
-
1
)
*
4
+
0
];
...
...
@@ -448,16 +449,6 @@ static void InvCipher(void)
AddRoundKey
(
0
);
}
static
void
BlockCopy
(
uint8_t
*
output
,
const
uint8_t
*
input
)
{
uint8_t
i
;
for
(
i
=
0
;
i
<
KEYLEN
;
++
i
)
{
output
[
i
]
=
input
[
i
];
}
}
/*****************************************************************************/
/* Public functions: */
...
...
@@ -465,10 +456,10 @@ static void BlockCopy(uint8_t* output, const uint8_t* input)
#if defined(ECB) && ECB
void
AES_ECB_encrypt
(
const
uint8_t
*
input
,
const
uint8_t
*
key
,
uint8_t
*
output
)
void
AES_ECB_encrypt
(
const
uint8_t
*
input
,
const
uint8_t
*
key
,
uint8_t
*
output
,
const
uint32_t
length
)
{
// Copy input to output, and work in-memory on output
BlockCo
py
(
output
,
input
);
memc
py
(
output
,
input
,
length
);
state
=
(
state_t
*
)
output
;
Key
=
key
;
...
...
@@ -478,10 +469,10 @@ void AES_ECB_encrypt(const uint8_t* input, const uint8_t* key, uint8_t* output)
Cipher
();
}
void
AES_ECB_decrypt
(
const
uint8_t
*
input
,
const
uint8_t
*
key
,
uint8_t
*
output
)
void
AES_ECB_decrypt
(
const
uint8_t
*
input
,
const
uint8_t
*
key
,
uint8_t
*
output
,
const
uint32_t
length
)
{
// Copy input to output, and work in-memory on output
BlockCo
py
(
output
,
input
);
memc
py
(
output
,
input
,
length
);
state
=
(
state_t
*
)
output
;
// The KeyExpansion routine must be called before encryption.
...
...
@@ -504,7 +495,7 @@ void AES_ECB_decrypt(const uint8_t* input, const uint8_t* key, uint8_t *output)
static
void
XorWithIv
(
uint8_t
*
buf
)
{
uint8_t
i
;
for
(
i
=
0
;
i
<
KEYLEN
;
++
i
)
for
(
i
=
0
;
i
<
BLOCKLEN
;
++
i
)
//WAS for(i = 0; i < KEYLEN; ++i) but the block in AES is always 128bit so 16 bytes!
{
buf
[
i
]
^=
Iv
[
i
];
}
...
...
@@ -513,9 +504,9 @@ static void XorWithIv(uint8_t* buf)
void
AES_CBC_encrypt_buffer
(
uint8_t
*
output
,
uint8_t
*
input
,
uint32_t
length
,
const
uint8_t
*
key
,
const
uint8_t
*
iv
)
{
uintptr_t
i
;
uint8_t
remainders
=
length
%
KEY
LEN
;
/* Remaining bytes in the last non-full block */
uint8_t
extra
=
length
%
BLOCK
LEN
;
/* Remaining bytes in the last non-full block */
BlockCo
py
(
output
,
input
);
memc
py
(
output
,
input
,
BLOCKLEN
);
state
=
(
state_t
*
)
output
;
// Skip the key expansion if key is passed as 0
...
...
@@ -530,21 +521,21 @@ void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, co
Iv
=
(
uint8_t
*
)
iv
;
}
for
(
i
=
0
;
i
<
length
;
i
+=
KEY
LEN
)
for
(
i
=
0
;
i
<
length
;
i
+=
BLOCK
LEN
)
{
XorWithIv
(
input
);
BlockCo
py
(
output
,
input
);
memc
py
(
output
,
input
,
BLOCKLEN
);
state
=
(
state_t
*
)
output
;
Cipher
();
Iv
=
output
;
input
+=
KEYLEN
;
output
+=
KEYLEN
;
input
+=
BLOCKLEN
;
output
+=
BLOCKLEN
;
//printf("Step %d - %d", i/16, i);
}
if
(
remainders
)
if
(
extra
)
{
BlockCopy
(
output
,
input
);
memset
(
output
+
remainders
,
0
,
KEYLEN
-
remainders
);
/* add 0-padding */
memcpy
(
output
,
input
,
extra
);
state
=
(
state_t
*
)
output
;
Cipher
();
}
...
...
@@ -553,11 +544,11 @@ void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, co
void
AES_CBC_decrypt_buffer
(
uint8_t
*
output
,
uint8_t
*
input
,
uint32_t
length
,
const
uint8_t
*
key
,
const
uint8_t
*
iv
)
{
uintptr_t
i
;
uint8_t
remainders
=
length
%
KEYLEN
;
/* Remaining bytes in the last non-full block */
BlockCopy
(
output
,
input
);
state
=
(
state_t
*
)
output
;
uint8_t
extra
=
length
%
BLOCKLEN
;
/* Remaining bytes in the last non-full block */
memcpy
(
output
,
input
,
BLOCKLEN
);
state
=
(
state_t
*
)
output
;
// Skip the key expansion if key is passed as 0
if
(
0
!=
key
)
{
...
...
@@ -571,21 +562,20 @@ void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, co
Iv
=
(
uint8_t
*
)
iv
;
}
for
(
i
=
0
;
i
<
length
;
i
+=
KEY
LEN
)
for
(
i
=
0
;
i
<
length
;
i
+=
BLOCK
LEN
)
{
BlockCo
py
(
output
,
input
);
memc
py
(
output
,
input
,
BLOCKLEN
);
state
=
(
state_t
*
)
output
;
InvCipher
();
XorWithIv
(
output
);
Iv
=
input
;
input
+=
KEY
LEN
;
output
+=
KEY
LEN
;
input
+=
BLOCK
LEN
;
output
+=
BLOCK
LEN
;
}
if
(
remainders
)
if
(
extra
)
{
BlockCopy
(
output
,
input
);
memset
(
output
+
remainders
,
0
,
KEYLEN
-
remainders
);
/* add 0-padding */
memcpy
(
output
,
input
,
extra
);
state
=
(
state_t
*
)
output
;
InvCipher
();
}
...
...
aes.h
View file @
7fbaf552
...
...
@@ -6,7 +6,7 @@
// #define the macros below to 1/0 to enable/disable the mode of operation.
//
// CBC enables AES
128
encryption in CBC-mode of operation
and handles 0-padding
.
// CBC enables AES encryption in CBC-mode of operation.
// ECB enables the basic ECB 16-byte block algorithm. Both can be enabled simultaneously.
// The #ifndef-guard allows it to be configured before #include'ing or at compile time.
...
...
@@ -20,11 +20,10 @@
#define AES128
#if defined(ECB) && ECB
void
AES_ECB_encrypt
(
const
uint8_t
*
input
,
const
uint8_t
*
key
,
uint8_t
*
output
);
void
AES_ECB_decrypt
(
const
uint8_t
*
input
,
const
uint8_t
*
key
,
uint8_t
*
output
);
void
AES_ECB_encrypt
(
const
uint8_t
*
input
,
const
uint8_t
*
key
,
uint8_t
*
output
,
const
uint32_t
length
);
void
AES_ECB_decrypt
(
const
uint8_t
*
input
,
const
uint8_t
*
key
,
uint8_t
*
output
,
const
uint32_t
length
);
#endif // #if defined(ECB) && ECB
...
...
test.c
View file @
7fbaf552
...
...
@@ -9,6 +9,7 @@
#include "aes.h"
static
void
phex
(
uint8_t
*
str
);
static
void
test_encrypt_ecb
(
void
);
static
void
test_decrypt_ecb
(
void
);
...
...
@@ -17,9 +18,20 @@ static void test_encrypt_cbc(void);
static
void
test_decrypt_cbc
(
void
);
int
main
(
void
)
{
#ifdef AES128
printf
(
"
\n
Testing AES128
\n\n
"
);
#elif defined(AES192)
printf
(
"
\n
Testing AES192
\n\n
"
);
#elif defined(AES256)
printf
(
"
\n
Testing AES256
\n\n
"
);
#else
printf
(
"You need to specify a symbol between AES128, AES192 or AES256. Exiting"
);
return
0
;
#endif
test_encrypt_cbc
();
test_decrypt_cbc
();
test_decrypt_ecb
();
...
...
@@ -30,12 +42,20 @@ int main(void)
}
// prints string as hex
static
void
phex
(
uint8_t
*
str
)
{
#ifdef AES128
uint8_t
len
=
16
;
#elif defined(AES192)
uint8_t
len
=
24
;
#elif defined(AES256)
uint8_t
len
=
32
;
#endif
unsigned
char
i
;
for
(
i
=
0
;
i
<
16
;
++
i
)
for
(
i
=
0
;
i
<
len
;
++
i
)
printf
(
"%.2x"
,
str
[
i
]);
printf
(
"
\n
"
);
}
...
...
@@ -83,14 +103,25 @@ static void test_encrypt_ecb_verbose(void)
static
void
test_encrypt_ecb
(
void
)
{
uint8_t
key
[]
=
{
0x2b
,
0x7e
,
0x15
,
0x16
,
0x28
,
0xae
,
0xd2
,
0xa6
,
0xab
,
0xf7
,
0x15
,
0x88
,
0x09
,
0xcf
,
0x4f
,
0x3c
};
#ifdef AES128
uint8_t
key
[]
=
{
0x2b
,
0x7e
,
0x15
,
0x16
,
0x28
,
0xae
,
0xd2
,
0xa6
,
0xab
,
0xf7
,
0x15
,
0x88
,
0x09
,
0xcf
,
0x4f
,
0x3c
};
uint8_t
out
[]
=
{
0x3a
,
0xd7
,
0x7b
,
0xb4
,
0x0d
,
0x7a
,
0x36
,
0x60
,
0xa8
,
0x9e
,
0xca
,
0xf3
,
0x24
,
0x66
,
0xef
,
0x97
};
#elif defined(AES192)
uint8_t
key
[]
=
{
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
out
[]
=
{
0xbd
,
0x33
,
0x4f
,
0x1d
,
0x6e
,
0x45
,
0xf2
,
0x5f
,
0xf7
,
0x12
,
0xa2
,
0x14
,
0x57
,
0x1f
,
0xa5
,
0xcc
};
#elif defined(AES256)
uint8_t
key
[]
=
{
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
out
[]
=
{
0xf3
,
0xee
,
0xd1
,
0xbd
,
0xb5
,
0xd2
,
0xa0
,
0x3c
,
0x06
,
0x4b
,
0x5a
,
0x7e
,
0x3d
,
0xb1
,
0x81
,
0xf8
};
#endif
uint8_t
in
[]
=
{
0x6b
,
0xc1
,
0xbe
,
0xe2
,
0x2e
,
0x40
,
0x9f
,
0x96
,
0xe9
,
0x3d
,
0x7e
,
0x11
,
0x73
,
0x93
,
0x17
,
0x2a
};
uint8_t
out
[]
=
{
0x3a
,
0xd7
,
0x7b
,
0xb4
,
0x0d
,
0x7a
,
0x36
,
0x60
,
0xa8
,
0x9e
,
0xca
,
0xf3
,
0x24
,
0x66
,
0xef
,
0x97
};
uint8_t
buffer
[
16
];
AES_ECB_encrypt
(
in
,
key
,
buffer
,
16
);
printf
(
"ECB
d
ecrypt: "
);
printf
(
"ECB e
n
crypt: "
);
if
(
0
==
memcmp
((
char
*
)
out
,
(
char
*
)
buffer
,
16
))
{
...
...
@@ -104,24 +135,36 @@ static void test_encrypt_ecb(void)
static
void
test_decrypt_cbc
(
void
)
{
// Example "simulating" a smaller buffer...
#ifdef AES128
uint8_t
key
[]
=
{
0x2b
,
0x7e
,
0x15
,
0x16
,
0x28
,
0xae
,
0xd2
,
0xa6
,
0xab
,
0xf7
,
0x15
,
0x88
,
0x09
,
0xcf
,
0x4f
,
0x3c
};
uint8_t
iv
[]
=
{
0x00
,
0x01
,
0x02
,
0x03
,
0x04
,
0x05
,
0x06
,
0x07
,
0x08
,
0x09
,
0x0a
,
0x0b
,
0x0c
,
0x0d
,
0x0e
,
0x0f
};
uint8_t
in
[]
=
{
0x76
,
0x49
,
0xab
,
0xac
,
0x81
,
0x19
,
0xb2
,
0x46
,
0xce
,
0xe9
,
0x8e
,
0x9b
,
0x12
,
0xe9
,
0x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x61
,
0xb2
,
0xeb
,
0x05
,
0xe2
,
0xc3
,
0x9b
,
0xe9
,
0xfc
,
0xda
,
0x6c
,
0x19
,
0x07
,
0x8c
,
0x6a
,
0x9d
,
0x1b
};
#endif
uint8_t
iv
[]
=
{
0x00
,
0x01
,
0x02
,
0x03
,
0x04
,
0x05
,
0x06
,
0x07
,
0x08
,
0x09
,
0x0a
,
0x0b
,
0x0c
,
0x0d
,
0x0e
,
0x0f
};
uint8_t
out
[]
=
{
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
};
uint8_t
buffer
[
64
];
uint8_t
buffer2
[
64
];
AES_CBC_decrypt_buffer
(
buffer
+
0
,
in
+
0
,
16
,
key
,
iv
);
AES_CBC_decrypt_buffer
(
buffer
+
16
,
in
+
16
,
16
,
0
,
0
);
AES_CBC_decrypt_buffer
(
buffer
+
32
,
in
+
32
,
16
,
0
,
0
);
AES_CBC_decrypt_buffer
(
buffer
+
48
,
in
+
48
,
16
,
0
,
0
);
AES_CBC_decrypt_buffer
(
buffer
,
in
,
64
,
key
,
iv
);
printf
(
"CBC decrypt: "
);
...
...
@@ -137,16 +180,32 @@ static void test_decrypt_cbc(void)
static
void
test_encrypt_cbc
(
void
)
{
#ifdef AES128
uint8_t
key
[]
=
{
0x2b
,
0x7e
,
0x15
,
0x16
,
0x28
,
0xae
,
0xd2
,
0xa6
,
0xab
,
0xf7
,
0x15
,
0x88
,
0x09
,
0xcf
,
0x4f
,
0x3c
};
uint8_t
out
[]
=
{
0x76
,
0x49
,
0xab
,
0xac
,
0x81
,
0x19
,
0xb2
,
0x46
,
0xce
,
0xe9
,
0x8e
,
0x9b
,
0x12
,
0xe9
,
0x19
,
0x7d
,
0x50
,
0x86
,
0xcb
,
0x9b
,
0x50
,
0x72
,
0x19
,
0xee
,
0x95
,
0xdb
,
0x11
,
0x3a
,
0x91
,
0x76
,
0x78
,
0xb2
,
0x73
,
0xbe
,
0xd6
,
0xb8
,
0xe3
,
0xc1
,
0x74
,
0x3b
,
0x71
,
0x16
,
0xe6
,
0x9e
,
0x22
,
0x22
,
0x95
,
0x16
,
0x3f
,
0xf1
,
0xca
,
0xa1
,
0x68
,
0x1f
,
0xac
,
0x09
,
0x12
,
0x0e
,
0xca
,
0x30
,
0x75
,
0x86
,
0xe1
,
0xa7
};
#elif defined(AES192)
uint8_t
key
[]
=
{
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
out
[]
=
{
0x4f
,
0x02
,
0x1d
,
0xb2
,
0x43
,
0xbc
,
0x63
,
0x3d
,
0x71
,
0x78
,
0x18
,
0x3a
,
0x9f
,
0xa0
,
0x71
,
0xe8
,
0xb4
,
0xd9
,
0xad
,
0xa9
,
0xad
,
0x7d
,
0xed
,
0xf4
,
0xe5
,
0xe7
,
0x38
,
0x76
,
0x3f
,
0x69
,
0x14
,
0x5a
,
0x57
,
0x1b
,
0x24
,
0x20
,
0x12
,
0xfb
,
0x7a
,
0xe0
,
0x7f
,
0xa9
,
0xba
,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
,
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
};
uint8_t
out
[]
=
{
0x76
,
0x49
,
0xab
,
0xac
,
0x81
,
0x19
,
0xb2
,
0x46
,
0xce
,
0xe9
,
0x8e
,
0x9b
,
0x12
,
0xe9
,
0x19
,
0x7d
,
0x50
,
0x86
,
0xcb
,
0x9b
,
0x50
,
0x72
,
0x19
,
0xee
,
0x95
,
0xdb
,
0x11
,
0x3a
,
0x91
,
0x76
,
0x78
,
0xb2
,
0x73
,
0xbe
,
0xd6
,
0xb8
,
0xe3
,
0xc1
,
0x74
,
0x3b
,
0x71
,
0x16
,
0xe6
,
0x9e
,
0x22
,
0x22
,
0x95
,
0x16
,
0x3f
,
0xf1
,
0xca
,
0xa1
,
0x68
,
0x1f
,
0xac
,
0x09
,
0x12
,
0x0e
,
0xca
,
0x30
,
0x75
,
0x86
,
0xe1
,
0xa7
};
uint8_t
buffer
[
64
];
AES_CBC_encrypt_buffer
(
buffer
,
in
,
64
,
key
,
iv
);
...
...
@@ -166,9 +225,20 @@ static void test_encrypt_cbc(void)
static
void
test_decrypt_ecb
(
void
)
{
uint8_t
key
[]
=
{
0x2b
,
0x7e
,
0x15
,
0x16
,
0x28
,
0xae
,
0xd2
,
0xa6
,
0xab
,
0xf7
,
0x15
,
0x88
,
0x09
,
0xcf
,
0x4f
,
0x3c
};
uint8_t
in
[]
=
{
0x3a
,
0xd7
,
0x7b
,
0xb4
,
0x0d
,
0x7a
,
0x36
,
0x60
,
0xa8
,
0x9e
,
0xca
,
0xf3
,
0x24
,
0x66
,
0xef
,
0x97
};
uint8_t
out
[]
=
{
0x6b
,
0xc1
,
0xbe
,
0xe2
,
0x2e
,
0x40
,
0x9f
,
0x96
,
0xe9
,
0x3d
,
0x7e
,
0x11
,
0x73
,
0x93
,
0x17
,
0x2a
};
#ifdef AES128
uint8_t
key
[]
=
{
0x2b
,
0x7e
,
0x15
,
0x16
,
0x28
,
0xae
,
0xd2
,
0xa6
,
0xab
,
0xf7
,
0x15
,
0x88
,
0x09
,
0xcf
,
0x4f
,
0x3c
};
uint8_t
in
[]
=
{
0x3a
,
0xd7
,
0x7b
,
0xb4
,
0x0d
,
0x7a
,
0x36
,
0x60
,
0xa8
,
0x9e
,
0xca
,
0xf3
,
0x24
,
0x66
,
0xef
,
0x97
};
#elif defined(AES192)
uint8_t
key
[]
=
{
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
[]
=
{
0xbd
,
0x33
,
0x4f
,
0x1d
,
0x6e
,
0x45
,
0xf2
,
0x5f
,
0xf7
,
0x12
,
0xa2
,
0x14
,
0x57
,
0x1f
,
0xa5
,
0xcc
};
#elif defined(AES256)
uint8_t
key
[]
=
{
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
[]
=
{
0xf3
,
0xee
,
0xd1
,
0xbd
,
0xb5
,
0xd2
,
0xa0
,
0x3c
,
0x06
,
0x4b
,
0x5a
,
0x7e
,
0x3d
,
0xb1
,
0x81
,
0xf8
};
#endif
uint8_t
out
[]
=
{
0x6b
,
0xc1
,
0xbe
,
0xe2
,
0x2e
,
0x40
,
0x9f
,
0x96
,
0xe9
,
0x3d
,
0x7e
,
0x11
,
0x73
,
0x93
,
0x17
,
0x2a
};
uint8_t
buffer
[
16
];
AES_ECB_decrypt
(
in
,
key
,
buffer
,
16
);
...
...
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