Tcl Library Source Code

Ticket Change Details
Login
Overview

Artifact ID: cf88f383af61e77183a7c5ebb2e689683b4d58ca32f17f36c585384bf3e7009d
Ticket: d56da1abcaa028bed473ff5ffbd814f41909c5f5
Incorrect padding of data when using input channel
User & Date: anonymous 2019-06-06 08:45:42
Changes

  1. assignee changed to: ""
  2. icomment:
    --- blowfish-1.0.4.tcl	2019-06-06 09:52:21 +0200
    +++ blowfish-1.0.5.tcl	2019-06-06 10:02:41 +0200
    @@ -509,18 +509,27 @@
         if {[eof $in]} {
             fileevent $in readable {}
             set state(reading) 0
    -    }
    -
    -    set data [read $in $chunksize]
    -    # FIX ME: we should ony pad after eof
    -    if {[string length $pad] > 0} {
    -        set data [Pad $data 8]
    -    }
    -
    -    if {$out == {}} {
    -        append state(output) [$state(cmd) $Key $data]
    +        set data $state(remainder)
    +        # Only pad at the end of the stream.
    +        if {[string length $pad] > 0} {
    +            set data [Pad $data 8 $pad]
    +        }
         } else {
    -        puts -nonewline $out [$state(cmd) $Key $data]
    +        set data [read $in $chunksize]
    +        puts "Chunk: reading [string len $data] bytes"
    +        set data $state(remainder)$data
    +        # If data is not a multiple of 8, state(remainder) will hold
    +        # excess bytes for the next round.
    +        set pagedlen [expr {([string length $data] / 8) * 8}]
    +        set state(remainder) [string range $data $pagedlen end]
    +        set data [string range $data 0 $pagedlen-1]
    +    }
    +    if {[string length $data] > 0} {
    +        if {$out == {}} {
    +            append state(output) [$state(cmd) $Key $data]
    +        } else {
    +            puts -nonewline $out [$state(cmd) $Key $data]
    +        }
         }
     }
     
    @@ -585,7 +594,7 @@
     proc ::blowfish::Pad {data blocksize {fill \0}} {
         set len [string length $data]
         if {$len == 0} {
    -        set data [string repeat $fill $blocksize]
    +        # do not pad an empty string
         } elseif {($len % $blocksize) != 0} {
             set pad [expr {$blocksize - ($len % $blocksize)}]
             append data [string repeat $fill $pad]
    @@ -682,6 +691,7 @@
                 set state(cmd) Decrypt
             }
             set state(output) ""
    +        set state(remainder) ""
             fileevent $opts(-in) readable \
                 [list [namespace origin Chunk] \
                      $Key $opts(-in) $opts(-out) $opts(-chunksize) $opts(-pad)]
    
  3. login: "anonymous"
  4. mimetype: "text/plain"