[c] ECDSA sign
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- void crypto_sign_message(uint8_t * msg, uint16_t msg_len, uint8_t * buffer)
- {
- uint16_t len_b64;
- uint16_t olen;
- uint8_t hash[32];
- uint8_t hash_b64[100];
- uint8_t sign[256];
- uint8_t sign_b64[256];
- mbedtls_pk_context pk, pk2;
- CRY_DEBUG_PRINTF("Message: %s", msg);
- /* Import private key */
- mbedtls_pk_init(&pk);
- if (mbedtls_pk_parse_key(&pk, (const unsigned char *) flash.flash_ver0.ecc_priv_key,
- strlen(flash.flash_ver0.ecc_priv_key) + 1,
- (const unsigned char *)CA_DEF_ISSUER_PWD,
- CA_DEF_ISSUER_PWD_LEN) != 0)
- {
- CRY_DEBUG_PRINTF("Private key error!");
- return;
- }
- CRY_DEBUG_PRINTF("Private key: %s", flash.flash_ver0.ecc_priv_key);
- /* Hash message */
- if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), msg, msg_len, hash) != 0)
- {
- CRY_DEBUG_PRINTF("Hashing error!");
- return;
- }
- mbedtls_base64_encode(hash_b64, sizeof(hash_b64), (size_t *) &len_b64, hash, 32);
- CRY_DEBUG_PRINTF("Hash: %s", hash_b64);
- /* Sign message */
- olen = 0;
- if (mbedtls_pk_sign(&pk, MBEDTLS_MD_SHA256, hash, 0, sign, (size_t *)&olen, mbedtls_ctr_drbg_random, &ctr_drbg) != 0)
- {
- CRY_DEBUG_PRINTF("Signing error!");
- return;
- }
- mbedtls_base64_encode(sign_b64, sizeof(sign_b64), (size_t *) &len_b64, sign, olen);
- CRY_DEBUG_PRINTF("Signature: %s", sign_b64);
- /* Import public key */
- mbedtls_pk_init(&pk2);
- if (mbedtls_pk_parse_public_key(&pk2, (const unsigned char *) flash.flash_ver0.ecc_key,
- strlen(flash.flash_ver0.ecc_key) + 1) != 0)
- {
- CRY_DEBUG_PRINTF("Public key error!");
- return false;
- }
- CRY_DEBUG_PRINTF("Public key: %s", flash.flash_ver0.ecc_key);
- if (mbedtls_pk_verify(&pk2, MBEDTLS_MD_SHA256, hash, 0, sign, olen) == 0)
- {
- CRY_DEBUG_PRINTF("Verify: OK");
- }
- else
- {
- CRY_DEBUG_PRINTF("Verify: Fail");
- }
- }
Editor
You can edit this paste and save as new: