Make tables of standard encryption schemes etc.
[u/mdw/catacomb] / gcipher.h
1 /* -*-c-*-
2 *
3 * $Id: gcipher.h,v 1.3 2004/04/04 19:42:30 mdw Exp $
4 *
5 * Generic symmetric cipher interface
6 *
7 * (c) 1999 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 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: gcipher.h,v $
33 * Revision 1.3 2004/04/04 19:42:30 mdw
34 * Make tables of standard encryption schemes etc.
35 *
36 * Revision 1.2 2000/06/17 10:56:00 mdw
37 * New key size interface.
38 *
39 * Revision 1.1 1999/12/10 23:16:01 mdw
40 * Generic interface.
41 *
42 */
43
44 #ifndef CATACOMB_GCIPHER_H
45 #define CATACOMB_GCIPHER_H
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 /*----- Header files ------------------------------------------------------*/
52
53 #include <stddef.h>
54
55 #include <mLib/bits.h>
56
57 /*----- Generic symmetric cipher interface --------------------------------*/
58
59 typedef struct gcipher {
60 const struct gcipher_ops *ops; /* Pointer to cipher operations */
61 } gcipher;
62
63 typedef struct gcipher_ops {
64 const struct gccipher *c; /* Pointer to cipher class */
65 void (*encrypt)(gcipher */*c*/, const void */*s*/,
66 void */*t*/, size_t /*sz*/);
67 void (*decrypt)(gcipher */*c*/, const void */*s*/,
68 void */*t*/, size_t /*sz*/);
69 void (*destroy)(gcipher */*c*/);
70 void (*setiv)(gcipher */*c*/, const void */*iv*/);
71 void (*bdry)(gcipher */*c*/);
72 } gcipher_ops;
73
74 typedef struct gccipher {
75 const char *name; /* Cipher name */
76 const octet *keysz; /* Preferred key size table */
77 size_t blksz; /* Block size or zero if none */
78 gcipher *(*init)(const void */*k*/, size_t /*sz*/);
79 } gccipher;
80
81 #define GC_INIT(cc, k, sz) (cc)->init((k), (sz))
82 #define GC_CLASS(c) (c)->ops->c
83 #define GC_ENCRYPT(c, s, t, sz) (c)->ops->encrypt((c), (s), (t), (sz))
84 #define GC_DECRYPT(c, s, t, sz) (c)->ops->decrypt((c), (s), (t), (sz))
85 #define GC_DESTROY(c) (c)->ops->destroy((c))
86 #define GC_SETIV(c, iv) (c)->ops->setiv((c), (iv))
87 #define GC_BDRY(c) (c)->ops->bdry((c))
88
89 /*----- Key size management -----------------------------------------------*/
90
91 /* --- Key size type constants --- *
92 *
93 * A key size limitation is an array of bytes. The first byte describes the
94 * kind of limitation on the key size %$k$%; the rest are argument bytes
95 * %$a_i$%, for %$i \ge 0$%. In all cases, %$a_0$% is the `recommended' key
96 * size.
97 *
98 * * @KSZ_ANY@ means there is no restriction.
99 *
100 * * @KSZ_RANGE@ requires that %$k \ge a_1$%, %$k \equiv 0 \pmod{a_3}$%,
101 * and, if %$a_2 \ne 0$%, %$k \le a_2$%.
102 *
103 * * @KSZ_SET@ requires that %$k \in {\,a_i\,}$%.
104 */
105
106 enum {
107 KSZ_ANY, /* Allows any key at all */
108 KSZ_RANGE, /* Allows keys within a range */
109 KSZ_SET /* Allows specific sizes of keys */
110 };
111
112 /* --- @keysz@ --- *
113 *
114 * Arguments: @size_t sz@ = a proposed key size, or zero
115 * @const octet *ksz@ = pointer to key size table
116 *
117 * Returns: See below.
118 *
119 * Use: Returns a sensible key size. If @sz@ is nonzero, it is
120 * interpreted as an amount (in bytes) of key material which the
121 * caller has available, and the return value is either the
122 * largest allowable key size less than or equal to the caller's
123 * size, or zero if there is no valid key length small enough.
124 * If @sz@ is zero, the function returns a `recommended' key
125 * size.
126 */
127
128 extern size_t keysz(size_t /*sz*/, const octet */*ksz*/);
129
130 #define KSZ_CHECK(pre, sz) (keysz((sz), pre##_keysz) == (sz))
131 #define KSZ_ASSERT(pre, sz) \
132 assert(((void)"Bad key size for " #pre, KSZ_CHECK(pre, sz)))
133
134 /*----- Tables ------------------------------------------------------------*/
135
136 extern const gccipher *const gciphertab[];
137
138 /* --- @gcipher_byname@ --- *
139 *
140 * Arguments: @const char *p@ = pointer to name string
141 *
142 * Returns: The named cipher class, or null.
143 */
144
145 extern const gccipher *gcipher_byname(const char */*p*/);
146
147 /*----- That's all, folks -------------------------------------------------*/
148
149 #ifdef __cplusplus
150 }
151 #endif
152
153 #endif