Skip to content

Commit a4baac5

Browse files
Dr. Maxie Dion SchmidtDr. Maxie Dion Schmidt
authored andcommitted
Stashing work on AES-192 and AES256 software-based encryption I
1 parent 8403d8d commit a4baac5

8 files changed

Lines changed: 3274 additions & 2701 deletions

Firmware/Chameleon-Mini/Application/CryptoAESExtended.c

Lines changed: 505 additions & 0 deletions
Large diffs are not rendered by default.

Firmware/Chameleon-Mini/Application/CryptoAESExtended.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,57 @@ This notice must be retained at the top of all source files where indicated.
3131
* https://github.com/kokke/tiny-AES-c
3232
*/
3333

34+
#ifdef ENABLE_DESFIRE_AES_EXTENDED
35+
36+
#ifndef __CRYPTO_AES_EXTENDED_SW_H__
37+
#define __CRYPTO_AES_EXTENDED_SW_H__
38+
39+
#define _CRYPTO_TYPE_AES192 (0x8A)
40+
#define _CRYPTO_TYPE_AES256 (0xDA)
41+
42+
#define AES_BLOCKLEN (16)
43+
#define CRYPTO_AES_MAX_KEY_EXPSIZE (240)
44+
45+
typedef struct {
46+
int Nk; // The number of 32 bit words in a key.
47+
int Nr; // The number of rounds in AES Cipher.
48+
int KeySize;
49+
int KeyExpSize;
50+
uint8_t *RoundKey;
51+
uint8_t *Iv;
52+
} AES_ctx;
53+
54+
extern uint8_t AESCryptoType;
55+
extern uint8_t Nk;
56+
extern uint8_t Nr;
57+
extern uint8_t AES_KEYLEN;
58+
extern uint8_t AES_keyExpSize;
59+
extern uint8_t RoundKey[CRYPTO_AES_MAX_KEY_EXPSIZE];
60+
extern uint8_t IV[AES_BLOCKLEN];
61+
extern const uint8_t SBox[256];
62+
extern const uint8_t RSBox[256];
63+
extern const uint8_t RCon[11];
64+
65+
void SetupLocalAESContext(AES_ctx *ctx, uint8_t cryptoType);
66+
67+
void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key);
68+
void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv);
69+
void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv);
70+
71+
// Buffer size is exactly AES_BLOCKLEN bytes.
72+
// You need only AES_init_ctx as IV is not used in ECB.
73+
// NB: ECB is considered insecure for most uses.
74+
void AES_ECB_encrypt(const struct AES_ctx* ctx, uint8_t* buf);
75+
void AES_ECB_decrypt(const struct AES_ctx* ctx, uint8_t* buf);
76+
77+
// Buffer size MUST be mutile of AES_BLOCKLEN.
78+
// See https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme.
79+
// NOTES: you need to set IV in ctx via AES_init_ctx_iv() or AES_ctx_set_iv()
80+
// no IV should ever be reused with the same key
81+
void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
82+
void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
83+
84+
#endif
85+
86+
#endif
3487

Firmware/Chameleon-Mini/Application/DESFire/DESFireCrypto.h

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ This notice must be retained at the top of all source files where indicated.
2929

3030
#include "../../Common.h"
3131
#include "../CryptoAES128.h"
32+
#include "../CryptoAESExtended.h"
33+
#include "../CryptoTDEA.h"
34+
#include "../CryptoCMAC.h"
3235

3336
#include "DESFireFirmwareSettings.h"
3437

@@ -44,6 +47,8 @@ This notice must be retained at the top of all source files where indicated.
4447
#define DESFIRE_COMMS_PLAINTEXT_MAC (0x01)
4548
#define DESFIRE_COMMS_CIPHERTEXT_DES (0x03)
4649
#define DESFIRE_COMMS_CIPHERTEXT_AES128 (0x04)
50+
#define DESFIRE_COMMS_CIPHERTEXT_AES192 (0x05)
51+
#define DESFIRE_COMMS_CIPHERTEXT_AES256 (0x06)
4752
#define DESFIRE_DEFAULT_COMMS_STANDARD (DESFIRE_COMMS_PLAINTEXT)
4853

4954
extern BYTE DesfireCommMode;
@@ -53,6 +58,8 @@ extern BYTE DesfireCommMode;
5358
#define CRYPTO_TYPE_2KTDEA (0x0A)
5459
#define CRYPTO_TYPE_3K3DES (0x1A)
5560
#define CRYPTO_TYPE_AES128 (0x4A)
61+
#define CRYPTO_TYPE_AES192 (0x8A)
62+
#define CRYPTO_TYPE_AES256 (0xDA)
5663

5764
#define CryptoTypeDES(ct) \
5865
((ct == CRYPTO_TYPE_DES) || (ct == CRYPTO_TYPE_ANY))
@@ -61,10 +68,17 @@ extern BYTE DesfireCommMode;
6168
#define CryptoType3KTDEA(ct) \
6269
((ct == CRYPTO_TYPE_3K3DES) || (ct == CRYPTO_TYPE_ANY))
6370
#define CryptoTypeAES(ct) \
64-
((ct == CRYPTO_TYPE_AES128) || (ct == CRYPTO_TYPE_ANY))
71+
((ct == CRYPTO_TYPE_AES128) || \
72+
(ct == CRYPTO_TYPE_AES192) || \
73+
(ct == CRYPTO_TYPE_AES256) || \
74+
(ct == CRYPTO_TYPE_ANY))
6575

6676
/* Key sizes, block sizes (in bytes): */
67-
#define CRYPTO_MAX_KEY_SIZE (24)
77+
#ifdef ENABLE_DESFIRE_AES_EXTENDED
78+
#define CRYPTO_MAX_KEY_SIZE (32)
79+
#else
80+
#define CRYPTO_MAX_KEY_SIZE (24)
81+
#endif
6882
#define CRYPTO_MAX_BLOCK_SIZE (16)
6983
#define DESFIRE_AES_IV_SIZE (CRYPTO_AES_BLOCK_SIZE)
7084
#define CRYPTO_CHALLENGE_RESPONSE_BYTES (16)
@@ -105,7 +119,9 @@ typedef enum DESFIRE_FIRMWARE_ENUM_PACKING {
105119
DESFIRE_AUTH_LEGACY,
106120
DESFIRE_AUTH_ISO_2KTDEA,
107121
DESFIRE_AUTH_ISO_3KTDEA,
108-
DESFIRE_AUTH_AES,
122+
DESFIRE_AUTH_AES128,
123+
DESFIRE_AUTH_AES192,
124+
DESFIRE_AUTH_AES256
109125
} DesfireAuthType;
110126

111127
BYTE GetCryptoKeyTypeFromAuthenticateMethod(BYTE authCmdMethod);
@@ -117,8 +133,6 @@ BYTE GetCryptoKeyTypeFromAuthenticateMethod(BYTE authCmdMethod);
117133
* AES (128) crypto routines:
118134
*********************************************************/
119135

120-
#include "../CryptoAES128.h"
121-
122136
extern CryptoAESConfig_t AESCryptoContext;
123137

124138
void InitAESCryptoKeyData(void);
@@ -134,9 +148,6 @@ typedef uint8_t (*CryptoTransferReceiveFunc)(uint8_t *, uint8_t);
134148
* TripleDES crypto routines:
135149
*********************************************************/
136150

137-
#include "../CryptoTDEA.h"
138-
#include "../CryptoCMAC.h"
139-
140151
#define DESFIRE_2KTDEA_NONCE_SIZE (CRYPTO_DES_BLOCK_SIZE)
141152
#define DESFIRE_DES_IV_SIZE (CRYPTO_DES_BLOCK_SIZE)
142153
#define DESFIRE_MAX_PAYLOAD_TDEA_BLOCKS (DESFIRE_MAX_PAYLOAD_SIZE / CRYPTO_DES_BLOCK_SIZE)
0 Bytes
Binary file not shown.

Firmware/Chameleon-Mini/Chameleon-Mini-CustomBuild_DESFire.eep

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
:1000000000004B2D07070808000004014100900183
1+
:100000000000572A0707080800000401410090017A
22
:1000100000000000000000000000000000000000E0
33
:1000200000000000000000000000000000000000D0
44
:1000300000000000000000000000000000000000C0
32 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)