secrets.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* Copyright 2016 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. #include "2sysincludes.h"
  6. #include "2hmac.h"
  7. #include "2sha.h"
  8. #include "bdb_api.h"
  9. #include "bdb_struct.h"
  10. #include "bdb.h"
  11. #include "secrets.h"
  12. const uint8_t secret_constant_a[] = {
  13. 0xad, 0xf8, 0xd1, 0xd9, 0x48, 0xe6, 0xb3, 0xe4, 0xe0, 0xc4,
  14. 0xd8, 0x66, 0x97, 0x95, 0x71, 0xa8, 0xc6, 0xc6, 0xc6, 0xc6,
  15. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  16. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  17. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  18. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  19. 0xc6, 0xc6, 0xc6, 0x01};
  20. const uint8_t secret_constant_b[] = {
  21. 0xba, 0x9d, 0x1d, 0x8b, 0x12, 0xbd, 0x8d, 0xcd, 0x4c, 0x89,
  22. 0xd8, 0x18, 0x72, 0x98, 0xb5, 0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, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  42. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  43. 0xc6, 0xc6, 0xc6, 0x0a};
  44. const uint8_t secret_constant_c[] = {
  45. 0x46, 0xda, 0x52, 0x8d, 0x08, 0x56, 0x14, 0xde, 0x75, 0x9c,
  46. 0x9a, 0xeb, 0x08, 0x93, 0x3d, 0xd6, 0xc6, 0xc6, 0xc6, 0xc6,
  47. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  48. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  49. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  50. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  51. 0xc6, 0xc6, 0xc6, 0x0b};
  52. const uint8_t secret_constant_fv0[] = {
  53. 0x93, 0x32, 0xf7, 0x8d, 0xec, 0x4b, 0x26, 0x2e, 0xb3, 0x5c,
  54. 0x39, 0xd7, 0xfc, 0xc6, 0x9f, 0x09, 0xc6, 0xc6, 0xc6, 0xc6,
  55. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  56. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  57. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  58. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  59. 0xc6, 0xc6, 0xc6, 0x05};
  60. const uint8_t secret_constant_fv1[] = {
  61. 0x60, 0x8d, 0x96, 0x35, 0xdf, 0xf6, 0x31, 0x67, 0xab, 0xb8,
  62. 0x9f, 0x50, 0x81, 0x28, 0x82, 0xec, 0xc6, 0xc6, 0xc6, 0xc6,
  63. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  64. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  65. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  66. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  67. 0xc6, 0xc6, 0xc6, 0x06};
  68. const uint8_t secret_constant_kv0[] = {
  69. 0x46, 0x6d, 0xef, 0x2c, 0x05, 0xc9, 0xbf, 0xa9, 0x6b, 0xee,
  70. 0xaa, 0x6c, 0xb9, 0xb4, 0x6d, 0x37, 0xc6, 0xc6, 0xc6, 0xc6,
  71. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  72. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  73. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  74. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  75. 0xc6, 0xc6, 0xc6, 0x07};
  76. const uint8_t secret_constant_kv1[] = {
  77. 0x0a, 0x9e, 0xc9, 0x20, 0x29, 0xa3, 0x5d, 0xd7, 0x27, 0x55,
  78. 0xb6, 0xa6, 0xb4, 0x80, 0x7c, 0x73, 0xc6, 0xc6, 0xc6, 0xc6,
  79. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  80. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  81. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  82. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  83. 0xc6, 0xc6, 0xc6, 0x08};
  84. const uint8_t secret_constant_k[] = {
  85. /*
  86. * Digest of kernel data key struct fills first 32 bytes
  87. */
  88. 0x1e, 0x1d, 0xec, 0xf2, 0x6d, 0x27, 0xa6, 0xd9,
  89. 0x67, 0x0f, 0x34, 0xc5, 0xfa, 0x01, 0x68, 0xf6, 0xc6, 0xc6,
  90. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  91. 0xc6, 0xc6, 0xc6, 0x03};
  92. const uint8_t secret_constant_l[] = {
  93. /*
  94. * Digest of kernel data key struct fills first 32 bytes
  95. */
  96. 0x9b, 0xc0, 0x29, 0xd3, 0xc3, 0x90, 0x7f, 0x82,
  97. 0x56, 0xe2, 0x67, 0x79, 0x11, 0x74, 0xbe, 0xd0, 0xc6, 0xc6,
  98. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  99. 0xc6, 0xc6, 0xc6, 0x04};
  100. const uint8_t secret_constant_p[] = {
  101. /*
  102. * Digest of KDB key struct fills first 32 bytes
  103. */
  104. 0xfe, 0x31, 0xed, 0xed, 0x45, 0xfd, 0x8a, 0x5d,
  105. 0x87, 0x90, 0xac, 0x17, 0x02, 0x89, 0x2c, 0xba, 0xc6, 0xc6,
  106. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  107. 0xc6, 0xc6, 0xc6, 0x0c};
  108. const uint8_t secret_constant_q[] = {
  109. /*
  110. * Digest of KDB key struct fills first 32 bytes
  111. */
  112. 0xc7, 0x60, 0x83, 0x0f, 0x20, 0x44, 0x5d, 0x9c,
  113. 0x70, 0x96, 0x05, 0x2d, 0x51, 0x4b, 0x15, 0x99, 0xc6, 0xc6,
  114. 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
  115. 0xc6, 0xc6, 0xc6, 0x0d};
  116. /**
  117. * Get constant with digest
  118. *
  119. * This function computes a digest of the given buffer and concatenates it
  120. * to the given constant.
  121. *
  122. * @param buf Data from which a digest is computed
  123. * @param buf_size Size of <buf>
  124. * @param constant Buffer containing constant
  125. * @param out Buffer where the result is stored
  126. * @return BDB_SUCCESS on success or !BDB_SUCCESS otherwise
  127. */
  128. static int get_constant_with_digest(const uint8_t *buf, uint32_t buf_size,
  129. const uint8_t *constant, uint8_t *out)
  130. {
  131. int digest_size = vb2_digest_size(VB2_HASH_SHA256);
  132. const struct bdb_key *key = (const struct bdb_key *)buf;
  133. if (!buf)
  134. return !BDB_SUCCESS;
  135. if (bdb_check_key(key, buf_size))
  136. return !BDB_SUCCESS;
  137. if (vb2_digest_buffer(buf, buf_size, VB2_HASH_SHA256, out, digest_size))
  138. return !BDB_SUCCESS;
  139. memcpy(out + digest_size, constant,
  140. BDB_CONSTANT_BLOCK_SIZE - digest_size);
  141. return BDB_SUCCESS;
  142. }
  143. /**
  144. * Derive secrets for SP-RO
  145. *
  146. * This function extends a BDS to derive secrets as done by SP-RO (a.k.a. mask
  147. * rom).
  148. *
  149. * @param ctx VBoot context
  150. * @param type Type of secret to derive
  151. * @param wsr Work secret register
  152. * @param buf Data from which a digest is computed
  153. * @param buf_size Size of <buf>
  154. * @param extend sha256 extension function to be used
  155. * @return BDB_SUCCESS on success or BDB_ERROR_* otherwise
  156. */
  157. static int derive_secret_ro(struct vba_context *ctx, enum bdb_secret_type type,
  158. uint8_t *wsr, const uint8_t *buf, uint32_t buf_size,
  159. f_extend extend)
  160. {
  161. uint8_t c[BDB_CONSTANT_BLOCK_SIZE];
  162. uint8_t *from;
  163. const uint8_t *by = (const uint8_t *)c;
  164. uint8_t *to;
  165. switch (type) {
  166. case BDB_SECRET_TYPE_WSR:
  167. from = to = wsr;
  168. by = secret_constant_x;
  169. break;
  170. case BDB_SECRET_TYPE_BDB:
  171. from = wsr;
  172. to = ctx->secrets->bdb;
  173. if (get_constant_with_digest(buf, buf_size,
  174. secret_constant_p, c))
  175. return BDB_ERROR_SECRET_BDB;
  176. break;
  177. case BDB_SECRET_TYPE_BOOT_PATH:
  178. from = wsr;
  179. to = ctx->secrets->boot_path;
  180. if (get_constant_with_digest(buf, buf_size,
  181. secret_constant_k, c))
  182. return BDB_ERROR_SECRET_BOOT_PATH;
  183. break;
  184. case BDB_SECRET_TYPE_BOOT_VERIFIED:
  185. from = wsr;
  186. to = ctx->secrets->boot_verified;
  187. if (ctx->flags & VBA_CONTEXT_FLAG_BDB_KEY_EFUSED)
  188. by = secret_constant_fv0;
  189. else
  190. by = secret_constant_fv1;
  191. break;
  192. case BDB_SECRET_TYPE_NVM_WP:
  193. from = wsr;
  194. by = secret_constant_a;
  195. to = ctx->secrets->nvm_wp;
  196. break;
  197. case BDB_SECRET_TYPE_NVM_RW:
  198. from = ctx->secrets->nvm_wp;
  199. by = secret_constant_b;
  200. to = ctx->secrets->nvm_rw;
  201. break;
  202. default:
  203. return BDB_ERROR_SECRET_TYPE;
  204. }
  205. if (extend)
  206. extend(from, by, to);
  207. else
  208. vb2_sha256_extend(from, by, to);
  209. return BDB_SUCCESS;
  210. }
  211. int vba_derive_secret(struct vba_context *ctx, enum bdb_secret_type type,
  212. uint8_t *wsr, const uint8_t *buf, uint32_t buf_size)
  213. {
  214. uint8_t c[BDB_CONSTANT_BLOCK_SIZE];
  215. uint8_t *from;
  216. const uint8_t *by = (const uint8_t *)c;
  217. uint8_t *to;
  218. switch (type) {
  219. case BDB_SECRET_TYPE_WSR:
  220. from = to = wsr;
  221. by = secret_constant_y;
  222. break;
  223. case BDB_SECRET_TYPE_BDB:
  224. from = to = ctx->secrets->bdb;
  225. if (get_constant_with_digest(buf, buf_size,
  226. secret_constant_q, c))
  227. return BDB_ERROR_SECRET_BDB;
  228. break;
  229. case BDB_SECRET_TYPE_BOOT_PATH:
  230. from = to = ctx->secrets->boot_path;
  231. if (get_constant_with_digest(buf, buf_size,
  232. secret_constant_l, c))
  233. return BDB_ERROR_SECRET_BOOT_PATH;
  234. break;
  235. case BDB_SECRET_TYPE_BOOT_VERIFIED:
  236. from = to = ctx->secrets->boot_verified;
  237. if (ctx->flags & VBA_CONTEXT_FLAG_KERNEL_DATA_KEY_VERIFIED)
  238. by = secret_constant_kv1;
  239. else
  240. by = secret_constant_kv0;
  241. break;
  242. case BDB_SECRET_TYPE_BUC:
  243. from = ctx->secrets->boot_verified;
  244. by = secret_constant_c;
  245. to = ctx->secrets->buc;
  246. break;
  247. default:
  248. return BDB_ERROR_SECRET_TYPE;
  249. }
  250. vb2_sha256_extend(from, by, to);
  251. return BDB_SUCCESS;
  252. }
  253. int vba_clear_secret(struct vba_context *ctx, enum bdb_secret_type type)
  254. {
  255. uint8_t *s;
  256. switch (type) {
  257. case BDB_SECRET_TYPE_NVM_RW:
  258. s = ctx->secrets->nvm_rw;
  259. break;
  260. case BDB_SECRET_TYPE_BDB:
  261. s = ctx->secrets->bdb;
  262. break;
  263. case BDB_SECRET_TYPE_BOOT_PATH:
  264. s = ctx->secrets->boot_path;
  265. break;
  266. case BDB_SECRET_TYPE_BOOT_VERIFIED:
  267. s = ctx->secrets->boot_verified;
  268. break;
  269. case BDB_SECRET_TYPE_BUC:
  270. s = ctx->secrets->buc;
  271. break;
  272. default:
  273. return BDB_ERROR_SECRET_TYPE;
  274. }
  275. memset(s, 0, BDB_SECRET_SIZE);
  276. return BDB_SUCCESS;
  277. }
  278. int vba_extend_secrets_ro(struct vba_context *ctx, const uint8_t *bdb,
  279. uint8_t *wsr, f_extend extend)
  280. {
  281. const struct bdb_key *bdbkey = bdb_get_bdbkey(bdb);
  282. const struct bdb_key *datakey = bdb_get_datakey(bdb);
  283. derive_secret_ro(ctx, BDB_SECRET_TYPE_BDB, wsr, (const uint8_t *)bdbkey,
  284. bdbkey->struct_size, extend);
  285. derive_secret_ro(ctx, BDB_SECRET_TYPE_BOOT_PATH, wsr,
  286. (const uint8_t *)datakey, datakey->struct_size,
  287. extend);
  288. derive_secret_ro(ctx, BDB_SECRET_TYPE_BOOT_VERIFIED, wsr, NULL, 0,
  289. extend);
  290. derive_secret_ro(ctx, BDB_SECRET_TYPE_NVM_WP, wsr, NULL, 0, extend);
  291. /* Deriving NVM-RW has to be done after NVM-WP */
  292. derive_secret_ro(ctx, BDB_SECRET_TYPE_NVM_RW, wsr, NULL, 0, extend);
  293. /* Extending WSR has to be done last. */
  294. derive_secret_ro(ctx, BDB_SECRET_TYPE_WSR, wsr, NULL, 0, extend);
  295. return BDB_SUCCESS;
  296. }