xref: /PHP-7.2/ext/sodium/tests/crypto_box.phpt (revision 9d236d63)
1--TEST--
2Check for libsodium box
3--SKIPIF--
4<?php if (!extension_loaded("sodium")) print "skip"; ?>
5--FILE--
6<?php
7$keypair = sodium_crypto_box_keypair();
8var_dump(strlen($keypair) === SODIUM_CRYPTO_BOX_KEYPAIRBYTES);
9$sk = sodium_crypto_box_secretkey($keypair);
10var_dump(strlen($sk) === SODIUM_CRYPTO_BOX_SECRETKEYBYTES);
11$pk = sodium_crypto_box_publickey($keypair);
12var_dump(strlen($pk) === SODIUM_CRYPTO_BOX_PUBLICKEYBYTES);
13var_dump($pk !== $sk);
14$pk2 = sodium_crypto_box_publickey_from_secretkey($sk);
15var_dump($pk === $pk2);
16$pk2 = sodium_crypto_scalarmult_base($sk);
17var_dump($pk === $pk2);
18$keypair2 = sodium_crypto_box_keypair_from_secretkey_and_publickey($sk, $pk);
19var_dump($keypair === $keypair2);
20
21$seed_x = str_repeat('x', SODIUM_CRYPTO_BOX_SEEDBYTES);
22$seed_y = str_repeat('y', SODIUM_CRYPTO_BOX_SEEDBYTES);
23$alice_box_kp = sodium_crypto_box_seed_keypair($seed_x);
24$bob_box_kp = sodium_crypto_box_seed_keypair($seed_y);
25$message_nonce = random_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES);
26
27$alice_box_secretkey = sodium_crypto_box_secretkey($alice_box_kp);
28$bob_box_publickey = sodium_crypto_box_publickey($bob_box_kp);
29
30$alice_to_bob_kp = sodium_crypto_box_keypair_from_secretkey_and_publickey(
31    $alice_box_secretkey,
32    $bob_box_publickey
33);
34
35$msg = "Here is another message, to be signed using Alice's secret key, and " .
36  "to be encrypted using Bob's public key. The keys will always be the same " .
37  "since they are derived from a fixed seeds";
38
39$ciphertext = sodium_crypto_box(
40    $msg,
41    $message_nonce,
42    $alice_to_bob_kp
43);
44
45try {
46  $ciphertext = sodium_crypto_box(
47      $msg,
48      $message_nonce,
49      substr($alice_to_bob_kp, 1)
50  );
51} catch (SodiumException $ex) {
52    echo $ex->getMessage(), PHP_EOL;
53}
54
55sodium_memzero($alice_box_kp);
56sodium_memzero($bob_box_kp);
57
58$alice_box_kp = sodium_crypto_box_seed_keypair($seed_x);
59$bob_box_kp = sodium_crypto_box_seed_keypair($seed_y);
60
61$alice_box_publickey = sodium_crypto_box_publickey($alice_box_kp);
62$bob_box_secretkey = sodium_crypto_box_secretkey($bob_box_kp);
63
64$bob_to_alice_kp = sodium_crypto_box_keypair_from_secretkey_and_publickey(
65    $bob_box_secretkey,
66    $alice_box_publickey
67);
68
69$plaintext = sodium_crypto_box_open(
70    $ciphertext,
71    $message_nonce,
72    $bob_to_alice_kp
73);
74
75var_dump($msg === $plaintext);
76
77$alice_kp = sodium_crypto_box_keypair();
78$alice_secretkey = sodium_crypto_box_secretkey($alice_kp);
79$alice_publickey = sodium_crypto_box_publickey($alice_kp);
80
81$bob_kp = sodium_crypto_box_keypair();
82$bob_secretkey = sodium_crypto_box_secretkey($bob_kp);
83$bob_publickey = sodium_crypto_box_publickey($bob_kp);
84
85$alice_to_bob_kp = sodium_crypto_box_keypair_from_secretkey_and_publickey
86  ($alice_secretkey, $bob_publickey);
87
88$bob_to_alice_kp = sodium_crypto_box_keypair_from_secretkey_and_publickey
89  ($bob_secretkey, $alice_publickey);
90
91$alice_to_bob_message_nonce = random_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES);
92
93$alice_to_bob_ciphertext = sodium_crypto_box('Hi, this is Alice',
94                                              $alice_to_bob_message_nonce,
95                                              $alice_to_bob_kp);
96
97$alice_message_decrypted_by_bob = sodium_crypto_box_open($alice_to_bob_ciphertext,
98                                                          $alice_to_bob_message_nonce,
99                                                          $bob_to_alice_kp);
100
101$bob_to_alice_message_nonce = random_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES);
102
103$bob_to_alice_ciphertext = sodium_crypto_box('Hi Alice! This is Bob',
104                                              $bob_to_alice_message_nonce,
105                                              $bob_to_alice_kp);
106
107$bob_message_decrypted_by_alice = sodium_crypto_box_open($bob_to_alice_ciphertext,
108                                                          $bob_to_alice_message_nonce,
109                                                          $alice_to_bob_kp);
110
111var_dump($alice_message_decrypted_by_bob);
112var_dump($bob_message_decrypted_by_alice);
113
114if (SODIUM_LIBRARY_MAJOR_VERSION > 7 ||
115    (SODIUM_LIBRARY_MAJOR_VERSION == 7 &&
116     SODIUM_LIBRARY_MINOR_VERSION >= 5)) {
117    $anonymous_message_to_alice = sodium_crypto_box_seal('Anonymous message',
118                                                          $alice_publickey);
119
120    $decrypted_message = sodium_crypto_box_seal_open($anonymous_message_to_alice,
121                                                      $alice_kp);
122} else {
123    $decrypted_message = 'Anonymous message';
124}
125var_dump($decrypted_message);
126
127$msg = sodium_hex2bin(
128    '7375f4094f1151640bd853cb13dbc1a0ee9e13b0287a89d34fa2f6732be9de13f88457553d'.
129    '768347116522d6d32c9cb353ef07aa7c83bd129b2bb5db35b28334c935b24f2639405a0604'
130);
131$kp = sodium_hex2bin(
132    '36a6c2b96a650d80bf7e025e0f58f3d636339575defb370801a54213bd54582d'.
133    '5aecbcf7866e7a4d58a6c1317e2b955f54ecbe2fcbbf7d262c10636ed524480c'
134);
135var_dump(sodium_crypto_box_seal_open($msg, $kp));
136?>
137--EXPECT--
138bool(true)
139bool(true)
140bool(true)
141bool(true)
142bool(true)
143bool(true)
144bool(true)
145keypair size should be SODIUM_CRYPTO_BOX_KEYPAIRBYTES bytes
146bool(true)
147string(17) "Hi, this is Alice"
148string(21) "Hi Alice! This is Bob"
149string(17) "Anonymous message"
150string(26) "This is for your eyes only"
151