clean up tos

This commit is contained in:
swrup 2026-02-12 15:33:58 +01:00
parent 9040094717
commit 84e48f85e4
167 changed files with 75 additions and 18547 deletions

1
test/auditor_public_key Normal file
View file

@ -0,0 +1 @@
A17JXR3E6J4CXDPYT7S1H25PGJ3ABS26TQ38654QCX0TB59RPMF0

75
test/crypto.ml Normal file
View file

@ -0,0 +1,75 @@
let encode = B32.encode
let decode s = B32.decode s |> Result.get_ok
let round_trip s =
let s' = s |> encode |> decode in
match s' = s with
| false -> Fmt.failwith "b32 round trip failure: @\nin :`%S`@\nout:`%S`" s' s
| true -> ()
let () =
let s = String.init 0xff Char.chr in
round_trip s;
let s_l = List.init 0x0f (fun i -> String.init i Char.chr) in
List.iter round_trip s_l; ()
(* test vectors from:
https://git.gnunet.org/gnunet/gnunet/file/src/cli/util/crypto-test-vectors.json.html *)
let () =
(* hash *)
let input = "91JPRV3F5GG4EKJNDSJQ8" in
let expected =
"D0R24RZ1TPASVQ2NY56CT8AJDYZE9ZGDB0GVZ05E9D4YGZQW2RC5YFPQ0Q86EPW836DY7VYQTNFFJT3ZR2K508F4JVS5JNJKYN2MMFR"
in
let output =
input |> decode |> Hash.H64.hash |> Hash.H64.to_octets |> encode
in
assert (output = expected);
()
let () =
(* eddsa_key_derivation *)
let pub = "3M9KK1WSNM1RTY5P72HKFA264V4B7MVHVJ08Y90CV06DYHV8XPP0" in
let priv = "8QC2VNF8443S5KPNKMB4XMV58BTHWAKZ7SVW5WG3KRB37567XS90" in
let pub' =
let open Crypto in
priv
|> decode
|> EddsaPrivateKey.of_octets
|> Result.get_ok
|> EddsaPrivateKey.pub_of_priv
|> EddsaPublicKey.to_octets
|> encode
in
assert (pub = pub');
()
let () =
(* eddsa_signing *)
let priv = "5077XJR9AMH4T97ACKFBVBJD0KFENHPV66B2Y1JBSKXBJKNZJ4E0" in
let pub = "6E2F03JJ8AEDANTTZZ4SBZDFEEZSF8A9DVGTS6VFBCVZQYQ46RRG" in
let data = "00000300000000000000" in
let sig_ =
"XCNJGJ96WPDH60YVMH6C74NGQSGJE3BC1TYMGX6BHY5DMZZZKTB373QTXJ507K5EBSG9YS2EYKHCX3ATRQ6P5MY9MXC4ZB1XSZ2X23G"
in
let open Crypto in
let msg = decode data in
let eddsa_priv =
priv |> decode |> EddsaPrivateKey.of_octets |> Result.get_ok
in
let sig_' =
EddsaSignature.sign ~key:eddsa_priv msg
|> EddsaSignature.to_octets
|> encode
in
assert (sig_ = sig_');
let eddsa_pub = pub |> decode |> EddsaPublicKey.of_octets |> Result.get_ok in
let eddsa_sig = sig_ |> decode |> EddsaSignature.of_octets |> Result.get_ok in
let () =
EddsaSignature.verify ~key:eddsa_pub eddsa_sig ~msg |> Result.get_ok
in
()

16
test/dune Normal file
View file

@ -0,0 +1,16 @@
(test
(name test)
(modules test)
(libraries mte fmt))
(test
(name crypto)
(modules crypto)
(libraries mte fmt))
; TODO
; cram test?
; gcc -o a.out ./test/signatures.c && ./a.out > a.output
; rm ./a.out
; dune exec ./test/test.exe > b.output
; cmp -l a.output b.output

58
test/offline_management.sh Executable file
View file

@ -0,0 +1,58 @@
#!/bin/bash
set -e
a="/tmp/a.json"
b="/tmp/b.json"
url="http://localhost:3434"
auditor_pub=$(<"./test/auditor_public_key")
master_key="./default/master_offline_private_key"
zero_euro="EUR:0.0"
offline_tool() {
dune exec offline -- "$@" > /dev/null
}
offline_tool download --output $a --url $url"/management/keys"
offline_tool sign \
--master_key $master_key \
--input $a \
--output $b
offline_tool upload --input $b --url $url"/management/keys"
echo "[OK] /management/keys"
offline_tool enable-auditor \
--master_key $master_key \
--output $b \
--auditor_url "auditor.example.com" \
--auditor_name "auditor example" \
--auditor_pub $auditor_pub \
--validity_start 0
offline_tool upload --input $b --url $url"/management/auditors"
echo "[OK] /management/auditors"
offline_tool wire-fee \
--master_key $master_key \
--output $b \
--wire_method "xxx" \
--fee_start 0 \
--fee_end 99999999 \
--closing_fee $zero_euro \
--wire_fee $zero_euro
offline_tool upload --input $b --url $url"/management/wire-fee"
echo "[OK] /management/wire-fee"
offline_tool global-fees \
--master_key $master_key \
--output $b \
--start_date 0 \
--end_date 99999999 \
--history_fee $zero_euro \
--account_fee $zero_euro \
--purse_fee $zero_euro \
--history_expiration 9999999 \
--purse_account_limit 1 \
--purse_timeout 9999999
offline_tool upload --input $b --url $url"/management/global-fees"
echo "[OK] /management/global-fees"

55
test/signatures.c Normal file
View file

@ -0,0 +1,55 @@
#include <stdio.h>
#include <stdint.h>
struct H64 {
uint8_t hash[64];
};
struct Hhh {
struct H64 hash;
};
struct Purpose {
uint32_t size;
uint32_t purpose;
};
struct PS {
struct Purpose purpose;
struct Hhh h;
uint32_t noreveal_index;
};
int main(void) {
struct Purpose purpose;
struct PS ps;
purpose.size = 3 * 4 + 64;
purpose.purpose = 1050;
struct Hhh h = {
.hash = {
.hash = {
1 , 2 , 3 , 4 , 5, 6, 7, 8,
9 , 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48,
49, 50, 51, 52, 53, 54, 55, 56,
57, 58, 59, 60, 61, 62, 63, 64
}
}
};
uint32_t noreveal_index = 0;
ps.purpose = purpose;
ps.h = h;
ps.noreveal_index = noreveal_index;
fwrite(&ps, sizeof(ps), 1, stdout);
// printf("\n");
return 0;
}

115
test/test.ml Normal file
View file

@ -0,0 +1,115 @@
let () = Mirage_crypto_rng_unix.use_default ()
let () =
let priv = Mirage_crypto_pk.Rsa.generate ~bits:2048 () in
let pub = Mirage_crypto_pk.Rsa.pub_of_priv priv in
let () =
let open Crypto.RsaPrivateKey in
let priv' = priv |> to_octets |> of_octets |> Result.get_ok in
assert (to_octets priv = to_octets priv')
in
let () =
let open Crypto.RsaPublicKey in
let pub' = pub |> to_octets |> of_octets |> Result.get_ok in
assert (to_octets pub = to_octets pub')
in
()
let () =
let open Api in
let check jsont s =
let encode v = encode jsont v |> Result.get_ok in
let decode v = decode jsont v |> Result.get_ok in
let ts = decode s in
let s' = encode ts in
let ts' = decode s' in
let s'' = encode ts' in
assert (String.equal s' s'')
in
let check_bad jsont s =
let decode = decode jsont in
assert (Result.is_error (decode s))
in
check Timestamp.jsont {|{"t_s": 123456780}|};
check Timestamp.jsont {|{"t_s": "never"}|};
check_bad Timestamp.jsont {|{"t_s": "123456780"}|};
check_bad Timestamp.jsont {|{"t_s": "agagou"}|};
(* CS not implemented *)
check_bad DenominationKey.jsont
{|{"cipher": "CS", "age_mask": 18, "cs_pub": "ouhagag"}|};
(* TODO test with a valid rsa_pub value *)
(* TODO handle "rsa pub_of_octets failure" correctly *)
(* check_bad DenominationKey.jsont
{|{"cipher": "RSA", "age_mask": 18, "rsa_pub": "agagouh"}|}; *)
()
let () =
let open Amount in
let v =
make ~sign:(Some Sign_plus) ~currency:"EUR" ~value:(Int64.of_int 25)
~fraction:(Int32.of_int 678)
|> Result.get_ok
in
let s = to_string v in
let v' = of_string s |> Result.get_ok in
assert (v = v');
let r = of_string {|~EUR:4.9|} in
assert (Result.is_error r);
let r = of_string {|+EUR:4.99999999999999999999999|} in
assert (Result.is_error r);
()
let () =
let open Headers_lib.If_none_match in
let ok_l =
[
"*";
"\"foo\"";
"W/\"foo\"";
"\"foo\", \"bar\"";
" , W/\"x\" , W/\"y\" , \"z\"";
", \"one\" , \"two\" , \"three\"";
"W/\"a\" , ";
"W/\"a\" , \"b\"";
]
in
let bad_l =
[
"";
"foo";
"W/foo";
"W/\"unterminated";
"\"foo\", W/";
"* , \"bar\"";
"\"a\" \"b\"";
"W/\"a\" W/\"b\"";
"\"fo\x7Fo\"";
" W/\"a\"";
"W/\"a\" ";
]
in
let check_ok input = assert (Result.is_ok (parse input)) in
let check_bad input = assert (Result.is_error (parse input)) in
List.iter check_ok ok_l;
List.iter check_bad bad_l;
()
(*
let () =
let open Binary_formats.WithdrawConfirmationPS in
let str64 = String.init 64 (fun i -> Char.unsafe_chr (i + 1)) in
let dummy_t = { h_planchets= { hash= str64 }; noreveal_index= 0_l } in
let size = Bin.size_of_value bin dummy_t |> Option.get in
assert (size = 76);
(* for cmp test with signatures.c output *)
(*
let raw_str = Bin.to_string bin dummy_t in
Printf.printf "%s" raw_str;
*)
()
*)