| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/dmfc/encryption.cpp $ |
| 21 | * $Revision: 1.1.1.1 $ |
| 22 | * $Date: 2003/08/26 03:57:21 $ |
| 23 | * $Author: kevinb $ |
| 24 | * |
| 25 | * header file for ICE encryption class |
| 26 | * |
| 27 | * $Log: encryption.cpp,v $ |
| 28 | * Revision 1.1.1.1 2003/08/26 03:57:21 kevinb |
| 29 | * initial 1.5 import |
| 30 | * |
| 31 | * |
| 32 | * 2 7/08/99 2:37a Jeff |
| 33 | * created encryption class based off of ICE encryption |
| 34 | * |
| 35 | * $NoKeywords: $ |
| 36 | */ |
| 37 | |
| 38 | #include "encryption.h"cxx . . $ `<file>/netgames/dmfc/encryption.h`/ |
| 39 | |
| 40 | class IceSubkeycxx . . $ IceSubkey# { |
| 41 | public: |
| 42 | uint32_tcxx . . $ uint32_t# valcxx . . $ IceSubkey#val.[3]; |
| 43 | }; |
| 44 | |
| 45 | // the S-boxes |
| 46 | static uint32_tcxx . . $ uint32_t# ice_sboxcxx . . $ ice_sbox.[4][1024]; |
| 47 | static int ice_sboxes_initialisedcxx . . $ ice_sboxes_initialised. = 0; |
| 48 | |
| 49 | // modulo values for the S-boxes |
| 50 | static const int ice_smodcxx . . $ ice_smod.[4][4] = { |
| 51 | {333, 313, 505, 369}, {379, 375, 319, 391}, {361, 445, 451, 397}, {397, 425, 395, 505}}; |
| 52 | |
| 53 | // XOR values for the S-boxes |
| 54 | static const int ice_sxorcxx . . $ ice_sxor.[4][4] = { |
| 55 | {0x83, 0x85, 0x9b, 0xcd}, {0xcc, 0xa7, 0xad, 0x41}, {0x4b, 0x2e, 0xd4, 0x33}, {0xea, 0xcb, 0x2e, 0x04}}; |
| 56 | |
| 57 | // Permutation values for the P-box |
| 58 | static const uint32_tcxx . . $ uint32_t# ice_pboxcxx . . $ ice_pbox.[32] = { |
| 59 | 0x00000001, 0x00000080, 0x00000400, 0x00002000, 0x00080000, 0x00200000, 0x01000000, 0x40000000, |
| 60 | 0x00000008, 0x00000020, 0x00000100, 0x00004000, 0x00010000, 0x00800000, 0x04000000, 0x20000000, |
| 61 | 0x00000004, 0x00000010, 0x00000200, 0x00008000, 0x00020000, 0x00400000, 0x08000000, 0x10000000, |
| 62 | 0x00000002, 0x00000040, 0x00000800, 0x00001000, 0x00040000, 0x00100000, 0x02000000, 0x80000000}; |
| 63 | |
| 64 | // key rotation schedule |
| 65 | static const int ice_keyrotcxx . . $ ice_keyrot.[16] = {0, 1, 2, 3, 2, 1, 3, 0, 1, 3, 2, 0, 3, 1, 0, 2}; |
| 66 | |
| 67 | // |
| 68 | // 8-bit Galois Field multiplication of a by b, modulo m. |
| 69 | // Just like arithmetic multiplication, except that additions and |
| 70 | // subtractions are replaced by XOR. |
| 71 | // |
| 72 | static uint32_tcxx . . $ uint32_t# gf_multcxx . . $ gf_mult(4eebda9b5e4ecc9c).(uint32_tcxx . . $ uint32_t# a, uint32_tcxx . . $ uint32_t# b, uint32_tcxx . . $ uint32_t# m) { |
| 73 | uint32_tcxx . . $ uint32_t# res = 0; |
| 74 | |
| 75 | while (b) { |
| 76 | if (b & 1) |
| 77 | res ^= a; |
| 78 | |
| 79 | a <<= 1; |
| 80 | b >>= 1; |
| 81 | |
| 82 | if (a >= 256) |
| 83 | a ^= m; |
| 84 | } |
| 85 | |
| 86 | return (res); |
| 87 | } |
| 88 | |
| 89 | // |
| 90 | // Galois Field exponentiation. |
| 91 | // Raise the base to the power of 7, modulo m. |
| 92 | // |
| 93 | static uint32_tcxx . . $ uint32_t# gf_exp7cxx . . $ gf_exp7(83f10ac8959d5577).(uint32_tcxx . . $ uint32_t# b, uint32_tcxx . . $ uint32_t# m) { |
| 94 | uint32_tcxx . . $ uint32_t# x; |
| 95 | |
| 96 | if (b == 0) |
| 97 | return 0; |
| 98 | |
| 99 | x = gf_multcxx . . $ gf_mult(4eebda9b5e4ecc9c).(b, b, m); |
| 100 | x = gf_multcxx . . $ gf_mult(4eebda9b5e4ecc9c).(b, x, m); |
| 101 | x = gf_multcxx . . $ gf_mult(4eebda9b5e4ecc9c).(x, x, m); |
| 102 | |
| 103 | return (gf_multcxx . . $ gf_mult(4eebda9b5e4ecc9c).(b, x, m)); |
| 104 | } |
| 105 | |
| 106 | // |
| 107 | // Carry out the ICE 32-bit P-box permutation. |
| 108 | // |
| 109 | static uint32_tcxx . . $ uint32_t# ice_perm32cxx . . $ ice_perm32(2141b656ce2cc063).(uint32_tcxx . . $ uint32_t# x) { |
| 110 | uint32_tcxx . . $ uint32_t# res = 0; |
| 111 | const uint32_tcxx . . $ uint32_t# *pbox = ice_pboxcxx . . $ ice_pbox.; |
| 112 | |
| 113 | while (x) { |
| 114 | if (x & 1) |
| 115 | res |= *pbox; |
| 116 | pbox++; |
| 117 | x >>= 1; |
| 118 | } |
| 119 | |
| 120 | return (res); |
| 121 | } |
| 122 | |
| 123 | // |
| 124 | // Initialise the ICE S-boxes. |
| 125 | // This only has to be done once. |
| 126 | // |
| 127 | static void ice_sboxes_initcxx . . $ ice_sboxes_init(49f6e7a06ebc5aa8).(void) { |
| 128 | int i; |
| 129 | |
| 130 | for (i = 0; i < 1024; i++) { |
| 131 | int col = (i >> 1) & 0xff; |
| 132 | int row = (i & 0x1) | ((i & 0x200) >> 8); |
| 133 | uint32_tcxx . . $ uint32_t# x; |
| 134 | |
| 135 | x = gf_exp7cxx . . $ gf_exp7(83f10ac8959d5577).(col ^ ice_sxorcxx . . $ ice_sxor.[0][row], ice_smodcxx . . $ ice_smod.[0][row]) << 24; |
| 136 | ice_sboxcxx . . $ ice_sbox.[0][i] = ice_perm32cxx . . $ ice_perm32(2141b656ce2cc063).(x); |
| 137 | |
| 138 | x = gf_exp7cxx . . $ gf_exp7(83f10ac8959d5577).(col ^ ice_sxorcxx . . $ ice_sxor.[1][row], ice_smodcxx . . $ ice_smod.[1][row]) << 16; |
| 139 | ice_sboxcxx . . $ ice_sbox.[1][i] = ice_perm32cxx . . $ ice_perm32(2141b656ce2cc063).(x); |
| 140 | |
| 141 | x = gf_exp7cxx . . $ gf_exp7(83f10ac8959d5577).(col ^ ice_sxorcxx . . $ ice_sxor.[2][row], ice_smodcxx . . $ ice_smod.[2][row]) << 8; |
| 142 | ice_sboxcxx . . $ ice_sbox.[2][i] = ice_perm32cxx . . $ ice_perm32(2141b656ce2cc063).(x); |
| 143 | |
| 144 | x = gf_exp7cxx . . $ gf_exp7(83f10ac8959d5577).(col ^ ice_sxorcxx . . $ ice_sxor.[3][row], ice_smodcxx . . $ ice_smod.[3][row]); |
| 145 | ice_sboxcxx . . $ ice_sbox.[3][i] = ice_perm32cxx . . $ ice_perm32(2141b656ce2cc063).(x); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // |
| 150 | // Create a new ICE key. |
| 151 | // |
| 152 | IceKeycxx . . $ IceKey#::IceKeycxx . . $ IceKey#IceKey(d4f767463ce0a6b3).(int n) { |
| 153 | if (!ice_sboxes_initialisedcxx . . $ ice_sboxes_initialised.) { |
| 154 | ice_sboxes_initcxx . . $ ice_sboxes_init(49f6e7a06ebc5aa8).(); |
| 155 | ice_sboxes_initialisedcxx . . $ ice_sboxes_initialised. = 1; |
| 156 | } |
| 157 | |
| 158 | if (n < 1) { |
| 159 | _sizecxx . . $ IceKey#_size. = 1; |
| 160 | _roundscxx . . $ IceKey#_rounds. = 8; |
| 161 | } else { |
| 162 | _sizecxx . . $ IceKey#_size. = n; |
| 163 | _roundscxx . . $ IceKey#_rounds. = n * 16; |
| 164 | } |
| 165 | |
| 166 | _keyschedcxx . . $ IceKey#_keysched. = new IceSubkeycxx . . $ IceSubkey#[_roundscxx . . $ IceKey#_rounds.]; |
| 167 | } |
| 168 | |
| 169 | // |
| 170 | // Destroy an ICE key. |
| 171 | // |
| 172 | IceKeycxx . . $ IceKey#::~cxx . . $ IceKey#`~IceKey`(ced63f7c635d850d).IceKeycxx . . $ IceKey#() { |
| 173 | int i, j; |
| 174 | |
| 175 | for (i = 0; i < _roundscxx . . $ IceKey#_rounds.; i++) { |
| 176 | for (j = 0; j < 3; j++) { |
| 177 | _keyschedcxx . . $ IceKey#_keysched.[i].valcxx . . $ IceSubkey#val.[j] = 0; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | _roundscxx . . $ IceKey#_rounds. = _sizecxx . . $ IceKey#_size. = 0; |
| 182 | |
| 183 | delete[] _keyschedcxx . . $ IceKey#_keysched.; |
| 184 | } |
| 185 | |
| 186 | // |
| 187 | // The single round ICE f function. |
| 188 | // |
| 189 | static uint32_tcxx . . $ uint32_t# ice_fcxx . . $ ice_f(a9273c178823ae44).(uint32_tcxx . . $ uint32_t# p, const IceSubkeycxx . . $ IceSubkey# *sk) { |
| 190 | uint32_tcxx . . $ uint32_t# tl, tr; /* Expanded 40-bit values */ |
| 191 | uint32_tcxx . . $ uint32_t# al, ar; /* Salted expanded 40-bit values */ |
| 192 | |
| 193 | // Left half expansion |
| 194 | tl = ((p >> 16) & 0x3ff) | (((p >> 14) | (p << 18)) & 0xffc00); |
| 195 | |
| 196 | // Right half expansion |
| 197 | tr = (p & 0x3ff) | ((p << 2) & 0xffc00); |
| 198 | |
| 199 | // Perform the salt permutation |
| 200 | al = sk->valcxx . . $ IceSubkey#val.[2] & (tl ^ tr); |
| 201 | ar = al ^ tr; |
| 202 | al ^= tl; |
| 203 | |
| 204 | al ^= sk->valcxx . . $ IceSubkey#val.[0]; // XOR with the subkey |
| 205 | ar ^= sk->valcxx . . $ IceSubkey#val.[1]; |
| 206 | |
| 207 | // S-box lookup and permutation |
| 208 | return (ice_sboxcxx . . $ ice_sbox.[0][al >> 10] | ice_sboxcxx . . $ ice_sbox.[1][al & 0x3ff] | ice_sboxcxx . . $ ice_sbox.[2][ar >> 10] | ice_sboxcxx . . $ ice_sbox.[3][ar & 0x3ff]); |
| 209 | } |
| 210 | |
| 211 | // |
| 212 | // Encrypt a block of 8 bytes of data with the given ICE key. |
| 213 | // |
| 214 | void IceKeycxx . . $ IceKey#::encryptcxx . . $ IceKey#encrypt(a18788b04e8c2902).(const uint8_tcxx . . $ uint8_t# *ptext, uint8_tcxx . . $ uint8_t# *ctext) const { |
| 215 | int i; |
| 216 | uint32_tcxx . . $ uint32_t# l, r; |
| 217 | |
| 218 | l = (((uint32_tcxx . . $ uint32_t#)ptext[0]) << 24) | (((uint32_tcxx . . $ uint32_t#)ptext[1]) << 16) | (((uint32_tcxx . . $ uint32_t#)ptext[2]) << 8) | |
| 219 | ptext[3]; |
| 220 | |
| 221 | r = (((uint32_tcxx . . $ uint32_t#)ptext[4]) << 24) | (((uint32_tcxx . . $ uint32_t#)ptext[5]) << 16) | (((uint32_tcxx . . $ uint32_t#)ptext[6]) << 8) | |
| 222 | ptext[7]; |
| 223 | |
| 224 | for (i = 0; i < _roundscxx . . $ IceKey#_rounds.; i += 2) { |
| 225 | l ^= ice_fcxx . . $ ice_f(a9273c178823ae44).(r, &_keyschedcxx . . $ IceKey#_keysched.[i]); |
| 226 | r ^= ice_fcxx . . $ ice_f(a9273c178823ae44).(l, &_keyschedcxx . . $ IceKey#_keysched.[i + 1]); |
| 227 | } |
| 228 | |
| 229 | for (i = 0; i < 4; i++) { |
| 230 | ctext[3 - i] = (uint8_tcxx . . $ uint8_t#)r & 0xff; |
| 231 | ctext[7 - i] = (uint8_tcxx . . $ uint8_t#)l & 0xff; |
| 232 | |
| 233 | r >>= 8; |
| 234 | l >>= 8; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | // |
| 239 | // Decrypt a block of 8 bytes of data with the given ICE key. |
| 240 | // |
| 241 | void IceKeycxx . . $ IceKey#::decryptcxx . . $ IceKey#decrypt(a18788b04e8c2902).(const uint8_tcxx . . $ uint8_t# *ctext, uint8_tcxx . . $ uint8_t# *ptext) const { |
| 242 | int i; |
| 243 | uint32_tcxx . . $ uint32_t# l, r; |
| 244 | |
| 245 | l = (((uint32_tcxx . . $ uint32_t#)ctext[0]) << 24) | (((uint32_tcxx . . $ uint32_t#)ctext[1]) << 16) | (((uint32_tcxx . . $ uint32_t#)ctext[2]) << 8) | |
| 246 | ctext[3]; |
| 247 | r = (((uint32_tcxx . . $ uint32_t#)ctext[4]) << 24) | (((uint32_tcxx . . $ uint32_t#)ctext[5]) << 16) | (((uint32_tcxx . . $ uint32_t#)ctext[6]) << 8) | |
| 248 | ctext[7]; |
| 249 | |
| 250 | for (i = _roundscxx . . $ IceKey#_rounds. - 1; i > 0; i -= 2) { |
| 251 | l ^= ice_fcxx . . $ ice_f(a9273c178823ae44).(r, &_keyschedcxx . . $ IceKey#_keysched.[i]); |
| 252 | r ^= ice_fcxx . . $ ice_f(a9273c178823ae44).(l, &_keyschedcxx . . $ IceKey#_keysched.[i - 1]); |
| 253 | } |
| 254 | |
| 255 | for (i = 0; i < 4; i++) { |
| 256 | ptext[3 - i] = (uint8_tcxx . . $ uint8_t#)r & 0xff; |
| 257 | ptext[7 - i] = (uint8_tcxx . . $ uint8_t#)l & 0xff; |
| 258 | |
| 259 | r >>= 8; |
| 260 | l >>= 8; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // |
| 265 | // Set 8 rounds [n, n+7] of the key schedule of an ICE key. |
| 266 | // |
| 267 | void IceKeycxx . . $ IceKey#::scheduleBuildcxx . . $ IceKey#scheduleBuild(d8d4a5161d44b488).(uint16_tcxx . . $ uint16_t# *kb, int n, const int *keyrot) { |
| 268 | int i; |
| 269 | |
| 270 | for (i = 0; i < 8; i++) { |
| 271 | int j; |
| 272 | int kr = keyrot[i]; |
| 273 | IceSubkeycxx . . $ IceSubkey# *isk = &_keyschedcxx . . $ IceKey#_keysched.[n + i]; |
| 274 | |
| 275 | for (j = 0; j < 3; j++) |
| 276 | isk->valcxx . . $ IceSubkey#val.[j] = 0; |
| 277 | |
| 278 | for (j = 0; j < 15; j++) { |
| 279 | int k; |
| 280 | uint32_tcxx . . $ uint32_t# *curr_sk = &isk->valcxx . . $ IceSubkey#val.[j % 3]; |
| 281 | |
| 282 | for (k = 0; k < 4; k++) { |
| 283 | uint16_tcxx . . $ uint16_t# *curr_kb = &kb[(kr + k) & 3]; |
| 284 | int bit = *curr_kb & 1; |
| 285 | |
| 286 | *curr_sk = (*curr_sk << 1) | bit; |
| 287 | *curr_kb = (*curr_kb >> 1) | ((bit ^ 1) << 15); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | // |
| 294 | // Set the key schedule of an ICE key. |
| 295 | // |
| 296 | void IceKeycxx . . $ IceKey#::setcxx . . $ IceKey#set(28caea9380bfac8a).(const uint8_tcxx . . $ uint8_t# *key) { |
| 297 | int i; |
| 298 | |
| 299 | if (_roundscxx . . $ IceKey#_rounds. == 8) { |
| 300 | uint16_tcxx . . $ uint16_t# kb[4]; |
| 301 | |
| 302 | for (i = 0; i < 4; i++) { |
| 303 | kb[3 - i] = (key[i * 2] << 8) | key[i * 2 + 1]; |
| 304 | } |
| 305 | |
| 306 | scheduleBuildcxx . . $ IceKey#scheduleBuild(d8d4a5161d44b488).(kb, 0, ice_keyrotcxx . . $ ice_keyrot.); |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | for (i = 0; i < _sizecxx . . $ IceKey#_size.; i++) { |
| 311 | int j; |
| 312 | uint16_tcxx . . $ uint16_t# kb[4]; |
| 313 | |
| 314 | for (j = 0; j < 4; j++) { |
| 315 | kb[3 - j] = (key[i * 8 + j * 2] << 8) | key[i * 8 + j * 2 + 1]; |
| 316 | |
| 317 | scheduleBuildcxx . . $ IceKey#scheduleBuild(d8d4a5161d44b488).(kb, i * 8, ice_keyrotcxx . . $ ice_keyrot.); |
| 318 | scheduleBuildcxx . . $ IceKey#scheduleBuild(d8d4a5161d44b488).(kb, _roundscxx . . $ IceKey#_rounds. - 8 - i * 8, &ice_keyrotcxx . . $ ice_keyrot.[8]); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | // |
| 324 | // Return the key size, in bytes. |
| 325 | // |
| 326 | int IceKeycxx . . $ IceKey#::keySizecxx . . $ IceKey#keySize(455f465bc33b4cdf).(void) const { return (_sizecxx . . $ IceKey#_size. * 8); } |
| 327 | |
| 328 | // |
| 329 | // Return the block size, in bytes. |
| 330 | // |
| 331 | int IceKeycxx . . $ IceKey#::blockSizecxx . . $ IceKey#blockSize(455f465bc33b4cdf).(void) const { return 8; } |