chiark / gitweb /
rand/rand.c: Mix the pool key in `rand_gate' and `rand_stretch'.
[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 /*----- Key size conversions ----------------------------------------------*/
92
93 /* --- @keysz_fromdl@, @_fromschnorr@, @_fromif@, @_fromec@ --- *
94  *
95  * Arguments:   @double nbits@ = key size
96  *
97  * Returns:     Equivalent symmetric key size.
98  *
99  * Use:         Converts key lengths of various kinds of reference problems
100  *              to (roughly) equivalent symmetric key sizes.
101  *
102  *                * Given the bit length of %$p$%, @keysz_fromdl@ returns a
103  *                  key size representing the difficulty of computing
104  *                  discrete logarithms in %$\gf{p}$%, for %$p$% prime or a
105  *                  small power of a prime.
106  *
107  *                * Given the bit length of %$r$%, @keysz_fromschnorr@
108  *                  returns a key size representing the difficulty of
109  *                  computing discrete logarithms in a subgroup of %$\gf{q}$%
110  *                  of order %$r$%.
111  *
112  *                * Given the bit length of %$n$%, @keysz_fromif@ returns a
113  *                  key size representing the difficulty of factoring a
114  *                  `hard' number %$n = p q$%, where %$p$% and %$q$% are
115  *                  primes of (near enough) the same length.
116  *
117  *                * Given the bit length of %$r$%, @keysz_fromec@ returns a
118  *                  key size representing the difficulty of computing
119  *                  discrete logarithms in a subgroup of order-%$r$% of an
120  *                  elliptic curve over a finite field.
121  *
122  *              These functions take and return @double@ rather than an
123  *              integer type in order to preserve precision between
124  *              conversions.
125  */
126
127 extern double keysz_fromdl(double /*nbits*/);
128 extern double keysz_fromschnorr(double /*nbits*/);
129 extern double keysz_fromif(double /*nbits*/);
130 extern double keysz_fromec(double /*nbits*/);
131
132 /* --- @keysz_todl@, @_toschnorr@, @_toif@, @_toec@ --- *
133  *
134  * Arguments:   @unsigned long nbits@ = symmetric key size
135  *
136  * Returns:     Equivalent key size.
137  *
138  * Use:         Converts symmetric key sizes to (roughly) equivalent key
139  *              sizes for various kinds of reference problems.  These are the
140  *              approximate inverses of the functions above.
141  */
142
143 extern double keysz_todl(double /*nbits*/);
144 extern double keysz_toschnorr(double /*nbits*/);
145 extern double keysz_toif(double /*nbits*/);
146 extern double keysz_toec(double /*nbits*/);
147
148 /*----- That's all, folks -------------------------------------------------*/
149
150 #ifdef __cplusplus
151   }
152 #endif
153
154 #endif