621468d8 |
1 | /* -*-c-*- |
621468d8 |
2 | * |
3 | * The SAFER block cipher |
4 | * |
5 | * (c) 2001 Straylight/Edgeware |
6 | */ |
7 | |
45c0fd36 |
8 | /*----- Licensing notice --------------------------------------------------* |
621468d8 |
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. |
45c0fd36 |
16 | * |
621468d8 |
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. |
45c0fd36 |
21 | * |
621468d8 |
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 | |
5233862a |
28 | /*----- Notes on the SAFER block cipher -----------------------------------* |
29 | * |
30 | * SAFER was designed by James Massey (who also worked on IDEA) for Cylink. |
31 | * It's free -- patents or other silliness. The original key schedule had |
32 | * some weaknesses, and a new one (the SK version) was added. SAFER has a |
33 | * variable number of rounds. The standard interface uses the recommended |
34 | * number for the given key schedule algorithm and key size. |
35 | * |
36 | * SAFER got a bad press in Schneier's book `Applied Cryptography'. I think |
37 | * this is undeserved. SAFER is a well-designed cipher which mostly looks |
38 | * pretty solid. |
39 | */ |
40 | |
621468d8 |
41 | #ifndef CATACOMB_SAFER_H |
42 | #define CATACOMB_SAFER_H |
43 | |
44 | #ifdef __cplusplus |
45 | extern "C" { |
46 | #endif |
47 | |
48 | /*----- Header files ------------------------------------------------------*/ |
49 | |
50 | #include <stddef.h> |
51 | |
52 | #include <mLib/bits.h> |
53 | |
54 | /*----- Magical numbers ---------------------------------------------------*/ |
55 | |
56 | #define SAFER_BLKSZ 8 |
57 | #define SAFER_KEYSZ 8 |
58 | #define SAFER_CLASS (N, B, 64) |
59 | |
60 | #define SAFERSK_BLKSZ SAFER_BLKSZ |
61 | #define SAFERSK_KEYSZ 16 |
62 | #define SAFERSK_CLASS SAFER_CLASS |
63 | |
64 | #define SAFER_MAXROUNDS 12 |
65 | |
66 | extern const octet safer_keysz[]; |
67 | #define safersk_keysz safer_keysz |
68 | |
69 | /*----- Data structures ---------------------------------------------------*/ |
70 | |
71 | typedef struct safer_ctx { |
72 | octet k[8 * (2 * SAFER_MAXROUNDS + 1)]; |
73 | unsigned r; |
74 | } safer_ctx, safersk_ctx; |
75 | |
76 | /*----- Functions provided ------------------------------------------------*/ |
77 | |
78 | /* --- @safer_setup@ --- * |
79 | * |
80 | * Arguments: @safer_ctx *k@ = pointer to context to initialize |
81 | * @unsigned r@ = number of rounds wanted |
82 | * @unsigned f@ = various other flags |
83 | * @const void *buf@ = pointer to key material |
84 | * @size_t sz@ = size of key material in bytes |
85 | * |
86 | * Returns: --- |
87 | * |
88 | * Use: Initializes an SAFER expanded key. A default number of |
89 | * rounds is chosen, based on the key length. |
90 | */ |
91 | |
92 | #define SAFER_SK 1u |
93 | |
94 | extern void safer_setup(safer_ctx */*k*/, unsigned /*r*/, unsigned /*f*/, |
95 | const void */*buf*/, size_t /*sz*/); |
96 | |
97 | /* --- @safer_init@, @safersk_init@ --- * |
98 | * |
99 | * Arguments: @safer_ctx *k@ = pointer to context to initialize |
100 | * @const void *buf@ = pointer to key material |
101 | * @size_t sz@ = size of key material in bytes |
102 | * |
103 | * Returns: --- |
104 | * |
105 | * Use: Initializes an SAFER expanded key. A default number of |
106 | * rounds is chosen, based on the key length. |
107 | */ |
108 | |
109 | extern void safer_init(safer_ctx */*k*/, |
110 | const void */*buf*/, size_t /*sz*/); |
111 | extern void safersk_init(safer_ctx */*k*/, |
112 | const void */*buf*/, size_t /*sz*/); |
113 | |
114 | /* --- @safer_eblk@, @safer_dblk@ --- * |
115 | * |
116 | * Arguments: @const safer_ctx *k@ = pointer to SAFER context |
117 | * @const uint32 s[2]@ = pointer to source block |
118 | * @const uint32 d[2]@ = pointer to destination block |
119 | * |
120 | * Returns: --- |
121 | * |
122 | * Use: Low-level block encryption and decryption. |
123 | */ |
124 | |
125 | extern void safer_eblk(const safer_ctx */*k*/, |
126 | const uint32 */*s*/, uint32 */*dst*/); |
127 | extern void safer_dblk(const safer_ctx */*k*/, |
128 | const uint32 */*s*/, uint32 */*dst*/); |
129 | |
130 | #define safersk_eblk safer_eblk |
131 | #define safersk_dblk safer_dblk |
132 | |
133 | /*----- That's all, folks -------------------------------------------------*/ |
134 | |
135 | #ifdef __cplusplus |
136 | } |
137 | #endif |
138 | |
139 | #endif |