hash.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. // Package hash provides TLS HashAlgorithm as defined in TLS 1.2
  4. package hash
  5. import ( //nolint:gci
  6. "crypto"
  7. "crypto/md5" //nolint:gosec
  8. "crypto/sha1" //nolint:gosec
  9. "crypto/sha256"
  10. "crypto/sha512"
  11. )
  12. // Algorithm is used to indicate the hash algorithm used
  13. // https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-18
  14. type Algorithm uint16
  15. // Supported hash algorithms
  16. const (
  17. None Algorithm = 0 // Blacklisted
  18. MD5 Algorithm = 1 // Blacklisted
  19. SHA1 Algorithm = 2 // Blacklisted
  20. SHA224 Algorithm = 3
  21. SHA256 Algorithm = 4
  22. SHA384 Algorithm = 5
  23. SHA512 Algorithm = 6
  24. Ed25519 Algorithm = 8
  25. )
  26. // String makes hashAlgorithm printable
  27. func (a Algorithm) String() string {
  28. switch a {
  29. case None:
  30. return "none"
  31. case MD5:
  32. return "md5" // [RFC3279]
  33. case SHA1:
  34. return "sha-1" // [RFC3279]
  35. case SHA224:
  36. return "sha-224" // [RFC4055]
  37. case SHA256:
  38. return "sha-256" // [RFC4055]
  39. case SHA384:
  40. return "sha-384" // [RFC4055]
  41. case SHA512:
  42. return "sha-512" // [RFC4055]
  43. case Ed25519:
  44. return "null"
  45. default:
  46. return "unknown or unsupported hash algorithm"
  47. }
  48. }
  49. // Digest performs a digest on the passed value
  50. func (a Algorithm) Digest(b []byte) []byte {
  51. switch a {
  52. case None:
  53. return nil
  54. case MD5:
  55. hash := md5.Sum(b) // #nosec
  56. return hash[:]
  57. case SHA1:
  58. hash := sha1.Sum(b) // #nosec
  59. return hash[:]
  60. case SHA224:
  61. hash := sha256.Sum224(b)
  62. return hash[:]
  63. case SHA256:
  64. hash := sha256.Sum256(b)
  65. return hash[:]
  66. case SHA384:
  67. hash := sha512.Sum384(b)
  68. return hash[:]
  69. case SHA512:
  70. hash := sha512.Sum512(b)
  71. return hash[:]
  72. default:
  73. return nil
  74. }
  75. }
  76. // Insecure returns if the given HashAlgorithm is considered secure in DTLS 1.2
  77. func (a Algorithm) Insecure() bool {
  78. switch a {
  79. case None, MD5, SHA1:
  80. return true
  81. default:
  82. return false
  83. }
  84. }
  85. // CryptoHash returns the crypto.Hash implementation for the given HashAlgorithm
  86. func (a Algorithm) CryptoHash() crypto.Hash {
  87. switch a {
  88. case None:
  89. return crypto.Hash(0)
  90. case MD5:
  91. return crypto.MD5
  92. case SHA1:
  93. return crypto.SHA1
  94. case SHA224:
  95. return crypto.SHA224
  96. case SHA256:
  97. return crypto.SHA256
  98. case SHA384:
  99. return crypto.SHA384
  100. case SHA512:
  101. return crypto.SHA512
  102. case Ed25519:
  103. return crypto.Hash(0)
  104. default:
  105. return crypto.Hash(0)
  106. }
  107. }
  108. // Algorithms returns all the supported Hash Algorithms
  109. func Algorithms() map[Algorithm]struct{} {
  110. return map[Algorithm]struct{}{
  111. None: {},
  112. MD5: {},
  113. SHA1: {},
  114. SHA224: {},
  115. SHA256: {},
  116. SHA384: {},
  117. SHA512: {},
  118. Ed25519: {},
  119. }
  120. }