| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: gcipher.h,v 1.2 2000/06/17 10:56:00 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.2 2000/06/17 10:56:00 mdw |
| 34 | * New key size interface. |
| 35 | * |
| 36 | * Revision 1.1 1999/12/10 23:16:01 mdw |
| 37 | * Generic interface. |
| 38 | * |
| 39 | */ |
| 40 | |
| 41 | #ifndef CATACOMB_GCIPHER_H |
| 42 | #define CATACOMB_GCIPHER_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 | /*----- Generic symmetric cipher interface --------------------------------*/ |
| 55 | |
| 56 | typedef struct gcipher { |
| 57 | const struct gcipher_ops *ops; /* Pointer to cipher operations */ |
| 58 | } gcipher; |
| 59 | |
| 60 | typedef struct gcipher_ops { |
| 61 | const struct gccipher *c; /* Pointer to cipher class */ |
| 62 | void (*encrypt)(gcipher */*c*/, const void */*s*/, |
| 63 | void */*t*/, size_t /*sz*/); |
| 64 | void (*decrypt)(gcipher */*c*/, const void */*s*/, |
| 65 | void */*t*/, size_t /*sz*/); |
| 66 | void (*destroy)(gcipher */*c*/); |
| 67 | void (*setiv)(gcipher */*c*/, const void */*iv*/); |
| 68 | void (*bdry)(gcipher */*c*/); |
| 69 | } gcipher_ops; |
| 70 | |
| 71 | typedef struct gccipher { |
| 72 | const char *name; /* Cipher name */ |
| 73 | const octet *keysz; /* Preferred key size table */ |
| 74 | size_t blksz; /* Block size or zero if none */ |
| 75 | gcipher *(*init)(const void */*k*/, size_t /*sz*/); |
| 76 | } gccipher; |
| 77 | |
| 78 | /*----- Key size management -----------------------------------------------*/ |
| 79 | |
| 80 | /* --- Key size type constants --- * |
| 81 | * |
| 82 | * A key size limitation is an array of bytes. The first byte describes the |
| 83 | * kind of limitation on the key size %$k$%; the rest are argument bytes |
| 84 | * %$a_i$%, for %$i \ge 0$%. In all cases, %$a_0$% is the `recommended' key |
| 85 | * size. |
| 86 | * |
| 87 | * * @KSZ_ANY@ means there is no restriction. |
| 88 | * |
| 89 | * * @KSZ_RANGE@ requires that %$k \ge a_1$%, %$k \equiv 0 \pmod{a_3}$%, |
| 90 | * and, if %$a_2 \ne 0$%, %$k \le a_2$%. |
| 91 | * |
| 92 | * * @KSZ_SET@ requires that %$k \in {\,a_i\,}$%. |
| 93 | */ |
| 94 | |
| 95 | enum { |
| 96 | KSZ_ANY, /* Allows any key at all */ |
| 97 | KSZ_RANGE, /* Allows keys within a range */ |
| 98 | KSZ_SET /* Allows specific sizes of keys */ |
| 99 | }; |
| 100 | |
| 101 | /* --- @keysz@ --- * |
| 102 | * |
| 103 | * Arguments: @size_t sz@ = a proposed key size, or zero |
| 104 | * @const octet *ksz@ = pointer to key size table |
| 105 | * |
| 106 | * Returns: See below. |
| 107 | * |
| 108 | * Use: Returns a sensible key size. If @sz@ is nonzero, it is |
| 109 | * interpreted as an amount (in bytes) of key material which the |
| 110 | * caller has available, and the return value is either the |
| 111 | * largest allowable key size less than or equal to the caller's |
| 112 | * size, or zero if there is no valid key length small enough. |
| 113 | * If @sz@ is zero, the function returns a `recommended' key |
| 114 | * size. |
| 115 | */ |
| 116 | |
| 117 | extern size_t keysz(size_t /*sz*/, const octet */*ksz*/); |
| 118 | |
| 119 | #define KSZ_CHECK(pre, sz) (keysz((sz), pre##_keysz) == (sz)) |
| 120 | #define KSZ_ASSERT(pre, sz) \ |
| 121 | assert(((void)"Bad key size for " #pre, KSZ_CHECK(pre, sz))) |
| 122 | |
| 123 | /*----- That's all, folks -------------------------------------------------*/ |
| 124 | |
| 125 | #ifdef __cplusplus |
| 126 | } |
| 127 | #endif |
| 128 | |
| 129 | #endif |