- collect
- `(setq ,a (mod32+ ,b (rol32 (mod32+ (mod32+ ,a (,op ,b ,c ,d))
- (mod32+ (aref ,block ,k)
- ,(aref *t* (1- i))))
- ,s)))
- into result
- finally
- (return `(progn ,@result))))
+ collect
+ `(setq ,a (mod32+ ,b (rol32 (mod32+ (mod32+ ,a (,op ,b ,c ,d))
+ (mod32+ (aref ,block ,k)
+ ,(aref *t* (1- i))))
+ ,s)))
+ into result
+ finally
+ (return `(progn ,@result))))
-(defun fill-block (block buffer offset)
- "Convert a complete 64 byte input vector segment into the given 16
-word MD5 block. This currently works on (unsigned-byte 8) and
-character simple-arrays, via the functions `fill-block-ub8' and
-`fill-block-char' respectively."
- (declare (type (integer 0 #.(- most-positive-fixnum 64)) offset)
- (type (simple-array ub32 (16)) block)
- (type (simple-array * (*)) buffer)
- (optimize (speed 3) (safety 0) (space 0) (debug 0)))
- (etypecase buffer
- ((simple-array (unsigned-byte 8) (*))
- (fill-block-ub8 block buffer offset))
- (simple-string
- (fill-block-char block buffer offset))))
(defun fill-block-ub8 (block buffer offset)
"Convert a complete 64 (unsigned-byte 8) input vector segment
starting from offset into the given 16 word MD5 block."
(declare (type (integer 0 #.(- most-positive-fixnum 64)) offset)
(defun fill-block-ub8 (block buffer offset)
"Convert a complete 64 (unsigned-byte 8) input vector segment
starting from offset into the given 16 word MD5 block."
(declare (type (integer 0 #.(- most-positive-fixnum 64)) offset)
(defun fill-block-char (block buffer offset)
"Convert a complete 64 character input string segment starting from
offset into the given 16 word MD5 block."
(declare (type (integer 0 #.(- most-positive-fixnum 64)) offset)
(defun fill-block-char (block buffer offset)
"Convert a complete 64 character input string segment starting from
offset into the given 16 word MD5 block."
(declare (type (integer 0 #.(- most-positive-fixnum 64)) offset)
- for j of-type (integer 0 #.most-positive-fixnum)
- from offset to (+ offset 63) by 4
- do
- (setf (aref block i)
- (assemble-ub32 (char-code (schar buffer j))
- (char-code (schar buffer (+ j 1)))
- (char-code (schar buffer (+ j 2)))
- (char-code (schar buffer (+ j 3)))))))
+ for j of-type (integer 0 #.most-positive-fixnum)
+ from offset to (+ offset 63) by 4
+ do
+ (setf (aref block i)
+ (assemble-ub32 (char-code (schar buffer j))
+ (char-code (schar buffer (+ j 1)))
+ (char-code (schar buffer (+ j 2)))
+ (char-code (schar buffer (+ j 3)))))))
+
+(defun fill-block (block buffer offset)
+ "Convert a complete 64 byte input vector segment into the given 16
+word MD5 block. This currently works on (unsigned-byte 8) and
+character simple-arrays, via the functions `fill-block-ub8' and
+`fill-block-char' respectively."
+ (declare (type (integer 0 #.(- most-positive-fixnum 64)) offset)
+ (type (simple-array ub32 (16)) block)
+ (type (simple-array * (*)) buffer)
+ (optimize (speed 3) (safety 0) (space 0) (debug 0)))
+ (etypecase buffer
+ ((simple-array (unsigned-byte 8) (*))
+ (fill-block-ub8 block buffer offset))
+ (simple-string
+ (fill-block-char block buffer offset))))
- (let ((var (gensym)))
- `(let ((,var ,reg))
- (declare (type ub32 ,var))
- (setf
- (aref result ,offset) (ldb (byte 8 0) ,var)
- (aref result ,(+ offset 1)) (ldb (byte 8 8) ,var)
- (aref result ,(+ offset 2)) (ldb (byte 8 16) ,var)
- (aref result ,(+ offset 3)) (ldb (byte 8 24) ,var))))))
+ (let ((var (gensym)))
+ `(let ((,var ,reg))
+ (declare (type ub32 ,var))
+ (setf
+ (aref result ,offset) (ldb (byte 8 0) ,var)
+ (aref result ,(+ offset 1)) (ldb (byte 8 8) ,var)
+ (aref result ,(+ offset 2)) (ldb (byte 8 16) ,var)
+ (aref result ,(+ offset 3)) (ldb (byte 8 24) ,var))))))
- (type (unsigned-byte 29) from-offset)
- (type (integer 0 63) count buffer-offset)
- (type (simple-array * (*)) from)
- (type (simple-array (unsigned-byte 8) (64)) buffer))
+ (type (unsigned-byte 29) from-offset)
+ (type (integer 0 63) count buffer-offset)
+ (type (simple-array * (*)) from)
+ (type (simple-array (unsigned-byte 8) (64)) buffer))
- for from-index of-type fixnum from from-offset
- below (+ from-offset count)
- do
- (setf (aref buffer buffer-index)
- (char-code (schar (the simple-string from) from-index)))))
+ for from-index of-type fixnum from from-offset
+ below (+ from-offset count)
+ do
+ (setf (aref buffer buffer-index)
+ (char-code (schar (the simple-string from) from-index)))))
- for from-index of-type fixnum from from-offset
- below (+ from-offset count)
- do
- (setf (aref buffer buffer-index)
- (aref (the (simple-array (unsigned-byte 8) (*)) from)
- from-index))))))
+ for from-index of-type fixnum from from-offset
+ below (+ from-offset count)
+ do
+ (setf (aref buffer buffer-index)
+ (aref (the (simple-array (unsigned-byte 8) (*)) from)
+ from-index))))))
(defun update-md5-state (state sequence &key (start 0) (end (length sequence)))
"Update the given md5-state from sequence, which is either a
simple-string or a simple-array with element-type (unsigned-byte 8),
bounded by start and end, which must be numeric bounding-indices."
(declare (type md5-state state)
(defun update-md5-state (state sequence &key (start 0) (end (length sequence)))
"Update the given md5-state from sequence, which is either a
simple-string or a simple-array with element-type (unsigned-byte 8),
bounded by start and end, which must be numeric bounding-indices."
(declare (type md5-state state)
- (type (simple-array * (*)) sequence)
- (type fixnum start end)
- (optimize (speed 3) #+cmu (safety 0) (space 0) (debug 0))
- #+cmu
- (ext:optimize-interface (safety 1) (debug 1)))
+ (type (simple-array * (*)) sequence)
+ (type fixnum start end)
+ (optimize (speed 3) #+cmu (safety 0) (space 0) (debug 0))
+ #+cmu
+ (ext:optimize-interface (safety 1) (debug 1)))
- (block (md5-state-block state))
- (buffer (md5-state-buffer state))
- (buffer-index (md5-state-buffer-index state))
- (length (- end start)))
- (declare (type md5-regs regs) (type fixnum length)
- (type (integer 0 63) buffer-index)
- (type (simple-array (unsigned-byte 32) (16)) block)
- (type (simple-array (unsigned-byte 8) (64)) buffer))
+ (block (md5-state-block state))
+ (buffer (md5-state-buffer state)))
+ (declare (type md5-regs regs)
+ (type (simple-array (unsigned-byte 32) (16)) block)
+ (type (simple-array (unsigned-byte 8) (64)) buffer))
- (unless (zerop buffer-index)
- (let ((amount (min (- 64 buffer-index) length)))
- (declare (type (integer 0 63) amount))
- (copy-to-buffer sequence start amount buffer buffer-index)
- (setq start (the fixnum (+ start amount)))
- (when (>= start end)
- (setf (md5-state-buffer-index state) (+ buffer-index amount))
- (return-from update-md5-state state)))
- (fill-block-ub8 block buffer 0)
- (update-md5-block regs block))
+ (unless (zerop (md5-state-buffer-index state))
+ (let* ((buffer-index (md5-state-buffer-index state))
+ (remainder (- 64 buffer-index))
+ (length (- end start))
+ (amount (min remainder length)))
+ (declare (type (integer 0 63) buffer-index remainder amount)
+ (type fixnum length))
+ (copy-to-buffer sequence start amount buffer buffer-index)
+ (setf (md5-state-amount state)
+ #-md5-small-length (+ (md5-state-amount state) amount)
+ #+md5-small-length (the (unsigned-byte 29)
+ (+ (md5-state-amount state) amount)))
+ (setq start (the fixnum (+ start amount)))
+ (if (< length remainder)
+ (setf (md5-state-buffer-index state)
+ (the (integer 0 63) (+ buffer-index amount)))
+ (progn
+ (fill-block-ub8 block buffer 0)
+ (update-md5-block regs block)
+ (setf (md5-state-buffer-index state) 0)))))
+ ;; Leave when nothing to do
+ (when (>= start end)
+ (return-from update-md5-state state))
- (declare (type (simple-array (unsigned-byte 8) (*)) sequence))
- (loop for offset of-type (unsigned-byte 29) from start below end by 64
- until (< (- end offset) 64)
- do
- (fill-block-ub8 block sequence offset)
- (update-md5-block regs block)
- finally
- (let ((amount (- end offset)))
- (unless (zerop amount)
- (copy-to-buffer sequence offset amount buffer 0))
- (setf (md5-state-buffer-index state) amount)))))
+ (declare (type (simple-array (unsigned-byte 8) (*)) sequence))
+ (loop for offset of-type (unsigned-byte 29) from start below end by 64
+ until (< (- end offset) 64)
+ do
+ (fill-block-ub8 block sequence offset)
+ (update-md5-block regs block)
+ finally
+ (let ((amount (- end offset)))
+ (unless (zerop amount)
+ (copy-to-buffer sequence offset amount buffer 0))
+ (setf (md5-state-buffer-index state) amount)))))
- (declare (type simple-string sequence))
- (loop for offset of-type (unsigned-byte 29) from start below end by 64
- until (< (- end offset) 64)
- do
- (fill-block-char block sequence offset)
- (update-md5-block regs block)
- finally
- (let ((amount (- end offset)))
- (unless (zerop amount)
- (copy-to-buffer sequence offset amount buffer 0))
- (setf (md5-state-buffer-index state) amount))))))
+ (declare (type simple-string sequence))
+ (loop for offset of-type (unsigned-byte 29) from start below end by 64
+ until (< (- end offset) 64)
+ do
+ (fill-block-char block sequence offset)
+ (update-md5-block regs block)
+ finally
+ (let ((amount (- end offset)))
+ (unless (zerop amount)
+ (copy-to-buffer sequence offset amount buffer 0))
+ (setf (md5-state-buffer-index state) amount))))))
- #-md5-small-length (+ (md5-state-amount state) length)
- #+md5-small-length (the (unsigned-byte 29)
- (+ (md5-state-amount state) length)))
+ #-md5-small-length (+ (md5-state-amount state)
+ (the fixnum (- end start)))
+ #+md5-small-length (the (unsigned-byte 29)
+ (+ (md5-state-amount state)
+ (the fixnum (- end start)))))
- (block (md5-state-block state))
- (buffer (md5-state-buffer state))
- (buffer-index (md5-state-buffer-index state))
- (total-length (* 8 (md5-state-amount state))))
- (declare (type md5-regs regs)
- (type (integer 0 63) buffer-index)
- (type (simple-array ub32 (16)) block)
- (type (simple-array (unsigned-byte 8) (*)) buffer))
- ;; Add mandatory bit 1 padding
- (setf (aref buffer buffer-index) #x80)
- ;; Fill with 0 bit padding
- (loop for index of-type (integer 0 64)
- from (1+ buffer-index) below 64
- do (setf (aref buffer index) #x00))
- (fill-block-ub8 block buffer 0)
- ;; Flush block first if length wouldn't fit
- (when (>= buffer-index 56)
- (update-md5-block regs block)
- ;; Create new fully 0 padded block
- (loop for index of-type (integer 0 16) from 0 below 16
- do (setf (aref block index) #x00000000)))
- ;; Add 64bit message bit length
- (setf (aref block 14) (ldb (byte 32 0) total-length))
- #-md5-small-length
- (setf (aref block 15) (ldb (byte 32 32) total-length))
- ;; Flush last block
- (update-md5-block regs block)
- ;; Done, remember digest for later calls
- (setf (md5-state-finalized-p state)
- (md5regs-digest regs)))))
+ (block (md5-state-block state))
+ (buffer (md5-state-buffer state))
+ (buffer-index (md5-state-buffer-index state))
+ (total-length (* 8 (md5-state-amount state))))
+ (declare (type md5-regs regs)
+ (type (integer 0 63) buffer-index)
+ (type (simple-array ub32 (16)) block)
+ (type (simple-array (unsigned-byte 8) (*)) buffer))
+ ;; Add mandatory bit 1 padding
+ (setf (aref buffer buffer-index) #x80)
+ ;; Fill with 0 bit padding
+ (loop for index of-type (integer 0 64)
+ from (1+ buffer-index) below 64
+ do (setf (aref buffer index) #x00))
+ (fill-block-ub8 block buffer 0)
+ ;; Flush block first if length wouldn't fit
+ (when (>= buffer-index 56)
+ (update-md5-block regs block)
+ ;; Create new fully 0 padded block
+ (loop for index of-type (integer 0 16) from 0 below 16
+ do (setf (aref block index) #x00000000)))
+ ;; Add 64bit message bit length
+ (setf (aref block 14) (ldb (byte 32 0) total-length))
+ #-md5-small-length
+ (setf (aref block 15) (ldb (byte 32 32) total-length))
+ ;; Flush last block
+ (update-md5-block regs block)
+ ;; Done, remember digest for later calls
+ (setf (md5-state-finalized-p state)
+ (md5regs-digest regs)))))
- :element-type '(unsigned-byte 8))))
- (declare (type (simple-array (unsigned-byte 8) (#.+buffer-size+))
- buffer))
- (loop for bytes of-type buffer-index = (read-sequence buffer stream)
- do (update-md5-state state buffer :end bytes)
- until (< bytes +buffer-size+)
- finally
- (return (finalize-md5-state state)))))
+ :element-type '(unsigned-byte 8))))
+ (declare (type (simple-array (unsigned-byte 8) (#.+buffer-size+))
+ buffer))
+ (loop for bytes of-type buffer-index = (read-sequence buffer stream)
+ do (update-md5-state state buffer :end bytes)
+ until (< bytes +buffer-size+)
+ finally
+ (return (finalize-md5-state state)))))
- (declare (type (simple-string #.+buffer-size+) buffer))
- (loop for bytes of-type buffer-index = (read-sequence buffer stream)
- do (update-md5-state state buffer :end bytes)
- until (< bytes +buffer-size+)
- finally
- (return (finalize-md5-state state)))))
+ (declare (type (simple-string #.+buffer-size+) buffer))
+ (loop for bytes of-type buffer-index = (read-sequence buffer stream)
+ do (update-md5-state state buffer :end bytes)
+ until (< bytes +buffer-size+)
+ finally
+ (return (finalize-md5-state state)))))
- for (source . md5-string) in testsuite
- for md5-digest = (md5sum-sequence source)
- for md5-result-string = (format nil "~(~{~2,'0X~}~)"
- (map 'list #'identity md5-digest))
- do
- (format
- *trace-output*
- "~2&Test-Case ~D:~% Input: ~S~% Required: ~A~% Returned: ~A~%"
- count source md5-string md5-result-string)
- when (string= md5-string md5-result-string)
- do (format *trace-output* " OK~%")
- else
- count 1 into failed
- and do (format *trace-output* " FAILED~%")
- finally
- (format *trace-output*
- "~2&~[All ~D test cases succeeded~:;~:*~D of ~D test cases failed~].~%"
- failed (1- count))
- (return (zerop failed))))
+ for (source . md5-string) in testsuite
+ for md5-digest = (md5sum-sequence source)
+ for md5-result-string = (format nil "~(~{~2,'0X~}~)"
+ (map 'list #'identity md5-digest))
+ do
+ (format
+ *trace-output*
+ "~2&Test-Case ~D:~% Input: ~S~% Required: ~A~% Returned: ~A~%"
+ count source md5-string md5-result-string)
+ when (string= md5-string md5-result-string)
+ do (format *trace-output* " OK~%")
+ else
+ count 1 into failed
+ and do (format *trace-output* " FAILED~%")
+ finally
+ (format *trace-output*
+ "~2&~[All ~D test cases succeeded~:;~:*~D of ~D test cases failed~].~%"
+ failed (1- count))
+ (return (zerop failed))))