test_sign_kernel.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #!/bin/bash -eux
  2. # Copyright (c) 2014 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. DEVKEYS=${SRCDIR}/tests/devkeys
  10. echo "hi there" > ${TMP}.config.txt
  11. echo "hello boys" > ${TMP}.config2.txt
  12. dd if=/dev/urandom bs=512 count=1 of=${TMP}.bootloader.bin
  13. dd if=/dev/urandom bs=512 count=1 of=${TMP}.bootloader2.bin
  14. dd if=/dev/urandom bs=1M count=16 of=${TMP}.kern_partition
  15. # default padding
  16. padding=49152
  17. try_arch () {
  18. local arch=$1
  19. echo -n "${arch}: 1 " 1>&3
  20. # pack it up the old way
  21. ${FUTILITY} --debug vbutil_kernel \
  22. --pack ${TMP}.blob1.${arch} \
  23. --keyblock ${DEVKEYS}/recovery_kernel.keyblock \
  24. --signprivate ${DEVKEYS}/recovery_kernel_data_key.vbprivk \
  25. --version 1 \
  26. --config ${TMP}.config.txt \
  27. --bootloader ${TMP}.bootloader.bin \
  28. --vmlinuz ${SCRIPTDIR}/data/vmlinuz-${arch}.bin \
  29. --arch ${arch} \
  30. --pad ${padding} \
  31. --kloadaddr 0x11000
  32. # verify the old way
  33. ${FUTILITY} vbutil_kernel --verify ${TMP}.blob1.${arch} \
  34. --pad ${padding} \
  35. --signpubkey ${DEVKEYS}/recovery_key.vbpubk > ${TMP}.verify1
  36. # pack it up the new way
  37. ${FUTILITY} --debug sign \
  38. --keyblock ${DEVKEYS}/recovery_kernel.keyblock \
  39. --signprivate ${DEVKEYS}/recovery_kernel_data_key.vbprivk \
  40. --version 1 \
  41. --config ${TMP}.config.txt \
  42. --bootloader ${TMP}.bootloader.bin \
  43. --vmlinuz ${SCRIPTDIR}/data/vmlinuz-${arch}.bin \
  44. --arch ${arch} \
  45. --pad ${padding} \
  46. --kloadaddr 0x11000 \
  47. --outfile ${TMP}.blob2.${arch}
  48. ${FUTILITY} vbutil_kernel --verify ${TMP}.blob2.${arch} \
  49. --pad ${padding} \
  50. --signpubkey ${DEVKEYS}/recovery_key.vbpubk > ${TMP}.verify2
  51. # they should be identical
  52. cmp ${TMP}.blob1.${arch} ${TMP}.blob2.${arch}
  53. diff ${TMP}.verify1 ${TMP}.verify2
  54. echo -n "2 " 1>&3
  55. # repack it the old way
  56. ${FUTILITY} --debug vbutil_kernel \
  57. --repack ${TMP}.blob3.${arch} \
  58. --oldblob ${TMP}.blob1.${arch} \
  59. --signprivate ${DEVKEYS}/kernel_data_key.vbprivk \
  60. --keyblock ${DEVKEYS}/kernel.keyblock \
  61. --version 2 \
  62. --pad ${padding} \
  63. --config ${TMP}.config2.txt \
  64. --bootloader ${TMP}.bootloader2.bin
  65. # verify the old way
  66. ${FUTILITY} vbutil_kernel --verify ${TMP}.blob3.${arch} \
  67. --pad ${padding} \
  68. --signpubkey ${DEVKEYS}/kernel_subkey.vbpubk > ${TMP}.verify3
  69. # repack it the new way
  70. ${FUTILITY} --debug sign \
  71. --signprivate ${DEVKEYS}/kernel_data_key.vbprivk \
  72. --keyblock ${DEVKEYS}/kernel.keyblock \
  73. --version 2 \
  74. --pad ${padding} \
  75. --config ${TMP}.config2.txt \
  76. --bootloader ${TMP}.bootloader2.bin \
  77. ${TMP}.blob2.${arch} \
  78. ${TMP}.blob4.${arch}
  79. ${FUTILITY} vbutil_kernel --verify ${TMP}.blob4.${arch} \
  80. --pad ${padding} \
  81. --signpubkey ${DEVKEYS}/kernel_subkey.vbpubk > ${TMP}.verify4
  82. # they should be identical
  83. cmp ${TMP}.blob3.${arch} ${TMP}.blob4.${arch}
  84. diff ${TMP}.verify3 ${TMP}.verify4
  85. echo -n "3 " 1>&3
  86. # repack it the new way, in-place
  87. cp ${TMP}.blob2.${arch} ${TMP}.blob5.${arch}
  88. ${FUTILITY} --debug sign \
  89. --signprivate ${DEVKEYS}/kernel_data_key.vbprivk \
  90. --keyblock ${DEVKEYS}/kernel.keyblock \
  91. --version 2 \
  92. --pad ${padding} \
  93. --config ${TMP}.config2.txt \
  94. --bootloader ${TMP}.bootloader2.bin \
  95. ${TMP}.blob5.${arch}
  96. ${FUTILITY} vbutil_kernel --verify ${TMP}.blob5.${arch} \
  97. --pad ${padding} \
  98. --signpubkey ${DEVKEYS}/kernel_subkey.vbpubk > ${TMP}.verify5
  99. # they should be identical
  100. cmp ${TMP}.blob3.${arch} ${TMP}.blob5.${arch}
  101. diff ${TMP}.verify3 ${TMP}.verify5
  102. # and now just the vblocks...
  103. echo -n "4 " 1>&3
  104. # pack the old way
  105. ${FUTILITY} vbutil_kernel \
  106. --pack ${TMP}.blob1.${arch}.vb1 \
  107. --vblockonly \
  108. --keyblock ${DEVKEYS}/recovery_kernel.keyblock \
  109. --signprivate ${DEVKEYS}/recovery_kernel_data_key.vbprivk \
  110. --version 1 \
  111. --config ${TMP}.config.txt \
  112. --bootloader ${TMP}.bootloader.bin \
  113. --vmlinuz ${SCRIPTDIR}/data/vmlinuz-${arch}.bin \
  114. --arch ${arch} \
  115. --pad ${padding} \
  116. --kloadaddr 0x11000
  117. # compare this new vblock with the one from the full pack
  118. dd bs=${padding} count=1 if=${TMP}.blob1.${arch} of=${TMP}.blob1.${arch}.vb0
  119. cmp ${TMP}.blob1.${arch}.vb0 ${TMP}.blob1.${arch}.vb1
  120. # pack the new way
  121. ${FUTILITY} --debug sign \
  122. --keyblock ${DEVKEYS}/recovery_kernel.keyblock \
  123. --signprivate ${DEVKEYS}/recovery_kernel_data_key.vbprivk \
  124. --version 1 \
  125. --config ${TMP}.config.txt \
  126. --bootloader ${TMP}.bootloader.bin \
  127. --vmlinuz ${SCRIPTDIR}/data/vmlinuz-${arch}.bin \
  128. --arch ${arch} \
  129. --pad ${padding} \
  130. --kloadaddr 0x11000 \
  131. --vblockonly \
  132. ${TMP}.blob2.${arch}.vb1
  133. # compare this new vblock with the one from the full pack
  134. dd bs=${padding} count=1 if=${TMP}.blob2.${arch} of=${TMP}.blob2.${arch}.vb0
  135. cmp ${TMP}.blob2.${arch}.vb0 ${TMP}.blob2.${arch}.vb1
  136. echo -n "5 " 1>&3
  137. # now repack the old way, again emitting just the vblock
  138. ${FUTILITY} vbutil_kernel \
  139. --repack ${TMP}.blob3.${arch}.vb1 \
  140. --vblockonly \
  141. --oldblob ${TMP}.blob1.${arch} \
  142. --signprivate ${DEVKEYS}/kernel_data_key.vbprivk \
  143. --keyblock ${DEVKEYS}/kernel.keyblock \
  144. --version 2 \
  145. --pad ${padding} \
  146. --config ${TMP}.config2.txt \
  147. --bootloader ${TMP}.bootloader2.bin
  148. # compare the full repacked vblock with the new repacked vblock
  149. dd bs=${padding} count=1 if=${TMP}.blob3.${arch} of=${TMP}.blob3.${arch}.vb0
  150. cmp ${TMP}.blob3.${arch}.vb0 ${TMP}.blob3.${arch}.vb1
  151. # extract just the kernel blob
  152. dd bs=${padding} skip=1 if=${TMP}.blob3.${arch} of=${TMP}.blob3.${arch}.kb0
  153. # and verify it using the new vblock (no way to do that with vbutil_kernel)
  154. ${FUTILITY} --debug verify \
  155. --pad ${padding} \
  156. --publickey ${DEVKEYS}/kernel_subkey.vbpubk \
  157. --fv ${TMP}.blob3.${arch}.kb0 \
  158. ${TMP}.blob3.${arch}.vb1 > ${TMP}.verify3v
  159. # repack the new way
  160. ${FUTILITY} --debug sign \
  161. --signprivate ${DEVKEYS}/kernel_data_key.vbprivk \
  162. --keyblock ${DEVKEYS}/kernel.keyblock \
  163. --version 2 \
  164. --config ${TMP}.config2.txt \
  165. --bootloader ${TMP}.bootloader2.bin \
  166. --pad ${padding} \
  167. --vblockonly \
  168. ${TMP}.blob2.${arch} \
  169. ${TMP}.blob4.${arch}.vb1 \
  170. # compare the full repacked vblock with the new repacked vblock
  171. dd bs=${padding} count=1 if=${TMP}.blob4.${arch} of=${TMP}.blob4.${arch}.vb0
  172. cmp ${TMP}.blob4.${arch}.vb0 ${TMP}.blob4.${arch}.vb1
  173. # extract just the kernel blob
  174. dd bs=${padding} skip=1 if=${TMP}.blob4.${arch} of=${TMP}.blob4.${arch}.kb0
  175. # and verify it using the new vblock (no way to do that with vbutil_kernel)
  176. ${FUTILITY} --debug verify \
  177. --pad ${padding} \
  178. --publickey ${DEVKEYS}/kernel_subkey.vbpubk \
  179. --fv ${TMP}.blob4.${arch}.kb0 \
  180. ${TMP}.blob4.${arch}.vb1 > ${TMP}.verify4v
  181. echo -n "6 " 1>&3
  182. # Now lets repack some kernel partitions, not just blobs.
  183. cp ${TMP}.kern_partition ${TMP}.part1.${arch}
  184. dd if=${TMP}.blob1.${arch} of=${TMP}.part1.${arch} conv=notrunc
  185. # Make sure the partitions verify
  186. ${FUTILITY} vbutil_kernel --verify ${TMP}.part1.${arch} \
  187. --pad ${padding} \
  188. --signpubkey ${DEVKEYS}/recovery_key.vbpubk > ${TMP}.verify6
  189. # The partition should verify the same way as the blob
  190. diff ${TMP}.verify1 ${TMP}.verify6
  191. # repack it the old way
  192. ${FUTILITY} --debug vbutil_kernel \
  193. --repack ${TMP}.part6.${arch} \
  194. --oldblob ${TMP}.part1.${arch} \
  195. --signprivate ${DEVKEYS}/kernel_data_key.vbprivk \
  196. --keyblock ${DEVKEYS}/kernel.keyblock \
  197. --version 2 \
  198. --pad ${padding} \
  199. --config ${TMP}.config2.txt \
  200. --bootloader ${TMP}.bootloader2.bin
  201. # verify the old way
  202. ${FUTILITY} vbutil_kernel --verify ${TMP}.part6.${arch} \
  203. --pad ${padding} \
  204. --signpubkey ${DEVKEYS}/kernel_subkey.vbpubk > ${TMP}.verify6.old
  205. # this "partition" should actually be the same as the old-way blob
  206. cmp ${TMP}.blob3.${arch} ${TMP}.part6.${arch}
  207. # repack it the new way, in-place
  208. cp ${TMP}.part1.${arch} ${TMP}.part6.${arch}.new1
  209. ${FUTILITY} --debug sign \
  210. --signprivate ${DEVKEYS}/kernel_data_key.vbprivk \
  211. --keyblock ${DEVKEYS}/kernel.keyblock \
  212. --version 2 \
  213. --pad ${padding} \
  214. --config ${TMP}.config2.txt \
  215. --bootloader ${TMP}.bootloader2.bin \
  216. ${TMP}.part6.${arch}.new1
  217. ${FUTILITY} vbutil_kernel --verify ${TMP}.part6.${arch}.new1 \
  218. --pad ${padding} \
  219. --signpubkey ${DEVKEYS}/kernel_subkey.vbpubk > ${TMP}.verify6.new1
  220. # The verification should be indentical
  221. diff ${TMP}.verify6.old ${TMP}.verify6.new1
  222. # But the content should only match up to the size of the kernel blob, since
  223. # we're modifying an entire partition in-place.
  224. blobsize=$(stat -c '%s' ${TMP}.part6.${arch})
  225. cmp -n ${blobsize} ${TMP}.part6.${arch} ${TMP}.part6.${arch}.new1
  226. # The rest of the partition should be unchanged.
  227. cmp -i ${blobsize} ${TMP}.part1.${arch} ${TMP}.part6.${arch}.new1
  228. # repack it the new way, from input to output
  229. cp ${TMP}.part1.${arch} ${TMP}.part1.${arch}.in
  230. ${FUTILITY} --debug sign \
  231. --signprivate ${DEVKEYS}/kernel_data_key.vbprivk \
  232. --keyblock ${DEVKEYS}/kernel.keyblock \
  233. --version 2 \
  234. --pad ${padding} \
  235. --config ${TMP}.config2.txt \
  236. --bootloader ${TMP}.bootloader2.bin \
  237. ${TMP}.part1.${arch}.in \
  238. ${TMP}.part6.${arch}.new2
  239. ${FUTILITY} vbutil_kernel --verify ${TMP}.part6.${arch}.new2 \
  240. --pad ${padding} \
  241. --signpubkey ${DEVKEYS}/kernel_subkey.vbpubk > ${TMP}.verify6.new2
  242. # The input file should not have changed (just being sure).
  243. cmp ${TMP}.part1.${arch} ${TMP}.part1.${arch}.in
  244. # The verification should be indentical
  245. diff ${TMP}.verify6.old ${TMP}.verify6.new2
  246. # And creating a new output file should only emit a blob's worth
  247. cmp ${TMP}.part6.${arch} ${TMP}.part6.${arch}.new2
  248. # Note: We specifically do not test repacking with a different --kloadaddr,
  249. # because the old way has a bug and does not update params->cmd_line_ptr to
  250. # point at the new on-disk location. Apparently (and not surprisingly), no
  251. # one has ever done that.
  252. }
  253. try_arch amd64
  254. try_arch arm
  255. # cleanup
  256. rm -rf ${TMP}*
  257. exit 0