* Perform the actual key exchange. * @param peer_public_key The public key chosen by the other participant of the key exchange. * @param side Whether we are the client or server; used to hash the public key of us and the peer in the right order. * @param our_secret_key The secret key of us. * @param our_public_key The public key of us. * @param extra_payload Extra payload to put into the hash
| 62 | * @return True when the key exchange has succeeded, false when an illegal public key was given. |
| 63 | */ |
| 64 | bool X25519DerivedKeys::Exchange(const X25519PublicKey &peer_public_key, X25519KeyExchangeSide side, |
| 65 | const X25519SecretKey &our_secret_key, const X25519PublicKey &our_public_key, std::string_view extra_payload) |
| 66 | { |
| 67 | X25519Key shared_secret; |
| 68 | crypto_x25519(shared_secret.data(), our_secret_key.data(), peer_public_key.data()); |
| 69 | if (std::all_of(shared_secret.begin(), shared_secret.end(), [](auto v) { return v == 0; })) { |
| 70 | /* A shared secret of all zeros means that the peer tried to force the shared secret to a known constant. */ |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | crypto_blake2b_ctx ctx; |
| 75 | crypto_blake2b_init(&ctx, this->keys.size()); |
| 76 | crypto_blake2b_update(&ctx, shared_secret.data(), shared_secret.size()); |
| 77 | switch (side) { |
| 78 | case X25519KeyExchangeSide::SERVER: |
| 79 | crypto_blake2b_update(&ctx, our_public_key.data(), our_public_key.size()); |
| 80 | crypto_blake2b_update(&ctx, peer_public_key.data(), peer_public_key.size()); |
| 81 | break; |
| 82 | case X25519KeyExchangeSide::CLIENT: |
| 83 | crypto_blake2b_update(&ctx, peer_public_key.data(), peer_public_key.size()); |
| 84 | crypto_blake2b_update(&ctx, our_public_key.data(), our_public_key.size()); |
| 85 | break; |
| 86 | default: |
| 87 | NOT_REACHED(); |
| 88 | } |
| 89 | crypto_blake2b_update(&ctx, reinterpret_cast<const uint8_t *>(extra_payload.data()), extra_payload.size()); |
| 90 | crypto_blake2b_final(&ctx, this->keys.data()); |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Encryption handler implementation for monocypher encryption after a X25519 key exchange. |
no test coverage detected