Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Generating custom messages and records for NFC communication

Hi,

I am implementing an application where I need to transfer a 32 byte array via NFC. The only issue is that existing modules in nrf52 sdk have support for UTF-8 encoding, URI encoding etc.

I understand that I'll have to write custom records and a custom message descriptor for it. But I can't figure how to implement the entire module. Can someone guide me as to how I can proceed to develop a custom module that will allow me to transfer a 32 byte array via NFC, and then store in the the same format?

Thanks,

Varun

Parents
  • Hi Varun,

    I suggest that you try to modify the NFC text record example from the SDK and just put the 32 bytes in the payload. 

    It is up to the receiver on how this should get decoded.

  • Hi Martin, 

    Thanks. There are few more roadblocks that I'm facing. Let me explain the flow of my application first.

    I am using an android phone to write a 32 byte array to the nrf52 device which is running the Type 4 Tag (Read/Write) NFC example. Now, once the 32 byte array is written to the device, I need to use the same to create an ecdsa signature which is then written back to the FDS and loaded to the NDEF message buffer so that the android phone can read the signature from the device.

    Now, the problem that I am facing is, where should I pick the 32 byte array in the application to use it for further processing.

    I tried fetching the value from the ndef_message_buff variable, which stores the updated ndef message and generating the signature here, but I'm guessing this interrupts the write process to the fds and hence I receive an error.

    I also tried fetching the value from the FDS once the FDS Update Success event is called, and generating the signature from here, but this results in a recursive loop, since I'm performing an update to the fds right after generating the signature.

    For clarification, here is my generate_signature function.

    ret_code_t generate_signature(uint8_t m_hash[])
    {
        static nrf_crypto_ecc_private_key_t alice_private_key;
        ret_code_t                          err_code = NRF_SUCCESS;
    
        NRF_LOG_INFO("Transaction Signature Generation");
    
    
        err_code = nrf_crypto_ecc_private_key_from_raw(&g_nrf_crypto_ecc_secp256k1_curve_info,
                                                       &alice_private_key,
                                                       m_alice_raw_private_key,
                                                       sizeof(m_alice_raw_private_key));
    
        // Generate signature of the RLP Encoded Hash using ECDSA
        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);
    
    
        // Alice can now send the message and its signature to Bob
        print_hex("RLP Encoded hash: ", m_hash, sizeof(m_hash));
        print_hex("Signed Transaction: ", m_signature, m_signature_size);
    
        // Key deallocation
        err_code = nrf_crypto_ecc_private_key_free(&alice_private_key);
    
    
        // Write signature to flash storage
    
        uint32_t size = sizeof(m_ndef_msg_buf);
        err_code = ndef_file_signature_encode(m_ndef_msg_buf, &size);
        APP_ERROR_CHECK(err_code);
    
        err_code = ndef_file_update(m_ndef_msg_buf, NDEF_FILE_SIZE);
        APP_ERROR_CHECK(err_code);
        NRF_LOG_DEBUG("Signature written to flash storage");
    }

    In this case, the variable m_hash is the 32 byte array that I will feed to the generate_signature function.

Reply
  • Hi Martin, 

    Thanks. There are few more roadblocks that I'm facing. Let me explain the flow of my application first.

    I am using an android phone to write a 32 byte array to the nrf52 device which is running the Type 4 Tag (Read/Write) NFC example. Now, once the 32 byte array is written to the device, I need to use the same to create an ecdsa signature which is then written back to the FDS and loaded to the NDEF message buffer so that the android phone can read the signature from the device.

    Now, the problem that I am facing is, where should I pick the 32 byte array in the application to use it for further processing.

    I tried fetching the value from the ndef_message_buff variable, which stores the updated ndef message and generating the signature here, but I'm guessing this interrupts the write process to the fds and hence I receive an error.

    I also tried fetching the value from the FDS once the FDS Update Success event is called, and generating the signature from here, but this results in a recursive loop, since I'm performing an update to the fds right after generating the signature.

    For clarification, here is my generate_signature function.

    ret_code_t generate_signature(uint8_t m_hash[])
    {
        static nrf_crypto_ecc_private_key_t alice_private_key;
        ret_code_t                          err_code = NRF_SUCCESS;
    
        NRF_LOG_INFO("Transaction Signature Generation");
    
    
        err_code = nrf_crypto_ecc_private_key_from_raw(&g_nrf_crypto_ecc_secp256k1_curve_info,
                                                       &alice_private_key,
                                                       m_alice_raw_private_key,
                                                       sizeof(m_alice_raw_private_key));
    
        // Generate signature of the RLP Encoded Hash using ECDSA
        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);
    
    
        // Alice can now send the message and its signature to Bob
        print_hex("RLP Encoded hash: ", m_hash, sizeof(m_hash));
        print_hex("Signed Transaction: ", m_signature, m_signature_size);
    
        // Key deallocation
        err_code = nrf_crypto_ecc_private_key_free(&alice_private_key);
    
    
        // Write signature to flash storage
    
        uint32_t size = sizeof(m_ndef_msg_buf);
        err_code = ndef_file_signature_encode(m_ndef_msg_buf, &size);
        APP_ERROR_CHECK(err_code);
    
        err_code = ndef_file_update(m_ndef_msg_buf, NDEF_FILE_SIZE);
        APP_ERROR_CHECK(err_code);
        NRF_LOG_DEBUG("Signature written to flash storage");
    }

    In this case, the variable m_hash is the 32 byte array that I will feed to the generate_signature function.

Children
No Data
Related