Mbed Crypto 和 MbedTLS 使用方法

看了下这两个库的源码,好像是一样的,MbedTLS更新的时间比较近一些,先用这个库试试。
试了下,想用这个库,至少留20k内存给它。


什么是 Mbed Crypto

Mbed Crypto 的是一个开源的加密库,支持很多种加密方式,包括

  • 密钥管理
  • 哈希
  • 对称加密
  • 非对称加密
  • 消息身份验证
  • 密钥生成和分发
  • 带关联数据的加密认证

Mbed Crypto库是Arm平台安全架构(PSA)加密接口的参考实现。 它是用可移植的C语言编写的。
Mbed Crypto库是在Apache许可证下发布的,版本2.0。


什么是平台安全架构(PSA)

Arm的平台安全架构(PSA)是一套完整的威胁模型,安全分析、硬件和固件体系结构规范,以及开源固件参考实现。PSA提供了一种基于行业最佳实践的方法,使您能够一致地将安全性设计到硬件和固件中。PSA提供的API的一部分是加密接口,它提供对一组原语的访问。


使用方法

  • 获取库方法
  • 构建库方法
  • 使用库方法
  • 导入个密钥
  • 对一个消息使用RSA签名
  • 对称加解密方法
  • 散列消息
  • 从已经存在的消息中派生一个新密钥
  • 生成一个随机数
  • 身份验证并加解密一段消息
  • 生成并导出密钥
  • 更多API相关的东西

获得方法

github上有


构建方法

  • GNU Make
  • C语言工具链
  • python来生成测试代码
  • 用Perl来跑测试代码

省略


使用这个库

一定要首先调用 psa_crypto_init()这个函数


导入一个密钥

先导入一个密钥,然后拿着这个handle后续调用。

导入密钥的前提条件:

  • psa_crypto_init()这个函数已经成功返回

下面的例子显示怎么去导入这个密钥:

void import_a_key(const uint8_t *key, size_t key_len)
{
    psa_status_t status;
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
    psa_key_handle_t handle;

    printf("Import an AES key...\t");
    fflush(stdout);

    /* Initialize PSA Crypto */
    status = psa_crypto_init();
    if (status != PSA_SUCCESS) {
        printf("Failed to initialize PSA Crypto\n");
        return;
    }

    /* Set key attributes */
    psa_set_key_usage_flags(&attributes, 0);
    psa_set_key_algorithm(&attributes, 0);
    psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
    psa_set_key_bits(&attributes, 128);

    /* Import the key */
    status = psa_import_key(&attributes, key, key_len, &handle);
    if (status != PSA_SUCCESS) {
        printf("Failed to import key\n");
        return;
    }
    printf("Imported a key\n");

    /* Free the attributes */
    psa_reset_key_attributes(&attributes);

    /* Destroy the key */
    psa_destroy_key(handle);

    mbedtls_psa_crypto_free();
}

使用RSA签名消息

Mbed Crypto支持使用公钥签名算法(如RSA或ECDSA)对消息进行加密、解密、签名和验证。
执行非对称签名操作的前提条件:

  • 先用 psa_crypto_init()
  • 有一个有效的键与适当的属性设置 :
    • 使用’ PSA_KEY_USAGE_SIGN_HASH '标志允许签名。
    • 使用标志’ PSA_KEY_USAGE_VERIFY_HASH '允许签名验证。
    • 算法设置为签名算法。

这个例子展示了如何对已经计算过的哈希进行签名:

void sign_a_message_using_rsa(const uint8_t *key, size_t key_len)
{
    psa_status_t status;
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
    uint8_t hash[32] = {0x50, 0xd8, 0x58, 0xe0, 0x98, 0x5e, 0xcc, 0x7f,
                        0x60, 0x41, 0x8a, 0xaf, 0x0c, 0xc5, 0xab, 0x58,
                        0x7f, 0x42, 0xc2, 0x57, 0x0a, 0x88, 0x40, 0x95,
                        0xa9, 0xe8, 0xcc, 0xac, 0xd0, 0xf6, 0x54, 0x5c};
    uint8_t signature[PSA_SIGNATURE_MAX_SIZE] = {0};
    size_t signature_length;
    psa_key_handle_t handle;

    printf("Sign a message...\t");
    fflush(stdout);

    /* Initialize PSA Crypto */
    status = psa_crypto_init();
    if (status != PSA_SUCCESS) {
        printf("Failed to initialize PSA Crypto\n");
        return;
    }

    /* Set key attributes */
    psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
    psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_SIGN_RAW);
    psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
    psa_set_key_bits(&attributes, 1024);

    /* Import the key */
    status = psa_import_key(&attributes, key, key_len, &handle);
    if (status != PSA_SUCCESS) {
        printf("Failed to import key\n");
        return;
    }

    /* Sign message using the key */
    status = psa_sign_hash(handle, PSA_ALG_RSA_PKCS1V15_SIGN_RAW,
                           hash, sizeof(hash),
                           signature, sizeof(signature),
                           &signature_length);
    if (status != PSA_SUCCESS) {
        printf("Failed to sign\n");
        return;
    }

    printf("Signed a message\n");

    /* Free the attributes */
    psa_reset_key_attributes(&attributes);

    /* Destroy the key */
    psa_destroy_key(handle);

    mbedtls_psa_crypto_free();
}

使用对称加密

Mbed Crypto支持使用各种对称密码算法(块和流密码)加密和解密消息。

使用对称密码API的先决条件:

  • psa_crypto_init()
  • 有一个对称键的句柄。此密钥的使用标志必须包括允许加密的’ PSA_KEY_USAGE_ENCRYPT ‘或允许解密的’ PSA_KEY_USAGE_DECRYPT '。

使用对称密码加密消息:

  1. 分配一个操作(’ psa_cipher_operation_t ')结构传递给密码函数。
  2. 将操作结构初始化为0或’ PSA_CIPHER_OPERATION_INIT '。
  3. 调用’ psa_cipher_encrypt_setup() '指定要使用的算法和密钥。
  4. 调用’ psa_cipher_generate_iv() ‘或’ psa_cipher_set_iv() ‘来生成或设置初始化向量(IV)。我们建议调用’ psa_cipher_generate_iv() ',除非你需要一个特定的IV值。
  5. 使用要加密的消息调用’ psa_cipher_update() '。你可以多次调用这个函数,在连续的调用中传递消息的连续片段。
  6. 调用’ psa_cipher_finish() '结束操作并输出加密消息。

这个例子展示了如何在没有填充的CBC (Cipher Block chainaining)模式下使用AES (Advanced Encryption Standard)密钥加密数据(假设所有先决条件都已经满足):

void encrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len)
{
    enum {
        block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES),
    };
    psa_status_t status;
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
    psa_algorithm_t alg = PSA_ALG_CBC_NO_PADDING;
    uint8_t plaintext[block_size] = SOME_PLAINTEXT;
    uint8_t iv[block_size];
    size_t iv_len;
    uint8_t output[block_size];
    size_t output_len;
    psa_key_handle_t handle;
    psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;

    printf("Encrypt with cipher...\t");
    fflush(stdout);

    /* Initialize PSA Crypto */
    status = psa_crypto_init();
    if (status != PSA_SUCCESS)
    {
        printf("Failed to initialize PSA Crypto\n");
        return;
    }

    /* Import a key */
    psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
    psa_set_key_algorithm(&attributes, alg);
    psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
    psa_set_key_bits(&attributes, 128);
    status = psa_import_key(&attributes, key, key_len, &handle);
    if (status != PSA_SUCCESS) {
        printf("Failed to import a key\n");
        return;
    }
    psa_reset_key_attributes(&attributes);

    /* Encrypt the plaintext */
    status = psa_cipher_encrypt_setup(&operation, handle, alg);
    if (status != PSA_SUCCESS) {
        printf("Failed to begin cipher operation\n");
        return;
    }
    status = psa_cipher_generate_iv(&operation, iv, sizeof(iv), &iv_len);
    if (status != PSA_SUCCESS) {
        printf("Failed to generate IV\n");
        return;
    }
    status = psa_cipher_update(&operation, plaintext, sizeof(plaintext),
                               output, sizeof(output), &output_len);
    if (status != PSA_SUCCESS) {
        printf("Failed to update cipher operation\n");
        return;
    }
    status = psa_cipher_finish(&operation, output + output_len,
                               sizeof(output) - output_len, &output_len);
    if (status != PSA_SUCCESS) {
        printf("Failed to finish cipher operation\n");
        return;
    }
    printf("Encrypted plaintext\n");

    /* Clean up cipher operation context */
    psa_cipher_abort(&operation);

    /* Destroy the key */
    psa_destroy_key(handle);

    mbedtls_psa_crypto_free();
}

用对称密码解密消息:

  1. 分配一个操作(’ psa_cipher_operation_t ')结构传递给密码函数。
  2. 将操作结构初始化为0或’ PSA_CIPHER_OPERATION_INIT '。
  3. 调用’ psa_cipher_decrypt_setup() '来指定要使用的算法和密钥。
  4. 调用’ psa_cipher_set_iv() '和IV一起进行解密。
  5. 使用要加密的消息调用’ psa_cipher_update() '。你可以多次调用这个函数,在连续的调用中传递消息的连续片段。
  6. 调用’ psa_cipher_finish() '结束操作并输出解密消息。

这个示例展示了如何在没有填充的CBC模式下使用AES密钥解密加密的数据
(假设所有先决条件都已满足):

void decrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len)
{
    enum {
        block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES),
    };
    psa_status_t status;
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
    psa_algorithm_t alg = PSA_ALG_CBC_NO_PADDING;
    psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
    uint8_t ciphertext[block_size] = SOME_CIPHERTEXT;
    uint8_t iv[block_size] = ENCRYPTED_WITH_IV;
    uint8_t output[block_size];
    size_t output_len;
    psa_key_handle_t handle;

    printf("Decrypt with cipher...\t");
    fflush(stdout);

    /* Initialize PSA Crypto */
    status = psa_crypto_init();
    if (status != PSA_SUCCESS)
    {
        printf("Failed to initialize PSA Crypto\n");
        return;
    }

    /* Import a key */
    psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
    psa_set_key_algorithm(&attributes, alg);
    psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
    psa_set_key_bits(&attributes, 128);
    status = psa_import_key(&attributes, key, key_len, &handle);
    if (status != PSA_SUCCESS) {
        printf("Failed to import a key\n");
        return;
    }
    psa_reset_key_attributes(&attributes);

    /* Decrypt the ciphertext */
    status = psa_cipher_decrypt_setup(&operation, handle, alg);
    if (status != PSA_SUCCESS) {
        printf("Failed to begin cipher operation\n");
        return;
    }
    status = psa_cipher_set_iv(&operation, iv, sizeof(iv));
    if (status != PSA_SUCCESS) {
        printf("Failed to set IV\n");
        return;
    }
    status = psa_cipher_update(&operation, ciphertext, sizeof(ciphertext),
                               output, sizeof(output), &output_len);
    if (status != PSA_SUCCESS) {
        printf("Failed to update cipher operation\n");
        return;
    }
    status = psa_cipher_finish(&operation, output + output_len,
                               sizeof(output) - output_len, &output_len);
    if (status != PSA_SUCCESS) {
        printf("Failed to finish cipher operation\n");
        return;
    }
    printf("Decrypted ciphertext\n");

    /* Clean up cipher operation context */
    psa_cipher_abort(&operation);

    /* Destroy the key */
    psa_destroy_key(handle);

    mbedtls_psa_crypto_free();
}

处理密码操作上下文

在通过成功调用’ psa_cipher_encrypt_setup() ‘或’ psa_cipher_decrypt_setup() ‘初始化操作结构后,可以通过调用’ psa_cipher_abort() '随时终止操作。

调用’ psa_cipher_abort() '释放与操作关联的任何资源,除了操作结构本身。

Mbed Crypto隐式调用’ psa_cipher_abort() '的情况:

  • 调用’ psa_cipher_generate_iv() ', ’ psa_cipher_set_iv() ‘或’ psa_cipher_update() ‘失败(返回’ PSA_SUCCESS '以外的任何状态)。
  • 调用’ psa_cipher_finish() '成功或失败。

在隐式或显式调用’ psa_cipher_abort() ‘之后,操作结构将失效;换句话说,您不能为相同的操作重用操作结构。但是,您可以通过再次调用’ psa_cipher_encrypt_setup() '或’psa_cipher_decrypt_setup() '来重用不同操作的操作结构。

对于任何成功初始化的操作(通过成功调用’ psa_cipher_encrypt_setup() '或’psa_cipher_decrypt_setup() ‘),您必须在某个点调用’ psa_cipher_abort() '。

对被(隐式或显式)终止的操作进行多次连续调用’ psa_cipher_abort() '是安全的,并且没有效果。


散列消息

Mbed Crypto允许您使用各种散列算法来计算和验证散列。

使用哈希api的先决条件:

  • 通过成功调用’ psa_crypto_init() '初始化库。

计算哈希值:

  1. 分配一个操作结构(’ psa_hash_operation_t ')传递给哈希函数。
  2. 将操作结构初始化为零或’ PSA_HASH_OPERATION_INIT '。
  3. 调用’ psa_hash_setup() '指定哈希算法。
  4. 使用要加密的消息调用’ psa_hash_update() '。你可以多次调用这个函数,在连续的调用中传递消息的连续片段。
  5. 调用’ psa_hash_finish() ‘来计算哈希值,或调用’ psa_hash_verify() '来比较计算的哈希值和预期的哈希值。

这个例子展示了如何计算一个消息的SHA-256哈希值:

 	psa_status_t status;
    psa_algorithm_t alg = PSA_ALG_SHA_256;
    psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
    unsigned char input[] = { 'a', 'b', 'c' };
    unsigned char actual_hash[PSA_HASH_MAX_SIZE];
    size_t actual_hash_len;

    printf("Hash a message...\t");
    fflush(stdout);

    /* Initialize PSA Crypto */
    status = psa_crypto_init();
    if (status != PSA_SUCCESS) {
        printf("Failed to initialize PSA Crypto\n");
        return;
    }

    /* Compute hash of message  */
    status = psa_hash_setup(&operation, alg);
    if (status != PSA_SUCCESS) {
        printf("Failed to begin hash operation\n");
        return;
    }
    status = psa_hash_update(&operation, input, sizeof(input));
    if (status != PSA_SUCCESS) {
        printf("Failed to update hash operation\n");
        return;
    }
    status = psa_hash_finish(&operation, actual_hash, sizeof(actual_hash),
                             &actual_hash_len);
    if (status != PSA_SUCCESS) {
        printf("Failed to finish hash operation\n");
        return;
    }

    printf("Hashed a message\n");

    /* Clean up hash operation context */
    psa_hash_abort(&operation);

    mbedtls_psa_crypto_free();

这个例子展示了如何验证消息的SHA-256哈希值:

  	psa_status_t status;
    psa_algorithm_t alg = PSA_ALG_SHA_256;
    psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
    unsigned char input[] = { 'a', 'b', 'c' };
    unsigned char expected_hash[] = {
        0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde,
        0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
        0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
    };
    size_t expected_hash_len = PSA_HASH_SIZE(alg);

    printf("Verify a hash...\t");
    fflush(stdout);

    /* Initialize PSA Crypto */
    status = psa_crypto_init();
    if (status != PSA_SUCCESS) {
        printf("Failed to initialize PSA Crypto\n");
        return;
    }

    /* Verify message hash */
    status = psa_hash_setup(&operation, alg);
    if (status != PSA_SUCCESS) {
        printf("Failed to begin hash operation\n");
        return;
    }
    status = psa_hash_update(&operation, input, sizeof(input));
    if (status != PSA_SUCCESS) {
        printf("Failed to update hash operation\n");
        return;
    }
    status = psa_hash_verify(&operation, expected_hash, expected_hash_len);
    if (status != PSA_SUCCESS) {
        printf("Failed to verify hash\n");
        return;
    }

    printf("Verified a hash\n");

    /* Clean up hash operation context */
    psa_hash_abort(&operation);

    mbedtls_psa_crypto_free();

API提供宏’ PSA_HASH_SIZE ',它返回指定算法的预期哈希长度(以字节为单位)。


处理哈希操作上下文

在成功调用’ psa_hash_setup() ‘之后,您可以通过调用’ psa_hash_abort() ‘随时终止操作。调用’ psa_hash_abort() '释放与操作关联的任何资源,除了操作结构本身。

Mbed Crypto隐式调用’ psa_hash_abort() '的情况:

  1. 调用’ psa_hash_update() ‘失败(返回’ PSA_SUCCESS '以外的任何状态)。
  2. 调用’ psa_hash_finish() '成功或失败。
  3. 调用’ psa_hash_verify() '成功或失败。

在隐式或显式调用’ psa_hash_abort() ‘之后,操作结构将失效;换句话说,您不能为相同的操作重用操作结构。但是,您可以通过再次调用’ psa_hash_setup() '来重用不同操作的操作结构。

对于任何成功初始化的操作(通过成功调用’ psa_hash_setup() ‘),您必须在某个点调用’ psa_hash_abort() '。

对已经终止(隐式或显式)的操作进行多次连续调用’ psa_hash_abort() '是安全的,并且没有效果。


生成随机值

Mbed Crypto可以生成随机数据。

生成随机数据的先决条件:

  • 通过成功调用’ psa_crypto_init() '初始化库。

注: 要生成一个随机密钥,使用’ psa_generate_key() ‘而不是’ psa_generate_random() ’ .

这个例子展示了如何通过调用’ psa_generate_random() '来生成10字节的随机数据:

    psa_status_t status;
    uint8_t random[10] = { 0 };

    printf("Generate random...\t");
    fflush(stdout);

    /* Initialize PSA Crypto */
    status = psa_crypto_init();
    if (status != PSA_SUCCESS) {
        printf("Failed to initialize PSA Crypto\n");
        return;
    }

    status = psa_generate_random(random, sizeof(random));
    if (status != PSA_SUCCESS) {
        printf("Failed to generate a random value\n");
        return;
    }

    printf("Generated random data\n");

    /* Clean up */
    mbedtls_psa_crypto_free();

从现有密钥派生新密钥

Mbed Crypto提供了一个密钥派生API,允许您从现有密钥派生新的密钥。 密钥派生API具有获取输入(包括其他密钥和数据)的函数,以及生成输出(如新密钥或其他数据)的函数。

您必须首先初始化并设置一个密钥派生上下文,该上下文由一个密钥和(可选)其他数据提供。 然后,使用密钥派生上下文将派生数据读入缓冲区或直接将派生数据发送到键槽。
请参阅特定算法(如HKDF或TLS1.2 PRF)的文档,了解何时通过哪些输入以及何时可以获得哪些输出的信息。

使用密钥派生api的先决条件:

  • 通过成功调用’ psa_crypto_init() '初始化库。
  • 使用具有适当属性的键:
    • 设置密钥派生的使用标志(’ PSA_KEY_USAGE_DERIVE ')
    • 密钥类型设置为PSA_KEY_TYPE_DERIVE
    • 算法设置为一个密钥派生算法 (例如,“PSA_ALG_HKDF (PSA_ALG_SHA_256)”)。

使用HKDF和给定的密钥、salt和info,将新的AES-CTR 128位加密密钥导出到给定的密钥槽:

  1. 使用’ psa_key_derivation_setup() ‘设置键派生上下文函数,指定派生算法’ PSA_ALG_HKDF(PSA_ALG_SHA_256) '。
  2. 使用’ psa_key_derivation_input_bytes() '提供一个可选的salt。
  3. 使用’ psa_key_derivation_input_bytes() '提供信息。
  4. 使用’ psa_key_derivation_input_key() '提供一个密钥,它引用一个密钥可用于密钥派生。
  5. 为新派生的密钥设置所需的属性。在这个例子里我们将设置使用’ PSA_KEY_USAGE_ENCRYPT ‘标志和’ PSA_ALG_CTR '算法。
  6. 通过调用’ psa_key_derivation_output_key() '获得密钥。
  7. 清理密钥派生上下文。

此时,派生的密钥槽持有一个新的128位AES-CTR加密密钥从密钥,盐和提供的信息派生:

    psa_status_t status;
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
    static const unsigned char key[] = {
        0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
        0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
        0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
        0x0b };
    static const unsigned char salt[] = {
        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
        0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c };
    static const unsigned char info[] = {
        0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6,
        0xf7, 0xf8, 0xf9 };
    psa_algorithm_t alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
    psa_key_derivation_operation_t operation =
        PSA_KEY_DERIVATION_OPERATION_INIT;
    size_t derived_bits = 128;
    size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
    psa_key_handle_t base_key;
    psa_key_handle_t derived_key;

    printf("Derive a key (HKDF)...\t");
    fflush(stdout);

    /* Initialize PSA Crypto */
    status = psa_crypto_init();
    if (status != PSA_SUCCESS) {
        printf("Failed to initialize PSA Crypto\n");
        return;
    }

    /* Import a key for use in key derivation. If such a key has already been
     * generated or imported, you can skip this part. */
    psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
    psa_set_key_algorithm(&attributes, alg);
    psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
    status = psa_import_key(&attributes, key, sizeof(key), &base_key);
    if (status != PSA_SUCCESS) {
        printf("Failed to import a key\n");
        return;
    }
    psa_reset_key_attributes(&attributes);

    /* Derive a key */
    status = psa_key_derivation_setup(&operation, alg);
    if (status != PSA_SUCCESS) {
        printf("Failed to begin key derivation\n");
        return;
    }
    status = psa_key_derivation_set_capacity(&operation, capacity);
    if (status != PSA_SUCCESS) {
        printf("Failed to set capacity\n");
        return;
    }
    status = psa_key_derivation_input_bytes(&operation,
                                            PSA_KEY_DERIVATION_INPUT_SALT,
                                            salt, sizeof(salt));
    if (status != PSA_SUCCESS) {
        printf("Failed to input salt (extract)\n");
        return;
    }
    status = psa_key_derivation_input_key(&operation,
                                          PSA_KEY_DERIVATION_INPUT_SECRET,
                                          base_key);
    if (status != PSA_SUCCESS) {
        printf("Failed to input key (extract)\n");
        return;
    }
    status = psa_key_derivation_input_bytes(&operation,
                                            PSA_KEY_DERIVATION_INPUT_INFO,
                                            info, sizeof(info));
    if (status != PSA_SUCCESS) {
        printf("Failed to input info (expand)\n");
        return;
    }
    psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
    psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
    psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
    psa_set_key_bits(&attributes, 128);
    status = psa_key_derivation_output_key(&attributes, &operation,
                                           &derived_key);
    if (status != PSA_SUCCESS) {
        printf("Failed to derive key\n");
        return;
    }
    psa_reset_key_attributes(&attributes);

    printf("Derived key\n");

    /* Clean up key derivation operation */
    psa_key_derivation_abort(&operation);

    /* Destroy the keys */
    psa_destroy_key(derived_key);
    psa_destroy_key(base_key);

    mbedtls_psa_crypto_free();

对消息进行身份验证、加密或解密

Mbed Crypto提供了一种使用关联数据(AEAD)进行身份验证和加密的简单方法,支持“PSA_ALG_CCM”算法。

使用AEAD加密api的先决条件:

  • 通过成功调用’ psa_crypto_init() '初始化库。
  • 用于派生的密钥的密钥属性必须具有’ PSA_KEY_USAGE_ENCRYPT ‘或’ PSA_KEY_USAGE_DECRYPT '的使用标志。

这个例子展示了如何验证和加密消息:

    psa_status_t status;
    static const uint8_t key[] = {
        0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
        0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF };
    static const uint8_t nonce[] = {
        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0A, 0x0B };
    static const uint8_t additional_data[] = {
        0xEC, 0x46, 0xBB, 0x63, 0xB0, 0x25,
        0x20, 0xC3, 0x3C, 0x49, 0xFD, 0x70 };
    static const uint8_t input_data[] = {
        0x20, 0x30, 0xE0, 0x36, 0xED, 0x09, 0xA0, 0x45, 0xAF, 0x3C, 0xBA, 0xEE,
        0x0F, 0xC8, 0x48, 0xAF, 0xCD, 0x89, 0x54, 0xF4, 0xF6, 0x3F, 0x28, 0x9A,
        0xA1, 0xDD, 0xB2, 0xB8, 0x09, 0xCD, 0x7C, 0xE1, 0x46, 0xE9, 0x98 };
    uint8_t *output_data = NULL;
    size_t output_size = 0;
    size_t output_length = 0;
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
    psa_key_handle_t handle;

    printf("Authenticate decrypt...\t");
    fflush(stdout);

    /* Initialize PSA Crypto */
    status = psa_crypto_init();
    if (status != PSA_SUCCESS) {
        printf("Failed to initialize PSA Crypto\n");
        return;
    }

    output_size = sizeof(input_data);
    output_data = (uint8_t *)malloc(output_size);
    if (!output_data) {
        printf("Out of memory\n");
        return;
    }

    /* Import a key */
    psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
    psa_set_key_algorithm(&attributes, PSA_ALG_CCM);
    psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
    psa_set_key_bits(&attributes, 128);
    status = psa_import_key(&attributes, key, sizeof(key), &handle);
    if (status != PSA_SUCCESS) {
        printf("Failed to import a key\n");
        return;
    }
    psa_reset_key_attributes(&attributes);

    /* Authenticate and decrypt */
    status = psa_aead_decrypt(handle, PSA_ALG_CCM,
                              nonce, sizeof(nonce),
                              additional_data, sizeof(additional_data),
                              input_data, sizeof(input_data),
                              output_data, output_size,
                              &output_length);
    if (status != PSA_SUCCESS) {
        printf("Failed to authenticate and decrypt %ld\n", status);
        return;
    }

    printf("Authenticated and decrypted\n");

    /* Clean up */
    free(output_data);

    /* Destroy the key */
    psa_destroy_key(handle);

    mbedtls_psa_crypto_free();

生成和导出密钥

Mbed Crypto提供了一种生成密钥或密钥对的简单方法。

使用密钥生成和导出api的先决条件:

  • 通过成功调用’ psa_crypto_init() '初始化库。

生成ECDSA密钥:

  1. 通过调用,为生成密钥设置所需的属性’ psa_set_key_algorithm() ‘使用选择的ECDSA算法(例如“PSA_ALG_DETERMINISTIC_ECDSA (PSA_ALG_SHA_256)”)。您只想导出公钥,而不是密钥对(或私钥);因此,不要设置’ PSA_KEY_USAGE_EXPORT '。
  2. 通过调用’ psa_generate_key() '生成密钥。
  3. 通过调用’ psa_export_public_key() '导出生成的公钥:
    enum {
        key_bits = 256,
    };
    psa_status_t status;
    size_t exported_length = 0;
    static uint8_t exported[PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits)];
    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
    psa_key_handle_t handle;

    printf("Generate a key pair...\t");
    fflush(stdout);

    /* Initialize PSA Crypto */
    status = psa_crypto_init();
    if (status != PSA_SUCCESS) {
        printf("Failed to initialize PSA Crypto\n");
        return;
    }

    /* Generate a key */
    psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
    psa_set_key_algorithm(&attributes,
                          PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256));
    psa_set_key_type(&attributes,
                     PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_CURVE_SECP256R1));
    psa_set_key_bits(&attributes, key_bits);
    status = psa_generate_key(&attributes, &handle);
    if (status != PSA_SUCCESS) {
        printf("Failed to generate key\n");
        return;
    }
    psa_reset_key_attributes(&attributes);

    status = psa_export_public_key(handle, exported, sizeof(exported),
                                   &exported_length);
    if (status != PSA_SUCCESS) {
        printf("Failed to export public key %ld\n", status);
        return;
    }

    printf("Exported a public key\n");

    /* Destroy the key */
    psa_destroy_key(handle);

    mbedtls_psa_crypto_free();

更多关于PSA Crypto API

有关PSA Crypto API的更多信息,请参阅 PSA加密API规范

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值