1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733 |
- /*
- +----------------------------------------------------------------------+
- | PHP Version 7 |
- +----------------------------------------------------------------------+
- | Copyright (c) 1997-2018 The PHP Group |
- +----------------------------------------------------------------------+
- | This source file is subject to version 3.01 of the PHP license, |
- | that is bundled with this package in the file LICENSE, and is |
- | available through the world-wide-web at the following url: |
- | http://www.php.net/license/3_01.txt |
- | If you did not receive a copy of the PHP license and are unable to |
- | obtain it through the world-wide-web, please send a note to |
- | license@php.net so we can mail you a copy immediately. |
- +----------------------------------------------------------------------+
- | Authors: Frank Denis <jedisct1@php.net> |
- +----------------------------------------------------------------------+
- */
- #ifdef HAVE_CONFIG_H
- # include "config.h"
- #endif
- #include "php.h"
- #include "php_ini.h"
- #include "ext/standard/info.h"
- #include "php_libsodium.h"
- #include "zend_exceptions.h"
- #include <sodium.h>
- #include <stdint.h>
- #include <string.h>
- #define PHP_SODIUM_ZSTR_TRUNCATE(zs, len) do { ZSTR_LEN(zs) = (len); } while(0)
- static zend_class_entry *sodium_exception_ce;
- ZEND_BEGIN_ARG_INFO_EX(AI_None, 0, 0, 0)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_FirstArgByReference, 0, 0, 1)
- ZEND_ARG_INFO(1, reference)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_String, 0, 0, 1)
- ZEND_ARG_INFO(0, string)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_StringRef, 0, 0, 1)
- ZEND_ARG_INFO(1, string)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_TwoStrings, 0, 0, 2)
- ZEND_ARG_INFO(0, string_1)
- ZEND_ARG_INFO(0, string_2)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_StringAndMaybeString, 0, 0, 1)
- ZEND_ARG_INFO(0, string_1)
- /* optional */
- ZEND_ARG_INFO(0, string_2)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_StringAndIdAndMaybeString, 0, 0, 2)
- ZEND_ARG_INFO(0, string_1)
- ZEND_ARG_INFO(0, id)
- /* optional */
- ZEND_ARG_INFO(0, string_2)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_StringRefAndString, 0, 0, 2)
- ZEND_ARG_INFO(1, string_1)
- ZEND_ARG_INFO(0, string_2)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_StringAndKey, 0, 0, 2)
- ZEND_ARG_INFO(0, string)
- ZEND_ARG_INFO(0, key)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_StringAndLength, 0, 0, 2)
- ZEND_ARG_INFO(0, string)
- ZEND_ARG_INFO(0, length)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_StringAndId, 0, 0, 2)
- ZEND_ARG_INFO(0, string)
- ZEND_ARG_INFO(0, id)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_StringAndKeyPair, 0, 0, 2)
- ZEND_ARG_INFO(0, string)
- ZEND_ARG_INFO(0, keypair)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_SignatureAndStringAndKey, 0, 0, 3)
- ZEND_ARG_INFO(0, signature)
- ZEND_ARG_INFO(0, string)
- ZEND_ARG_INFO(0, key)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_Key, 0, 0, 1)
- ZEND_ARG_INFO(0, key)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_SecretKeyAndPublicKey, 0, 0, 2)
- ZEND_ARG_INFO(0, secret_key)
- ZEND_ARG_INFO(0, public_key)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_LengthAndNonceAndKey, 0, 0, 3)
- ZEND_ARG_INFO(0, length)
- ZEND_ARG_INFO(0, nonce)
- ZEND_ARG_INFO(0, key)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_StringAndNonceAndKey, 0, 0, 3)
- ZEND_ARG_INFO(0, string)
- ZEND_ARG_INFO(0, nonce)
- ZEND_ARG_INFO(0, key)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_StringAndNonceAndKeyPair, 0, 0, 3)
- ZEND_ARG_INFO(0, string)
- ZEND_ARG_INFO(0, nonce)
- ZEND_ARG_INFO(0, key)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_StringAndMaybeKeyAndLength, 0, 0, 1)
- ZEND_ARG_INFO(0, string)
- /* optional */
- ZEND_ARG_INFO(0, key)
- ZEND_ARG_INFO(0, length)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_LengthAndPasswordAndSaltAndOpsLimitAndMemLimit, 0, 0, 5)
- ZEND_ARG_INFO(0, length)
- ZEND_ARG_INFO(0, password)
- ZEND_ARG_INFO(0, salt)
- ZEND_ARG_INFO(0, opslimit)
- ZEND_ARG_INFO(0, memlimit)
- /* optional */
- ZEND_ARG_INFO(0, alg)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_PasswordAndOpsLimitAndMemLimit, 0, 0, 3)
- ZEND_ARG_INFO(0, password)
- ZEND_ARG_INFO(0, opslimit)
- ZEND_ARG_INFO(0, memlimit)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_HashAndPassword, 0, 0, 2)
- ZEND_ARG_INFO(0, hash)
- ZEND_ARG_INFO(0, password)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_StringAndADAndNonceAndKey, 0, 0, 4)
- ZEND_ARG_INFO(0, string)
- ZEND_ARG_INFO(0, ad)
- ZEND_ARG_INFO(0, nonce)
- ZEND_ARG_INFO(0, key)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_StateByReference, 0, 0, 1)
- ZEND_ARG_INFO(1, state)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_StateByReferenceAndStringAndMaybeStringAndLong, 0, 0, 2)
- ZEND_ARG_INFO(1, state)
- ZEND_ARG_INFO(0, string)
- /* optional */
- ZEND_ARG_INFO(0, string)
- ZEND_ARG_INFO(0, long)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_StateByReferenceAndStringAndMaybeString, 0, 0, 2)
- ZEND_ARG_INFO(1, state)
- ZEND_ARG_INFO(0, string)
- /* optional */
- ZEND_ARG_INFO(0, string)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_StateByReferenceAndMaybeLength, 0, 0, 1)
- ZEND_ARG_INFO(1, state)
- /* optional */
- ZEND_ARG_INFO(0, length)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_StateByReferenceAndString, 0, 0, 2)
- ZEND_ARG_INFO(1, state)
- ZEND_ARG_INFO(0, string)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_MaybeKeyAndLength, 0, 0, 0)
- /* optional */
- ZEND_ARG_INFO(0, key)
- ZEND_ARG_INFO(0, length)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_KXClientSession, 0, 0, 2)
- ZEND_ARG_INFO(0, client_keypair)
- ZEND_ARG_INFO(0, server_key)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_KXServerSession, 0, 0, 2)
- ZEND_ARG_INFO(0, server_keypair)
- ZEND_ARG_INFO(0, client_key)
- ZEND_END_ARG_INFO()
- ZEND_BEGIN_ARG_INFO_EX(AI_KDF, 0, 0, 4)
- ZEND_ARG_INFO(0, subkey_len)
- ZEND_ARG_INFO(0, subkey_id)
- ZEND_ARG_INFO(0, context)
- ZEND_ARG_INFO(0, key)
- ZEND_END_ARG_INFO()
- #if (defined(__amd64) || defined(__amd64__) || defined(__x86_64__) || defined(__i386__) || \
- defined(_M_AMD64) || defined(_M_IX86))
- # define HAVE_AESGCM 1
- #endif
- #ifndef crypto_aead_chacha20poly1305_IETF_KEYBYTES
- # define crypto_aead_chacha20poly1305_IETF_KEYBYTES crypto_aead_chacha20poly1305_KEYBYTES
- #endif
- #ifndef crypto_aead_chacha20poly1305_IETF_NSECBYTES
- # define crypto_aead_chacha20poly1305_IETF_NSECBYTES crypto_aead_chacha20poly1305_NSECBYTES
- #endif
- #ifndef crypto_aead_chacha20poly1305_IETF_ABYTES
- # define crypto_aead_chacha20poly1305_IETF_ABYTES crypto_aead_chacha20poly1305_ABYTES
- #endif
- #if defined(crypto_secretstream_xchacha20poly1305_ABYTES) && SODIUM_LIBRARY_VERSION_MAJOR < 10
- # undef crypto_secretstream_xchacha20poly1305_ABYTES
- #endif
- #ifndef crypto_pwhash_OPSLIMIT_MIN
- # define crypto_pwhash_OPSLIMIT_MIN crypto_pwhash_OPSLIMIT_INTERACTIVE
- #endif
- #ifndef crypto_pwhash_MEMLIMIT_MIN
- # define crypto_pwhash_MEMLIMIT_MIN crypto_pwhash_MEMLIMIT_INTERACTIVE
- #endif
- #ifndef crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN
- # define crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE
- #endif
- #ifndef crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN
- # define crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE
- #endif
- static const zend_function_entry sodium_functions[] = {
- PHP_FE(sodium_crypto_aead_aes256gcm_is_available, AI_None)
- #ifdef HAVE_AESGCM
- PHP_FE(sodium_crypto_aead_aes256gcm_decrypt, AI_StringAndADAndNonceAndKey)
- PHP_FE(sodium_crypto_aead_aes256gcm_encrypt, AI_StringAndADAndNonceAndKey)
- PHP_FE(sodium_crypto_aead_aes256gcm_keygen, AI_None)
- #endif
- PHP_FE(sodium_crypto_aead_chacha20poly1305_decrypt, AI_StringAndADAndNonceAndKey)
- PHP_FE(sodium_crypto_aead_chacha20poly1305_encrypt, AI_StringAndADAndNonceAndKey)
- PHP_FE(sodium_crypto_aead_chacha20poly1305_keygen, AI_None)
- PHP_FE(sodium_crypto_aead_chacha20poly1305_ietf_decrypt, AI_StringAndADAndNonceAndKey)
- PHP_FE(sodium_crypto_aead_chacha20poly1305_ietf_encrypt, AI_StringAndADAndNonceAndKey)
- PHP_FE(sodium_crypto_aead_chacha20poly1305_ietf_keygen, AI_None)
- #ifdef crypto_aead_xchacha20poly1305_IETF_NPUBBYTES
- PHP_FE(sodium_crypto_aead_xchacha20poly1305_ietf_decrypt, AI_StringAndADAndNonceAndKey)
- PHP_FE(sodium_crypto_aead_xchacha20poly1305_ietf_keygen, AI_None)
- PHP_FE(sodium_crypto_aead_xchacha20poly1305_ietf_encrypt, AI_StringAndADAndNonceAndKey)
- #endif
- PHP_FE(sodium_crypto_auth, AI_StringAndKey)
- PHP_FE(sodium_crypto_auth_keygen, AI_None)
- PHP_FE(sodium_crypto_auth_verify, AI_SignatureAndStringAndKey)
- PHP_FE(sodium_crypto_box, AI_StringAndNonceAndKeyPair)
- PHP_FE(sodium_crypto_box_keypair, AI_None)
- PHP_FE(sodium_crypto_box_seed_keypair, AI_Key)
- PHP_FE(sodium_crypto_box_keypair_from_secretkey_and_publickey, AI_SecretKeyAndPublicKey)
- PHP_FE(sodium_crypto_box_open, AI_StringAndNonceAndKey)
- PHP_FE(sodium_crypto_box_publickey, AI_Key)
- PHP_FE(sodium_crypto_box_publickey_from_secretkey, AI_Key)
- PHP_FE(sodium_crypto_box_seal, AI_StringAndKey)
- PHP_FE(sodium_crypto_box_seal_open, AI_StringAndKey)
- PHP_FE(sodium_crypto_box_secretkey, AI_Key)
- PHP_FE(sodium_crypto_kx_keypair, AI_None)
- PHP_FE(sodium_crypto_kx_publickey, AI_Key)
- PHP_FE(sodium_crypto_kx_secretkey, AI_Key)
- PHP_FE(sodium_crypto_kx_seed_keypair, AI_String)
- PHP_FE(sodium_crypto_kx_client_session_keys, AI_KXClientSession)
- PHP_FE(sodium_crypto_kx_server_session_keys, AI_KXServerSession)
- PHP_FE(sodium_crypto_generichash, AI_StringAndMaybeKeyAndLength)
- PHP_FE(sodium_crypto_generichash_keygen, AI_None)
- PHP_FE(sodium_crypto_generichash_init, AI_MaybeKeyAndLength)
- PHP_FE(sodium_crypto_generichash_update, AI_StateByReferenceAndString)
- PHP_FE(sodium_crypto_generichash_final, AI_StateByReferenceAndMaybeLength)
- PHP_FE(sodium_crypto_kdf_derive_from_key, AI_KDF)
- PHP_FE(sodium_crypto_kdf_keygen, AI_None)
- #ifdef crypto_pwhash_SALTBYTES
- PHP_FE(sodium_crypto_pwhash, AI_LengthAndPasswordAndSaltAndOpsLimitAndMemLimit)
- PHP_FE(sodium_crypto_pwhash_str, AI_PasswordAndOpsLimitAndMemLimit)
- PHP_FE(sodium_crypto_pwhash_str_verify, AI_HashAndPassword)
- #endif
- #if SODIUM_LIBRARY_VERSION_MAJOR > 9 || (SODIUM_LIBRARY_VERSION_MAJOR == 9 && SODIUM_LIBRARY_VERSION_MINOR >= 6)
- PHP_FE(sodium_crypto_pwhash_str_needs_rehash, AI_PasswordAndOpsLimitAndMemLimit)
- #endif
- #ifdef crypto_pwhash_scryptsalsa208sha256_SALTBYTES
- PHP_FE(sodium_crypto_pwhash_scryptsalsa208sha256, AI_LengthAndPasswordAndSaltAndOpsLimitAndMemLimit)
- PHP_FE(sodium_crypto_pwhash_scryptsalsa208sha256_str, AI_PasswordAndOpsLimitAndMemLimit)
- PHP_FE(sodium_crypto_pwhash_scryptsalsa208sha256_str_verify, AI_HashAndPassword)
- #endif
- PHP_FE(sodium_crypto_scalarmult, AI_TwoStrings)
- PHP_FE(sodium_crypto_secretbox, AI_StringAndNonceAndKey)
- PHP_FE(sodium_crypto_secretbox_keygen, AI_None)
- PHP_FE(sodium_crypto_secretbox_open, AI_StringAndNonceAndKey)
- #ifdef crypto_secretstream_xchacha20poly1305_ABYTES
- PHP_FE(sodium_crypto_secretstream_xchacha20poly1305_keygen, AI_None)
- PHP_FE(sodium_crypto_secretstream_xchacha20poly1305_init_push, AI_Key)
- PHP_FE(sodium_crypto_secretstream_xchacha20poly1305_push, AI_StateByReferenceAndStringAndMaybeStringAndLong)
- PHP_FE(sodium_crypto_secretstream_xchacha20poly1305_init_pull, AI_StringAndKey)
- PHP_FE(sodium_crypto_secretstream_xchacha20poly1305_pull, AI_StateByReferenceAndStringAndMaybeString)
- PHP_FE(sodium_crypto_secretstream_xchacha20poly1305_rekey, AI_StateByReference)
- #endif
- PHP_FE(sodium_crypto_shorthash, AI_StringAndKey)
- PHP_FE(sodium_crypto_shorthash_keygen, AI_None)
- PHP_FE(sodium_crypto_sign, AI_StringAndKeyPair)
- PHP_FE(sodium_crypto_sign_detached, AI_StringAndKeyPair)
- PHP_FE(sodium_crypto_sign_ed25519_pk_to_curve25519, AI_Key)
- PHP_FE(sodium_crypto_sign_ed25519_sk_to_curve25519, AI_Key)
- PHP_FE(sodium_crypto_sign_keypair, AI_None)
- PHP_FE(sodium_crypto_sign_keypair_from_secretkey_and_publickey, AI_SecretKeyAndPublicKey)
- PHP_FE(sodium_crypto_sign_open, AI_StringAndKeyPair)
- PHP_FE(sodium_crypto_sign_publickey, AI_Key)
- PHP_FE(sodium_crypto_sign_secretkey, AI_Key)
- PHP_FE(sodium_crypto_sign_publickey_from_secretkey, AI_Key)
- PHP_FE(sodium_crypto_sign_seed_keypair, AI_Key)
- PHP_FE(sodium_crypto_sign_verify_detached, AI_SignatureAndStringAndKey)
- PHP_FE(sodium_crypto_stream, AI_LengthAndNonceAndKey)
- PHP_FE(sodium_crypto_stream_keygen, AI_None)
- PHP_FE(sodium_crypto_stream_xor, AI_StringAndNonceAndKey)
- /* helpers */
- PHP_FE(sodium_add, AI_StringRefAndString)
- PHP_FE(sodium_compare, AI_TwoStrings)
- PHP_FE(sodium_increment, AI_StringRef)
- PHP_FE(sodium_memcmp, AI_TwoStrings)
- PHP_FE(sodium_memzero, AI_FirstArgByReference)
- PHP_FE(sodium_pad, AI_StringAndLength)
- PHP_FE(sodium_unpad, AI_StringAndLength)
- /* codecs */
- PHP_FE(sodium_bin2hex, AI_String)
- PHP_FE(sodium_hex2bin, AI_StringAndMaybeString)
- #ifdef sodium_base64_VARIANT_ORIGINAL
- PHP_FE(sodium_bin2base64, AI_StringAndId)
- PHP_FE(sodium_base642bin, AI_StringAndIdAndMaybeString)
- #endif
- /* aliases */
- PHP_FALIAS(sodium_crypto_scalarmult_base, sodium_crypto_box_publickey_from_secretkey, AI_TwoStrings)
- PHP_FE_END
- };
- zend_module_entry sodium_module_entry = {
- STANDARD_MODULE_HEADER,
- "sodium",
- sodium_functions,
- PHP_MINIT(sodium),
- PHP_MSHUTDOWN(sodium),
- NULL,
- NULL,
- PHP_MINFO(sodium),
- PHP_SODIUM_VERSION,
- STANDARD_MODULE_PROPERTIES
- };
- /* }}} */
- #ifdef COMPILE_DL_SODIUM
- ZEND_GET_MODULE(sodium)
- #endif
- /* Remove argument information from backtrace to prevent information leaks */
- static void sodium_remove_param_values_from_backtrace(zend_object *obj) {
- zval obj_zv, rv, *trace;
- ZVAL_OBJ(&obj_zv, obj);
- trace = zend_read_property(zend_get_exception_base(&obj_zv), &obj_zv, "trace", sizeof("trace")-1, 0, &rv);
- if (trace && Z_TYPE_P(trace) == IS_ARRAY) {
- zval *frame;
- ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(trace), frame) {
- if (Z_TYPE_P(frame) == IS_ARRAY) {
- zval *args = zend_hash_str_find(Z_ARRVAL_P(frame), "args", sizeof("args")-1);
- if (args) {
- zval_ptr_dtor(args);
- ZVAL_EMPTY_ARRAY(args);
- }
- }
- } ZEND_HASH_FOREACH_END();
- }
- }
- static zend_object *sodium_exception_create_object(zend_class_entry *ce) {
- zend_object *obj = zend_ce_exception->create_object(ce);
- sodium_remove_param_values_from_backtrace(obj);
- return obj;
- }
- static void sodium_separate_string(zval *zv) {
- ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
- if (!Z_REFCOUNTED_P(zv) || Z_REFCOUNT_P(zv) > 1) {
- zend_string *copy = zend_string_init(Z_STRVAL_P(zv), Z_STRLEN_P(zv), 0);
- Z_TRY_DELREF_P(zv);
- ZVAL_STR(zv, copy);
- }
- }
- PHP_MINIT_FUNCTION(sodium)
- {
- zend_class_entry ce;
- if (sodium_init() < 0) {
- zend_error(E_ERROR, "sodium_init()");
- }
- INIT_CLASS_ENTRY(ce, "SodiumException", NULL);
- sodium_exception_ce = zend_register_internal_class_ex(&ce, zend_ce_exception);
- sodium_exception_ce->create_object = sodium_exception_create_object;
- REGISTER_STRING_CONSTANT("SODIUM_LIBRARY_VERSION",
- (char *) (void *) sodium_version_string(), CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_LIBRARY_MAJOR_VERSION",
- sodium_library_version_major(), CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_LIBRARY_MINOR_VERSION",
- sodium_library_version_minor(), CONST_CS | CONST_PERSISTENT);
- #ifdef HAVE_AESGCM
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_AES256GCM_KEYBYTES",
- crypto_aead_aes256gcm_KEYBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_AES256GCM_NSECBYTES",
- crypto_aead_aes256gcm_NSECBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES",
- crypto_aead_aes256gcm_NPUBBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_AES256GCM_ABYTES",
- crypto_aead_aes256gcm_ABYTES, CONST_CS | CONST_PERSISTENT);
- #endif
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES",
- crypto_aead_chacha20poly1305_KEYBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES",
- crypto_aead_chacha20poly1305_NSECBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES",
- crypto_aead_chacha20poly1305_NPUBBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_ABYTES",
- crypto_aead_chacha20poly1305_ABYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES",
- crypto_aead_chacha20poly1305_IETF_KEYBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_NSECBYTES",
- crypto_aead_chacha20poly1305_IETF_NSECBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES",
- crypto_aead_chacha20poly1305_IETF_NPUBBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES",
- crypto_aead_chacha20poly1305_IETF_ABYTES, CONST_CS | CONST_PERSISTENT);
- #ifdef crypto_aead_xchacha20poly1305_IETF_NPUBBYTES
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES",
- crypto_aead_xchacha20poly1305_IETF_KEYBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NSECBYTES",
- crypto_aead_xchacha20poly1305_IETF_NSECBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES",
- crypto_aead_xchacha20poly1305_IETF_NPUBBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES",
- crypto_aead_xchacha20poly1305_IETF_ABYTES, CONST_CS | CONST_PERSISTENT);
- #endif
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AUTH_BYTES",
- crypto_auth_BYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AUTH_KEYBYTES",
- crypto_auth_KEYBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_BOX_SEALBYTES",
- crypto_box_SEALBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_BOX_SECRETKEYBYTES",
- crypto_box_SECRETKEYBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_BOX_PUBLICKEYBYTES",
- crypto_box_PUBLICKEYBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_BOX_KEYPAIRBYTES",
- crypto_box_SECRETKEYBYTES + crypto_box_PUBLICKEYBYTES,
- CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_BOX_MACBYTES",
- crypto_box_MACBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_BOX_NONCEBYTES",
- crypto_box_NONCEBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_BOX_SEEDBYTES",
- crypto_box_SEEDBYTES, CONST_CS | CONST_PERSISTENT);
- #ifndef crypto_kdf_BYTES_MIN
- # define crypto_kdf_BYTES_MIN 16
- # define crypto_kdf_BYTES_MAX 64
- # define crypto_kdf_CONTEXTBYTES 8
- # define crypto_kdf_KEYBYTES 32
- #endif
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KDF_BYTES_MIN",
- crypto_kdf_BYTES_MIN, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KDF_BYTES_MAX",
- crypto_kdf_BYTES_MAX, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KDF_CONTEXTBYTES",
- crypto_kdf_CONTEXTBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KDF_KEYBYTES",
- crypto_kdf_KEYBYTES, CONST_CS | CONST_PERSISTENT);
- #ifndef crypto_kx_SEEDBYTES
- # define crypto_kx_SEEDBYTES 32
- # define crypto_kx_SESSIONKEYBYTES 32
- # define crypto_kx_PUBLICKEYBYTES 32
- # define crypto_kx_SECRETKEYBYTES 32
- #endif
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KX_SEEDBYTES",
- crypto_kx_SEEDBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KX_SESSIONKEYBYTES",
- crypto_kx_SESSIONKEYBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KX_PUBLICKEYBYTES",
- crypto_kx_PUBLICKEYBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KX_SECRETKEYBYTES",
- crypto_kx_SECRETKEYBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KX_KEYPAIRBYTES",
- crypto_kx_SECRETKEYBYTES + crypto_kx_PUBLICKEYBYTES,
- CONST_CS | CONST_PERSISTENT);
- #ifdef crypto_secretstream_xchacha20poly1305_ABYTES
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES",
- crypto_secretstream_xchacha20poly1305_ABYTES,
- CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES",
- crypto_secretstream_xchacha20poly1305_HEADERBYTES,
- CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES",
- crypto_secretstream_xchacha20poly1305_KEYBYTES,
- CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX",
- crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX,
- CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_MESSAGE",
- crypto_secretstream_xchacha20poly1305_TAG_MESSAGE,
- CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PUSH",
- crypto_secretstream_xchacha20poly1305_TAG_PUSH,
- CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY",
- crypto_secretstream_xchacha20poly1305_TAG_REKEY,
- CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL",
- crypto_secretstream_xchacha20poly1305_TAG_FINAL,
- CONST_CS | CONST_PERSISTENT);
- #endif
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_GENERICHASH_BYTES",
- crypto_generichash_BYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_GENERICHASH_BYTES_MIN",
- crypto_generichash_BYTES_MIN, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_GENERICHASH_BYTES_MAX",
- crypto_generichash_BYTES_MAX, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_GENERICHASH_KEYBYTES",
- crypto_generichash_KEYBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_GENERICHASH_KEYBYTES_MIN",
- crypto_generichash_KEYBYTES_MIN, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_GENERICHASH_KEYBYTES_MAX",
- crypto_generichash_KEYBYTES_MAX, CONST_CS | CONST_PERSISTENT);
- #ifdef crypto_pwhash_SALTBYTES
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_ALG_ARGON2I13",
- crypto_pwhash_ALG_ARGON2I13, CONST_CS | CONST_PERSISTENT);
- # ifdef crypto_pwhash_ALG_ARGON2ID13
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13",
- crypto_pwhash_ALG_ARGON2ID13, CONST_CS | CONST_PERSISTENT);
- # endif
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_ALG_DEFAULT",
- crypto_pwhash_ALG_DEFAULT, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_SALTBYTES",
- crypto_pwhash_SALTBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_STRING_CONSTANT("SODIUM_CRYPTO_PWHASH_STRPREFIX",
- crypto_pwhash_STRPREFIX, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE",
- crypto_pwhash_opslimit_interactive(), CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE",
- crypto_pwhash_memlimit_interactive(), CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_OPSLIMIT_MODERATE",
- crypto_pwhash_opslimit_moderate(), CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_MEMLIMIT_MODERATE",
- crypto_pwhash_memlimit_moderate(), CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_OPSLIMIT_SENSITIVE",
- crypto_pwhash_opslimit_sensitive(), CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_MEMLIMIT_SENSITIVE",
- crypto_pwhash_memlimit_sensitive(), CONST_CS | CONST_PERSISTENT);
- #endif
- #ifdef crypto_pwhash_scryptsalsa208sha256_SALTBYTES
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_SALTBYTES",
- crypto_pwhash_scryptsalsa208sha256_SALTBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_STRING_CONSTANT("SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_STRPREFIX",
- crypto_pwhash_scryptsalsa208sha256_STRPREFIX, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_INTERACTIVE",
- crypto_pwhash_scryptsalsa208sha256_opslimit_interactive(), CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_INTERACTIVE",
- crypto_pwhash_scryptsalsa208sha256_memlimit_interactive(), CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_SENSITIVE",
- crypto_pwhash_scryptsalsa208sha256_opslimit_sensitive(), CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_SENSITIVE",
- crypto_pwhash_scryptsalsa208sha256_memlimit_sensitive(), CONST_CS | CONST_PERSISTENT);
- #endif
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SCALARMULT_BYTES",
- crypto_scalarmult_BYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SCALARMULT_SCALARBYTES",
- crypto_scalarmult_SCALARBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SHORTHASH_BYTES",
- crypto_shorthash_BYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SHORTHASH_KEYBYTES",
- crypto_shorthash_KEYBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETBOX_KEYBYTES",
- crypto_secretbox_KEYBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETBOX_MACBYTES",
- crypto_secretbox_MACBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETBOX_NONCEBYTES",
- crypto_secretbox_NONCEBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SIGN_BYTES",
- crypto_sign_BYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SIGN_SEEDBYTES",
- crypto_sign_SEEDBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES",
- crypto_sign_PUBLICKEYBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SIGN_SECRETKEYBYTES",
- crypto_sign_SECRETKEYBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SIGN_KEYPAIRBYTES",
- crypto_sign_SECRETKEYBYTES + crypto_sign_PUBLICKEYBYTES,
- CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_STREAM_NONCEBYTES",
- crypto_stream_NONCEBYTES, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_STREAM_KEYBYTES",
- crypto_stream_KEYBYTES, CONST_CS | CONST_PERSISTENT);
- #ifdef sodium_base64_VARIANT_ORIGINAL
- REGISTER_LONG_CONSTANT("SODIUM_BASE64_VARIANT_ORIGINAL",
- sodium_base64_VARIANT_ORIGINAL, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING",
- sodium_base64_VARIANT_ORIGINAL_NO_PADDING, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_BASE64_VARIANT_URLSAFE",
- sodium_base64_VARIANT_URLSAFE, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING",
- sodium_base64_VARIANT_URLSAFE_NO_PADDING, CONST_CS | CONST_PERSISTENT);
- #endif
- return SUCCESS;
- }
- PHP_MSHUTDOWN_FUNCTION(sodium)
- {
- randombytes_close();
- return SUCCESS;
- }
- PHP_MINFO_FUNCTION(sodium)
- {
- php_info_print_table_start();
- php_info_print_table_header(2, "sodium support", "enabled");
- php_info_print_table_row(2, "libsodium headers version", SODIUM_VERSION_STRING);
- php_info_print_table_row(2, "libsodium library version", sodium_version_string());
- php_info_print_table_end();
- }
- PHP_FUNCTION(sodium_memzero)
- {
- zval *buf_zv;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(),
- "z", &buf_zv) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- ZVAL_DEREF(buf_zv);
- if (Z_TYPE_P(buf_zv) != IS_STRING) {
- zend_throw_exception(sodium_exception_ce, "a PHP string is required", 0);
- return;
- }
- if (Z_REFCOUNTED_P(buf_zv) && Z_REFCOUNT_P(buf_zv) == 1) {
- char *buf = Z_STRVAL(*buf_zv);
- size_t buf_len = Z_STRLEN(*buf_zv);
- if (buf_len > 0) {
- sodium_memzero(buf, (size_t) buf_len);
- }
- }
- convert_to_null(buf_zv);
- }
- PHP_FUNCTION(sodium_increment)
- {
- zval *val_zv;
- unsigned char *val;
- size_t val_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(),
- "z", &val_zv) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- ZVAL_DEREF(val_zv);
- if (Z_TYPE_P(val_zv) != IS_STRING) {
- zend_throw_exception(sodium_exception_ce, "a PHP string is required", 0);
- return;
- }
- sodium_separate_string(val_zv);
- val = (unsigned char *) Z_STRVAL(*val_zv);
- val_len = Z_STRLEN(*val_zv);
- sodium_increment(val, val_len);
- }
- PHP_FUNCTION(sodium_add)
- {
- zval *val_zv;
- unsigned char *val;
- unsigned char *addv;
- size_t val_len;
- size_t addv_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(),
- "zs", &val_zv, &addv, &addv_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- ZVAL_DEREF(val_zv);
- if (Z_TYPE_P(val_zv) != IS_STRING) {
- zend_throw_exception(sodium_exception_ce, "PHP strings are required", 0);
- return;
- }
- sodium_separate_string(val_zv);
- val = (unsigned char *) Z_STRVAL(*val_zv);
- val_len = Z_STRLEN(*val_zv);
- if (val_len != addv_len) {
- zend_throw_exception(sodium_exception_ce, "values must have the same length", 0);
- return;
- }
- sodium_add(val, addv, val_len);
- }
- PHP_FUNCTION(sodium_memcmp)
- {
- char *buf1;
- char *buf2;
- size_t len1;
- size_t len2;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ss",
- &buf1, &len1,
- &buf2, &len2) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (len1 != len2) {
- zend_throw_exception(sodium_exception_ce, "arguments have different sizes", 0);
- return;
- }
- RETURN_LONG(sodium_memcmp(buf1, buf2, len1));
- }
- PHP_FUNCTION(sodium_crypto_shorthash)
- {
- zend_string *hash;
- unsigned char *key;
- unsigned char *msg;
- size_t key_len;
- size_t msg_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ss",
- &msg, &msg_len,
- &key, &key_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (key_len != crypto_shorthash_KEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "key size should be SODIUM_CRYPTO_SHORTHASH_KEYBYTES bytes",
- 0);
- return;
- }
- hash = zend_string_alloc(crypto_shorthash_BYTES, 0);
- if (crypto_shorthash((unsigned char *) ZSTR_VAL(hash), msg,
- (unsigned long long) msg_len, key) != 0) {
- zend_string_efree(hash);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- ZSTR_VAL(hash)[crypto_shorthash_BYTES] = 0;
- RETURN_NEW_STR(hash);
- }
- PHP_FUNCTION(sodium_crypto_secretbox)
- {
- zend_string *ciphertext;
- unsigned char *key;
- unsigned char *msg;
- unsigned char *nonce;
- size_t key_len;
- size_t msg_len;
- size_t nonce_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "sss",
- &msg, &msg_len,
- &nonce, &nonce_len,
- &key, &key_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (nonce_len != crypto_secretbox_NONCEBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "nonce size should be SODIUM_CRYPTO_SECRETBOX_NONCEBYTES bytes",
- 0);
- return;
- }
- if (key_len != crypto_secretbox_KEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "key size should be SODIUM_CRYPTO_SECRETBOX_KEYBYTES bytes",
- 0);
- return;
- }
- if (SIZE_MAX - msg_len <= crypto_secretbox_MACBYTES) {
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- ciphertext = zend_string_alloc((size_t) msg_len + crypto_secretbox_MACBYTES, 0);
- if (crypto_secretbox_easy((unsigned char *) ZSTR_VAL(ciphertext),
- msg, (unsigned long long) msg_len,
- nonce, key) != 0) {
- zend_string_efree(ciphertext);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- ZSTR_VAL(ciphertext)[msg_len + crypto_secretbox_MACBYTES] = 0;
- RETURN_NEW_STR(ciphertext);
- }
- PHP_FUNCTION(sodium_crypto_secretbox_open)
- {
- zend_string *msg;
- unsigned char *key;
- unsigned char *ciphertext;
- unsigned char *nonce;
- size_t key_len;
- size_t ciphertext_len;
- size_t nonce_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "sss",
- &ciphertext, &ciphertext_len,
- &nonce, &nonce_len,
- &key, &key_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (nonce_len != crypto_secretbox_NONCEBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "nonce size should be SODIUM_CRYPTO_SECRETBOX_NONCEBYTES bytes",
- 0);
- return;
- }
- if (key_len != crypto_secretbox_KEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "key size should be SODIUM_CRYPTO_SECRETBOX_KEYBYTES bytes",
- 0);
- return;
- }
- if (ciphertext_len < crypto_secretbox_MACBYTES) {
- RETURN_FALSE;
- }
- msg = zend_string_alloc
- ((size_t) ciphertext_len - crypto_secretbox_MACBYTES, 0);
- if (crypto_secretbox_open_easy((unsigned char *) ZSTR_VAL(msg), ciphertext,
- (unsigned long long) ciphertext_len,
- nonce, key) != 0) {
- zend_string_efree(msg);
- RETURN_FALSE;
- } else {
- ZSTR_VAL(msg)[ciphertext_len - crypto_secretbox_MACBYTES] = 0;
- RETURN_NEW_STR(msg);
- }
- }
- PHP_FUNCTION(sodium_crypto_generichash)
- {
- zend_string *hash;
- unsigned char *key = NULL;
- unsigned char *msg;
- zend_long hash_len = crypto_generichash_BYTES;
- size_t key_len = 0;
- size_t msg_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s|sl",
- &msg, &msg_len,
- &key, &key_len,
- &hash_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (hash_len < crypto_generichash_BYTES_MIN ||
- hash_len > crypto_generichash_BYTES_MAX) {
- zend_throw_exception(sodium_exception_ce, "unsupported output length", 0);
- return;
- }
- if (key_len != 0 &&
- (key_len < crypto_generichash_KEYBYTES_MIN ||
- key_len > crypto_generichash_KEYBYTES_MAX)) {
- zend_throw_exception(sodium_exception_ce, "unsupported key length", 0);
- return;
- }
- hash = zend_string_alloc(hash_len, 0);
- if (crypto_generichash((unsigned char *) ZSTR_VAL(hash), (size_t) hash_len,
- msg, (unsigned long long) msg_len,
- key, (size_t) key_len) != 0) {
- zend_string_efree(hash);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- ZSTR_VAL(hash)[hash_len] = 0;
- RETURN_NEW_STR(hash);
- }
- PHP_FUNCTION(sodium_crypto_generichash_init)
- {
- crypto_generichash_state state_tmp;
- zend_string *state;
- unsigned char *key = NULL;
- size_t state_len = sizeof (crypto_generichash_state);
- zend_long hash_len = crypto_generichash_BYTES;
- size_t key_len = 0;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "|sl",
- &key, &key_len,
- &hash_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (hash_len < crypto_generichash_BYTES_MIN ||
- hash_len > crypto_generichash_BYTES_MAX) {
- zend_throw_exception(sodium_exception_ce, "unsupported output length", 0);
- return;
- }
- if (key_len != 0 &&
- (key_len < crypto_generichash_KEYBYTES_MIN ||
- key_len > crypto_generichash_KEYBYTES_MAX)) {
- zend_throw_exception(sodium_exception_ce, "unsupported key length", 0);
- return;
- }
- memset(&state_tmp, 0, sizeof state_tmp);
- if (crypto_generichash_init((void *) &state_tmp, key, (size_t) key_len,
- (size_t) hash_len) != 0) {
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- state = zend_string_alloc(state_len, 0);
- memcpy(ZSTR_VAL(state), &state_tmp, state_len);
- sodium_memzero(&state_tmp, sizeof state_tmp);
- ZSTR_VAL(state)[state_len] = 0;
- RETURN_STR(state);
- }
- PHP_FUNCTION(sodium_crypto_generichash_update)
- {
- crypto_generichash_state state_tmp;
- zval *state_zv;
- unsigned char *msg;
- unsigned char *state;
- size_t msg_len;
- size_t state_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "zs",
- &state_zv, &msg, &msg_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- ZVAL_DEREF(state_zv);
- if (Z_TYPE_P(state_zv) != IS_STRING) {
- zend_throw_exception(sodium_exception_ce, "a reference to a state is required", 0);
- return;
- }
- sodium_separate_string(state_zv);
- state = (unsigned char *) Z_STRVAL(*state_zv);
- state_len = Z_STRLEN(*state_zv);
- if (state_len != sizeof (crypto_generichash_state)) {
- zend_throw_exception(sodium_exception_ce, "incorrect state length", 0);
- return;
- }
- memcpy(&state_tmp, state, sizeof state_tmp);
- if (crypto_generichash_update((void *) &state_tmp, msg,
- (unsigned long long) msg_len) != 0) {
- sodium_memzero(&state_tmp, sizeof state_tmp);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- memcpy(state, &state_tmp, state_len);
- sodium_memzero(&state_tmp, sizeof state_tmp);
- RETURN_TRUE;
- }
- PHP_FUNCTION(sodium_crypto_generichash_final)
- {
- crypto_generichash_state state_tmp;
- zend_string *hash;
- zval *state_zv;
- unsigned char *state;
- size_t state_len;
- zend_long hash_len = crypto_generichash_BYTES;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "z|l",
- &state_zv, &hash_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- ZVAL_DEREF(state_zv);
- if (Z_TYPE_P(state_zv) != IS_STRING) {
- zend_throw_exception(sodium_exception_ce, "a reference to a state is required", 0);
- return;
- }
- sodium_separate_string(state_zv);
- state = (unsigned char *) Z_STRVAL(*state_zv);
- state_len = Z_STRLEN(*state_zv);
- if (state_len != sizeof (crypto_generichash_state)) {
- zend_throw_exception(sodium_exception_ce, "incorrect state length", 0);
- return;
- }
- if (hash_len < crypto_generichash_BYTES_MIN ||
- hash_len > crypto_generichash_BYTES_MAX) {
- zend_throw_exception(sodium_exception_ce, "unsupported output length", 0);
- return;
- }
- hash = zend_string_alloc(hash_len, 0);
- memcpy(&state_tmp, state, sizeof state_tmp);
- if (crypto_generichash_final((void *) &state_tmp,
- (unsigned char *) ZSTR_VAL(hash),
- (size_t) hash_len) != 0) {
- sodium_memzero(&state_tmp, sizeof state_tmp);
- zend_string_efree(hash);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- sodium_memzero(&state_tmp, sizeof state_tmp);
- sodium_memzero(state, state_len);
- convert_to_null(state_zv);
- ZSTR_VAL(hash)[hash_len] = 0;
- RETURN_NEW_STR(hash);
- }
- PHP_FUNCTION(sodium_crypto_box_keypair)
- {
- zend_string *keypair;
- size_t keypair_len;
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- keypair_len = crypto_box_SECRETKEYBYTES + crypto_box_PUBLICKEYBYTES;
- keypair = zend_string_alloc(keypair_len, 0);
- if (crypto_box_keypair((unsigned char *) ZSTR_VAL(keypair) +
- crypto_box_SECRETKEYBYTES,
- (unsigned char *) ZSTR_VAL(keypair)) != 0) {
- zend_string_efree(keypair);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- ZSTR_VAL(keypair)[keypair_len] = 0;
- RETURN_NEW_STR(keypair);
- }
- PHP_FUNCTION(sodium_crypto_box_seed_keypair)
- {
- zend_string *keypair;
- unsigned char *seed;
- size_t keypair_len;
- size_t seed_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s",
- &seed, &seed_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (seed_len != crypto_box_SEEDBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "seed should be SODIUM_CRYPTO_BOX_SEEDBYTES bytes",
- 0);
- return;
- }
- keypair_len = crypto_box_SECRETKEYBYTES + crypto_box_PUBLICKEYBYTES;
- keypair = zend_string_alloc(keypair_len, 0);
- if (crypto_box_seed_keypair((unsigned char *) ZSTR_VAL(keypair) +
- crypto_box_SECRETKEYBYTES,
- (unsigned char *) ZSTR_VAL(keypair),
- seed) != 0) {
- zend_string_efree(keypair);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- ZSTR_VAL(keypair)[keypair_len] = 0;
- RETURN_NEW_STR(keypair);
- }
- PHP_FUNCTION(sodium_crypto_box_keypair_from_secretkey_and_publickey)
- {
- zend_string *keypair;
- char *publickey;
- char *secretkey;
- size_t keypair_len;
- size_t publickey_len;
- size_t secretkey_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ss",
- &secretkey, &secretkey_len,
- &publickey, &publickey_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (secretkey_len != crypto_box_SECRETKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "secretkey should be SODIUM_CRYPTO_BOX_SECRETKEYBYTES bytes",
- 0);
- return;
- }
- if (publickey_len != crypto_box_PUBLICKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "publickey should be SODIUM_CRYPTO_BOX_PUBLICKEYBYTES bytes",
- 0);
- return;
- }
- keypair_len = crypto_box_SECRETKEYBYTES + crypto_box_PUBLICKEYBYTES;
- keypair = zend_string_alloc(keypair_len, 0);
- memcpy(ZSTR_VAL(keypair), secretkey, crypto_box_SECRETKEYBYTES);
- memcpy(ZSTR_VAL(keypair) + crypto_box_SECRETKEYBYTES, publickey,
- crypto_box_PUBLICKEYBYTES);
- ZSTR_VAL(keypair)[keypair_len] = 0;
- RETURN_STR(keypair);
- }
- PHP_FUNCTION(sodium_crypto_box_secretkey)
- {
- zend_string *secretkey;
- unsigned char *keypair;
- size_t keypair_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s",
- &keypair, &keypair_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (keypair_len !=
- crypto_box_SECRETKEYBYTES + crypto_box_PUBLICKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "keypair should be SODIUM_CRYPTO_BOX_KEYPAIRBYTES bytes",
- 0);
- return;
- }
- secretkey = zend_string_alloc(crypto_box_SECRETKEYBYTES, 0);
- memcpy(ZSTR_VAL(secretkey), keypair, crypto_box_SECRETKEYBYTES);
- ZSTR_VAL(secretkey)[crypto_box_SECRETKEYBYTES] = 0;
- RETURN_STR(secretkey);
- }
- PHP_FUNCTION(sodium_crypto_box_publickey)
- {
- zend_string *publickey;
- unsigned char *keypair;
- size_t keypair_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s",
- &keypair, &keypair_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (keypair_len !=
- crypto_box_SECRETKEYBYTES + crypto_box_PUBLICKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "keypair should be SODIUM_CRYPTO_BOX_KEYPAIRBYTES bytes",
- 0);
- return;
- }
- publickey = zend_string_alloc(crypto_box_PUBLICKEYBYTES, 0);
- memcpy(ZSTR_VAL(publickey), keypair + crypto_box_SECRETKEYBYTES,
- crypto_box_PUBLICKEYBYTES);
- ZSTR_VAL(publickey)[crypto_box_PUBLICKEYBYTES] = 0;
- RETURN_STR(publickey);
- }
- PHP_FUNCTION(sodium_crypto_box_publickey_from_secretkey)
- {
- zend_string *publickey;
- unsigned char *secretkey;
- size_t secretkey_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s",
- &secretkey, &secretkey_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (secretkey_len != crypto_box_SECRETKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "key should be SODIUM_CRYPTO_BOX_SECRETKEYBYTES bytes",
- 0);
- return;
- }
- publickey = zend_string_alloc(crypto_box_PUBLICKEYBYTES, 0);
- (void) sizeof(int[crypto_scalarmult_BYTES ==
- crypto_box_PUBLICKEYBYTES ? 1 : -1]);
- (void) sizeof(int[crypto_scalarmult_SCALARBYTES ==
- crypto_box_SECRETKEYBYTES ? 1 : -1]);
- crypto_scalarmult_base((unsigned char *) ZSTR_VAL(publickey), secretkey);
- ZSTR_VAL(publickey)[crypto_box_PUBLICKEYBYTES] = 0;
- RETURN_STR(publickey);
- }
- PHP_FUNCTION(sodium_crypto_box)
- {
- zend_string *ciphertext;
- unsigned char *keypair;
- unsigned char *msg;
- unsigned char *nonce;
- unsigned char *publickey;
- unsigned char *secretkey;
- size_t keypair_len;
- size_t msg_len;
- size_t nonce_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "sss",
- &msg, &msg_len,
- &nonce, &nonce_len,
- &keypair, &keypair_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (nonce_len != crypto_box_NONCEBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "nonce size should be SODIUM_CRYPTO_BOX_NONCEBYTES bytes",
- 0);
- return;
- }
- if (keypair_len != crypto_box_SECRETKEYBYTES + crypto_box_PUBLICKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "keypair size should be SODIUM_CRYPTO_BOX_KEYPAIRBYTES bytes",
- 0);
- return;
- }
- secretkey = keypair;
- publickey = keypair + crypto_box_SECRETKEYBYTES;
- if (SIZE_MAX - msg_len <= crypto_box_MACBYTES) {
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- ciphertext = zend_string_alloc((size_t) msg_len + crypto_box_MACBYTES, 0);
- if (crypto_box_easy((unsigned char *) ZSTR_VAL(ciphertext), msg,
- (unsigned long long) msg_len,
- nonce, publickey, secretkey) != 0) {
- zend_string_efree(ciphertext);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- ZSTR_VAL(ciphertext)[msg_len + crypto_box_MACBYTES] = 0;
- RETURN_NEW_STR(ciphertext);
- }
- PHP_FUNCTION(sodium_crypto_box_open)
- {
- zend_string *msg;
- unsigned char *ciphertext;
- unsigned char *keypair;
- unsigned char *nonce;
- unsigned char *publickey;
- unsigned char *secretkey;
- size_t ciphertext_len;
- size_t keypair_len;
- size_t nonce_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "sss",
- &ciphertext, &ciphertext_len,
- &nonce, &nonce_len,
- &keypair, &keypair_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (nonce_len != crypto_box_NONCEBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "nonce size should be SODIUM_CRYPTO_BOX_NONCEBYTES bytes",
- 0);
- return;
- }
- if (keypair_len != crypto_box_SECRETKEYBYTES + crypto_box_PUBLICKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "keypair size should be SODIUM_CRYPTO_BOX_KEYPAIRBYTES bytes",
- 0);
- return;
- }
- secretkey = keypair;
- publickey = keypair + crypto_box_SECRETKEYBYTES;
- if (ciphertext_len < crypto_box_MACBYTES) {
- RETURN_FALSE;
- }
- msg = zend_string_alloc((size_t) ciphertext_len - crypto_box_MACBYTES, 0);
- if (crypto_box_open_easy((unsigned char *) ZSTR_VAL(msg), ciphertext,
- (unsigned long long) ciphertext_len,
- nonce, publickey, secretkey) != 0) {
- zend_string_efree(msg);
- RETURN_FALSE;
- } else {
- ZSTR_VAL(msg)[ciphertext_len - crypto_box_MACBYTES] = 0;
- RETURN_NEW_STR(msg);
- }
- }
- PHP_FUNCTION(sodium_crypto_box_seal)
- {
- zend_string *ciphertext;
- unsigned char *msg;
- unsigned char *publickey;
- size_t msg_len;
- size_t publickey_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ss",
- &msg, &msg_len,
- &publickey, &publickey_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (publickey_len != crypto_box_PUBLICKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "public key size should be SODIUM_CRYPTO_BOX_PUBLICKEYBYTES bytes",
- 0);
- return;
- }
- if (SIZE_MAX - msg_len <= crypto_box_SEALBYTES) {
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- ciphertext = zend_string_alloc((size_t) msg_len + crypto_box_SEALBYTES, 0);
- if (crypto_box_seal((unsigned char *) ZSTR_VAL(ciphertext), msg,
- (unsigned long long) msg_len, publickey) != 0) {
- zend_string_efree(ciphertext);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- ZSTR_VAL(ciphertext)[msg_len + crypto_box_SEALBYTES] = 0;
- RETURN_NEW_STR(ciphertext);
- }
- PHP_FUNCTION(sodium_crypto_box_seal_open)
- {
- zend_string *msg;
- unsigned char *ciphertext;
- unsigned char *keypair;
- unsigned char *publickey;
- unsigned char *secretkey;
- size_t ciphertext_len;
- size_t keypair_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ss",
- &ciphertext, &ciphertext_len,
- &keypair, &keypair_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (keypair_len != crypto_box_SECRETKEYBYTES + crypto_box_PUBLICKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "keypair size should be SODIUM_CRYPTO_BOX_KEYPAIRBYTES bytes",
- 0);
- return;
- }
- secretkey = keypair;
- publickey = keypair + crypto_box_SECRETKEYBYTES;
- if (ciphertext_len < crypto_box_SEALBYTES) {
- RETURN_FALSE;
- }
- msg = zend_string_alloc((size_t) ciphertext_len - crypto_box_SEALBYTES, 0);
- if (crypto_box_seal_open((unsigned char *) ZSTR_VAL(msg), ciphertext,
- (unsigned long long) ciphertext_len,
- publickey, secretkey) != 0) {
- zend_string_efree(msg);
- RETURN_FALSE;
- } else {
- ZSTR_VAL(msg)[ciphertext_len - crypto_box_SEALBYTES] = 0;
- RETURN_NEW_STR(msg);
- }
- }
- PHP_FUNCTION(sodium_crypto_sign_keypair)
- {
- zend_string *keypair;
- size_t keypair_len;
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- keypair_len = crypto_sign_SECRETKEYBYTES + crypto_sign_PUBLICKEYBYTES;
- keypair = zend_string_alloc(keypair_len, 0);
- if (crypto_sign_keypair((unsigned char *) ZSTR_VAL(keypair) +
- crypto_sign_SECRETKEYBYTES,
- (unsigned char *) ZSTR_VAL(keypair)) != 0) {
- zend_string_efree(keypair);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- ZSTR_VAL(keypair)[keypair_len] = 0;
- RETURN_NEW_STR(keypair);
- }
- PHP_FUNCTION(sodium_crypto_sign_seed_keypair)
- {
- zend_string *keypair;
- unsigned char *seed;
- size_t keypair_len;
- size_t seed_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s",
- &seed, &seed_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (seed_len != crypto_sign_SEEDBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "seed should be SODIUM_CRYPTO_SIGN_SEEDBYTES bytes",
- 0);
- return;
- }
- keypair_len = crypto_sign_SECRETKEYBYTES + crypto_sign_PUBLICKEYBYTES;
- keypair = zend_string_alloc(keypair_len, 0);
- if (crypto_sign_seed_keypair((unsigned char *) ZSTR_VAL(keypair) +
- crypto_sign_SECRETKEYBYTES,
- (unsigned char *) ZSTR_VAL(keypair),
- seed) != 0) {
- zend_string_efree(keypair);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- ZSTR_VAL(keypair)[keypair_len] = 0;
- RETURN_NEW_STR(keypair);
- }
- PHP_FUNCTION(sodium_crypto_sign_keypair_from_secretkey_and_publickey)
- {
- zend_string *keypair;
- char *publickey;
- char *secretkey;
- size_t keypair_len;
- size_t publickey_len;
- size_t secretkey_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ss",
- &secretkey, &secretkey_len,
- &publickey, &publickey_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (secretkey_len != crypto_sign_SECRETKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "secretkey should be SODIUM_CRYPTO_SIGN_SECRETKEYBYTES bytes",
- 0);
- return;
- }
- if (publickey_len != crypto_sign_PUBLICKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "publickey should be SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES bytes",
- 0);
- return;
- }
- keypair_len = crypto_sign_SECRETKEYBYTES + crypto_sign_PUBLICKEYBYTES;
- keypair = zend_string_alloc(keypair_len, 0);
- memcpy(ZSTR_VAL(keypair), secretkey, crypto_sign_SECRETKEYBYTES);
- memcpy(ZSTR_VAL(keypair) + crypto_sign_SECRETKEYBYTES, publickey,
- crypto_sign_PUBLICKEYBYTES);
- ZSTR_VAL(keypair)[keypair_len] = 0;
- RETURN_STR(keypair);
- }
- PHP_FUNCTION(sodium_crypto_sign_publickey_from_secretkey)
- {
- zend_string *publickey;
- char *secretkey;
- size_t secretkey_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s",
- &secretkey, &secretkey_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (secretkey_len != crypto_sign_SECRETKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "secretkey should be SODIUM_CRYPTO_SIGN_SECRETKEYBYTES bytes",
- 0);
- return;
- }
- publickey = zend_string_alloc(crypto_sign_PUBLICKEYBYTES, 0);
- if (crypto_sign_ed25519_sk_to_pk((unsigned char *) ZSTR_VAL(publickey),
- (const unsigned char *) secretkey) != 0) {
- zend_throw_exception(sodium_exception_ce,
- "internal error", 0);
- return;
- }
- ZSTR_VAL(publickey)[crypto_sign_PUBLICKEYBYTES] = 0;
- RETURN_STR(publickey);
- }
- PHP_FUNCTION(sodium_crypto_sign_secretkey)
- {
- zend_string *secretkey;
- unsigned char *keypair;
- size_t keypair_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s",
- &keypair, &keypair_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (keypair_len !=
- crypto_sign_SECRETKEYBYTES + crypto_sign_PUBLICKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "keypair should be SODIUM_CRYPTO_SIGN_KEYPAIRBYTES bytes",
- 0);
- return;
- }
- secretkey = zend_string_alloc(crypto_sign_SECRETKEYBYTES, 0);
- memcpy(ZSTR_VAL(secretkey), keypair, crypto_sign_SECRETKEYBYTES);
- ZSTR_VAL(secretkey)[crypto_sign_SECRETKEYBYTES] = 0;
- RETURN_STR(secretkey);
- }
- PHP_FUNCTION(sodium_crypto_sign_publickey)
- {
- zend_string *publickey;
- unsigned char *keypair;
- size_t keypair_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s",
- &keypair, &keypair_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (keypair_len !=
- crypto_sign_SECRETKEYBYTES + crypto_sign_PUBLICKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "keypair should be SODIUM_CRYPTO_SIGN_KEYPAIRBYTES bytes",
- 0);
- return;
- }
- publickey = zend_string_alloc(crypto_sign_PUBLICKEYBYTES, 0);
- memcpy(ZSTR_VAL(publickey), keypair + crypto_sign_SECRETKEYBYTES,
- crypto_sign_PUBLICKEYBYTES);
- ZSTR_VAL(publickey)[crypto_sign_PUBLICKEYBYTES] = 0;
- RETURN_STR(publickey);
- }
- PHP_FUNCTION(sodium_crypto_sign)
- {
- zend_string *msg_signed;
- unsigned char *msg;
- unsigned char *secretkey;
- unsigned long long msg_signed_real_len;
- size_t msg_len;
- size_t msg_signed_len;
- size_t secretkey_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ss",
- &msg, &msg_len,
- &secretkey, &secretkey_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (secretkey_len != crypto_sign_SECRETKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "secret key size should be SODIUM_CRYPTO_SIGN_SECRETKEYBYTES bytes",
- 0);
- return;
- }
- if (SIZE_MAX - msg_len <= crypto_sign_BYTES) {
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- msg_signed_len = msg_len + crypto_sign_BYTES;
- msg_signed = zend_string_alloc((size_t) msg_signed_len, 0);
- if (crypto_sign((unsigned char *) ZSTR_VAL(msg_signed),
- &msg_signed_real_len, msg,
- (unsigned long long) msg_len, secretkey) != 0) {
- zend_string_efree(msg_signed);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- if (msg_signed_real_len >= SIZE_MAX || msg_signed_real_len > msg_signed_len) {
- zend_string_efree(msg_signed);
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- PHP_SODIUM_ZSTR_TRUNCATE(msg_signed, (size_t) msg_signed_real_len);
- ZSTR_VAL(msg_signed)[msg_signed_real_len] = 0;
- RETURN_NEW_STR(msg_signed);
- }
- PHP_FUNCTION(sodium_crypto_sign_open)
- {
- zend_string *msg;
- unsigned char *msg_signed;
- unsigned char *publickey;
- unsigned long long msg_real_len;
- size_t msg_len;
- size_t msg_signed_len;
- size_t publickey_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ss",
- &msg_signed, &msg_signed_len,
- &publickey, &publickey_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (publickey_len != crypto_sign_PUBLICKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "public key size should be SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES bytes",
- 0);
- return;
- }
- msg_len = msg_signed_len;
- if (msg_len >= SIZE_MAX) {
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- msg = zend_string_alloc((size_t) msg_len, 0);
- if (crypto_sign_open((unsigned char *) ZSTR_VAL(msg), &msg_real_len,
- msg_signed, (unsigned long long) msg_signed_len,
- publickey) != 0) {
- zend_string_efree(msg);
- RETURN_FALSE;
- }
- if (msg_real_len >= SIZE_MAX || msg_real_len > msg_signed_len) {
- zend_string_efree(msg);
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- PHP_SODIUM_ZSTR_TRUNCATE(msg, (size_t) msg_real_len);
- ZSTR_VAL(msg)[msg_real_len] = 0;
- RETURN_NEW_STR(msg);
- }
- PHP_FUNCTION(sodium_crypto_sign_detached)
- {
- zend_string *signature;
- unsigned char *msg;
- unsigned char *secretkey;
- unsigned long long signature_real_len;
- size_t msg_len;
- size_t secretkey_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ss",
- &msg, &msg_len,
- &secretkey, &secretkey_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (secretkey_len != crypto_sign_SECRETKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "secret key size should be SODIUM_CRYPTO_SIGN_SECRETKEYBYTES bytes",
- 0);
- return;
- }
- signature = zend_string_alloc((size_t) crypto_sign_BYTES, 0);
- memset(ZSTR_VAL(signature), 0, (size_t) crypto_sign_BYTES);
- if (crypto_sign_detached((unsigned char *) ZSTR_VAL(signature),
- &signature_real_len, msg,
- (unsigned long long) msg_len, secretkey) != 0) {
- zend_string_efree(signature);
- zend_throw_exception(sodium_exception_ce, "signature creation failed", 0);
- return;
- }
- if (signature_real_len <= 0U || signature_real_len > crypto_sign_BYTES) {
- zend_string_efree(signature);
- zend_throw_exception(sodium_exception_ce, "signature has a bogus size", 0);
- return;
- }
- PHP_SODIUM_ZSTR_TRUNCATE(signature, (size_t) signature_real_len);
- ZSTR_VAL(signature)[signature_real_len] = 0;
- RETURN_NEW_STR(signature);
- }
- PHP_FUNCTION(sodium_crypto_sign_verify_detached)
- {
- unsigned char *msg;
- unsigned char *publickey;
- unsigned char *signature;
- size_t msg_len;
- size_t publickey_len;
- size_t signature_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "sss",
- &signature, &signature_len,
- &msg, &msg_len,
- &publickey, &publickey_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (signature_len != crypto_sign_BYTES) {
- zend_throw_exception(sodium_exception_ce,
- "signature size should be SODIUM_CRYPTO_SIGN_BYTES bytes",
- 0);
- return;
- }
- if (publickey_len != crypto_sign_PUBLICKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "public key size should be SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES bytes",
- 0);
- return;
- }
- if (crypto_sign_verify_detached(signature,
- msg, (unsigned long long) msg_len,
- publickey) != 0) {
- RETURN_FALSE;
- }
- RETURN_TRUE;
- }
- PHP_FUNCTION(sodium_crypto_stream)
- {
- zend_string *ciphertext;
- unsigned char *key;
- unsigned char *nonce;
- zend_long ciphertext_len;
- size_t key_len;
- size_t nonce_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "lss",
- &ciphertext_len,
- &nonce, &nonce_len,
- &key, &key_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (ciphertext_len <= 0 || ciphertext_len >= SIZE_MAX) {
- zend_throw_exception(sodium_exception_ce, "ciphertext length must be greater than 0", 0);
- return;
- }
- if (nonce_len != crypto_stream_NONCEBYTES) {
- zend_throw_exception(sodium_exception_ce, "nonce should be SODIUM_CRYPTO_STREAM_NONCEBYTES bytes", 0);
- return;
- }
- if (key_len != crypto_stream_KEYBYTES) {
- zend_throw_exception(sodium_exception_ce, "key should be SODIUM_CRYPTO_STREAM_KEYBYTES bytes", 0);
- return;
- }
- ciphertext = zend_string_alloc((size_t) ciphertext_len, 0);
- if (crypto_stream((unsigned char *) ZSTR_VAL(ciphertext),
- (unsigned long long) ciphertext_len, nonce, key) != 0) {
- zend_string_efree(ciphertext);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- ZSTR_VAL(ciphertext)[ciphertext_len] = 0;
- RETURN_NEW_STR(ciphertext);
- }
- PHP_FUNCTION(sodium_crypto_stream_xor)
- {
- zend_string *ciphertext;
- unsigned char *key;
- unsigned char *msg;
- unsigned char *nonce;
- size_t ciphertext_len;
- size_t key_len;
- size_t msg_len;
- size_t nonce_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "sss",
- &msg, &msg_len,
- &nonce, &nonce_len,
- &key, &key_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (nonce_len != crypto_stream_NONCEBYTES) {
- zend_throw_exception(sodium_exception_ce, "nonce should be SODIUM_CRYPTO_STREAM_NONCEBYTES bytes", 0);
- return;
- }
- if (key_len != crypto_stream_KEYBYTES) {
- zend_throw_exception(sodium_exception_ce, "key should be SODIUM_CRYPTO_STREAM_KEYBYTES bytes", 0);
- return;
- }
- ciphertext_len = msg_len;
- ciphertext = zend_string_alloc((size_t) ciphertext_len, 0);
- if (crypto_stream_xor((unsigned char *) ZSTR_VAL(ciphertext), msg,
- (unsigned long long) msg_len, nonce, key) != 0) {
- zend_string_efree(ciphertext);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- ZSTR_VAL(ciphertext)[ciphertext_len] = 0;
- RETURN_NEW_STR(ciphertext);
- }
- #ifdef crypto_pwhash_SALTBYTES
- PHP_FUNCTION(sodium_crypto_pwhash)
- {
- zend_string *hash;
- unsigned char *salt;
- char *passwd;
- zend_long hash_len;
- zend_long memlimit;
- zend_long opslimit;
- zend_long alg;
- size_t passwd_len;
- size_t salt_len;
- int ret;
- alg = (zend_long) crypto_pwhash_ALG_DEFAULT;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "lssll|l",
- &hash_len,
- &passwd, &passwd_len,
- &salt, &salt_len,
- &opslimit, &memlimit, &alg) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (hash_len <= 0 || hash_len >= 0xffffffff) {
- zend_throw_exception(sodium_exception_ce, "hash length must be greater than 0", 0);
- return;
- }
- if (passwd_len >= 0xffffffff) {
- zend_throw_exception(sodium_exception_ce, "unsupported password length", 0);
- return;
- }
- if (opslimit <= 0) {
- zend_throw_exception(sodium_exception_ce, "ops limit must be greater than 0", 0);
- return;
- }
- if (memlimit <= 0 || memlimit > SIZE_MAX) {
- zend_throw_exception(sodium_exception_ce, "memory limit must be greater than 0", 0);
- return;
- }
- if (alg != crypto_pwhash_ALG_ARGON2I13
- # ifdef crypto_pwhash_ALG_ARGON2ID13
- && alg != crypto_pwhash_ALG_ARGON2ID13
- # endif
- && alg != crypto_pwhash_ALG_DEFAULT) {
- zend_throw_exception(sodium_exception_ce, "unsupported password hashing algorithm", 0);
- return;
- }
- if (passwd_len <= 0) {
- zend_error(E_WARNING, "empty password");
- }
- if (salt_len != crypto_pwhash_SALTBYTES) {
- zend_throw_exception(sodium_exception_ce, "salt should be SODIUM_CRYPTO_PWHASH_SALTBYTES bytes", 0);
- return;
- }
- if (opslimit < crypto_pwhash_OPSLIMIT_MIN) {
- zend_throw_exception(sodium_exception_ce,
- "number of operations for the password hashing function is too low", 0);
- return;
- }
- if (memlimit < crypto_pwhash_MEMLIMIT_MIN) {
- zend_throw_exception(sodium_exception_ce,
- "maximum memory for the password hashing function is too low", 0);
- }
- hash = zend_string_alloc((size_t) hash_len, 0);
- ret = -1;
- # ifdef crypto_pwhash_ALG_ARGON2ID13
- if (alg == crypto_pwhash_ALG_ARGON2ID13) {
- ret = crypto_pwhash_argon2id
- ((unsigned char *) ZSTR_VAL(hash), (unsigned long long) hash_len,
- passwd, (unsigned long long) passwd_len, salt,
- (unsigned long long) opslimit, (size_t) memlimit, (int) alg);
- }
- # endif
- if (ret == -1) {
- ret = crypto_pwhash
- ((unsigned char *) ZSTR_VAL(hash), (unsigned long long) hash_len,
- passwd, (unsigned long long) passwd_len, salt,
- (unsigned long long) opslimit, (size_t) memlimit, (int) alg);
- }
- if (ret != 0) {
- zend_string_efree(hash);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- ZSTR_VAL(hash)[hash_len] = 0;
- RETURN_NEW_STR(hash);
- }
- PHP_FUNCTION(sodium_crypto_pwhash_str)
- {
- zend_string *hash_str;
- char *passwd;
- zend_long memlimit;
- zend_long opslimit;
- size_t passwd_len;
- size_t len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "sll",
- &passwd, &passwd_len,
- &opslimit, &memlimit) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (opslimit <= 0) {
- zend_throw_exception(sodium_exception_ce, "ops limit must be greater than 0", 0);
- return;
- }
- if (memlimit <= 0 || memlimit > SIZE_MAX) {
- zend_throw_exception(sodium_exception_ce, "memory limit must be greater than 0", 0);
- return;
- }
- if (passwd_len >= 0xffffffff) {
- zend_throw_exception(sodium_exception_ce, "unsupported password length", 0);
- return;
- }
- if (passwd_len <= 0) {
- zend_error(E_WARNING, "empty password");
- }
- if (opslimit < crypto_pwhash_OPSLIMIT_MIN) {
- zend_throw_exception(sodium_exception_ce,
- "number of operations for the password hashing function is too low", 0);
- }
- if (memlimit < crypto_pwhash_MEMLIMIT_MIN) {
- zend_throw_exception(sodium_exception_ce,
- "maximum memory for the password hashing function is too low", 0);
- }
- hash_str = zend_string_alloc(crypto_pwhash_STRBYTES - 1, 0);
- if (crypto_pwhash_str
- (ZSTR_VAL(hash_str), passwd, (unsigned long long) passwd_len,
- (unsigned long long) opslimit, (size_t) memlimit) != 0) {
- zend_string_efree(hash_str);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- ZSTR_VAL(hash_str)[crypto_pwhash_STRBYTES - 1] = 0;
- len = strlen(ZSTR_VAL(hash_str));
- PHP_SODIUM_ZSTR_TRUNCATE(hash_str, len);
- RETURN_NEW_STR(hash_str);
- }
- #if SODIUM_LIBRARY_VERSION_MAJOR > 9 || (SODIUM_LIBRARY_VERSION_MAJOR == 9 && SODIUM_LIBRARY_VERSION_MINOR >= 6)
- PHP_FUNCTION(sodium_crypto_pwhash_str_needs_rehash)
- {
- char *hash_str;
- zend_long memlimit;
- zend_long opslimit;
- size_t hash_str_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "sll",
- &hash_str, &hash_str_len, &opslimit, &memlimit) == FAILURE) {
- zend_throw_exception(sodium_exception_ce, "a PHP string is required", 0);
- return;
- }
- if (crypto_pwhash_str_needs_rehash(hash_str, opslimit, memlimit) == 0) {
- RETURN_FALSE;
- }
- RETURN_TRUE;
- }
- #endif
- PHP_FUNCTION(sodium_crypto_pwhash_str_verify)
- {
- char *hash_str;
- char *passwd;
- size_t hash_str_len;
- size_t passwd_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ss",
- &hash_str, &hash_str_len,
- &passwd, &passwd_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (passwd_len >= 0xffffffff) {
- zend_throw_exception(sodium_exception_ce,
- "unsupported password length", 0);
- return;
- }
- if (passwd_len <= 0) {
- zend_error(E_WARNING, "empty password");
- }
- if (crypto_pwhash_str_verify
- (hash_str, passwd, (unsigned long long) passwd_len) == 0) {
- RETURN_TRUE;
- }
- RETURN_FALSE;
- }
- #endif
- #ifdef crypto_pwhash_scryptsalsa208sha256_SALTBYTES
- PHP_FUNCTION(sodium_crypto_pwhash_scryptsalsa208sha256)
- {
- zend_string *hash;
- unsigned char *salt;
- char *passwd;
- zend_long hash_len;
- zend_long memlimit;
- zend_long opslimit;
- size_t passwd_len;
- size_t salt_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "lssll",
- &hash_len,
- &passwd, &passwd_len,
- &salt, &salt_len,
- &opslimit, &memlimit) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (hash_len <= 0 || hash_len >= SIZE_MAX || hash_len > 0x1fffffffe0ULL) {
- zend_throw_exception(sodium_exception_ce, "hash length must be greater than 0", 0);
- return;
- }
- if (opslimit <= 0) {
- zend_throw_exception(sodium_exception_ce, "ops limit must be greater than 0", 0);
- return;
- }
- if (memlimit <= 0 || memlimit > SIZE_MAX) {
- zend_throw_exception(sodium_exception_ce, "memory limit must be greater than 0", 0);
- return;
- }
- if (passwd_len <= 0) {
- zend_error(E_WARNING, "empty password");
- }
- if (salt_len != crypto_pwhash_scryptsalsa208sha256_SALTBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "salt should be SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_SALTBYTES bytes",
- 0);
- return;
- }
- if (opslimit < crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE) {
- zend_throw_exception(sodium_exception_ce,
- "number of operations for the scrypt function is too low", 0);
- }
- if (memlimit < crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE) {
- zend_throw_exception(sodium_exception_ce,
- "maximum memory for the scrypt function is too low", 0);
- }
- hash = zend_string_alloc((size_t) hash_len, 0);
- if (crypto_pwhash_scryptsalsa208sha256
- ((unsigned char *) ZSTR_VAL(hash), (unsigned long long) hash_len,
- passwd, (unsigned long long) passwd_len, salt,
- (unsigned long long) opslimit, (size_t) memlimit) != 0) {
- zend_string_efree(hash);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- ZSTR_VAL(hash)[hash_len] = 0;
- RETURN_NEW_STR(hash);
- }
- PHP_FUNCTION(sodium_crypto_pwhash_scryptsalsa208sha256_str)
- {
- zend_string *hash_str;
- char *passwd;
- zend_long memlimit;
- zend_long opslimit;
- size_t passwd_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "sll",
- &passwd, &passwd_len,
- &opslimit, &memlimit) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (opslimit <= 0) {
- zend_throw_exception(sodium_exception_ce, "ops limit must be greater than 0", 0);
- return;
- }
- if (memlimit <= 0 || memlimit > SIZE_MAX) {
- zend_throw_exception(sodium_exception_ce, "memory limit must be greater than 0", 0);
- return;
- }
- if (passwd_len <= 0) {
- zend_error(E_WARNING, "empty password");
- }
- if (opslimit < crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE) {
- zend_throw_exception(sodium_exception_ce,
- "number of operations for the scrypt function is too low", 0);
- }
- if (memlimit < crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE) {
- zend_throw_exception(sodium_exception_ce,
- "maximum memory for the scrypt function is too low", 0);
- }
- hash_str = zend_string_alloc
- (crypto_pwhash_scryptsalsa208sha256_STRBYTES - 1, 0);
- if (crypto_pwhash_scryptsalsa208sha256_str
- (ZSTR_VAL(hash_str), passwd, (unsigned long long) passwd_len,
- (unsigned long long) opslimit, (size_t) memlimit) != 0) {
- zend_string_efree(hash_str);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- ZSTR_VAL(hash_str)[crypto_pwhash_scryptsalsa208sha256_STRBYTES - 1] = 0;
- RETURN_NEW_STR(hash_str);
- }
- PHP_FUNCTION(sodium_crypto_pwhash_scryptsalsa208sha256_str_verify)
- {
- char *hash_str;
- char *passwd;
- size_t hash_str_len;
- size_t passwd_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ss",
- &hash_str, &hash_str_len,
- &passwd, &passwd_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (passwd_len <= 0) {
- zend_error(E_WARNING, "empty password");
- }
- if (hash_str_len != crypto_pwhash_scryptsalsa208sha256_STRBYTES - 1) {
- zend_error(E_WARNING, "wrong size for the hashed password");
- RETURN_FALSE;
- }
- if (crypto_pwhash_scryptsalsa208sha256_str_verify
- (hash_str, passwd, (unsigned long long) passwd_len) == 0) {
- RETURN_TRUE;
- }
- RETURN_FALSE;
- }
- #endif
- PHP_FUNCTION(sodium_crypto_aead_aes256gcm_is_available)
- {
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- #ifdef HAVE_AESGCM
- RETURN_BOOL(crypto_aead_aes256gcm_is_available());
- #else
- RETURN_FALSE;
- #endif
- }
- #ifdef HAVE_AESGCM
- PHP_FUNCTION(sodium_crypto_aead_aes256gcm_encrypt)
- {
- zend_string *ciphertext;
- unsigned char *ad;
- unsigned char *msg;
- unsigned char *npub;
- unsigned char *secretkey;
- unsigned long long ciphertext_real_len;
- size_t ad_len;
- size_t ciphertext_len;
- size_t msg_len;
- size_t npub_len;
- size_t secretkey_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ssss",
- &msg, &msg_len,
- &ad, &ad_len,
- &npub, &npub_len,
- &secretkey, &secretkey_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (npub_len != crypto_aead_aes256gcm_NPUBBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "public nonce size should be "
- "SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES bytes",
- 0);
- return;
- }
- if (secretkey_len != crypto_aead_aes256gcm_KEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "secret key size should be "
- "SODIUM_CRYPTO_AEAD_AES256GCM_KEYBYTES bytes",
- 0);
- return;
- }
- if (SIZE_MAX - msg_len <= crypto_aead_aes256gcm_ABYTES) {
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- if ((unsigned long long) msg_len > (16ULL * ((1ULL << 32) - 2ULL)) - crypto_aead_aes256gcm_ABYTES) {
- zend_throw_exception(sodium_exception_ce, "message too long for a single key", 0);
- return;
- }
- ciphertext_len = msg_len + crypto_aead_aes256gcm_ABYTES;
- ciphertext = zend_string_alloc((size_t) ciphertext_len, 0);
- if (crypto_aead_aes256gcm_encrypt
- ((unsigned char *) ZSTR_VAL(ciphertext), &ciphertext_real_len, msg,
- (unsigned long long) msg_len,
- ad, (unsigned long long) ad_len, NULL, npub, secretkey) != 0) {
- zend_string_efree(ciphertext);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- if (ciphertext_real_len <= 0U || ciphertext_real_len >= SIZE_MAX ||
- ciphertext_real_len > ciphertext_len) {
- zend_string_efree(ciphertext);
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- PHP_SODIUM_ZSTR_TRUNCATE(ciphertext, (size_t) ciphertext_real_len);
- ZSTR_VAL(ciphertext)[ciphertext_real_len] = 0;
- RETURN_NEW_STR(ciphertext);
- }
- PHP_FUNCTION(sodium_crypto_aead_aes256gcm_decrypt)
- {
- zend_string *msg;
- unsigned char *ad;
- unsigned char *ciphertext;
- unsigned char *npub;
- unsigned char *secretkey;
- unsigned long long msg_real_len;
- size_t ad_len;
- size_t ciphertext_len;
- size_t msg_len;
- size_t npub_len;
- size_t secretkey_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ssss",
- &ciphertext, &ciphertext_len,
- &ad, &ad_len,
- &npub, &npub_len,
- &secretkey, &secretkey_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (npub_len != crypto_aead_aes256gcm_NPUBBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "public nonce size should be "
- "SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES bytes",
- 0);
- return;
- }
- if (secretkey_len != crypto_aead_aes256gcm_KEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "secret key size should be "
- "SODIUM_CRYPTO_AEAD_AES256GCM_KEYBYTES bytes",
- 0);
- return;
- }
- if (ciphertext_len < crypto_aead_aes256gcm_ABYTES) {
- RETURN_FALSE;
- }
- if (ciphertext_len - crypto_aead_aes256gcm_ABYTES > 16ULL * ((1ULL << 32) - 2ULL)) {
- zend_throw_exception(sodium_exception_ce, "message too long for a single key", 0);
- return;
- }
- msg_len = ciphertext_len;
- if (msg_len >= SIZE_MAX) {
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- msg = zend_string_alloc((size_t) msg_len, 0);
- if (crypto_aead_aes256gcm_decrypt
- ((unsigned char *) ZSTR_VAL(msg), &msg_real_len, NULL,
- ciphertext, (unsigned long long) ciphertext_len,
- ad, (unsigned long long) ad_len, npub, secretkey) != 0) {
- zend_string_efree(msg);
- RETURN_FALSE;
- }
- if (msg_real_len >= SIZE_MAX || msg_real_len > msg_len) {
- zend_string_efree(msg);
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- PHP_SODIUM_ZSTR_TRUNCATE(msg, (size_t) msg_real_len);
- ZSTR_VAL(msg)[msg_real_len] = 0;
- RETURN_NEW_STR(msg);
- }
- #endif
- PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_encrypt)
- {
- zend_string *ciphertext;
- unsigned char *ad;
- unsigned char *msg;
- unsigned char *npub;
- unsigned char *secretkey;
- unsigned long long ciphertext_real_len;
- size_t ad_len;
- size_t ciphertext_len;
- size_t msg_len;
- size_t npub_len;
- size_t secretkey_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ssss",
- &msg, &msg_len,
- &ad, &ad_len,
- &npub, &npub_len,
- &secretkey, &secretkey_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (npub_len != crypto_aead_chacha20poly1305_NPUBBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "public nonce size should be "
- "SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES bytes",
- 0);
- return;
- }
- if (secretkey_len != crypto_aead_chacha20poly1305_KEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "secret key size should be "
- "SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES bytes",
- 0);
- return;
- }
- if (SIZE_MAX - msg_len <= crypto_aead_chacha20poly1305_ABYTES) {
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- ciphertext_len = msg_len + crypto_aead_chacha20poly1305_ABYTES;
- ciphertext = zend_string_alloc((size_t) ciphertext_len, 0);
- if (crypto_aead_chacha20poly1305_encrypt
- ((unsigned char *) ZSTR_VAL(ciphertext), &ciphertext_real_len, msg,
- (unsigned long long) msg_len,
- ad, (unsigned long long) ad_len, NULL, npub, secretkey) != 0) {
- zend_string_efree(ciphertext);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- if (ciphertext_real_len <= 0U || ciphertext_real_len >= SIZE_MAX ||
- ciphertext_real_len > ciphertext_len) {
- zend_string_efree(ciphertext);
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- PHP_SODIUM_ZSTR_TRUNCATE(ciphertext, (size_t) ciphertext_real_len);
- ZSTR_VAL(ciphertext)[ciphertext_real_len] = 0;
- RETURN_NEW_STR(ciphertext);
- }
- PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_decrypt)
- {
- zend_string *msg;
- unsigned char *ad;
- unsigned char *ciphertext;
- unsigned char *npub;
- unsigned char *secretkey;
- unsigned long long msg_real_len;
- size_t ad_len;
- size_t ciphertext_len;
- size_t msg_len;
- size_t npub_len;
- size_t secretkey_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ssss",
- &ciphertext, &ciphertext_len,
- &ad, &ad_len,
- &npub, &npub_len,
- &secretkey, &secretkey_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (npub_len != crypto_aead_chacha20poly1305_NPUBBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "public nonce size should be "
- "SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES bytes",
- 0);
- return;
- }
- if (secretkey_len != crypto_aead_chacha20poly1305_KEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "secret key size should be "
- "SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES bytes",
- 0);
- return;
- }
- if (ciphertext_len < crypto_aead_chacha20poly1305_ABYTES) {
- RETURN_FALSE;
- }
- msg_len = ciphertext_len;
- if (msg_len >= SIZE_MAX) {
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- msg = zend_string_alloc((size_t) msg_len, 0);
- if (crypto_aead_chacha20poly1305_decrypt
- ((unsigned char *) ZSTR_VAL(msg), &msg_real_len, NULL,
- ciphertext, (unsigned long long) ciphertext_len,
- ad, (unsigned long long) ad_len, npub, secretkey) != 0) {
- zend_string_efree(msg);
- RETURN_FALSE;
- }
- if (msg_real_len >= SIZE_MAX || msg_real_len > msg_len) {
- zend_string_efree(msg);
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- PHP_SODIUM_ZSTR_TRUNCATE(msg, (size_t) msg_real_len);
- ZSTR_VAL(msg)[msg_real_len] = 0;
- RETURN_NEW_STR(msg);
- }
- PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_ietf_encrypt)
- {
- zend_string *ciphertext;
- unsigned char *ad;
- unsigned char *msg;
- unsigned char *npub;
- unsigned char *secretkey;
- unsigned long long ciphertext_real_len;
- size_t ad_len;
- size_t ciphertext_len;
- size_t msg_len;
- size_t npub_len;
- size_t secretkey_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ssss",
- &msg, &msg_len,
- &ad, &ad_len,
- &npub, &npub_len,
- &secretkey, &secretkey_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (npub_len != crypto_aead_chacha20poly1305_IETF_NPUBBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "public nonce size should be "
- "SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES bytes",
- 0);
- return;
- }
- if (secretkey_len != crypto_aead_chacha20poly1305_IETF_KEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "secret key size should be "
- "SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES bytes",
- 0);
- return;
- }
- if (SIZE_MAX - msg_len <= crypto_aead_chacha20poly1305_IETF_ABYTES) {
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- if ((unsigned long long) msg_len > 64ULL * (1ULL << 32) - 64ULL) {
- zend_throw_exception(sodium_exception_ce, "message too long for a single key", 0);
- return;
- }
- ciphertext_len = msg_len + crypto_aead_chacha20poly1305_IETF_ABYTES;
- ciphertext = zend_string_alloc((size_t) ciphertext_len, 0);
- if (crypto_aead_chacha20poly1305_ietf_encrypt
- ((unsigned char *) ZSTR_VAL(ciphertext), &ciphertext_real_len, msg,
- (unsigned long long) msg_len,
- ad, (unsigned long long) ad_len, NULL, npub, secretkey) != 0) {
- zend_string_efree(ciphertext);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- if (ciphertext_real_len <= 0U || ciphertext_real_len >= SIZE_MAX ||
- ciphertext_real_len > ciphertext_len) {
- zend_string_efree(ciphertext);
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- PHP_SODIUM_ZSTR_TRUNCATE(ciphertext, (size_t) ciphertext_real_len);
- ZSTR_VAL(ciphertext)[ciphertext_real_len] = 0;
- RETURN_NEW_STR(ciphertext);
- }
- PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_ietf_decrypt)
- {
- zend_string *msg;
- unsigned char *ad;
- unsigned char *ciphertext;
- unsigned char *npub;
- unsigned char *secretkey;
- unsigned long long msg_real_len;
- size_t ad_len;
- size_t ciphertext_len;
- size_t msg_len;
- size_t npub_len;
- size_t secretkey_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ssss",
- &ciphertext, &ciphertext_len,
- &ad, &ad_len,
- &npub, &npub_len,
- &secretkey, &secretkey_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (npub_len != crypto_aead_chacha20poly1305_IETF_NPUBBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "public nonce size should be "
- "SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES bytes",
- 0);
- return;
- }
- if (secretkey_len != crypto_aead_chacha20poly1305_IETF_KEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "secret key size should be "
- "SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES bytes",
- 0);
- return;
- }
- msg_len = ciphertext_len;
- if (msg_len >= SIZE_MAX) {
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- if (ciphertext_len < crypto_aead_chacha20poly1305_IETF_ABYTES) {
- RETURN_FALSE;
- }
- if ((unsigned long long) ciphertext_len -
- crypto_aead_chacha20poly1305_IETF_ABYTES > 64ULL * (1ULL << 32) - 64ULL) {
- zend_throw_exception(sodium_exception_ce, "message too long for a single key", 0);
- return;
- }
- msg = zend_string_alloc((size_t) msg_len, 0);
- if (crypto_aead_chacha20poly1305_ietf_decrypt
- ((unsigned char *) ZSTR_VAL(msg), &msg_real_len, NULL,
- ciphertext, (unsigned long long) ciphertext_len,
- ad, (unsigned long long) ad_len, npub, secretkey) != 0) {
- zend_string_efree(msg);
- RETURN_FALSE;
- }
- if (msg_real_len >= SIZE_MAX || msg_real_len > msg_len) {
- zend_string_efree(msg);
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- PHP_SODIUM_ZSTR_TRUNCATE(msg, (size_t) msg_real_len);
- ZSTR_VAL(msg)[msg_real_len] = 0;
- RETURN_NEW_STR(msg);
- }
- #ifdef crypto_aead_xchacha20poly1305_IETF_NPUBBYTES
- PHP_FUNCTION(sodium_crypto_aead_xchacha20poly1305_ietf_encrypt)
- {
- zend_string *ciphertext;
- unsigned char *ad;
- unsigned char *msg;
- unsigned char *npub;
- unsigned char *secretkey;
- unsigned long long ciphertext_real_len;
- size_t ad_len;
- size_t ciphertext_len;
- size_t msg_len;
- size_t npub_len;
- size_t secretkey_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ssss",
- &msg, &msg_len,
- &ad, &ad_len,
- &npub, &npub_len,
- &secretkey, &secretkey_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (npub_len != crypto_aead_xchacha20poly1305_IETF_NPUBBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "public nonce size should be "
- "SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES bytes",
- 0);
- return;
- }
- if (secretkey_len != crypto_aead_xchacha20poly1305_IETF_KEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "secret key size should be "
- "SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES bytes",
- 0);
- return;
- }
- if (SIZE_MAX - msg_len <= crypto_aead_xchacha20poly1305_IETF_ABYTES) {
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- ciphertext_len = msg_len + crypto_aead_xchacha20poly1305_IETF_ABYTES;
- ciphertext = zend_string_alloc((size_t) ciphertext_len, 0);
- if (crypto_aead_xchacha20poly1305_ietf_encrypt
- ((unsigned char *) ZSTR_VAL(ciphertext), &ciphertext_real_len, msg,
- (unsigned long long) msg_len,
- ad, (unsigned long long) ad_len, NULL, npub, secretkey) != 0) {
- zend_string_efree(ciphertext);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- if (ciphertext_real_len <= 0U || ciphertext_real_len >= SIZE_MAX ||
- ciphertext_real_len > ciphertext_len) {
- zend_string_efree(ciphertext);
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- PHP_SODIUM_ZSTR_TRUNCATE(ciphertext, (size_t) ciphertext_real_len);
- ZSTR_VAL(ciphertext)[ciphertext_real_len] = 0;
- RETURN_NEW_STR(ciphertext);
- }
- PHP_FUNCTION(sodium_crypto_aead_xchacha20poly1305_ietf_decrypt)
- {
- zend_string *msg;
- unsigned char *ad;
- unsigned char *ciphertext;
- unsigned char *npub;
- unsigned char *secretkey;
- unsigned long long msg_real_len;
- size_t ad_len;
- size_t ciphertext_len;
- size_t msg_len;
- size_t npub_len;
- size_t secretkey_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ssss",
- &ciphertext, &ciphertext_len,
- &ad, &ad_len,
- &npub, &npub_len,
- &secretkey, &secretkey_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (npub_len != crypto_aead_xchacha20poly1305_IETF_NPUBBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "public nonce size should be "
- "SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES bytes",
- 0);
- return;
- }
- if (secretkey_len != crypto_aead_xchacha20poly1305_IETF_KEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "secret key size should be "
- "SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES bytes",
- 0);
- return;
- }
- if (ciphertext_len < crypto_aead_xchacha20poly1305_IETF_ABYTES) {
- RETURN_FALSE;
- }
- msg_len = ciphertext_len;
- if (msg_len - crypto_aead_xchacha20poly1305_IETF_ABYTES >= SIZE_MAX) {
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- if ((unsigned long long) ciphertext_len -
- crypto_aead_xchacha20poly1305_IETF_ABYTES > 64ULL * (1ULL << 32) - 64ULL) {
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- msg = zend_string_alloc((size_t) msg_len, 0);
- if (crypto_aead_xchacha20poly1305_ietf_decrypt
- ((unsigned char *) ZSTR_VAL(msg), &msg_real_len, NULL,
- ciphertext, (unsigned long long) ciphertext_len,
- ad, (unsigned long long) ad_len, npub, secretkey) != 0) {
- zend_string_efree(msg);
- RETURN_FALSE;
- }
- if (msg_real_len >= SIZE_MAX || msg_real_len > msg_len) {
- zend_string_efree(msg);
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- PHP_SODIUM_ZSTR_TRUNCATE(msg, (size_t) msg_real_len);
- ZSTR_VAL(msg)[msg_real_len] = 0;
- RETURN_NEW_STR(msg);
- }
- #endif
- PHP_FUNCTION(sodium_bin2hex)
- {
- zend_string *hex;
- unsigned char *bin;
- size_t bin_len;
- size_t hex_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s",
- &bin, &bin_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (bin_len >= SIZE_MAX / 2U) {
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- hex_len = bin_len * 2U;
- hex = zend_string_alloc((size_t) hex_len, 0);
- sodium_bin2hex(ZSTR_VAL(hex), hex_len + 1U, bin, bin_len);
- ZSTR_VAL(hex)[hex_len] = 0;
- RETURN_STR(hex);
- }
- PHP_FUNCTION(sodium_hex2bin)
- {
- zend_string *bin;
- const char *end;
- char *hex;
- char *ignore = NULL;
- size_t bin_real_len;
- size_t bin_len;
- size_t hex_len;
- size_t ignore_len = 0;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s|s",
- &hex, &hex_len,
- &ignore, &ignore_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- bin_len = hex_len / 2;
- bin = zend_string_alloc(bin_len, 0);
- if (sodium_hex2bin((unsigned char *) ZSTR_VAL(bin), bin_len, hex, hex_len,
- ignore, &bin_real_len, &end) != 0 ||
- end != hex + hex_len) {
- zend_string_efree(bin);
- zend_throw_exception(sodium_exception_ce, "invalid hex string", 0);
- return;
- }
- if (bin_real_len >= SIZE_MAX || bin_real_len > bin_len) {
- zend_string_efree(bin);
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- PHP_SODIUM_ZSTR_TRUNCATE(bin, (size_t) bin_real_len);
- ZSTR_VAL(bin)[bin_real_len] = 0;
- RETURN_NEW_STR(bin);
- }
- #ifdef sodium_base64_VARIANT_ORIGINAL
- PHP_FUNCTION(sodium_bin2base64)
- {
- zend_string *b64;
- unsigned char *bin;
- zend_long variant;
- size_t bin_len;
- size_t b64_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "sl",
- &bin, &bin_len, &variant) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if ((((unsigned int) variant) & ~ 0x6U) != 0x1U) {
- zend_throw_exception(sodium_exception_ce,
- "invalid base64 variant identifier", 0);
- return;
- }
- if (bin_len >= SIZE_MAX / 4U * 3U - 3U - 1U) {
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- b64_len = sodium_base64_ENCODED_LEN(bin_len, variant);
- b64 = zend_string_alloc((size_t) b64_len - 1U, 0);
- sodium_bin2base64(ZSTR_VAL(b64), b64_len, bin, bin_len, (int) variant);
- RETURN_STR(b64);
- }
- PHP_FUNCTION(sodium_base642bin)
- {
- zend_string *bin;
- char *b64;
- const char *end;
- char *ignore = NULL;
- zend_long variant;
- size_t bin_real_len;
- size_t bin_len;
- size_t b64_len;
- size_t ignore_len = 0;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "sl|s",
- &b64, &b64_len, &variant,
- &ignore, &ignore_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if ((((unsigned int) variant) & ~ 0x6U) != 0x1U) {
- zend_throw_exception(sodium_exception_ce,
- "invalid base64 variant identifier", 0);
- return;
- }
- bin_len = b64_len / 4U * 3U + 2U;
- bin = zend_string_alloc(bin_len, 0);
- if (sodium_base642bin((unsigned char *) ZSTR_VAL(bin), bin_len,
- b64, b64_len,
- ignore, &bin_real_len, &end, (int) variant) != 0 ||
- end != b64 + b64_len) {
- zend_string_efree(bin);
- zend_throw_exception(sodium_exception_ce, "invalid base64 string", 0);
- return;
- }
- if (bin_real_len >= SIZE_MAX || bin_real_len > bin_len) {
- zend_string_efree(bin);
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- PHP_SODIUM_ZSTR_TRUNCATE(bin, (size_t) bin_real_len);
- ZSTR_VAL(bin)[bin_real_len] = 0;
- RETURN_NEW_STR(bin);
- }
- #endif
- PHP_FUNCTION(sodium_crypto_scalarmult)
- {
- zend_string *q;
- unsigned char *n;
- unsigned char *p;
- size_t n_len;
- size_t p_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ss",
- &n, &n_len, &p, &p_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (n_len != crypto_scalarmult_SCALARBYTES ||
- p_len != crypto_scalarmult_BYTES) {
- zend_throw_exception(sodium_exception_ce, "scalar and point must be "
- "SODIUM_CRYPTO_SCALARMULT_SCALARBYTES bytes",
- 0);
- return;
- }
- q = zend_string_alloc(crypto_scalarmult_BYTES, 0);
- if (crypto_scalarmult((unsigned char *) ZSTR_VAL(q), n, p) != 0) {
- zend_string_efree(q);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- ZSTR_VAL(q)[crypto_scalarmult_BYTES] = 0;
- RETURN_NEW_STR(q);
- }
- PHP_FUNCTION(sodium_crypto_kx_seed_keypair)
- {
- unsigned char *sk;
- unsigned char *pk;
- unsigned char *seed;
- size_t seed_len;
- zend_string *keypair;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s",
- &seed, &seed_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (seed_len != crypto_kx_SEEDBYTES) {
- zend_throw_exception(sodium_exception_ce, "seed must be SODIUM_CRYPTO_KX_SEEDBYTES bytes", 0);
- return;
- }
- (void) sizeof(int[crypto_scalarmult_SCALARBYTES == crypto_kx_PUBLICKEYBYTES ? 1 : -1]);
- (void) sizeof(int[crypto_scalarmult_SCALARBYTES == crypto_kx_SECRETKEYBYTES ? 1 : -1]);
- keypair = zend_string_alloc(crypto_kx_SECRETKEYBYTES + crypto_kx_PUBLICKEYBYTES, 0);
- sk = (unsigned char *) ZSTR_VAL(keypair);
- pk = sk + crypto_kx_SECRETKEYBYTES;
- crypto_generichash(sk, crypto_kx_SECRETKEYBYTES,
- seed, crypto_kx_SEEDBYTES, NULL, 0);
- if (crypto_scalarmult_base(pk, sk) != 0) {
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- ZSTR_VAL(keypair)[crypto_kx_SECRETKEYBYTES + crypto_kx_PUBLICKEYBYTES] = 0;
- RETURN_STR(keypair);
- }
- PHP_FUNCTION(sodium_crypto_kx_keypair)
- {
- unsigned char *sk;
- unsigned char *pk;
- zend_string *keypair;
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- keypair = zend_string_alloc(crypto_kx_SECRETKEYBYTES + crypto_kx_PUBLICKEYBYTES, 0);
- sk = (unsigned char *) ZSTR_VAL(keypair);
- pk = sk + crypto_kx_SECRETKEYBYTES;
- randombytes_buf(sk, crypto_kx_SECRETKEYBYTES);
- if (crypto_scalarmult_base(pk, sk) != 0) {
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- ZSTR_VAL(keypair)[crypto_kx_SECRETKEYBYTES + crypto_kx_PUBLICKEYBYTES] = 0;
- RETURN_STR(keypair);
- }
- PHP_FUNCTION(sodium_crypto_kx_secretkey)
- {
- zend_string *secretkey;
- unsigned char *keypair;
- size_t keypair_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s",
- &keypair, &keypair_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (keypair_len !=
- crypto_kx_SECRETKEYBYTES + crypto_kx_PUBLICKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "keypair should be SODIUM_CRYPTO_KX_KEYPAIRBYTES bytes",
- 0);
- return;
- }
- secretkey = zend_string_alloc(crypto_kx_SECRETKEYBYTES, 0);
- memcpy(ZSTR_VAL(secretkey), keypair, crypto_kx_SECRETKEYBYTES);
- ZSTR_VAL(secretkey)[crypto_kx_SECRETKEYBYTES] = 0;
- RETURN_STR(secretkey);
- }
- PHP_FUNCTION(sodium_crypto_kx_publickey)
- {
- zend_string *publickey;
- unsigned char *keypair;
- size_t keypair_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s",
- &keypair, &keypair_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (keypair_len !=
- crypto_kx_SECRETKEYBYTES + crypto_kx_PUBLICKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "keypair should be SODIUM_CRYPTO_KX_KEYPAIRBYTES bytes",
- 0);
- return;
- }
- publickey = zend_string_alloc(crypto_kx_PUBLICKEYBYTES, 0);
- memcpy(ZSTR_VAL(publickey), keypair + crypto_kx_SECRETKEYBYTES,
- crypto_kx_PUBLICKEYBYTES);
- ZSTR_VAL(publickey)[crypto_kx_PUBLICKEYBYTES] = 0;
- RETURN_STR(publickey);
- }
- PHP_FUNCTION(sodium_crypto_kx_client_session_keys)
- {
- crypto_generichash_state h;
- unsigned char q[crypto_scalarmult_BYTES];
- unsigned char *keypair;
- unsigned char *client_sk;
- unsigned char *client_pk;
- unsigned char *server_pk;
- unsigned char session_keys[2 * crypto_kx_SESSIONKEYBYTES];
- size_t keypair_len;
- size_t server_pk_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ss",
- &keypair, &keypair_len,
- &server_pk, &server_pk_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (keypair_len != crypto_kx_SECRETKEYBYTES + crypto_kx_PUBLICKEYBYTES) {
- zend_throw_exception(sodium_exception_ce, "keypair must be SODIUM_CRYPTO_KX_KEYPAIRBYTES bytes", 0);
- return;
- }
- if (server_pk_len != crypto_kx_PUBLICKEYBYTES) {
- zend_throw_exception(sodium_exception_ce, "public keys must be SODIUM_CRYPTO_KX_PUBLICKEYBYTES bytes", 0);
- return;
- }
- client_sk = &keypair[0];
- client_pk = &keypair[crypto_kx_SECRETKEYBYTES];
- (void) sizeof(int[crypto_scalarmult_SCALARBYTES == crypto_kx_PUBLICKEYBYTES ? 1 : -1]);
- (void) sizeof(int[crypto_scalarmult_SCALARBYTES == crypto_kx_SECRETKEYBYTES ? 1 : -1]);
- if (crypto_scalarmult(q, client_sk, server_pk) != 0) {
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- crypto_generichash_init(&h, NULL, 0U, 2 * crypto_kx_SESSIONKEYBYTES);
- crypto_generichash_update(&h, q, sizeof q);
- sodium_memzero(q, sizeof q);
- crypto_generichash_update(&h, client_pk, crypto_kx_PUBLICKEYBYTES);
- crypto_generichash_update(&h, server_pk, crypto_kx_PUBLICKEYBYTES);
- crypto_generichash_final(&h, session_keys, 2 * crypto_kx_SESSIONKEYBYTES);
- sodium_memzero(&h, sizeof h);
- array_init(return_value);
- add_next_index_stringl(return_value,
- (const char *) session_keys,
- crypto_kx_SESSIONKEYBYTES);
- add_next_index_stringl(return_value,
- (const char *) session_keys + crypto_kx_SESSIONKEYBYTES,
- crypto_kx_SESSIONKEYBYTES);
- }
- PHP_FUNCTION(sodium_crypto_kx_server_session_keys)
- {
- crypto_generichash_state h;
- unsigned char q[crypto_scalarmult_BYTES];
- unsigned char *keypair;
- unsigned char *server_sk;
- unsigned char *server_pk;
- unsigned char *client_pk;
- unsigned char session_keys[2 * crypto_kx_SESSIONKEYBYTES];
- size_t keypair_len;
- size_t client_pk_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ss",
- &keypair, &keypair_len,
- &client_pk, &client_pk_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (keypair_len != crypto_kx_SECRETKEYBYTES + crypto_kx_PUBLICKEYBYTES) {
- zend_throw_exception(sodium_exception_ce, "keypair must be SODIUM_CRYPTO_KX_KEYPAIRBYTES bytes", 0);
- return;
- }
- if (client_pk_len != crypto_kx_PUBLICKEYBYTES) {
- zend_throw_exception(sodium_exception_ce, "public keys must be SODIUM_CRYPTO_KX_PUBLICKEYBYTES bytes", 0);
- return;
- }
- server_sk = &keypair[0];
- server_pk = &keypair[crypto_kx_SECRETKEYBYTES];
- (void) sizeof(int[crypto_scalarmult_SCALARBYTES == crypto_kx_PUBLICKEYBYTES ? 1 : -1]);
- (void) sizeof(int[crypto_scalarmult_SCALARBYTES == crypto_kx_SECRETKEYBYTES ? 1 : -1]);
- if (crypto_scalarmult(q, server_sk, client_pk) != 0) {
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- crypto_generichash_init(&h, NULL, 0U, 2 * crypto_kx_SESSIONKEYBYTES);
- crypto_generichash_update(&h, q, sizeof q);
- sodium_memzero(q, sizeof q);
- crypto_generichash_update(&h, client_pk, crypto_kx_PUBLICKEYBYTES);
- crypto_generichash_update(&h, server_pk, crypto_kx_PUBLICKEYBYTES);
- crypto_generichash_final(&h, session_keys, 2 * crypto_kx_SESSIONKEYBYTES);
- sodium_memzero(&h, sizeof h);
- array_init(return_value);
- add_next_index_stringl(return_value,
- (const char *) session_keys + crypto_kx_SESSIONKEYBYTES,
- crypto_kx_SESSIONKEYBYTES);
- add_next_index_stringl(return_value,
- (const char *) session_keys,
- crypto_kx_SESSIONKEYBYTES);
- }
- PHP_FUNCTION(sodium_crypto_auth)
- {
- zend_string *mac;
- char *key;
- char *msg;
- size_t msg_len;
- size_t key_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ss",
- &msg, &msg_len,
- &key, &key_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (key_len != crypto_auth_KEYBYTES) {
- zend_throw_exception(sodium_exception_ce, "key must be SODIUM_CRYPTO_AUTH_KEYBYTES bytes", 0);
- return;
- }
- mac = zend_string_alloc(crypto_auth_BYTES, 0);
- if (crypto_auth((unsigned char *) ZSTR_VAL(mac),
- (const unsigned char *) msg, msg_len,
- (const unsigned char *) key) != 0) {
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- ZSTR_VAL(mac)[crypto_auth_BYTES] = 0;
- RETURN_STR(mac);
- }
- PHP_FUNCTION(sodium_crypto_auth_verify)
- {
- char *mac;
- char *key;
- char *msg;
- size_t mac_len;
- size_t msg_len;
- size_t key_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "sss",
- &mac, &mac_len,
- &msg, &msg_len,
- &key, &key_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (key_len != crypto_auth_KEYBYTES) {
- zend_throw_exception(sodium_exception_ce, "key must be SODIUM_CRYPTO_AUTH_KEYBYTES bytes", 0);
- return;
- }
- if (mac_len != crypto_auth_BYTES) {
- zend_throw_exception(sodium_exception_ce, "authentication tag must be SODIUM_CRYPTO_AUTH_BYTES bytes", 0);
- return;
- }
- if (crypto_auth_verify((const unsigned char *) mac,
- (const unsigned char *) msg, msg_len,
- (const unsigned char *) key) != 0) {
- RETURN_FALSE;
- }
- RETURN_TRUE;
- }
- PHP_FUNCTION(sodium_crypto_sign_ed25519_sk_to_curve25519)
- {
- zend_string *ecdhkey;
- char *eddsakey;
- size_t eddsakey_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s",
- &eddsakey, &eddsakey_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (eddsakey_len != crypto_sign_SECRETKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "Ed25519 key should be SODIUM_CRYPTO_SIGN_SECRETKEYBYTES bytes",
- 0);
- return;
- }
- ecdhkey = zend_string_alloc(crypto_box_SECRETKEYBYTES, 0);
- if (crypto_sign_ed25519_sk_to_curve25519((unsigned char *) ZSTR_VAL(ecdhkey),
- (const unsigned char *) eddsakey) != 0) {
- zend_throw_exception(sodium_exception_ce, "conversion failed", 0);
- return;
- }
- ZSTR_VAL(ecdhkey)[crypto_box_SECRETKEYBYTES] = 0;
- RETURN_STR(ecdhkey);
- }
- PHP_FUNCTION(sodium_crypto_sign_ed25519_pk_to_curve25519)
- {
- zend_string *ecdhkey;
- char *eddsakey;
- size_t eddsakey_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s",
- &eddsakey, &eddsakey_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (eddsakey_len != crypto_sign_PUBLICKEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "Ed25519 key should be SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES bytes",
- 0);
- return;
- }
- ecdhkey = zend_string_alloc(crypto_sign_PUBLICKEYBYTES, 0);
- if (crypto_sign_ed25519_pk_to_curve25519((unsigned char *) ZSTR_VAL(ecdhkey),
- (const unsigned char *) eddsakey) != 0) {
- zend_throw_exception(sodium_exception_ce, "conversion failed", 0);
- return;
- }
- ZSTR_VAL(ecdhkey)[crypto_box_PUBLICKEYBYTES] = 0;
- RETURN_STR(ecdhkey);
- }
- PHP_FUNCTION(sodium_compare)
- {
- char *buf1;
- char *buf2;
- size_t len1;
- size_t len2;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ss",
- &buf1, &len1,
- &buf2, &len2) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (len1 != len2) {
- zend_throw_exception(sodium_exception_ce, "arguments have different sizes", 0);
- return;
- } else {
- RETURN_LONG(sodium_compare((const unsigned char *) buf1,
- (const unsigned char *) buf2, (size_t) len1));
- }
- }
- #ifdef HAVE_AESGCM
- PHP_FUNCTION(sodium_crypto_aead_aes256gcm_keygen)
- {
- unsigned char key[crypto_aead_aes256gcm_KEYBYTES];
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- randombytes_buf(key, sizeof key);
- RETURN_STRINGL((const char *) key, sizeof key);
- }
- #endif
- PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_keygen)
- {
- unsigned char key[crypto_aead_chacha20poly1305_KEYBYTES];
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- randombytes_buf(key, sizeof key);
- RETURN_STRINGL((const char *) key, sizeof key);
- }
- PHP_FUNCTION(sodium_crypto_aead_chacha20poly1305_ietf_keygen)
- {
- unsigned char key[crypto_aead_chacha20poly1305_IETF_KEYBYTES];
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- randombytes_buf(key, sizeof key);
- RETURN_STRINGL((const char *) key, sizeof key);
- }
- #ifdef crypto_aead_xchacha20poly1305_IETF_NPUBBYTES
- PHP_FUNCTION(sodium_crypto_aead_xchacha20poly1305_ietf_keygen)
- {
- unsigned char key[crypto_aead_xchacha20poly1305_IETF_KEYBYTES];
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- randombytes_buf(key, sizeof key);
- RETURN_STRINGL((const char *) key, sizeof key);
- }
- #endif
- PHP_FUNCTION(sodium_crypto_auth_keygen)
- {
- unsigned char key[crypto_auth_KEYBYTES];
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- randombytes_buf(key, sizeof key);
- RETURN_STRINGL((const char *) key, sizeof key);
- }
- PHP_FUNCTION(sodium_crypto_generichash_keygen)
- {
- unsigned char key[crypto_generichash_KEYBYTES];
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- randombytes_buf(key, sizeof key);
- RETURN_STRINGL((const char *) key, sizeof key);
- }
- PHP_FUNCTION(sodium_crypto_kdf_keygen)
- {
- unsigned char key[crypto_kdf_KEYBYTES];
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- randombytes_buf(key, sizeof key);
- RETURN_STRINGL((const char *) key, sizeof key);
- }
- PHP_FUNCTION(sodium_crypto_secretbox_keygen)
- {
- unsigned char key[crypto_secretbox_KEYBYTES];
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- randombytes_buf(key, sizeof key);
- RETURN_STRINGL((const char *) key, sizeof key);
- }
- PHP_FUNCTION(sodium_crypto_shorthash_keygen)
- {
- unsigned char key[crypto_shorthash_KEYBYTES];
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- randombytes_buf(key, sizeof key);
- RETURN_STRINGL((const char *) key, sizeof key);
- }
- PHP_FUNCTION(sodium_crypto_stream_keygen)
- {
- unsigned char key[crypto_stream_KEYBYTES];
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- randombytes_buf(key, sizeof key);
- RETURN_STRINGL((const char *) key, sizeof key);
- }
- PHP_FUNCTION(sodium_crypto_kdf_derive_from_key)
- {
- unsigned char ctx_padded[crypto_generichash_blake2b_PERSONALBYTES];
- #ifndef crypto_kdf_PRIMITIVE
- unsigned char salt[crypto_generichash_blake2b_SALTBYTES];
- #endif
- char *ctx;
- char *key;
- zend_string *subkey;
- zend_long subkey_id;
- zend_long subkey_len;
- size_t ctx_len;
- size_t key_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "llss",
- &subkey_len,
- &subkey_id,
- &ctx, &ctx_len,
- &key, &key_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (subkey_len < crypto_kdf_BYTES_MIN) {
- zend_throw_exception(sodium_exception_ce, "subkey cannot be smaller than SODIUM_CRYPTO_KDF_BYTES_MIN", 0);
- return;
- }
- if (subkey_len > crypto_kdf_BYTES_MAX || subkey_len > SIZE_MAX) {
- zend_throw_exception(sodium_exception_ce, "subkey cannot be larger than SODIUM_CRYPTO_KDF_BYTES_MAX", 0);
- return;
- }
- if (subkey_id < 0) {
- zend_throw_exception(sodium_exception_ce, "subkey_id cannot be negative", 0);
- return;
- }
- if (ctx_len != crypto_kdf_CONTEXTBYTES) {
- zend_throw_exception(sodium_exception_ce, "context should be SODIUM_CRYPTO_KDF_CONTEXTBYTES bytes", 0);
- return;
- }
- if (key_len != crypto_kdf_KEYBYTES) {
- zend_throw_exception(sodium_exception_ce, "key should be SODIUM_CRYPTO_KDF_KEYBYTES bytes", 0);
- return;
- }
- memcpy(ctx_padded, ctx, crypto_kdf_CONTEXTBYTES);
- memset(ctx_padded + crypto_kdf_CONTEXTBYTES, 0, sizeof ctx_padded - crypto_kdf_CONTEXTBYTES);
- subkey = zend_string_alloc((size_t) subkey_len, 0);
- #ifdef crypto_kdf_PRIMITIVE
- crypto_kdf_derive_from_key((unsigned char *) ZSTR_VAL(subkey),
- (size_t) subkey_len, (uint64_t) subkey_id,
- ctx, (const unsigned char *) key);
- #else
- salt[0] = (unsigned char) (((uint64_t) subkey_id) );
- salt[1] = (unsigned char) (((uint64_t) subkey_id) >> 8);
- salt[2] = (unsigned char) (((uint64_t) subkey_id) >> 16);
- salt[3] = (unsigned char) (((uint64_t) subkey_id) >> 24);
- salt[4] = (unsigned char) (((uint64_t) subkey_id) >> 32);
- salt[5] = (unsigned char) (((uint64_t) subkey_id) >> 40);
- salt[6] = (unsigned char) (((uint64_t) subkey_id) >> 48);
- salt[7] = (unsigned char) (((uint64_t) subkey_id) >> 56);
- memset(salt + 8, 0, (sizeof salt) - 8);
- crypto_generichash_blake2b_salt_personal((unsigned char *) ZSTR_VAL(subkey),
- (size_t) subkey_len,
- NULL, 0,
- (const unsigned char *) key,
- crypto_kdf_KEYBYTES,
- salt, ctx_padded);
- #endif
- ZSTR_VAL(subkey)[subkey_len] = 0;
- RETURN_STR(subkey);
- }
- PHP_FUNCTION(sodium_pad)
- {
- zend_string *padded;
- char *unpadded;
- zend_long blocksize;
- volatile size_t st;
- size_t i, j, k;
- size_t unpadded_len;
- size_t xpadlen;
- size_t xpadded_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "sl",
- &unpadded, &unpadded_len, &blocksize) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (blocksize <= 0) {
- zend_throw_exception(sodium_exception_ce, "block size cannot be less than 1", 0);
- return;
- }
- if (blocksize > SIZE_MAX) {
- zend_throw_exception(sodium_exception_ce, "block size is too large", 0);
- return;
- }
- xpadlen = blocksize - 1U;
- if ((blocksize & (blocksize - 1U)) == 0U) {
- xpadlen -= unpadded_len & ((size_t) blocksize - 1U);
- } else {
- xpadlen -= unpadded_len % (size_t) blocksize;
- }
- if ((size_t) SIZE_MAX - unpadded_len <= xpadlen) {
- zend_throw_exception(sodium_exception_ce, "input is too large", 0);
- return;
- }
- xpadded_len = unpadded_len + xpadlen;
- padded = zend_string_alloc(xpadded_len + 1U, 0);
- if (unpadded_len > 0) {
- st = 1U;
- i = 0U;
- k = unpadded_len;
- for (j = 0U; j <= xpadded_len; j++) {
- ZSTR_VAL(padded)[j] = unpadded[i];
- k -= st;
- st = (size_t) (~(((( (((uint64_t) k) >> 48) | (((uint64_t) k) >> 32) |
- (k >> 16) | k) & 0xffff) - 1U) >> 16)) & 1U;
- i += st;
- }
- }
- #if SODIUM_LIBRARY_VERSION_MAJOR > 9 || (SODIUM_LIBRARY_VERSION_MAJOR == 9 && SODIUM_LIBRARY_VERSION_MINOR >= 6)
- if (sodium_pad(NULL, (unsigned char *) ZSTR_VAL(padded), unpadded_len,
- (size_t) blocksize, xpadded_len + 1U) != 0) {
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- #else
- {
- char *tail;
- volatile unsigned char mask;
- unsigned char barrier_mask;
- tail = &ZSTR_VAL(padded)[xpadded_len];
- mask = 0U;
- for (i = 0; i < blocksize; i++) {
- barrier_mask = (unsigned char)
- (((i ^ xpadlen) - 1U) >> ((sizeof(size_t) - 1U) * CHAR_BIT));
- tail[-i] = (tail[-i] & mask) | (0x80 & barrier_mask);
- mask |= barrier_mask;
- }
- }
- #endif
- ZSTR_VAL(padded)[xpadded_len + 1U] = 0;
- RETURN_STR(padded);
- }
- PHP_FUNCTION(sodium_unpad)
- {
- zend_string *unpadded;
- char *padded;
- size_t padded_len;
- size_t unpadded_len;
- zend_long blocksize;
- int ret;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "sl",
- &padded, &padded_len, &blocksize) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (blocksize <= 0) {
- zend_throw_exception(sodium_exception_ce, "block size cannot be less than 1", 0);
- return;
- }
- if (blocksize > SIZE_MAX) {
- zend_throw_exception(sodium_exception_ce, "block size is too large", 0);
- return;
- }
- if (padded_len < blocksize) {
- zend_throw_exception(sodium_exception_ce, "invalid padding", 0);
- return;
- }
- #if SODIUM_LIBRARY_VERSION_MAJOR > 9 || (SODIUM_LIBRARY_VERSION_MAJOR == 9 && SODIUM_LIBRARY_VERSION_MINOR >= 6)
- ret = sodium_unpad(&unpadded_len, (const unsigned char *) padded,
- padded_len, (size_t) blocksize);
- #else
- {
- const char *tail;
- unsigned char acc = 0U;
- unsigned char c;
- unsigned char valid = 0U;
- volatile size_t pad_len = 0U;
- size_t i;
- size_t is_barrier;
- tail = &padded[padded_len - 1U];
- for (i = 0U; i < (size_t) blocksize; i++) {
- c = tail[-i];
- is_barrier =
- (( (acc - 1U) & (pad_len - 1U) & ((c ^ 0x80) - 1U) ) >> 8) & 1U;
- acc |= c;
- pad_len |= i & (1U + ~is_barrier);
- valid |= (unsigned char) is_barrier;
- }
- unpadded_len = padded_len - 1U - pad_len;
- ret = (int) (valid - 1U);
- }
- #endif
- if (ret != 0 || unpadded_len > LONG_MAX) {
- zend_throw_exception(sodium_exception_ce, "invalid padding", 0);
- return;
- }
- unpadded = zend_string_init(padded, padded_len, 0);
- PHP_SODIUM_ZSTR_TRUNCATE(unpadded, unpadded_len);
- ZSTR_VAL(unpadded)[unpadded_len] = 0;
- RETURN_STR(unpadded);
- }
- #ifdef crypto_secretstream_xchacha20poly1305_ABYTES
- PHP_FUNCTION(sodium_crypto_secretstream_xchacha20poly1305_keygen)
- {
- unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES];
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- randombytes_buf(key, sizeof key);
- RETURN_STRINGL((const char *) key, sizeof key);
- }
- PHP_FUNCTION(sodium_crypto_secretstream_xchacha20poly1305_init_push)
- {
- crypto_secretstream_xchacha20poly1305_state state;
- unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES];
- unsigned char *key;
- size_t key_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s",
- &key, &key_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (key_len != crypto_secretstream_xchacha20poly1305_KEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "key size should be SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES bytes",
- 0);
- return;
- }
- if (crypto_secretstream_xchacha20poly1305_init_push(&state,
- header, key) != 0) {
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- array_init(return_value);
- add_next_index_stringl(return_value, (const char *) &state, sizeof state);
- add_next_index_stringl(return_value, (const char *) header, sizeof header);
- }
- PHP_FUNCTION(sodium_crypto_secretstream_xchacha20poly1305_push)
- {
- zval *state_zv;
- zend_string *c;
- unsigned char *ad = NULL;
- unsigned char *msg;
- unsigned char *state;
- unsigned long long c_real_len;
- zend_long tag = crypto_secretstream_xchacha20poly1305_TAG_MESSAGE;
- size_t ad_len = (size_t) 0U;
- size_t c_len;
- size_t msg_len;
- size_t state_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "zs|sl",
- &state_zv,
- &msg, &msg_len, &ad, &ad_len, &tag) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- ZVAL_DEREF(state_zv);
- if (Z_TYPE_P(state_zv) != IS_STRING) {
- zend_throw_exception(sodium_exception_ce, "a reference to a state is required", 0);
- return;
- }
- sodium_separate_string(state_zv);
- state = (unsigned char *) Z_STRVAL(*state_zv);
- state_len = Z_STRLEN(*state_zv);
- if (state_len != sizeof (crypto_secretstream_xchacha20poly1305_state)) {
- zend_throw_exception(sodium_exception_ce, "incorrect state length", 0);
- return;
- }
- if (msg_len > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX ||
- msg_len > SIZE_MAX - crypto_secretstream_xchacha20poly1305_ABYTES) {
- zend_throw_exception(sodium_exception_ce, "message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes", 0);
- return;
- }
- if (tag < 0 || tag > 255) {
- zend_throw_exception(sodium_exception_ce, "unsupported value for the tag", 0);
- return;
- }
- c_len = msg_len + crypto_secretstream_xchacha20poly1305_ABYTES;
- c = zend_string_alloc((size_t) c_len, 0);
- if (crypto_secretstream_xchacha20poly1305_push
- ((void *) state, (unsigned char *) ZSTR_VAL(c), &c_real_len,
- msg, (unsigned long long) msg_len, ad, (unsigned long long) ad_len,
- (unsigned char) tag) != 0) {
- zend_string_efree(c);
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- if (c_real_len <= 0U || c_real_len >= SIZE_MAX || c_real_len > c_len) {
- zend_string_efree(c);
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- PHP_SODIUM_ZSTR_TRUNCATE(c, (size_t) c_real_len);
- ZSTR_VAL(c)[c_real_len] = 0;
- RETURN_NEW_STR(c);
- }
- PHP_FUNCTION(sodium_crypto_secretstream_xchacha20poly1305_init_pull)
- {
- crypto_secretstream_xchacha20poly1305_state state;
- unsigned char *header;
- unsigned char *key;
- size_t header_len;
- size_t key_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ss",
- &header, &header_len,
- &key, &key_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- if (header_len != crypto_secretstream_xchacha20poly1305_HEADERBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "header size should be SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES bytes",
- 0);
- return;
- }
- if (key_len != crypto_secretstream_xchacha20poly1305_KEYBYTES) {
- zend_throw_exception(sodium_exception_ce,
- "key size should be SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES bytes",
- 0);
- return;
- }
- if (crypto_secretstream_xchacha20poly1305_init_pull(&state,
- header, key) != 0) {
- zend_throw_exception(sodium_exception_ce, "internal error", 0);
- return;
- }
- RETURN_STRINGL((const char *) &state, sizeof state);
- }
- PHP_FUNCTION(sodium_crypto_secretstream_xchacha20poly1305_pull)
- {
- zval *state_zv;
- zend_string *msg;
- unsigned char *ad = NULL;
- unsigned char *c;
- unsigned char *state;
- unsigned long long msg_real_len;
- size_t ad_len = (size_t) 0U;
- size_t msg_len;
- size_t c_len;
- size_t state_len;
- unsigned char tag;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "zs|s",
- &state_zv,
- &c, &c_len, &ad, &ad_len) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- ZVAL_DEREF(state_zv);
- if (Z_TYPE_P(state_zv) != IS_STRING) {
- zend_throw_exception(sodium_exception_ce, "a reference to a state is required", 0);
- return;
- }
- sodium_separate_string(state_zv);
- state = (unsigned char *) Z_STRVAL(*state_zv);
- state_len = Z_STRLEN(*state_zv);
- if (state_len != sizeof (crypto_secretstream_xchacha20poly1305_state)) {
- zend_throw_exception(sodium_exception_ce, "incorrect state length", 0);
- return;
- }
- if (c_len < crypto_secretstream_xchacha20poly1305_ABYTES) {
- RETURN_FALSE;
- }
- msg_len = c_len - crypto_secretstream_xchacha20poly1305_ABYTES;
- msg = zend_string_alloc((size_t) msg_len, 0);
- if (crypto_secretstream_xchacha20poly1305_pull
- ((void *) state, (unsigned char *) ZSTR_VAL(msg), &msg_real_len, &tag,
- c, (unsigned long long) c_len, ad, (unsigned long long) ad_len) != 0) {
- zend_string_efree(msg);
- RETURN_FALSE;
- }
- if (msg_real_len >= SIZE_MAX || msg_real_len > msg_len) {
- zend_string_efree(msg);
- zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
- return;
- }
- PHP_SODIUM_ZSTR_TRUNCATE(msg, (size_t) msg_real_len);
- ZSTR_VAL(msg)[msg_real_len] = 0;
- array_init(return_value);
- add_next_index_str(return_value, msg);
- add_next_index_long(return_value, (long) tag);
- }
- PHP_FUNCTION(sodium_crypto_secretstream_xchacha20poly1305_rekey)
- {
- zval *state_zv;
- unsigned char *state;
- size_t state_len;
- if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "z", &state_zv) == FAILURE) {
- sodium_remove_param_values_from_backtrace(EG(exception));
- return;
- }
- ZVAL_DEREF(state_zv);
- if (Z_TYPE_P(state_zv) != IS_STRING) {
- zend_throw_exception(sodium_exception_ce, "a reference to a state is required", 0);
- return;
- }
- sodium_separate_string(state_zv);
- state = (unsigned char *) Z_STRVAL(*state_zv);
- state_len = Z_STRLEN(*state_zv);
- if (state_len != sizeof (crypto_secretstream_xchacha20poly1305_state)) {
- zend_throw_exception(sodium_exception_ce, "incorrect state length", 0);
- return;
- }
- crypto_secretstream_xchacha20poly1305_rekey((void *) state);
- }
- #endif
- /*
- * Local variables:
- * tab-width: 4
- * c-basic-offset: 4
- * End:
- * vim600: sw=4 ts=4 tw=78 fdm=marker
- * vim<600: sw=4 ts=4 tw=78
- */
|