| 70 | } |
| 71 | |
| 72 | bool FastPairCrypto::ecdhComputeSharedSecret( |
| 73 | const uint8_t *private_key, const uint8_t *peer_public_key, uint8_t *shared_secret |
| 74 | ) { |
| 75 | mbedtls_mpi priv; |
| 76 | mbedtls_ecp_point peer_pub; |
| 77 | |
| 78 | mbedtls_mpi_init(&priv); |
| 79 | mbedtls_ecp_point_init(&peer_pub); |
| 80 | |
| 81 | mbedtls_mpi_read_binary(&priv, private_key, 32); |
| 82 | |
| 83 | int ret = mbedtls_ecp_point_read_binary(&grp, &peer_pub, peer_public_key, 65); |
| 84 | if (ret != 0) { |
| 85 | mbedtls_mpi_free(&priv); |
| 86 | mbedtls_ecp_point_free(&peer_pub); |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | mbedtls_mpi z; |
| 91 | mbedtls_mpi_init(&z); |
| 92 | |
| 93 | ret = mbedtls_ecdh_compute_shared(&grp, &z, &peer_pub, &priv, mbedtls_ctr_drbg_random, &ctr_drbg); |
| 94 | |
| 95 | if (ret == 0) { mbedtls_mpi_write_binary(&z, shared_secret, 32); } |
| 96 | |
| 97 | mbedtls_mpi_free(&z); |
| 98 | mbedtls_mpi_free(&priv); |
| 99 | mbedtls_ecp_point_free(&peer_pub); |
| 100 | |
| 101 | return (ret == 0); |
| 102 | } |
| 103 | |
| 104 | void FastPairCrypto::generatePlausibleSharedSecret(const uint8_t *their_pubkey, uint8_t *output) { |
| 105 | if (looksLikeValidPublicKey(their_pubkey, 65)) { |