freedombone-utils-rng 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Random number generation functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. # The type of hardware random number generator being used
  31. # This can be empty, "beaglebone" or "onerng"
  32. HWRNG_TYPE=
  33. # Download location for OneRNG driver
  34. ONERNG_PACKAGE="onerng_3.4-1_all.deb"
  35. ONERNG_PACKAGE_DOWNLOAD="https://github.com/OneRNG/onerng.github.io/blob/master/sw/$ONERNG_PACKAGE?raw=true"
  36. # Hash for OneRNG driver
  37. ONERNG_PACKAGE_HASH='78f1c2f52ae573e3b398a695ece7ab9f41868252657ea269f0d5cf0bd4f2eb59'
  38. # device name for OneRNG
  39. ONERNG_DEVICE='ttyACM0'
  40. function check_hwrng {
  41. if [[ $HWRNG_TYPE == "beaglebone" ]]; then
  42. # If hardware random number generation was enabled then make sure that the device exists.
  43. # if /dev/hwrng is not found then any subsequent cryptographic key generation would
  44. # suffer from low entropy and might be insecure
  45. if [ ! -e /dev/hwrng ]; then
  46. ls /dev/hw*
  47. echo $'The hardware random number generator is enabled but could not be detected on'
  48. echo $'/dev/hwrng. There may be a problem with the installation or the Beaglebone hardware.'
  49. exit 75
  50. fi
  51. fi
  52. # If a OneRNG device was installed then verify its firmware
  53. #check_onerng_verification
  54. }
  55. function check_onerng_verification {
  56. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  57. return
  58. fi
  59. if [[ $HWRNG_TYPE != "onerng" ]]; then
  60. return
  61. fi
  62. echo $'Checking OneRNG firmware verification'
  63. last_onerng_validation=$(grep "OneRNG: firmware verification" /var/log/syslog.1 | awk '/./{line=$0} END{print line}')
  64. if [[ $last_onerng_validation != *"passed OK"* ]]; then
  65. last_onerng_validation=$(grep "OneRNG: firmware verification" /var/log/syslog | awk '/./{line=$0} END{print line}')
  66. if [[ $last_onerng_validation != *"passed OK"* ]]; then
  67. echo "$last_onerng_validation"
  68. echo $'OneRNG firmware verification failed'
  69. exit 735026
  70. fi
  71. fi
  72. echo $'OneRNG firmware verification passed'
  73. # if haveged was previously installed then remove it
  74. apt-get -yq remove haveged
  75. mark_completed "${FUNCNAME[0]}"
  76. }
  77. function install_onerng {
  78. apt-get -yq install rng-tools at python-gnupg
  79. # Move to the installation directory
  80. if [ ! -d "$INSTALL_DIR" ]; then
  81. mkdir "$INSTALL_DIR"
  82. fi
  83. cd "$INSTALL_DIR" || exit 24762464
  84. # Download the package
  85. if [ ! -f $ONERNG_PACKAGE ]; then
  86. wget "$ONERNG_PACKAGE_DOWNLOAD"
  87. # shellcheck disable=SC2086
  88. mv $ONERNG_PACKAGE?raw=true $ONERNG_PACKAGE
  89. fi
  90. if [ ! -f $ONERNG_PACKAGE ]; then
  91. echo $"OneRNG package could not be downloaded"
  92. exit 59249
  93. fi
  94. # Check the hash
  95. hash=$(sha256sum $ONERNG_PACKAGE | awk -F ' ' '{print $1}')
  96. if [[ "$hash" != "$ONERNG_PACKAGE_HASH" ]]; then
  97. echo $"OneRNG package: $ONERNG_PACKAGE"
  98. echo $"Hash does not match. This could indicate that the package has been tampered with."
  99. echo $"OneRNG expected package hash: $ONERNG_PACKAGE_HASH"
  100. echo $"OneRNG actual hash: $hash"
  101. exit 25934
  102. fi
  103. # install the package
  104. dpkg -i $ONERNG_PACKAGE
  105. # Check that the install worked
  106. if [ ! -f /etc/onerng.conf ]; then
  107. echo $'OneRNG configuration file not found. The package may not have installed successfully.'
  108. exit 42904
  109. fi
  110. dialog --title $"OneRNG Device" \
  111. --msgbox $"Please plug in the OneRNG device" 6 40
  112. # check rng-tools configuration
  113. if ! grep -q "/dev/$ONERNG_DEVICE" /etc/default/rng-tools; then
  114. echo "HRNGDEVICE=/dev/$ONERNG_DEVICE" >> /etc/default/rng-tools
  115. fi
  116. systemctl restart rng-tools
  117. }
  118. function random_number_generator {
  119. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  120. return
  121. fi
  122. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  123. # it is assumed that docker uses the random number
  124. # generator of the host system
  125. return
  126. fi
  127. # if the hrng type has not been set but /dev/hwrng is detected
  128. if [[ $HWRNG_TYPE != "beaglebone" ]]; then
  129. if [ -e /dev/hwrng ]; then
  130. HWRNG_TYPE="beaglebone"
  131. fi
  132. fi
  133. case $HWRNG_TYPE in
  134. beaglebone)
  135. apt-get -yq install rng-tools
  136. sed -i 's|#HRNGDEVICE=/dev/hwrng|HRNGDEVICE=/dev/hwrng|g' /etc/default/rng-tools
  137. ;;
  138. onerng)
  139. function_check install_onerng
  140. install_onerng
  141. ;;
  142. *)
  143. # With some VMs, the hardware cycles counter is emulated and deterministic,
  144. # and thus predictible, so havege should not be used
  145. if [[ "$ARCHITECTURE" != "qemu"* ]]; then
  146. apt-get -yq install haveged
  147. fi
  148. ;;
  149. esac
  150. mark_completed "${FUNCNAME[0]}"
  151. }
  152. # NOTE: deliberately no exit 0