Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Freeze after encrypt a lot of data on Raspberry Pi Pico #7

Open
Bob-YsPan opened this issue Nov 3, 2023 · 0 comments
Open

Freeze after encrypt a lot of data on Raspberry Pi Pico #7

Bob-YsPan opened this issue Nov 3, 2023 · 0 comments

Comments

@Bob-YsPan
Copy link

Bob-YsPan commented Nov 3, 2023

Describe the bug
I prepare a pair of Raspberry Pi Pico to do the RSA Encrypt and Decrypt between two boards.
And the program works, but it will freeze after encrypt a lot of data, I not very sure what the cause of the problem

To Reproduce
Steps to reproduce the behavior:

  1. Flash the reciver and transmitter program at two Raspberry Pi Pico Board
  2. Feed data to Transmitter board, and the Reciver recives the data and decrypt.
  3. After a lot of data sent, the transmitter board Freezes when run the encrypt command

Expected behavior

  • It can transmit and recive a lot of data without any freeze

Screenshots
The ENCS will print before encrypt command, and the program freezes after prints this text
image

Board Info:

  • Device: Raspberry Pi Pico W
  • Library Version: 6e87101(Latest)
  • IDE: VSCode + Platfrom IO

Additional context
The Loop section for the Transmitter at below, and the full program attached at the attatchment(But with some Chinese Comment).
Full program(Transmitter and Reciver in the same Source Code):
main.zip

void loop() {
  // Wait for a client to connect
  WiFiClient client = server.available();
  if (client) {
    // While the client is connected
    while (client.connected()) {
      // Check if the keys have been exchanged
      if (!keytrans) {
        // Initialize the pk object
        mbedtls_pk_init(&depk);
        // Send the public key
        int ret = transfer_sock(client, pkey_der, sizeof(pkey_der));
        if (ret != 0) {
          failblink();
        }
        // Receive the public key
        ret = receive_sock(client, r_raw_der);
        if (ret != 0) {
          // Parse the public key
          ret = mbedtls_pk_parse_public_key(&depk, r_raw_der, sizeof(r_raw_der));
        } else {
          failblink();
        }
        // Check the key format
        if (mbedtls_pk_get_type(&depk) == MBEDTLS_PK_RSA && ret == 0) {
          keytrans = true;
        } else {
          failblink();
        }
      } else {
        // Start the actual data transfer
        // The buffer for the encrypted data
        uint8_t enc_data[128] = {};
        // Empty the buffer
        // std::fill_n(enc_data, sizeof(enc_data), 0);
        // Receive the encrypted data
        ret = receive_sock(client, enc_data);
        if (ret == 0) {
          failblink();
        }
        // The buffer for the signature
        uint8_t sig_data[128] = {};
        // Empty the buffer
        // std::fill_n(sig_data, sizeof(sig_data), 0);
        // Receive the signature
        ret = receive_sock(client, sig_data);
        if (ret == 0) {
          failblink();
        }
        // Decrypt the data
        size_t olen;
        unsigned char decrypted[85] = {0};
        // Empty the buffer
        // std::fill_n(decrypted, sizeof(decrypted), 0);
        ret = mbedtls_rsa_pkcs1_decrypt(
            mbedtls_pk_rsa(keys),
            mbedtls_ctr_drbg_random,
            &drbg,
            MBEDTLS_RSA_PRIVATE,
            &olen,
            enc_data,
            decrypted,
            sizeof(decrypted));
        if (ret != 0) {
          failblink();
        }
        // Get the hash of the received message
        uint8_t rhash[32] = {0};
        // Empty the buffer
        // std::fill_n(rhash, sizeof(rhash), 0);
        mbedtls_sha256(decrypted, olen, rhash, 0);
        // Verify the signature
        ret = mbedtls_rsa_pkcs1_verify(
            mbedtls_pk_rsa(depk),
            mbedtls_ctr_drbg_random,
            &drbg,
            MBEDTLS_RSA_PUBLIC,
            MBEDTLS_MD_SHA256,
            sizeof(rhash),
            rhash,
            sig_data);
        if (ret == 0) {
          // Write the decrypted data, using the correct receive length
          Serial.write(decrypted, olen);
        }
      }
    }
    failblink();
  } else {
    // Wait and blink
    delay(100);
    cyw43_arch_gpio_put(0, 1);
    delay(200);
    cyw43_arch_gpio_put(0, 0);
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant