并发执行加密函数encode会导致失败
实际测试发现并发执行encode后,程序异常,解密失败,定位后发现是并发调用encode,encode能支持并发吗?
奇怪,应该不会这样子啊。你确定吗?
是我代码里有static的字段吗, 我太久没看这个仓库了。如果有你试着改改。最近几天我上不了网。
同步调用encode就没有问题,所以应该是并发引起的
确实有static字段: static state_t* state; // The array that stores the round keys. static uint8_t RoundKey[176]; // The Key input to the AES Program static const uint8_t* Key; #if defined(CBC) && CBC // Initial Vector used only for CBC mode static uint8_t* Iv; #endif
解决了,将 static state_t* state; static uint8_t RoundKey[176]; static const uint8_t* Key; 定义为局部变量,能够解决并发问题
Okay, Can u give me a pull request? Thanx a lot.
If u have more suggestions for this repository, I can add you to an organization and we will maintain it together.
好的,正在整理
帮我设置为开发者吧,一直提示我没有权限push
AESJniEncrypt forest$ git push origin develop ERROR: Permission to BruceWind/AESJniEncrypt.git denied to forest606. fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
From 889c28113ffdc90e29541a711750308c5b2af635 Mon Sep 17 00:00:00 2001 From: "forest.liu" [email protected] Date: Mon, 3 Jun 2019 19:54:53 +0800 Subject: [PATCH] Support concurrently call encrypt and decrypt
aesjni/src/main/cpp/aes.c | 74 +++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 37 deletions(-)
diff --git a/aesjni/src/main/cpp/aes.c b/aesjni/src/main/cpp/aes.c index 9f8a572..185c7eb 100644 --- a/aesjni/src/main/cpp/aes.c +++ b/aesjni/src/main/cpp/aes.c @@ -62,13 +62,13 @@ NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0) /*****************************************************************************/ // state - array holding the intermediate results during decryption. typedef uint8_t state_t[4][4]; -static state_t* state; +//static state_t* state;
// The array that stores the round keys. -static uint8_t RoundKey[176]; +//static uint8_t RoundKey[176];
// The Key input to the AES Program -static const uint8_t* Key; +//static const uint8_t* Key; #if defined(CBC) && CBC // Initial Vector used only for CBC mode static uint8_t* Iv; @@ -151,7 +151,7 @@ static uint8_t getSBoxInvert(uint8_t num) }
// This function produces Nb(Nr+1) round keys. The round keys are used in each round to decrypt the states. -static void KeyExpansion(void) +static void KeyExpansion(uint8_t *RoundKey, const uint8_t *Key) { uint32_t i, j, k; uint8_t tempa[4]; // Used for the column/row operations @@ -218,7 +218,7 @@ static void KeyExpansion(void)
// This function adds the round key to state. // The round key is added to the state by an XOR function. -static void AddRoundKey(uint8_t round) +static void AddRoundKey(state_t* state, uint8_t *RoundKey, uint8_t round) { uint8_t i,j; for(i=0;i<4;++i) @@ -232,7 +232,7 @@ static void AddRoundKey(uint8_t round)
// The SubBytes Function Substitutes the values in the // state matrix with values in an S-box. -static void SubBytes(void) +static void SubBytes(state_t* state) { uint8_t i, j; for(i = 0; i < 4; ++i) @@ -247,7 +247,7 @@ static void SubBytes(void) // The ShiftRows() function shifts the rows in the state to the left. // Each row is shifted with different offset. // Offset = Row number. So the first row is not shifted. -static void ShiftRows(void) +static void ShiftRows(state_t* state) { uint8_t temp;
@@ -281,7 +281,7 @@ static uint8_t xtime(uint8_t x) }
// MixColumns function mixes the columns of the state matrix -static void MixColumns(void) +static void MixColumns(state_t* state) { uint8_t i; uint8_t Tmp,Tm,t; @@ -319,7 +319,7 @@ static uint8_t Multiply(uint8_t x, uint8_t y) // MixColumns function mixes the columns of the state matrix. // The method used to multiply may be difficult to understand for the inexperienced. // Please use the references to gain more information. -static void InvMixColumns(void) +static void InvMixColumns(state_t* state) { int i; uint8_t a,b,c,d; @@ -340,7 +340,7 @@ static void InvMixColumns(void)
// The SubBytes Function Substitutes the values in the // state matrix with values in an S-box. -static void InvSubBytes(void) +static void InvSubBytes(state_t* state) { uint8_t i,j; for(i=0;i<4;++i) @@ -352,7 +352,7 @@ static void InvSubBytes(void) } }
-static void InvShiftRows(void) +static void InvShiftRows(state_t* state) { uint8_t temp;
@@ -382,54 +382,54 @@ static void InvShiftRows(void)
// Cipher is the main function that encrypts the PlainText. -static void Cipher(void) +static void Cipher(state_t* state, uint8_t *RoundKey) { uint8_t round = 0;
// Add the First round key to the state before starting the rounds.
- AddRoundKey(0);
-
AddRoundKey(state, RoundKey, 0);
// There will be Nr rounds. // The first Nr-1 rounds are identical. // These Nr-1 rounds are executed in the loop below. for(round = 1; round < Nr; ++round) {
- SubBytes();
- ShiftRows();
- MixColumns();
- AddRoundKey(round);
- SubBytes(state);
- ShiftRows(state);
- MixColumns(state);
- AddRoundKey(state, RoundKey, round); }
// The last round is given below. // The MixColumns function is not here in the last round.
- SubBytes();
- ShiftRows();
- AddRoundKey(Nr);
- SubBytes(state);
- ShiftRows(state);
- AddRoundKey(state, RoundKey, Nr); }
-static void InvCipher(void) +static void InvCipher(state_t* state, uint8_t *RoundKey) { uint8_t round=0;
// Add the First round key to the state before starting the rounds.
- AddRoundKey(Nr);
-
AddRoundKey(state, RoundKey, Nr);
// There will be Nr rounds. // The first Nr-1 rounds are identical. // These Nr-1 rounds are executed in the loop below. for(round=Nr-1;round>0;round--) {
- InvShiftRows();
- InvSubBytes();
- AddRoundKey(round);
- InvMixColumns();
- InvShiftRows(state);
- InvSubBytes(state);
- AddRoundKey(state, RoundKey, round);
- InvMixColumns(state); }
// The last round is given below. // The MixColumns function is not here in the last round.
- InvShiftRows();
- InvSubBytes();
- AddRoundKey(0);
- InvShiftRows(state);
- InvSubBytes(state);
- AddRoundKey(state, RoundKey, 0); }
static void BlockCopy(uint8_t* output, uint8_t* input) @@ -453,26 +453,26 @@ void AES128_ECB_encrypt(uint8_t* input, const uint8_t* key, uint8_t* output) { // Copy input to output, and work in-memory on output BlockCopy(output, input);
- state = (state_t*)output;
- state_t* state = (state_t*)output;
- Key = key;
- KeyExpansion();
-
uint8_t RoundKey[176] = {0};
-
KeyExpansion(RoundKey, key);
// The next function call encrypts the PlainText with the Key using AES algorithm.
- Cipher();
- Cipher(state, RoundKey); }
void AES128_ECB_decrypt(uint8_t* input, const uint8_t* key, uint8_t *output) { // Copy input to output, and work in-memory on output BlockCopy(output, input);
- state = (state_t*)output;
-
state_t* state = (state_t*)output;
// The KeyExpansion routine must be called before encryption.
- Key = key;
- KeyExpansion();
- uint8_t RoundKey[176] = {0};
- KeyExpansion(RoundKey, key);
- InvCipher();
- InvCipher(state, RoundKey); }
/**
2.13.2
https://github.com/BruceWind/AESJniEncrypt/issues/23#issuecomment-498228334
sorry,the reply is late. u can do this: 1.fork the repo to your github. 2.modify code. 3.push code to your repo. 4.create a pull request to my repo.
Sorry ever one, AES is no longer supported. I have pushed code that use chacah20 instead of AES. https://github.com/BruceWind/AESJniEncrypt/pull/40