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