run_vbutil_tests.sh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #!/bin/bash
  2. # Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. # Run verified boot firmware and kernel verification tests.
  6. # Load common constants and variables.
  7. . "$(dirname "$0")/common.sh"
  8. return_code=0
  9. function test_vbutil_key_single {
  10. local algonum=$1
  11. local keylen=$2
  12. local hashalgo=$3
  13. echo -e "For signing key ${COL_YELLOW}RSA-$keylen/$hashalgo${COL_STOP}:"
  14. # Pack the key
  15. ${FUTILITY} vbutil_key \
  16. --pack ${TESTKEY_SCRATCH_DIR}/key_alg${algonum}.vbpubk \
  17. --key ${TESTKEY_DIR}/key_rsa${keylen}.keyb \
  18. --version 1 \
  19. --algorithm $algonum
  20. if [ $? -ne 0 ]
  21. then
  22. return_code=255
  23. fi
  24. # Unpack the key
  25. # TODO: should verify we get the same key back out?
  26. ${FUTILITY} vbutil_key \
  27. --unpack ${TESTKEY_SCRATCH_DIR}/key_alg${algonum}.vbpubk
  28. if [ $? -ne 0 ]
  29. then
  30. return_code=255
  31. fi
  32. }
  33. function test_vbutil_key_all {
  34. algorithmcounter=0
  35. for keylen in ${key_lengths[@]}
  36. do
  37. for hashalgo in ${hash_algos[@]}
  38. do
  39. test_vbutil_key_single $algorithmcounter $keylen $hashalgo
  40. let algorithmcounter=algorithmcounter+1
  41. done
  42. done
  43. }
  44. function test_vbutil_key {
  45. test_vbutil_key_single 4 2048 sha256
  46. test_vbutil_key_single 7 4096 sha256
  47. test_vbutil_key_single 11 8192 sha512
  48. }
  49. function test_vbutil_keyblock_single {
  50. local signing_algonum=$1
  51. local signing_keylen=$2
  52. local signing_hashalgo=$3
  53. local data_algonum=$4
  54. local data_keylen=$5
  55. local data_hashalgo=$6
  56. echo -e "For ${COL_YELLOW}signing algorithm \
  57. RSA-${signing_keylen}/${signing_hashalgo}${COL_STOP} \
  58. and ${COL_YELLOW}data key algorithm RSA-${datakeylen}/\
  59. ${datahashalgo}${COL_STOP}"
  60. # Remove old file
  61. keyblockfile="${TESTKEY_SCRATCH_DIR}/"
  62. keyblockfile+="sign${signing_algonum}_data"
  63. keyblockfile+="${data_algonum}.keyblock"
  64. rm -f ${keyblockfile}
  65. # Wrap private key
  66. ${FUTILITY} vbutil_key \
  67. --pack ${TESTKEY_SCRATCH_DIR}/key_alg${algonum}.vbprivk \
  68. --key ${TESTKEY_DIR}/key_rsa${signing_keylen}.pem \
  69. --algorithm $signing_algonum
  70. if [ $? -ne 0 ]
  71. then
  72. echo -e "${COL_RED}Wrap vbprivk${COL_STOP}"
  73. return_code=255
  74. fi
  75. # Wrap public key
  76. ${FUTILITY} vbutil_key \
  77. --pack ${TESTKEY_SCRATCH_DIR}/key_alg${algonum}.vbpubk \
  78. --key ${TESTKEY_DIR}/key_rsa${signing_keylen}.keyb \
  79. --algorithm $signing_algonum
  80. if [ $? -ne 0 ]
  81. then
  82. echo -e "${COL_RED}Wrap vbpubk${COL_STOP}"
  83. return_code=255
  84. fi
  85. # Pack
  86. ${FUTILITY} vbutil_keyblock --pack ${keyblockfile} \
  87. --datapubkey \
  88. ${TESTKEY_SCRATCH_DIR}/key_alg${data_algonum}.vbpubk \
  89. --signprivate \
  90. ${TESTKEY_SCRATCH_DIR}/key_alg${algonum}.vbprivk
  91. if [ $? -ne 0 ]
  92. then
  93. echo -e "${COL_RED}Pack${COL_STOP}"
  94. return_code=255
  95. fi
  96. # Unpack
  97. ${FUTILITY} vbutil_keyblock --unpack ${keyblockfile} \
  98. --datapubkey \
  99. ${TESTKEY_SCRATCH_DIR}/key_alg${data_algonum}.vbpubk2 \
  100. --signpubkey \
  101. ${TESTKEY_SCRATCH_DIR}/key_alg${algonum}.vbpubk
  102. if [ $? -ne 0 ]
  103. then
  104. echo -e "${COL_RED}Unpack${COL_STOP}"
  105. return_code=255
  106. fi
  107. # Check
  108. if ! cmp -s \
  109. ${TESTKEY_SCRATCH_DIR}/key_alg${data_algonum}.vbpubk \
  110. ${TESTKEY_SCRATCH_DIR}/key_alg${data_algonum}.vbpubk2
  111. then
  112. echo -e "${COL_RED}Check${COL_STOP}"
  113. return_code=255
  114. exit 1
  115. fi
  116. echo -e "${COL_YELLOW}Testing keyblock creation using \
  117. external signer.${COL_STOP}"
  118. # Pack using external signer
  119. # Pack
  120. ${FUTILITY} vbutil_keyblock --pack ${keyblockfile} \
  121. --datapubkey \
  122. ${TESTKEY_SCRATCH_DIR}/key_alg${data_algonum}.vbpubk \
  123. --signprivate_pem \
  124. ${TESTKEY_DIR}/key_rsa${signing_keylen}.pem \
  125. --pem_algorithm "${signing_algonum}" \
  126. --externalsigner "${SCRIPT_DIR}/external_rsa_signer.sh"
  127. if [ $? -ne 0 ]
  128. then
  129. echo -e "${COL_RED}Pack${COL_STOP}"
  130. return_code=255
  131. fi
  132. # Unpack
  133. ${FUTILITY} vbutil_keyblock --unpack ${keyblockfile} \
  134. --datapubkey \
  135. ${TESTKEY_SCRATCH_DIR}/key_alg${data_algonum}.vbpubk2 \
  136. --signpubkey \
  137. ${TESTKEY_SCRATCH_DIR}/key_alg${signing_algonum}.vbpubk
  138. if [ $? -ne 0 ]
  139. then
  140. echo -e "${COL_RED}Unpack${COL_STOP}"
  141. return_code=255
  142. fi
  143. # Check
  144. if ! cmp -s \
  145. ${TESTKEY_SCRATCH_DIR}/key_alg${data_algonum}.vbpubk \
  146. ${TESTKEY_SCRATCH_DIR}/key_alg${data_algonum}.vbpubk2
  147. then
  148. echo -e "${COL_RED}Check${COL_STOP}"
  149. return_code=255
  150. exit 1
  151. fi
  152. }
  153. function test_vbutil_keyblock_all {
  154. # Test for various combinations of firmware signing algorithm and
  155. # kernel signing algorithm
  156. signing_algorithmcounter=0
  157. data_algorithmcounter=0
  158. for signing_keylen in ${key_lengths[@]}
  159. do
  160. for signing_hashalgo in ${hash_algos[@]}
  161. do
  162. let data_algorithmcounter=0
  163. for datakeylen in ${key_lengths[@]}
  164. do
  165. for datahashalgo in ${hash_algos[@]}
  166. do
  167. test_vbutil_keyblock_single \
  168. $signing_algorithmcounter $signing_keylen $signing_hashalgo \
  169. $data_algorithmcounter $data_keylen $data_hashalgo
  170. let data_algorithmcounter=data_algorithmcounter+1
  171. done
  172. done
  173. let signing_algorithmcounter=signing_algorithmcounter+1
  174. done
  175. done
  176. }
  177. function test_vbutil_keyblock {
  178. test_vbutil_keyblock_single 7 4096 sha256 4 2048 sha256
  179. test_vbutil_keyblock_single 11 8192 sha512 4 2048 sha256
  180. test_vbutil_keyblock_single 11 8192 sha512 7 4096 sha256
  181. }
  182. check_test_keys
  183. echo
  184. echo "Testing vbutil_key..."
  185. if [ "$1" == "--all" ] ; then
  186. test_vbutil_key_all
  187. else
  188. test_vbutil_key
  189. fi
  190. echo
  191. echo "Testing vbutil_keyblock..."
  192. if [ "$1" == "--all" ] ; then
  193. test_vbutil_keyblock_all
  194. else
  195. test_vbutil_keyblock
  196. fi
  197. exit $return_code