| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include <stddef.h> |
| 33 |
#include <string.h> |
| 34 |
#include <assert.h> |
| 35 |
#include <stdio.h> |
| 36 |
#include <stdlib.h> |
| 37 |
#include "common.h" |
| 38 |
#include "endianess.h" |
| 39 |
|
| 40 |
FAKE_INIT(modexp) |
| 41 |
|
| 42 |
#include "mont.h" |
| 43 |
#include "modexp_utils.h" |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
#define WINDOW_SIZE 4 |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
EXPORT_SYM int monty_pow( |
| 64 |
uint8_t *out, |
| 65 |
const uint8_t *base, |
| 66 |
const uint8_t *exp, |
| 67 |
const uint8_t *modulus, |
| 68 |
size_t len, |
| 69 |
uint64_t seed) |
| 70 |
{ |
| 71 |
unsigned i, j; |
| 72 |
size_t exp_len; |
| 73 |
int res; |
| 74 |
|
| 75 |
MontContext *ctx = NULL; |
| 76 |
uint64_t *powers[1 << WINDOW_SIZE] = { NULL }; |
| 77 |
uint64_t *power_idx = NULL; |
| 78 |
ProtMemory *prot = NULL; |
| 79 |
uint64_t *mont_base = NULL; |
| 80 |
uint64_t *x = NULL; |
| 81 |
uint64_t *scratchpad = NULL; |
| 82 |
uint8_t *buf_out = NULL; |
| 83 |
|
| 84 |
struct BitWindow_LR bit_window; |
| 85 |
|
| 86 |
if (!base || !exp || !modulus || !out) |
| 87 |
return ERR_NULL; |
| 88 |
|
| 89 |
if (len == 0) |
| 90 |
return ERR_NOT_ENOUGH_DATA; |
| 91 |
|
| 92 |
|
| 93 |
res = mont_context_init(&ctx, modulus, len); |
| 94 |
if (res) |
| 95 |
return res; |
| 96 |
|
| 97 |
for (i=0; i<(1 << WINDOW_SIZE); i++) { |
| 98 |
res = mont_number(powers+i, 1, ctx); |
| 99 |
if (res) goto cleanup; |
| 100 |
} |
| 101 |
|
| 102 |
res = mont_number(&power_idx, 1, ctx); |
| 103 |
if (res) goto cleanup; |
| 104 |
|
| 105 |
res = mont_from_bytes(&mont_base, base, len, ctx); |
| 106 |
if (res) goto cleanup; |
| 107 |
|
| 108 |
res = mont_number(&x, 1, ctx); |
| 109 |
if (res) goto cleanup; |
| 110 |
|
| 111 |
res = mont_number(&scratchpad, SCRATCHPAD_NR, ctx); |
| 112 |
if (res) goto cleanup; |
| 113 |
|
| 114 |
buf_out = (uint8_t*)calloc(1, mont_bytes(ctx)); |
| 115 |
if (NULL == buf_out) { |
| 116 |
res = ERR_MEMORY; |
| 117 |
goto cleanup; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
mont_set(x, 1, ctx); |
| 122 |
|
| 123 |
|
| 124 |
mont_copy(powers[0], x, ctx); |
| 125 |
mont_copy(powers[1], mont_base, ctx); |
| 126 |
for (i=1; i<(1 << (WINDOW_SIZE-1)); i++) { |
| 127 |
mont_mult(powers[i*2], powers[i], powers[i], scratchpad, ctx); |
| 128 |
mont_mult(powers[i*2+1], powers[i*2], mont_base, scratchpad, ctx); |
| 129 |
} |
| 130 |
|
| 131 |
res = scatter(&prot, (const void**)powers, 1<<WINDOW_SIZE, mont_bytes(ctx), seed); |
| 132 |
if (res) goto cleanup; |
| 133 |
|
| 134 |
|
| 135 |
exp_len = len; |
| 136 |
for (i=0; i<len && *exp==0; i++) { |
| 137 |
exp_len--; |
| 138 |
exp++; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
if (exp_len == 0) { |
| 143 |
memset(out, 0, len); |
| 144 |
out[len-1] = 1; |
| 145 |
res = 0; |
| 146 |
goto cleanup; |
| 147 |
} |
| 148 |
|
| 149 |
bit_window = init_bit_window_lr(WINDOW_SIZE, exp, exp_len); |
| 150 |
|
| 151 |
|
| 152 |
for (i=0; i < bit_window.nr_windows; i++) { |
| 153 |
unsigned index; |
| 154 |
|
| 155 |
for (j=0; j<WINDOW_SIZE; j++) { |
| 156 |
mont_mult(x, x, x, scratchpad, ctx); |
| 157 |
} |
| 158 |
|
| 159 |
index = get_next_digit_lr(&bit_window); |
| 160 |
gather(power_idx, prot, index); |
| 161 |
mont_mult(x, x, power_idx, scratchpad, ctx); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
res = mont_to_bytes(out, len, x, ctx); |
| 166 |
|
| 167 |
cleanup: |
| 168 |
mont_context_free(ctx); |
| 169 |
for (i=0; i<(1 << WINDOW_SIZE); i++) { |
| 170 |
free(powers[i]); |
| 171 |
} |
| 172 |
free(power_idx); |
| 173 |
free_scattered(prot); |
| 174 |
free(mont_base); |
| 175 |
free(x); |
| 176 |
free(scratchpad); |
| 177 |
free(buf_out); |
| 178 |
|
| 179 |
return res; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
EXPORT_SYM int monty_multiply( |
| 195 |
uint8_t *out, |
| 196 |
const uint8_t *term1, |
| 197 |
const uint8_t *term2, |
| 198 |
const uint8_t *modulus, |
| 199 |
size_t len) |
| 200 |
{ |
| 201 |
MontContext *ctx = NULL; |
| 202 |
uint64_t *mont_term1 = NULL; |
| 203 |
uint64_t *mont_term2 = NULL; |
| 204 |
uint64_t *mont_output = NULL; |
| 205 |
uint64_t *scratchpad = NULL; |
| 206 |
int res; |
| 207 |
|
| 208 |
if (!term1 || !term2 || !modulus || !out) |
| 209 |
return ERR_NULL; |
| 210 |
|
| 211 |
if (len == 0) |
| 212 |
return ERR_NOT_ENOUGH_DATA; |
| 213 |
|
| 214 |
|
| 215 |
res = mont_context_init(&ctx, modulus, len); |
| 216 |
if (res) |
| 217 |
return res; |
| 218 |
|
| 219 |
res = mont_from_bytes(&mont_term1, term1, len, ctx); |
| 220 |
if (res) goto cleanup; |
| 221 |
|
| 222 |
res = mont_from_bytes(&mont_term2, term2, len, ctx); |
| 223 |
if (res) goto cleanup; |
| 224 |
|
| 225 |
res = mont_number(&mont_output, 1, ctx); |
| 226 |
if (res) goto cleanup; |
| 227 |
|
| 228 |
res = mont_number(&scratchpad, SCRATCHPAD_NR, ctx); |
| 229 |
if (res) goto cleanup; |
| 230 |
|
| 231 |
|
| 232 |
res = mont_mult(mont_output, mont_term1, mont_term2, scratchpad, ctx); |
| 233 |
if (res) goto cleanup; |
| 234 |
|
| 235 |
res = mont_to_bytes(out, len, mont_output, ctx); |
| 236 |
|
| 237 |
cleanup: |
| 238 |
mont_context_free(ctx); |
| 239 |
free(mont_term1); |
| 240 |
free(mont_term2); |
| 241 |
free(mont_output); |
| 242 |
free(scratchpad); |
| 243 |
|
| 244 |
return res; |
| 245 |
} |
| 246 |
|
| 247 |
#ifdef MAIN |
| 248 |
int main(void) |
| 249 |
{ |
| 250 |
uint16_t length; |
| 251 |
uint8_t *base, *modulus, *exponent, *out; |
| 252 |
int result; |
| 253 |
size_t res; |
| 254 |
|
| 255 |
res = fread(&length, 2, 1, stdin); |
| 256 |
assert(res == 2); |
| 257 |
|
| 258 |
base = malloc(length); |
| 259 |
modulus = malloc(length); |
| 260 |
exponent = malloc(length); |
| 261 |
out = malloc(length); |
| 262 |
|
| 263 |
res = fread(base, 1, length, stdin); |
| 264 |
assert(res == length); |
| 265 |
res = fread(modulus, 1, length, stdin); |
| 266 |
assert(res == length); |
| 267 |
res = fread(exponent, 1, length, stdin); |
| 268 |
assert(res == length); |
| 269 |
assert(res == length); |
| 270 |
res = fread(out, 1, length, stdin); |
| 271 |
|
| 272 |
result = monty_pow(out, base, exponent, modulus, length, 12); |
| 273 |
|
| 274 |
free(base); |
| 275 |
free(modulus); |
| 276 |
free(exponent); |
| 277 |
free(out); |
| 278 |
|
| 279 |
return result; |
| 280 |
} |
| 281 |
|
| 282 |
#endif |
| 283 |
|
| 284 |
#ifdef PROFILE |
| 285 |
int main(void) |
| 286 |
{ |
| 287 |
uint8_t base[256], exponent[256], modulus[256], out[256]; |
| 288 |
unsigned length = 256, i, j; |
| 289 |
|
| 290 |
for (i=0; i<256; i++) { |
| 291 |
base[i] = (uint8_t)i | 0x80 | 1; |
| 292 |
exponent[i] = base[i] = modulus[i] = base[i]; |
| 293 |
} |
| 294 |
base[0] = 0x7F; |
| 295 |
|
| 296 |
for (j=0; j<50; j++) { |
| 297 |
monty_pow(out, base, exponent, modulus, length, 12); |
| 298 |
} |
| 299 |
|
| 300 |
monty_multiply(out, base, out, modulus, length); |
| 301 |
} |
| 302 |
#endif |