From b7ef541f354a4d664391d2b38b27bda01860f72d Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 14 Oct 2009 09:33:15 -0700 Subject: [PATCH] toss crypto/block Digest in favor of hash.Hash R=r DELTA=30 (8 added, 15 deleted, 7 changed) OCL=35677 CL=35713 --- src/pkg/Make.deps | 2 +- src/pkg/crypto/block/cipher.go | 15 --------------- src/pkg/crypto/block/cmac.go | 11 +++++++++-- src/pkg/crypto/block/eax.go | 9 +++++---- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/pkg/Make.deps b/src/pkg/Make.deps index 99c9755232..19942a9d99 100644 --- a/src/pkg/Make.deps +++ b/src/pkg/Make.deps @@ -13,7 +13,7 @@ container/list.install: container/ring.install: container/vector.install: crypto/aes.install: os.install strconv.install -crypto/block.install: fmt.install io.install os.install strconv.install +crypto/block.install: fmt.install hash.install io.install os.install strconv.install crypto/hmac.install: crypto/md5.install crypto/sha1.install hash.install os.install crypto/md5.install: hash.install os.install crypto/rc4.install: os.install strconv.install diff --git a/src/pkg/crypto/block/cipher.go b/src/pkg/crypto/block/cipher.go index ed6db02f7b..8bff1b8782 100644 --- a/src/pkg/crypto/block/cipher.go +++ b/src/pkg/crypto/block/cipher.go @@ -8,8 +8,6 @@ // and NIST Special Publication 800-38A. package block -import "io" - // A Cipher represents an implementation of block cipher // using a given key. It provides the capability to encrypt // or decrypt individual blocks. The mode implementations @@ -27,19 +25,6 @@ type Cipher interface { Decrypt(src, dst []byte); } -// TODO(rsc): Digest belongs elsewhere. - -// A Digest is an implementation of a message digest algorithm. -// Write data to it and then call Sum to retreive the digest. -// Calling Reset resets the internal state, as though no data has -// been written. -type Digest interface { - io.Writer; - Sum() []byte; - Reset(); -} - - // Utility routines func shift1(src, dst []byte) byte { diff --git a/src/pkg/crypto/block/cmac.go b/src/pkg/crypto/block/cmac.go index ff8d0b0fbe..667dc7b3e5 100644 --- a/src/pkg/crypto/block/cmac.go +++ b/src/pkg/crypto/block/cmac.go @@ -7,7 +7,10 @@ package block -import "os" +import ( + "hash"; + "os"; +) const ( // minimal irreducible polynomial of degree b @@ -25,7 +28,7 @@ type cmac struct { // NewCMAC returns a new instance of a CMAC message authentication code // digest using the given Cipher. -func NewCMAC(c Cipher) Digest { +func NewCMAC(c Cipher) hash.Hash { var r byte; n := c.BlockSize(); switch n { @@ -98,3 +101,7 @@ func (d *cmac) Sum() []byte { d.c.Encrypt(d.digest, d.digest); return d.digest; } + +func (d *cmac) Size() int { + return len(d.digest); +} diff --git a/src/pkg/crypto/block/eax.go b/src/pkg/crypto/block/eax.go index e6dc39195c..3992ea8f7a 100644 --- a/src/pkg/crypto/block/eax.go +++ b/src/pkg/crypto/block/eax.go @@ -16,6 +16,7 @@ package block import ( "fmt"; + "hash"; "io"; "os"; ) @@ -32,7 +33,7 @@ func (e *EAXTagError) String() string { return fmt.Sprintf("crypto/block: EAX tag mismatch: read %x but computed %x", e.Read, e.Computed); } -func setupEAX(c Cipher, iv, hdr []byte, tagBytes int) (ctrIV, tag []byte, cmac Digest) { +func setupEAX(c Cipher, iv, hdr []byte, tagBytes int) (ctrIV, tag []byte, cmac hash.Hash) { n := len(iv); if n != c.BlockSize() { panicln("crypto/block: EAX: iv length", n, "!=", c.BlockSize()); @@ -63,7 +64,7 @@ func setupEAX(c Cipher, iv, hdr []byte, tagBytes int) (ctrIV, tag []byte, cmac D return; } -func finishEAX(tag []byte, cmac Digest) { +func finishEAX(tag []byte, cmac hash.Hash) { // Finish CMAC #2 and xor into tag. sum := cmac.Sum(); for i := range tag { @@ -75,7 +76,7 @@ func finishEAX(tag []byte, cmac Digest) { // Knows that cmac never returns write errors. type cmacWriter struct { w io.Writer; - cmac Digest; + cmac hash.Hash; } func (cw *cmacWriter) Write(p []byte) (n int, err os.Error) { @@ -133,7 +134,7 @@ func (x *eaxEncrypter) Close() os.Error { // but the latter half is trivial. type cmacReader struct { r io.Reader; - cmac Digest; + cmac hash.Hash; tag []byte; tmp []byte; } -- 2.48.1