2rsa.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #ifndef VBOOT_REFERENCE_2RSA_H_
  6. #define VBOOT_REFERENCE_2RSA_H_
  7. #include "2crypto.h"
  8. #include "2struct.h"
  9. struct vb2_workbuf;
  10. /* Public key structure in RAM */
  11. struct vb2_public_key {
  12. uint32_t arrsize; /* Length of n[] and rr[] in number of uint32_t */
  13. uint32_t n0inv; /* -1 / n[0] mod 2^32 */
  14. const uint32_t *n; /* Modulus as little endian array */
  15. const uint32_t *rr; /* R^2 as little endian array */
  16. enum vb2_signature_algorithm sig_alg; /* Signature algorithm */
  17. enum vb2_hash_algorithm hash_alg; /* Hash algorithm */
  18. const char *desc; /* Description */
  19. uint32_t version; /* Key version */
  20. const struct vb2_id *id; /* Key ID */
  21. };
  22. /**
  23. * Convert vb2_crypto_algorithm to vb2_signature_algorithm.
  24. *
  25. * @param algorithm Crypto algorithm (vb2_crypto_algorithm)
  26. *
  27. * @return The signature algorithm for that crypto algorithm, or
  28. * VB2_SIG_INVALID if the crypto algorithm or its corresponding signature
  29. * algorithm is invalid or not supported.
  30. */
  31. enum vb2_signature_algorithm vb2_crypto_to_signature(uint32_t algorithm);
  32. /**
  33. * Return the size of a RSA signature
  34. *
  35. * @param sig_alg Signature algorithm
  36. * @return The size of the signature in bytes, or 0 if error.
  37. */
  38. uint32_t vb2_rsa_sig_size(enum vb2_signature_algorithm sig_alg);
  39. /**
  40. * Return the size of a pre-processed RSA public key.
  41. *
  42. * @param sig_alg Signature algorithm
  43. * @return The size of the preprocessed key in bytes, or 0 if error.
  44. */
  45. uint32_t vb2_packed_key_size(enum vb2_signature_algorithm sig_alg);
  46. /**
  47. * Check pkcs 1.5 padding bytes
  48. *
  49. * @param sig Signature to verify
  50. * @param key Key to take signature and hash algorithms from
  51. * @return VB2_SUCCESS, or non-zero if error.
  52. */
  53. int vb2_check_padding(const uint8_t *sig, const struct vb2_public_key *key);
  54. /* Size of work buffer sufficient for vb2_rsa_verify_digest() worst case */
  55. #define VB2_VERIFY_RSA_DIGEST_WORKBUF_BYTES (3 * 1024)
  56. /**
  57. * Verify a RSA PKCS1.5 signature against an expected hash digest.
  58. *
  59. * @param key Key to use in signature verification
  60. * @param sig Signature to verify (destroyed in process)
  61. * @param digest Digest of signed data
  62. * @param wb Work buffer
  63. * @return VB2_SUCCESS, or non-zero if error.
  64. */
  65. int vb2_rsa_verify_digest(const struct vb2_public_key *key,
  66. uint8_t *sig,
  67. const uint8_t *digest,
  68. const struct vb2_workbuf *wb);
  69. #endif /* VBOOT_REFERENCE_2RSA_H_ */