Pollard's rho algorithm for computing discrete logs.
[u/mdw/catacomb] / keysz.c
CommitLineData
7809fc3d 1/* -*-c-*-
2 *
3 * $Id: keysz.c,v 1.1 2000/06/17 11:27:52 mdw Exp $
4 *
5 * General block cipher utilities
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: keysz.c,v $
33 * Revision 1.1 2000/06/17 11:27:52 mdw
34 * Key size table interpretation.
35 *
36 */
37
38/*----- Header files ------------------------------------------------------*/
39
40#include <assert.h>
41
42#include "gcipher.h"
43
44/*----- Main code ---------------------------------------------------------*/
45
46/* --- @keysz@ --- *
47 *
48 * Arguments: @size_t sz@ = a proposed key size, or zero
49 * @const octet *ksz@ = pointer to key size table
50 *
51 * Returns: See below.
52 *
53 * Use: Returns a sensible key size. If @sz@ is nonzero, it is
54 * interpreted as an amount (in bytes) of key material which the
55 * caller has available, and the return value is either the
56 * largest allowable key size less than or equal to the caller's
57 * size, or zero if there is no valid key length small enough.
58 * If @sz@ is zero, the function returns a `recommended' key
59 * size.
60 */
61
62size_t keysz(size_t sz, const octet *ksz)
63{
64 if (sz == 0)
65 return (ksz[1]);
66 else switch (ksz[0]) {
67 case KSZ_ANY:
68 return (sz);
69 case KSZ_RANGE:
70 if (ksz[4])
71 sz -= sz % ksz[4];
72 if (sz < ksz[2])
73 return (0);
74 if (ksz[3] && sz > ksz[3])
75 return (ksz[3]);
76 return (sz);
77 case KSZ_SET: {
78 unsigned q = 0;
79 for (ksz++; *ksz; ksz++) {
80 if (sz >= *ksz && q < *ksz)
81 q = *ksz;
82 }
83 return (q);
84 }
85 }
86
87 assert(((void)"bad key size table", 0));
88 return (0);
89}
90
91/*----- That's all, folks -------------------------------------------------*/