chiark / gitweb /
symm/ccm.c: Fix the title of the comment for `ccm_check'.
[catacomb] / base / 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 #define KSZ_OPMASK 0x1f                 /* Kinds of keysize specs */
59 enum {
60   KSZ_ANY,                              /* Allows any key at all */
61   KSZ_RANGE,                            /* Allows keys within a range */
62   KSZ_SET                               /* Allows specific sizes of keys */
63 };
64
65 #define KSZ_16BIT 0x20                  /* Arguments are 16 bits long */
66
67 /*----- Key sizes for symmetric algorithms --------------------------------*/
68
69 /* --- @keysz@ --- *
70  *
71  * Arguments:   @size_t sz@ = a proposed key size, or zero
72  *              @const octet *ksz@ = pointer to key size table
73  *
74  * Returns:     See below.
75  *
76  * Use:         Returns a sensible key size.  If @sz@ is nonzero, it is
77  *              interpreted as an amount (in bytes) of key material which the
78  *              caller has available, and the return value is either the
79  *              largest allowable key size less than or equal to the caller's
80  *              size, or zero if there is no valid key length small enough.
81  *              If @sz@ is zero, the function returns a `recommended' key
82  *              size.
83  */
84
85 extern size_t keysz(size_t /*sz*/, const octet */*ksz*/);
86
87 #define KSZ_CHECK(pre, sz) (keysz((sz), pre##_keysz) == (sz))
88 #define KSZ_ASSERT(pre, sz)                                             \
89   assert(((void)"Bad key size for " #pre, KSZ_CHECK(pre, sz)))
90
91 /* --- @keysz_pad@ --- *
92  *
93  * Arguments:   @size_t sz@ = a proposed key size
94  *              @const octet *ksz@ = pointer to key size table
95  *
96  * Returns:     A key size, at least as large as @sz@, or zero if no such
97  *              size is available.
98  */
99
100 extern size_t keysz_pad(size_t /*sz*/, const octet */*ksz*/);
101
102 /*----- Key size conversions ----------------------------------------------*/
103
104 /* --- @keysz_fromdl@, @_fromschnorr@, @_fromif@, @_fromec@ --- *
105  *
106  * Arguments:   @double nbits@ = key size
107  *
108  * Returns:     Equivalent symmetric key size.
109  *
110  * Use:         Converts key lengths of various kinds of reference problems
111  *              to (roughly) equivalent symmetric key sizes.
112  *
113  *                * Given the bit length of %$p$%, @keysz_fromdl@ returns a
114  *                  key size representing the difficulty of computing
115  *                  discrete logarithms in %$\gf{p}$%, for %$p$% prime or a
116  *                  small power of a prime.
117  *
118  *                * Given the bit length of %$r$%, @keysz_fromschnorr@
119  *                  returns a key size representing the difficulty of
120  *                  computing discrete logarithms in a subgroup of %$\gf{q}$%
121  *                  of order %$r$%.
122  *
123  *                * Given the bit length of %$n$%, @keysz_fromif@ returns a
124  *                  key size representing the difficulty of factoring a
125  *                  `hard' number %$n = p q$%, where %$p$% and %$q$% are
126  *                  primes of (near enough) the same length.
127  *
128  *                * Given the bit length of %$r$%, @keysz_fromec@ returns a
129  *                  key size representing the difficulty of computing
130  *                  discrete logarithms in a subgroup of order-%$r$% of an
131  *                  elliptic curve over a finite field.
132  *
133  *              These functions take and return @double@ rather than an
134  *              integer type in order to preserve precision between
135  *              conversions.
136  */
137
138 extern double keysz_fromdl(double /*nbits*/);
139 extern double keysz_fromschnorr(double /*nbits*/);
140 extern double keysz_fromif(double /*nbits*/);
141 extern double keysz_fromec(double /*nbits*/);
142
143 /* --- @keysz_todl@, @_toschnorr@, @_toif@, @_toec@ --- *
144  *
145  * Arguments:   @unsigned long nbits@ = symmetric key size
146  *
147  * Returns:     Equivalent key size.
148  *
149  * Use:         Converts symmetric key sizes to (roughly) equivalent key
150  *              sizes for various kinds of reference problems.  These are the
151  *              approximate inverses of the functions above.
152  */
153
154 extern double keysz_todl(double /*nbits*/);
155 extern double keysz_toschnorr(double /*nbits*/);
156 extern double keysz_toif(double /*nbits*/);
157 extern double keysz_toec(double /*nbits*/);
158
159 /*----- That's all, folks -------------------------------------------------*/
160
161 #ifdef __cplusplus
162   }
163 #endif
164
165 #endif