You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

59 lines
2.0 KiB

  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. /*
  5. Package auth authenticates a message using a secret key.
  6. The Sum function, viewed as a function of the message for a uniform random
  7. key, is designed to meet the standard notion of unforgeability. This means
  8. that an attacker cannot find authenticators for any messages not authenticated
  9. by the sender, even if the attacker has adaptively influenced the messages
  10. authenticated by the sender. For a formal definition see, e.g., Section 2.4
  11. of Bellare, Kilian, and Rogaway, "The security of the cipher block chaining
  12. message authentication code," Journal of Computer and System Sciences 61 (2000),
  13. 362–399; http://www-cse.ucsd.edu/~mihir/papers/cbc.html.
  14. auth does not make any promises regarding "strong" unforgeability; perhaps
  15. one valid authenticator can be converted into another valid authenticator for
  16. the same message. NaCl also does not make any promises regarding "truncated
  17. unforgeability."
  18. This package is interoperable with NaCl: https://nacl.cr.yp.to/auth.html.
  19. */
  20. package auth
  21. import (
  22. "crypto/hmac"
  23. "crypto/sha512"
  24. )
  25. const (
  26. // Size is the size, in bytes, of an authenticated digest.
  27. Size = 32
  28. // KeySize is the size, in bytes, of an authentication key.
  29. KeySize = 32
  30. )
  31. // Sum generates an authenticator for m using a secret key and returns the
  32. // 32-byte digest.
  33. func Sum(m []byte, key *[KeySize]byte) *[Size]byte {
  34. mac := hmac.New(sha512.New, key[:])
  35. mac.Write(m)
  36. out := new([KeySize]byte)
  37. copy(out[:], mac.Sum(nil)[:Size])
  38. return out
  39. }
  40. // Verify checks that digest is a valid authenticator of message m under the
  41. // given secret key. Verify does not leak timing information.
  42. func Verify(digest []byte, m []byte, key *[KeySize]byte) bool {
  43. if len(digest) != Size {
  44. return false
  45. }
  46. mac := hmac.New(sha512.New, key[:])
  47. mac.Write(m)
  48. expectedMAC := mac.Sum(nil) // first 256 bits of 512-bit sum
  49. return hmac.Equal(digest, expectedMAC[:Size])
  50. }