|
|
@ -43,6 +43,9 @@ |
|
|
|
|
|
|
|
|
|
|
|
#include <px4_platform_common/crypto_backend.h> |
|
|
|
#include <px4_platform_common/crypto_backend.h> |
|
|
|
#include <lib/crypto/monocypher/src/optional/monocypher-ed25519.h> |
|
|
|
#include <lib/crypto/monocypher/src/optional/monocypher-ed25519.h> |
|
|
|
|
|
|
|
#include <tomcrypt.h> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
extern void libtomcrypt_init(void); |
|
|
|
|
|
|
|
|
|
|
|
/* room for 16 keys */ |
|
|
|
/* room for 16 keys */ |
|
|
|
#define KEY_CACHE_LEN 16 |
|
|
|
#define KEY_CACHE_LEN 16 |
|
|
@ -67,6 +70,11 @@ typedef struct { |
|
|
|
|
|
|
|
|
|
|
|
static volatile_key_t key_cache[KEY_CACHE_LEN]; |
|
|
|
static volatile_key_t key_cache[KEY_CACHE_LEN]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct { |
|
|
|
|
|
|
|
uint8_t nonce[24]; |
|
|
|
|
|
|
|
uint64_t ctr; |
|
|
|
|
|
|
|
} chacha20_context_t; |
|
|
|
|
|
|
|
|
|
|
|
/* Clear key cache */ |
|
|
|
/* Clear key cache */ |
|
|
|
static void clear_key_cache(void) |
|
|
|
static void clear_key_cache(void) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -126,19 +134,43 @@ void crypto_init() |
|
|
|
{ |
|
|
|
{ |
|
|
|
keystore_init(); |
|
|
|
keystore_init(); |
|
|
|
clear_key_cache(); |
|
|
|
clear_key_cache(); |
|
|
|
|
|
|
|
libtomcrypt_init(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
crypto_session_handle_t crypto_open(px4_crypto_algorithm_t algorithm) |
|
|
|
crypto_session_handle_t crypto_open(px4_crypto_algorithm_t algorithm) |
|
|
|
{ |
|
|
|
{ |
|
|
|
crypto_session_handle_t ret; |
|
|
|
crypto_session_handle_t ret; |
|
|
|
ret.algorithm = algorithm; |
|
|
|
|
|
|
|
ret.keystore_handle = keystore_open(); |
|
|
|
ret.keystore_handle = keystore_open(); |
|
|
|
|
|
|
|
|
|
|
|
if (keystore_session_handle_valid(ret.keystore_handle)) { |
|
|
|
if (keystore_session_handle_valid(ret.keystore_handle)) { |
|
|
|
|
|
|
|
ret.algorithm = algorithm; |
|
|
|
ret.handle = ++crypto_open_count; |
|
|
|
ret.handle = ++crypto_open_count; |
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
ret.handle = 0; |
|
|
|
ret.handle = 0; |
|
|
|
|
|
|
|
ret.context = NULL; |
|
|
|
|
|
|
|
ret.algorithm = CRYPTO_NONE; |
|
|
|
|
|
|
|
return ret; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (algorithm) { |
|
|
|
|
|
|
|
case CRYPTO_XCHACHA20: { |
|
|
|
|
|
|
|
chacha20_context_t *context = malloc(sizeof(chacha20_context_t)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!context) { |
|
|
|
|
|
|
|
ret.handle = 0; |
|
|
|
|
|
|
|
crypto_open_count--; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
ret.context = context; |
|
|
|
|
|
|
|
px4_get_secure_random(context->nonce, sizeof(context->nonce)); |
|
|
|
|
|
|
|
context->ctr = 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
ret.context = NULL; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return ret; |
|
|
|
return ret; |
|
|
@ -146,9 +178,17 @@ crypto_session_handle_t crypto_open(px4_crypto_algorithm_t algorithm) |
|
|
|
|
|
|
|
|
|
|
|
void crypto_close(crypto_session_handle_t *handle) |
|
|
|
void crypto_close(crypto_session_handle_t *handle) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Not open?
|
|
|
|
|
|
|
|
if (!crypto_session_handle_valid(*handle)) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
crypto_open_count--; |
|
|
|
crypto_open_count--; |
|
|
|
handle->handle = 0; |
|
|
|
handle->handle = 0; |
|
|
|
keystore_close(&handle->keystore_handle); |
|
|
|
keystore_close(&handle->keystore_handle); |
|
|
|
|
|
|
|
free(handle->context); |
|
|
|
|
|
|
|
handle->context = NULL; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool crypto_signature_check(crypto_session_handle_t handle, |
|
|
|
bool crypto_signature_check(crypto_session_handle_t handle, |
|
|
@ -159,13 +199,13 @@ bool crypto_signature_check(crypto_session_handle_t handle, |
|
|
|
{ |
|
|
|
{ |
|
|
|
bool ret = false; |
|
|
|
bool ret = false; |
|
|
|
size_t keylen = 0; |
|
|
|
size_t keylen = 0; |
|
|
|
const uint8_t *public_key; |
|
|
|
const uint8_t *public_key = NULL; |
|
|
|
|
|
|
|
|
|
|
|
if (crypto_session_handle_valid(handle)) { |
|
|
|
if (crypto_session_handle_valid(handle)) { |
|
|
|
public_key = crypto_get_key_ptr(handle.keystore_handle, key_index, &keylen); |
|
|
|
public_key = crypto_get_key_ptr(handle.keystore_handle, key_index, &keylen); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (keylen == 0) { |
|
|
|
if (keylen == 0 || public_key == NULL) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -180,3 +220,170 @@ bool crypto_signature_check(crypto_session_handle_t handle, |
|
|
|
|
|
|
|
|
|
|
|
return ret; |
|
|
|
return ret; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool crypto_encrypt_data(crypto_session_handle_t handle, |
|
|
|
|
|
|
|
uint8_t key_idx, |
|
|
|
|
|
|
|
const uint8_t *message, |
|
|
|
|
|
|
|
size_t message_size, |
|
|
|
|
|
|
|
uint8_t *cipher, |
|
|
|
|
|
|
|
size_t *cipher_size) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ret = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!crypto_session_handle_valid(handle)) { |
|
|
|
|
|
|
|
return ret; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (handle.algorithm) { |
|
|
|
|
|
|
|
case CRYPTO_NONE: |
|
|
|
|
|
|
|
if (*cipher_size >= message_size) { |
|
|
|
|
|
|
|
/* In-place there is no copy needed */ |
|
|
|
|
|
|
|
if (message != cipher) { |
|
|
|
|
|
|
|
memcpy(cipher, message, message_size); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*cipher_size = message_size; |
|
|
|
|
|
|
|
ret = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case CRYPTO_XCHACHA20: { |
|
|
|
|
|
|
|
size_t key_sz; |
|
|
|
|
|
|
|
uint8_t *key = (uint8_t *)crypto_get_key_ptr(handle.keystore_handle, key_idx, &key_sz); |
|
|
|
|
|
|
|
chacha20_context_t *context = handle.context; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (key_sz == 32) { |
|
|
|
|
|
|
|
context->ctr = crypto_xchacha20_ctr(cipher, message, *cipher_size, key, context->nonce, context->ctr); |
|
|
|
|
|
|
|
ret = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case CRYPTO_RSA_OAEP: { |
|
|
|
|
|
|
|
rsa_key key; |
|
|
|
|
|
|
|
size_t key_sz; |
|
|
|
|
|
|
|
unsigned long outlen = *cipher_size; |
|
|
|
|
|
|
|
uint8_t *public_key = (uint8_t *)crypto_get_key_ptr(handle.keystore_handle, key_idx, &key_sz); |
|
|
|
|
|
|
|
*cipher_size = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (public_key && |
|
|
|
|
|
|
|
rsa_import(public_key, key_sz, &key) == CRYPT_OK) { |
|
|
|
|
|
|
|
if (outlen >= ltc_mp.unsigned_size(key.N) && |
|
|
|
|
|
|
|
pkcs_1_oaep_encode(message, message_size, |
|
|
|
|
|
|
|
NULL, 0, |
|
|
|
|
|
|
|
ltc_mp.count_bits(key.N), |
|
|
|
|
|
|
|
NULL, |
|
|
|
|
|
|
|
0, 0, |
|
|
|
|
|
|
|
cipher, &outlen) == CRYPT_OK && |
|
|
|
|
|
|
|
ltc_mp.rsa_me(cipher, outlen, cipher, &outlen, PK_PUBLIC, &key) == CRYPT_OK) { |
|
|
|
|
|
|
|
*cipher_size = outlen; |
|
|
|
|
|
|
|
ret = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rsa_free(&key); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ret; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool crypto_get_encrypted_key(crypto_session_handle_t handle, |
|
|
|
|
|
|
|
uint8_t key_idx, |
|
|
|
|
|
|
|
uint8_t *key, |
|
|
|
|
|
|
|
size_t *max_len, |
|
|
|
|
|
|
|
uint8_t encryption_key_idx) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// Retrieve the plaintext key
|
|
|
|
|
|
|
|
bool ret = true; |
|
|
|
|
|
|
|
size_t key_sz; |
|
|
|
|
|
|
|
const uint8_t *plain_key = crypto_get_key_ptr(handle.keystore_handle, key_idx, &key_sz); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (key_sz == 0) { |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Encrypt it
|
|
|
|
|
|
|
|
if (key != NULL) { |
|
|
|
|
|
|
|
ret = crypto_encrypt_data(handle, |
|
|
|
|
|
|
|
encryption_key_idx, |
|
|
|
|
|
|
|
plain_key, |
|
|
|
|
|
|
|
key_sz, |
|
|
|
|
|
|
|
key, |
|
|
|
|
|
|
|
max_len); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// The key size, encrypted, is a multiple of minimum block size for the algorithm+key
|
|
|
|
|
|
|
|
size_t min_block = crypto_get_min_blocksize(handle, encryption_key_idx); |
|
|
|
|
|
|
|
*max_len = key_sz / min_block * min_block; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (key_sz % min_block) { |
|
|
|
|
|
|
|
*max_len += min_block; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ret; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool crypto_get_nonce(crypto_session_handle_t handle, |
|
|
|
|
|
|
|
uint8_t *nonce, |
|
|
|
|
|
|
|
size_t *nonce_len) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
switch (handle.algorithm) { |
|
|
|
|
|
|
|
case CRYPTO_XCHACHA20: { |
|
|
|
|
|
|
|
chacha20_context_t *context = handle.context; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (nonce != NULL && context != NULL) { |
|
|
|
|
|
|
|
memcpy(nonce, context->nonce, sizeof(context->nonce)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*nonce_len = sizeof(context->nonce); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
*nonce_len = 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
size_t crypto_get_min_blocksize(crypto_session_handle_t handle, uint8_t key_idx) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
size_t ret; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (handle.algorithm) { |
|
|
|
|
|
|
|
case CRYPTO_XCHACHA20: |
|
|
|
|
|
|
|
ret = 64; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case CRYPTO_RSA_OAEP: { |
|
|
|
|
|
|
|
rsa_key enc_key; |
|
|
|
|
|
|
|
unsigned pub_key_sz; |
|
|
|
|
|
|
|
uint8_t *pub_key = (uint8_t *)crypto_get_key_ptr(handle.keystore_handle, key_idx, &pub_key_sz); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (pub_key && |
|
|
|
|
|
|
|
rsa_import(pub_key, pub_key_sz, &enc_key) == CRYPT_OK) { |
|
|
|
|
|
|
|
ret = ltc_mp.unsigned_size(enc_key.N); |
|
|
|
|
|
|
|
rsa_free(&enc_key); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
ret = 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
ret = 1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ret; |
|
|
|
|
|
|
|
} |
|
|
|