• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef AVUTIL_ENCRYPTION_INFO_H
20 #define AVUTIL_ENCRYPTION_INFO_H
21 
22 #include <stddef.h>
23 #include <stdint.h>
24 
25 #ifdef OHOS_DRM
26 #define AV_DRM_KEY_ID_SIZE                    16
27 #define AV_DRM_IV_SIZE                        16
28 #define AV_DRM_MAX_SUB_SAMPLE_NUM             64
29 #define AV_DRM_MAX_DRM_UUID_LEN               16
30 #define AV_DRM_MAX_DRM_PSSH_LEN               2048
31 #define AV_DRM_UUID_OFFSET                    (12)
32 
33 typedef enum {
34     AV_DRM_ALG_CENC_UNENCRYPTED = 0x0,
35     AV_DRM_ALG_CENC_AES_CTR = 0x1,
36     AV_DRM_ALG_CENC_AES_WV = 0x2,
37     AV_DRM_ALG_CENC_AES_CBC = 0x3,
38     AV_DRM_ALG_CENC_SM4_CBC = 0x4,
39     AV_DRM_ALG_CENC_SM4_CTR,
40 } AV_DrmCencAlgorithm;
41 
42 typedef enum {
43     /* key/iv/subsample set. */
44     AV_DRM_CENC_INFO_KEY_IV_SUBSAMPLES_SET = 0x0,
45     /* key/iv/subsample not set. */
46     AV_DRM_CENC_INFO_KEY_IV_SUBSAMPLES_NOT_SET = 0x1,
47 } AV_DrmCencInfoMode;
48 
49 struct _AV_DrmSubSample {
50     uint32_t clear_header_len;
51     uint32_t pay_load_len;
52 };
53 typedef struct _AV_DrmSubSample AV_DrmSubSample;
54 
55 struct _AV_DrmCencInfo {
56     AV_DrmCencAlgorithm algo;
57     uint8_t key_id[AV_DRM_KEY_ID_SIZE];
58     uint32_t key_id_len;
59     uint8_t iv[AV_DRM_IV_SIZE];
60     uint32_t iv_len;
61     uint32_t encrypt_blocks;
62     uint32_t skip_blocks;
63     uint32_t first_encrypt_offset;
64     AV_DrmSubSample sub_samples[AV_DRM_MAX_SUB_SAMPLE_NUM];
65     uint32_t sub_sample_num;
66     AV_DrmCencInfoMode mode;
67 };
68 typedef struct _AV_DrmCencInfo AV_DrmCencInfo;
69 
70 struct _AV_DrmInfo {
71     uint32_t uuid_len;
72     uint8_t uuid[AV_DRM_MAX_DRM_UUID_LEN];
73     uint32_t pssh_len;
74     uint8_t pssh[AV_DRM_MAX_DRM_PSSH_LEN];
75 };
76 typedef struct _AV_DrmInfo AV_DrmInfo;
77 #endif
78 
79 typedef struct AVSubsampleEncryptionInfo {
80     /** The number of bytes that are clear. */
81     unsigned int bytes_of_clear_data;
82 
83     /**
84      * The number of bytes that are protected.  If using pattern encryption,
85      * the pattern applies to only the protected bytes; if not using pattern
86      * encryption, all these bytes are encrypted.
87      */
88     unsigned int bytes_of_protected_data;
89 } AVSubsampleEncryptionInfo;
90 
91 /**
92  * This describes encryption info for a packet.  This contains frame-specific
93  * info for how to decrypt the packet before passing it to the decoder.
94  *
95  * The size of this struct is not part of the public ABI.
96  */
97 typedef struct AVEncryptionInfo {
98     /** The fourcc encryption scheme, in big-endian byte order. */
99     uint32_t scheme;
100 
101     /**
102      * Only used for pattern encryption.  This is the number of 16-byte blocks
103      * that are encrypted.
104      */
105     uint32_t crypt_byte_block;
106 
107     /**
108      * Only used for pattern encryption.  This is the number of 16-byte blocks
109      * that are clear.
110      */
111     uint32_t skip_byte_block;
112 
113     /**
114      * The ID of the key used to encrypt the packet.  This should always be
115      * 16 bytes long, but may be changed in the future.
116      */
117     uint8_t *key_id;
118     uint32_t key_id_size;
119 
120     /**
121      * The initialization vector.  This may have been zero-filled to be the
122      * correct block size.  This should always be 16 bytes long, but may be
123      * changed in the future.
124      */
125     uint8_t *iv;
126     uint32_t iv_size;
127 
128     /**
129      * An array of subsample encryption info specifying how parts of the sample
130      * are encrypted.  If there are no subsamples, then the whole sample is
131      * encrypted.
132      */
133     AVSubsampleEncryptionInfo *subsamples;
134     uint32_t subsample_count;
135 } AVEncryptionInfo;
136 
137 /**
138  * This describes info used to initialize an encryption key system.
139  *
140  * The size of this struct is not part of the public ABI.
141  */
142 typedef struct AVEncryptionInitInfo {
143     /**
144      * A unique identifier for the key system this is for, can be NULL if it
145      * is not known.  This should always be 16 bytes, but may change in the
146      * future.
147      */
148     uint8_t* system_id;
149     uint32_t system_id_size;
150 
151     /**
152      * An array of key IDs this initialization data is for.  All IDs are the
153      * same length.  Can be NULL if there are no known key IDs.
154      */
155     uint8_t** key_ids;
156     /** The number of key IDs. */
157     uint32_t num_key_ids;
158     /**
159      * The number of bytes in each key ID.  This should always be 16, but may
160      * change in the future.
161      */
162     uint32_t key_id_size;
163 
164     /**
165      * Key-system specific initialization data.  This data is copied directly
166      * from the file and the format depends on the specific key system.  This
167      * can be NULL if there is no initialization data; in that case, there
168      * will be at least one key ID.
169      */
170     uint8_t* data;
171     uint32_t data_size;
172 
173     /**
174      * An optional pointer to the next initialization info in the list.
175      */
176     struct AVEncryptionInitInfo *next;
177 } AVEncryptionInitInfo;
178 
179 /**
180  * Allocates an AVEncryptionInfo structure and sub-pointers to hold the given
181  * number of subsamples.  This will allocate pointers for the key ID, IV,
182  * and subsample entries, set the size members, and zero-initialize the rest.
183  *
184  * @param subsample_count The number of subsamples.
185  * @param key_id_size The number of bytes in the key ID, should be 16.
186  * @param iv_size The number of bytes in the IV, should be 16.
187  *
188  * @return The new AVEncryptionInfo structure, or NULL on error.
189  */
190 AVEncryptionInfo *av_encryption_info_alloc(uint32_t subsample_count, uint32_t key_id_size, uint32_t iv_size);
191 
192 /**
193  * Allocates an AVEncryptionInfo structure with a copy of the given data.
194  * @return The new AVEncryptionInfo structure, or NULL on error.
195  */
196 AVEncryptionInfo *av_encryption_info_clone(const AVEncryptionInfo *info);
197 
198 /**
199  * Frees the given encryption info object.  This MUST NOT be used to free the
200  * side-data data pointer, that should use normal side-data methods.
201  */
202 void av_encryption_info_free(AVEncryptionInfo *info);
203 
204 /**
205  * Creates a copy of the AVEncryptionInfo that is contained in the given side
206  * data.  The resulting object should be passed to av_encryption_info_free()
207  * when done.
208  *
209  * @return The new AVEncryptionInfo structure, or NULL on error.
210  */
211 AVEncryptionInfo *av_encryption_info_get_side_data(const uint8_t *side_data, size_t side_data_size);
212 
213 /**
214  * Allocates and initializes side data that holds a copy of the given encryption
215  * info.  The resulting pointer should be either freed using av_free or given
216  * to av_packet_add_side_data().
217  *
218  * @return The new side-data pointer, or NULL.
219  */
220 uint8_t *av_encryption_info_add_side_data(
221       const AVEncryptionInfo *info, size_t *side_data_size);
222 
223 #ifdef OHOS_DRM
224 AV_DrmCencInfo *av_encryption_info_add_side_data_ex(
225     const AVEncryptionInfo *info, size_t *side_data_size, AV_DrmCencInfo *cenc_info);
226 #endif
227 
228 /**
229  * Allocates an AVEncryptionInitInfo structure and sub-pointers to hold the
230  * given sizes.  This will allocate pointers and set all the fields.
231  *
232  * @return The new AVEncryptionInitInfo structure, or NULL on error.
233  */
234 AVEncryptionInitInfo *av_encryption_init_info_alloc(
235     uint32_t system_id_size, uint32_t num_key_ids, uint32_t key_id_size, uint32_t data_size);
236 
237 /**
238  * Frees the given encryption init info object.  This MUST NOT be used to free
239  * the side-data data pointer, that should use normal side-data methods.
240  */
241 void av_encryption_init_info_free(AVEncryptionInitInfo* info);
242 
243 /**
244  * Creates a copy of the AVEncryptionInitInfo that is contained in the given
245  * side data.  The resulting object should be passed to
246  * av_encryption_init_info_free() when done.
247  *
248  * @return The new AVEncryptionInitInfo structure, or NULL on error.
249  */
250 AVEncryptionInitInfo *av_encryption_init_info_get_side_data(
251     const uint8_t* side_data, size_t side_data_size);
252 
253 /**
254  * Allocates and initializes side data that holds a copy of the given encryption
255  * init info.  The resulting pointer should be either freed using av_free or
256  * given to av_packet_add_side_data().
257  *
258  * @return The new side-data pointer, or NULL.
259  */
260 uint8_t *av_encryption_init_info_add_side_data(
261     const AVEncryptionInitInfo *info, size_t *side_data_size);
262 
263 #endif /* AVUTIL_ENCRYPTION_INFO_H */
264