| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * The Rijndael block cipher |
| 4 | * |
| 5 | * (c) 2000 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of Catacomb. |
| 11 | * |
| 12 | * Catacomb is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU Library General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of the |
| 15 | * License, or (at your option) any later version. |
| 16 | * |
| 17 | * Catacomb is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU Library General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Library General Public |
| 23 | * License along with Catacomb; if not, write to the Free |
| 24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 25 | * MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | /*----- Notes on the Rijndael block cipher --------------------------------* |
| 29 | * |
| 30 | * Invented by Joan Daemen and Vincent Rijmen, Rijndael is a fast, elegant |
| 31 | * and relatively simple 128-bit block cipher. It was chosen by NIST to be |
| 32 | * the new Advanced Encryption Standard (AES) algorithm. |
| 33 | * |
| 34 | * Rijnadel appears to have a low security margin. I recommend waiting |
| 35 | * before using Rijndael for any sensitive applications. |
| 36 | */ |
| 37 | |
| 38 | #ifndef CATACOMB_RIJNDAEL_H |
| 39 | #define CATACOMB_RIJNDAEL_H |
| 40 | |
| 41 | #ifdef __cplusplus |
| 42 | extern "C" { |
| 43 | #endif |
| 44 | |
| 45 | /*----- Header files ------------------------------------------------------*/ |
| 46 | |
| 47 | #include <stddef.h> |
| 48 | |
| 49 | #include <mLib/bits.h> |
| 50 | |
| 51 | /*----- Magical numbers ---------------------------------------------------*/ |
| 52 | |
| 53 | #define RIJNDAEL_BLKSZ 16 |
| 54 | #define RIJNDAEL_KEYSZ 32 |
| 55 | #define RIJNDAEL_CLASS (N, B, 128) |
| 56 | |
| 57 | extern const octet rijndael_keysz[]; |
| 58 | |
| 59 | /*----- Data structures ---------------------------------------------------*/ |
| 60 | |
| 61 | #define RIJNDAEL_MAXROUNDS 16 |
| 62 | #define RIJNDAEL_KWORDS ((RIJNDAEL_MAXROUNDS + 1) * 8) |
| 63 | |
| 64 | typedef struct rijndael_ctx { |
| 65 | unsigned nr; |
| 66 | uint32 w[RIJNDAEL_KWORDS]; |
| 67 | uint32 wi[RIJNDAEL_KWORDS]; |
| 68 | } rijndael_ctx; |
| 69 | |
| 70 | /*----- Functions provided ------------------------------------------------*/ |
| 71 | |
| 72 | /* --- @rijndael_setup@ --- * |
| 73 | * |
| 74 | * Arguments: @rijndael_ctx *k@ = pointer to context to initialize |
| 75 | * @unsigned nb@ = number of words in the block |
| 76 | * @const void *buf@ = pointer to buffer of key material |
| 77 | * @size_t sz@ = size of the key material |
| 78 | * |
| 79 | * Returns: --- |
| 80 | * |
| 81 | * Use: Low-level key-scheduling. Don't call this directly. |
| 82 | */ |
| 83 | |
| 84 | extern void rijndael_setup(rijndael_ctx */*k*/, unsigned /*nb*/, |
| 85 | const void */*buf*/, size_t /*sz*/); |
| 86 | |
| 87 | /* --- @rijndael_init@ --- * |
| 88 | * |
| 89 | * Arguments: @rijndael_ctx *k@ = pointer to context to initialize |
| 90 | * @const void *buf@ = pointer to buffer of key material |
| 91 | * @size_t sz@ = size of the key material |
| 92 | * |
| 93 | * Returns: --- |
| 94 | * |
| 95 | * Use: Initializes a Rijndael context with a particular key. This |
| 96 | * implementation of Rijndael doesn't impose any particular |
| 97 | * limits on the key size except that it must be multiple of 4 |
| 98 | * bytes long. 256 bits seems sensible, though. |
| 99 | */ |
| 100 | |
| 101 | extern void rijndael_init(rijndael_ctx */*k*/, |
| 102 | const void */*buf*/, size_t /*sz*/); |
| 103 | |
| 104 | /* --- @rijndael_eblk@, @rijndael_dblk@ --- * |
| 105 | * |
| 106 | * Arguments: @const rijndael_ctx *k@ = pointer to Rijndael context |
| 107 | * @const uint32 s[4]@ = pointer to source block |
| 108 | * @uint32 d[4]@ = pointer to destination block |
| 109 | * |
| 110 | * Returns: --- |
| 111 | * |
| 112 | * Use: Low-level block encryption and decryption. |
| 113 | */ |
| 114 | |
| 115 | extern void rijndael_eblk(const rijndael_ctx */*k*/, |
| 116 | const uint32 */*s*/, uint32 */*dst*/); |
| 117 | extern void rijndael_dblk(const rijndael_ctx */*k*/, |
| 118 | const uint32 */*s*/, uint32 */*dst*/); |
| 119 | |
| 120 | /*----- That's all, folks -------------------------------------------------*/ |
| 121 | |
| 122 | #ifdef __cplusplus |
| 123 | } |
| 124 | #endif |
| 125 | |
| 126 | #endif |