|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/sys/util.h> |
| 8 | +#include <zephyr/logging/log.h> |
| 9 | +#include <psa/crypto.h> |
| 10 | +#include <psa/crypto_extra.h> |
| 11 | + |
| 12 | +#include "ml_dsa_65_vectors.h" |
| 13 | + |
| 14 | +#define APP_SUCCESS (0) |
| 15 | +#define APP_ERROR (-1) |
| 16 | +#define APP_SUCCESS_MESSAGE "Example finished successfully!" |
| 17 | +#define APP_ERROR_MESSAGE "Example exited with error!" |
| 18 | + |
| 19 | +/* ML-DSA signatures and keys are long, so printing is limited to just first bytes of these */ |
| 20 | +#define PRINT_HEX_BYTE_LIMIT (16u) |
| 21 | + |
| 22 | +#define PRINT_HEX(p_label, p_text, len)\ |
| 23 | + ({\ |
| 24 | + size_t print_len = (size_t)(len);\ |
| 25 | + size_t hex_bytes_to_print = MIN(print_len, (size_t)PRINT_HEX_BYTE_LIMIT); \ |
| 26 | + LOG_INF("---- %s (total len: %zu, printing %zu bytes): ----", \ |
| 27 | + p_label, print_len, hex_bytes_to_print); \ |
| 28 | + LOG_HEXDUMP_INF(p_text, hex_bytes_to_print, "Content:"); \ |
| 29 | + LOG_INF("---- %s end ----", p_label); \ |
| 30 | + }) |
| 31 | + |
| 32 | +LOG_MODULE_REGISTER(ml_dsa, LOG_LEVEL_DBG); |
| 33 | + |
| 34 | +/* ====================================================================== */ |
| 35 | +/* Global variables/defines for the ML-DSA example */ |
| 36 | + |
| 37 | +static psa_key_id_t pub_key_id; |
| 38 | +/* ====================================================================== */ |
| 39 | + |
| 40 | +int crypto_init(void) |
| 41 | +{ |
| 42 | + psa_status_t status; |
| 43 | + |
| 44 | + /* Initialize PSA Crypto */ |
| 45 | + status = psa_crypto_init(); |
| 46 | + if (status != PSA_SUCCESS) { |
| 47 | + LOG_ERR("psa_crypto_init failed! (Error: %d)", status); |
| 48 | + return APP_ERROR; |
| 49 | + } |
| 50 | + |
| 51 | + return APP_SUCCESS; |
| 52 | +} |
| 53 | + |
| 54 | +int crypto_finish(void) |
| 55 | +{ |
| 56 | + psa_status_t status; |
| 57 | + |
| 58 | + /* Destroy the key handle */ |
| 59 | + status = psa_destroy_key(pub_key_id); |
| 60 | + if (status != PSA_SUCCESS) { |
| 61 | + LOG_ERR("psa_destroy_key failed! (Error: %d)", status); |
| 62 | + return APP_ERROR; |
| 63 | + } |
| 64 | + |
| 65 | + return APP_SUCCESS; |
| 66 | +} |
| 67 | + |
| 68 | +int import_ml_dsa_pub_key(void) |
| 69 | +{ |
| 70 | + /* Configure the key attributes */ |
| 71 | + psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 72 | + psa_status_t status; |
| 73 | + |
| 74 | + LOG_INF("Importing an ML-DSA-65 public key..."); |
| 75 | + |
| 76 | + /* Configure the key attributes */ |
| 77 | + psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 78 | + psa_set_key_lifetime(&key_attributes, PSA_KEY_LIFETIME_VOLATILE); |
| 79 | + psa_set_key_algorithm(&key_attributes, PSA_ALG_ML_DSA); |
| 80 | + psa_set_key_type(&key_attributes, PSA_KEY_TYPE_ML_DSA_PUBLIC_KEY); |
| 81 | + |
| 82 | + status = psa_import_key(&key_attributes, ml_dsa_65_pub_key, sizeof(ml_dsa_65_pub_key), |
| 83 | + &pub_key_id); |
| 84 | + if (status != PSA_SUCCESS) { |
| 85 | + LOG_ERR("psa_import_key failed! (Error: %d)", status); |
| 86 | + return APP_ERROR; |
| 87 | + } |
| 88 | + |
| 89 | + LOG_INF("ML-DSA-65 public key imported successfully!"); |
| 90 | + PRINT_HEX("ML-DSA-65 public key", ml_dsa_65_pub_key, sizeof(ml_dsa_65_pub_key)); |
| 91 | + |
| 92 | + /* Reset key attributes and free any allocated resources. */ |
| 93 | + psa_reset_key_attributes(&key_attributes); |
| 94 | + |
| 95 | + return APP_SUCCESS; |
| 96 | +} |
| 97 | + |
| 98 | +int verify_message(void) |
| 99 | +{ |
| 100 | + psa_status_t status; |
| 101 | + |
| 102 | + LOG_INF("Verifying the ML-DSA signature..."); |
| 103 | + |
| 104 | + /* Verify the signature of the message */ |
| 105 | + status = psa_verify_message(pub_key_id, PSA_ALG_ML_DSA, ml_dsa_65_message, |
| 106 | + sizeof(ml_dsa_65_message), ml_dsa_65_signature, |
| 107 | + sizeof(ml_dsa_65_signature)); |
| 108 | + if (status != PSA_SUCCESS) { |
| 109 | + LOG_ERR("psa_verify_message failed! (Error: %d)", status); |
| 110 | + return APP_ERROR; |
| 111 | + } |
| 112 | + |
| 113 | + PRINT_HEX("Message", ml_dsa_65_message, sizeof(ml_dsa_65_message)); |
| 114 | + PRINT_HEX("Signature", ml_dsa_65_signature, sizeof(ml_dsa_65_signature)); |
| 115 | + LOG_INF("Signature verification was successful!"); |
| 116 | + |
| 117 | + return APP_SUCCESS; |
| 118 | +} |
| 119 | + |
| 120 | +int main(void) |
| 121 | +{ |
| 122 | + int status; |
| 123 | + |
| 124 | + LOG_INF("Starting ML-DSA example..."); |
| 125 | + |
| 126 | + status = crypto_init(); |
| 127 | + if (status != APP_SUCCESS) { |
| 128 | + LOG_INF(APP_ERROR_MESSAGE); |
| 129 | + return APP_ERROR; |
| 130 | + } |
| 131 | + |
| 132 | + status = import_ml_dsa_pub_key(); |
| 133 | + if (status != APP_SUCCESS) { |
| 134 | + LOG_INF(APP_ERROR_MESSAGE); |
| 135 | + return APP_ERROR; |
| 136 | + } |
| 137 | + |
| 138 | + status = verify_message(); |
| 139 | + if (status != APP_SUCCESS) { |
| 140 | + LOG_INF(APP_ERROR_MESSAGE); |
| 141 | + (void)crypto_finish(); |
| 142 | + return APP_ERROR; |
| 143 | + } |
| 144 | + |
| 145 | + status = crypto_finish(); |
| 146 | + if (status != APP_SUCCESS) { |
| 147 | + LOG_INF(APP_ERROR_MESSAGE); |
| 148 | + return APP_ERROR; |
| 149 | + } |
| 150 | + |
| 151 | + LOG_INF(APP_SUCCESS_MESSAGE); |
| 152 | + |
| 153 | + return APP_SUCCESS; |
| 154 | +} |
0 commit comments