FFmpeg
Loading...
Searching...
No Matches
encryption_info.c
Go to the documentation of this file.
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
20
21#include <stdio.h>
22#include <string.h>
23
24#include "libavutil/avassert.h"
25#include "libavutil/mem.h"
26
27static const AVSubsampleEncryptionInfo test_subsamples[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
28static const size_t test_subsample_count = sizeof(test_subsamples) / sizeof(test_subsamples[0]);
29static const uint8_t test_iv[] = {0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18};
30static const uint8_t test_key_id[] = {0x21, 0x22, 0x23, 0x24};
31static const uint8_t test_key_id_2[] = {0x31, 0x32, 0x33, 0x34};
32static const uint8_t test_system_id[] = {0x41, 0x42, 0x43};
33static const uint8_t test_data[] = {0x51, 0x52};
34
36 if (!a || !b || a->scheme != b->scheme || a->crypt_byte_block != b->crypt_byte_block ||
37 a->skip_byte_block != b->skip_byte_block || a->key_id_size != b->key_id_size ||
38 a->iv_size != b->iv_size || a->subsample_count != b->subsample_count)
39 return 1;
40
41 if (memcmp(a->key_id, b->key_id, a->key_id_size) != 0 ||
42 memcmp(a->iv, b->iv, a->iv_size) != 0 ||
43 memcmp(a->subsamples, b->subsamples, a->subsample_count * sizeof(a->subsamples[0])))
44 return 1;
45
46 return 0;
47}
48
50 if (!a || !b)
51 return 1;
52
53 while (a && b) {
54 if (a->system_id_size != b->system_id_size ||
55 a->num_key_ids != b->num_key_ids || a->key_id_size != b->key_id_size ||
56 a->data_size != b->data_size)
57 return 1;
58
59 if (memcmp(a->system_id, b->system_id, a->system_id_size) != 0 ||
60 memcmp(a->data, b->data, a->data_size) != 0)
61 return 1;
62
63 for (uint32_t i = 0; i < a->num_key_ids; i++) {
64 if (memcmp(a->key_ids[i], b->key_ids[i], a->key_id_size) != 0)
65 return 1;
66 }
67
68 a = a->next;
69 b = b->next;
70 }
71
72 return a != b;
73}
74
75static void run_encryption_info_test(void)
76{
77 AVEncryptionInfo *info, *copy;
78 uint8_t *side_data;
79 size_t side_data_size;
80
82 av_assert0(info);
83 av_assert0(info->key_id);
84 av_assert0(info->key_id_size == sizeof(test_key_id));
85 av_assert0(info->iv);
86 av_assert0(info->iv_size == sizeof(test_iv));
89
90 info->scheme = 1234;
91 info->crypt_byte_block = 333;
92 info->skip_byte_block = 444;
93 memcpy(info->key_id, test_key_id, sizeof(test_key_id));
94 memcpy(info->iv, test_iv, sizeof(test_iv));
95 memcpy(info->subsamples, test_subsamples, sizeof(test_subsamples));
96
99 av_assert0(copy != info);
102
103 side_data = av_encryption_info_add_side_data(info, &side_data_size);
104 av_assert0(side_data);
105 av_assert0(side_data_size > 0);
106
107 copy = av_encryption_info_get_side_data(side_data, side_data_size);
109 av_assert0(copy != info);
112 av_free(side_data);
113
115}
116
118{
120
121 info = av_encryption_init_info_alloc(sizeof(test_system_id), 2, sizeof(test_key_id), sizeof(test_data));
122 av_assert0(info);
123 av_assert0(info->system_id);
124 av_assert0(info->system_id_size == sizeof(test_system_id));
125 av_assert0(info->key_ids);
126 av_assert0(info->num_key_ids == 2);
127 av_assert0(info->key_id_size == sizeof(test_key_id));
128 av_assert0(info->key_ids[0]);
129 av_assert0(info->key_ids[1]);
130 av_assert0(info->data);
131 av_assert0(info->data_size == sizeof(test_data));
132 av_assert0(!info->next);
133
134 memcpy(info->system_id, test_system_id, sizeof(test_system_id));
135 memcpy(info->key_ids[0], test_key_id, sizeof(test_key_id));
136 memcpy(info->key_ids[1], test_key_id_2, sizeof(test_key_id_2));
137 memcpy(info->data, test_data, sizeof(test_data));
138
139 return info;
140}
141
143{
145 uint8_t *side_data, *appended;
146 size_t side_data_size, appended_size;
147 unsigned int allocated_size = 0;
148
149 info = create_init_info();
150
151 side_data = av_encryption_init_info_add_side_data(info, &side_data_size);
152 av_assert0(side_data);
153 av_assert0(side_data_size > 0);
154 copy = av_encryption_init_info_get_side_data(side_data, side_data_size);
158 av_free(side_data);
159
160 // Make the first init info different from the second to test the correct order.
161 memset(info->system_id, 0, info->system_id_size);
162 appended = av_encryption_init_info_add_side_data(info, &appended_size);
163 av_assert0(appended);
164 info->next = create_init_info();
165 side_data = av_encryption_init_info_add_side_data(info, &side_data_size);
166 av_assert0(side_data);
167 copy = av_encryption_init_info_get_side_data(side_data, side_data_size);
171
172 av_assert0(av_encryption_init_info_append_side_data(&appended, &appended_size, &allocated_size, info->next) == 0);
173 av_assert0(appended_size == side_data_size);
174 av_assert0(memcmp(appended, side_data, side_data_size) == 0);
175 av_free(appended);
176 av_free(side_data);
177
179}
180
181int main(int argc, char **argv)
182{
185 return 0;
186}
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
int main
Definition dovi_rpuenc.c:38
AVEncryptionInfo * av_encryption_info_alloc(uint32_t subsample_count, uint32_t key_id_size, uint32_t iv_size)
Allocates an AVEncryptionInfo structure and sub-pointers to hold the given number of subsamples.
AVEncryptionInfo * av_encryption_info_get_side_data(const uint8_t *buffer, size_t size)
Creates a copy of the AVEncryptionInfo that is contained in the given side data.
AVEncryptionInfo * av_encryption_info_clone(const AVEncryptionInfo *info)
Allocates an AVEncryptionInfo structure with a copy of the given data.
AVEncryptionInitInfo * av_encryption_init_info_get_side_data(const uint8_t *side_data, size_t side_data_size)
Creates a copy of the AVEncryptionInitInfo that is contained in the given side data.
int av_encryption_init_info_append_side_data(uint8_t **side_data, size_t *side_data_size, unsigned int *allocated_size, const AVEncryptionInitInfo *info)
Appends a copy of the given encryption init info list to encryption init info side data,...
uint8_t * av_encryption_init_info_add_side_data(const AVEncryptionInitInfo *info, size_t *side_data_size)
Allocates and initializes side data that holds a copy of the given encryption init info.
uint8_t * av_encryption_info_add_side_data(const AVEncryptionInfo *info, size_t *size)
Allocates and initializes side data that holds a copy of the given encryption info.
AVEncryptionInitInfo * av_encryption_init_info_alloc(uint32_t system_id_size, uint32_t num_key_ids, uint32_t key_id_size, uint32_t data_size)
Allocates an AVEncryptionInitInfo structure and sub-pointers to hold the given sizes.
void av_encryption_init_info_free(AVEncryptionInitInfo *info)
Frees the given encryption init info object.
void av_encryption_info_free(AVEncryptionInfo *info)
Frees the given encryption info object.
int a
#define b
Definition input.c:43
Memory handling functions.
This describes encryption info for a packet.
uint32_t skip_byte_block
Only used for pattern encryption.
uint8_t * iv
The initialization vector.
uint32_t crypt_byte_block
Only used for pattern encryption.
uint8_t * key_id
The ID of the key used to encrypt the packet.
AVSubsampleEncryptionInfo * subsamples
An array of subsample encryption info specifying how parts of the sample are encrypted.
uint32_t scheme
The fourcc encryption scheme, in big-endian byte order.
This describes info used to initialize an encryption key system.
uint8_t * data
Key-system specific initialization data.
struct AVEncryptionInitInfo * next
An optional pointer to the next initialization info in the list.
uint8_t ** key_ids
An array of key IDs this initialization data is for.
uint8_t * system_id
A unique identifier for the key system this is for, can be NULL if it is not known.
uint32_t key_id_size
The number of bytes in each key ID.
uint32_t num_key_ids
The number of key IDs.
This file is part of FFmpeg.
#define av_free(p)
static const uint8_t test_data[]
static const AVSubsampleEncryptionInfo test_subsamples[]
static int compare_encryption_info(const AVEncryptionInfo *a, const AVEncryptionInfo *b)
static AVEncryptionInitInfo * create_init_info(void)
static const size_t test_subsample_count
static const uint8_t test_key_id_2[]
static void run_encryption_init_info_test(void)
static const uint8_t test_iv[]
static const uint8_t test_key_id[]
static int compare_encryption_init_info(const AVEncryptionInitInfo *a, const AVEncryptionInitInfo *b)
static void run_encryption_info_test(void)
static const uint8_t test_system_id[]
static void copy(const float *p1, float *p2, const int length)