/stub/sodium_stub.php

https://github.com/paragonie/halite · PHP · 1095 lines · 587 code · 63 blank · 445 comment · 59 complexity · d41dea0eaacfe1c0bcfdf0e8c7846115 MD5 · raw file

  1. <?php
  2. declare(strict_types=1);
  3. if (\extension_loaded('sodium')) {
  4. return;
  5. }
  6. const SODIUM_CRYPTO_AEAD_AES256GCM_KEYBYTES = 32;
  7. const SODIUM_CRYPTO_AEAD_AES256GCM_NSECBYTES = 0;
  8. const SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES = 12;
  9. const SODIUM_CRYPTO_AEAD_AES256GCM_ABYTES = 16;
  10. const SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES = 32;
  11. const SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES = 0;
  12. const SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES = 8;
  13. const SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_ABYTES = 16;
  14. const SODIUM_CRYPTO_AUTH_BYTES = 32;
  15. const SODIUM_CRYPTO_AUTH_KEYBYTES = 32;
  16. const SODIUM_CRYPTO_BOX_SEALBYTES = 16;
  17. const SODIUM_CRYPTO_BOX_SECRETKEYBYTES = 32;
  18. const SODIUM_CRYPTO_BOX_PUBLICKEYBYTES = 32;
  19. const SODIUM_CRYPTO_BOX_KEYPAIRBYTES = 64;
  20. const SODIUM_CRYPTO_BOX_MACBYTES = 16;
  21. const SODIUM_CRYPTO_BOX_NONCEBYTES = 24;
  22. const SODIUM_CRYPTO_BOX_SEEDBYTES = 32;
  23. const SODIUM_CRYPTO_KX_BYTES = 32;
  24. const SODIUM_CRYPTO_KX_PUBLICKEYBYTES = 32;
  25. const SODIUM_CRYPTO_KX_SECRETKEYBYTES = 32;
  26. const SODIUM_CRYPTO_GENERICHASH_BYTES = 32;
  27. const SODIUM_CRYPTO_GENERICHASH_BYTES_MIN = 16;
  28. const SODIUM_CRYPTO_GENERICHASH_BYTES_MAX = 64;
  29. const SODIUM_CRYPTO_GENERICHASH_KEYBYTES = 32;
  30. const SODIUM_CRYPTO_GENERICHASH_KEYBYTES_MIN = 16;
  31. const SODIUM_CRYPTO_GENERICHASH_KEYBYTES_MAX = 64;
  32. const SODIUM_CRYPTO_PWHASH_ALG_ARGON2I13 = 1;
  33. const SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13 = 2;
  34. const SODIUM_CRYPTO_PWHASH_ALG_DEFAULT = 2;
  35. const SODIUM_CRYPTO_PWHASH_SALTBYTES = 16;
  36. const SODIUM_CRYPTO_PWHASH_STRPREFIX = '$argon2id$';
  37. const SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE = 2;
  38. const SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE = 67108864;
  39. const SODIUM_CRYPTO_PWHASH_OPSLIMIT_MODERATE = 3;
  40. const SODIUM_CRYPTO_PWHASH_MEMLIMIT_MODERATE = 268435456;
  41. const SODIUM_CRYPTO_PWHASH_OPSLIMIT_SENSITIVE = 4;
  42. const SODIUM_CRYPTO_PWHASH_MEMLIMIT_SENSITIVE = 1073741824;
  43. const SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_SALTBYTES = 32;
  44. const SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_STRPREFIX = '$7$';
  45. const SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_INTERACTIVE = 534288;
  46. const SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_INTERACTIVE = 16777216;
  47. const SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_SENSITIVE = 33554432;
  48. const SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_SENSITIVE = 1073741824;
  49. const SODIUM_CRYPTO_SCALARMULT_BYTES = 32;
  50. const SODIUM_CRYPTO_SCALARMULT_SCALARBYTES = 32;
  51. const SODIUM_CRYPTO_SHORTHASH_BYTES = 8;
  52. const SODIUM_CRYPTO_SHORTHASH_KEYBYTES = 16;
  53. const SODIUM_CRYPTO_SECRETBOX_KEYBYTES = 32;
  54. const SODIUM_CRYPTO_SECRETBOX_MACBYTES = 16;
  55. const SODIUM_CRYPTO_SECRETBOX_NONCEBYTES = 24;
  56. const SODIUM_CRYPTO_SIGN_BYTES = 64;
  57. const SODIUM_CRYPTO_SIGN_SEEDBYTES = 32;
  58. const SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES = 32;
  59. const SODIUM_CRYPTO_SIGN_SECRETKEYBYTES = 64;
  60. const SODIUM_CRYPTO_SIGN_KEYPAIRBYTES = 96;
  61. const SODIUM_CRYPTO_STREAM_KEYBYTES = 32;
  62. const SODIUM_CRYPTO_STREAM_NONCEBYTES = 24;
  63. const SODIUM_LIBRARY_VERSION = '0.0.0';
  64. const SODIUM_LIBRARY_MAJOR_VERSION = 1;
  65. const SODIUM_LIBRARY_MINOR_VERSION = 0;
  66. if (!\extension_loaded('sodium')) {
  67. /**
  68. * Can you access AES-256-GCM? This is only available if you have supported
  69. * hardware.
  70. *
  71. * @return bool
  72. */
  73. function sodium_crypto_aead_aes256gcm_is_available(): bool
  74. {
  75. if (\extension_loaded('libsodium')) {
  76. return \Sodium\crypto_aead_aes256gcm_is_available();
  77. }
  78. return false;
  79. }
  80. /**
  81. * Authenticated Encryption with Associated Data (decrypt)
  82. * AES-256-GCM
  83. *
  84. * @param string $msg encrypted message
  85. * @param string $nonce
  86. * @param string $key
  87. * @param string $ad additional data (optional)
  88. * @return string
  89. */
  90. function sodium_crypto_aead_aes256gcm_decrypt(
  91. string $msg,
  92. string $nonce,
  93. string $key,
  94. string $ad = ''
  95. ): string {
  96. if (\extension_loaded('libsodium')) {
  97. return \Sodium\crypto_aead_aes256gcm_decrypt($msg, $nonce, $key, $ad);
  98. }
  99. return '';
  100. }
  101. /**
  102. * Authenticated Encryption with Associated Data (encrypt)
  103. * AES-256-GCM
  104. *
  105. * @param string $msg plaintext message
  106. * @param string $nonce
  107. * @param string $key
  108. * @param string $ad additional data (optional)
  109. * @return string
  110. */
  111. function sodium_crypto_aead_aes256gcm_encrypt(
  112. string $msg,
  113. string $nonce,
  114. string $key,
  115. string $ad = ''
  116. ): string {
  117. if (\extension_loaded('libsodium')) {
  118. return \Sodium\crypto_aead_aes256gcm_encrypt($msg, $nonce, $key, $ad);
  119. }
  120. return '';
  121. }
  122. /**
  123. * Authenticated Encryption with Associated Data (decrypt)
  124. * ChaCha20 + Poly1305
  125. *
  126. * @param string $msg encrypted message
  127. * @param string $nonce
  128. * @param string $key
  129. * @param string $ad additional data (optional)
  130. * @return string
  131. */
  132. function sodium_crypto_aead_chacha20poly1305_decrypt(
  133. string $msg,
  134. string $nonce,
  135. string $key,
  136. string $ad = ''
  137. ): string {
  138. if (\extension_loaded('libsodium')) {
  139. return \Sodium\crypto_aead_chacha20poly1305_decrypt($msg, $nonce, $key, $ad);
  140. }
  141. return '';
  142. }
  143. /**
  144. * Authenticated Encryption with Associated Data (encrypt)
  145. * ChaCha20 + Poly1305
  146. *
  147. * @param string $msg plaintext message
  148. * @param string $nonce
  149. * @param string $key
  150. * @param string $ad additional data (optional)
  151. * @return string
  152. */
  153. function sodium_crypto_aead_chacha20poly1305_encrypt(
  154. string $msg,
  155. string $nonce,
  156. string $key,
  157. string $ad = ''
  158. ): string {
  159. if (\extension_loaded('libsodium')) {
  160. return \Sodium\crypto_aead_chacha20poly1305_encrypt($msg, $nonce, $key, $ad);
  161. }
  162. return '';
  163. }
  164. /**
  165. * Secret-key message authentication
  166. * HMAC SHA-512/256
  167. *
  168. * @param string $msg
  169. * @param string $key
  170. * @return string
  171. */
  172. function sodium_crypto_auth(
  173. string $msg,
  174. string $key
  175. ): string {
  176. if (\extension_loaded('libsodium')) {
  177. return \Sodium\crypto_auth($msg, $key);
  178. }
  179. return '';
  180. }
  181. /**
  182. * Secret-key message verification
  183. * HMAC SHA-512/256
  184. *
  185. * @param string $mac
  186. * @param string $msg
  187. * @param string $key
  188. * @return bool
  189. */
  190. function sodium_crypto_auth_verify(
  191. string $mac,
  192. string $msg,
  193. string $key
  194. ): bool {
  195. if (\extension_loaded('libsodium')) {
  196. return \Sodium\crypto_auth_verify($mac, $msg, $key);
  197. }
  198. return false;
  199. }
  200. /**
  201. * Public-key authenticated encryption (encrypt)
  202. * X25519 + Xsalsa20 + Poly1305
  203. *
  204. * @param string $msg
  205. * @param string $nonce
  206. * @param string $keypair
  207. * @return string
  208. */
  209. function sodium_crypto_box(
  210. string $msg,
  211. string $nonce,
  212. string $keypair
  213. ): string {
  214. if (\extension_loaded('libsodium')) {
  215. return \Sodium\crypto_box($msg, $nonce, $keypair);
  216. }
  217. return '';
  218. }
  219. /**
  220. * Generate an X25519 keypair for use with the crypto_box API
  221. *
  222. * @return string
  223. */
  224. function sodium_crypto_box_keypair(): string {
  225. if (\extension_loaded('libsodium')) {
  226. return \Sodium\crypto_box_keypair();
  227. }
  228. return '';
  229. }
  230. /**
  231. * Derive an X25519 keypair for use with the crypto_box API from a seed
  232. *
  233. * @param string $seed
  234. * @return string
  235. */
  236. function sodium_crypto_box_seed_keypair(
  237. string $seed
  238. ): string {
  239. if (\extension_loaded('libsodium')) {
  240. return \Sodium\crypto_box_seed_keypair($seed);
  241. }
  242. return '';
  243. }
  244. /**
  245. * Create an X25519 keypair from an X25519 secret key and X25519 public key
  246. *
  247. * @param string $secretkey
  248. * @param string $publickey
  249. * @return string
  250. */
  251. function sodium_crypto_box_keypair_from_secretkey_and_publickey(
  252. string $secretkey,
  253. string $publickey
  254. ): string {
  255. if (\extension_loaded('libsodium')) {
  256. return \Sodium\crypto_box_keypair_from_secretkey_and_publickey($secretkey, $publickey);
  257. }
  258. return '';
  259. }
  260. /**
  261. * Public-key authenticated encryption (decrypt)
  262. * X25519 + Xsalsa20 + Poly1305
  263. *
  264. * @param string $msg
  265. * @param string $nonce
  266. * @param string $keypair
  267. * @return string
  268. */
  269. function sodium_crypto_box_open(
  270. string $msg,
  271. string $nonce,
  272. string $keypair
  273. ): string {
  274. if (\extension_loaded('libsodium')) {
  275. return \Sodium\crypto_box_open($msg, $nonce, $keypair);
  276. }
  277. return '';
  278. }
  279. /**
  280. * Get an X25519 public key from an X25519 keypair
  281. *
  282. * @param string $keypair
  283. * @return string
  284. */
  285. function sodium_crypto_box_publickey(
  286. string $keypair
  287. ): string {
  288. if (\extension_loaded('libsodium')) {
  289. return \Sodium\crypto_box_publickey($keypair);
  290. }
  291. return '';
  292. }
  293. /**
  294. * Derive an X25519 public key from an X25519 secret key
  295. *
  296. * @param string $secretkey
  297. * @return string
  298. */
  299. function sodium_crypto_box_publickey_from_secretkey(
  300. string $secretkey
  301. ): string {
  302. if (\extension_loaded('libsodium')) {
  303. return \Sodium\crypto_box_publickey_from_secretkey($secretkey);
  304. }
  305. return '';
  306. }
  307. /**
  308. * Anonymous public-key encryption (encrypt)
  309. * X25519 + Xsalsa20 + Poly1305 + BLAKE2b
  310. *
  311. * @param string $message
  312. * @param string $publickey
  313. * @return string
  314. */
  315. function sodium_crypto_box_seal(
  316. string $message,
  317. string $publickey
  318. ): string {
  319. if (\extension_loaded('libsodium')) {
  320. return \Sodium\crypto_box_seal($message, $publickey);
  321. }
  322. return '';
  323. }
  324. /**
  325. * Anonymous public-key encryption (decrypt)
  326. * X25519 + Xsalsa20 + Poly1305 + BLAKE2b
  327. *
  328. * @param string $encrypted
  329. * @param string $keypair
  330. * @return string
  331. */
  332. function sodium_crypto_box_seal_open(
  333. string $encrypted,
  334. string $keypair
  335. ): string {
  336. if (\extension_loaded('libsodium')) {
  337. return \Sodium\crypto_box_seal_open($encrypted, $keypair);
  338. }
  339. return '';
  340. }
  341. /**
  342. * Extract the X25519 secret key from an X25519 keypair
  343. *
  344. * @param string $keypair
  345. * @return string
  346. */
  347. function sodium_crypto_box_secretkey(string $keypair): string
  348. {
  349. if (\extension_loaded('libsodium')) {
  350. return \Sodium\crypto_box_secretkey($keypair);
  351. }
  352. return '';
  353. }
  354. /**
  355. * Elliptic Curve Diffie Hellman Key Exchange
  356. * X25519
  357. *
  358. * @param string $secretkey
  359. * @param string $publickey
  360. * @param string $client_publickey
  361. * @param string $server_publickey
  362. * @return string
  363. */
  364. function sodium_crypto_kx(
  365. string $secretkey,
  366. string $publickey,
  367. string $client_publickey,
  368. string $server_publickey
  369. ): string {
  370. if (\extension_loaded('libsodium')) {
  371. return \Sodium\crypto_kx($secretkey, $publickey, $client_publickey, $server_publickey);
  372. }
  373. return '';
  374. }
  375. /**
  376. * Fast and secure cryptographic hash
  377. *
  378. * @param string $input
  379. * @param string $key
  380. * @param int $length
  381. * @return string
  382. */
  383. function sodium_crypto_generichash(
  384. string $input,
  385. string $key = '',
  386. int $length = 32
  387. ): string{
  388. if (\extension_loaded('libsodium')) {
  389. return \Sodium\crypto_generichash($input, $key, $length);
  390. }
  391. return '';
  392. }
  393. /**
  394. * Create a new hash state (e.g. to use for streams)
  395. * BLAKE2b
  396. *
  397. * @param string $key
  398. * @param int $length
  399. * @return string
  400. */
  401. function sodium_crypto_generichash_init(
  402. string $key = '',
  403. int $length = 32
  404. ): string {
  405. if (\extension_loaded('libsodium')) {
  406. return \Sodium\crypto_generichash_init($key, $length);
  407. }
  408. return '';
  409. }
  410. /**
  411. * Update the hash state with some data
  412. * BLAKE2b
  413. *
  414. * @param &string $hashState
  415. * @param string $append
  416. * @return bool
  417. */
  418. function sodium_crypto_generichash_update(
  419. string &$hashState,
  420. string $append
  421. ) {
  422. if (\extension_loaded('libsodium')) {
  423. return \Sodium\crypto_generichash_update($hashState, $append);
  424. }
  425. }
  426. /**
  427. * Get the final hash
  428. * BLAKE2b
  429. *
  430. * @param string $hashState
  431. * @param int $length
  432. * @return string
  433. */
  434. function sodium_crypto_generichash_final(
  435. string $hashState,
  436. int $length = 32
  437. ): string {
  438. if (\extension_loaded('libsodium')) {
  439. return \Sodium\crypto_generichash_final($hashState, $length);
  440. }
  441. return '';
  442. }
  443. /**
  444. * Secure password-based key derivation function
  445. * Argon2i
  446. *
  447. * @param int $out_len
  448. * @param string $passwd
  449. * @param string $salt
  450. * @param int $opslimit
  451. * @param int $memlimit
  452. * @return string
  453. */
  454. function sodium_crypto_pwhash(
  455. int $out_len,
  456. string $passwd,
  457. string $salt,
  458. int $opslimit,
  459. int $memlimit
  460. ): string {
  461. if (\extension_loaded('libsodium')) {
  462. return \Sodium\crypto_pwhash($out_len, $passwd, $salt, $opslimit, $memlimit);
  463. }
  464. return '';
  465. }
  466. /**
  467. * Get a formatted password hash (for storage)
  468. * Argon2i
  469. *
  470. * @param string $passwd
  471. * @param int $opslimit
  472. * @param int $memlimit
  473. * @return string
  474. */
  475. function sodium_crypto_pwhash_str(
  476. string $passwd,
  477. int $opslimit,
  478. int $memlimit
  479. ): string {
  480. if (\extension_loaded('libsodium')) {
  481. return \Sodium\crypto_pwhash_str($passwd, $opslimit, $memlimit);
  482. }
  483. return '';
  484. }
  485. /**
  486. * Verify a password against a hash
  487. * Argon2i
  488. *
  489. * @param string $hash
  490. * @param string $passwd
  491. * @return bool
  492. */
  493. function sodium_crypto_pwhash_str_verify(
  494. string $hash,
  495. string $passwd
  496. ): bool {
  497. if (\extension_loaded('libsodium')) {
  498. return \Sodium\crypto_pwhash_str_verify($hash, $passwd);
  499. }
  500. return false;
  501. }
  502. /**
  503. * Secure password-based key derivation function
  504. * Scrypt
  505. *
  506. * @param int $out_len
  507. * @param string $passwd
  508. * @param string $salt
  509. * @param int $opslimit
  510. * @param int $memlimit
  511. * @return string
  512. */
  513. function sodium_crypto_pwhash_scryptsalsa208sha256(
  514. int $out_len,
  515. string $passwd,
  516. string $salt,
  517. int $opslimit,
  518. int $memlimit
  519. ): string {
  520. if (\extension_loaded('libsodium')) {
  521. return \Sodium\crypto_pwhash_scryptsalsa208sha256($out_len, $passwd, $salt, $opslimit, $memlimit);
  522. }
  523. return '';
  524. }
  525. /**
  526. * Get a formatted password hash (for storage)
  527. * Scrypt
  528. *
  529. * @param string $passwd
  530. * @param int $opslimit
  531. * @param int $memlimit
  532. * @return string
  533. */
  534. function sodium_crypto_pwhash_scryptsalsa208sha256_str(
  535. string $passwd,
  536. int $opslimit,
  537. int $memlimit
  538. ): string {
  539. if (\extension_loaded('libsodium')) {
  540. return \Sodium\crypto_pwhash_scryptsalsa208sha256_str($passwd, $opslimit, $memlimit);
  541. }
  542. return '';
  543. }
  544. /**
  545. * Verify a password against a hash
  546. * Scrypt
  547. *
  548. * @param string $hash
  549. * @param string $passwd
  550. * @return bool
  551. */
  552. function sodium_crypto_pwhash_scryptsalsa208sha256_str_verify(
  553. string $hash,
  554. string $passwd
  555. ): bool {
  556. if (\extension_loaded('libsodium')) {
  557. return \Sodium\crypto_pwhash_scryptsalsa208sha256_str_verify($hash, $passwd);
  558. }
  559. return false;
  560. }
  561. /**
  562. * Elliptic Curve Diffie Hellman over Curve25519
  563. * X25519
  564. *
  565. * @param string $ecdhA
  566. * @param string $ecdhB
  567. * @return string
  568. */
  569. function sodium_crypto_scalarmult(
  570. string $ecdhA,
  571. string $ecdhB
  572. ): string {
  573. if (\extension_loaded('libsodium')) {
  574. return \Sodium\crypto_scalarmult($ecdhA, $ecdhB);
  575. }
  576. return '';
  577. }
  578. /**
  579. * Authenticated secret-key encryption (encrypt)
  580. * Xsals20 + Poly1305
  581. *
  582. * @param string $plaintext
  583. * @param string $nonce
  584. * @param string $key
  585. * @return string
  586. */
  587. function sodium_crypto_secretbox(
  588. string $plaintext,
  589. string $nonce,
  590. string $key
  591. ): string {
  592. if (\extension_loaded('libsodium')) {
  593. return \Sodium\crypto_secretbox($plaintext, $nonce, $key);
  594. }
  595. return '';
  596. }
  597. /**
  598. * Authenticated secret-key encryption (decrypt)
  599. * Xsals20 + Poly1305
  600. *
  601. * @param string $ciphertext
  602. * @param string $nonce
  603. * @param string $key
  604. * @return string
  605. */
  606. function sodium_crypto_secretbox_open(
  607. string $ciphertext,
  608. string $nonce,
  609. string $key
  610. ): string {
  611. if (\extension_loaded('libsodium')) {
  612. return \Sodium\crypto_secretbox_open($ciphertext, $nonce, $key);
  613. }
  614. return '';
  615. }
  616. /**
  617. * A short keyed hash suitable for data structures
  618. * SipHash-2-4
  619. *
  620. * @param string $message
  621. * @param string $key
  622. * @return string
  623. */
  624. function sodium_crypto_shorthash(
  625. string $message,
  626. string $key
  627. ): string {
  628. if (\extension_loaded('libsodium')) {
  629. return \Sodium\crypto_shorthash($message, $key);
  630. }
  631. return '';
  632. }
  633. /**
  634. * Digital Signature
  635. * Ed25519
  636. *
  637. * @param string $message
  638. * @param string $secretkey
  639. * @return string
  640. */
  641. function sodium_crypto_sign(
  642. string $message,
  643. string $secretkey
  644. ): string {
  645. if (\extension_loaded('libsodium')) {
  646. return \Sodium\crypto_sign($message, $secretkey);
  647. }
  648. return '';
  649. }
  650. /**
  651. * Digital Signature (detached)
  652. * Ed25519
  653. *
  654. * @param string $message
  655. * @param string $secretkey
  656. * @return string
  657. */
  658. function sodium_crypto_sign_detached(
  659. string $message,
  660. string $secretkey
  661. ): string {
  662. if (\extension_loaded('libsodium')) {
  663. return \Sodium\crypto_sign_detached($message, $secretkey);
  664. }
  665. return '';
  666. }
  667. /**
  668. * Convert an Ed25519 public key to an X25519 public key
  669. *
  670. * @param string $sign_pk
  671. * @return string
  672. */
  673. function sodium_crypto_sign_ed25519_pk_to_curve25519(
  674. string $sign_pk
  675. ): string {
  676. if (\extension_loaded('libsodium')) {
  677. return \Sodium\crypto_sign_ed25519_pk_to_curve25519($sign_pk);
  678. }
  679. return '';
  680. }
  681. /**
  682. * Convert an Ed25519 secret key to an X25519 secret key
  683. *
  684. * @param string $sign_sk
  685. * @return string
  686. */
  687. function sodium_crypto_sign_ed25519_sk_to_curve25519(
  688. string $sign_sk
  689. ): string {
  690. if (\extension_loaded('libsodium')) {
  691. return \Sodium\crypto_sign_ed25519_sk_to_curve25519($sign_sk);
  692. }
  693. return '';
  694. }
  695. /**
  696. * Generate an Ed25519 keypair for use with the crypto_sign API
  697. *
  698. * @return string
  699. */
  700. function sodium_crypto_sign_keypair(): string
  701. {
  702. if (\extension_loaded('libsodium')) {
  703. return \Sodium\crypto_sign_keypair();
  704. }
  705. return '';
  706. }
  707. /**
  708. * Create an Ed25519 keypair from an Ed25519 secret key + Ed25519 public key
  709. *
  710. * @param string $secretkey
  711. * @param string $publickey
  712. * @return string
  713. */
  714. function sodium_crypto_sign_keypair_from_secretkey_and_publickey(
  715. string $secretkey,
  716. string $publickey
  717. ): string {
  718. if (\extension_loaded('libsodium')) {
  719. return \Sodium\crypto_keypair_from_secretkey_and_publickey($secretkey, $publickey);
  720. }
  721. return '';
  722. }
  723. /**
  724. * Verify a signed message and return the plaintext
  725. *
  726. * @param string $signed_message
  727. * @param string $publickey
  728. * @return string
  729. */
  730. function sodium_crypto_sign_open(
  731. string $signed_message,
  732. string $publickey
  733. ): string {
  734. if (\extension_loaded('libsodium')) {
  735. return \Sodium\crypto_sign_open($signed_message, $publickey);
  736. }
  737. return '';
  738. }
  739. /**
  740. * Get the public key from an Ed25519 keypair
  741. *
  742. * @param string $keypair
  743. * @return string
  744. */
  745. function sodium_crypto_sign_publickey(
  746. string $keypair
  747. ): string {
  748. if (\extension_loaded('libsodium')) {
  749. return \Sodium\crypto_sign_publickey($keypair);
  750. }
  751. return '';
  752. }
  753. /**
  754. * Get the secret key from an Ed25519 keypair
  755. *
  756. * @param string $keypair
  757. * @return string
  758. */
  759. function sodium_crypto_sign_secretkey(
  760. string $keypair
  761. ): string {
  762. if (\extension_loaded('libsodium')) {
  763. return \Sodium\crypto_sign_secretkey($keypair);
  764. }
  765. return '';
  766. }
  767. /**
  768. * Derive an Ed25519 public key from an Ed25519 secret key
  769. *
  770. * @param string $secretkey
  771. * @return string
  772. */
  773. function sodium_crypto_sign_publickey_from_secretkey(
  774. string $secretkey
  775. ): string {
  776. if (\extension_loaded('libsodium')) {
  777. return \Sodium\crypto_sign_publickey_from_secretkey($secretkey);
  778. }
  779. return '';
  780. }
  781. /**
  782. * Derive an Ed25519 keypair for use with the crypto_sign API from a seed
  783. *
  784. * @param string $seed
  785. * @return string
  786. */
  787. function sodium_crypto_sign_seed_keypair(
  788. string $seed
  789. ): string {
  790. if (\extension_loaded('libsodium')) {
  791. return \Sodium\crypto_sign_seed_keypair($seed);
  792. }
  793. return '';
  794. }
  795. /**
  796. * Verify a detached signature
  797. *
  798. * @param string $signature
  799. * @param string $msg
  800. * @param string $publickey
  801. * @return bool
  802. */
  803. function sodium_crypto_sign_verify_detached(
  804. string $signature,
  805. string $msg,
  806. string $publickey
  807. ): bool {
  808. if (\extension_loaded('libsodium')) {
  809. return \Sodium\crypto_sign_verify_detached($signature, $msg, $publickey);
  810. }
  811. return false;
  812. }
  813. /**
  814. * Create a keystream from a key and nonce
  815. * Xsalsa20
  816. *
  817. * @param int $length
  818. * @param string $nonce
  819. * @param string $key
  820. * @return string
  821. */
  822. function sodium_crypto_stream(
  823. int $length,
  824. string $nonce,
  825. string $key
  826. ): string {
  827. if (\extension_loaded('libsodium')) {
  828. return \Sodium\crypto_stream($length, $nonce, $key);
  829. }
  830. return '';
  831. }
  832. /**
  833. * Encrypt a message using a stream cipher
  834. * Xsalsa20
  835. *
  836. * @param string $plaintext
  837. * @param string $nonce
  838. * @param string $key
  839. * @return string
  840. */
  841. function sodium_crypto_stream_xor(
  842. string $plaintext,
  843. string $nonce,
  844. string $key
  845. ): string {
  846. if (\extension_loaded('libsodium')) {
  847. return \Sodium\crypto_stream_xor($plaintext, $nonce, $key);
  848. }
  849. return '';
  850. }
  851. /**
  852. * Generate a string of random bytes
  853. * /dev/urandom
  854. *
  855. * @param int $length
  856. * @return string
  857. */
  858. function sodium_randombytes_buf(
  859. int $length
  860. ): string {
  861. return \random_bytes($length);
  862. }
  863. /**
  864. * Generate a 16-bit integer
  865. * /dev/urandom
  866. *
  867. * @return int
  868. */
  869. function sodium_randombytes_random16(): int
  870. {
  871. return randombytes_uniform(0xffff);
  872. }
  873. /**
  874. * Generate an unbiased random integer between 0 and a specified value
  875. * /dev/urandom
  876. *
  877. * @param int $upperBoundNonInclusive
  878. * @return int
  879. */
  880. function sodium_randombytes_uniform(
  881. int $upperBoundNonInclusive
  882. ): int {
  883. return \random_int(0, $upperBoundNonInclusive - 1);
  884. }
  885. /**
  886. * Convert to hex without side-chanels
  887. *
  888. * @param string $binary
  889. * @return string
  890. */
  891. function sodium_bin2hex(
  892. string $binary
  893. ): string {
  894. if (\extension_loaded('libsodium')) {
  895. return \Sodium\bin2hex($binary);
  896. }
  897. return '';
  898. }
  899. /**
  900. * Compare two strings in constant time
  901. *
  902. * @param string $left
  903. * @param string $right
  904. * @return int
  905. */
  906. function sodium_compare(
  907. string $left,
  908. string $right
  909. ): int {
  910. if (\extension_loaded('libsodium')) {
  911. return \Sodium\compare($left, $right);
  912. }
  913. return 0;
  914. }
  915. /**
  916. * Convert from hex without side-chanels
  917. *
  918. * @param string $hex
  919. * @return string
  920. */
  921. function sodium_hex2bin(
  922. string $hex
  923. ): string {
  924. if (\extension_loaded('libsodium')) {
  925. return \Sodium\hex2bin($hex);
  926. }
  927. return '';
  928. }
  929. /**
  930. * Increment a string in little-endian
  931. *
  932. * @param &string $nonce
  933. * @return string
  934. */
  935. function sodium_increment(string &$nonce)
  936. {
  937. if (\extension_loaded('libsodium')) {
  938. return \Sodium\increment($nonce);
  939. }
  940. return '';
  941. }
  942. /**
  943. * Add the right operand to the left
  944. *
  945. * @param string &$left
  946. * @param string $right
  947. * @return string
  948. */
  949. function sodium_add(
  950. string &$left,
  951. string $right
  952. ): string {
  953. if (\extension_loaded('libsodium')) {
  954. return \Sodium\add($left, $right);
  955. }
  956. return '';
  957. }
  958. /**
  959. * Get the true major version of libsodium
  960. * @return int
  961. */
  962. function sodium_library_version_major(): int {
  963. if (\extension_loaded('libsodium')) {
  964. return \SODIUM_LIBRARY_MAJOR_VERSION;
  965. }
  966. return 0;
  967. }
  968. /**
  969. * Get the true minor version of libsodium
  970. * @return int
  971. */
  972. function sodium_library_version_minor(): int {
  973. if (\extension_loaded('libsodium')) {
  974. return \SODIUM_LIBRARY_MINOR_VERSION;
  975. }
  976. return 0;
  977. }
  978. /**
  979. * Compare two strings in constant time
  980. *
  981. * @param string $left
  982. * @param string $right
  983. * @return int
  984. */
  985. function sodium_memcmp(
  986. string $left,
  987. string $right
  988. ): int {
  989. if (\extension_loaded('libsodium')) {
  990. return \Sodium\memcmp($left, $right);
  991. }
  992. return 0;
  993. }
  994. /**
  995. * Wipe a buffer
  996. *
  997. * @param &string $nonce
  998. */
  999. function sodium_memzero(
  1000. string &$target
  1001. ) {
  1002. if (\extension_loaded('libsodium')) {
  1003. return \Sodium\memzero($target);
  1004. }
  1005. $target = '';
  1006. }
  1007. /**
  1008. * Get the version string
  1009. *
  1010. * @return string
  1011. */
  1012. function sodium_version_string(): string {
  1013. if (\extension_loaded('libsodium')) {
  1014. return \Sodium\version_string();
  1015. }
  1016. return 'NA';
  1017. }
  1018. /**
  1019. * Scalar multiplication of the base point and your key
  1020. *
  1021. * @param string $sk
  1022. * @return string
  1023. */
  1024. function sodium_crypto_scalarmult_base(
  1025. string $sk
  1026. ): string {
  1027. if (\extension_loaded('libsodium')) {
  1028. return \Sodium\scalarmult_base($sk);
  1029. }
  1030. return '';
  1031. }
  1032. }