packed_key.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (c) 2014 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. * Key unpacking functions
  6. */
  7. #include "2sysincludes.h"
  8. #include "2common.h"
  9. #include "2rsa.h"
  10. #include "vb21_common.h"
  11. int vb2_unpack_key_data(struct vb2_public_key *key,
  12. const uint8_t *key_data,
  13. uint32_t key_size)
  14. {
  15. const uint32_t *buf32 = (const uint32_t *)key_data;
  16. uint32_t expected_key_size = vb2_packed_key_size(key->sig_alg);
  17. /* Make sure buffer is the correct length */
  18. if (!expected_key_size || expected_key_size != key_size) {
  19. VB2_DEBUG("Wrong key size for algorithm\n");
  20. return VB2_ERROR_UNPACK_KEY_SIZE;
  21. }
  22. /* Check for alignment */
  23. if (!vb2_aligned(buf32, sizeof(uint32_t)))
  24. return VB2_ERROR_UNPACK_KEY_ALIGN;
  25. key->arrsize = buf32[0];
  26. /* Sanity check key array size */
  27. if (key->arrsize * sizeof(uint32_t) != vb2_rsa_sig_size(key->sig_alg))
  28. return VB2_ERROR_UNPACK_KEY_ARRAY_SIZE;
  29. key->n0inv = buf32[1];
  30. /* Arrays point inside the key data */
  31. key->n = buf32 + 2;
  32. key->rr = buf32 + 2 + key->arrsize;
  33. return VB2_SUCCESS;
  34. }
  35. int vb21_unpack_key(struct vb2_public_key *key,
  36. const uint8_t *buf,
  37. uint32_t size)
  38. {
  39. const struct vb21_packed_key *pkey =
  40. (const struct vb21_packed_key *)buf;
  41. uint32_t sig_size;
  42. uint32_t min_offset = 0;
  43. int rv;
  44. /* Check magic number */
  45. if (pkey->c.magic != VB21_MAGIC_PACKED_KEY)
  46. return VB2_ERROR_UNPACK_KEY_MAGIC;
  47. rv = vb21_verify_common_header(buf, size);
  48. if (rv)
  49. return rv;
  50. /* Make sure key data is inside */
  51. rv = vb21_verify_common_member(pkey, &min_offset,
  52. pkey->key_offset, pkey->key_size);
  53. if (rv)
  54. return rv;
  55. /*
  56. * Check for compatible version. No need to check minor version, since
  57. * that's compatible across readers matching the major version, and we
  58. * haven't added any new fields.
  59. */
  60. if (pkey->c.struct_version_major != VB21_PACKED_KEY_VERSION_MAJOR)
  61. return VB2_ERROR_UNPACK_KEY_STRUCT_VERSION;
  62. /* Copy key algorithms */
  63. key->hash_alg = pkey->hash_alg;
  64. if (!vb2_digest_size(key->hash_alg))
  65. return VB2_ERROR_UNPACK_KEY_HASH_ALGORITHM;
  66. key->sig_alg = pkey->sig_alg;
  67. if (key->sig_alg != VB2_SIG_NONE) {
  68. sig_size = vb2_rsa_sig_size(key->sig_alg);
  69. if (!sig_size)
  70. return VB2_ERROR_UNPACK_KEY_SIG_ALGORITHM;
  71. rv = vb2_unpack_key_data(
  72. key,
  73. (const uint8_t *)pkey + pkey->key_offset,
  74. pkey->key_size);
  75. if (rv)
  76. return rv;
  77. }
  78. /* Key description */
  79. key->desc = vb21_common_desc(pkey);
  80. key->version = pkey->key_version;
  81. key->id = &pkey->id;
  82. return VB2_SUCCESS;
  83. }