xref: /PHP-7.2/ext/sodium/tests/crypto_sign.phpt (revision 9d236d63)
1--TEST--
2Check for libsodium ed25519 signatures
3--SKIPIF--
4<?php if (!extension_loaded("sodium")) print "skip"; ?>
5--FILE--
6<?php
7$keypair = sodium_crypto_sign_keypair();
8var_dump(strlen($keypair) === SODIUM_CRYPTO_SIGN_KEYPAIRBYTES);
9$sk = sodium_crypto_sign_secretkey($keypair);
10var_dump(strlen($sk) === SODIUM_CRYPTO_SIGN_SECRETKEYBYTES);
11$pk = sodium_crypto_sign_publickey($keypair);
12var_dump(strlen($pk) === SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES);
13var_dump($pk !== $sk);
14$keypair2 = sodium_crypto_sign_keypair_from_secretkey_and_publickey($sk, $pk);
15var_dump($keypair === $keypair2);
16
17$alice_kp = sodium_crypto_sign_keypair();
18$alice_secretkey = sodium_crypto_sign_secretkey($alice_kp);
19$alice_publickey = sodium_crypto_sign_publickey($alice_kp);
20
21$msg = "Here is the message, to be signed using Alice's secret key, and " .
22  "to be verified using Alice's public key";
23
24$msg_signed = sodium_crypto_sign($msg, $alice_secretkey);
25var_dump(strlen($msg_signed) - strlen($msg) === SODIUM_CRYPTO_SIGN_BYTES);
26
27$msg_orig = sodium_crypto_sign_open($msg_signed, $alice_publickey);
28var_dump($msg_orig === $msg);
29
30$seed = str_repeat('x', SODIUM_CRYPTO_SIGN_SEEDBYTES);
31$alice_kp = sodium_crypto_sign_seed_keypair($seed);
32
33$alice_secretkey = sodium_crypto_sign_secretkey($alice_kp);
34$alice_publickey = sodium_crypto_sign_publickey($alice_kp);
35
36$msg = "Here is another message, to be signed using Alice's secret key, and " .
37  "to be verified using Alice's public key, which will be always the same " .
38  "since they are derived from a fixed seed";
39
40$msg_signed = sodium_crypto_sign($msg, $alice_secretkey);
41var_dump(strlen($msg_signed) - strlen($msg) === SODIUM_CRYPTO_SIGN_BYTES);
42
43$msg_orig = sodium_crypto_sign_open($msg_signed, $alice_publickey);
44var_dump($msg_orig === $msg);
45
46$signature = sodium_crypto_sign_detached($msg, $alice_secretkey);
47var_dump(strlen($signature) === SODIUM_CRYPTO_SIGN_BYTES);
48var_dump(sodium_crypto_sign_verify_detached($signature,
49                                             $msg, $alice_publickey));
50var_dump(sodium_crypto_sign_verify_detached($signature,
51                                             $msg . "\0", $alice_publickey));
52
53$calc_pubkey = sodium_crypto_sign_publickey_from_secretkey($alice_secretkey);
54var_dump(sodium_memcmp($calc_pubkey, $alice_publickey) === 0);
55
56$ed25519key = sodium_hex2bin("55b62f664bf1c359f58a6b91b89556f97284273510573055b9237d17f5a20564607f0626f49e63c2c8f814ed6d955bf8b005b33fd5fd56eaca93073d8eb99165");
57$curve25519key = sodium_crypto_sign_ed25519_sk_to_curve25519($ed25519key);
58var_dump($curve25519key === sodium_hex2bin("381b2be5e3d38820deb1243fb58b4be654da30dd3ccde492cb88f937eb489363"));
59
60try {
61    sodium_crypto_sign($msg, substr($alice_secretkey, 1));
62} catch (SodiumException $ex) {
63    var_dump(true);
64}
65?>
66--EXPECT--
67bool(true)
68bool(true)
69bool(true)
70bool(true)
71bool(true)
72bool(true)
73bool(true)
74bool(true)
75bool(true)
76bool(true)
77bool(true)
78bool(false)
79bool(true)
80bool(true)
81bool(true)
82