mirror of
https://github.com/sheumann/65816-crypto.git
synced 2024-11-26 02:49:19 +00:00
Rename some interfaces.
This commit is contained in:
parent
14fe68de79
commit
d9b8467bb7
18
aes.asm
18
aes.asm
@ -193,21 +193,21 @@ keysize_256 gequ 128
|
|||||||
|
|
||||||
|
|
||||||
* Callable from C, with state structure pointer on stack.
|
* Callable from C, with state structure pointer on stack.
|
||||||
aes_expandkey128 start
|
aes128_expandkey start
|
||||||
CFunction ExpandKey128
|
CFunction AES128_EXPANDKEY
|
||||||
end
|
end
|
||||||
|
|
||||||
aes_expandkey192 start
|
aes192_expandkey start
|
||||||
CFunction ExpandKey192
|
CFunction AES192_EXPANDKEY
|
||||||
end
|
end
|
||||||
|
|
||||||
aes_expandkey256 start
|
aes256_expandkey start
|
||||||
CFunction ExpandKey256
|
CFunction AES256_EXPANDKEY
|
||||||
end
|
end
|
||||||
|
|
||||||
* Call with DP = AES state structure (with key expanded),
|
* Call with DP = AES state structure (with key expanded),
|
||||||
* DB = bank containing AES tables.
|
* DB = bank containing AES tables.
|
||||||
ExpandKey128 start
|
AES128_EXPANDKEY start
|
||||||
using tables
|
using tables
|
||||||
|
|
||||||
stz keysize-1 ;keysize_128
|
stz keysize-1 ;keysize_128
|
||||||
@ -228,7 +228,7 @@ top anop
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
ExpandKey192 start
|
AES192_EXPANDKEY start
|
||||||
using tables
|
using tables
|
||||||
|
|
||||||
lda #keysize_192|8
|
lda #keysize_192|8
|
||||||
@ -250,7 +250,7 @@ top anop
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
ExpandKey256 start
|
AES256_EXPANDKEY start
|
||||||
using tables
|
using tables
|
||||||
|
|
||||||
lda #keysize_256|8
|
lda #keysize_256|8
|
||||||
|
18
aes.h
18
aes.h
@ -1,17 +1,17 @@
|
|||||||
struct aes_state {
|
struct aes_context {
|
||||||
unsigned char data[16];
|
unsigned char data[16];
|
||||||
unsigned char reserved1[17];
|
unsigned char reserved1[17];
|
||||||
unsigned char key[32];
|
unsigned char key[32];
|
||||||
unsigned char reserved2[16*13];
|
unsigned char reserved2[16*13];
|
||||||
};
|
};
|
||||||
|
|
||||||
/* state must be in bank 0, preferably page-aligned. */
|
/* context must be in bank 0, preferably page-aligned. */
|
||||||
void aes_expandkey128(struct aes_state *state);
|
void aes128_expandkey(struct aes_context *);
|
||||||
void aes_expandkey192(struct aes_state *state);
|
void aes128_expandkey(struct aes_context *);
|
||||||
void aes_expandkey256(struct aes_state *state);
|
void aes128_expandkey(struct aes_context *);
|
||||||
|
|
||||||
void aes_encrypt(struct aes_state *state);
|
void aes_encrypt(struct aes_context *);
|
||||||
|
|
||||||
void aes128_decrypt(struct aes_state *state);
|
void aes128_decrypt(struct aes_context *);
|
||||||
void aes192_decrypt(struct aes_state *state);
|
void aes192_decrypt(struct aes_context *);
|
||||||
void aes256_decrypt(struct aes_state *state);
|
void aes256_decrypt(struct aes_context *);
|
||||||
|
72
aestest.c
72
aestest.c
@ -17,91 +17,91 @@ void print_hexbytes(char *prefix, unsigned char *data, unsigned int n) {
|
|||||||
|
|
||||||
void aes128_test(void) {
|
void aes128_test(void) {
|
||||||
int i;
|
int i;
|
||||||
struct aes_state aes_state = {
|
struct aes_context aes_context = {
|
||||||
{0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff},
|
{0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff},
|
||||||
{0},
|
{0},
|
||||||
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}
|
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}
|
||||||
};
|
};
|
||||||
|
|
||||||
print_hexbytes("Input: ", aes_state.data, 16);
|
print_hexbytes("Input: ", aes_context.data, 16);
|
||||||
print_hexbytes("Key: ", aes_state.key, 16);
|
print_hexbytes("Key: ", aes_context.key, 16);
|
||||||
|
|
||||||
aes_expandkey128(&aes_state);
|
aes128_expandkey(&aes_context);
|
||||||
|
|
||||||
#ifdef PRINT_ROUND_KEYS
|
#ifdef PRINT_ROUND_KEYS
|
||||||
for (i = 1; i <= 10; i++) {
|
for (i = 1; i <= 10; i++) {
|
||||||
printf("Round key %2i: ", i);
|
printf("Round key %2i: ", i);
|
||||||
print_hexbytes("", aes_state.key + i*16, 16);
|
print_hexbytes("", aes_context.key + i*16, 16);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
aes_encrypt(&aes_state);
|
aes_encrypt(&aes_context);
|
||||||
|
|
||||||
print_hexbytes("Output: ", aes_state.data, 16);
|
print_hexbytes("Output: ", aes_context.data, 16);
|
||||||
|
|
||||||
aes128_decrypt(&aes_state);
|
aes128_decrypt(&aes_context);
|
||||||
|
|
||||||
print_hexbytes("Decrypted: ", aes_state.data, 16);
|
print_hexbytes("Decrypted: ", aes_context.data, 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
void aes192_test(void) {
|
void aes192_test(void) {
|
||||||
int i;
|
int i;
|
||||||
struct aes_state aes_state = {
|
struct aes_context aes_context = {
|
||||||
{0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff},
|
{0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff},
|
||||||
{0},
|
{0},
|
||||||
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
|
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
|
||||||
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17}
|
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17}
|
||||||
};
|
};
|
||||||
|
|
||||||
print_hexbytes("Input: ", aes_state.data, 16);
|
print_hexbytes("Input: ", aes_context.data, 16);
|
||||||
print_hexbytes("Key: ", aes_state.key, 24);
|
print_hexbytes("Key: ", aes_context.key, 24);
|
||||||
|
|
||||||
aes_expandkey192(&aes_state);
|
aes192_expandkey(&aes_context);
|
||||||
|
|
||||||
#ifdef PRINT_ROUND_KEYS
|
#ifdef PRINT_ROUND_KEYS
|
||||||
for (i = 1; i <= 12; i++) {
|
for (i = 1; i <= 12; i++) {
|
||||||
printf("Round key %2i: ", i);
|
printf("Round key %2i: ", i);
|
||||||
print_hexbytes("", aes_state.key + i*16, 16);
|
print_hexbytes("", aes_context.key + i*16, 16);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
aes_encrypt(&aes_state);
|
aes_encrypt(&aes_context);
|
||||||
|
|
||||||
print_hexbytes("Output: ", aes_state.data, 16);
|
print_hexbytes("Output: ", aes_context.data, 16);
|
||||||
|
|
||||||
aes192_decrypt(&aes_state);
|
aes192_decrypt(&aes_context);
|
||||||
|
|
||||||
print_hexbytes("Decrypted: ", aes_state.data, 16);
|
print_hexbytes("Decrypted: ", aes_context.data, 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
void aes256_test(void) {
|
void aes256_test(void) {
|
||||||
int i;
|
int i;
|
||||||
struct aes_state aes_state = {
|
struct aes_context aes_context = {
|
||||||
{0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff},
|
{0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff},
|
||||||
{0},
|
{0},
|
||||||
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
|
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
|
||||||
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f}
|
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f}
|
||||||
};
|
};
|
||||||
|
|
||||||
print_hexbytes("Input: ", aes_state.data, 16);
|
print_hexbytes("Input: ", aes_context.data, 16);
|
||||||
print_hexbytes("Key: ", aes_state.key, 32);
|
print_hexbytes("Key: ", aes_context.key, 32);
|
||||||
|
|
||||||
aes_expandkey256(&aes_state);
|
aes256_expandkey(&aes_context);
|
||||||
|
|
||||||
#ifdef PRINT_ROUND_KEYS
|
#ifdef PRINT_ROUND_KEYS
|
||||||
for (i = 1; i <= 14; i++) {
|
for (i = 1; i <= 14; i++) {
|
||||||
printf("Round key %2i: ", i);
|
printf("Round key %2i: ", i);
|
||||||
print_hexbytes("", aes_state.key + i*16, 16);
|
print_hexbytes("", aes_context.key + i*16, 16);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
aes_encrypt(&aes_state);
|
aes_encrypt(&aes_context);
|
||||||
|
|
||||||
print_hexbytes("Output: ", aes_state.data, 16);
|
print_hexbytes("Output: ", aes_context.data, 16);
|
||||||
|
|
||||||
aes256_decrypt(&aes_state);
|
aes256_decrypt(&aes_context);
|
||||||
|
|
||||||
print_hexbytes("Decrypted: ", aes_state.data, 16);
|
print_hexbytes("Decrypted: ", aes_context.data, 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long aes128_time_test(unsigned int iters) {
|
unsigned long aes128_time_test(unsigned int iters) {
|
||||||
@ -109,25 +109,25 @@ unsigned long aes128_time_test(unsigned int iters) {
|
|||||||
unsigned long tick_count;
|
unsigned long tick_count;
|
||||||
long double bytes_per_sec;
|
long double bytes_per_sec;
|
||||||
|
|
||||||
struct aes_state *aes_state, **aes_state_hndl;
|
struct aes_context *aes_context, **aes_context_hndl;
|
||||||
static struct aes_state aes_state_init = {
|
static struct aes_context aes_context_init = {
|
||||||
{0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff},
|
{0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff},
|
||||||
{0},
|
{0},
|
||||||
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}
|
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}
|
||||||
};
|
};
|
||||||
|
|
||||||
aes_state_hndl = (struct aes_state **)NewHandle(sizeof(struct aes_state),
|
aes_context_hndl = (struct aes_context **)NewHandle(sizeof(struct aes_context),
|
||||||
userid(), attrFixed|attrPage|attrBank|attrNoCross, 0x000000);
|
userid(), attrFixed|attrPage|attrBank|attrNoCross, 0x000000);
|
||||||
if (toolerror())
|
if (toolerror())
|
||||||
return 0;
|
return 0;
|
||||||
aes_state = *aes_state_hndl;
|
aes_context = *aes_context_hndl;
|
||||||
*aes_state = aes_state_init;
|
*aes_context = aes_context_init;
|
||||||
|
|
||||||
aes_expandkey128(aes_state);
|
aes128_expandkey(aes_context);
|
||||||
|
|
||||||
tick_count = GetTick();
|
tick_count = GetTick();
|
||||||
for (i = 0; i < iters; i++) {
|
for (i = 0; i < iters; i++) {
|
||||||
aes_encrypt(aes_state);
|
aes_encrypt(aes_context);
|
||||||
}
|
}
|
||||||
tick_count = GetTick() - tick_count;
|
tick_count = GetTick() - tick_count;
|
||||||
|
|
||||||
@ -137,7 +137,7 @@ unsigned long aes128_time_test(unsigned int iters) {
|
|||||||
|
|
||||||
tick_count = GetTick();
|
tick_count = GetTick();
|
||||||
for (i = 0; i < iters; i++) {
|
for (i = 0; i < iters; i++) {
|
||||||
aes128_decrypt(aes_state);
|
aes128_decrypt(aes_context);
|
||||||
}
|
}
|
||||||
tick_count = GetTick() - tick_count;
|
tick_count = GetTick() - tick_count;
|
||||||
|
|
||||||
@ -145,7 +145,7 @@ unsigned long aes128_time_test(unsigned int iters) {
|
|||||||
printf("Decryption: %u iterations takes %lu ticks (%lf bytes/sec)\n",
|
printf("Decryption: %u iterations takes %lu ticks (%lf bytes/sec)\n",
|
||||||
iters, tick_count, bytes_per_sec);
|
iters, tick_count, bytes_per_sec);
|
||||||
|
|
||||||
print_hexbytes("Decrypted: ", aes_state->data, 16);
|
print_hexbytes("Decrypted: ", aes_context->data, 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
Loading…
Reference in New Issue
Block a user