crypto.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2011 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 crypto collects common cryptographic constants.
  5. package crypto
  6. import (
  7. "hash"
  8. "io"
  9. "strconv"
  10. )
  11. // Hash identifies a cryptographic hash function that is implemented in another
  12. // package.
  13. type Hash uint
  14. // HashFunc simply returns the value of h so that Hash implements SignerOpts.
  15. func (h Hash) HashFunc() Hash {
  16. return h
  17. }
  18. const (
  19. MD4 Hash = 1 + iota // import golang.org/x/crypto/md4
  20. MD5 // import crypto/md5
  21. SHA1 // import crypto/sha1
  22. SHA224 // import crypto/sha256
  23. SHA256 // import crypto/sha256
  24. SHA384 // import crypto/sha512
  25. SHA512 // import crypto/sha512
  26. MD5SHA1 // no implementation; MD5+SHA1 used for TLS RSA
  27. RIPEMD160 // import golang.org/x/crypto/ripemd160
  28. SHA3_224 // import golang.org/x/crypto/sha3
  29. SHA3_256 // import golang.org/x/crypto/sha3
  30. SHA3_384 // import golang.org/x/crypto/sha3
  31. SHA3_512 // import golang.org/x/crypto/sha3
  32. SHA512_224 // import crypto/sha512
  33. SHA512_256 // import crypto/sha512
  34. maxHash
  35. )
  36. var digestSizes = []uint8{
  37. MD4: 16,
  38. MD5: 16,
  39. SHA1: 20,
  40. SHA224: 28,
  41. SHA256: 32,
  42. SHA384: 48,
  43. SHA512: 64,
  44. SHA512_224: 28,
  45. SHA512_256: 32,
  46. SHA3_224: 28,
  47. SHA3_256: 32,
  48. SHA3_384: 48,
  49. SHA3_512: 64,
  50. MD5SHA1: 36,
  51. RIPEMD160: 20,
  52. }
  53. // Size returns the length, in bytes, of a digest resulting from the given hash
  54. // function. It doesn't require that the hash function in question be linked
  55. // into the program.
  56. func (h Hash) Size() int {
  57. if h > 0 && h < maxHash {
  58. return int(digestSizes[h])
  59. }
  60. panic("crypto: Size of unknown hash function")
  61. }
  62. var hashes = make([]func() hash.Hash, maxHash)
  63. // New returns a new hash.Hash calculating the given hash function. New panics
  64. // if the hash function is not linked into the binary.
  65. func (h Hash) New() hash.Hash {
  66. if h > 0 && h < maxHash {
  67. f := hashes[h]
  68. if f != nil {
  69. return f()
  70. }
  71. }
  72. panic("crypto: requested hash function #" + strconv.Itoa(int(h)) + " is unavailable")
  73. }
  74. // Available reports whether the given hash function is linked into the binary.
  75. func (h Hash) Available() bool {
  76. return h < maxHash && hashes[h] != nil
  77. }
  78. // RegisterHash registers a function that returns a new instance of the given
  79. // hash function. This is intended to be called from the init function in
  80. // packages that implement hash functions.
  81. func RegisterHash(h Hash, f func() hash.Hash) {
  82. if h >= maxHash {
  83. panic("crypto: RegisterHash of unknown hash function")
  84. }
  85. hashes[h] = f
  86. }
  87. // PublicKey represents a public key using an unspecified algorithm.
  88. type PublicKey interface{}
  89. // PrivateKey represents a private key using an unspecified algorithm.
  90. type PrivateKey interface{}
  91. // Signer is an interface for an opaque private key that can be used for
  92. // signing operations. For example, an RSA key kept in a hardware module.
  93. type Signer interface {
  94. // Public returns the public key corresponding to the opaque,
  95. // private key.
  96. Public() PublicKey
  97. // Sign signs digest with the private key, possibly using entropy from
  98. // rand. For an RSA key, the resulting signature should be either a
  99. // PKCS#1 v1.5 or PSS signature (as indicated by opts). For an (EC)DSA
  100. // key, it should be a DER-serialised, ASN.1 signature structure.
  101. //
  102. // Hash implements the SignerOpts interface and, in most cases, one can
  103. // simply pass in the hash function used as opts. Sign may also attempt
  104. // to type assert opts to other types in order to obtain algorithm
  105. // specific values. See the documentation in each package for details.
  106. //
  107. // Note that when a signature of a hash of a larger message is needed,
  108. // the caller is responsible for hashing the larger message and passing
  109. // the hash (as digest) and the hash function (as opts) to Sign.
  110. Sign(rand io.Reader, digest []byte, opts SignerOpts) (signature []byte, err error)
  111. }
  112. // SignerOpts contains options for signing with a Signer.
  113. type SignerOpts interface {
  114. // HashFunc returns an identifier for the hash function used to produce
  115. // the message passed to Signer.Sign, or else zero to indicate that no
  116. // hashing was done.
  117. HashFunc() Hash
  118. }
  119. // Decrypter is an interface for an opaque private key that can be used for
  120. // asymmetric decryption operations. An example would be an RSA key
  121. // kept in a hardware module.
  122. type Decrypter interface {
  123. // Public returns the public key corresponding to the opaque,
  124. // private key.
  125. Public() PublicKey
  126. // Decrypt decrypts msg. The opts argument should be appropriate for
  127. // the primitive used. See the documentation in each implementation for
  128. // details.
  129. Decrypt(rand io.Reader, msg []byte, opts DecrypterOpts) (plaintext []byte, err error)
  130. }
  131. type DecrypterOpts interface{}