{ "cells": [ { "cell_type": "markdown", "id": "9a5e57eb-9521-4b41-ad73-bab3f76c5eb6", "metadata": {}, "source": [ "# Key Exchange" ] }, { "cell_type": "markdown", "id": "5c685326-c1e6-42e0-ab75-c684e9290551", "metadata": {}, "source": [ "Using the key exchange API, two parties can securely compute a set of shared keys using their peer's public key and their own secret key." ] }, { "cell_type": "code", "execution_count": 1, "id": "53fc113f-c643-47bb-9881-09b6b3fe9026", "metadata": { "tags": [] }, "outputs": [], "source": [ "%load_ext sql" ] }, { "cell_type": "code", "execution_count": 2, "id": "dc2a4492-3637-4aa9-91fb-6f6d64e3b810", "metadata": { "tags": [] }, "outputs": [], "source": [ "%config SqlMagic.feedback=False\n", "%config SqlMagic.displaycon=False\n", "%sql postgresql://postgres@/" ] }, { "cell_type": "code", "execution_count": 3, "id": "e2e54e9b-836b-4e4e-a2f7-991d9d1014fc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%sql \n", "CREATE EXTENSION IF NOT EXISTS pgsodium;" ] }, { "cell_type": "markdown", "id": "43256a56-092a-4bd5-ae0c-4afaff394e77", "metadata": {}, "source": [ "### `crypto_kx_new_keypair()`" ] }, { "cell_type": "code", "execution_count": 4, "id": "6c0f972b-eabb-409c-8a49-e41b4ff25410", "metadata": {}, "outputs": [], "source": [ "bob = %sql select public, secret from pgsodium.crypto_kx_new_keypair()\n", "bob_public, bob_secret = bob[0][0].tobytes(), bob[0][1].tobytes()\n", "\n", "alice = %sql select public, secret from pgsodium.crypto_kx_new_keypair()\n", "alice_public, alice_secret = alice[0][0].tobytes(), alice[0][1].tobytes()" ] }, { "cell_type": "markdown", "id": "3b76dd1e-8e7b-4a33-95ac-3cdc339fa6f3", "metadata": {}, "source": [ "### `crypto_kx_client_session_keys(client_public bytea, client_secret bytea, server_public bytea)`" ] }, { "cell_type": "code", "execution_count": 5, "id": "4695674a-a704-424e-800a-143a0f5ce94b", "metadata": {}, "outputs": [], "source": [ "bob_keys = %sql select tx, rx from pgsodium.crypto_kx_client_session_keys(:bob_public, :bob_secret, :alice_public)" ] }, { "cell_type": "markdown", "id": "b7a87ef7-adc0-4207-81c4-fc3d0f11d47e", "metadata": {}, "source": [ "### `crypto_kx_server_session_keys(server_public bytea, server_secret bytea, client_public bytea)`" ] }, { "cell_type": "code", "execution_count": 6, "id": "559f3e93-36a0-4051-a772-af6fd0a688fb", "metadata": {}, "outputs": [], "source": [ "alice_keys = %sql select tx, rx from pgsodium.crypto_kx_server_session_keys(:alice_public, :alice_secret, :bob_public)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.2" } }, "nbformat": 4, "nbformat_minor": 5 }