host_signature.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. * Host functions for signatures.
  6. */
  7. #include <openssl/rsa.h>
  8. #include "2sysincludes.h"
  9. #include "2common.h"
  10. #include "2rsa.h"
  11. #include "2sha.h"
  12. #include "vb21_common.h"
  13. #include "host_common.h"
  14. #include "host_key2.h"
  15. #include "host_signature2.h"
  16. #include "host_misc.h"
  17. int vb2_digest_info(enum vb2_hash_algorithm hash_alg,
  18. const uint8_t **buf_ptr,
  19. uint32_t *size_ptr)
  20. {
  21. *buf_ptr = NULL;
  22. *size_ptr = 0;
  23. switch (hash_alg) {
  24. #if VB2_SUPPORT_SHA1
  25. case VB2_HASH_SHA1:
  26. {
  27. static const uint8_t info[] = {
  28. 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e,
  29. 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14
  30. };
  31. *buf_ptr = info;
  32. *size_ptr = sizeof(info);
  33. return VB2_SUCCESS;
  34. }
  35. #endif
  36. #if VB2_SUPPORT_SHA256
  37. case VB2_HASH_SHA256:
  38. {
  39. static const uint8_t info[] = {
  40. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  41. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  42. 0x00, 0x04, 0x20
  43. };
  44. *buf_ptr = info;
  45. *size_ptr = sizeof(info);
  46. return VB2_SUCCESS;
  47. }
  48. #endif
  49. #if VB2_SUPPORT_SHA512
  50. case VB2_HASH_SHA512:
  51. {
  52. static const uint8_t info[] = {
  53. 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  54. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05,
  55. 0x00, 0x04, 0x40
  56. };
  57. *buf_ptr = info;
  58. *size_ptr = sizeof(info);
  59. return VB2_SUCCESS;
  60. }
  61. #endif
  62. default:
  63. return VB2_ERROR_DIGEST_INFO;
  64. }
  65. }
  66. int vb21_sign_data(struct vb21_signature **sig_ptr,
  67. const uint8_t *data,
  68. uint32_t size,
  69. const struct vb2_private_key *key,
  70. const char *desc)
  71. {
  72. struct vb21_signature s = {
  73. .c.magic = VB21_MAGIC_SIGNATURE,
  74. .c.struct_version_major = VB21_SIGNATURE_VERSION_MAJOR,
  75. .c.struct_version_minor = VB21_SIGNATURE_VERSION_MINOR,
  76. .c.fixed_size = sizeof(s),
  77. .sig_alg = key->sig_alg,
  78. .hash_alg = key->hash_alg,
  79. .data_size = size,
  80. .id = key->id,
  81. };
  82. struct vb2_digest_context dc;
  83. uint32_t digest_size;
  84. const uint8_t *info = NULL;
  85. uint32_t info_size = 0;
  86. uint32_t sig_digest_size;
  87. uint8_t *sig_digest;
  88. uint8_t *buf;
  89. *sig_ptr = NULL;
  90. /* Use key description if no description supplied */
  91. if (!desc)
  92. desc = key->desc;
  93. s.c.desc_size = vb2_desc_size(desc);
  94. s.sig_offset = s.c.fixed_size + s.c.desc_size;
  95. s.sig_size = vb2_sig_size(key->sig_alg, key->hash_alg);
  96. if (!s.sig_size)
  97. return VB2_SIGN_DATA_SIG_SIZE;
  98. s.c.total_size = s.sig_offset + s.sig_size;
  99. /* Determine digest size and allocate buffer */
  100. if (s.sig_alg != VB2_SIG_NONE) {
  101. if (vb2_digest_info(s.hash_alg, &info, &info_size))
  102. return VB2_SIGN_DATA_DIGEST_INFO;
  103. }
  104. digest_size = vb2_digest_size(key->hash_alg);
  105. if (!digest_size)
  106. return VB2_SIGN_DATA_DIGEST_SIZE;
  107. sig_digest_size = info_size + digest_size;
  108. sig_digest = malloc(sig_digest_size);
  109. if (!sig_digest)
  110. return VB2_SIGN_DATA_DIGEST_ALLOC;
  111. /* Prepend digest info, if any */
  112. if (info_size)
  113. memcpy(sig_digest, info, info_size);
  114. /* Calculate hash digest */
  115. if (vb2_digest_init(&dc, s.hash_alg)) {
  116. free(sig_digest);
  117. return VB2_SIGN_DATA_DIGEST_INIT;
  118. }
  119. if (vb2_digest_extend(&dc, data, size)) {
  120. free(sig_digest);
  121. return VB2_SIGN_DATA_DIGEST_EXTEND;
  122. }
  123. if (vb2_digest_finalize(&dc, sig_digest + info_size, digest_size)) {
  124. free(sig_digest);
  125. return VB2_SIGN_DATA_DIGEST_FINALIZE;
  126. }
  127. /* Allocate signature buffer and copy header */
  128. buf = calloc(1, s.c.total_size);
  129. memcpy(buf, &s, sizeof(s));
  130. /* strcpy() is ok because we allocated buffer based on desc length */
  131. if (desc)
  132. strcpy((char *)buf + s.c.fixed_size, desc);
  133. if (s.sig_alg == VB2_SIG_NONE) {
  134. /* Bare hash signature is just the digest */
  135. memcpy(buf + s.sig_offset, sig_digest, sig_digest_size);
  136. } else {
  137. /* RSA-encrypt the signature */
  138. if (RSA_private_encrypt(sig_digest_size,
  139. sig_digest,
  140. buf + s.sig_offset,
  141. key->rsa_private_key,
  142. RSA_PKCS1_PADDING) == -1) {
  143. free(sig_digest);
  144. free(buf);
  145. return VB2_SIGN_DATA_RSA_ENCRYPT;
  146. }
  147. }
  148. free(sig_digest);
  149. *sig_ptr = (struct vb21_signature *)buf;
  150. return VB2_SUCCESS;
  151. }
  152. int vb21_sig_size_for_key(uint32_t *size_ptr,
  153. const struct vb2_private_key *key,
  154. const char *desc)
  155. {
  156. uint32_t size = vb2_sig_size(key->sig_alg, key->hash_alg);
  157. if (!size)
  158. return VB2_ERROR_SIG_SIZE_FOR_KEY;
  159. size += sizeof(struct vb21_signature);
  160. size += vb2_desc_size(desc ? desc : key->desc);
  161. *size_ptr = size;
  162. return VB2_SUCCESS;
  163. }
  164. int vb21_sig_size_for_keys(uint32_t *size_ptr,
  165. const struct vb2_private_key **key_list,
  166. uint32_t key_count)
  167. {
  168. uint32_t total = 0, size = 0;
  169. int rv, i;
  170. *size_ptr = 0;
  171. for (i = 0; i < key_count; i++) {
  172. rv = vb21_sig_size_for_key(&size, key_list[i], NULL);
  173. if (rv)
  174. return rv;
  175. total += size;
  176. }
  177. *size_ptr = total;
  178. return VB2_SUCCESS;
  179. }
  180. int vb21_sign_object(uint8_t *buf,
  181. uint32_t sig_offset,
  182. const struct vb2_private_key *key,
  183. const char *desc)
  184. {
  185. struct vb21_struct_common *c = (struct vb21_struct_common *)buf;
  186. struct vb21_signature *sig = NULL;
  187. int rv;
  188. rv = vb21_sign_data(&sig, buf, sig_offset, key, desc);
  189. if (rv)
  190. return rv;
  191. if (sig_offset + sig->c.total_size > c->total_size) {
  192. free(sig);
  193. return VB2_SIGN_OBJECT_OVERFLOW;
  194. }
  195. memcpy(buf + sig_offset, sig, sig->c.total_size);
  196. free(sig);
  197. return VB2_SUCCESS;
  198. }
  199. int vb21_sign_object_multiple(uint8_t *buf,
  200. uint32_t sig_offset,
  201. const struct vb2_private_key **key_list,
  202. uint32_t key_count)
  203. {
  204. struct vb21_struct_common *c = (struct vb21_struct_common *)buf;
  205. uint32_t sig_next = sig_offset;
  206. int rv, i;
  207. for (i = 0; i < key_count; i++) {
  208. struct vb21_signature *sig = NULL;
  209. rv = vb21_sign_data(&sig, buf, sig_offset, key_list[i], NULL);
  210. if (rv)
  211. return rv;
  212. if (sig_next + sig->c.total_size > c->total_size) {
  213. free(sig);
  214. return VB2_SIGN_OBJECT_OVERFLOW;
  215. }
  216. memcpy(buf + sig_next, sig, sig->c.total_size);
  217. sig_next += sig->c.total_size;
  218. free(sig);
  219. }
  220. return VB2_SUCCESS;
  221. }