forked from resilar/crchack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforge.c
124 lines (111 loc) · 3.37 KB
/
forge.c
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "forge.h"
#define FLIP_BIT(msg, idx) ((msg)[(idx)/8] ^= 1 << ((idx) % 8))
int forge(const u8 *msg, size_t length,
void (*H)(const u8 *msg, size_t length, struct bigint *out),
struct bigint *checksum, size_t bits[], int bits_size, u8 *buf)
{
struct bigint *AT, Hmsg, x, d, acc, mask;
size_t width;
int i, j, p;
int ret;
/* Transpose of a (width x bits_size) size matrix A */
width = checksum->bits;
AT = malloc(bits_size * sizeof(struct bigint));
if (!AT) return -(width+1);
/* A[i] = H(msg ^ bits[i]) ^ H(msg) */
bigint_init(&Hmsg, width);
H(msg, length, &Hmsg);
if (msg != buf)
memcpy(buf, msg, length);
for (i = 0; i < bits_size; i++) {
FLIP_BIT(buf, bits[i]);
bigint_init(&AT[i], width);
H(buf, length, &AT[i]);
bigint_xor(&AT[i], &Hmsg);
FLIP_BIT(buf, bits[i]);
}
/* d = checksum ^ H(msg) */
bigint_init(&d, width);
bigint_mov(&d, checksum);
bigint_xor(&d, &Hmsg);
/**
* Solve x from Ax = d
*/
p = 0;
bigint_init(&x, width);
bigint_load_zeros(&x);
bigint_init(&acc, width);
bigint_init(&mask, width);
bigint_load_zeros(&mask);
for (i = 0; i < width; i++) {
/* Find next pivot (row with a non-zero column i) */
for (j = p; j < bits_size; j++) {
if (bigint_get_bit(&AT[j], i)) {
/* Swap rows j and p */
size_t tmp = bits[j];
bits[j] = bits[p];
bits[p] = tmp;
bigint_swap(&AT[j], &AT[p]);
break;
}
}
if (j < bits_size) {
/* Pivot found */
/* Zero out column i in rows below pivot */
for (j = p+1; j < bits_size; j++) {
if (bigint_get_bit(&AT[j], i)) {
bigint_xor(&AT[j], &AT[p]);
/* For backtracking */
bigint_set_bit(&AT[j], p);
}
}
if (bigint_get_bit(&d, i)) {
/* d ^= AT[p] & ~((1 << i)-1) */
bigint_mov(&acc, &mask);
bigint_not(&acc);
bigint_and(&acc, &AT[p]);
bigint_xor(&d, &acc);
/* x ^= (1 << p) ^ (AT[p] & ((1 << i)-1)) */
bigint_xor(&acc, &AT[p]);
bigint_flip_bit(&acc, p);
bigint_xor(&x, &acc);
}
p++;
} else {
/* No pivot (zero column). Need more bits! */
if (bigint_get_bit(&d, i))
break;
}
/* mask = (1 << i)-1 */
bigint_shl_1(&mask);
bigint_set_lsb(&mask);
}
/* Adjust the input message */
if (i >= width) {
ret = 0;
for (i = 0; i < width; i++) {
if (bigint_get_bit(&x, i)) {
FLIP_BIT(buf, bits[i]);
if (ret != i) {
size_t tmp = bits[i];
bits[i] = bits[ret];
bits[ret] = tmp;
}
ret++;
}
}
} else {
ret = -(width-i);
}
/* Cleanup */
bigint_destroy(&mask);
bigint_destroy(&acc);
bigint_destroy(&d);
bigint_destroy(&x);
bigint_destroy(&Hmsg);
for (i = 0; i < bits_size; i++)
bigint_destroy(&AT[i]);
free(AT);
return ret;
}
#undef FLIP_BIT