-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(decrypto): add decrypto interface
impl avAESDecrypt and OpenSSAESDecrypt Signed-off-by: pingkai <pingkai010@gmail.com>
- Loading branch information
Showing
6 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// Created by moqi on 2020/2/20. | ||
// | ||
|
||
#ifndef CICADAMEDIA_IAESDECRYPT_H | ||
#define CICADAMEDIA_IAESDECRYPT_H | ||
|
||
#include <cstdint> | ||
|
||
|
||
namespace Cicada { | ||
class IAESDecrypt { | ||
public: | ||
const static int BLOCK_SIZE = 16; | ||
|
||
IAESDecrypt() = default; | ||
|
||
virtual ~IAESDecrypt() = default; | ||
|
||
virtual int setKey(const uint8_t *key, int key_bits) = 0; | ||
|
||
virtual void decrypt(uint8_t *dst, const uint8_t *src, int count, uint8_t *iv) = 0; | ||
|
||
}; | ||
} | ||
|
||
|
||
#endif //CICADAMEDIA_IAESDECRYPT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// Created by moqi on 2020/2/20. | ||
// | ||
|
||
#include <openssl/aes.h> | ||
#include "OpenSSAESDecrypt.h" | ||
|
||
using namespace Cicada; | ||
|
||
OpenSSAESDecrypt::OpenSSAESDecrypt() = default; | ||
|
||
OpenSSAESDecrypt::~OpenSSAESDecrypt() = default; | ||
|
||
int OpenSSAESDecrypt::setKey(const uint8_t *key, int key_bits) | ||
{ | ||
return AES_set_decrypt_key(key, 8 * AES_BLOCK_SIZE/*128*/, &mAesKey); | ||
} | ||
|
||
void OpenSSAESDecrypt::decrypt(uint8_t *dst, const uint8_t *src, int count, uint8_t *iv) | ||
{ | ||
AES_cbc_encrypt(src, dst, count * AES_BLOCK_SIZE, &mAesKey, iv, AES_DECRYPT); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// Created by moqi on 2020/2/20. | ||
// | ||
|
||
#ifndef CICADAMEDIA_OPENSSAESDECRYPT_H | ||
#define CICADAMEDIA_OPENSSAESDECRYPT_H | ||
|
||
#include "IAESDecrypt.h" | ||
|
||
namespace Cicada { | ||
class OpenSSAESDecrypt : public IAESDecrypt { | ||
public: | ||
OpenSSAESDecrypt(); | ||
|
||
~OpenSSAESDecrypt() override; | ||
|
||
int setKey(const uint8_t *key, int key_bits) override; | ||
|
||
void decrypt(uint8_t *dst, const uint8_t *src, int count, uint8_t *iv) override; | ||
|
||
private: | ||
AES_KEY mAesKey{}; | ||
|
||
}; | ||
} | ||
|
||
|
||
#endif //CICADAMEDIA_OPENSSAESDECRYPT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// Created by moqi on 2020/2/20. | ||
// | ||
extern "C" { | ||
#include <libavutil/mem.h> | ||
#include <libavutil/aes.h> | ||
} | ||
|
||
#include "avAESDecrypt.h" | ||
|
||
using namespace Cicada; | ||
|
||
avAESDecrypt::avAESDecrypt() | ||
{ | ||
mAes = av_aes_alloc(); | ||
} | ||
|
||
avAESDecrypt::~avAESDecrypt() | ||
{ | ||
av_free(mAes); | ||
} | ||
|
||
void avAESDecrypt::decrypt(uint8_t *dst, const uint8_t *src, int count, uint8_t *iv) | ||
{ | ||
av_aes_crypt(mAes, dst, src, count, iv, 1); | ||
} | ||
|
||
int avAESDecrypt::setKey(const uint8_t *key, int key_bits) | ||
{ | ||
return av_aes_init(mAes, key, key_bits, 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// Created by moqi on 2020/2/20. | ||
// | ||
|
||
#ifndef CICADAMEDIA_AVAESDECRYPT_H | ||
#define CICADAMEDIA_AVAESDECRYPT_H | ||
|
||
#include "IAESDecrypt.h" | ||
|
||
namespace Cicada { | ||
class avAESDecrypt : public IAESDecrypt { | ||
public: | ||
avAESDecrypt(); | ||
|
||
~avAESDecrypt() override; | ||
|
||
int setKey(const uint8_t *key, int key_bits) override; | ||
|
||
void decrypt(uint8_t *dst, const uint8_t *src, int count, uint8_t *iv) override; | ||
|
||
private: | ||
struct AVAES *mAes{nullptr}; | ||
|
||
}; | ||
} | ||
|
||
|
||
#endif //CICADAMEDIA_AVAESDECRYPT_H |