test_bdb.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/bin/bash -eux
  2. # Copyright 2015 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. me=${0##*/}
  6. TMP="$me.tmp"
  7. # Work in scratch directory
  8. cd "$OUTDIR"
  9. BDB_FILE=bdb.bin
  10. TESTKEY_DIR=${SRCDIR}/tests/testkeys
  11. TESTDATA_DIR=${SRCDIR}/tests/testdata
  12. BDBKEY_PUB=${TESTKEY_DIR}/bdbkey.keyb
  13. BDBKEY_PRI=${TESTKEY_DIR}/bdbkey.pem
  14. DATAKEY_PUB=${TESTKEY_DIR}/datakey.keyb
  15. DATAKEY_PRI=${TESTKEY_DIR}/datakey.pem
  16. BDBKEY_DIGEST=${TESTDATA_DIR}/bdbkey_digest.bin
  17. DATAKEY_DIGEST=${TESTDATA_DIR}/datakey_digest.bin
  18. DATA_FILE=${TESTDATA_DIR}/sp-rw.bin
  19. declare -i num_hash
  20. # Verify a BDB
  21. #
  22. # $1: Key digest file
  23. # $2: Any remaining option passed to futility bdb --verify
  24. verify() {
  25. local key_digest=${1:-${BDBKEY_DIGEST}}
  26. local extra_option=${2:-}
  27. ${FUTILITY} bdb --verify ${BDB_FILE} --key_digest ${key_digest} \
  28. ${extra_option}
  29. }
  30. get_num_hash() {
  31. printf "%d" \
  32. $(${FUTILITY} show ${BDB_FILE} \
  33. | grep '# of Hashes' | cut -d':' -f 2)
  34. }
  35. # Tests field matches a specified value in a BDB
  36. # e.g. check_field 'Data Version:' 2 returns error if the data version isn't 2.
  37. check_field() {
  38. # Find the field
  39. x=$(${FUTILITY} show ${BDB_FILE} | grep "${1}")
  40. [ "${x}" ] || return 1
  41. # Remove the field name
  42. x=${x##*:}
  43. [ "${x}" ] || return 1
  44. # Remove the leading and trailing spaces
  45. x=${x//[[:blank:]]/}
  46. [ "${x}" == "${2}" ] || return 1
  47. }
  48. # Demonstrate bdb --create can create a valid BDB
  49. load_address=0x60061ec0de
  50. ${FUTILITY} bdb --create ${BDB_FILE} \
  51. --bdbkey_pri ${BDBKEY_PRI} --bdbkey_pub ${BDBKEY_PUB} \
  52. --datakey_pub ${DATAKEY_PUB} --datakey_pri ${DATAKEY_PRI} \
  53. --load_address ${load_address}
  54. verify
  55. check_field "Load Address:" ${load_address}
  56. # Demonstrate bdb --add can add a new hash
  57. num_hash=$(get_num_hash)
  58. ${FUTILITY} bdb --add ${BDB_FILE} \
  59. --data ${DATA_FILE} --partition 1 --type 2 --offset 3 --load_address 4
  60. # Use futility show command to verify the hash is added
  61. num_hash+=1
  62. [ $(get_num_hash) -eq $num_hash ]
  63. # TODO: verify partition, type, offset, and load_address
  64. # Demonstrate futility bdb --resign can resign the BDB
  65. data_version=2
  66. ${FUTILITY} bdb --resign ${BDB_FILE} --datakey_pri ${DATAKEY_PRI} \
  67. --data_version $data_version
  68. verify
  69. check_field "Data Version:" $data_version
  70. # Demonstrate futility bdb --resign can resign with a new data key
  71. # Note resigning with a new data key requires a private BDB key as well
  72. ${FUTILITY} bdb --resign ${BDB_FILE} \
  73. --bdbkey_pri ${BDBKEY_PRI} \
  74. --datakey_pri ${BDBKEY_PRI} --datakey_pub ${BDBKEY_PUB}
  75. verify
  76. # Demonstrate futility bdb --resign can resign with a new BDB key
  77. ${FUTILITY} bdb --resign ${BDB_FILE} \
  78. --bdbkey_pri ${DATAKEY_PRI} --bdbkey_pub ${DATAKEY_PUB}
  79. verify ${DATAKEY_DIGEST}
  80. # Demonstrate futility bdb --verify can return success when key digest doesn't
  81. # match but --ignore_key_digest is specified.
  82. verify ${BDBKEY_DIGEST} --ignore_key_digest
  83. # cleanup
  84. rm -rf ${TMP}*
  85. exit 0