From: ian Date: Sat, 2 Mar 2002 15:52:36 +0000 (+0000) Subject: Use openssl bf-ofb instead of md5sum repeatedly. Make random be own namespace. X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=b9810123714164abb8b92455ae6fc2d16ba5ba99;p=vinegar-ip.git Use openssl bf-ofb instead of md5sum repeatedly. Make random be own namespace. --- diff --git a/make-probes.tcl b/make-probes.tcl index 2fb83be..8782f1a 100755 --- a/make-probes.tcl +++ b/make-probes.tcl @@ -18,9 +18,7 @@ proc manyset {list args} { proc start_gen {use_gen_counter} { global gen_counter rand_counter getlog_log rand_buf - set gen_counter $use_gen_counter - set rand_counter 0 - set rand_buf {} + random-bytes-init $use_gen_counter set getlog_log {} } @@ -35,24 +33,33 @@ proc packet-csum-ip {packet} { return [expr {$cs & 0xffff}] } -set rand_buf {} -proc random-bytes {n} { - global gen_counter rand_counter rand_buf - set n [expr {$n*2}] - set o {} - while {[set w [expr {$n - [string length $o]}]] > 0} { - if {![string length $rand_buf]} { - set md5 [exec md5sum << "$gen_counter $rand_counter"] - regexp {^[0-9a-f]{32}} $md5 rand_buf - if {![string length $rand_buf]} { error "?$md5?" } - incr rand_counter + +namespace eval Random-Bytes { + namespace export random-bytes random-bytes-init + + proc random-bytes-init {seed} { + variable counter + variable fh + catch { set h $fh; unset fh; close $h } + set counter 0 + set fh [open |[list openssl bf-ofb < /dev/zero -e -k " $seed"] r] + fconfigure $fh -translation binary + } + proc random-bytes {n} { + variable fh + set x [read $fh $n] + if {[string length $x] != $n} { + set h $fh; unset fh; close $h + error "openssl bf-ofb exited unexpectedly" } - append o [string range $rand_buf 0 [expr {$w-1}]] - set rand_buf [string range $rand_buf $w end] + binary scan $x H* y + if {[string length $y] != $n*2} { error "binary format failed $n $y" } + return $y } - return $o } +namespace import Random-Bytes::* + proc choice-int {min max} { set rv 0x[random-bytes 3] return [expr { @@ -186,7 +193,7 @@ proc get/v4addr {s v} { for {set i 0} {$i<4} {incr i} { set b [random-bytes 1] append rv $b - append p $d [format %d $b] + append p $d [format %d 0x$b] set d . } getlog "$v=$p" @@ -312,22 +319,30 @@ proc assembly-overwrite {outvarname location value} { if {$bitlen < 32 && $value >= (1<<$bitlen)} { error "$diag $value >= 2**$bitlen" } - while {$bitlen > 0} { - set byteno [expr {$bit1 / 8}] - set char0no [expr {$byteno*2}] - set char1no [expr {$char0no+1}] - set bytebit [expr {128>>($bit1 % 8)}] - set byte 0x[string range $out $char0no $char1no] - debug 7 "< $bit1+$bitlen $byteno $char0no $bytebit $byte $out" - set byte [expr { - ($value & (1<<($bitlen-1))) - ? ($byte | $bytebit) - : ($byte & ~$bytebit) - }] - set out [string replace $out $char0no $char1no [format %02x $byte]] + if {!($bit1 % 8) && !($bitlen % 8)} { + set char0no [expr {$bit1/4}] + set charlen [expr {$bitlen/4}] + set chareno [expr {$char0no + $charlen -1}] + set repl [format %0${charlen}x $value] + set out [string replace $out $char0no $chareno $repl] + } else { + while {$bitlen > 0} { + set byteno [expr {$bit1 / 8}] + set char0no [expr {$byteno*2}] + set char1no [expr {$char0no+1}] + set bytebit [expr {128>>($bit1 % 8)}] + set byte 0x[string range $out $char0no $char1no] + debug 7 "< $bit1+$bitlen $byteno $char0no $bytebit $byte $out" + set byte [expr { + ($value & (1<<($bitlen-1))) + ? ($byte | $bytebit) + : ($byte & ~$bytebit) + }] + set out [string replace $out $char0no $char1no [format %02x $byte]] debug 8 "> $bit1+$bitlen $byteno $char0no $bytebit 0x[format %02x $byte] $out" - incr bitlen -1 - incr bit1 + incr bitlen -1 + incr bit1 + } } } @@ -373,7 +388,7 @@ proc gen_1_ip {mtu} { +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ } assembly-overwrite ip ${?_header_checksum} [packet-csum-ip $ip] - append $ip $body + append ip $body return $ip } @@ -528,6 +543,10 @@ define ip-proto 17 udp {mtu} { assembly-overwrite udp ${?_checksum} $csum } + + + + proc emit {count} { global getlog_log errorInfo if {[catch {