mac.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2012 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. package ssh
  5. // Message authentication support
  6. import (
  7. "crypto/hmac"
  8. "crypto/sha1"
  9. "hash"
  10. )
  11. type macMode struct {
  12. keySize int
  13. new func(key []byte) hash.Hash
  14. }
  15. // truncatingMAC wraps around a hash.Hash and truncates the output digest to
  16. // a given size.
  17. type truncatingMAC struct {
  18. length int
  19. hmac hash.Hash
  20. }
  21. func (t truncatingMAC) Write(data []byte) (int, error) {
  22. return t.hmac.Write(data)
  23. }
  24. func (t truncatingMAC) Sum(in []byte) []byte {
  25. out := t.hmac.Sum(in)
  26. return out[:len(in)+t.length]
  27. }
  28. func (t truncatingMAC) Reset() {
  29. t.hmac.Reset()
  30. }
  31. func (t truncatingMAC) Size() int {
  32. return t.length
  33. }
  34. func (t truncatingMAC) BlockSize() int { return t.hmac.BlockSize() }
  35. var macModes = map[string]*macMode{
  36. "hmac-sha1": {20, func(key []byte) hash.Hash {
  37. return hmac.New(sha1.New, key)
  38. }},
  39. "hmac-sha1-96": {20, func(key []byte) hash.Hash {
  40. return truncatingMAC{12, hmac.New(sha1.New, key)}
  41. }},
  42. }