unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* [PATCH] md5: fix errors when input size modulo 64 is > 55 bytes
@ 2013-01-12  6:43 Daniel Hartwig
  2013-01-12  6:56 ` Daniel Hartwig
  2013-01-21 21:16 ` Andy Wingo
  0 siblings, 2 replies; 7+ messages in thread
From: Daniel Hartwig @ 2013-01-12  6:43 UTC (permalink / raw)
  To: guile-devel

[-- Attachment #1: Type: text/plain, Size: 1286 bytes --]


Originally reported as <http://bugs.debian.org/437214>.

Triggered when input has size modulo 64 is 56–63 bytes.

scheme@(guile-user)> (use-modules (md5))
scheme@(guile-user)> (md5 (open-input-string (make-string 60 #\0)))
ERROR: In procedure string-set!:
ERROR: In procedure string-set!: Wrong type argument in position 1 (expecting string): #f

Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
scheme@(guile-user) [1]> ,bt
In /usr/share/guile/site/md5.scm:
   349:10  1 (md5-finalize ((values (a . 1732584193) (b . 4023233417) (c . 2562383102) (. #)) # …))
In unknown file:
           0 (string-set! #f #f #\200)

Two errors in that section of the file.  The first is a simple typo:
context → 'context.

The second required looking up the libgcrypt source (the basis of this
module) to repair.  Lines 322–380 of md5.scm are equivalent to lines
288–301 of cipher/md5.c.  Attached patch repairs this part of the file
based on the algorithm in libgcrypt.

$ guile -c '(display (make-string 60 #\0))' | md5sum
5b19445b70b493c78f3bc06eb7962315  -
$ guile
[…]
scheme@(guile-user)> (use-modules (md5))
scheme@(guile-user)> (md5 (open-input-string (make-string 60 #\0)))
$1 = "5b19445b70b493c78f3bc06eb7962315"

Regards


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-md5-fix-errors-when-input-size-modulo-64-is-55-bytes.patch --]
[-- Type: text/x-diff, Size: 1409 bytes --]

From 47c92db862ce846dbcc5d27843bc9d26b7708d5d Mon Sep 17 00:00:00 2001
From: Daniel Hartwig <mandyke@gmail.com>
Date: Sat, 12 Jan 2013 14:34:26 +0800
Subject: [PATCH] md5: fix errors when input size modulo 64 is > 55 bytes

* src/md5.scm (md5-finalize): Fix typos and missing parts of algorithm
  based on cipher/md5.c in libgcrypt.
---
 src/md5.scm |   12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/md5.scm b/src/md5.scm
index 571c857..1acc78b 100644
--- a/src/md5.scm
+++ b/src/md5.scm
@@ -347,11 +347,17 @@ referenced C implementation into Scheme.
 					   'data-size)
 				 1)))))
 	(begin
-	  (string-set! (assq-ref (assq-ref 'context 'buffer)
+	  (string-set! (assq-ref (assq-ref context 'buffer)
 				 'space)
-		       (assq-ref (assq-ref 'context 'buffer)
+		       (assq-ref (assq-ref context 'buffer)
 				 'data-size)
 		       (integer->char #x80))
+	  (assq-set! (assq-ref context 'buffer)
+		     'data-size
+		     (+ (assq-ref (assq-ref context 'buffer)
+				  'data-size)
+			1))
+
 	  (while (< (assq-ref (assq-ref context 'buffer)
 			      'data-size)
 		    64)
@@ -360,7 +366,7 @@ referenced C implementation into Scheme.
 					  'space)
 				(assq-ref (assq-ref context 'buffer)
 					  'data-size)
-				0)
+				#\nul)
 		   (assq-set! (assq-ref context 'buffer)
 			      'data-size
 			      (+ (assq-ref (assq-ref context 'buffer)
-- 
1.7.10.4


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] md5: fix errors when input size modulo 64 is > 55 bytes
  2013-01-12  6:43 [PATCH] md5: fix errors when input size modulo 64 is > 55 bytes Daniel Hartwig
@ 2013-01-12  6:56 ` Daniel Hartwig
  2013-01-12  8:35   ` Nala Ginrut
  2013-01-21 21:16 ` Andy Wingo
  1 sibling, 1 reply; 7+ messages in thread
From: Daniel Hartwig @ 2013-01-12  6:56 UTC (permalink / raw)
  To: guile-devel

On 12 January 2013 14:43, Daniel Hartwig <mandyke@gmail.com> wrote:
>
> Originally reported as <http://bugs.debian.org/437214>.
>
> Triggered when input has size modulo 64 is 56–63 bytes.
>
> scheme@(guile-user)> (use-modules (md5))

… from guile-lib, of course!



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] md5: fix errors when input size modulo 64 is > 55 bytes
  2013-01-12  6:56 ` Daniel Hartwig
@ 2013-01-12  8:35   ` Nala Ginrut
  0 siblings, 0 replies; 7+ messages in thread
From: Nala Ginrut @ 2013-01-12  8:35 UTC (permalink / raw)
  To: Daniel Hartwig; +Cc: guile-devel

On Sat, 2013-01-12 at 14:56 +0800, Daniel Hartwig wrote:
> On 12 January 2013 14:43, Daniel Hartwig <mandyke@gmail.com> wrote:
> >
> > Originally reported as <http://bugs.debian.org/437214>.
> >
> > Triggered when input has size modulo 64 is 56–63 bytes.
> >
> > scheme@(guile-user)> (use-modules (md5))
> 
> … from guile-lib, of course!
> 

Maybe I'm off topic. ;-)
I suggest add a 'digest' module implemented in C code in Guile to
provide common see 'digest algorithm', since these things are very
useful nowadays. I believe they should be in ice-9. Like Ruby does.
I've started a project guile-digest. I'll send patch if it's done.





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] md5: fix errors when input size modulo 64 is > 55 bytes
  2013-01-12  6:43 [PATCH] md5: fix errors when input size modulo 64 is > 55 bytes Daniel Hartwig
  2013-01-12  6:56 ` Daniel Hartwig
@ 2013-01-21 21:16 ` Andy Wingo
  2013-01-31  5:31   ` Daniel Hartwig
  1 sibling, 1 reply; 7+ messages in thread
From: Andy Wingo @ 2013-01-21 21:16 UTC (permalink / raw)
  To: Daniel Hartwig; +Cc: guile-devel

Any chance on getting a test case as well? :-)
-- 
http://wingolog.org/



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] md5: fix errors when input size modulo 64 is > 55 bytes
  2013-01-21 21:16 ` Andy Wingo
@ 2013-01-31  5:31   ` Daniel Hartwig
  2013-01-31  5:31     ` Daniel Hartwig
  2013-01-31 10:09     ` Andy Wingo
  0 siblings, 2 replies; 7+ messages in thread
From: Daniel Hartwig @ 2013-01-31  5:31 UTC (permalink / raw)
  To: guile-devel

[-- Attachment #1: Type: text/plain, Size: 220 bytes --]

On 22 January 2013 05:16, Andy Wingo <wingo@pobox.com> wrote:
> Any chance on getting a test case as well? :-)
> --
> http://wingolog.org/

Updated with tests from RFC 1321.  Confirmed that the new test fails pre-patch.

[-- Attachment #2: 0001-md5-fix-errors-when-input-size-modulo-64-is-55-bytes.patch --]
[-- Type: application/octet-stream, Size: 3147 bytes --]

From 3547fe846b7ea1035a9f4a338ee25f446f410ce0 Mon Sep 17 00:00:00 2001
From: Daniel Hartwig <mandyke@gmail.com>
Date: Sat, 12 Jan 2013 14:34:26 +0800
Subject: [PATCH] md5: fix errors when input size modulo 64 is > 55 bytes

* src/md5.scm (md5-finalize): Fix typos and missing parts of algorithm
  based on cipher/md5.c in libgcrypt.
* unit-tests/md5.scm: Include tests from RFC 1321.
---
 src/md5.scm        |   12 +++++++++---
 unit-tests/md5.scm |   20 ++++++++++++++++++--
 2 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/src/md5.scm b/src/md5.scm
index 571c857..1acc78b 100644
--- a/src/md5.scm
+++ b/src/md5.scm
@@ -347,11 +347,17 @@ referenced C implementation into Scheme.
 					   'data-size)
 				 1)))))
 	(begin
-	  (string-set! (assq-ref (assq-ref 'context 'buffer)
+	  (string-set! (assq-ref (assq-ref context 'buffer)
 				 'space)
-		       (assq-ref (assq-ref 'context 'buffer)
+		       (assq-ref (assq-ref context 'buffer)
 				 'data-size)
 		       (integer->char #x80))
+	  (assq-set! (assq-ref context 'buffer)
+		     'data-size
+		     (+ (assq-ref (assq-ref context 'buffer)
+				  'data-size)
+			1))
+
 	  (while (< (assq-ref (assq-ref context 'buffer)
 			      'data-size)
 		    64)
@@ -360,7 +366,7 @@ referenced C implementation into Scheme.
 					  'space)
 				(assq-ref (assq-ref context 'buffer)
 					  'data-size)
-				0)
+				#\nul)
 		   (assq-set! (assq-ref context 'buffer)
 			      'data-size
 			      (+ (assq-ref (assq-ref context 'buffer)
diff --git a/unit-tests/md5.scm b/unit-tests/md5.scm
index bea76f7..06f532a 100644
--- a/unit-tests/md5.scm
+++ b/unit-tests/md5.scm
@@ -26,8 +26,18 @@
                #:init-value "The quick brown fox.")
   ;; this answer generated with /usr/bin/md5 for comparison purposes...
   (test-answer #:getter test-answer 
-               #:init-value  "2e87284d245c2aae1c74fa4c50a74c77"))
-
+               #:init-value  "2e87284d245c2aae1c74fa4c50a74c77")
+
+  ;; These digests from RFC 1321 test suite.
+  (test-digests #:getter test-digests
+                #:init-value
+   '(("" . "d41d8cd98f00b204e9800998ecf8427e")
+     ("a" . "0cc175b9c0f1b6a831c399e269772661")
+     ("abc" . "900150983cd24fb0d6963f7d28e17f72")
+     ("message digest" . "f96b697d7cb7938d525a2f31aaf161d0")
+     ("abcdefghijklmnopqrstuvwxyz" . "c3fcd3d76192e4007dfb496cca67e13b")
+     ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" . "d174ab98d277d9f5a5611c2c9f419d9f")
+     ("12345678901234567890123456789012345678901234567890123456789012345678901234567890" . "57edf4a22be3c955ac49da2e2107b67a"))))
 
 (define-method (test-default-port (self <test-md5>))
   (assert-equal (test-answer self) 
@@ -38,6 +48,12 @@
   (assert-equal (test-answer self) 
                 (md5 (open-input-string (test-string self)))))
 
+(define-method (test-rfc (self <test-md5>))
+  (for-each (lambda (pair)
+              (assert-equal (cdr pair)
+                            (md5 (open-input-string (car pair)))))
+            (test-digests self)))
+
 (exit-with-summary (run-all-defined-test-cases))
 
 ;;; arch-tag: 0D9E8711-F9E7-11D8-AE52-000A95CD5044
-- 
1.7.10.4


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] md5: fix errors when input size modulo 64 is > 55 bytes
  2013-01-31  5:31   ` Daniel Hartwig
@ 2013-01-31  5:31     ` Daniel Hartwig
  2013-01-31 10:09     ` Andy Wingo
  1 sibling, 0 replies; 7+ messages in thread
From: Daniel Hartwig @ 2013-01-31  5:31 UTC (permalink / raw)
  To: guile-devel

On 31 January 2013 13:31, Daniel Hartwig <mandyke@gmail.com> wrote:
> On 22 January 2013 05:16, Andy Wingo <wingo@pobox.com> wrote:
>> Any chance on getting a test case as well? :-)
>> --
>> http://wingolog.org/
>
> Updated with tests from RFC 1321.  Confirmed that the new test fails pre-patch.

Please excuse the very long lines of test data.  Couldn't figure out
how to make that any nicer :-)



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] md5: fix errors when input size modulo 64 is > 55 bytes
  2013-01-31  5:31   ` Daniel Hartwig
  2013-01-31  5:31     ` Daniel Hartwig
@ 2013-01-31 10:09     ` Andy Wingo
  1 sibling, 0 replies; 7+ messages in thread
From: Andy Wingo @ 2013-01-31 10:09 UTC (permalink / raw)
  To: Daniel Hartwig; +Cc: guile-devel

On Thu 31 Jan 2013 06:31, Daniel Hartwig <mandyke@gmail.com> writes:

> On 22 January 2013 05:16, Andy Wingo <wingo@pobox.com> wrote:
>> Any chance on getting a test case as well? :-)
>> --
>> http://wingolog.org/
>
> Updated with tests from RFC 1321.  Confirmed that the new test fails pre-patch.

Applied, thanks!

Andy
-- 
http://wingolog.org/



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2013-01-31 10:09 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-01-12  6:43 [PATCH] md5: fix errors when input size modulo 64 is > 55 bytes Daniel Hartwig
2013-01-12  6:56 ` Daniel Hartwig
2013-01-12  8:35   ` Nala Ginrut
2013-01-21 21:16 ` Andy Wingo
2013-01-31  5:31   ` Daniel Hartwig
2013-01-31  5:31     ` Daniel Hartwig
2013-01-31 10:09     ` Andy Wingo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).