2secdatak.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Copyright 2015 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. * Secure storage APIs - kernel version space
  6. */
  7. #include "2sysincludes.h"
  8. #include "2common.h"
  9. #include "2crc8.h"
  10. #include "2misc.h"
  11. #include "2secdata.h"
  12. int vb2_secdatak_check_crc(const struct vb2_context *ctx)
  13. {
  14. const struct vb2_secdatak *sec =
  15. (const struct vb2_secdatak *)ctx->secdatak;
  16. /* Verify CRC */
  17. if (sec->crc8 != vb2_crc8(sec, offsetof(struct vb2_secdatak, crc8)))
  18. return VB2_ERROR_SECDATAK_CRC;
  19. return VB2_SUCCESS;
  20. }
  21. int vb2_secdatak_create(struct vb2_context *ctx)
  22. {
  23. struct vb2_secdatak *sec = (struct vb2_secdatak *)ctx->secdatak;
  24. /* Clear the entire struct */
  25. memset(sec, 0, sizeof(*sec));
  26. /* Set to current version */
  27. sec->struct_version = VB2_SECDATAK_VERSION;
  28. /* Set UID */
  29. sec->uid = VB2_SECDATAK_UID;
  30. /* Calculate initial CRC */
  31. sec->crc8 = vb2_crc8(sec, offsetof(struct vb2_secdatak, crc8));
  32. ctx->flags |= VB2_CONTEXT_SECDATAK_CHANGED;
  33. return VB2_SUCCESS;
  34. }
  35. int vb2_secdatak_init(struct vb2_context *ctx)
  36. {
  37. struct vb2_secdatak *sec = (struct vb2_secdatak *)ctx->secdatak;
  38. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  39. int rv;
  40. rv = vb2_secdatak_check_crc(ctx);
  41. if (rv)
  42. return rv;
  43. /* Make sure the UID is correct */
  44. if (sec->uid != VB2_SECDATAK_UID)
  45. return VB2_ERROR_SECDATAK_UID;
  46. /* Set status flag */
  47. sd->status |= VB2_SD_STATUS_SECDATAK_INIT;
  48. /* TODO: unit test for that */
  49. return VB2_SUCCESS;
  50. }
  51. int vb2_secdatak_get(struct vb2_context *ctx,
  52. enum vb2_secdatak_param param,
  53. uint32_t *dest)
  54. {
  55. struct vb2_secdatak *sec = (struct vb2_secdatak *)ctx->secdatak;
  56. if (!(vb2_get_sd(ctx)->status & VB2_SD_STATUS_SECDATAK_INIT))
  57. return VB2_ERROR_SECDATAK_GET_UNINITIALIZED;
  58. switch(param) {
  59. case VB2_SECDATAK_VERSIONS:
  60. *dest = sec->kernel_versions;
  61. return VB2_SUCCESS;
  62. default:
  63. return VB2_ERROR_SECDATAK_GET_PARAM;
  64. }
  65. }
  66. int vb2_secdatak_set(struct vb2_context *ctx,
  67. enum vb2_secdatak_param param,
  68. uint32_t value)
  69. {
  70. struct vb2_secdatak *sec = (struct vb2_secdatak *)ctx->secdatak;
  71. uint32_t now;
  72. if (!(vb2_get_sd(ctx)->status & VB2_SD_STATUS_SECDATAK_INIT))
  73. return VB2_ERROR_SECDATAK_SET_UNINITIALIZED;
  74. /* If not changing the value, don't regenerate the CRC. */
  75. if (vb2_secdatak_get(ctx, param, &now) == VB2_SUCCESS && now == value)
  76. return VB2_SUCCESS;
  77. switch(param) {
  78. case VB2_SECDATAK_VERSIONS:
  79. sec->kernel_versions = value;
  80. break;
  81. default:
  82. return VB2_ERROR_SECDATAK_SET_PARAM;
  83. }
  84. /* Regenerate CRC */
  85. sec->crc8 = vb2_crc8(sec, offsetof(struct vb2_secdatak, crc8));
  86. ctx->flags |= VB2_CONTEXT_SECDATAK_CHANGED;
  87. return VB2_SUCCESS;
  88. }