2common.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * Common functions between firmware and kernel verified boot.
  6. * (Firmware portion)
  7. */
  8. #include "2sysincludes.h"
  9. #include "2common.h"
  10. #include "2rsa.h"
  11. #include "2sha.h"
  12. int vb2_safe_memcmp(const void *s1, const void *s2, size_t size)
  13. {
  14. const unsigned char *us1 = s1;
  15. const unsigned char *us2 = s2;
  16. int result = 0;
  17. if (0 == size)
  18. return 0;
  19. /*
  20. * Code snippet without data-dependent branch due to Nate Lawson
  21. * (nate@root.org) of Root Labs.
  22. */
  23. while (size--)
  24. result |= *us1++ ^ *us2++;
  25. return result != 0;
  26. }
  27. int vb2_align(uint8_t **ptr, uint32_t *size, uint32_t align, uint32_t want_size)
  28. {
  29. uintptr_t p = (uintptr_t)*ptr;
  30. uintptr_t offs = p & (align - 1);
  31. if (offs) {
  32. offs = align - offs;
  33. if (*size < offs)
  34. return VB2_ERROR_ALIGN_BIGGER_THAN_SIZE;
  35. *ptr += offs;
  36. *size -= offs;
  37. }
  38. if (*size < want_size)
  39. return VB2_ERROR_ALIGN_SIZE;
  40. return VB2_SUCCESS;
  41. }
  42. void vb2_workbuf_init(struct vb2_workbuf *wb, uint8_t *buf, uint32_t size)
  43. {
  44. wb->buf = buf;
  45. wb->size = size;
  46. /* Align the buffer so allocations will be aligned */
  47. if (vb2_align(&wb->buf, &wb->size, VB2_WORKBUF_ALIGN, 0))
  48. wb->size = 0;
  49. }
  50. /**
  51. * Round up a number to a multiple of VB2_WORKBUF_ALIGN
  52. *
  53. * @param v Number to round up
  54. * @return The number, rounded up.
  55. */
  56. static __inline uint32_t wb_round_up(uint32_t v)
  57. {
  58. return (v + VB2_WORKBUF_ALIGN - 1) & ~(VB2_WORKBUF_ALIGN - 1);
  59. }
  60. void *vb2_workbuf_alloc(struct vb2_workbuf *wb, uint32_t size)
  61. {
  62. uint8_t *ptr = wb->buf;
  63. /* Round up size to work buffer alignment */
  64. size = wb_round_up(size);
  65. if (size > wb->size)
  66. return NULL;
  67. wb->buf += size;
  68. wb->size -= size;
  69. return ptr;
  70. }
  71. void *vb2_workbuf_realloc(struct vb2_workbuf *wb,
  72. uint32_t oldsize,
  73. uint32_t newsize)
  74. {
  75. /*
  76. * Just free and allocate to update the size. No need to move/copy
  77. * memory, since the new pointer is guaranteed to be the same as the
  78. * old one. The new allocation can fail, if the new size is too big.
  79. */
  80. vb2_workbuf_free(wb, oldsize);
  81. return vb2_workbuf_alloc(wb, newsize);
  82. }
  83. void vb2_workbuf_free(struct vb2_workbuf *wb, uint32_t size)
  84. {
  85. /* Round up size to work buffer alignment */
  86. size = wb_round_up(size);
  87. wb->buf -= size;
  88. wb->size += size;
  89. }
  90. ptrdiff_t vb2_offset_of(const void *base, const void *ptr)
  91. {
  92. return (uintptr_t)ptr - (uintptr_t)base;
  93. }