43 lines
1020 B
C
43 lines
1020 B
C
#include "rsa.h"
|
|
|
|
void array_set_random_bytes(uint32_t *n, size_t size) {
|
|
int fd = open("/dev/urandom", O_RDONLY);
|
|
if (read(fd, n, size) == -1) {
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
void array_set_msb_and_lsb_to_one(uint32_t *n, size_t size) {
|
|
n[0] |= 1;
|
|
n[size / sizeof(uint32_t) - 1] |= 1 << 31;
|
|
}
|
|
|
|
void array_bitwise_right_shift(uint32_t *a, size_t len) {
|
|
size_t size = sizeof(uint32_t) * 8 - 1;
|
|
for (size_t n = 0; n < len - 1; n++) {
|
|
a[n] = a[n] >> 1 | (a[n + 1] & 1) << size;
|
|
}
|
|
a[len - 1] >>= 1;
|
|
}
|
|
|
|
void array_bitwise_left_shift(uint32_t *a, size_t len) {
|
|
size_t size = sizeof(uint32_t) * 8 - 1;
|
|
for (size_t n = len - 1; n > 0; n--) {
|
|
a[n] = a[n] << 1 | ((a[n - 1] & (1 << size)) >> size);
|
|
}
|
|
a[0] <<= 1;
|
|
}
|
|
|
|
// Will underflow
|
|
void array_decrement(uint32_t *a, size_t len) {
|
|
size_t cursor = 0;
|
|
size_t size = sizeof(uint32_t) * 8;
|
|
while (cursor < size * len) {
|
|
a[cursor / size] = a[cursor / size] ^ (1 << (cursor % size));
|
|
if (((a[cursor / size] >> (cursor % size)) & 1) == 0) {
|
|
return;
|
|
}
|
|
cursor += 1;
|
|
}
|
|
}
|