-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathConstantTime.xs
More file actions
58 lines (41 loc) · 923 Bytes
/
Copy pathConstantTime.xs
File metadata and controls
58 lines (41 loc) · 923 Bytes
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
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
static int do_compare(unsigned char *a, unsigned char *b, size_t n) {
size_t i;
unsigned char r = 0;
for (i = 0; i < n; i++) {
r |= *a++ ^ *b++;
}
return r;
}
MODULE = String::Compare::ConstantTime PACKAGE = String::Compare::ConstantTime
PROTOTYPES: ENABLE
int
equals(a, b)
SV *a
SV *b
CODE:
size_t alen;
unsigned char *ap;
size_t blen;
unsigned char *bp;
int r;
SvGETMAGIC(a);
SvGETMAGIC(b);
if (SvOK(a) && SvOK(b)) {
ap = SvPV(a, alen);
bp = SvPV(b, blen);
if (alen == blen) {
r = !do_compare(ap, bp, alen);
} else {
r = 0;
}
} else if (SvOK(a) || SvOK(b)) {
r = 0;
} else {
r = 1;
}
RETVAL = r;
OUTPUT:
RETVAL