security_adapter.c 1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "security_adapter.h"
#include "aes.h"

void security_adapter_encrypt(
	const uint8_t *secure_key,
	uint8_t *encrypted_payload, 
	uint8_t *encrypted_payload_length,
	uint8_t *decrypted_payload,
	uint8_t decrypted_payload_length) 
{
	uint16_t i;
	struct AES_ctx ctx;
	AES_init_ctx(&ctx, secure_key);
	
	for (i = 0; i < decrypted_payload_length; i+= SECURITY_KEY_SIZE) {
		AES_ECB_encrypt(&ctx, &decrypted_payload[i]);
	}

	*encrypted_payload_length = i;
	memcpy(encrypted_payload, decrypted_payload, *encrypted_payload_length);
}



void security_adapter_decrypt(
	const uint8_t *secure_key,
	uint8_t *encrypted_payload, 
	uint8_t encrypted_payload_length,
	uint8_t *decrypted_payload,
	uint8_t *decrypted_payload_length)
{
	uint16_t i;
	struct AES_ctx ctx;
	AES_init_ctx(&ctx, secure_key);
	
	for (i = 0; i < encrypted_payload_length; i+= SECURITY_KEY_SIZE) {
		AES_ECB_decrypt(&ctx, &encrypted_payload[i]);
	}

	*decrypted_payload_length = i;
	memcpy(decrypted_payload, encrypted_payload, *decrypted_payload_length);
}