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.

crypto.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2019 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package procfs
  14. import (
  15. "bytes"
  16. "fmt"
  17. "io/ioutil"
  18. "strconv"
  19. "strings"
  20. "github.com/prometheus/procfs/internal/util"
  21. )
  22. // Crypto holds info parsed from /proc/crypto.
  23. type Crypto struct {
  24. Alignmask *uint64
  25. Async bool
  26. Blocksize *uint64
  27. Chunksize *uint64
  28. Ctxsize *uint64
  29. Digestsize *uint64
  30. Driver string
  31. Geniv string
  32. Internal string
  33. Ivsize *uint64
  34. Maxauthsize *uint64
  35. MaxKeysize *uint64
  36. MinKeysize *uint64
  37. Module string
  38. Name string
  39. Priority *int64
  40. Refcnt *int64
  41. Seedsize *uint64
  42. Selftest string
  43. Type string
  44. Walksize *uint64
  45. }
  46. // Crypto parses an crypto-file (/proc/crypto) and returns a slice of
  47. // structs containing the relevant info. More information available here:
  48. // https://kernel.readthedocs.io/en/sphinx-samples/crypto-API.html
  49. func (fs FS) Crypto() ([]Crypto, error) {
  50. data, err := ioutil.ReadFile(fs.proc.Path("crypto"))
  51. if err != nil {
  52. return nil, fmt.Errorf("error parsing crypto %s: %s", fs.proc.Path("crypto"), err)
  53. }
  54. crypto, err := parseCrypto(data)
  55. if err != nil {
  56. return nil, fmt.Errorf("error parsing crypto %s: %s", fs.proc.Path("crypto"), err)
  57. }
  58. return crypto, nil
  59. }
  60. func parseCrypto(cryptoData []byte) ([]Crypto, error) {
  61. crypto := []Crypto{}
  62. cryptoBlocks := bytes.Split(cryptoData, []byte("\n\n"))
  63. for _, block := range cryptoBlocks {
  64. var newCryptoElem Crypto
  65. lines := strings.Split(string(block), "\n")
  66. for _, line := range lines {
  67. if strings.TrimSpace(line) == "" || line[0] == ' ' {
  68. continue
  69. }
  70. fields := strings.Split(line, ":")
  71. key := strings.TrimSpace(fields[0])
  72. value := strings.TrimSpace(fields[1])
  73. vp := util.NewValueParser(value)
  74. switch strings.TrimSpace(key) {
  75. case "async":
  76. b, err := strconv.ParseBool(value)
  77. if err == nil {
  78. newCryptoElem.Async = b
  79. }
  80. case "blocksize":
  81. newCryptoElem.Blocksize = vp.PUInt64()
  82. case "chunksize":
  83. newCryptoElem.Chunksize = vp.PUInt64()
  84. case "digestsize":
  85. newCryptoElem.Digestsize = vp.PUInt64()
  86. case "driver":
  87. newCryptoElem.Driver = value
  88. case "geniv":
  89. newCryptoElem.Geniv = value
  90. case "internal":
  91. newCryptoElem.Internal = value
  92. case "ivsize":
  93. newCryptoElem.Ivsize = vp.PUInt64()
  94. case "maxauthsize":
  95. newCryptoElem.Maxauthsize = vp.PUInt64()
  96. case "max keysize":
  97. newCryptoElem.MaxKeysize = vp.PUInt64()
  98. case "min keysize":
  99. newCryptoElem.MinKeysize = vp.PUInt64()
  100. case "module":
  101. newCryptoElem.Module = value
  102. case "name":
  103. newCryptoElem.Name = value
  104. case "priority":
  105. newCryptoElem.Priority = vp.PInt64()
  106. case "refcnt":
  107. newCryptoElem.Refcnt = vp.PInt64()
  108. case "seedsize":
  109. newCryptoElem.Seedsize = vp.PUInt64()
  110. case "selftest":
  111. newCryptoElem.Selftest = value
  112. case "type":
  113. newCryptoElem.Type = value
  114. case "walksize":
  115. newCryptoElem.Walksize = vp.PUInt64()
  116. }
  117. }
  118. crypto = append(crypto, newCryptoElem)
  119. }
  120. return crypto, nil
  121. }