From 51cd83f6744ca1546fe6e64187150c60d0acd717 Mon Sep 17 00:00:00 2001 From: BONNAURE Olivier Date: Sun, 21 Jan 2024 15:58:48 +0100 Subject: [PATCH] Add Curve25519 method in help.txt + add new specs file (#1096) --- test/tool/net/curve25519_test.lua | 32 +++++++++++++++++++++++++++++++ tool/net/help.txt | 16 ++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 test/tool/net/curve25519_test.lua diff --git a/test/tool/net/curve25519_test.lua b/test/tool/net/curve25519_test.lua new file mode 100644 index 000000000..8573bb913 --- /dev/null +++ b/test/tool/net/curve25519_test.lua @@ -0,0 +1,32 @@ +-- Copyright 2022 Justine Alexandra Roberts Tunney +-- +-- Permission to use, copy, modify, and/or distribute this software for +-- any purpose with or without fee is hereby granted, provided that the +-- above copyright notice and this permission notice appear in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL +-- DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR +-- PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER +-- TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +-- PERFORMANCE OF THIS SOFTWARE. + +local secret1 = GetRandomBytes(32) +local public1 = Curve25519(secret1, "\9") +local secret2 = GetRandomBytes(32) +local public2 = Curve25519(secret2, "\9") +local secret3 = GetRandomBytes(32) +local public3 = Curve25519(secret3, "\9") + +local shared_key1 = Curve25519(secret1, public2) +local shared_key2 = Curve25519(secret2, public1) + +local shared_key3 = Curve25519(secret2, public3) +local shared_key4 = Curve25519(secret3, public2) + +assert(shared_key1 == shared_key2) +assert(shared_key1 ~= shared_key3) +assert(shared_key2 ~= shared_key3) +assert(shared_key4 == shared_key3) diff --git a/tool/net/help.txt b/tool/net/help.txt index 595475a53..e238634f9 100644 --- a/tool/net/help.txt +++ b/tool/net/help.txt @@ -1137,6 +1137,22 @@ FUNCTIONS The name can be one of the following strings: MD5, SHA1, SHA224, SHA256, SHA384, SHA512, and BLAKE2B256. + Curve25519(secret:str, public_or_basepoint:str) -> str + Returns the shared key for the specified secret and public values. + The second argument can be either a public key or a basepoint, used + for generating a shared or public key. + + Here a small usage sample : + + >: secret1 = GetRandomBytes(32) + >: public1 = Curve25519(secret1, "\9") + >: secret2 = GetRandomBytes(32) + >: public2 = Curve25519(secret2, "\9") + >: shared_key1 = Curve25519(secret1, public2) + >: shared_key2 = Curve25519(secret2, public1) + >: shared_key1 == shared_key2 + true + GetRemoteAddr() → ip:uint32,port:uint16 Returns client ip4 address and port, e.g. 0x01020304,31337 would represent 1.2.3.4:31337. This is the same as GetClientAddr except