7809fc3d |
1 | /* -*-c-*- |
7809fc3d |
2 | * |
3 | * General block cipher utilities |
4 | * |
5 | * (c) 2000 Straylight/Edgeware |
6 | */ |
7 | |
45c0fd36 |
8 | /*----- Licensing notice --------------------------------------------------* |
7809fc3d |
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. |
45c0fd36 |
16 | * |
7809fc3d |
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. |
45c0fd36 |
21 | * |
7809fc3d |
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 | |
7809fc3d |
28 | /*----- Header files ------------------------------------------------------*/ |
29 | |
30 | #include <assert.h> |
31 | |
32 | #include "gcipher.h" |
33 | |
34 | /*----- Main code ---------------------------------------------------------*/ |
35 | |
36 | /* --- @keysz@ --- * |
37 | * |
38 | * Arguments: @size_t sz@ = a proposed key size, or zero |
39 | * @const octet *ksz@ = pointer to key size table |
40 | * |
41 | * Returns: See below. |
42 | * |
43 | * Use: Returns a sensible key size. If @sz@ is nonzero, it is |
44 | * interpreted as an amount (in bytes) of key material which the |
45 | * caller has available, and the return value is either the |
46 | * largest allowable key size less than or equal to the caller's |
47 | * size, or zero if there is no valid key length small enough. |
48 | * If @sz@ is zero, the function returns a `recommended' key |
49 | * size. |
50 | */ |
51 | |
52 | size_t keysz(size_t sz, const octet *ksz) |
53 | { |
54 | if (sz == 0) |
55 | return (ksz[1]); |
56 | else switch (ksz[0]) { |
57 | case KSZ_ANY: |
58 | return (sz); |
59 | case KSZ_RANGE: |
60 | if (ksz[4]) |
61 | sz -= sz % ksz[4]; |
62 | if (sz < ksz[2]) |
63 | return (0); |
64 | if (ksz[3] && sz > ksz[3]) |
65 | return (ksz[3]); |
66 | return (sz); |
67 | case KSZ_SET: { |
68 | unsigned q = 0; |
69 | for (ksz++; *ksz; ksz++) { |
70 | if (sz >= *ksz && q < *ksz) |
71 | q = *ksz; |
72 | } |
73 | return (q); |
74 | } |
75 | } |
76 | |
77 | assert(((void)"bad key size table", 0)); |
78 | return (0); |
79 | } |
80 | |
81 | /*----- That's all, folks -------------------------------------------------*/ |