/* * Highly portable ANSI C implementation of Rijndael cipher. * Written by Kaz Kylheku * Copyright 2010 * * License: * 1. Please retain attribution and copyright notice in source code. * 2. No restrictions on compiled code. */ #ifndef RIJNDAEL_H #define RIJNDAEL_H #include #ifdef __cplusplus extern "C" { #endif #define RIJN_MAX_ROUNDS 14 /* * Constants representing choice of Rijndael key or block size, * either of which can be 128 bits, 192 bits or 256 bits. */ typedef enum { rijn_128 = 4, /* number of rows */ rijn_192 = 6, rijn_256 = 8 } rijn_size_t; /* * Rijndael parameters. */ typedef struct { rijn_size_t rijn_blockrows; rijn_size_t rijn_keyrows; } rijn_param_t; #define RIJN_PARAM_DEFAULT_INITIALIZER { rijn_128, rijn_256 } #define RIJN_PARAM_INITIALIZER(BLOCKSZ, KEYSZ) { (BLOCKSZ), (KEYSZ) } void rijn_param_init(rijn_param_t *, rijn_size_t blocksz, rijn_size_t keysz); /* * Rijndael keys are 128, 192 or 256 bits wide. There are up to * RIJN_MAX_ROUNDS + 1 round keys needed, so the schedule is that big. */ typedef unsigned char rijn_unit_t[4]; typedef rijn_unit_t rijn_key_t[8]; typedef rijn_unit_t rijn_block_t[8]; typedef unsigned char rijn_flatblock_t[sizeof (rijn_block_t)]; typedef struct { rijn_param_t rijn_param; int rijn_nrounds; rijn_block_t rijn_roundkey[RIJN_MAX_ROUNDS+1]; } rijn_keysched_t; void rijn_sched_key(rijn_keysched_t *, rijn_key_t *, const rijn_param_t *); void rijn_encrypt(rijn_keysched_t *, unsigned char *, const unsigned char *); void rijn_decrypt(rijn_keysched_t *, unsigned char *, const unsigned char *); void rijn_cbc_encrypt(rijn_keysched_t *, unsigned char *iv, unsigned char *out, const unsigned char *in, size_t nblocks); void rijn_cbc_decrypt(rijn_keysched_t *, unsigned char *iv, unsigned char *out, const unsigned char *in, size_t nblocks); void rijn_cfb_encrypt(rijn_keysched_t *, unsigned char *iv, unsigned char *out, const unsigned char *pt, size_t nbytes); void rijn_cfb_decrypt(rijn_keysched_t *, unsigned char *iv, unsigned char *out, const unsigned char *in, size_t nbytes); #ifdef __cplusplus } #endif #endif