5 * (c) 2007 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 #ifndef CATACOMB_KEYSZ_H
29 #define CATACOMB_KEYSZ_H
35 /*----- Header files ------------------------------------------------------*/
39 #include <mLib/bits.h>
41 /*----- Data structures ---------------------------------------------------*/
43 /* --- Key size type constants --- *
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
50 * * @KSZ_ANY@ means there is no restriction.
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$%.
55 * * @KSZ_SET@ requires that %$k \in {\,a_i\,}$%.
58 #define KSZ_OPMASK 0x1f /* Kinds of keysize specs */
60 KSZ_ANY
, /* Allows any key at all */
61 KSZ_RANGE
, /* Allows keys within a range */
62 KSZ_SET
/* Allows specific sizes of keys */
65 #define KSZ_16BIT 0x20 /* Arguments are 16 bits long */
67 /*----- Key sizes for symmetric algorithms --------------------------------*/
71 * Arguments: @size_t sz@ = a proposed key size, or zero
72 * @const octet *ksz@ = pointer to key size table
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
85 extern size_t keysz(size_t /*sz*/, const octet */
*ksz*/
);
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)))
91 /* --- @keysz_pad@ --- *
93 * Arguments: @size_t sz@ = a proposed key size
94 * @const octet *ksz@ = pointer to key size table
96 * Returns: A key size, at least as large as @sz@, or zero if no such
100 extern size_t keysz_pad(size_t /*sz*/, const octet */
*ksz*/
);
102 /*----- Key size conversions ----------------------------------------------*/
104 /* --- @keysz_fromdl@, @_fromschnorr@, @_fromif@, @_fromec@ --- *
106 * Arguments: @double nbits@ = key size
108 * Returns: Equivalent symmetric key size.
110 * Use: Converts key lengths of various kinds of reference problems
111 * to (roughly) equivalent symmetric key sizes.
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.
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}$%
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.
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.
133 * These functions take and return @double@ rather than an
134 * integer type in order to preserve precision between
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*/);
143 /* --- @keysz_todl@, @_toschnorr@, @_toif@, @_toec@ --- *
145 * Arguments: @unsigned long nbits@ = symmetric key size
147 * Returns: Equivalent key size.
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.
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*/);
159 /*----- That's all, folks -------------------------------------------------*/