This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

I like to convert private key to code.

Hello,

  I generate a private key by using nrfutil.

  I like to convert raw data format to use in the code from private key.

  Additionally, I already used a nrf_crypto_ecc_key_pair_generate() function to generate private key.

  But, I don't  know how to convert a format to use it on PC. 

  Any way( private key(*.pem) to code or Key generated from board to PC)  is Okay.

   Please, help me out to resolve this conversion issue.

  Thanks,

     Chongchun Moon

PS C:\work\E-ciga\dfu> nrfutil keys display --key sk --format hex .\test.pem

WARNING: Security risk! Do not share the private key.

Private (signing) key sk:
6a72e923fa15c81782c90a02f6c7e9755d4509c5d9be16e5f9f2830fae7754dc

  • Hello,

     I just use example(examples\crypto\nrf_crypto\ecdsa) in the SDK

     And, I just replaced private key and public key on that example as my previous comment.

     So, I believe I am using same hash and generation procedure for signature is no problem.

     Anyway,  code is attached as below.

    /** @brief Alice signs the message.
     */
    static void alice_sign()
    {
        static nrf_crypto_ecc_private_key_t alice_private_key;
        ret_code_t                          err_code = NRF_SUCCESS;
    
        NRF_LOG_INFO("Alice's signature generation");
    
        // Alice converts her raw private key to internal representation
        err_code = nrf_crypto_ecc_private_key_from_raw(&g_nrf_crypto_ecc_secp256r1_curve_info,
                                                       &alice_private_key,
                                                       m_alice_raw_private_key,
                                                       sizeof(m_alice_raw_private_key));
        DEMO_ERROR_CHECK(err_code);
    
        // Alice generates signature using ECDSA and SHA-256
        m_signature_size = sizeof(m_signature);
        err_code = nrf_crypto_ecdsa_sign(NULL,
                                         &alice_private_key,
                                         m_hash,
                                         sizeof(m_hash),
                                         m_signature,
                                         &m_signature_size);
        DEMO_ERROR_CHECK(err_code);
    
        // Alice can now send the message and its signature to Bob
        print_hex("Alice's message hash: ", m_hash, sizeof(m_hash));
        print_hex("Alice's signature: ", m_signature, m_signature_size);
    
        // Key deallocation
        err_code = nrf_crypto_ecc_private_key_free(&alice_private_key);
        DEMO_ERROR_CHECK(err_code);
    }

    You can check if the signature you generate using the private key in your code match with the signature generated by nrfutil. 

    How can I check the signature? Please help me out to do it.

    Could you tell me private key conversion as below is correct?

      Private key generation :

     nrfutil keys display --key sk --format hex .\test.pem

     WARNING: Security risk! Do not share the private key.

     Private (signing) key sk:
     b954ae7fea6910692bd6879072074ea1519676922f11984290f2f24ddb104904

     Private key conversion :

    static uint8_t m_alice_raw_private_key[] =
    {
        0xb9, 0x54, 0xae, 0x7f, 0xea, 0x69, 0x10, 0x69,
        0x2b, 0xd6, 0x87, 0x90, 0x72, 0x07, 0x4e, 0xa1,
        0x51, 0x96, 0x76, 0x92, 0x2f, 0x11, 0x98, 0x42,
        0x90, 0xf2, 0xf2, 0x4d, 0xdb, 0x10, 0x49, 0x04,
    };

     

    Thanks,

      Chongchun Moon

  • Hi Moon, 

    Ok so you just want to try encrypt a message using the same private key and public key generated by nrfutil. 

    I will try to test here and let you now if it works. 

  • Hello,

      Is there any update for this? 

    Thanks,

      Chongchun Moon

  • Hi Moon, 

    I'm really sorry for the late response. I was out of office in the last 2 weeks. I tried to test what you wanted to do and had the same issue. I have contacted our team to find the reason why it didn't work but haven't got the solution yet. 

    I will try to push this. Will update you if I have any response from the team. 

Related