wallet-core/src/crypto/talerCrypto-test.ts

213 lines
7.7 KiB
TypeScript
Raw Normal View History

/*
This file is part of GNU Taler
(C) 2019 GNUnet e.V.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
* Imports
*/
import test from "ava";
import {
encodeCrock,
decodeCrock,
ecdheGetPublic,
eddsaGetPublic,
keyExchangeEddsaEcdhe,
keyExchangeEcdheEddsa,
rsaBlind,
rsaUnblind,
rsaVerify,
} from "./talerCrypto";
import { sha512, kdf } from "./primitives/kdf";
import nacl = require("./primitives/nacl-fast");
function hexToBytes(hex: string) {
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
}
function bytesToHex(bytes: Uint8Array): string {
for (var hex = [], i = 0; i < bytes.length; i++) {
var current = bytes[i] < 0 ? bytes[i] + 256 : bytes[i];
hex.push((current >>> 4).toString(16));
hex.push((current & 0xf).toString(16));
}
return hex.join("");
}
2020-03-30 12:39:32 +02:00
test("encoding", (t) => {
const utf8decoder = new TextDecoder("utf-8");
const utf8encoder = new TextEncoder();
const s = "Hello, World";
const encStr = encodeCrock(utf8encoder.encode(s));
const outBuf = decodeCrock(encStr);
const sOut = utf8decoder.decode(outBuf);
t.deepEqual(s, sOut);
});
2020-03-30 12:39:32 +02:00
test("taler-exchange-tvg hash code", (t) => {
const input = "91JPRV3F5GG4EKJN41A62V35E8";
const output =
"CW96WR74JS8T53EC8GKSGD49QKH4ZNFTZXDAWMMV5GJ1E4BM6B8GPN5NVHDJ8ZVXNCW7Q4WBYCV61HCA3PZC2YJD850DT29RHHN7ESR";
const myOutput = encodeCrock(sha512(decodeCrock(input)));
t.deepEqual(myOutput, output);
});
2020-03-30 12:39:32 +02:00
test("taler-exchange-tvg ecdhe key", (t) => {
const priv1 = "X4T4N0M8PVQXQEBW2BA7049KFSM7J437NSDFC6GDNM3N5J9367A0";
const pub1 = "M997P494MS6A95G1P0QYWW2VNPSHSX5Q6JBY5B9YMNYWP0B50X3G";
const priv2 = "14A0MMQ64DCV8HE0CS3WBC9DHFJAHXRGV7NEARFJPC5R5E1697E0";
const skm =
"NXRY2YCY7H9B6KM928ZD55WG964G59YR0CPX041DYXKBZZ85SAWNPQ8B30QRM5FMHYCXJAN0EAADJYWEF1X3PAC2AJN28626TR5A6AR";
const myPub1 = nacl.scalarMult_base(decodeCrock(priv1));
t.deepEqual(encodeCrock(myPub1), pub1);
const mySkm = nacl.hash(
nacl.scalarMult(decodeCrock(priv2), decodeCrock(pub1)),
);
t.deepEqual(encodeCrock(mySkm), skm);
});
2020-03-30 12:39:32 +02:00
test("taler-exchange-tvg eddsa key", (t) => {
const priv = "9TM70AKDTS57AWY9JK2J4TMBTMW6K62WHHGZWYDG0VM5ABPZKD40";
const pub = "8GSJZ649T2PXMKZC01Y4ANNBE7MF14QVK9SQEC4E46ZHKCVG8AS0";
const pair = nacl.sign_keyPair_fromSeed(decodeCrock(priv));
t.deepEqual(encodeCrock(pair.publicKey), pub);
});
2020-03-30 12:39:32 +02:00
test("taler-exchange-tvg kdf", (t) => {
const salt = "94KPT83PCNS7J83KC5P78Y8";
const ikm = "94KPT83MD1JJ0WV5CDS6AX10D5Q70XBM41NPAY90DNGQ8SBJD5GPR";
const ctx =
"94KPT83141HPYVKMCNW78833D1TPWTSC41GPRWVF41NPWVVQDRG62WS04XMPWSKF4WG6JVH0EHM6A82J8S1G";
const outLen = 64;
const out =
"GTMR4QT05Z9WF5HKVG0WK9RPXGHSMHJNW377G9GJXCA8B0FEKPF4D27RJMSJZYWSQNTBJ5EYVV7ZW18B48Z0JVJJ80RHB706Y96Q358";
const myOut = kdf(
outLen,
decodeCrock(ikm),
decodeCrock(salt),
decodeCrock(ctx),
);
t.deepEqual(encodeCrock(myOut), out);
});
2020-03-30 12:39:32 +02:00
test("taler-exchange-tvg eddsa_ecdh", (t) => {
const priv_ecdhe = "4AFZWMSGTVCHZPQ0R81NWXDCK4N58G7SDBBE5KXE080Y50370JJG";
const pub_ecdhe = "FXFN5GPAFTKVPWJDPVXQ87167S8T82T5ZV8CDYC0NH2AE14X0M30";
const priv_eddsa = "1KG54M8T3X8BSFSZXCR3SQBSR7Y9P53NX61M864S7TEVMJ2XVPF0";
const pub_eddsa = "7BXWKG6N224C57RTDV8XEAHR108HG78NMA995BE8QAT5GC1S7E80";
const key_material =
"PKZ42Z56SVK2796HG1QYBRJ6ZQM2T9QGA3JA4AAZ8G7CWK9FPX175Q9JE5P0ZAX3HWWPHAQV4DPCK10R9X3SAXHRV0WF06BHEC2ZTKR";
const myEcdhePub = ecdheGetPublic(decodeCrock(priv_ecdhe));
t.deepEqual(encodeCrock(myEcdhePub), pub_ecdhe);
const myEddsaPub = eddsaGetPublic(decodeCrock(priv_eddsa));
t.deepEqual(encodeCrock(myEddsaPub), pub_eddsa);
const myKm1 = keyExchangeEddsaEcdhe(
decodeCrock(priv_eddsa),
decodeCrock(pub_ecdhe),
);
t.deepEqual(encodeCrock(myKm1), key_material);
const myKm2 = keyExchangeEcdheEddsa(
decodeCrock(priv_ecdhe),
decodeCrock(pub_eddsa),
);
t.deepEqual(encodeCrock(myKm2), key_material);
});
2020-03-30 12:39:32 +02:00
test("taler-exchange-tvg blind signing", (t) => {
const messageHash =
"TT1R28D79EJEJ9PC35AQS35CCG85DSXSZ508MV2HS2FN4ME6AHESZX5WP485R8A75KG53FN6F1YNW95008663TKAPWB81420VG17BY8";
const rsaPublicKey =
"040000Y62RSDDKZXTE7GDVA302ZZR0DY224RSDT6WDWR1XGT8E3YG80XV6TMT3ZCNP8XC84W0N6MSZ0EF8S3YB1JJ2AXY9JQZW3MCA0CG38ER4YE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RNGF7K49CR4ZBFRXDAWMB3X6T05Q4NKSG0F1KP5JA0XBMF2YJK7KEPRD1EWCHJE44T9YXBTK4W9CV77X7Z9P407ZC6YB3M2ARANZXHJKSM3XC33M";
const sig =
"PFT6WQJGCM9DE6264DJS6RMG4XDMCDBJKZGSXAF3BEXWZ979Q13NETKK05S1YV91CX3Y034FSS86SSHZTTE8097RRESQP52EKFGTWJXKHZJEQJ49YHMBNQDHW4CFBJECNJSV2PMHWVGXV7HB84R6P0S3ES559HWQX01Q9MYDEGRNHKW87QR2BNSG951D5NQGAKEJ2SSJBE18S6WYAC24FAP8TT8ANECH5371J0DJY0YR0VWAFWVJDV8XQSFXWMJ80N3A80SPSHPYJY3WZZXW63WQ46WHYY56ZSNE5G1RZ5CR0XYV2ECKPM8R0FS58EV16WTRAM1ABBFVNAT3CAEFAZCWP3XHPVBQY5NZVTD5QS2Q8SKJQ2XB30E11CWDN9KTV5CBK4DN72EVG73F3W3BATAKHG";
2020-03-30 12:39:32 +02:00
const myBm = rsaBlind(
decodeCrock(messageHash),
decodeCrock(bks),
decodeCrock(rsaPublicKey),
);
t.deepEqual(encodeCrock(myBm), bm);
2020-03-30 12:39:32 +02:00
const mySig = rsaUnblind(
decodeCrock(bs),
decodeCrock(rsaPublicKey),
decodeCrock(bks),
);
t.deepEqual(encodeCrock(mySig), sig);
2020-03-30 12:39:32 +02:00
const v = rsaVerify(
decodeCrock(messageHash),
decodeCrock(sig),
decodeCrock(rsaPublicKey),
);
t.true(v);
});
test("incremental hashing #1", (t) => {
const n = 1024;
const d = nacl.randomBytes(n);
const h1 = nacl.hash(d);
const h2 = new nacl.HashState().update(d).finish();
const s = new nacl.HashState();
for (let i = 0; i < n; i++) {
const b = new Uint8Array(1);
b[0] = d[i];
s.update(b);
}
const h3 = s.finish();
t.deepEqual(encodeCrock(h1), encodeCrock(h2));
t.deepEqual(encodeCrock(h1), encodeCrock(h3));
});
test("incremental hashing #2", (t) => {
const n = 10;
const d = nacl.randomBytes(n);
const h1 = nacl.hash(d);
const h2 = new nacl.HashState().update(d).finish();
const s = new nacl.HashState();
for (let i = 0; i < n; i++) {
const b = new Uint8Array(1);
b[0] = d[i];
s.update(b);
}
const h3 = s.finish();
t.deepEqual(encodeCrock(h1), encodeCrock(h3));
t.deepEqual(encodeCrock(h1), encodeCrock(h2));
2020-03-30 12:39:32 +02:00
});