| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * General block cipher utilities |
| 4 | * |
| 5 | * (c) 2000 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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 | |
| 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 | unsigned op = ksz[0]&KSZ_OPMASK; |
| 55 | unsigned wd = (ksz[0]&KSZ_16BIT) ? 2 : 1; |
| 56 | unsigned t, u, v; |
| 57 | |
| 58 | ksz++; |
| 59 | #define ARG(i) (wd == 1 ? ksz[i] : LOAD16(ksz + 2*i)) |
| 60 | if (sz == 0) |
| 61 | return (ARG(0)); |
| 62 | else switch (op) { |
| 63 | case KSZ_ANY: return (sz); |
| 64 | case KSZ_RANGE: |
| 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); |
| 69 | return (sz); |
| 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; |
| 75 | } |
| 76 | return (u); |
| 77 | } |
| 78 | #undef ARG |
| 79 | |
| 80 | assert(((void)"bad key size table", 0)); |
| 81 | return (0); |
| 82 | } |
| 83 | |
| 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 | |
| 93 | size_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 | |
| 123 | /*----- That's all, folks -------------------------------------------------*/ |