blob: 2ad62b07d1dce8528c95b9f54cc5eb9e462c5f1d [file] [log] [blame]
Randall Spangler729b8722010-06-11 11:16:20 -07001/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 *
5 * Host functions for verified boot.
6 */
7
Randall Spangler729b8722010-06-11 11:16:20 -07008#include "host_keyblock.h"
9
10#include "cryptolib.h"
11#include "host_common.h"
12#include "utility.h"
13#include "vboot_common.h"
14
15
16VbKeyBlockHeader* KeyBlockCreate(const VbPublicKey* data_key,
17 const VbPrivateKey* signing_key,
18 uint64_t flags) {
19
20 VbKeyBlockHeader* h;
21 uint64_t signed_size = sizeof(VbKeyBlockHeader) + data_key->key_size;
22 uint64_t block_size = (signed_size + SHA512_DIGEST_SIZE +
Randall Spangler138acfe2010-08-17 15:45:21 -070023 (signing_key ?
24 siglen_map[signing_key->algorithm] : 0));
Randall Spangler729b8722010-06-11 11:16:20 -070025 uint8_t* data_key_dest;
26 uint8_t* block_sig_dest;
27 uint8_t* block_chk_dest;
28 VbSignature *sigtmp;
29
30 /* Allocate key block */
31 h = (VbKeyBlockHeader*)Malloc(block_size);
32 if (!h)
33 return NULL;
34 data_key_dest = (uint8_t*)(h + 1);
35 block_chk_dest = data_key_dest + data_key->key_size;
36 block_sig_dest = block_chk_dest + SHA512_DIGEST_SIZE;
37
38 Memcpy(h->magic, KEY_BLOCK_MAGIC, KEY_BLOCK_MAGIC_SIZE);
39 h->header_version_major = KEY_BLOCK_HEADER_VERSION_MAJOR;
40 h->header_version_minor = KEY_BLOCK_HEADER_VERSION_MINOR;
41 h->key_block_size = block_size;
42 h->key_block_flags = flags;
43
44 /* Copy data key */
45 PublicKeyInit(&h->data_key, data_key_dest, data_key->key_size);
46 PublicKeyCopy(&h->data_key, data_key);
47
48 /* Set up signature structs so we can calculate the signatures */
49 SignatureInit(&h->key_block_checksum, block_chk_dest,
50 SHA512_DIGEST_SIZE, signed_size);
Bill Richardson4f36ef32010-08-09 17:50:14 -070051 if (signing_key)
52 SignatureInit(&h->key_block_signature, block_sig_dest,
53 siglen_map[signing_key->algorithm], signed_size);
54 else
55 Memset(&h->key_block_signature, 0, sizeof(VbSignature));
Randall Spangler729b8722010-06-11 11:16:20 -070056
57 /* Calculate checksum */
58 sigtmp = CalculateChecksum((uint8_t*)h, signed_size);
59 SignatureCopy(&h->key_block_checksum, sigtmp);
60 Free(sigtmp);
61
62 /* Calculate signature */
Bill Richardson4f36ef32010-08-09 17:50:14 -070063 if (signing_key) {
64 sigtmp = CalculateSignature((uint8_t*)h, signed_size, signing_key);
65 SignatureCopy(&h->key_block_signature, sigtmp);
66 Free(sigtmp);
67 }
Randall Spangler729b8722010-06-11 11:16:20 -070068
69 /* Return the header */
70 return h;
71}
72
73
74/* Read a key block from a .keyblock file. Caller owns the returned
75 * pointer, and must free it with Free().
76 *
77 * Returns NULL if error. */
78VbKeyBlockHeader* KeyBlockRead(const char* filename) {
79
80 VbKeyBlockHeader* block;
81 uint64_t file_size;
82
83 block = (VbKeyBlockHeader*)ReadFile(filename, &file_size);
84 if (!block) {
Bill Richardsonabf05502010-07-01 10:22:06 -070085 VBDEBUG(("Error reading key block file: %s\n", filename));
Randall Spangler729b8722010-06-11 11:16:20 -070086 return NULL;
87 }
88
89 /* Verify the hash of the key block, since we can do that without
90 * the public signing key. */
Randall Spangler138acfe2010-08-17 15:45:21 -070091 if (0 != KeyBlockVerify(block, file_size, NULL, 1)) {
Bill Richardsonabf05502010-07-01 10:22:06 -070092 VBDEBUG(("Invalid key block file: filename\n", filename));
Randall Spangler729b8722010-06-11 11:16:20 -070093 Free(block);
94 return NULL;
95 }
96
97 return block;
98}
99
100
101/* Write a key block to a file in .keyblock format. */
102int KeyBlockWrite(const char* filename, const VbKeyBlockHeader* key_block) {
103
104 if (0 != WriteFile(filename, key_block, key_block->key_block_size)) {
Bill Richardsonabf05502010-07-01 10:22:06 -0700105 VBDEBUG(("KeyBlockWrite() error writing key block\n"));
Randall Spangler729b8722010-06-11 11:16:20 -0700106 return 1;
107 }
108
109 return 0;
110}