| 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 |
#include <stdlib.h> |
| 11 |
|
| 12 |
#undef c2l |
| 13 |
#define c2l(c, l) (l = ((unsigned long)(*((c)++))), \ |
| 14 |
l |= ((unsigned long)(*((c)++))) << 8L, \ |
| 15 |
l |= ((unsigned long)(*((c)++))) << 16L, \ |
| 16 |
l |= ((unsigned long)(*((c)++))) << 24L) |
| 17 |
|
| 18 |
/* NOTE - c is not incremented as per c2l */ |
| 19 |
#undef c2ln |
| 20 |
#define c2ln(c, l1, l2, n) \ |
| 21 |
{ \ |
| 22 |
c += n; \ |
| 23 |
l1 = l2 = 0; \ |
| 24 |
switch (n) { \ |
| 25 |
case 8: \ |
| 26 |
l2 = ((unsigned long)(*(--(c)))) << 24L; \ |
| 27 |
/* fall through */ \ |
| 28 |
case 7: \ |
| 29 |
l2 |= ((unsigned long)(*(--(c)))) << 16L; \ |
| 30 |
/* fall through */ \ |
| 31 |
case 6: \ |
| 32 |
l2 |= ((unsigned long)(*(--(c)))) << 8L; \ |
| 33 |
/* fall through */ \ |
| 34 |
case 5: \ |
| 35 |
l2 |= ((unsigned long)(*(--(c)))); \ |
| 36 |
/* fall through */ \ |
| 37 |
case 4: \ |
| 38 |
l1 = ((unsigned long)(*(--(c)))) << 24L; \ |
| 39 |
/* fall through */ \ |
| 40 |
case 3: \ |
| 41 |
l1 |= ((unsigned long)(*(--(c)))) << 16L; \ |
| 42 |
/* fall through */ \ |
| 43 |
case 2: \ |
| 44 |
l1 |= ((unsigned long)(*(--(c)))) << 8L; \ |
| 45 |
/* fall through */ \ |
| 46 |
case 1: \ |
| 47 |
l1 |= ((unsigned long)(*(--(c)))); \ |
| 48 |
} \ |
| 49 |
} |
| 50 |
|
| 51 |
#undef l2c |
| 52 |
#define l2c(l, c) (*((c)++) = (unsigned char)(((l)) & 0xff), \ |
| 53 |
*((c)++) = (unsigned char)(((l) >> 8L) & 0xff), \ |
| 54 |
*((c)++) = (unsigned char)(((l) >> 16L) & 0xff), \ |
| 55 |
*((c)++) = (unsigned char)(((l) >> 24L) & 0xff)) |
| 56 |
|
| 57 |
/* NOTE - c is not incremented as per l2c */ |
| 58 |
#undef l2cn |
| 59 |
#define l2cn(l1, l2, c, n) \ |
| 60 |
{ \ |
| 61 |
c += n; \ |
| 62 |
switch (n) { \ |
| 63 |
case 8: \ |
| 64 |
*(--(c)) = (unsigned char)(((l2) >> 24L) & 0xff); \ |
| 65 |
/* fall through */ \ |
| 66 |
case 7: \ |
| 67 |
*(--(c)) = (unsigned char)(((l2) >> 16L) & 0xff); \ |
| 68 |
/* fall through */ \ |
| 69 |
case 6: \ |
| 70 |
*(--(c)) = (unsigned char)(((l2) >> 8L) & 0xff); \ |
| 71 |
/* fall through */ \ |
| 72 |
case 5: \ |
| 73 |
*(--(c)) = (unsigned char)(((l2)) & 0xff); \ |
| 74 |
/* fall through */ \ |
| 75 |
case 4: \ |
| 76 |
*(--(c)) = (unsigned char)(((l1) >> 24L) & 0xff); \ |
| 77 |
/* fall through */ \ |
| 78 |
case 3: \ |
| 79 |
*(--(c)) = (unsigned char)(((l1) >> 16L) & 0xff); \ |
| 80 |
/* fall through */ \ |
| 81 |
case 2: \ |
| 82 |
*(--(c)) = (unsigned char)(((l1) >> 8L) & 0xff); \ |
| 83 |
/* fall through */ \ |
| 84 |
case 1: \ |
| 85 |
*(--(c)) = (unsigned char)(((l1)) & 0xff); \ |
| 86 |
} \ |
| 87 |
} |
| 88 |
|
| 89 |
#if (defined(OPENSSL_SYS_WIN32) && defined(_MSC_VER)) |
| 90 |
#define ROTATE_l32(a, n) _lrotl(a, n) |
| 91 |
#define ROTATE_r32(a, n) _lrotr(a, n) |
| 92 |
#elif defined(__ICC) |
| 93 |
#define ROTATE_l32(a, n) _rotl(a, n) |
| 94 |
#define ROTATE_r32(a, n) _rotr(a, n) |
| 95 |
#elif defined(__GNUC__) && __GNUC__ >= 2 && !defined(__STRICT_ANSI__) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC) |
| 96 |
#if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__) |
| 97 |
#define ROTATE_l32(a, n) ({ \ |
| 98 |
register unsigned int ret; \ |
| 99 |
asm("roll %%cl,%0" \ |
| 100 |
: "=r"(ret) \ |
| 101 |
: "c"(n), "0"((unsigned int)(a)) \ |
| 102 |
: "cc"); \ |
| 103 |
ret; \ |
| 104 |
}) |
| 105 |
#define ROTATE_r32(a, n) ({ \ |
| 106 |
register unsigned int ret; \ |
| 107 |
asm("rorl %%cl,%0" \ |
| 108 |
: "=r"(ret) \ |
| 109 |
: "c"(n), "0"((unsigned int)(a)) \ |
| 110 |
: "cc"); \ |
| 111 |
ret; \ |
| 112 |
}) |
| 113 |
#endif |
| 114 |
#endif |
| 115 |
#ifndef ROTATE_l32 |
| 116 |
#define ROTATE_l32(a, n) (((a) << (n & 0x1f)) | (((a) & 0xffffffff) >> ((32 - n) & 0x1f))) |
| 117 |
#endif |
| 118 |
#ifndef ROTATE_r32 |
| 119 |
#define ROTATE_r32(a, n) (((a) << ((32 - n) & 0x1f)) | (((a) & 0xffffffff) >> (n & 0x1f))) |
| 120 |
#endif |
| 121 |
|
| 122 |
#define RC5_32_MASK 0xffffffffL |
| 123 |
|
| 124 |
#define RC5_32_P 0xB7E15163L |
| 125 |
#define RC5_32_Q 0x9E3779B9L |
| 126 |
|
| 127 |
#define E_RC5_32(a, b, s, n) \ |
| 128 |
a ^= b; \ |
| 129 |
a = ROTATE_l32(a, b); \ |
| 130 |
a += s[n]; \ |
| 131 |
a &= RC5_32_MASK; \ |
| 132 |
b ^= a; \ |
| 133 |
b = ROTATE_l32(b, a); \ |
| 134 |
b += s[n + 1]; \ |
| 135 |
b &= RC5_32_MASK; |
| 136 |
|
| 137 |
#define D_RC5_32(a, b, s, n) \ |
| 138 |
b -= s[n + 1]; \ |
| 139 |
b &= RC5_32_MASK; \ |
| 140 |
b = ROTATE_r32(b, a); \ |
| 141 |
b ^= a; \ |
| 142 |
a -= s[n]; \ |
| 143 |
a &= RC5_32_MASK; \ |
| 144 |
a = ROTATE_r32(a, b); \ |
| 145 |
a ^= b; |