| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include "common.h" |
| 24 |
#include "endianess.h" |
| 25 |
|
| 26 |
FAKE_INIT(ghash_portable) |
| 27 |
|
| 28 |
#define ALIGNMENT 32 |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
typedef uint64_t t_v_tables[128][2][2]; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
typedef struct { |
| 47 |
uint8_t buffer[sizeof(t_v_tables)+ALIGNMENT]; |
| 48 |
int offset; |
| 49 |
} t_exp_key; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
static void make_v_tables(const uint8_t h[16], t_v_tables *tables) |
| 57 |
{ |
| 58 |
uint64_t (*cur)[2]; |
| 59 |
int i; |
| 60 |
|
| 61 |
memset(tables, 0, sizeof(t_v_tables)); |
| 62 |
|
| 63 |
cur = &((*tables)[0][1]); |
| 64 |
|
| 65 |
(*cur)[0] = LOAD_U64_BIG(&h[0]); |
| 66 |
(*cur)[1] = LOAD_U64_BIG(&h[8]); |
| 67 |
|
| 68 |
for (i=1; i<128; i++) { |
| 69 |
uint64_t c; |
| 70 |
uint64_t (*next)[2]; |
| 71 |
|
| 72 |
next = &((*tables)[i][1]); |
| 73 |
|
| 74 |
|
| 75 |
c = (*cur)[1]&1 ? 0xE100000000000000ULL : 0; |
| 76 |
(*next)[1] = (*cur)[1]>>1 | (*cur)[0]<<63; |
| 77 |
(*next)[0] = (*cur)[0]>>1 ^ c; |
| 78 |
|
| 79 |
cur = next; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
static void gcm_mult2(uint8_t out[16], const t_v_tables *key_tables, const uint8_t x[16]) |
| 92 |
{ |
| 93 |
int i, bit_scan_128; |
| 94 |
uint64_t z[2]; |
| 95 |
|
| 96 |
z[0] = z[1] = 0; |
| 97 |
bit_scan_128 = 0; |
| 98 |
for (i=0; i<16; i++) { |
| 99 |
unsigned xi; |
| 100 |
int j; |
| 101 |
|
| 102 |
xi = x[i]; |
| 103 |
for (j=0; j<8; j++) { |
| 104 |
unsigned bit; |
| 105 |
|
| 106 |
bit = xi>>7 & 1; |
| 107 |
z[0] ^= (*key_tables)[bit_scan_128][bit][0]; |
| 108 |
z[1] ^= (*key_tables)[bit_scan_128][bit][1]; |
| 109 |
|
| 110 |
xi <<= 1; |
| 111 |
bit_scan_128++; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
STORE_U64_BIG(out, z[0]); |
| 116 |
STORE_U64_BIG(out+8, z[1]); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
EXPORT_SYM int ghash_portable( |
| 132 |
uint8_t y_out[16], |
| 133 |
const uint8_t block_data[], |
| 134 |
size_t len, |
| 135 |
const uint8_t y_in[16], |
| 136 |
const t_exp_key *exp_key |
| 137 |
) |
| 138 |
{ |
| 139 |
unsigned i; |
| 140 |
const t_v_tables *v_tables; |
| 141 |
|
| 142 |
if (NULL==y_out || NULL==block_data || NULL==y_in || NULL==exp_key) |
| 143 |
return ERR_NULL; |
| 144 |
|
| 145 |
if (len % 16) |
| 146 |
return ERR_NOT_ENOUGH_DATA; |
| 147 |
|
| 148 |
v_tables = (const t_v_tables*)(exp_key->buffer + exp_key->offset); |
| 149 |
|
| 150 |
memcpy(y_out, y_in, 16); |
| 151 |
for (i=0; i<len; i+=16) { |
| 152 |
unsigned j; |
| 153 |
uint8_t x[16]; |
| 154 |
|
| 155 |
for (j=0; j<16; j++) { |
| 156 |
x[j] = y_out[j] ^ block_data[i+j]; |
| 157 |
} |
| 158 |
gcm_mult2(y_out, v_tables, x); |
| 159 |
} |
| 160 |
|
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
EXPORT_SYM int ghash_expand_portable(const uint8_t h[16], t_exp_key **ghash_tables) |
| 168 |
{ |
| 169 |
t_exp_key *exp_key; |
| 170 |
|
| 171 |
if (NULL==h || NULL==ghash_tables) |
| 172 |
return ERR_NULL; |
| 173 |
|
| 174 |
*ghash_tables = exp_key = calloc(1, sizeof(t_exp_key)); |
| 175 |
if (NULL == exp_key) |
| 176 |
return ERR_MEMORY; |
| 177 |
|
| 178 |
exp_key->offset = ALIGNMENT - (int)((uintptr_t)exp_key->buffer & (ALIGNMENT-1)); |
| 179 |
make_v_tables(h, (t_v_tables*)(exp_key->buffer + exp_key->offset)); |
| 180 |
|
| 181 |
return 0; |
| 182 |
} |
| 183 |
|
| 184 |
EXPORT_SYM int ghash_destroy_portable(t_exp_key *ghash_tables) |
| 185 |
{ |
| 186 |
free(ghash_tables); |
| 187 |
return 0; |
| 188 |
} |