Skip to content

Commit 9f99eab

Browse files
committed
add and compile with crypto Libraries
1 parent e82ca04 commit 9f99eab

File tree

4 files changed

+194
-2
lines changed

4 files changed

+194
-2
lines changed

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ AddCompilerFlag("-fpic" C_FLAGS Vc_ARCHITECTURE_FLAGS)
159159
AddCompilerFlag("-fno-tree-vectorize" C_FLAGS Vc_ARCHITECTURE_FLAGS)
160160

161161
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${Vc_ARCHITECTURE_FLAGS}")
162-
162+
find_package(CRYPTOPP)
163163
find_package(SDL)
164164
find_package(SDL2)
165165

@@ -288,7 +288,8 @@ set(libfilenames
288288
libavcodec/utils.c
289289
libavcodec/videodsp.c
290290
libavcodec/vorbis_parser.c
291-
libavcodec/xiph.c
291+
libavcodec/xiph.c
292+
libavcodec/crypto.cpp
292293
)
293294
if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7l")
294295
list(APPEND libfilenames

MyCMakeScripts/FindCryptopp.cmake

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# -*- cmake -*-
2+
#
3+
# Find cryptopp library or sources.
4+
#
5+
# Input variables:
6+
# CRYPTOPP_DIR - set this to specify the cryptopp source to be built.
7+
#
8+
# Output variables:
9+
#
10+
# CRYPTOPP_FOUND - set if library was found
11+
# CRYPTOPP_INCLUDE_DIR - Set to where include files ar found (or sources)
12+
# CRYPTOPP_LIBRARIES - Set to library
13+
# CRYPTOPP_IS_LIB - set if library was found
14+
# CRYPTOPP_IS_SOURCE - set if sources were found
15+
16+
FIND_PATH(CRYPTOPP_INCLUDE_DIR
17+
cryptlib.h
18+
PATHS
19+
/usr/include/cryptopp
20+
/usr/include
21+
)
22+
23+
FIND_LIBRARY(CRYPTOPP_LIBRARIES
24+
NAMES cryptopp
25+
)
26+
27+
IF(CRYPTOPP_INCLUDE_DIR AND CRYPTOPP_LIBRARY)
28+
SET(CRYPTOPP_FOUND TRUE)
29+
SET(CRYPTOPP_IS_LIB TRUE)
30+
SET(CRYPTOPP_IS_SOURCE FALSE)
31+
IF(CMAKE_TRACE)
32+
MESSAGE(STATUS "Cryptopp libraries found in: ${CRYPTOPP_INCLUDE_DIR}")
33+
ENDIF(CMAKE_TRACE)
34+
ELSE(CRYPTOPP_INCLUDE_DIR AND CRYPTOPP_LIBRARY)
35+
IF(CRYPTOPP_DIR)
36+
FIND_PATH(CRYPTOPP_INCLUDE_DIR
37+
cryptlib.h
38+
PATHS
39+
${CRYPTOPP_DIR}
40+
)
41+
IF(CRYPTOPP_INCLUDE_DIR)
42+
IF(CMAKE_TRACE)
43+
MESSAGE(STATUS "Cryptopp source found in: ${CRYPTOPP_INCLUDE_DIR}")
44+
ENDIF(CMAKE_TRACE)
45+
SET(CRYPTOPP_FOUND TRUE)
46+
SET(CRYPTOPP_IS_LIB FALSE)
47+
SET(CRYPTOPP_IS_SOURCE TRUE)
48+
ENDIF(CRYPTOPP_INCLUDE_DIR)
49+
ENDIF(CRYPTOPP_DIR)
50+
include_directories(/opt/local/include/)
51+
link_directories(/opt/local/lib/)
52+
ENDIF(CRYPTOPP_INCLUDE_DIR AND CRYPTOPP_LIBRARY)

libavcodec/crypto.cpp

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
2+
#include <libavcodec/crypto.h>
3+
#include <cryptopp/aes.h>
4+
#include <cryptopp/modes.h>
5+
#include <cryptopp/osrng.h>
6+
typedef struct AESDecoder {
7+
#if AESEncryptionStreamMode
8+
CryptoPP::CFB_Mode<CryptoPP::AES>::Encryption *CFBdec;
9+
#else
10+
CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption *CFBdec;
11+
#endif
12+
13+
byte key[CryptoPP::AES::DEFAULT_KEYLENGTH], iv[CryptoPP::AES::BLOCKSIZE], out_stream_counter[CryptoPP::AES::BLOCKSIZE], counter[CryptoPP::AES::BLOCKSIZE];
14+
int couter_avail, counter_index, counter_index_pos;
15+
} AESDecoder;
16+
17+
18+
AESDecoder* Init() {
19+
int init_val[32] = {201, 75, 219, 152, 6, 245, 237, 107, 179, 194, 81, 29, 66, 98, 198, 0, 16, 213, 27, 56, 255, 127, 242, 112, 97, 126, 197, 204, 25, 59, 38, 30};
20+
AESDecoder * AESdecoder = (AESDecoder *)malloc(sizeof(AESDecoder));
21+
for(int i=0;i<16; i++) {
22+
AESdecoder->iv [i] = init_val[i];
23+
AESdecoder->counter[i] = init_val[5+i];
24+
AESdecoder->key[i] = init_val[i+16];
25+
}
26+
#if AESEncryptionStreamMode
27+
AESdecoder->CFBdec = new CryptoPP::CFB_Mode<CryptoPP::AES >::Encryption(AESdecoder->key, CryptoPP::AES::DEFAULT_KEYLENGTH, AESdecoder->iv);
28+
#else
29+
AESdecoder->CFBdec = new CryptoPP::CFB_Mode<CryptoPP::AES >::Decryption(AESdecoder->key, CryptoPP::AES::DEFAULT_KEYLENGTH, AESdecoder->iv);
30+
#endif
31+
AESdecoder->couter_avail = 0;
32+
AESdecoder->counter_index = 0;
33+
AESdecoder->counter_index_pos = 0;
34+
return AESdecoder;
35+
}
36+
37+
void DeleteCrypto(AESDecoder * AESdecoder) {
38+
if(AESdecoder)
39+
free(AESdecoder);
40+
}
41+
42+
void Decrypt(AESDecoder *AESdecoder, const unsigned char *in_stream, int size_bits, unsigned char *out_stream) {
43+
int nb_bytes = ceil((double)size_bits/8);
44+
AESdecoder->CFBdec->ProcessData(out_stream, in_stream, nb_bytes);
45+
if(size_bits&7)
46+
AESdecoder->CFBdec->SetKeyWithIV(AESdecoder->key, CryptoPP::AES::DEFAULT_KEYLENGTH, AESdecoder->iv);
47+
48+
}
49+
void Incr_counter (unsigned char *counter) {
50+
counter[0]++;
51+
}
52+
53+
#if AESEncryptionStreamMode
54+
void Decrypt_counter(AESDecoder * AESdecoder) {
55+
AESdecoder->CFBdec->ProcessData(AESdecoder->out_stream_counter, AESdecoder->counter, 16);
56+
AESdecoder->couter_avail = 128;
57+
AESdecoder->counter_index = 15;
58+
AESdecoder->counter_index_pos = 8;
59+
Incr_counter(AESdecoder->counter);
60+
}
61+
#endif
62+
63+
#if AESEncryptionStreamMode
64+
unsigned int get_key (AESDecoder * AESdecoder, int nb_bits) {
65+
unsigned int key_ = 0;
66+
if(nb_bits > 32) {
67+
printf("The Generator can not generate more than 32 bit %d \n", nb_bits);
68+
return 0;
69+
}
70+
if( !nb_bits )
71+
return 0;
72+
if(!AESdecoder->couter_avail)
73+
Decrypt_counter(AESdecoder);
74+
75+
if(AESdecoder->couter_avail >= nb_bits)
76+
AESdecoder->couter_avail -= nb_bits;
77+
else
78+
AESdecoder->couter_avail = 0;
79+
int nb = 0;
80+
while( nb_bits ) {
81+
if( nb_bits >= AESdecoder->counter_index_pos )
82+
nb = AESdecoder->counter_index_pos;
83+
else
84+
nb = nb_bits;
85+
key_ <<= nb;
86+
key_ += (AESdecoder->out_stream_counter[AESdecoder->counter_index] & ((1<<nb)-1));
87+
AESdecoder->out_stream_counter[AESdecoder->counter_index] >>= nb;
88+
nb_bits -= nb;
89+
90+
if(AESdecoder->counter_index && nb == AESdecoder->counter_index_pos ) {
91+
AESdecoder->counter_index--;
92+
AESdecoder->counter_index_pos = 8;
93+
} else {
94+
AESdecoder->counter_index_pos -= nb;
95+
if(nb_bits) {
96+
Decrypt_counter(AESdecoder);
97+
AESdecoder->couter_avail -= nb_bits;
98+
}
99+
}
100+
}
101+
return key_;
102+
}
103+
#endif
104+
105+
Crypto_Handle InitC(){
106+
AESDecoder* AESdecoder = Init();
107+
return AESdecoder;
108+
}
109+
#if AESEncryptionStreamMode
110+
unsigned int ff_get_key (Crypto_Handle *hdl, int nb_bits) {
111+
return get_key ((AESDecoder*)*hdl, nb_bits);
112+
}
113+
#endif
114+
void DecryptC(Crypto_Handle hdl, const unsigned char *in_stream, int size_bits, unsigned char *out_stream) {
115+
Decrypt((AESDecoder*)hdl, in_stream, size_bits, out_stream);
116+
}
117+
118+
void DeleteCryptoC(Crypto_Handle hdl) {
119+
DeleteCryptoC(hdl);
120+
}

libavcodec/crypto.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
#include <stdio.h>
3+
#include <math.h>
4+
#define AESEncryptionStreamMode 1
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
typedef void* Crypto_Handle;
9+
Crypto_Handle InitC();
10+
void DecryptC(Crypto_Handle hdl, const unsigned char *in_stream, int size_bits, unsigned char *out_stream);
11+
#if AESEncryptionStreamMode
12+
unsigned int ff_get_key (Crypto_Handle *hdl, int nb_bits);
13+
#endif
14+
void DeleteCryptoC(Crypto_Handle hdl);
15+
16+
#ifdef __cplusplus
17+
}
18+
#endif
19+

0 commit comments

Comments
 (0)