Changeset 13221 in project for release/3/cryptotools/trunk/cryptotools.scm
 Timestamp:
 02/09/09 13:31:09 (12 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

release/3/cryptotools/trunk/cryptotools.scm
r13197 r13221 4 4 (declare (export 5 5 blob>hexstring blob>hexstring/uppercase hexstring>blob 6 subblob blobxor blobpad blobunpad 7 makecbcencryptor makecbcdecryptor)) 6 blobxor blobpad blobunpad 7 makecbcencryptor makecbcdecryptor 8 makecbc*encryptor makecbc*decryptor)) 8 9 9 10 (define *thenullblob* (makeblob 0)) … … 190 191 (output (makeblob inputsize))) 191 192 (decrypt input 0 inputsize iv output 0))))) 193 194 ; As above, but the encryptor stores the IV in the output block (encrypted)... 195 196 (define (makecbc*encryptor encryptor blocksize) 197 (letrec ((encrypt (lambda (input inoffset inputsize iv output outoffset) 198 (cond 199 ((= inoffset inputsize) ; Zero bytes 200 (let* ((inblock (blobpad *thenullblob* blocksize)) 201 (outblock (encryptor (blobxor iv inblock)))) 202 (movememory! outblock output blocksize 0 outoffset) 203 output)) ; Terminate 204 ((<= (+ inoffset blocksize) inputsize) ; Just another block 205 (let* ((inblock (subblob input inoffset blocksize)) 206 (outblock (encryptor (blobxor iv inblock)))) 207 208 (movememory! outblock output blocksize 0 outoffset) 209 (encrypt input (+ inoffset blocksize) inputsize outblock output (+ outoffset blocksize)))) ; Recurse 210 (else ; Partial block 211 (let* ((inblock (blobpad 212 (subblob input inoffset ( inputsize inoffset)) 213 blocksize)) 214 (outblock (encryptor (blobxor iv inblock)))) 215 216 (movememory! outblock output blocksize 0 outoffset) 217 output)))))) ; Terminate 218 219 (lambda (input iv) 220 (let* ((inputsize (blobsize input)) 221 (outputwholeblocks (quotient inputsize blocksize)) 222 (outputoverflow (remainder inputsize blocksize)) 223 (outputsize (if (zero? outputoverflow) ; Round up to block size, plus an extra block for the IV 224 (+ inputsize blocksize blocksize) 225 (* (+ 2 outputwholeblocks) blocksize))) 226 (output (makeblob outputsize)) 227 (encryptediv (encryptor iv))) 228 (movememory! encryptediv output blocksize) 229 (encrypt input 0 inputsize iv output blocksize))))) 230 231 ;... and the decryptor retreives it. 232 233 (define (makecbc*decryptor decryptor blocksize) 234 (letrec ((decrypt (lambda (input inoffset inputsize iv output outoffset) 235 (if (= (+ inoffset blocksize) inputsize) 236 ; Last block 237 (let* ((inblock (subblob input inoffset blocksize)) 238 (outblock 239 (blobunpad 240 (blobxor iv 241 (decryptor inblock))))) 242 (movememory! outblock output (blobsize outblock) 0 outoffset) 243 (subblob output 0 (+ outoffset (blobsize outblock)))) ; Terminate 244 ; Not last block 245 (let* ((inblock (subblob input inoffset blocksize)) 246 (outblock 247 (blobxor iv 248 (decryptor inblock)))) 249 250 (movememory! outblock output blocksize 0 outoffset) 251 (decrypt input (+ inoffset blocksize) inputsize inblock output (+ outoffset blocksize))))))) ; Recurse 252 253 (lambda (input) 254 (let* ((inputsize (blobsize input)) 255 (output (makeblob inputsize)) 256 (iv (decryptor (subblob input 0 blocksize)))) 257 (decrypt input blocksize inputsize iv output 0)))))
Note: See TracChangeset
for help on using the changeset viewer.