0 aes
2 blowfish
1 twofish
pt: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
keysize for aes = 32
ENC ct <- pt
ct: c3 27 1e f0 98 e9 54 0b fd d1 a1 76 ff 12 ba 6c 25 04 d6 88 50 d2 88 88 02 1a 19 ab 99 e1 b7 8a 7a 6f 71 f9 98 4f d9 54 f2 5e 5e 8e 03 56 0e 42 bd ec ab 0b 61 db 63 1c 18 49 d8 e0 08 2e dc fa 0f 27 2e ee d4 29 46 24 3a a3 c8 a4 8a 52 33 2f
keysize for aes = 32
DEC ot <- ct
ot: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
#include <tomcrypt.h>
#include <stdio.h>
typedef unsigned char u8;
void
dump_(u8 *data, size_t size)
{
for (int i = 0; i < size; i++)
printf(" %02x", data[i]);
putchar('\n');
}
#define dump(data, size) do { \
fputs(#data ":", stdout); \
dump_(data, size); } while (0);
void
enc_dec(u8 *out, u8 *in, size_t size, int enc)
{
u8 key[80] = { 1, 2, 3 };
u8 iv[80] = { 0 };
symmetric_CBC sm;
int keysize = sizeof key;
int err = aes_keysize(&keysize);
if (err != CRYPT_OK) {
printf("ERR %s() %s\n", __func__, error_to_string(err));
return; }
printf("keysize for aes = %d\n", keysize);
int c = find_cipher("aes");
err = cbc_start(c, iv, key, keysize, 0, &sm);
if (err != CRYPT_OK) {
printf("ERR %s() %s\n", __func__, error_to_string(err));
return; }
(enc ? cbc_encrypt : cbc_decrypt)(in, out, size, &sm);
cbc_done(&sm);
zeromem(key, sizeof key);
zeromem(&sm, sizeof sm);
}
void
enc(u8 *out, u8 *in, size_t size)
{
enc_dec(out, in, size, /*enc=*/1);
}
void
dec(u8 *out, u8 *in, size_t size)
{
enc_dec(out, in, size, /*enc=*/0);
}
int
main(void)
{
u8 pt[80] = { 0 }; /* plain text */
u8 ct[80] = { 0 }; /* cypher text */
u8 ot[80] = { 0 }; /* output text */
const char *cname[] = { "aes", "blowfish", "twofish", NULL };
if (register_cipher(&aes_desc) == -1) return 1;
if (register_cipher(&twofish_desc) == -1) return 1;
if (register_cipher(&blowfish_desc) == -1) return 1;
for (const char **i = cname; *i; i++) {
int c = find_cipher(*i);
printf("%3d %s\n", c, *i); }
for (int i = 0; i < sizeof pt; i++)
pt[i] = i & 0xff;
dump(pt, sizeof pt);
enc(ct, pt, sizeof pt); /* ct <- pt */
puts("ENC ct <- pt");
// dump(pt, sizeof pt);
dump(ct, sizeof ct);
dec(ot, ct, sizeof ct); /* ot <- ct */
puts("DEC ot <- ct");
// dump(ct, sizeof ct);
dump(ot, sizeof ot);
return 0;
}