Skip to content

Commit 3c68c3f

Browse files
authored
Merge pull request #604 from thingsconnected/pullreq3
Reduced code size of PayloadEncrypt
2 parents 638ecc5 + da6ae37 commit 3c68c3f

File tree

1 file changed

+3
-16
lines changed

1 file changed

+3
-16
lines changed

src/mac/LoRaMacCrypto.c

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ static KeyAddr_t KeyAddrList[NUM_OF_SEC_CTX] =
243243
* \param[IN/OUT] buffer - Data buffer
244244
* \retval - Status of the operation
245245
*/
246-
static LoRaMacCryptoStatus_t PayloadEncrypt( uint8_t* buffer, uint16_t size, KeyIdentifier_t keyID, uint32_t address, uint8_t dir, uint32_t frameCounter )
246+
static LoRaMacCryptoStatus_t PayloadEncrypt( uint8_t* buffer, int16_t size, KeyIdentifier_t keyID, uint32_t address, uint8_t dir, uint32_t frameCounter )
247247
{
248248
if( buffer == 0 )
249249
{
@@ -269,7 +269,7 @@ static LoRaMacCryptoStatus_t PayloadEncrypt( uint8_t* buffer, uint16_t size, Key
269269
aBlock[12] = ( frameCounter >> 16 ) & 0xFF;
270270
aBlock[13] = ( frameCounter >> 24 ) & 0xFF;
271271

272-
while( size >= 16 )
272+
while( size > 0 )
273273
{
274274
aBlock[15] = ctr & 0xFF;
275275
ctr++;
@@ -278,27 +278,14 @@ static LoRaMacCryptoStatus_t PayloadEncrypt( uint8_t* buffer, uint16_t size, Key
278278
return LORAMAC_CRYPTO_ERROR_SECURE_ELEMENT_FUNC;
279279
}
280280

281-
for( uint8_t i = 0; i < 16; i++ )
281+
for( uint8_t i = 0; i < ( ( size > 16 ) ? 16 : size ); i++ )
282282
{
283283
buffer[bufferIndex + i] = buffer[bufferIndex + i] ^ sBlock[i];
284284
}
285285
size -= 16;
286286
bufferIndex += 16;
287287
}
288288

289-
if( size > 0 )
290-
{
291-
aBlock[15] = ctr & 0xFF;
292-
if( SecureElementAesEncrypt( aBlock, 16, keyID, sBlock ) != SECURE_ELEMENT_SUCCESS )
293-
{
294-
return LORAMAC_CRYPTO_ERROR_SECURE_ELEMENT_FUNC;
295-
}
296-
for( uint8_t i = 0; i < size; i++ )
297-
{
298-
buffer[bufferIndex + i] = buffer[bufferIndex + i] ^ sBlock[i];
299-
}
300-
}
301-
302289
return LORAMAC_CRYPTO_SUCCESS;
303290
}
304291

0 commit comments

Comments
 (0)