CryptoAPI.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (C) 2012 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <media/stagefright/MediaErrors.h>
  17. #include <utils/Errors.h>
  18. #include <utils/Vector.h>
  19. #ifndef CRYPTO_API_H_
  20. #define CRYPTO_API_H_
  21. namespace android {
  22. struct AString;
  23. struct CryptoPlugin;
  24. struct CryptoFactory {
  25. CryptoFactory() {}
  26. virtual ~CryptoFactory() {}
  27. virtual bool isCryptoSchemeSupported(const uint8_t uuid[16]) const = 0;
  28. virtual status_t createPlugin(
  29. const uint8_t uuid[16], const void *data, size_t size,
  30. CryptoPlugin **plugin) = 0;
  31. private:
  32. CryptoFactory(const CryptoFactory &);
  33. CryptoFactory &operator=(const CryptoFactory &);
  34. };
  35. struct CryptoPlugin {
  36. enum Mode {
  37. kMode_Unencrypted = 0,
  38. kMode_AES_CTR = 1,
  39. // Neither key nor iv are being used in this mode.
  40. // Each subsample is encrypted w/ an iv of all zeroes.
  41. kMode_AES_WV = 2, // FIX constant
  42. };
  43. struct SubSample {
  44. uint32_t mNumBytesOfClearData;
  45. uint32_t mNumBytesOfEncryptedData;
  46. };
  47. CryptoPlugin() {}
  48. virtual ~CryptoPlugin() {}
  49. // If this method returns false, a non-secure decoder will be used to
  50. // decode the data after decryption. The decrypt API below will have
  51. // to support insecure decryption of the data (secure = false) for
  52. // media data of the given mime type.
  53. virtual bool requiresSecureDecoderComponent(const char *mime) const = 0;
  54. // To implement resolution constraints, the crypto plugin needs to know
  55. // the resolution of the video being decrypted. The media player should
  56. // call this method when the resolution is determined and any time it
  57. // is subsequently changed.
  58. virtual void notifyResolution(uint32_t /* width */, uint32_t /* height */) {}
  59. // A MediaDrm session may be associated with a MediaCrypto session. The
  60. // associated MediaDrm session is used to load decryption keys
  61. // into the crypto/drm plugin. The keys are then referenced by key-id
  62. // in the 'key' parameter to the decrypt() method.
  63. // Should return NO_ERROR on success, ERROR_DRM_SESSION_NOT_OPENED if
  64. // the session is not opened and a code from MediaErrors.h otherwise.
  65. virtual status_t setMediaDrmSession(const Vector<uint8_t> & /*sessionId */) {
  66. return ERROR_UNSUPPORTED;
  67. }
  68. // If the error returned falls into the range
  69. // ERROR_DRM_VENDOR_MIN..ERROR_DRM_VENDOR_MAX, errorDetailMsg should be
  70. // filled in with an appropriate string.
  71. // At the java level these special errors will then trigger a
  72. // MediaCodec.CryptoException that gives clients access to both
  73. // the error code and the errorDetailMsg.
  74. // Returns a non-negative result to indicate the number of bytes written
  75. // to the dstPtr, or a negative result to indicate an error.
  76. virtual ssize_t decrypt(
  77. bool secure,
  78. const uint8_t key[16],
  79. const uint8_t iv[16],
  80. Mode mode,
  81. const void *srcPtr,
  82. const SubSample *subSamples, size_t numSubSamples,
  83. void *dstPtr,
  84. AString *errorDetailMsg) = 0;
  85. private:
  86. CryptoPlugin(const CryptoPlugin &);
  87. CryptoPlugin &operator=(const CryptoPlugin &);
  88. };
  89. } // namespace android
  90. extern "C" {
  91. extern android::CryptoFactory *createCryptoFactory();
  92. }
  93. #endif // CRYPTO_API_H_