functions.php 16 KB

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