util.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2014 The Chromium 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. #ifndef COMPONENTS_WEBCRYPTO_ALGORITHMS_UTIL_H_
  5. #define COMPONENTS_WEBCRYPTO_ALGORITHMS_UTIL_H_
  6. #include <string>
  7. #include <vector>
  8. #include <stddef.h>
  9. #include <stdint.h>
  10. #include "base/containers/span.h"
  11. #include "third_party/blink/public/platform/web_crypto_algorithm.h"
  12. #include "third_party/blink/public/platform/web_crypto_key.h"
  13. #include "third_party/boringssl/src/include/openssl/base.h"
  14. // This file contains miscellaneous helpers that don't belong in any of the
  15. // other *_util.h
  16. namespace webcrypto {
  17. class Status;
  18. // Returns the EVP_MD that corresponds with |hash_algorithm|, or nullptr on
  19. // failure.
  20. const EVP_MD* GetDigest(const blink::WebCryptoAlgorithm& hash_algorithm);
  21. // Returns the EVP_MD that corresponds with |id|, or nullptr on failure.
  22. const EVP_MD* GetDigest(blink::WebCryptoAlgorithmId id);
  23. // Truncates an octet string to a particular bit length. This is accomplished by
  24. // resizing to the closest byte length, and then zero-ing the unused
  25. // least-significant bits of the final byte.
  26. //
  27. // It is an error to call this function with a bit length that is larger than
  28. // that of |bytes|.
  29. //
  30. // TODO(eroman): This operation is not yet defined by the WebCrypto spec,
  31. // however this is a reasonable interpretation:
  32. // https://www.w3.org/Bugs/Public/show_bug.cgi?id=27402
  33. void TruncateToBitLength(size_t length_bits, std::vector<uint8_t>* bytes);
  34. // Rounds a bit count (up) to the nearest byte count.
  35. //
  36. // This is mathematically equivalent to (x + 7) / 8, however has no
  37. // possibility of integer overflow.
  38. template <typename T>
  39. T NumBitsToBytes(T x) {
  40. return (x / 8) + (7 + (x % 8)) / 8;
  41. }
  42. // Verifies whether a key can be created using |actual_usages| when the
  43. // algorithm supports |all_possible_usages|.
  44. Status CheckKeyCreationUsages(blink::WebCryptoKeyUsageMask all_possible_usages,
  45. blink::WebCryptoKeyUsageMask actual_usages);
  46. // TODO(eroman): This doesn't really belong in this file. Move it into Blink
  47. // instead.
  48. //
  49. // Returns true if the set bits in b make up a subset of the set bits in a.
  50. bool ContainsKeyUsages(blink::WebCryptoKeyUsageMask a,
  51. blink::WebCryptoKeyUsageMask b);
  52. // The values of these constants correspond with the "enc" parameter of
  53. // EVP_CipherInit_ex(), do not change.
  54. enum EncryptOrDecrypt { DECRYPT = 0, ENCRYPT = 1 };
  55. // Does either encryption or decryption for an AEAD algorithm.
  56. // * |mode| controls whether encryption or decryption is done
  57. // * |aead_alg| the algorithm (for instance AES-GCM)
  58. // * |buffer| where the ciphertext or plaintext is written to.
  59. Status AeadEncryptDecrypt(EncryptOrDecrypt mode,
  60. base::span<const uint8_t> raw_key,
  61. base::span<const uint8_t> data,
  62. unsigned int tag_length_bytes,
  63. base::span<const uint8_t> iv,
  64. base::span<const uint8_t> additional_data,
  65. const EVP_AEAD* aead_alg,
  66. std::vector<uint8_t>* buffer);
  67. } // namespace webcrypto
  68. #endif // COMPONENTS_WEBCRYPTO_ALGORITHMS_UTIL_H_