signature.txt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. U-Boot FIT Signature Verification
  2. =================================
  3. Introduction
  4. ------------
  5. FIT supports hashing of images so that these hashes can be checked on
  6. loading. This protects against corruption of the image. However it does not
  7. prevent the substitution of one image for another.
  8. The signature feature allows the hash to be signed with a private key such
  9. that it can be verified using a public key later. Provided that the private
  10. key is kept secret and the public key is stored in a non-volatile place,
  11. any image can be verified in this way.
  12. See verified-boot.txt for more general information on verified boot.
  13. Concepts
  14. --------
  15. Some familiarity with public key cryptography is assumed in this section.
  16. The procedure for signing is as follows:
  17. - hash an image in the FIT
  18. - sign the hash with a private key to produce a signature
  19. - store the resulting signature in the FIT
  20. The procedure for verification is:
  21. - read the FIT
  22. - obtain the public key
  23. - extract the signature from the FIT
  24. - hash the image from the FIT
  25. - verify (with the public key) that the extracted signature matches the
  26. hash
  27. The signing is generally performed by mkimage, as part of making a firmware
  28. image for the device. The verification is normally done in U-Boot on the
  29. device.
  30. Algorithms
  31. ----------
  32. In principle any suitable algorithm can be used to sign and verify a hash.
  33. At present only one class of algorithms is supported: SHA1 hashing with RSA.
  34. This works by hashing the image to produce a 20-byte hash.
  35. While it is acceptable to bring in large cryptographic libraries such as
  36. openssl on the host side (e.g. mkimage), it is not desirable for U-Boot.
  37. For the run-time verification side, it is important to keep code and data
  38. size as small as possible.
  39. For this reason the RSA image verification uses pre-processed public keys
  40. which can be used with a very small amount of code - just some extraction
  41. of data from the FDT and exponentiation mod n. Code size impact is a little
  42. under 5KB on Tegra Seaboard, for example.
  43. It is relatively straightforward to add new algorithms if required. If
  44. another RSA variant is needed, then it can be added to the table in
  45. image-sig.c. If another algorithm is needed (such as DSA) then it can be
  46. placed alongside rsa.c, and its functions added to the table in image-sig.c
  47. also.
  48. Creating an RSA key pair and certificate
  49. ----------------------------------------
  50. To create a new public/private key pair, size 2048 bits:
  51. $ openssl genpkey -algorithm RSA -out keys/dev.key \
  52. -pkeyopt rsa_keygen_bits:2048 -pkeyopt rsa_keygen_pubexp:65537
  53. To create a certificate for this containing the public key:
  54. $ openssl req -batch -new -x509 -key keys/dev.key -out keys/dev.crt
  55. If you like you can look at the public key also:
  56. $ openssl rsa -in keys/dev.key -pubout
  57. Device Tree Bindings
  58. --------------------
  59. The following properties are required in the FIT's signature node(s) to
  60. allow the signer to operate. These should be added to the .its file.
  61. Signature nodes sit at the same level as hash nodes and are called
  62. signature@1, signature@2, etc.
  63. - algo: Algorithm name (e.g. "sha1,rs2048")
  64. - key-name-hint: Name of key to use for signing. The keys will normally be in
  65. a single directory (parameter -k to mkimage). For a given key <name>, its
  66. private key is stored in <name>.key and the certificate is stored in
  67. <name>.crt.
  68. When the image is signed, the following properties are added (mandatory):
  69. - value: The signature data (e.g. 256 bytes for 2048-bit RSA)
  70. When the image is signed, the following properties are optional:
  71. - timestamp: Time when image was signed (standard Unix time_t format)
  72. - signer-name: Name of the signer (e.g. "mkimage")
  73. - signer-version: Version string of the signer (e.g. "2013.01")
  74. - comment: Additional information about the signer or image
  75. For config bindings (see Signed Configurations below), the following
  76. additional properties are optional:
  77. - sign-images: A list of images to sign, each being a property of the conf
  78. node that contains then. The default is "kernel,fdt" which means that these
  79. two images will be looked up in the config and signed if present.
  80. For config bindings, these properties are added by the signer:
  81. - hashed-nodes: A list of nodes which were hashed by the signer. Each is
  82. a string - the full path to node. A typical value might be:
  83. hashed-nodes = "/", "/configurations/conf@1", "/images/kernel@1",
  84. "/images/kernel@1/hash@1", "/images/fdt@1",
  85. "/images/fdt@1/hash@1";
  86. - hashed-strings: The start and size of the string region of the FIT that
  87. was hashed
  88. Example: See sign-images.its for an example image tree source file and
  89. sign-configs.its for config signing.
  90. Public Key Storage
  91. ------------------
  92. In order to verify an image that has been signed with a public key we need to
  93. have a trusted public key. This cannot be stored in the signed image, since
  94. it would be easy to alter. For this implementation we choose to store the
  95. public key in U-Boot's control FDT (using CONFIG_OF_CONTROL).
  96. Public keys should be stored as sub-nodes in a /signature node. Required
  97. properties are:
  98. - algo: Algorithm name (e.g. "sha1,rs2048")
  99. Optional properties are:
  100. - key-name-hint: Name of key used for signing. This is only a hint since it
  101. is possible for the name to be changed. Verification can proceed by checking
  102. all available signing keys until one matches.
  103. - required: If present this indicates that the key must be verified for the
  104. image / configuration to be considered valid. Only required keys are
  105. normally verified by the FIT image booting algorithm. Valid values are
  106. "image" to force verification of all images, and "conf" to force verification
  107. of the selected configuration (which then relies on hashes in the images to
  108. verify those).
  109. Each signing algorithm has its own additional properties.
  110. For RSA the following are mandatory:
  111. - rsa,num-bits: Number of key bits (e.g. 2048)
  112. - rsa,modulus: Modulus (N) as a big-endian multi-word integer
  113. - rsa,exponent: Public exponent (E) as a 64 bit unsigned integer
  114. - rsa,r-squared: (2^num-bits)^2 as a big-endian multi-word integer
  115. - rsa,n0-inverse: -1 / modulus[0] mod 2^32
  116. Signed Configurations
  117. ---------------------
  118. While signing images is useful, it does not provide complete protection
  119. against several types of attack. For example, it it possible to create a
  120. FIT with the same signed images, but with the configuration changed such
  121. that a different one is selected (mix and match attack). It is also possible
  122. to substitute a signed image from an older FIT version into a newer FIT
  123. (roll-back attack).
  124. As an example, consider this FIT:
  125. / {
  126. images {
  127. kernel@1 {
  128. data = <data for kernel1>
  129. signature@1 {
  130. algo = "sha1,rsa2048";
  131. value = <...kernel signature 1...>
  132. };
  133. };
  134. kernel@2 {
  135. data = <data for kernel2>
  136. signature@1 {
  137. algo = "sha1,rsa2048";
  138. value = <...kernel signature 2...>
  139. };
  140. };
  141. fdt@1 {
  142. data = <data for fdt1>;
  143. signature@1 {
  144. algo = "sha1,rsa2048";
  145. vaue = <...fdt signature 1...>
  146. };
  147. };
  148. fdt@2 {
  149. data = <data for fdt2>;
  150. signature@1 {
  151. algo = "sha1,rsa2048";
  152. vaue = <...fdt signature 2...>
  153. };
  154. };
  155. };
  156. configurations {
  157. default = "conf@1";
  158. conf@1 {
  159. kernel = "kernel@1";
  160. fdt = "fdt@1";
  161. };
  162. conf@1 {
  163. kernel = "kernel@2";
  164. fdt = "fdt@2";
  165. };
  166. };
  167. };
  168. Since both kernels are signed it is easy for an attacker to add a new
  169. configuration 3 with kernel 1 and fdt 2:
  170. configurations {
  171. default = "conf@1";
  172. conf@1 {
  173. kernel = "kernel@1";
  174. fdt = "fdt@1";
  175. };
  176. conf@1 {
  177. kernel = "kernel@2";
  178. fdt = "fdt@2";
  179. };
  180. conf@3 {
  181. kernel = "kernel@1";
  182. fdt = "fdt@2";
  183. };
  184. };
  185. With signed images, nothing protects against this. Whether it gains an
  186. advantage for the attacker is debatable, but it is not secure.
  187. To solve this problem, we support signed configurations. In this case it
  188. is the configurations that are signed, not the image. Each image has its
  189. own hash, and we include the hash in the configuration signature.
  190. So the above example is adjusted to look like this:
  191. / {
  192. images {
  193. kernel@1 {
  194. data = <data for kernel1>
  195. hash@1 {
  196. algo = "sha1";
  197. value = <...kernel hash 1...>
  198. };
  199. };
  200. kernel@2 {
  201. data = <data for kernel2>
  202. hash@1 {
  203. algo = "sha1";
  204. value = <...kernel hash 2...>
  205. };
  206. };
  207. fdt@1 {
  208. data = <data for fdt1>;
  209. hash@1 {
  210. algo = "sha1";
  211. value = <...fdt hash 1...>
  212. };
  213. };
  214. fdt@2 {
  215. data = <data for fdt2>;
  216. hash@1 {
  217. algo = "sha1";
  218. value = <...fdt hash 2...>
  219. };
  220. };
  221. };
  222. configurations {
  223. default = "conf@1";
  224. conf@1 {
  225. kernel = "kernel@1";
  226. fdt = "fdt@1";
  227. signature@1 {
  228. algo = "sha1,rsa2048";
  229. value = <...conf 1 signature...>;
  230. };
  231. };
  232. conf@2 {
  233. kernel = "kernel@2";
  234. fdt = "fdt@2";
  235. signature@1 {
  236. algo = "sha1,rsa2048";
  237. value = <...conf 1 signature...>;
  238. };
  239. };
  240. };
  241. };
  242. You can see that we have added hashes for all images (since they are no
  243. longer signed), and a signature to each configuration. In the above example,
  244. mkimage will sign configurations/conf@1, the kernel and fdt that are
  245. pointed to by the configuration (/images/kernel@1, /images/kernel@1/hash@1,
  246. /images/fdt@1, /images/fdt@1/hash@1) and the root structure of the image
  247. (so that it isn't possible to add or remove root nodes). The signature is
  248. written into /configurations/conf@1/signature@1/value. It can easily be
  249. verified later even if the FIT has been signed with other keys in the
  250. meantime.
  251. Verification
  252. ------------
  253. FITs are verified when loaded. After the configuration is selected a list
  254. of required images is produced. If there are 'required' public keys, then
  255. each image must be verified against those keys. This means that every image
  256. that might be used by the target needs to be signed with 'required' keys.
  257. This happens automatically as part of a bootm command when FITs are used.
  258. Enabling FIT Verification
  259. -------------------------
  260. In addition to the options to enable FIT itself, the following CONFIGs must
  261. be enabled:
  262. CONFIG_FIT_SIGNATURE - enable signing and verification in FITs
  263. CONFIG_RSA - enable RSA algorithm for signing
  264. WARNING: When relying on signed FIT images with required signature check
  265. the legacy image format is default disabled by not defining
  266. CONFIG_IMAGE_FORMAT_LEGACY
  267. Testing
  268. -------
  269. An easy way to test signing and verification is to use the test script
  270. provided in test/vboot/vboot_test.sh. This uses sandbox (a special version
  271. of U-Boot which runs under Linux) to show the operation of a 'bootm'
  272. command loading and verifying images.
  273. A sample run is show below:
  274. $ make O=sandbox sandbox_config
  275. $ make O=sandbox
  276. $ O=sandbox ./test/vboot/vboot_test.sh
  277. Simple Verified Boot Test
  278. =========================
  279. Please see doc/uImage.FIT/verified-boot.txt for more information
  280. /home/hs/ids/u-boot/sandbox/tools/mkimage -D -I dts -O dtb -p 2000
  281. Build keys
  282. do sha1 test
  283. Build FIT with signed images
  284. Test Verified Boot Run: unsigned signatures:: OK
  285. Sign images
  286. Test Verified Boot Run: signed images: OK
  287. Build FIT with signed configuration
  288. Test Verified Boot Run: unsigned config: OK
  289. Sign images
  290. Test Verified Boot Run: signed config: OK
  291. check signed config on the host
  292. Signature check OK
  293. OK
  294. Test Verified Boot Run: signed config: OK
  295. Test Verified Boot Run: signed config with bad hash: OK
  296. do sha256 test
  297. Build FIT with signed images
  298. Test Verified Boot Run: unsigned signatures:: OK
  299. Sign images
  300. Test Verified Boot Run: signed images: OK
  301. Build FIT with signed configuration
  302. Test Verified Boot Run: unsigned config: OK
  303. Sign images
  304. Test Verified Boot Run: signed config: OK
  305. check signed config on the host
  306. Signature check OK
  307. OK
  308. Test Verified Boot Run: signed config: OK
  309. Test Verified Boot Run: signed config with bad hash: OK
  310. Test passed
  311. Hardware Signing with PKCS#11
  312. -----------------------------
  313. Securely managing private signing keys can challenging, especially when the
  314. keys are stored on the file system of a computer that is connected to the
  315. Internet. If an attacker is able to steal the key, they can sign malicious FIT
  316. images which will appear genuine to your devices.
  317. An alternative solution is to keep your signing key securely stored on hardware
  318. device like a smartcard, USB token or Hardware Security Module (HSM) and have
  319. them perform the signing. PKCS#11 is standard for interfacing with these crypto
  320. device.
  321. Requirements:
  322. Smartcard/USB token/HSM which can work with the pkcs11 engine
  323. openssl
  324. libp11 (provides pkcs11 engine)
  325. p11-kit (recommended to simplify setup)
  326. opensc (for smartcards and smartcard like USB devices)
  327. gnutls (recommended for key generation, p11tool)
  328. The following examples use the Nitrokey Pro. Instructions for other devices may vary.
  329. Notes on pkcs11 engine setup:
  330. Make sure p11-kit, opensc are installed and that p11-kit is setup to use opensc.
  331. /usr/share/p11-kit/modules/opensc.module should be present on your system.
  332. Generating Keys On the Nitrokey:
  333. $ gpg --card-edit
  334. Reader ...........: Nitrokey Nitrokey Pro (xxxxxxxx0000000000000000) 00 00
  335. Application ID ...: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  336. Version ..........: 2.1
  337. Manufacturer .....: ZeitControl
  338. Serial number ....: xxxxxxxx
  339. Name of cardholder: [not set]
  340. Language prefs ...: de
  341. Sex ..............: unspecified
  342. URL of public key : [not set]
  343. Login data .......: [not set]
  344. Signature PIN ....: forced
  345. Key attributes ...: rsa2048 rsa2048 rsa2048
  346. Max. PIN lengths .: 32 32 32
  347. PIN retry counter : 3 0 3
  348. Signature counter : 0
  349. Signature key ....: [none]
  350. Encryption key....: [none]
  351. Authentication key: [none]
  352. General key info..: [none]
  353. gpg/card> generate
  354. Make off-card backup of encryption key? (Y/n) n
  355. Please note that the factory settings of the PINs are
  356. PIN = '123456' Admin PIN = '12345678'
  357. You should change them using the command --change-pin
  358. What keysize do you want for the Signature key? (2048) 4096
  359. The card will now be re-configured to generate a key of 4096 bits
  360. Note: There is no guarantee that the card supports the requested size.
  361. If the key generation does not succeed, please check the
  362. documentation of your card to see what sizes are allowed.
  363. What keysize do you want for the Encryption key? (2048) 4096
  364. The card will now be re-configured to generate a key of 4096 bits
  365. What keysize do you want for the Authentication key? (2048) 4096
  366. The card will now be re-configured to generate a key of 4096 bits
  367. Please specify how long the key should be valid.
  368. 0 = key does not expire
  369. <n> = key expires in n days
  370. <n>w = key expires in n weeks
  371. <n>m = key expires in n months
  372. <n>y = key expires in n years
  373. Key is valid for? (0)
  374. Key does not expire at all
  375. Is this correct? (y/N) y
  376. GnuPG needs to construct a user ID to identify your key.
  377. Real name: John Doe
  378. Email address: john.doe@email.com
  379. Comment:
  380. You selected this USER-ID:
  381. "John Doe <john.doe@email.com>"
  382. Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
  383. Using p11tool to get the token URL:
  384. Depending on system configuration, gpg-agent may need to be killed first.
  385. $ p11tool --provider /usr/lib/opensc-pkcs11.so --list-tokens
  386. Token 0:
  387. URL: pkcs11:model=PKCS%2315%20emulated;manufacturer=ZeitControl;serial=000xxxxxxxxx;token=OpenPGP%20card%20%28User%20PIN%20%28sig%29%29
  388. Label: OpenPGP card (User PIN (sig))
  389. Type: Hardware token
  390. Manufacturer: ZeitControl
  391. Model: PKCS#15 emulated
  392. Serial: 000xxxxxxxxx
  393. Module: (null)
  394. Token 1:
  395. URL: pkcs11:model=PKCS%2315%20emulated;manufacturer=ZeitControl;serial=000xxxxxxxxx;token=OpenPGP%20card%20%28User%20PIN%29
  396. Label: OpenPGP card (User PIN)
  397. Type: Hardware token
  398. Manufacturer: ZeitControl
  399. Model: PKCS#15 emulated
  400. Serial: 000xxxxxxxxx
  401. Module: (null)
  402. Use the portion of the signature token URL after "pkcs11:" as the keydir argument (-k) to mkimage below.
  403. Use the URL of the token to list the private keys:
  404. $ p11tool --login --provider /usr/lib/opensc-pkcs11.so --list-privkeys \
  405. "pkcs11:model=PKCS%2315%20emulated;manufacturer=ZeitControl;serial=000xxxxxxxxx;token=OpenPGP%20card%20%28User%20PIN%20%28sig%29%29"
  406. Token 'OpenPGP card (User PIN (sig))' with URL 'pkcs11:model=PKCS%2315%20emulated;manufacturer=ZeitControl;serial=000xxxxxxxxx;token=OpenPGP%20card%20%28User%20PIN%20%28sig%29%29' requires user PIN
  407. Enter PIN:
  408. Object 0:
  409. URL: pkcs11:model=PKCS%2315%20emulated;manufacturer=ZeitControl;serial=000xxxxxxxxx;token=OpenPGP%20card%20%28User%20PIN%20%28sig%29%29;id=%01;object=Signature%20key;type=private
  410. Type: Private key
  411. Label: Signature key
  412. Flags: CKA_PRIVATE; CKA_NEVER_EXTRACTABLE; CKA_SENSITIVE;
  413. ID: 01
  414. Use the label, in this case "Signature key" as the key-name-hint in your FIT.
  415. Create the fitImage:
  416. $ ./tools/mkimage -f fit-image.its fitImage
  417. Sign the fitImage with the hardware key:
  418. $ ./tools/mkimage -F -k \
  419. "model=PKCS%2315%20emulated;manufacturer=ZeitControl;serial=000xxxxxxxxx;token=OpenPGP%20card%20%28User%20PIN%20%28sig%29%29" \
  420. -K u-boot.dtb -N pkcs11 -r fitImage
  421. Future Work
  422. -----------
  423. - Roll-back protection using a TPM is done using the tpm command. This can
  424. be scripted, but we might consider a default way of doing this, built into
  425. bootm.
  426. Possible Future Work
  427. --------------------
  428. - Add support for other RSA/SHA variants, such as rsa4096,sha512.
  429. - Other algorithms besides RSA
  430. - More sandbox tests for failure modes
  431. - Passwords for keys/certificates
  432. - Perhaps implement OAEP
  433. - Enhance bootm to permit scripted signature verification (so that a script
  434. can verify an image but not actually boot it)
  435. Simon Glass
  436. sjg@chromium.org
  437. 1-1-13