3 * $Id: gcipher.h,v 1.5 2004/04/21 00:37:32 mdw Exp $
5 * Generic symmetric cipher interface
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 #ifndef CATACOMB_GCIPHER_H
31 #define CATACOMB_GCIPHER_H
37 /*----- Header files ------------------------------------------------------*/
41 #include <mLib/bits.h>
43 /*----- Generic symmetric cipher interface --------------------------------*/
45 typedef struct gcipher {
46 const struct gcipher_ops *ops; /* Pointer to cipher operations */
49 typedef struct gcipher_ops {
50 const struct gccipher *c; /* Pointer to cipher class */
51 void (*encrypt)(gcipher */*c*/, const void */*s*/,
52 void */*t*/, size_t /*sz*/);
53 void (*decrypt)(gcipher */*c*/, const void */*s*/,
54 void */*t*/, size_t /*sz*/);
55 void (*destroy)(gcipher */*c*/);
56 void (*setiv)(gcipher */*c*/, const void */*iv*/);
57 void (*bdry)(gcipher */*c*/);
60 typedef struct gccipher {
61 const char *name; /* Cipher name */
62 const octet *keysz; /* Preferred key size table */
63 size_t blksz; /* Block size or zero if none */
64 gcipher *(*init)(const void */*k*/, size_t /*sz*/);
67 #define GC_INIT(cc, k, sz) (cc)->init((k), (sz))
68 #define GC_CLASS(cc) (cc)->ops->c
69 #define GC_ENCRYPT(c, s, t, sz) (c)->ops->encrypt((c), (s), (t), (sz))
70 #define GC_DECRYPT(c, s, t, sz) (c)->ops->decrypt((c), (s), (t), (sz))
71 #define GC_DESTROY(c) (c)->ops->destroy((c))
72 #define GC_SETIV(c, iv) (c)->ops->setiv((c), (iv))
73 #define GC_BDRY(c) (c)->ops->bdry((c))
75 /*----- Key size management -----------------------------------------------*/
77 /* --- Key size type constants --- *
79 * A key size limitation is an array of bytes. The first byte describes the
80 * kind of limitation on the key size %$k$%; the rest are argument bytes
81 * %$a_i$%, for %$i \ge 0$%. In all cases, %$a_0$% is the `recommended' key
84 * * @KSZ_ANY@ means there is no restriction.
86 * * @KSZ_RANGE@ requires that %$k \ge a_1$%, %$k \equiv 0 \pmod{a_3}$%,
87 * and, if %$a_2 \ne 0$%, %$k \le a_2$%.
89 * * @KSZ_SET@ requires that %$k \in {\,a_i\,}$%.
93 KSZ_ANY, /* Allows any key at all */
94 KSZ_RANGE, /* Allows keys within a range */
95 KSZ_SET /* Allows specific sizes of keys */
100 * Arguments: @size_t sz@ = a proposed key size, or zero
101 * @const octet *ksz@ = pointer to key size table
103 * Returns: See below.
105 * Use: Returns a sensible key size. If @sz@ is nonzero, it is
106 * interpreted as an amount (in bytes) of key material which the
107 * caller has available, and the return value is either the
108 * largest allowable key size less than or equal to the caller's
109 * size, or zero if there is no valid key length small enough.
110 * If @sz@ is zero, the function returns a `recommended' key
114 extern size_t keysz(size_t /*sz*/, const octet */*ksz*/);
116 #define KSZ_CHECK(pre, sz) (keysz((sz), pre##_keysz) == (sz))
117 #define KSZ_ASSERT(pre, sz) \
118 assert(((void)"Bad key size for " #pre, KSZ_CHECK(pre, sz)))
120 /*----- Tables ------------------------------------------------------------*/
122 extern const gccipher *const gciphertab[];
124 /* --- @gcipher_byname@ --- *
126 * Arguments: @const char *p@ = pointer to name string
128 * Returns: The named cipher class, or null.
131 extern const gccipher *gcipher_byname(const char */*p*/);
133 /*----- That's all, folks -------------------------------------------------*/