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 {}
}
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 {
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"
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
+ }
}
}
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
}
assembly-overwrite ip ${?_header_checksum} [packet-csum-ip $ip]
- append $ip $body
+ append ip $body
return $ip
}
assembly-overwrite udp ${?_checksum} $csum
}
+
+
+
+
proc emit {count} {
global getlog_log errorInfo
if {[catch {