Skip to content

Commit 0de162b

Browse files
committed
fixed AES. Added extract
1 parent 2e4574a commit 0de162b

File tree

9 files changed

+358
-33
lines changed

9 files changed

+358
-33
lines changed

cli/CMakeLists.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ MESSAGE(STATUS "Creating CLI Build Files")
66
MESSAGE(STATUS "-------------------------------------------------------------------------------")
77

88
#--set output targets and paths-----------------------------------------------
9-
SET(OPENDCP_TARGETS opendcp_xml opendcp_j2k opendcp_mxf opendcp_largefile)
9+
SET(OPENDCP_TARGETS opendcp_xml opendcp_j2k opendcp_mxf opendcp_extract opendcp_largefile)
1010
IF(ENABLE_XMLSEC)
1111
SET(OPENDCP_TARGETS ${OPENDCP_TARGETS} opendcp_xml_verify)
1212
ENDIF(ENABLE_XMLSEC)
@@ -41,6 +41,9 @@ TARGET_LINK_LIBRARIES(opendcp_j2k ${OPENDCP_LIB})
4141
ADD_EXECUTABLE(opendcp_mxf opendcp_mxf_cmd.c opendcp_cli.c)
4242
TARGET_LINK_LIBRARIES(opendcp_mxf ${OPENDCP_LIB} ${LIBS})
4343

44+
ADD_EXECUTABLE(opendcp_extract opendcp_extract_cmd.c opendcp_cli.c)
45+
TARGET_LINK_LIBRARIES(opendcp_extract ${OPENDCP_LIB} ${LIBS})
46+
4447
IF(ENABLE_XMLSEC)
4548
ADD_EXECUTABLE(opendcp_xml_verify opendcp_xml_verify_cmd.c)
4649
TARGET_LINK_LIBRARIES(opendcp_xml_verify ${OPENDCP_LIB} ${LIBS})

cli/opendcp_extract_cmd.c

+228
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
/*
2+
OpenDCP: Builds Digital Cinema Packages
3+
Copyright (c) 2010-2013 Terrence Meiczinger, All Rights Reserved
4+
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include <string.h>
20+
#include <getopt.h>
21+
#include <stdlib.h>
22+
#include <stdio.h>
23+
#include <time.h>
24+
#include <sys/stat.h>
25+
#include <opendcp.h>
26+
#include "opendcp_cli.h"
27+
28+
void progress_bar();
29+
30+
void version() {
31+
FILE *fp;
32+
33+
fp = stdout;
34+
fprintf(fp, "\n%s version %s %s\n\n", OPENDCP_NAME, OPENDCP_VERSION, OPENDCP_COPYRIGHT);
35+
36+
exit(0);
37+
}
38+
39+
void dcp_usage() {
40+
FILE *fp;
41+
fp = stdout;
42+
43+
fprintf(fp, "\n%s version %s %s\n\n", OPENDCP_NAME, OPENDCP_VERSION, OPENDCP_COPYRIGHT);
44+
fprintf(fp, "Usage:\n");
45+
fprintf(fp, " opendcp_extract -i <file> [options ...]\n\n");
46+
fprintf(fp, "Required:\n");
47+
fprintf(fp, " -i | --input <file> - input mxf file\n");
48+
fprintf(fp, "\n");
49+
fprintf(fp, "Options:\n");
50+
fprintf(fp, " -s | --start <frame> - start frame\n");
51+
fprintf(fp, " -d | --end <frame> - end frame\n");
52+
fprintf(fp, " -l | --log_level <level> - Sets the log level 0:Quiet, 1:Error, 2:Warn (default), 3:Info, 4:Debug\n");
53+
fprintf(fp, " -k | --key <key> - set encryption key (this enables encryption)\n");
54+
fprintf(fp, " -h | --help - show help\n");
55+
fprintf(fp, " -v | --version - show version\n");
56+
fprintf(fp, "\n\n");
57+
58+
fclose(fp);
59+
exit(0);
60+
}
61+
62+
int total = 0;
63+
int val = 0;
64+
65+
int frame_done_cb(void *p) {
66+
val++;
67+
UNUSED(p);
68+
progress_bar();
69+
70+
return 0;
71+
}
72+
73+
int write_done_cb(void *p) {
74+
UNUSED(p);
75+
printf("\n MXF Complete\n");
76+
77+
return 0;
78+
}
79+
80+
void progress_bar() {
81+
int x;
82+
int step = 20;
83+
float c = (float)step / total * (float)val;
84+
85+
printf(" MXF Creation [");
86+
87+
for (x = 0; x < step; x++) {
88+
if (c > x) {
89+
printf("=");
90+
}
91+
else {
92+
printf(" ");
93+
}
94+
}
95+
96+
printf("] 100%% [%d/%d]\r", val, total);
97+
fflush(stdout);
98+
}
99+
100+
int main (int argc, char **argv) {
101+
int c;
102+
opendcp_t *opendcp;
103+
char *filename = NULL;
104+
int key_id_flag = 0;
105+
106+
if (argc <= 1) {
107+
dcp_usage();
108+
}
109+
110+
opendcp = opendcp_create();
111+
112+
/* set initial values */
113+
opendcp->log_level = LOG_WARN;
114+
opendcp->ns = XML_NS_SMPTE;
115+
116+
/* parse options */
117+
while (1)
118+
{
119+
static struct option long_options[] =
120+
{
121+
{"key", required_argument, 0, 'k'},
122+
{"help", required_argument, 0, 'h'},
123+
{"input", required_argument, 0, 'i'},
124+
{"start", required_argument, 0, 's'},
125+
{"log_level", required_argument, 0, 'l'},
126+
{"version", no_argument, 0, 'v'},
127+
{0, 0, 0, 0}
128+
};
129+
130+
/* getopt_long stores the option index here. */
131+
int option_index = 0;
132+
133+
c = getopt_long (argc, argv, "i:k:s:l:hv",
134+
long_options, &option_index);
135+
136+
/* Detect the end of the options. */
137+
if (c == -1)
138+
{ break; }
139+
140+
switch (c)
141+
{
142+
case 0:
143+
144+
/* If this option set a flag, do nothing else now. */
145+
if (long_options[option_index].flag != 0)
146+
{ break; }
147+
148+
printf ("option %s", long_options[option_index].name);
149+
150+
if (optarg)
151+
{ printf (" with arg %s", optarg); }
152+
153+
printf ("\n");
154+
break;
155+
156+
case 's':
157+
opendcp->mxf.start_frame = atoi(optarg);
158+
159+
if (opendcp->mxf.start_frame < 1) {
160+
dcp_fatal(opendcp, "Start frame must be greater than 0");
161+
}
162+
163+
break;
164+
165+
case 'i':
166+
filename = optarg;
167+
break;
168+
169+
case 'l':
170+
opendcp->log_level = atoi(optarg);
171+
break;
172+
173+
case 'h':
174+
dcp_usage();
175+
break;
176+
177+
case 'k':
178+
if (!is_key(optarg)) {
179+
dcp_fatal(opendcp, "Invalid encryption key format");
180+
}
181+
182+
if (hex2bin(optarg, opendcp->mxf.key_value, 16)) {
183+
dcp_fatal(opendcp, "Invalid encryption key format");
184+
}
185+
186+
opendcp->mxf.key_flag = 1;
187+
188+
break;
189+
190+
case 'v':
191+
version();
192+
break;
193+
}
194+
}
195+
196+
opendcp_log_init(opendcp->log_level);
197+
198+
if (opendcp->log_level > 0) {
199+
printf("\nOpenDCP Extract %s %s\n", OPENDCP_VERSION, OPENDCP_COPYRIGHT);
200+
}
201+
202+
/* set the callbacks (optional) for the mxf writer */
203+
if (opendcp->log_level > 0 && opendcp->log_level < 3) {
204+
opendcp->mxf.frame_done.callback = frame_done_cb;
205+
opendcp->mxf.file_done.callback = write_done_cb;
206+
}
207+
208+
int class = get_file_essence_class(filename, 1);
209+
210+
if (opendcp->log_level > 0 && opendcp->log_level < 3) { progress_bar(); }
211+
212+
total = opendcp->mxf.duration;
213+
214+
if (read_j2k_mxf(opendcp, filename) != 0 ) {
215+
OPENDCP_LOG(LOG_INFO, "Could not READ MXF file");
216+
}
217+
else {
218+
OPENDCP_LOG(LOG_INFO, "MXF extract complete");
219+
}
220+
221+
if (opendcp->log_level > 0) {
222+
printf("\n");
223+
}
224+
225+
opendcp_delete(opendcp);
226+
227+
exit(0);
228+
}

libasdcp/AS_DCP_AES.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ ASDCP::AESDecContext::InitKey(const byte_t* key)
176176
KM_TEST_NULL_L(key);
177177

178178
if ( m_Context )
179-
return RESULT_INIT;
179+
return RESULT_INIT;
180180

181181
m_Context = new h__AESContext;
182182
m_Context->m_KeyBuf.Set(key);

libcrypto/CMakeLists.txt

+9-1
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,21 @@ SET(CRYPTO_SRC_FILES
2929
)
3030
#-------------------------------------------------------------------------------
3131

32+
#--set header files-------------------------------------------------------------
33+
SET(CRYPTO_HDR_FILES
34+
md5.h
35+
sha1.h
36+
aes.h
37+
)
38+
39+
3240
#--compile libraries------------------------------------------------------------
3341
SET(LIB_CRYPTO opendcp-crypto PARENT_SCOPE)
3442
ADD_LIBRARY(opendcp-crypto OBJECT ${CRYPTO_SRC_FILES})
3543
#-------------------------------------------------------------------------------
3644

3745
#--install header---------------------------------------------------------------
3846
IF(INSTALL_LIB)
39-
INSTALL(FILES md5.h sha1.h aes.h DESTINATION include)
47+
INSTALL(FILES ${CRYPTO_HDR_FILES} DESTINATION include)
4048
ENDIF()
4149
#-------------------------------------------------------------------------------

libcrypto/aes.c

+16-10
Original file line numberDiff line numberDiff line change
@@ -1068,28 +1068,35 @@ void aes_decrypt(const byte_t in[], byte_t out[], const word_t key[], int keysiz
10681068
out[15] = state[3][3];
10691069
}
10701070

1071-
int AES_set_encrypt_key (const unsigned char *user_key, const int bits, AES_KEY *key) {
1072-
aes_key_setup(user_key, key->rd_key, bits);
1071+
int AES_set_encrypt_key (const unsigned char *user_key, const int bits, aes_key_t *key) {
1072+
key->size = bits;
1073+
aes_key_setup(user_key, key->schedule, key->size);
1074+
10731075
return 0;
10741076
}
10751077

1076-
int AES_set_decrypt_key (const unsigned char *user_key, const int bits, AES_KEY *key) {
1077-
aes_key_setup(user_key, key->rd_key, bits);
1078+
int AES_set_decrypt_key (const unsigned char *user_key, const int bits, aes_key_t *key) {
1079+
key->size = bits;
1080+
aes_key_setup(user_key, key->schedule, key->size);
1081+
10781082
return 0;
10791083
}
10801084

1081-
void AES_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key) {
1082-
aes_encrypt(in, out, key->rd_key, key->rounds);
1085+
void AES_encrypt(const unsigned char *in, unsigned char *out, const aes_key_t *key) {
1086+
aes_encrypt(in, out, key->schedule, key->size);
1087+
}
1088+
1089+
void AES_decrypt(const unsigned char *in, unsigned char *out, const aes_key_t *key) {
1090+
aes_decrypt(in, out, key->schedule, key->size);
10831091
}
10841092

1085-
void AES_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key) {
1086-
aes_encrypt(in, out, key->rd_key, key->rounds);
1093+
void AES_decrypt_cbc(const unsigned char *in, int in_len, unsigned char *out, const aes_key_t *key, byte_t *iv) {
1094+
aes_encrypt_cbc(in, in_len, out, key->schedule, key->size, iv);
10871095
}
10881096

10891097
/*******************
10901098
** AES DEBUGGING FUNCTIONS
10911099
*******************/
1092-
/*
10931100
// This prints the "state" grid as a linear hex string.
10941101
void print_state(byte_t state[][4])
10951102
{
@@ -1110,4 +1117,3 @@ void print_rnd_key(word_t key[])
11101117
printf("%08x",key[idx]);
11111118
printf("\n");
11121119
}
1113-
*/

libcrypto/aes.h

+11-10
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,17 @@ int aes_decrypt_ccm(const byte_t ciphertext[], // IN - Ciphertext,
118118
/*********************** OPENSSL COMPATIBLE DECLARATIONS **********************/
119119
#define AES_MAXNR 14
120120

121-
struct aes_key_st {
122-
unsigned int rd_key[4 * (AES_MAXNR + 1)];
123-
int rounds;
124-
};
125-
typedef struct aes_key_st AES_KEY;
126-
127-
int AES_set_encrypt_key (const unsigned char *userKey, const int bits, AES_KEY *key);
128-
int AES_set_decrypt_key (const unsigned char *userKey, const int bits, AES_KEY *key);
129-
void AES_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key);
130-
void AES_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key);
121+
typedef struct aes_key_st {
122+
word_t schedule[4 * (AES_MAXNR + 1)];
123+
int size;
124+
} aes_key_t;
125+
typedef aes_key_t AES_KEY;
126+
127+
int AES_set_encrypt_key (const unsigned char *user_key, const int bits, aes_key_t *key);
128+
int AES_set_decrypt_key (const unsigned char *user_key, const int bits, aes_key_t *key);
129+
void AES_encrypt(const unsigned char *in, unsigned char *out, const aes_key_t *key);
130+
void AES_decrypt(const unsigned char *in, unsigned char *out, const aes_key_t *key);
131+
void AES_decrypt_cbc(const unsigned char *in, int in_len, unsigned char *out, const aes_key_t *key, byte_t *iv);
131132

132133
///////////////////
133134
// Test functions

0 commit comments

Comments
 (0)