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