-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate-ctr-drbg-vectors
executable file
·73 lines (70 loc) · 2.24 KB
/
generate-ctr-drbg-vectors
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
#! /usr/bin/perl -w
use strict;
our $n = 0;
sub h {
my $name = shift;
my $value = shift;
++$n;
print " static const uint8_t ${name}_${n}[] = { ";
print join(", ", map("0x$_", ($value =~ /../g)));
print "};\n";
return "${name}_${n}";
};
our $v;
our %u = ();
while(<>) {
s/\r//;
if(/^# *(.*)/) {
print " /* $1 */\n";
}
if(/\[AES-(\d+) no df\]/) {
$v->{bits} = $1;
next;
} elsif(/\[AES|\[3Key/) {
undef $v;
next;
}
next unless defined $v;
if(/\[(\S+) = (\S+)\]/) {
$v->{$1} = $2 if defined $v;
next;
}
if(/(\S+) = (\S+)/) {
$u{$1} = $2;
if($1 eq 'ReturnedBits') {
if(0) {
print " /* ";
print join(" ", map("$_=$v->{$_}", sort keys %$v));
print "*/\n";
print " /* ";
print join(" ", map("$_=$u{$_}", sort keys %u));
print "*/\n";
}
if(!exists $u{'AdditionalInput'}) {
printf " /* %s:%d */\n", $ARGV, $.;
print " {\n";
my $entropy = h('entropy', $u{'EntropyInput'});
my $returned = h('returned', $u{'ReturnedBits'});
my $bits = $v->{bits};
printf " uint8_t buffer\[%d];\n",$v->{ReturnedBitsLen} / 8;
print " AesCtrDrbg$bits rng;\n";
if(exists $u{'PersonalizationString'}) {
my $personalization = h('personalization',
$u{'PersonalizationString'});
printf " rng.instantiate($entropy, $personalization, %d);\n",
length($u{'PersonalizationString'}) / 2;
} else {
print " rng.instantiate($entropy, NULL, 0);\n";
}
printf " rng.stream(buffer, %d);\n",
$v->{ReturnedBitsLen} / 8;
printf " rng.stream(buffer, %d);\n",
$v->{ReturnedBitsLen} / 8;
printf " assert(!memcmp(buffer, $returned, %d));\n",
$v->{ReturnedBitsLen} / 8;
print " }\n";
}
%u = ();
}
}
}