Skip to content

Commit

Permalink
Added all missing ops listed in Service API that Mbed Crypto support …
Browse files Browse the repository at this point in the history
…(plus some it doesn't)

Signed-off-by: Samuel Bailey <samuel.bailey@arm.com>
  • Loading branch information
sbailey-arm committed Aug 3, 2020
1 parent fed003f commit f4af888
Show file tree
Hide file tree
Showing 17 changed files with 1,145 additions and 78 deletions.
138 changes: 132 additions & 6 deletions psa-crypto-sys/src/c/shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ shim_key_attributes_init(void)
return psa_key_attributes_init();
}

psa_key_derivation_operation_t
shim_key_derivation_operation_init(void)
{
return psa_key_derivation_operation_init();
}

void
shim_set_key_algorithm(psa_key_attributes_t *attributes,
psa_algorithm_t alg)
Expand Down Expand Up @@ -102,6 +108,16 @@ shim_PSA_ALG_IS_MAC(psa_algorithm_t alg) {
return PSA_ALG_IS_MAC(alg);
}

int
shim_PSA_ALG_IS_HMAC(psa_algorithm_t alg) {
return PSA_ALG_IS_HMAC(alg);
}

int
shim_PSA_ALG_IS_BLOCK_CIPHER_MAC (psa_algorithm_t alg) {
return PSA_ALG_IS_BLOCK_CIPHER_MAC (alg);
}

int
shim_PSA_ALG_IS_CIPHER(psa_algorithm_t alg) {
return PSA_ALG_IS_CIPHER(alg);
Expand Down Expand Up @@ -157,14 +173,49 @@ shim_PSA_ALG_IS_DETERMINISTIC_ECDSA(psa_algorithm_t alg) {
return PSA_ALG_IS_DETERMINISTIC_ECDSA(alg);
}

int
shim_PSA_ALG_IS_HKDF(psa_algorithm_t alg) {
return PSA_ALG_IS_HKDF(alg);
}

int
shim_PSA_ALG_IS_TLS12_PRF(psa_algorithm_t alg) {
return PSA_ALG_IS_TLS12_PRF(alg);
}

int
shim_PSA_ALG_IS_TLS12_PSK_TO_MS(psa_algorithm_t alg) {
return PSA_ALG_IS_TLS12_PSK_TO_MS(alg);
}

psa_algorithm_t
shim_PSA_ALG_SIGN_GET_HASH(psa_algorithm_t alg) {
return PSA_ALG_SIGN_GET_HASH(alg);
shim_PSA_ALG_SIGN_GET_HASH(psa_algorithm_t sign_alg) {
return PSA_ALG_SIGN_GET_HASH(sign_alg);
}

psa_algorithm_t
shim_PSA_ALG_RSA_OAEP_GET_HASH(psa_algorithm_t alg) {
return PSA_ALG_RSA_OAEP_GET_HASH(alg);
shim_PSA_ALG_RSA_OAEP_GET_HASH(psa_algorithm_t rsa_oaep_alg) {
return PSA_ALG_RSA_OAEP_GET_HASH(rsa_oaep_alg);
}

psa_algorithm_t
shim_PSA_ALG_HMAC_GET_HASH(psa_algorithm_t hmac_alg) {
return PSA_ALG_HMAC_GET_HASH(hmac_alg);
}

psa_algorithm_t
shim_PSA_ALG_HKDF_GET_HASH(psa_algorithm_t hkdf_alg) {
return PSA_ALG_HKDF_GET_HASH(hkdf_alg);
}

psa_algorithm_t
shim_PSA_ALG_TLS12_PRF_GET_HASH(psa_algorithm_t tls12_prf_alg) {
return PSA_ALG_TLS12_PRF_GET_HASH(tls12_prf_alg);
}

psa_algorithm_t
shim_PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(psa_algorithm_t tls12_psk_to_ms_alg) {
return PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(tls12_psk_to_ms_alg);
}

psa_algorithm_t
Expand All @@ -187,6 +238,51 @@ shim_PSA_ALG_DETERMINISTIC_ECDSA(psa_algorithm_t hash_alg) {
return PSA_ALG_DETERMINISTIC_ECDSA(hash_alg);
}

psa_algorithm_t
shim_PSA_ALG_HMAC(psa_algorithm_t hash_alg) {
return PSA_ALG_HMAC(hash_alg);
}

psa_algorithm_t
shim_PSA_ALG_TRUNCATED_MAC(psa_algorithm_t mac_alg, size_t mac_length) {
return PSA_ALG_TRUNCATED_MAC(mac_alg, mac_length);
}

psa_algorithm_t
shim_PSA_ALG_FULL_LENGTH_MAC(psa_algorithm_t mac_alg) {
return PSA_ALG_FULL_LENGTH_MAC(mac_alg);
}

psa_algorithm_t
shim_PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(psa_algorithm_t aead_alg) {
return PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(aead_alg);
}

psa_algorithm_t
shim_PSA_ALG_AEAD_WITH_SHORTENED_TAG(psa_algorithm_t aead_alg, size_t tag_length) {
return PSA_ALG_AEAD_WITH_TAG_LENGTH(aead_alg, tag_length);
}

psa_algorithm_t
shim_PSA_ALG_HKDF(psa_algorithm_t hash_alg) {
return PSA_ALG_HKDF(hash_alg);
}

psa_algorithm_t
shim_PSA_ALG_TLS12_PRF(psa_algorithm_t hash_alg) {
return PSA_ALG_TLS12_PRF(hash_alg);
}

psa_algorithm_t
shim_PSA_ALG_TLS12_PSK_TO_MS(psa_algorithm_t hash_alg) {
return PSA_ALG_TLS12_PSK_TO_MS(hash_alg);
}

psa_algorithm_t
shim_PSA_ALG_KEY_AGREEMENT(psa_algorithm_t raw_key_agreement, psa_algorithm_t key_derivation) {
return PSA_ALG_KEY_AGREEMENT(raw_key_agreement, key_derivation);
}

int
shim_PSA_KEY_TYPE_IS_ECC_KEY_PAIR(psa_key_type_t key_type)
{
Expand All @@ -212,9 +308,9 @@ shim_PSA_KEY_TYPE_IS_DH_KEY_PAIR(psa_key_type_t key_type)
}

psa_algorithm_t
shim_PSA_ALG_RSA_OAEP(psa_algorithm_t alg_type)
shim_PSA_ALG_RSA_OAEP(psa_algorithm_t hash_alg)
{
return PSA_ALG_RSA_OAEP(alg_type);
return PSA_ALG_RSA_OAEP(hash_alg);
}

psa_ecc_curve_t
Expand Down Expand Up @@ -281,4 +377,34 @@ size_t
shim_PSA_KEY_EXPORT_MAX_SIZE(psa_key_type_t key_type, size_t key_bits)
{
return PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits);
}

size_t
shim_PSA_HASH_LENGTH(psa_algorithm_t alg)
{
return PSA_HASH_SIZE(alg);
}

size_t
shim_PSA_MAC_LENGTH(psa_key_type_t key_type, size_t key_bits, psa_algorithm_t alg)
{
return PSA_MAC_FINAL_SIZE(key_type, key_bits, alg);
}

size_t
shim_PSA_MAC_TRUNCATED_LENGTH(psa_algorithm_t alg)
{
return PSA_MAC_TRUNCATED_LENGTH(alg);
}

size_t
shim_PSA_AEAD_ENCRYPT_OUTPUT_SIZE(psa_algorithm_t aead_alg, size_t plaintext_bytes)
{
return PSA_AEAD_ENCRYPT_OUTPUT_SIZE(aead_alg, plaintext_bytes);
}

size_t
shim_PSA_AEAD_DECRYPT_OUTPUT_SIZE(psa_algorithm_t aead_alg, size_t ciphertext_bytes)
{
return PSA_AEAD_DECRYPT_OUTPUT_SIZE(aead_alg, ciphertext_bytes);
}
33 changes: 29 additions & 4 deletions psa-crypto-sys/src/c/shim.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ psa_key_lifetime_t shim_get_key_lifetime(const psa_key_attributes_t *attributes)
psa_key_type_t shim_get_key_type(const psa_key_attributes_t *attributes);
psa_key_usage_t shim_get_key_usage_flags(const psa_key_attributes_t *attributes);
psa_key_attributes_t shim_key_attributes_init(void);
psa_key_derivation_operation_t shim_key_derivation_operation_init(void);

void shim_set_key_algorithm(psa_key_attributes_t *attributes, psa_algorithm_t alg);
void shim_set_key_bits(psa_key_attributes_t *attributes, size_t bits);
Expand All @@ -90,6 +91,9 @@ void shim_set_key_usage_flags(psa_key_attributes_t *attributes, psa_key_usage_t

int shim_PSA_ALG_IS_HASH(psa_algorithm_t alg);
int shim_PSA_ALG_IS_MAC(psa_algorithm_t alg);
int shim_PSA_ALG_IS_HMAC(psa_algorithm_t alg);
int shim_PSA_ALG_IS_BLOCK_CIPHER_MAC(psa_algorithm_t alg);
int shim_PSA_ALG_IS_FULL_LENGTH_MAC(psa_algorithm_t alg);
int shim_PSA_ALG_IS_CIPHER(psa_algorithm_t alg);
int shim_PSA_ALG_IS_AEAD(psa_algorithm_t alg);
int shim_PSA_ALG_IS_SIGN(psa_algorithm_t alg);
Expand All @@ -101,13 +105,29 @@ int shim_PSA_ALG_IS_RSA_PKCS1V15_SIGN(psa_algorithm_t alg);
int shim_PSA_ALG_IS_RSA_PSS(psa_algorithm_t alg);
int shim_PSA_ALG_IS_ECDSA(psa_algorithm_t alg);
int shim_PSA_ALG_IS_DETERMINISTIC_ECDSA(psa_algorithm_t alg);
psa_algorithm_t shim_PSA_ALG_RSA_OAEP(psa_algorithm_t alg);
int shim_PSA_ALG_IS_HKDF(psa_algorithm_t alg);
int shim_PSA_ALG_IS_TLS12_PRF(psa_algorithm_t alg);
int shim_PSA_ALG_IS_TLS12_PSK_TO_MS(psa_algorithm_t alg);
psa_algorithm_t shim_PSA_ALG_RSA_OAEP(psa_algorithm_t hash_alg);
psa_algorithm_t shim_PSA_ALG_RSA_PKCS1V15_SIGN(psa_algorithm_t hash_alg);
psa_algorithm_t shim_PSA_ALG_RSA_PSS(psa_algorithm_t hash_alg);
psa_algorithm_t shim_PSA_ALG_ECDSA(psa_algorithm_t hash_alg);
psa_algorithm_t shim_PSA_ALG_DETERMINISTIC_ECDSA(psa_algorithm_t hash_alg);
psa_algorithm_t shim_PSA_ALG_SIGN_GET_HASH(psa_algorithm_t alg);
psa_algorithm_t shim_PSA_ALG_RSA_OAEP_GET_HASH(psa_algorithm_t alg);
psa_algorithm_t shim_PSA_ALG_HMAC(psa_algorithm_t hash_alg);
psa_algorithm_t shim_PSA_ALG_SIGN_GET_HASH(psa_algorithm_t sign_alg);
psa_algorithm_t shim_PSA_ALG_RSA_OAEP_GET_HASH(psa_algorithm_t rsa_oaep_alg);
psa_algorithm_t shim_PSA_ALG_HMAC_GET_HASH(psa_algorithm_t hmac_alg);
psa_algorithm_t shim_PSA_ALG_HKDF_GET_HASH(psa_algorithm_t hkdf_alg);
psa_algorithm_t shim_PSA_ALG_TLS12_PRF_GET_HASH(psa_algorithm_t tls12_prf_alg);
psa_algorithm_t shim_PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(psa_algorithm_t tls12_psk_to_ms_alg);
psa_algorithm_t shim_PSA_ALG_TRUNCATED_MAC(psa_algorithm_t mac_alg, size_t mac_length);
psa_algorithm_t shim_PSA_ALG_FULL_LENGTH_MAC(psa_algorithm_t mac_alg);
psa_algorithm_t shim_PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(psa_algorithm_t aead_alg);
psa_algorithm_t shim_PSA_ALG_AEAD_WITH_SHORTENED_TAG(psa_algorithm_t aead_alg, size_t tag_length);
psa_algorithm_t shim_PSA_ALG_HKDF(psa_algorithm_t hash_alg);
psa_algorithm_t shim_PSA_ALG_TLS12_PRF(psa_algorithm_t hash_alg);
psa_algorithm_t shim_PSA_ALG_TLS12_PSK_TO_MS(psa_algorithm_t hash_alg);
psa_algorithm_t shim_PSA_ALG_KEY_AGREEMENT(psa_algorithm_t raw_key_agreement, psa_algorithm_t key_derivation);
int shim_PSA_KEY_TYPE_IS_ECC_KEY_PAIR(psa_key_type_t key_type);
int shim_PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(psa_key_type_t key_type);
int shim_PSA_KEY_TYPE_IS_DH_PUBLIC_KEY(psa_key_type_t key_type);
Expand All @@ -122,4 +142,9 @@ psa_key_type_t shim_PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(psa_key_type_t key_type)
size_t shim_PSA_SIGN_OUTPUT_SIZE(psa_key_type_t key_type, size_t key_bits, psa_algorithm_t alg);
size_t shim_PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(psa_key_type_t key_type, size_t key_bits, psa_algorithm_t alg);
size_t shim_PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(psa_key_type_t key_type, size_t key_bits, psa_algorithm_t alg);
size_t shim_PSA_KEY_EXPORT_MAX_SIZE(psa_key_type_t key_type, size_t key_bits);
size_t shim_PSA_KEY_EXPORT_MAX_SIZE(psa_key_type_t key_type, size_t key_bits);
size_t shim_PSA_HASH_LENGTH(psa_algorithm_t alg);
size_t shim_PSA_MAC_LENGTH(psa_key_type_t key_type, size_t key_bits, psa_algorithm_t alg);
size_t shim_PSA_MAC_TRUNCATED_LENGTH(psa_algorithm_t alg);
size_t shim_PSA_AEAD_ENCRYPT_OUTPUT_SIZE(psa_algorithm_t aead_alg, size_t plaintext_bytes);
size_t shim_PSA_AEAD_DECRYPT_OUTPUT_SIZE(psa_algorithm_t aead_alg, size_t ciphertext_bytes);
9 changes: 9 additions & 0 deletions psa-crypto-sys/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ pub const PSA_ALG_CBC_NO_PADDING: psa_algorithm_t = 73_400_576;
pub const PSA_ALG_CBC_PKCS7: psa_algorithm_t = 73_400_577;
pub const PSA_ALG_CCM: psa_algorithm_t = 104_861_697;
pub const PSA_ALG_GCM: psa_algorithm_t = 104_861_698;
pub const PSA_ALG_CHACHA20_POLY1305: psa_algorithm_t = 84_935_936;
pub const PSA_ALG_FFDH: psa_algorithm_t = 0x30100000; // Doesn't match latest PSA spec
pub const PSA_ALG_ECDH: psa_algorithm_t = 0x30200000; // Doesn't match latest PSA spec
pub const PSA_ALG_RSA_PKCS1V15_SIGN_RAW: psa_algorithm_t = 268_566_528;
pub const PSA_ALG_ECDSA_ANY: psa_algorithm_t = 268_828_672;
pub const PSA_ALG_RSA_PKCS1V15_CRYPT: psa_algorithm_t = 302_120_960;
Expand All @@ -92,3 +95,9 @@ pub const PSA_KEY_USAGE_VERIFY: psa_key_usage_t = 2048;
pub const PSA_KEY_USAGE_DERIVE: psa_key_usage_t = 4096;
pub const PSA_KEY_ID_USER_MIN: psa_key_id_t = 0x0000_0001;
pub const PSA_KEY_ID_USER_MAX: psa_key_id_t = 0x3fff_ffff;

pub const PSA_KEY_DERIVATION_INPUT_SECRET: psa_key_derivation_step_t = 0x0101;
pub const PSA_KEY_DERIVATION_INPUT_LABEL: psa_key_derivation_step_t = 0x0201;
pub const PSA_KEY_DERIVATION_INPUT_SALT: psa_key_derivation_step_t = 0x0202;
pub const PSA_KEY_DERIVATION_INPUT_INFO: psa_key_derivation_step_t = 0x0203;
pub const PSA_KEY_DERIVATION_INPUT_SEED: psa_key_derivation_step_t = 0x0204;
48 changes: 48 additions & 0 deletions psa-crypto-sys/src/extras.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2020 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0
#![allow(non_snake_case)]
/// Additional functionality required that PSA Crypto does not provide
use crate::types::psa_algorithm_t;

/// Retrieves the tag length from an aead_alg.
/// Note: `aead_alg` is an AEAD algorithm, such that `PSA_ALG_IS_AEAD(aead_alg)` is `true`.
pub fn PSA_ALG_AEAD_TAG_TRUNCATED_LENGTH(aead_alg: psa_algorithm_t) -> usize {
const TAG_LENGTH_MASK: u32 = 0b111111; // tag lengths are 6 bits in length
const PSA_V1_0_0_TAG_LENGTH_START_BIT: u32 = 16; // tag length at bit position [21:16]

let pre_mask_tag_length = aead_alg >> PSA_V1_0_0_TAG_LENGTH_START_BIT;

(pre_mask_tag_length & TAG_LENGTH_MASK) as usize
}

#[test]
fn truncated_aead_length_1() {
let test_aead_alg = 0b11001110010010110001110011010011; // 21:16 is 001011
assert_eq!(11, PSA_ALG_AEAD_TAG_TRUNCATED_LENGTH(test_aead_alg));
}

#[test]
fn truncated_aead_length_2() {
let test_aead_alg = 0b11001110010000000001110011010011; // 21:16 is 000000
assert_eq!(0, PSA_ALG_AEAD_TAG_TRUNCATED_LENGTH(test_aead_alg));
}

#[test]
fn truncated_aead_length_3() {
let test_aead_alg = 0b11001110011111110001110011010011; // 21:16 is 111111
assert_eq!(63, PSA_ALG_AEAD_TAG_TRUNCATED_LENGTH(test_aead_alg));
}

#[test]
fn truncated_aead_length_full_range() {
// Test from 0 to 63
let base_mask = 0b11001110010000000001110011010011;
for test_val in 0..63 {
let test_mask = test_val << 16;
let test_aead_alg = base_mask | test_mask;
assert_eq!(
test_val as usize,
PSA_ALG_AEAD_TAG_TRUNCATED_LENGTH(test_aead_alg)
);
}
}
18 changes: 13 additions & 5 deletions psa-crypto-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ mod psa_crypto_binding {

#[allow(dead_code)]
mod constants;
#[cfg(feature = "interface")]
mod extras;
#[allow(dead_code)]
#[cfg(feature = "interface")]
mod shim_methods;
Expand All @@ -39,14 +41,18 @@ pub use types::*;

#[cfg(feature = "operations")]
pub use psa_crypto_binding::{
psa_asymmetric_decrypt, psa_asymmetric_encrypt, psa_close_key, psa_crypto_init,
psa_destroy_key, psa_export_key, psa_export_public_key, psa_generate_key, psa_generate_random,
psa_get_key_attributes, psa_import_key, psa_open_key, psa_reset_key_attributes, psa_sign_hash,
psa_verify_hash,
psa_aead_decrypt, psa_aead_encrypt, psa_asymmetric_decrypt, psa_asymmetric_encrypt,
psa_close_key, psa_copy_key, psa_crypto_init, psa_destroy_key, psa_export_key,
psa_export_public_key, psa_generate_key, psa_generate_random, psa_get_key_attributes,
psa_hash_compare, psa_hash_compute, psa_import_key, psa_key_derivation_abort,
psa_key_derivation_input_bytes, psa_key_derivation_input_key, psa_key_derivation_key_agreement,
psa_key_derivation_output_key, psa_key_derivation_set_capacity, psa_key_derivation_setup,
psa_mac_compute, psa_mac_verify, psa_open_key, psa_raw_key_agreement, psa_reset_key_attributes,
psa_sign_hash, psa_verify_hash,
};

#[cfg(feature = "interface")]
pub use psa_crypto_binding::psa_key_attributes_t;
pub use psa_crypto_binding::{psa_key_attributes_t, psa_key_derivation_operation_t};

// Secure Element Driver definitions
#[cfg(feature = "interface")]
Expand All @@ -55,5 +61,7 @@ pub use psa_crypto_binding::{
psa_key_creation_method_t, psa_key_slot_number_t,
};

#[cfg(feature = "interface")]
pub use extras::*;
#[cfg(feature = "interface")]
pub use shim_methods::*;
Loading

0 comments on commit f4af888

Please sign in to comment.