host_key.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* 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-side functions for verified boot.
  6. */
  7. #ifndef VBOOT_REFERENCE_HOST_KEY_H_
  8. #define VBOOT_REFERENCE_HOST_KEY_H_
  9. #include "2crypto.h"
  10. struct vb2_packed_key;
  11. struct vb2_private_key;
  12. /**
  13. * Convert a vb2 hash and crypto algorithm to a vb1 crypto algorithm.
  14. *
  15. * @param hash_alg Hash algorithm
  16. * @param sig_alg Signature algorithm
  17. *
  18. * @return The equivalent vb1 crypto algorithm or VB2_ALG_COUNT if error.
  19. */
  20. enum vb2_crypto_algorithm vb2_get_crypto_algorithm(
  21. enum vb2_hash_algorithm hash_alg,
  22. enum vb2_signature_algorithm sig_alg);
  23. /**
  24. * Read a private key from a .pem file.
  25. *
  26. * @param filename Filename to read from
  27. * @param algorithm Algorithm to associate with file
  28. * (enum vb2_crypto_algorithm)
  29. *
  30. * @return The private key or NULL if error. Caller must free() it.
  31. */
  32. struct vb2_private_key *vb2_read_private_key_pem(
  33. const char *filename,
  34. enum vb2_crypto_algorithm algorithm);
  35. /**
  36. * Free a private key.
  37. *
  38. * @param key Key to free; ok to pass NULL (ignored).
  39. */
  40. void vb2_free_private_key(struct vb2_private_key *key);
  41. /**
  42. * Write a private key to a file in .vbprivk format.
  43. *
  44. * @param filename Filename to write to
  45. * @param key Key to write
  46. *
  47. * @return VB2_SUCCESS, or non-zero if error.
  48. */
  49. int vb2_write_private_key(const char *filename,
  50. const struct vb2_private_key *key);
  51. /**
  52. * Read a private key from a .vbprivk file.
  53. *
  54. * @param filename Filename to read key from.
  55. *
  56. * @return The private key or NULL if error. Caller must free() it.
  57. */
  58. struct vb2_private_key *vb2_read_private_key(const char *filename);
  59. /**
  60. * Allocate a new public key.
  61. * @param key_size Size of key data the key can hold
  62. * @param algorithm Algorithm to store in key header
  63. * @param version Version to store in key header
  64. *
  65. * @return The public key or NULL if error. Caller must free() it.
  66. */
  67. struct vb2_packed_key *vb2_alloc_packed_key(uint32_t key_size,
  68. uint32_t algorithm,
  69. uint32_t version);
  70. /**
  71. * Initialize a packed key structure.
  72. *
  73. * @param key Structure to initialize
  74. * @param key_data Pointer to key data (following the structure)
  75. * @param key_size Size of key
  76. */
  77. void vb2_init_packed_key(struct vb2_packed_key *key, uint8_t *key_data,
  78. uint32_t key_size);
  79. /**
  80. * Copy a packed key.
  81. *
  82. * @param dest Destination packed key
  83. * @param src Source packed key
  84. *
  85. * @return VB2_SUCCESS, or non-zero if error.
  86. */
  87. int vb2_copy_packed_key(struct vb2_packed_key *dest,
  88. const struct vb2_packed_key *src);
  89. /**
  90. * Read a packed key from a .vbpubk file.
  91. *
  92. * @param filename Name of file to read
  93. * @param algorithm Crypto algorithm to associate with key
  94. * @param version Version to store in key
  95. *
  96. * @return The packed key, or NULL if error. Caller must free() it.
  97. */
  98. struct vb2_packed_key *vb2_read_packed_key(const char *filename);
  99. /**
  100. * Sanity-check a packed key structure.
  101. *
  102. * @param key Key to check
  103. * @param size Size of key buffer in bytes
  104. *
  105. * @return True if the key struct appears valid.
  106. */
  107. int packed_key_looks_ok(const struct vb2_packed_key *key, uint32_t size);
  108. /**
  109. * Read a packed key from a .keyb file.
  110. *
  111. * @param filename Name of file to read
  112. * @param algorithm Crypto algorithm to associate with key
  113. * @param version Version to store in key
  114. *
  115. * @return The packed key, or NULL if error. Caller must free() it.
  116. */
  117. struct vb2_packed_key *vb2_read_packed_keyb(const char *filename,
  118. uint32_t algorithm,
  119. uint32_t version);
  120. /**
  121. * Write a packed key in .vbpubk format.
  122. *
  123. * @param filename Name of file to write
  124. * @param key Key to write
  125. *
  126. * @return VB2_SUCCESS, or non-zero if error.
  127. */
  128. int vb2_write_packed_key(const char *filename,
  129. const struct vb2_packed_key *key);
  130. #endif /* VBOOT_REFERENCE_HOST_KEY_H_ */