| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: safer.h,v 1.3 2004/04/08 01:36:15 mdw Exp $ |
| 4 | * |
| 5 | * The SAFER block cipher |
| 6 | * |
| 7 | * (c) 2001 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of Catacomb. |
| 13 | * |
| 14 | * Catacomb is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU Library General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of the |
| 17 | * License, or (at your option) any later version. |
| 18 | * |
| 19 | * Catacomb is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU Library General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU Library General Public |
| 25 | * License along with Catacomb; if not, write to the Free |
| 26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 27 | * MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | /*----- Notes on the SAFER block cipher -----------------------------------* |
| 31 | * |
| 32 | * SAFER was designed by James Massey (who also worked on IDEA) for Cylink. |
| 33 | * It's free -- patents or other silliness. The original key schedule had |
| 34 | * some weaknesses, and a new one (the SK version) was added. SAFER has a |
| 35 | * variable number of rounds. The standard interface uses the recommended |
| 36 | * number for the given key schedule algorithm and key size. |
| 37 | * |
| 38 | * SAFER got a bad press in Schneier's book `Applied Cryptography'. I think |
| 39 | * this is undeserved. SAFER is a well-designed cipher which mostly looks |
| 40 | * pretty solid. |
| 41 | */ |
| 42 | |
| 43 | #ifndef CATACOMB_SAFER_H |
| 44 | #define CATACOMB_SAFER_H |
| 45 | |
| 46 | #ifdef __cplusplus |
| 47 | extern "C" { |
| 48 | #endif |
| 49 | |
| 50 | /*----- Header files ------------------------------------------------------*/ |
| 51 | |
| 52 | #include <stddef.h> |
| 53 | |
| 54 | #include <mLib/bits.h> |
| 55 | |
| 56 | /*----- Magical numbers ---------------------------------------------------*/ |
| 57 | |
| 58 | #define SAFER_BLKSZ 8 |
| 59 | #define SAFER_KEYSZ 8 |
| 60 | #define SAFER_CLASS (N, B, 64) |
| 61 | |
| 62 | #define SAFERSK_BLKSZ SAFER_BLKSZ |
| 63 | #define SAFERSK_KEYSZ 16 |
| 64 | #define SAFERSK_CLASS SAFER_CLASS |
| 65 | |
| 66 | #define SAFER_MAXROUNDS 12 |
| 67 | |
| 68 | extern const octet safer_keysz[]; |
| 69 | #define safersk_keysz safer_keysz |
| 70 | |
| 71 | /*----- Data structures ---------------------------------------------------*/ |
| 72 | |
| 73 | typedef struct safer_ctx { |
| 74 | octet k[8 * (2 * SAFER_MAXROUNDS + 1)]; |
| 75 | unsigned r; |
| 76 | } safer_ctx, safersk_ctx; |
| 77 | |
| 78 | /*----- Functions provided ------------------------------------------------*/ |
| 79 | |
| 80 | /* --- @safer_setup@ --- * |
| 81 | * |
| 82 | * Arguments: @safer_ctx *k@ = pointer to context to initialize |
| 83 | * @unsigned r@ = number of rounds wanted |
| 84 | * @unsigned f@ = various other flags |
| 85 | * @const void *buf@ = pointer to key material |
| 86 | * @size_t sz@ = size of key material in bytes |
| 87 | * |
| 88 | * Returns: --- |
| 89 | * |
| 90 | * Use: Initializes an SAFER expanded key. A default number of |
| 91 | * rounds is chosen, based on the key length. |
| 92 | */ |
| 93 | |
| 94 | #define SAFER_SK 1u |
| 95 | |
| 96 | extern void safer_setup(safer_ctx */*k*/, unsigned /*r*/, unsigned /*f*/, |
| 97 | const void */*buf*/, size_t /*sz*/); |
| 98 | |
| 99 | /* --- @safer_init@, @safersk_init@ --- * |
| 100 | * |
| 101 | * Arguments: @safer_ctx *k@ = pointer to context to initialize |
| 102 | * @const void *buf@ = pointer to key material |
| 103 | * @size_t sz@ = size of key material in bytes |
| 104 | * |
| 105 | * Returns: --- |
| 106 | * |
| 107 | * Use: Initializes an SAFER expanded key. A default number of |
| 108 | * rounds is chosen, based on the key length. |
| 109 | */ |
| 110 | |
| 111 | extern void safer_init(safer_ctx */*k*/, |
| 112 | const void */*buf*/, size_t /*sz*/); |
| 113 | extern void safersk_init(safer_ctx */*k*/, |
| 114 | const void */*buf*/, size_t /*sz*/); |
| 115 | |
| 116 | /* --- @safer_eblk@, @safer_dblk@ --- * |
| 117 | * |
| 118 | * Arguments: @const safer_ctx *k@ = pointer to SAFER context |
| 119 | * @const uint32 s[2]@ = pointer to source block |
| 120 | * @const uint32 d[2]@ = pointer to destination block |
| 121 | * |
| 122 | * Returns: --- |
| 123 | * |
| 124 | * Use: Low-level block encryption and decryption. |
| 125 | */ |
| 126 | |
| 127 | extern void safer_eblk(const safer_ctx */*k*/, |
| 128 | const uint32 */*s*/, uint32 */*dst*/); |
| 129 | extern void safer_dblk(const safer_ctx */*k*/, |
| 130 | const uint32 */*s*/, uint32 */*dst*/); |
| 131 | |
| 132 | #define safersk_eblk safer_eblk |
| 133 | #define safersk_dblk safer_dblk |
| 134 | |
| 135 | /*----- That's all, folks -------------------------------------------------*/ |
| 136 | |
| 137 | #ifdef __cplusplus |
| 138 | } |
| 139 | #endif |
| 140 | |
| 141 | #endif |