]> Cypherpunks repositories - gostls13.git/commitdiff
crypto/hmac: Deprecate hmac.NewMD5, hmac.NewSHA1 and hmac.NewSHA256
authorLuit van Drongelen <luitvd@gmail.com>
Thu, 19 Jan 2012 22:28:38 +0000 (17:28 -0500)
committerAdam Langley <agl@golang.org>
Thu, 19 Jan 2012 22:28:38 +0000 (17:28 -0500)
Remove NewMD5, NewSHA1 and NewSHA256 in favor of using New and
explicitly importing the used hash-function. This way when using, for
example, HMAC with RIPEMD there's no md5, sha1 and sha256 linked in
through the hmac package.

A gofix rule is included, and applied to the standard library (3 files
altered).

This change is the result of a discussion at
https://golang.org/cl/5550043/ to pull the discussion about
deprecating these functions out of that issue.

R=golang-dev, agl
CC=golang-dev, r, rsc
https://golang.org/cl/5556058

src/cmd/gofix/Makefile
src/cmd/gofix/hmacnew.go [new file with mode: 0644]
src/cmd/gofix/hmacnew_test.go [new file with mode: 0644]
src/pkg/crypto/hmac/hmac.go
src/pkg/crypto/tls/cipher_suites.go
src/pkg/exp/ssh/transport.go
src/pkg/net/smtp/auth.go

index a00ec34733031c303349df45b4355b6c843e84d5..1aabd19f31c8f69071649394dc13b0205f6919ea 100644 (file)
@@ -12,6 +12,7 @@ GOFILES=\
        go1pkgrename.go\
        googlecode.go\
        hashsum.go\
+       hmacnew.go\
        htmlerr.go\
        httpfinalurl.go\
        httpfs.go\
diff --git a/src/cmd/gofix/hmacnew.go b/src/cmd/gofix/hmacnew.go
new file mode 100644 (file)
index 0000000..c0c44ef
--- /dev/null
@@ -0,0 +1,61 @@
+// Copyright 2011 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import "go/ast"
+
+func init() {
+       register(hmacNewFix)
+}
+
+var hmacNewFix = fix{
+       "hmacnew",
+       "2012-01-19",
+       hmacnew,
+       `Deprecate hmac.NewMD5, hmac.NewSHA1 and hmac.NewSHA256.
+
+This fix rewrites code using hmac.NewMD5, hmac.NewSHA1 and hmac.NewSHA256 to
+use hmac.New:
+
+       hmac.NewMD5(key) -> hmac.New(md5.New, key)
+       hmac.NewSHA1(key) -> hmac.New(sha1.New, key)
+       hmac.NewSHA256(key) -> hmac.New(sha256.New, key)
+
+`,
+}
+
+func hmacnew(f *ast.File) (fixed bool) {
+       if !imports(f, "crypto/hmac") {
+               return
+       }
+
+       walk(f, func(n interface{}) {
+               ce, ok := n.(*ast.CallExpr)
+               if !ok {
+                       return
+               }
+
+               var pkg string
+               switch {
+               case isPkgDot(ce.Fun, "hmac", "NewMD5"):
+                       pkg = "md5"
+               case isPkgDot(ce.Fun, "hmac", "NewSHA1"):
+                       pkg = "sha1"
+               case isPkgDot(ce.Fun, "hmac", "NewSHA256"):
+                       pkg = "sha256"
+               default:
+                       return
+               }
+
+               addImport(f, "crypto/"+pkg)
+
+               ce.Fun = ast.NewIdent("hmac.New")
+               ce.Args = append([]ast.Expr{ast.NewIdent(pkg + ".New")}, ce.Args...)
+
+               fixed = true
+       })
+
+       return
+}
diff --git a/src/cmd/gofix/hmacnew_test.go b/src/cmd/gofix/hmacnew_test.go
new file mode 100644 (file)
index 0000000..5aeee85
--- /dev/null
@@ -0,0 +1,107 @@
+// Copyright 2011 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+func init() {
+       addTestCases(hmacNewTests, hmacnew)
+}
+
+var hmacNewTests = []testCase{
+       {
+               Name: "hmacnew.0",
+               In: `package main
+
+import "crypto/hmac"
+
+var f = hmac.NewSHA1([]byte("some key"))
+`,
+               Out: `package main
+
+import (
+       "crypto/hmac"
+       "crypto/sha1"
+)
+
+var f = hmac.New(sha1.New, []byte("some key"))
+`,
+       },
+       {
+               Name: "hmacnew.1",
+               In: `package main
+
+import "crypto/hmac"
+
+var key = make([]byte, 8)
+var f = hmac.NewSHA1(key)
+`,
+               Out: `package main
+
+import (
+       "crypto/hmac"
+       "crypto/sha1"
+)
+
+var key = make([]byte, 8)
+var f = hmac.New(sha1.New, key)
+`,
+       },
+       {
+               Name: "hmacnew.2",
+               In: `package main
+
+import "crypto/hmac"
+
+var f = hmac.NewMD5([]byte("some key"))
+`,
+               Out: `package main
+
+import (
+       "crypto/hmac"
+       "crypto/md5"
+)
+
+var f = hmac.New(md5.New, []byte("some key"))
+`,
+       },
+       {
+               Name: "hmacnew.3",
+               In: `package main
+
+import "crypto/hmac"
+
+var f = hmac.NewSHA256([]byte("some key"))
+`,
+               Out: `package main
+
+import (
+       "crypto/hmac"
+       "crypto/sha256"
+)
+
+var f = hmac.New(sha256.New, []byte("some key"))
+`,
+       },
+       {
+               Name: "hmacnew.4",
+               In: `package main
+
+import (
+       "crypto/hmac"
+       "crypto/sha1"
+)
+
+var f = hmac.New(sha1.New, []byte("some key"))
+`,
+               Out: `package main
+
+import (
+       "crypto/hmac"
+       "crypto/sha1"
+)
+
+var f = hmac.New(sha1.New, []byte("some key"))
+`,
+       },
+}
index 6bdbbb40308a5e041e447167c7c78fb6bd392dea..a97ce09727abaaacccbba5db59a588e81ae17d51 100644 (file)
@@ -9,9 +9,6 @@
 package hmac
 
 import (
-       "crypto/md5"
-       "crypto/sha1"
-       "crypto/sha256"
        "hash"
 )
 
@@ -63,7 +60,7 @@ func (h *hmac) Reset() {
        h.inner.Write(h.tmp[0:h.blocksize])
 }
 
-// New returns a new HMAC hash using the given crypto.Hash type and key.
+// New returns a new HMAC hash using the given hash.Hash type and key.
 func New(h func() hash.Hash, key []byte) hash.Hash {
        hm := new(hmac)
        hm.outer = h()
@@ -81,12 +78,3 @@ func New(h func() hash.Hash, key []byte) hash.Hash {
        hm.Reset()
        return hm
 }
-
-// NewMD5 returns a new HMAC-MD5 hash using the given key.
-func NewMD5(key []byte) hash.Hash { return New(md5.New, key) }
-
-// NewSHA1 returns a new HMAC-SHA1 hash using the given key.
-func NewSHA1(key []byte) hash.Hash { return New(sha1.New, key) }
-
-// NewSHA256 returns a new HMAC-SHA256 hash using the given key.
-func NewSHA256(key []byte) hash.Hash { return New(sha256.New, key) }
index 914491d6b4a84f4eb68272213b8b349e69efb6cc..00695e7d15b484d0bc85ccc36b685fc1d8287128 100644 (file)
@@ -91,7 +91,7 @@ func macSHA1(version uint16, key []byte) macFunction {
                copy(mac.key, key)
                return mac
        }
-       return tls10MAC{hmac.NewSHA1(key)}
+       return tls10MAC{hmac.New(sha1.New, key)}
 }
 
 type macFunction interface {
index 60a636f0a4bacb2025ec16cfb99e55bcd5ffea8d..e21bc4ba2025084bf687e0b2676ab7197f77739a 100644 (file)
@@ -9,6 +9,7 @@ import (
        "crypto"
        "crypto/cipher"
        "crypto/hmac"
+       "crypto/sha1"
        "crypto/subtle"
        "errors"
        "hash"
@@ -266,7 +267,7 @@ func (c *common) setupKeys(d direction, K, H, sessionId []byte, hashFunc crypto.
        generateKeyMaterial(key, d.keyTag, K, H, sessionId, h)
        generateKeyMaterial(macKey, d.macKeyTag, K, H, sessionId, h)
 
-       c.mac = truncatingMAC{12, hmac.NewSHA1(macKey)}
+       c.mac = truncatingMAC{12, hmac.New(sha1.New, macKey)}
 
        cipher, err := cipherMode.createCipher(key, iv)
        if err != nil {
index 6f0cde0d2833d43a0ebf963b608923d05371e444..d401e3c21fd0721e87c0a66594328b2049dacd2b 100644 (file)
@@ -6,6 +6,7 @@ package smtp
 
 import (
        "crypto/hmac"
+       "crypto/md5"
        "errors"
        "fmt"
 )
@@ -88,7 +89,7 @@ func (a *cramMD5Auth) Start(server *ServerInfo) (string, []byte, error) {
 
 func (a *cramMD5Auth) Next(fromServer []byte, more bool) ([]byte, error) {
        if more {
-               d := hmac.NewMD5([]byte(a.secret))
+               d := hmac.New(md5.New, []byte(a.secret))
                d.Write(fromServer)
                s := make([]byte, 0, d.Size())
                return []byte(fmt.Sprintf("%s %x", a.username, d.Sum(s))), nil