1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
/* Copyright 2014 The ChromiumOS Authors
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
* Common functions between firmware and kernel verified boot.
* (Firmware portion)
*/
#include "2common.h"
#include "2rsa.h"
test_mockable
vb2_error_t vb2_safe_memcmp(const void *s1, const void *s2, size_t size)
{
const unsigned char *us1 = s1;
const unsigned char *us2 = s2;
int result = 0;
if (0 == size)
return 0;
/*
* Code snippet without data-dependent branch due to Nate Lawson
* (nate@root.org) of Root Labs.
*/
while (size--)
result |= *us1++ ^ *us2++;
return result != 0;
}
vb2_error_t vb2_align(uint8_t **ptr, uint32_t *size, uint32_t align,
uint32_t want_size)
{
uintptr_t p = (uintptr_t)*ptr;
uintptr_t offs = p & (align - 1);
if (offs) {
offs = align - offs;
if (*size < offs)
return VB2_ERROR_ALIGN_BIGGER_THAN_SIZE;
*ptr += offs;
*size -= offs;
}
if (*size < want_size)
return VB2_ERROR_ALIGN_SIZE;
return VB2_SUCCESS;
}
void vb2_workbuf_init(struct vb2_workbuf *wb, uint8_t *buf, uint32_t size)
{
wb->buf = buf;
wb->size = size;
/* Align the buffer so allocations will be aligned */
if (vb2_align(&wb->buf, &wb->size, VB2_WORKBUF_ALIGN, 0))
wb->size = 0;
}
void *vb2_workbuf_alloc(struct vb2_workbuf *wb, uint32_t size)
{
uint8_t *ptr = wb->buf;
/* Round up size to work buffer alignment */
size = vb2_wb_round_up(size);
if (size > wb->size)
return NULL;
wb->buf += size;
wb->size -= size;
return ptr;
}
void *vb2_workbuf_realloc(struct vb2_workbuf *wb, uint32_t oldsize,
uint32_t newsize)
{
/*
* Just free and allocate to update the size. No need to move/copy
* memory, since the new pointer is guaranteed to be the same as the
* old one. The new allocation can fail, if the new size is too big.
*/
vb2_workbuf_free(wb, oldsize);
return vb2_workbuf_alloc(wb, newsize);
}
void vb2_workbuf_free(struct vb2_workbuf *wb, uint32_t size)
{
/* Round up size to work buffer alignment */
size = vb2_wb_round_up(size);
wb->buf -= size;
wb->size += size;
}
ptrdiff_t vb2_offset_of(const void *base, const void *ptr)
{
return (uintptr_t)ptr - (uintptr_t)base;
}
void *vb2_member_of(void *parent, ptrdiff_t offset)
{
/* TODO(kitching): vb2_assert(parent && offset) */
return parent + offset;
}
vb2_error_t vb2_verify_member_inside(const void *parent, size_t parent_size,
const void *member, size_t member_size,
ptrdiff_t member_data_offset,
size_t member_data_size)
{
const uintptr_t parent_end = (uintptr_t)parent + parent_size;
const ptrdiff_t member_offs = vb2_offset_of(parent, member);
const ptrdiff_t member_end_offs = member_offs + member_size;
const ptrdiff_t data_offs = member_offs + member_data_offset;
const ptrdiff_t data_end_offs = data_offs + member_data_size;
/* Make sure parent doesn't wrap */
if (parent_end < (uintptr_t)parent)
return VB2_ERROR_INSIDE_PARENT_WRAPS;
/*
* Make sure the member is fully contained in the parent and doesn't
* wrap. Use >, not >=, since member_size = 0 is possible.
*/
if (member_end_offs < member_offs)
return VB2_ERROR_INSIDE_MEMBER_WRAPS;
if (member_offs < 0 || member_offs > parent_size ||
member_end_offs > parent_size)
return VB2_ERROR_INSIDE_MEMBER_OUTSIDE;
/* Make sure the member data is after the member */
if (member_data_size > 0 && data_offs < member_end_offs)
return VB2_ERROR_INSIDE_DATA_OVERLAP;
/* Make sure parent fully contains member data, if any */
if (data_end_offs < data_offs)
return VB2_ERROR_INSIDE_DATA_WRAPS;
if (data_offs < 0 || data_offs > parent_size ||
data_end_offs > parent_size)
return VB2_ERROR_INSIDE_DATA_OUTSIDE;
return VB2_SUCCESS;
}
test_mockable
vb2_error_t vb2_verify_digest(const struct vb2_public_key *key,
struct vb2_signature *sig, const uint8_t *digest,
const struct vb2_workbuf *wb)
{
/* A signature is destroyed in the process of being verified. */
uint8_t *sig_data = vb2_signature_data_mutable(sig);
if (!sig->data_size)
return VB2_ERROR_VDATA_NOT_ENOUGH_DATA;
if (sig->sig_size != vb2_rsa_sig_size(key->sig_alg)) {
VB2_DEBUG("Wrong data signature size for algorithm, "
"sig_size=%d, expected %d for algorithm %d.\n",
sig->sig_size, vb2_rsa_sig_size(key->sig_alg),
key->sig_alg);
return VB2_ERROR_VDATA_SIG_SIZE;
}
if (key->allow_hwcrypto) {
vb2_error_t rv =
vb2ex_hwcrypto_rsa_verify_digest(key, sig_data, digest);
if (rv != VB2_ERROR_EX_HWCRYPTO_UNSUPPORTED) {
VB2_DEBUG("Using HW RSA engine for sig_alg %d %s\n",
key->sig_alg,
rv ? "failed" : "succeeded");
return rv;
}
VB2_DEBUG("HW RSA for sig_alg %d not supported, using SW\n",
key->sig_alg);
} else {
VB2_DEBUG("HW RSA forbidden, using SW\n");
}
return vb2_rsa_verify_digest(key, sig_data, digest, wb);
}
test_mockable
vb2_error_t vb2_verify_data(const uint8_t *data, uint32_t size,
struct vb2_signature *sig,
const struct vb2_public_key *key,
const struct vb2_workbuf *wb)
{
struct vb2_hash hash;
if (sig->data_size > size) {
VB2_DEBUG("Data buffer smaller than length of signed data.\n");
return VB2_ERROR_VDATA_NOT_ENOUGH_DATA;
}
VB2_TRY(vb2_hash_calculate(key->allow_hwcrypto, data, sig->data_size,
key->hash_alg, &hash));
return vb2_verify_digest(key, sig, hash.raw, wb);
}
|