[stupid-crypto] 6 new revisions pushed by ben@links.org on 2010-04-02 14:52 GMT

2 views
Skip to first unread message

stupid...@googlecode.com

unread,
Apr 2, 2010, 10:53:34 AM4/2/10
to stupi...@googlegroups.com
6 new revisions:

Revision: 333e83d2cb
Author: Ben Laurie <b...@links.org>
Date: Fri Apr 2 06:55:48 2010
Log: Switch to using here document.
http://code.google.com/p/stupid-crypto/source/detail?r=333e83d2cb

Revision: b5fbd4b46e
Author: Ben Laurie <b...@links.org>
Date: Fri Apr 2 07:32:31 2010
Log: Handle run-time failures.
http://code.google.com/p/stupid-crypto/source/detail?r=b5fbd4b46e

Revision: b507333919
Author: Ben Laurie <b...@links.org>
Date: Fri Apr 2 07:33:12 2010
Log: Start testing for overflow.
http://code.google.com/p/stupid-crypto/source/detail?r=b507333919

Revision: 91dac0e67f
Author: Ben Laurie <b...@links.org>
Date: Fri Apr 2 07:37:56 2010
Log: Start checking 32-bit overflows.
http://code.google.com/p/stupid-crypto/source/detail?r=91dac0e67f

Revision: 5fdb47fb12
Author: Ben Laurie <b...@links.org>
Date: Fri Apr 2 07:49:18 2010
Log: Add wrapplus32.
http://code.google.com/p/stupid-crypto/source/detail?r=5fdb47fb12

Revision: 2cf232ad7f
Author: Ben Laurie <b...@links.org>
Date: Fri Apr 2 07:52:36 2010
Log: Add 32 bit overflow test.
http://code.google.com/p/stupid-crypto/source/detail?r=2cf232ad7f

==============================================================================
Revision: 333e83d2cb
Author: Ben Laurie <b...@links.org>
Date: Fri Apr 2 06:55:48 2010
Log: Switch to using here document.
http://code.google.com/p/stupid-crypto/source/detail?r=333e83d2cb

Modified:
/src/Stupid/C.pm

=======================================
--- /src/Stupid/C.pm Tue Mar 30 02:51:04 2010
+++ /src/Stupid/C.pm Fri Apr 2 06:55:48 2010
@@ -7,20 +7,22 @@
sub Stupid::LanguageWrapper::emitCode {
my $self = shift;

- print "#include <sys/types.h>\n";
-
- print "#ifdef __APPLE__\n";
- print "typedef u_int32_t uint32;\n";
- print "typedef u_int8_t uint8;\n";
- print "#else\n";
- print "typedef uint32_t uint32;\n";
- print "typedef uint8_t uint8;\n";
- print "#endif\n";
-
- print "typedef struct {\n";
- print " void (*put)(void *info, uint8 ch);\n";
- print " void *info;\n";
- print "} stupid_ostream;\n";
+ print <<EOC;
+#include <sys/types.h>
+
+#ifdef __APPLE__
+typedef u_int32_t uint32;
+typedef u_int8_t uint8;
+#else
+typedef uint32_t uint32;
+typedef uint8_t uint8;
+#endif
+
+typedef struct {
+ void (*put)(void *info, uint8 ch);
+ void *info;
+} stupid_ostream;
+EOC

$self->{tree}->emitCode();
}

==============================================================================
Revision: b5fbd4b46e
Author: Ben Laurie <b...@links.org>
Date: Fri Apr 2 07:32:31 2010
Log: Handle run-time failures.
http://code.google.com/p/stupid-crypto/source/detail?r=b5fbd4b46e

Modified:
/test/test.pl

=======================================
--- /test/test.pl Sat Mar 20 10:58:37 2010
+++ /test/test.pl Fri Apr 2 07:32:31 2010
@@ -5,6 +5,7 @@
use Getopt::Long;
use Carp;
use File::Slurp;
+use IPC::Run qw(run);

$| = 1;

@@ -33,10 +34,16 @@
$status = '-BUILD-FAIL';
$output = '';
} else {
- open(my $f, "generated/$language/$base |");
- $status = '';
- $output = read_file($f);
- close $f;
+ my @cmd;
+ $cmd[0] = "generated/$language/$base";
+ my $err;
+ my $ok = run \@cmd, \undef, \$output, \$err;
+ if($ok) {
+ $status = '';
+ } else {
+ $status = '-RUN-FAIL';
+ $output = $err;
+ }
}
if($expect_status ne $status) {
print "FAIL (expected status $expect_status, got $status)";

==============================================================================
Revision: b507333919
Author: Ben Laurie <b...@links.org>
Date: Fri Apr 2 07:33:12 2010
Log: Start testing for overflow.
http://code.google.com/p/stupid-crypto/source/detail?r=b507333919

Added:
/test/overflow8.stupid
Modified:
/src/Stupid/C.pm

=======================================
--- /dev/null
+++ /test/overflow8.stupid Fri Apr 2 07:33:12 2010
@@ -0,0 +1,10 @@
+"EXPECT-RUN-FAIL:129 plus8 128 overflows
+";
+
+function() test() {
+ uint8 a = 129;
+ uint8 b = 128;
+ uint8 c = 0;
+
+ c = a plus8 b;
+}
=======================================
--- /src/Stupid/C.pm Fri Apr 2 06:55:48 2010
+++ /src/Stupid/C.pm Fri Apr 2 07:33:12 2010
@@ -9,6 +9,9 @@

print <<EOC;
#include <sys/types.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>

#ifdef __APPLE__
typedef u_int32_t uint32;
@@ -22,6 +25,22 @@
void (*put)(void *info, uint8 ch);
void *info;
} stupid_ostream;
+
+void stupid_fatal(const char * const fmt, ...) {
+ va_list vl;
+
+ va_start(vl, fmt);
+ vfprintf(stderr, fmt, vl);
+ fputc('\\n', stderr);
+ exit(1);
+}
+
+uint8 plus8(const uint8 a, const uint8 b) {
+ uint8 t = a + b;
+ if(t < a)
+ stupid_fatal("%d plus8 %d overflows", a, b);
+ return t;
+}
EOC

$self->{tree}->emitCode();
@@ -776,9 +795,9 @@
sub Stupid::Plus8::emitCode {
my $self = shift;

- print '(';
+ print 'plus8(';
$self->{left}->emitCode();
- print ' + ';
+ print ', ';
$self->{right}->emitCode();
print ')';
}

==============================================================================
Revision: 91dac0e67f
Author: Ben Laurie <b...@links.org>
Date: Fri Apr 2 07:37:56 2010
Log: Start checking 32-bit overflows.
http://code.google.com/p/stupid-crypto/source/detail?r=91dac0e67f

Modified:
/src/Stupid/C.pm
/test/test.pl

=======================================
--- /src/Stupid/C.pm Fri Apr 2 07:33:12 2010
+++ /src/Stupid/C.pm Fri Apr 2 07:37:56 2010
@@ -41,6 +41,13 @@
stupid_fatal("%d plus8 %d overflows", a, b);
return t;
}
+
+uint32 plus32(const uint32 a, const uint32 b) {
+ uint32 t = a + b;
+ if(t < a)
+ stupid_fatal("%u plus32 %u overflows", a, b);
+ return t;
+}
EOC

$self->{tree}->emitCode();
@@ -805,9 +812,9 @@
sub Stupid::Plus32::emitCode {
my $self = shift;

- print '(';
+ print 'plus32(';
$self->{left}->emitCode();
- print ' + ';
+ print ', ';
$self->{right}->emitCode();
print ')';
}
=======================================
--- /test/test.pl Fri Apr 2 07:32:31 2010
+++ /test/test.pl Fri Apr 2 07:37:56 2010
@@ -46,7 +46,7 @@
}
}
if($expect_status ne $status) {
- print "FAIL (expected status $expect_status, got $status)";
+ print "FAIL (expected status $expect_status, got $status with output
$output)";
++$failed;
} elsif($expect_output ne $output) {
print "FAIL (expected '$expect_output', got '$output')";

==============================================================================
Revision: 5fdb47fb12
Author: Ben Laurie <b...@links.org>
Date: Fri Apr 2 07:49:18 2010
Log: Add wrapplus32.
http://code.google.com/p/stupid-crypto/source/detail?r=5fdb47fb12

Modified:
/src/Stupid/C.pm
/src/grammar.y
/src/stupid.pl
/test/compilet1.stupid
/test/sha256-struct.stupid

=======================================
--- /src/Stupid/C.pm Fri Apr 2 07:37:56 2010
+++ /src/Stupid/C.pm Fri Apr 2 07:49:18 2010
@@ -818,6 +818,16 @@
$self->{right}->emitCode();
print ')';
}
+
+sub Stupid::WrapPlus32::emitCode {
+ my $self = shift;
+
+ print '(';
+ $self->{left}->emitCode();
+ print ' + ';
+ $self->{right}->emitCode();
+ print ')';
+}

sub Stupid::RRotate32::emitCode {
my $self = shift;
=======================================
--- /src/grammar.y Sun Mar 21 10:29:19 2010
+++ /src/grammar.y Fri Apr 2 07:49:18 2010
@@ -117,6 +117,8 @@
{ new Stupid::Plus8($_[1], $_[3]); }
| expr 'plus32' expr
{ new Stupid::Plus32($_[1], $_[3]); }
+ | expr 'wrapplus32' expr
+ { new Stupid::WrapPlus32($_[1], $_[3]); }
| expr 'rrotate32' expr
{ new Stupid::RRotate32($_[1], $_[3]); }
| expr 'rshift32' expr
=======================================
--- /src/stupid.pl Sun Mar 21 10:29:19 2010
+++ /src/stupid.pl Fri Apr 2 07:49:18 2010
@@ -50,7 +50,7 @@
function struct if else while
and8 and32 band bor eq32 ge8 le8 lshift32 lshift8 mask32to8 minus8 minus32
mod8 mod32 ne32 ne8 not32 not8 or8 plus8 plus32 rrotate32 rshift32
widen8to32
- xor32);
+ wrapplus32 xor32);
}

sub lexer {
@@ -1139,7 +1139,30 @@

use strict;

-# Unsigned decimal value, any length
+sub new {
+ my $class = shift;
+ my $l = shift;
+ my $r = shift;
+
+ my $self = {};
+ bless $self, $class;
+
+ $self->{left} = $l;
+ $self->{right} = $r;
+
+ return $self;
+}
+
+sub value {
+ my $self = shift;
+
+ # FIXME type and overflow checking
+ $self->{left}->value()->badd($self->{right}->value());
+}
+
+package Stupid::WrapPlus32;
+
+use strict;

sub new {
my $class = shift;
@@ -1166,8 +1189,6 @@

use strict;

-# Unsigned decimal value, any length
-
sub new {
my $class = shift;
my $l = shift;
=======================================
--- /test/compilet1.stupid Sun Mar 21 05:09:59 2010
+++ /test/compilet1.stupid Fri Apr 2 07:49:18 2010
@@ -145,7 +145,7 @@
while(i ne32 64) {
s0 = (w[i minus32 15] rrotate32 7) xor32 (w[i minus32 15] rrotate32
18) xor32 (w[i minus32 15] rshift32 3);
s1 = (w[i minus32 2] rrotate32 17) xor32 (w[i minus32 2] rrotate32 19)
xor32 (w[i minus32 2] rshift32 10);
- w[i] = w[i minus32 16] plus32 s0 plus32 w[i minus32 7] plus32 s1;
+ w[i] = w[i minus32 16] wrapplus32 s0 wrapplus32 w[i minus32 7]
wrapplus32 s1;
i = i plus32 1;
}

@@ -166,31 +166,31 @@
while(i ne32 64) {
s0 = (a rrotate32 2) xor32 (a rrotate32 13) xor32 (a rrotate32 22);
maj = (a and32 b) xor32 (a and32 c) xor32 (b and32 c);
- t2 = s0 plus32 maj;
+ t2 = s0 wrapplus32 maj;
s1 = (e rrotate32 6) xor32 (e rrotate32 11) xor32 (e rrotate32 25);
ch = (e and32 f) xor32 ((not32 e) and32 g);
- t1 = h plus32 s1 plus32 ch plus32 k[i] plus32 w[i];
+ t1 = h wrapplus32 s1 wrapplus32 ch wrapplus32 k[i] wrapplus32 w[i];
h = g;
g = f;
f = e;
- e = d plus32 t1;
+ e = d wrapplus32 t1;
d = c;
c = b;
b = a;
- a = t1 plus32 t2;
+ a = t1 wrapplus32 t2;
i = i plus32 1;
}

" Add this chunk's hash to result so far:";

-h0 = h0 plus32 a;
-h1 = h1 plus32 b;
-h2 = h2 plus32 c;
-h3 = h3 plus32 d;
-h4 = h4 plus32 e;
-h5 = h5 plus32 f;
-h6 = h6 plus32 g;
-h7 = h7 plus32 h;
+h0 = h0 wrapplus32 a;
+h1 = h1 wrapplus32 b;
+h2 = h2 wrapplus32 c;
+h3 = h3 wrapplus32 d;
+h4 = h4 wrapplus32 e;
+h5 = h5 wrapplus32 f;
+h6 = h6 wrapplus32 g;
+h7 = h7 wrapplus32 h;

"end of outer loop (when we do it)";

=======================================
--- /test/sha256-struct.stupid Sun Mar 21 10:55:01 2010
+++ /test/sha256-struct.stupid Fri Apr 2 07:49:18 2010
@@ -76,7 +76,7 @@
while(i ne32 64) {
s0 = (w[i minus32 15] rrotate32 7) xor32 (w[i minus32 15] rrotate32
18) xor32 (w[i minus32 15] rshift32 3);
s1 = (w[i minus32 2] rrotate32 17) xor32 (w[i minus32 2] rrotate32 19)
xor32 (w[i minus32 2] rshift32 10);
- w[i] = w[i minus32 16] plus32 s0 plus32 w[i minus32 7] plus32 s1;
+ w[i] = w[i minus32 16] wrapplus32 s0 wrapplus32 w[i minus32 7]
wrapplus32 s1;
i = i plus32 1;
}

@@ -97,31 +97,31 @@
while(i ne32 64) {
s0 = (a rrotate32 2) xor32 (a rrotate32 13) xor32 (a rrotate32 22);
maj = (a and32 b) xor32 (a and32 c) xor32 (b and32 c);
- t2 = s0 plus32 maj;
+ t2 = s0 wrapplus32 maj;
s1 = (e rrotate32 6) xor32 (e rrotate32 11) xor32 (e rrotate32 25);
ch = (e and32 f) xor32 ((not32 e) and32 g);
- t1 = h plus32 s1 plus32 ch plus32 k[i] plus32 w[i];
+ t1 = h wrapplus32 s1 wrapplus32 ch wrapplus32 k[i] wrapplus32 w[i];
h = g;
g = f;
f = e;
- e = d plus32 t1;
+ e = d wrapplus32 t1;
d = c;
c = b;
b = a;
- a = t1 plus32 t2;
+ a = t1 wrapplus32 t2;
i = i plus32 1;
}

" Add this chunk's hash to result so far:";

-out.h[0] = in.h[0] plus32 a;
-out.h[1] = in.h[1] plus32 b;
-out.h[2] = in.h[2] plus32 c;
-out.h[3] = in.h[3] plus32 d;
-out.h[4] = in.h[4] plus32 e;
-out.h[5] = in.h[5] plus32 f;
-out.h[6] = in.h[6] plus32 g;
-out.h[7] = in.h[7] plus32 h;
+out.h[0] = in.h[0] wrapplus32 a;
+out.h[1] = in.h[1] wrapplus32 b;
+out.h[2] = in.h[2] wrapplus32 c;
+out.h[3] = in.h[3] wrapplus32 d;
+out.h[4] = in.h[4] wrapplus32 e;
+out.h[5] = in.h[5] wrapplus32 f;
+out.h[6] = in.h[6] wrapplus32 g;
+out.h[7] = in.h[7] wrapplus32 h;
}

function (array(uint8, 32) output) sha256_final(struct sha256 state_in,

==============================================================================
Revision: 2cf232ad7f
Author: Ben Laurie <b...@links.org>
Date: Fri Apr 2 07:52:36 2010
Log: Add 32 bit overflow test.
http://code.google.com/p/stupid-crypto/source/detail?r=2cf232ad7f

Added:
/test/overflow32.stupid

=======================================
--- /dev/null
+++ /test/overflow32.stupid Fri Apr 2 07:52:36 2010
@@ -0,0 +1,10 @@
+"EXPECT-RUN-FAIL:305419896 plus32 4275878552 overflows
+";
+
+function() test() {
+ uint32 a = 0x12345678;
+ uint32 b = 0xfedcba98;
+ uint32 c = 0;
+
+ c = a plus32 b;
+}

Reply all
Reply to author
Forward
0 new messages