3 * General block cipher utilities
5 * (c) 2000 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 /*----- Header files ------------------------------------------------------*/
34 /*----- Main code ---------------------------------------------------------*/
38 * Arguments: @size_t sz@ = a proposed key size, or zero
39 * @const octet *ksz@ = pointer to key size table
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
52 size_t keysz(size_t sz
, const octet
*ksz
)
54 unsigned op
= ksz
[0]&KSZ_OPMASK
;
55 unsigned wd
= (ksz
[0]&KSZ_16BIT
) ?
2 : 1;
59 #define ARG(i) (wd == 1 ? ksz[i] : LOAD16(ksz + 2*i))
63 case KSZ_ANY
: return (sz
);
65 t
= ARG(1); u
= ARG(2); v
= ARG(3);
67 if (sz
< t
) return (0);
68 if (u
&& sz
> u
) return (u
);
73 t
= ARG(0); ksz
+= wd
; if (!t
) break;
74 if (sz
>= t
&& u
< t
) u
= t
;
80 assert(((void)"bad key size table", 0));
84 /*----- That's all, folks -------------------------------------------------*/