| 1 |
/* |
| 2 |
* Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 5 |
* this file except in compliance with the License. You can obtain a copy |
| 6 |
* in the file LICENSE in the source distribution or at |
| 7 |
* https://www.openssl.org/source/license.html |
| 8 |
*/ |
| 9 |
|
| 10 |
#ifdef OPENSSL_SYS_WIN32 |
| 11 |
#include <stdlib.h> |
| 12 |
#endif |
| 13 |
|
| 14 |
/* NOTE - c is not incremented as per n2l */ |
| 15 |
#define n2ln(c, l1, l2, n) \ |
| 16 |
{ \ |
| 17 |
c += n; \ |
| 18 |
l1 = l2 = 0; \ |
| 19 |
switch (n) { \ |
| 20 |
case 8: \ |
| 21 |
l2 = ((unsigned long)(*(--(c)))); \ |
| 22 |
/* fall through */ \ |
| 23 |
case 7: \ |
| 24 |
l2 |= ((unsigned long)(*(--(c)))) << 8; \ |
| 25 |
/* fall through */ \ |
| 26 |
case 6: \ |
| 27 |
l2 |= ((unsigned long)(*(--(c)))) << 16; \ |
| 28 |
/* fall through */ \ |
| 29 |
case 5: \ |
| 30 |
l2 |= ((unsigned long)(*(--(c)))) << 24; \ |
| 31 |
/* fall through */ \ |
| 32 |
case 4: \ |
| 33 |
l1 = ((unsigned long)(*(--(c)))); \ |
| 34 |
/* fall through */ \ |
| 35 |
case 3: \ |
| 36 |
l1 |= ((unsigned long)(*(--(c)))) << 8; \ |
| 37 |
/* fall through */ \ |
| 38 |
case 2: \ |
| 39 |
l1 |= ((unsigned long)(*(--(c)))) << 16; \ |
| 40 |
/* fall through */ \ |
| 41 |
case 1: \ |
| 42 |
l1 |= ((unsigned long)(*(--(c)))) << 24; \ |
| 43 |
} \ |
| 44 |
} |
| 45 |
|
| 46 |
/* NOTE - c is not incremented as per l2n */ |
| 47 |
#define l2nn(l1, l2, c, n) \ |
| 48 |
{ \ |
| 49 |
c += n; \ |
| 50 |
switch (n) { \ |
| 51 |
case 8: \ |
| 52 |
*(--(c)) = (unsigned char)(((l2)) & 0xff); \ |
| 53 |
/* fall through */ \ |
| 54 |
case 7: \ |
| 55 |
*(--(c)) = (unsigned char)(((l2) >> 8) & 0xff); \ |
| 56 |
/* fall through */ \ |
| 57 |
case 6: \ |
| 58 |
*(--(c)) = (unsigned char)(((l2) >> 16) & 0xff); \ |
| 59 |
/* fall through */ \ |
| 60 |
case 5: \ |
| 61 |
*(--(c)) = (unsigned char)(((l2) >> 24) & 0xff); \ |
| 62 |
/* fall through */ \ |
| 63 |
case 4: \ |
| 64 |
*(--(c)) = (unsigned char)(((l1)) & 0xff); \ |
| 65 |
/* fall through */ \ |
| 66 |
case 3: \ |
| 67 |
*(--(c)) = (unsigned char)(((l1) >> 8) & 0xff); \ |
| 68 |
/* fall through */ \ |
| 69 |
case 2: \ |
| 70 |
*(--(c)) = (unsigned char)(((l1) >> 16) & 0xff); \ |
| 71 |
/* fall through */ \ |
| 72 |
case 1: \ |
| 73 |
*(--(c)) = (unsigned char)(((l1) >> 24) & 0xff); \ |
| 74 |
} \ |
| 75 |
} |
| 76 |
|
| 77 |
#undef n2l |
| 78 |
#define n2l(c, l) (l = ((unsigned long)(*((c)++))) << 24L, \ |
| 79 |
l |= ((unsigned long)(*((c)++))) << 16L, \ |
| 80 |
l |= ((unsigned long)(*((c)++))) << 8L, \ |
| 81 |
l |= ((unsigned long)(*((c)++)))) |
| 82 |
|
| 83 |
#undef l2n |
| 84 |
#define l2n(l, c) (*((c)++) = (unsigned char)(((l) >> 24L) & 0xff), \ |
| 85 |
*((c)++) = (unsigned char)(((l) >> 16L) & 0xff), \ |
| 86 |
*((c)++) = (unsigned char)(((l) >> 8L) & 0xff), \ |
| 87 |
*((c)++) = (unsigned char)(((l)) & 0xff)) |
| 88 |
|
| 89 |
#if defined(OPENSSL_SYS_WIN32) && defined(_MSC_VER) |
| 90 |
#define ROTL(a, n) (_lrotl(a, n)) |
| 91 |
#else |
| 92 |
#define ROTL(a, n) ((((a) << (n)) & 0xffffffffL) | ((a) >> ((32 - (n)) & 31))) |
| 93 |
#endif |
| 94 |
|
| 95 |
#define C_M 0x3fc |
| 96 |
#define C_0 22L |
| 97 |
#define C_1 14L |
| 98 |
#define C_2 6L |
| 99 |
#define C_3 2L /* left shift */ |
| 100 |
|
| 101 |
/* The rotate has an extra 16 added to it to help the x86 asm */ |
| 102 |
#if defined(CAST_PTR) |
| 103 |
#define E_CAST(n, key, L, R, OP1, OP2, OP3) \ |
| 104 |
{ \ |
| 105 |
int i; \ |
| 106 |
t = (key[n * 2] OP1 R) & 0xffffffffL; \ |
| 107 |
i = key[n * 2 + 1]; \ |
| 108 |
t = ROTL(t, i); \ |
| 109 |
L ^= (((((*(CAST_LONG *)((unsigned char *) \ |
| 110 |
CAST_S_table0 \ |
| 111 |
+ ((t >> C_2) & C_M)) OP2 \ |
| 112 |
* (CAST_LONG *)((unsigned char *) \ |
| 113 |
CAST_S_table1 \ |
| 114 |
+ ((t << C_3) & C_M))) \ |
| 115 |
& 0xffffffffL) OP3 \ |
| 116 |
* (CAST_LONG *)((unsigned char *) \ |
| 117 |
CAST_S_table2 \ |
| 118 |
+ ((t >> C_0) & C_M))) \ |
| 119 |
& 0xffffffffL) OP1 \ |
| 120 |
* (CAST_LONG *)((unsigned char *) \ |
| 121 |
CAST_S_table3 \ |
| 122 |
+ ((t >> C_1) & C_M))) \ |
| 123 |
& 0xffffffffL; \ |
| 124 |
} |
| 125 |
#elif defined(CAST_PTR2) |
| 126 |
#define E_CAST(n, key, L, R, OP1, OP2, OP3) \ |
| 127 |
{ \ |
| 128 |
int i; \ |
| 129 |
CAST_LONG u, v, w; \ |
| 130 |
w = (key[n * 2] OP1 R) & 0xffffffffL; \ |
| 131 |
i = key[n * 2 + 1]; \ |
| 132 |
w = ROTL(w, i); \ |
| 133 |
u = w >> C_2; \ |
| 134 |
v = w << C_3; \ |
| 135 |
u &= C_M; \ |
| 136 |
v &= C_M; \ |
| 137 |
t = *(CAST_LONG *)((unsigned char *)CAST_S_table0 + u); \ |
| 138 |
u = w >> C_0; \ |
| 139 |
t = (t OP2 * (CAST_LONG *)((unsigned char *)CAST_S_table1 + v)) & 0xffffffffL; \ |
| 140 |
v = w >> C_1; \ |
| 141 |
u &= C_M; \ |
| 142 |
v &= C_M; \ |
| 143 |
t = (t OP3 * (CAST_LONG *)((unsigned char *)CAST_S_table2 + u) & 0xffffffffL); \ |
| 144 |
t = (t OP1 * (CAST_LONG *)((unsigned char *)CAST_S_table3 + v) & 0xffffffffL); \ |
| 145 |
L ^= (t & 0xffffffff); \ |
| 146 |
} |
| 147 |
#else |
| 148 |
#define E_CAST(n, key, L, R, OP1, OP2, OP3) \ |
| 149 |
{ \ |
| 150 |
CAST_LONG a, b, c, d; \ |
| 151 |
t = (key[n * 2] OP1 R) & 0xffffffff; \ |
| 152 |
t = ROTL(t, (key[n * 2 + 1])); \ |
| 153 |
a = CAST_S_table0[(t >> 8) & 0xff]; \ |
| 154 |
b = CAST_S_table1[(t) & 0xff]; \ |
| 155 |
c = CAST_S_table2[(t >> 24) & 0xff]; \ |
| 156 |
d = CAST_S_table3[(t >> 16) & 0xff]; \ |
| 157 |
L ^= (((((a OP2 b) & 0xffffffffL) OP3 c) & 0xffffffffL) OP1 d) & 0xffffffffL; \ |
| 158 |
} |
| 159 |
#endif |
| 160 |
|
| 161 |
extern const CAST_LONG CAST_S_table0[256]; |
| 162 |
extern const CAST_LONG CAST_S_table1[256]; |
| 163 |
extern const CAST_LONG CAST_S_table2[256]; |
| 164 |
extern const CAST_LONG CAST_S_table3[256]; |
| 165 |
extern const CAST_LONG CAST_S_table4[256]; |
| 166 |
extern const CAST_LONG CAST_S_table5[256]; |
| 167 |
extern const CAST_LONG CAST_S_table6[256]; |
| 168 |
extern const CAST_LONG CAST_S_table7[256]; |