host_signature2.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Copyright (c) 2011 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 signature generation.
  6. */
  7. #include <openssl/rsa.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <sys/types.h>
  11. #include <sys/wait.h>
  12. #include <unistd.h>
  13. #include "2sysincludes.h"
  14. #include "2common.h"
  15. #include "2rsa.h"
  16. #include "2sha.h"
  17. #include "file_keys.h"
  18. #include "host_common.h"
  19. #include "host_key2.h"
  20. #include "host_signature2.h"
  21. #include "vb2_common.h"
  22. #include "vboot_common.h"
  23. struct vb2_signature *vb2_alloc_signature(uint32_t sig_size,
  24. uint32_t data_size)
  25. {
  26. struct vb2_signature *sig = (struct vb2_signature *)
  27. calloc(sizeof(*sig) + sig_size, 1);
  28. if (!sig)
  29. return NULL;
  30. sig->sig_offset = sizeof(*sig);
  31. sig->sig_size = sig_size;
  32. sig->data_size = data_size;
  33. return sig;
  34. }
  35. void vb2_init_signature(struct vb2_signature *sig, uint8_t *sig_data,
  36. uint32_t sig_size, uint32_t data_size)
  37. {
  38. memset(sig, 0, sizeof(*sig));
  39. sig->sig_offset = vb2_offset_of(sig, sig_data);
  40. sig->sig_size = sig_size;
  41. sig->data_size = data_size;
  42. }
  43. int vb2_copy_signature(struct vb2_signature *dest,
  44. const struct vb2_signature *src)
  45. {
  46. if (dest->sig_size < src->sig_size)
  47. return VB2_ERROR_SIG_SIZE;
  48. dest->sig_size = src->sig_size;
  49. dest->data_size = src->data_size;
  50. memcpy(vb2_signature_data(dest),
  51. vb2_signature_data((struct vb2_signature *)src),
  52. src->sig_size);
  53. return VB2_SUCCESS;
  54. }
  55. struct vb2_signature *vb2_sha512_signature(const uint8_t *data, uint32_t size)
  56. {
  57. uint8_t digest[VB2_SHA512_DIGEST_SIZE];
  58. if (VB2_SUCCESS != vb2_digest_buffer(data, size, VB2_HASH_SHA512,
  59. digest, sizeof(digest)))
  60. return NULL;
  61. struct vb2_signature *sig =
  62. vb2_alloc_signature(VB2_SHA512_DIGEST_SIZE, size);
  63. if (!sig)
  64. return NULL;
  65. memcpy(vb2_signature_data(sig), digest, VB2_SHA512_DIGEST_SIZE);
  66. return sig;
  67. }
  68. struct vb2_signature *vb2_calculate_signature(
  69. const uint8_t *data, uint32_t size,
  70. const struct vb2_private_key *key)
  71. {
  72. uint8_t digest[VB2_MAX_DIGEST_SIZE];
  73. uint32_t digest_size = vb2_digest_size(key->hash_alg);
  74. uint32_t digest_info_size = 0;
  75. const uint8_t *digest_info = NULL;
  76. if (VB2_SUCCESS != vb2_digest_info(key->hash_alg,
  77. &digest_info, &digest_info_size))
  78. return NULL;
  79. /* Calculate the digest */
  80. if (VB2_SUCCESS != vb2_digest_buffer(data, size, key->hash_alg,
  81. digest, digest_size))
  82. return NULL;
  83. /* Prepend the digest info to the digest */
  84. int signature_digest_len = digest_size + digest_info_size;
  85. uint8_t *signature_digest = malloc(signature_digest_len);
  86. if (!signature_digest)
  87. return NULL;
  88. memcpy(signature_digest, digest_info, digest_info_size);
  89. memcpy(signature_digest + digest_info_size, digest, digest_size);
  90. /* Allocate output signature */
  91. struct vb2_signature *sig = (struct vb2_signature *)
  92. vb2_alloc_signature(vb2_rsa_sig_size(key->sig_alg), size);
  93. if (!sig) {
  94. free(signature_digest);
  95. return NULL;
  96. }
  97. /* Sign the signature_digest into our output buffer */
  98. int rv = RSA_private_encrypt(signature_digest_len, /* Input length */
  99. signature_digest, /* Input data */
  100. vb2_signature_data(sig), /* Output sig */
  101. key->rsa_private_key, /* Key to use */
  102. RSA_PKCS1_PADDING); /* Padding */
  103. free(signature_digest);
  104. if (-1 == rv) {
  105. fprintf(stderr, "%s: RSA_private_encrypt() failed\n", __func__);
  106. free(sig);
  107. return NULL;
  108. }
  109. /* Return the signature */
  110. return sig;
  111. }