| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Implementation of DESX |
| 4 | * |
| 5 | * (c) 2001 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 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | #include <mLib/bits.h> |
| 36 | |
| 37 | #include "blkc.h" |
| 38 | #include "des-base.h" |
| 39 | #include "des.h" |
| 40 | #include "desx.h" |
| 41 | #include "gcipher.h" |
| 42 | |
| 43 | /*----- Tables ------------------------------------------------------------*/ |
| 44 | |
| 45 | extern const octet desx_s[256]; |
| 46 | |
| 47 | /*----- Global variables --------------------------------------------------*/ |
| 48 | |
| 49 | const octet desx_keysz[] = { KSZ_SET, 23, 7, 8, 15, 16, 24, 0 }; |
| 50 | |
| 51 | /*----- Main code ---------------------------------------------------------*/ |
| 52 | |
| 53 | /* --- @desx_init@ --- * |
| 54 | * |
| 55 | * Arguments: @desx_ctx *k@ = pointer to key block |
| 56 | * @const void *buf@ = pointer to key buffer |
| 57 | * @size_t sz@ = size of key material |
| 58 | * |
| 59 | * Returns: --- |
| 60 | * |
| 61 | * Use: Initializes a DESX key buffer. The key buffer contains, in |
| 62 | * order, a single-DES key (either 7 or 8 bytes), an optional |
| 63 | * 8-byte pre-whitening key, and an optional 8-byte |
| 64 | * port-whitening key. If no whitening keys are specified, the |
| 65 | * algorithm becomes the same as single-DES. |
| 66 | */ |
| 67 | |
| 68 | static void mangle(octet *b, const octet *p) |
| 69 | { |
| 70 | unsigned i; |
| 71 | |
| 72 | for (i = 0; i < 8; i++) |
| 73 | b[i] = *p++ ^ desx_s[b[i] ^ b[(i + 1) & 7u]]; |
| 74 | } |
| 75 | |
| 76 | void desx_init(desx_ctx *k, const void *buf, size_t sz) |
| 77 | { |
| 78 | const octet *p = buf, *kk = buf; |
| 79 | size_t n; |
| 80 | |
| 81 | KSZ_ASSERT(desx, sz); |
| 82 | |
| 83 | n = sz % 8 == 7 ? 7 : 8; |
| 84 | des_init(&k->k, p, n); |
| 85 | p += n; |
| 86 | sz -= n; |
| 87 | if (!sz) |
| 88 | k->prea = k->preb = k->posta = k->postb = 0; |
| 89 | else { |
| 90 | const octet *q = p; |
| 91 | k->prea = LOAD32(q + 0); |
| 92 | k->preb = LOAD32(q + 4); |
| 93 | p += 8; |
| 94 | sz -= 8; |
| 95 | if (sz) { |
| 96 | k->posta = LOAD32(p + 0); |
| 97 | k->postb = LOAD32(p + 4); |
| 98 | } else { |
| 99 | octet b[16]; |
| 100 | uint32 x, y; |
| 101 | |
| 102 | des_expand(kk, n, &x, &y); |
| 103 | STORE32(b + 8, x); STORE32(b + 12, y); |
| 104 | memset(b, 0, 8); |
| 105 | mangle(b, b + 8); |
| 106 | mangle(b, q); |
| 107 | k->posta = LOAD32(b + 0); |
| 108 | k->postb = LOAD32(b + 4); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* --- @desx_eblk@, @desx_dblk@ --- * |
| 114 | * |
| 115 | * Arguments: @const desx_ctx *k@ = pointer to key block |
| 116 | * @const uint32 s[2]@ = pointer to source block |
| 117 | * @uint32 d[2]@ = pointer to destination block |
| 118 | * |
| 119 | * Returns: --- |
| 120 | * |
| 121 | * Use: Low-level block encryption and decryption. |
| 122 | */ |
| 123 | |
| 124 | void desx_eblk(const desx_ctx *k, const uint32 *s, uint32 *d) |
| 125 | { |
| 126 | uint32 x = s[0], y = s[1]; |
| 127 | x ^= k->prea; y ^= k->preb; |
| 128 | DES_IP(x, y); |
| 129 | DES_EBLK(k->k.k, x, y, x, y); |
| 130 | DES_IPINV(x, y); |
| 131 | x ^= k->posta; y ^= k->postb; |
| 132 | d[0] = x, d[1] = y; |
| 133 | } |
| 134 | |
| 135 | void desx_dblk(const desx_ctx *k, const uint32 *s, uint32 *d) |
| 136 | { |
| 137 | uint32 x = s[0], y = s[1]; |
| 138 | x ^= k->posta; y ^= k->postb; |
| 139 | DES_IP(x, y); |
| 140 | DES_DBLK(k->k.k, x, y, x, y); |
| 141 | DES_IPINV(x, y); |
| 142 | x ^= k->prea; y ^= k->preb; |
| 143 | d[0] = x, d[1] = y; |
| 144 | } |
| 145 | |
| 146 | BLKC_TEST(DESX, desx) |
| 147 | |
| 148 | /*----- That's all, folks -------------------------------------------------*/ |