progs/perftest.c: Use from Glibc syscall numbers.
[catacomb] / base / keysz.c
CommitLineData
7809fc3d 1/* -*-c-*-
2 *
7809fc3d 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
52size_t keysz(size_t sz, const octet *ksz)
53{
c77f9bb9
MW
54 unsigned op = ksz[0]&KSZ_OPMASK;
55 unsigned wd = (ksz[0]&KSZ_16BIT) ? 2 : 1;
56 unsigned t, u, v;
57
58 ksz++;
0588f9b2 59#define ARG(i) (wd == 1 ? ksz[i] : LOAD16(ksz + 2*i))
7809fc3d 60 if (sz == 0)
c77f9bb9
MW
61 return (ARG(0));
62 else switch (op) {
63 case KSZ_ANY: return (sz);
7809fc3d 64 case KSZ_RANGE:
c77f9bb9
MW
65 t = ARG(1); u = ARG(2); v = ARG(3);
66 if (v) sz -= sz%v;
67 if (sz < t) return (0);
68 if (u && sz > u) return (u);
7809fc3d 69 return (sz);
c77f9bb9
MW
70 case KSZ_SET:
71 u = 0;
72 for (;;) {
73 t = ARG(0); ksz += wd; if (!t) break;
74 if (sz >= t && u < t) u = t;
7809fc3d 75 }
c77f9bb9 76 return (u);
7809fc3d 77 }
c77f9bb9 78#undef ARG
7809fc3d 79
80 assert(((void)"bad key size table", 0));
81 return (0);
82}
83
778b9e26
MW
84/* --- @keysz_pad@ --- *
85 *
86 * Arguments: @size_t sz@ = a proposed key size
87 * @const octet *ksz@ = pointer to key size table
88 *
89 * Returns: A key size, at least as large as @sz@, or zero if no such
90 * size is available.
91 */
92
93size_t keysz_pad(size_t sz, const octet *ksz)
94{
95 unsigned op = ksz[0]&KSZ_OPMASK;
96 unsigned wd = (ksz[0]&KSZ_16BIT) ? 2 : 1;
97 unsigned t, u, v;
98
99 ksz++;
100#define ARG(i) (wd == 1 ? ksz[i] : LOAD16(ksz + 2*i))
101 switch (op) {
102 case KSZ_ANY: return (sz);
103 case KSZ_RANGE:
104 t = ARG(1); u = ARG(2); v = ARG(3);
105 if (v) { sz += v - 1; sz -= sz%v; }
106 if (u && sz > u) return (0);
107 if (sz < t) return (t);
108 return (sz);
109 case KSZ_SET:
110 u = 0;
111 for (;;) {
112 t = ARG(0); ksz += wd; if (!t) break;
113 if (sz <= t && (!u || u > t)) u = t;
114 }
115 return (u);
116 }
117#undef ARG
118
119 assert(((void)"bad key size table", 0));
120 return (0);
121}
122
7809fc3d 123/*----- That's all, folks -------------------------------------------------*/