| 1 | /* |
| 2 | * FreeSec: libcrypt for NetBSD |
| 3 | * |
| 4 | * Copyright (c) 1994 David Burren |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Adapted for FreeBSD-2.0 by Geoffrey M. Rehmet |
| 8 | * this file should now *only* export crypt(), in order to make |
| 9 | * binaries of libcrypt exportable from the USA |
| 10 | * |
| 11 | * Adapted for FreeBSD-4.0 by Mark R V Murray |
| 12 | * this file should now *only* export crypt_des(), in order to make |
| 13 | * a module that can be optionally included in libcrypt. |
| 14 | * |
| 15 | * Adapted for libxcrypt by Zack Weinberg, 2017 |
| 16 | * writable global data eliminated; type-punning eliminated; |
| 17 | * des_init() run at build time (see des-mktables.c); |
| 18 | * made into a libxcrypt algorithm module (see des-crypt.c); |
| 19 | * functionality required to support the legacy encrypt() and |
| 20 | * setkey() primitives re-exposed (see des-obsolete.c). |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the above copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. Neither the name of the author nor the names of other contributors |
| 31 | * may be used to endorse or promote products derived from this software |
| 32 | * without specific prior written permission. |
| 33 | * |
| 34 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 35 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 36 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 37 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 38 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 39 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 40 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 41 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 42 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 43 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 44 | * SUCH DAMAGE. |
| 45 | * |
| 46 | * This is an original implementation of the DES and the crypt(3) interfaces |
| 47 | * by David Burren <[email protected]>. |
| 48 | * |
| 49 | * An excellent reference on the underlying algorithm (and related |
| 50 | * algorithms) is: |
| 51 | * |
| 52 | * B. Schneier, Applied Cryptography: protocols, algorithms, |
| 53 | * and source code in C, John Wiley & Sons, 1994. |
| 54 | * |
| 55 | * Note that in that book's description of DES the lookups for the initial, |
| 56 | * pbox, and final permutations are inverted (this has been brought to the |
| 57 | * attention of the author). A list of errata for this book has been |
| 58 | * posted to the sci.crypt newsgroup by the author and is available for FTP. |
| 59 | */ |
| 60 | |
| 61 | #include "crypt-port.h" |
| 62 | |
| 63 | #if INCLUDE_descrypt || INCLUDE_bigcrypt || INCLUDE_bsdicrypt |
| 64 | |
| 65 | #include "alg-des.h" |
| 66 | #include "byteorder.h" |
| 67 | |
| 68 | static const uint8_t key_shifts[16] = |
| 69 | { |
| 70 | 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 |
| 71 | }; |
| 72 | |
| 73 | void |
| 74 | des_set_key (struct des_ctx *restrict ctx, |
| 75 | const unsigned char key[MIN_SIZE(8)]) |
| 76 | { |
| 77 | uint32_t rawkey0, rawkey1, k0, k1, t0, t1; |
| 78 | int shifts, round; |
| 79 | |
| 80 | rawkey0 = be32_to_cpu (&key[0]); |
| 81 | rawkey1 = be32_to_cpu (&key[4]); |
| 82 | |
| 83 | /* Do key permutation and split into two 28-bit subkeys. */ |
| 84 | k0 = key_perm_maskl[0][(rawkey0 >> 25) & 0x7f] |
| 85 | | key_perm_maskl[1][(rawkey0 >> 17) & 0x7f] |
| 86 | | key_perm_maskl[2][(rawkey0 >> 9) & 0x7f] |
| 87 | | key_perm_maskl[3][(rawkey0 >> 1) & 0x7f] |
| 88 | | key_perm_maskl[4][(rawkey1 >> 25) & 0x7f] |
| 89 | | key_perm_maskl[5][(rawkey1 >> 17) & 0x7f] |
| 90 | | key_perm_maskl[6][(rawkey1 >> 9) & 0x7f] |
| 91 | | key_perm_maskl[7][(rawkey1 >> 1) & 0x7f]; |
| 92 | k1 = key_perm_maskr[0][(rawkey0 >> 25) & 0x7f] |
| 93 | | key_perm_maskr[1][(rawkey0 >> 17) & 0x7f] |
| 94 | | key_perm_maskr[2][(rawkey0 >> 9) & 0x7f] |
| 95 | | key_perm_maskr[3][(rawkey0 >> 1) & 0x7f] |
| 96 | | key_perm_maskr[4][(rawkey1 >> 25) & 0x7f] |
| 97 | | key_perm_maskr[5][(rawkey1 >> 17) & 0x7f] |
| 98 | | key_perm_maskr[6][(rawkey1 >> 9) & 0x7f] |
| 99 | | key_perm_maskr[7][(rawkey1 >> 1) & 0x7f]; |
| 100 | |
| 101 | /* Rotate subkeys and do compression permutation. */ |
| 102 | shifts = 0; |
| 103 | for (round = 0; round < 16; round++) |
| 104 | { |
| 105 | shifts += key_shifts[round]; |
| 106 | |
| 107 | t0 = (k0 << shifts) | (k0 >> (28 - shifts)); |
| 108 | t1 = (k1 << shifts) | (k1 >> (28 - shifts)); |
| 109 | |
| 110 | ctx->keysl[round] = |
| 111 | comp_maskl[0][(t0 >> 21) & 0x7f] |
| 112 | | comp_maskl[1][(t0 >> 14) & 0x7f] |
| 113 | | comp_maskl[2][(t0 >> 7) & 0x7f] |
| 114 | | comp_maskl[3][(t0 >> 0) & 0x7f] |
| 115 | | comp_maskl[4][(t1 >> 21) & 0x7f] |
| 116 | | comp_maskl[5][(t1 >> 14) & 0x7f] |
| 117 | | comp_maskl[6][(t1 >> 7) & 0x7f] |
| 118 | | comp_maskl[7][(t1 >> 0) & 0x7f]; |
| 119 | |
| 120 | ctx->keysr[round] = |
| 121 | comp_maskr[0][(t0 >> 21) & 0x7f] |
| 122 | | comp_maskr[1][(t0 >> 14) & 0x7f] |
| 123 | | comp_maskr[2][(t0 >> 7) & 0x7f] |
| 124 | | comp_maskr[3][(t0 >> 0) & 0x7f] |
| 125 | | comp_maskr[4][(t1 >> 21) & 0x7f] |
| 126 | | comp_maskr[5][(t1 >> 14) & 0x7f] |
| 127 | | comp_maskr[6][(t1 >> 7) & 0x7f] |
| 128 | | comp_maskr[7][(t1 >> 0) & 0x7f]; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | des_set_salt (struct des_ctx *restrict ctx, uint32_t salt) |
| 134 | { |
| 135 | uint32_t obit, saltbit, saltbits; |
| 136 | int i; |
| 137 | saltbits = 0L; |
| 138 | saltbit = 1; |
| 139 | obit = 0x800000; |
| 140 | for (i = 0; i < 24; i++) |
| 141 | { |
| 142 | if (salt & saltbit) |
| 143 | saltbits |= obit; |
| 144 | saltbit <<= 1; |
| 145 | obit >>= 1; |
| 146 | } |
| 147 | ctx->saltbits = saltbits; |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | des_crypt_block (struct des_ctx *restrict ctx, |
| 152 | unsigned char *out, const unsigned char *in, |
| 153 | unsigned int count, bool decrypt) |
| 154 | { |
| 155 | uint32_t l_in, r_in, l_out, r_out; |
| 156 | uint32_t l, r, *kl, *kr, *kl1, *kr1; |
| 157 | uint32_t f, r48l, r48r; |
| 158 | uint32_t saltbits = ctx->saltbits; |
| 159 | int round, rk_step; |
| 160 | |
| 161 | /* Zero encryptions/decryptions doesn't make sense. */ |
| 162 | if (count == 0) |
| 163 | count = 1; |
| 164 | |
| 165 | if (decrypt) |
| 166 | { |
| 167 | kl1 = ctx->keysl + 15; |
| 168 | kr1 = ctx->keysr + 15; |
| 169 | rk_step = -1; |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | kl1 = ctx->keysl; |
| 174 | kr1 = ctx->keysr; |
| 175 | rk_step = 1; |
| 176 | } |
| 177 | |
| 178 | /* Read the input, which is notionally in "big-endian" format. */ |
| 179 | l_in = be32_to_cpu (in); |
| 180 | r_in = be32_to_cpu (in + 4); |
| 181 | |
| 182 | /* Do initial permutation. */ |
| 183 | l = ip_maskl[0][(l_in >> 24) & 0xff] |
| 184 | | ip_maskl[1][(l_in >> 16) & 0xff] |
| 185 | | ip_maskl[2][(l_in >> 8) & 0xff] |
| 186 | | ip_maskl[3][(l_in >> 0) & 0xff] |
| 187 | | ip_maskl[4][(r_in >> 24) & 0xff] |
| 188 | | ip_maskl[5][(r_in >> 16) & 0xff] |
| 189 | | ip_maskl[6][(r_in >> 8) & 0xff] |
| 190 | | ip_maskl[7][(r_in >> 0) & 0xff]; |
| 191 | r = ip_maskr[0][(l_in >> 24) & 0xff] |
| 192 | | ip_maskr[1][(l_in >> 16) & 0xff] |
| 193 | | ip_maskr[2][(l_in >> 8) & 0xff] |
| 194 | | ip_maskr[3][(l_in >> 0) & 0xff] |
| 195 | | ip_maskr[4][(r_in >> 24) & 0xff] |
| 196 | | ip_maskr[5][(r_in >> 16) & 0xff] |
| 197 | | ip_maskr[6][(r_in >> 8) & 0xff] |
| 198 | | ip_maskr[7][(r_in >> 0) & 0xff]; |
| 199 | |
| 200 | do |
| 201 | { |
| 202 | kl = kl1; |
| 203 | kr = kr1; |
| 204 | round = 16; |
| 205 | do |
| 206 | { |
| 207 | /* Expand R to 48 bits (simulate the E-box). */ |
| 208 | r48l = ((r & 0x00000001) << 23) |
| 209 | | ((r & 0xf8000000) >> 9) |
| 210 | | ((r & 0x1f800000) >> 11) |
| 211 | | ((r & 0x01f80000) >> 13) |
| 212 | | ((r & 0x001f8000) >> 15); |
| 213 | r48r = ((r & 0x0001f800) << 7) |
| 214 | | ((r & 0x00001f80) << 5) |
| 215 | | ((r & 0x000001f8) << 3) |
| 216 | | ((r & 0x0000001f) << 1) |
| 217 | | ((r & 0x80000000) >> 31); |
| 218 | |
| 219 | /* Apply salt and permuted round key. */ |
| 220 | f = (r48l ^ r48r) & saltbits; |
| 221 | r48l ^= f ^ *kl; |
| 222 | r48r ^= f ^ *kr; |
| 223 | kl += rk_step; |
| 224 | kr += rk_step; |
| 225 | |
| 226 | /* Do sbox lookups (which shrink it back to 32 bits) |
| 227 | and the pbox permutation at the same time. */ |
| 228 | f = psbox[0][m_sbox[0][r48l >> 12]] |
| 229 | | psbox[1][m_sbox[1][r48l & 0xfff]] |
| 230 | | psbox[2][m_sbox[2][r48r >> 12]] |
| 231 | | psbox[3][m_sbox[3][r48r & 0xfff]]; |
| 232 | |
| 233 | /* Now that we've permuted things, complete f(). */ |
| 234 | f ^= l; |
| 235 | l = r; |
| 236 | r = f; |
| 237 | } |
| 238 | while (--round); |
| 239 | |
| 240 | r = l; |
| 241 | l = f; |
| 242 | } |
| 243 | while (--count); |
| 244 | |
| 245 | /* Do final permutation (inverse of IP). */ |
| 246 | l_out = |
| 247 | fp_maskl[0][(l >> 24) & 0xff] |
| 248 | | fp_maskl[1][(l >> 16) & 0xff] |
| 249 | | fp_maskl[2][(l >> 8) & 0xff] |
| 250 | | fp_maskl[3][(l >> 0) & 0xff] |
| 251 | | fp_maskl[4][(r >> 24) & 0xff] |
| 252 | | fp_maskl[5][(r >> 16) & 0xff] |
| 253 | | fp_maskl[6][(r >> 8) & 0xff] |
| 254 | | fp_maskl[7][(r >> 0) & 0xff]; |
| 255 | r_out = |
| 256 | fp_maskr[0][(l >> 24) & 0xff] |
| 257 | | fp_maskr[1][(l >> 16) & 0xff] |
| 258 | | fp_maskr[2][(l >> 8) & 0xff] |
| 259 | | fp_maskr[3][(l >> 0) & 0xff] |
| 260 | | fp_maskr[4][(r >> 24) & 0xff] |
| 261 | | fp_maskr[5][(r >> 16) & 0xff] |
| 262 | | fp_maskr[6][(r >> 8) & 0xff] |
| 263 | | fp_maskr[7][(r >> 0) & 0xff]; |
| 264 | |
| 265 | cpu_to_be32 (out, l_out); |
| 266 | cpu_to_be32 (out + 4, r_out); |
| 267 | } |
| 268 | |
| 269 | #endif |