chiark / gitweb /
keysz.h: Extract key-size stuff into a separate header file.
[catacomb] / keysz.h
1 /* -*-c-*-
2  *
3  * Key size management
4  *
5  * (c) 2007 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
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.
16  *
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.
21  *
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
28 #ifndef CATACOMB_KEYSZ_H
29 #define CATACOMB_KEYSZ_H
30
31 #ifdef __cplusplus
32   extern "C" {
33 #endif
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #include <stddef.h>
38
39 #include <mLib/bits.h>
40
41 /*----- Data structures ---------------------------------------------------*/
42
43 /* --- Key size type constants --- *
44  *
45  * A key size limitation is an array of bytes.  The first byte describes the
46  * kind of limitation on the key size %$k$%; the rest are argument bytes
47  * %$a_i$%, for %$i \ge 0$%.  In all cases, %$a_0$% is the `recommended' key
48  * size.
49  *
50  *   * @KSZ_ANY@ means there is no restriction.
51  *
52  *   * @KSZ_RANGE@ requires that %$k \ge a_1$%, %$k \equiv 0 \pmod{a_3}$%,
53  *     and, if %$a_2 \ne 0$%, %$k \le a_2$%.
54  *
55  *   * @KSZ_SET@ requires that %$k \in {\,a_i\,}$%.
56  */
57
58 enum {
59   KSZ_ANY,                              /* Allows any key at all */
60   KSZ_RANGE,                            /* Allows keys within a range */
61   KSZ_SET                               /* Allows specific sizes of keys */
62 };
63
64 /*----- Key sizes for symmetric algorithms --------------------------------*/
65
66 /* --- @keysz@ --- *
67  *
68  * Arguments:   @size_t sz@ = a proposed key size, or zero
69  *              @const octet *ksz@ = pointer to key size table
70  *
71  * Returns:     See below.
72  *
73  * Use:         Returns a sensible key size.  If @sz@ is nonzero, it is
74  *              interpreted as an amount (in bytes) of key material which the
75  *              caller has available, and the return value is either the
76  *              largest allowable key size less than or equal to the caller's
77  *              size, or zero if there is no valid key length small enough.
78  *              If @sz@ is zero, the function returns a `recommended' key
79  *              size.
80  */
81
82 extern size_t keysz(size_t /*sz*/, const octet */*ksz*/);
83
84 #define KSZ_CHECK(pre, sz) (keysz((sz), pre##_keysz) == (sz))
85 #define KSZ_ASSERT(pre, sz)                                             \
86   assert(((void)"Bad key size for " #pre, KSZ_CHECK(pre, sz)))
87
88 /*----- That's all, folks -------------------------------------------------*/
89
90 #ifdef __cplusplus
91   }
92 #endif
93
94 #endif