zose.hoon 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668
  1. :: /common/zose: vendored types from zuse
  2. :: # %zose
  3. ::
  4. :: This library contains cryptographic primitives and utilities
  5. :: vendored from zuse.hoon. It includes various cryptosuites,
  6. :: number theory operations, and specific implementations like
  7. :: AES and elliptic curve cryptography. Also includes translation
  8. :: utilities for working with various formats.
  9. ::
  10. /= zeke /common/zeke
  11. ~% %zose ..stark-engine-jet-hook:zeke ~
  12. |%
  13. +| %types
  14. ::
  15. +$ octs (pair @ud @) :: octet-stream
  16. +$ desk @tas
  17. ::
  18. +| %conversion
  19. ++ wrap ^?
  20. |%
  21. :: +as-octs: atom to octet-stream
  22. ::
  23. ++ as-octs
  24. |= tam=@ ^- octs
  25. [(met 3 tam) tam]
  26. :: +as-octt: tape to octet-stream
  27. ::
  28. ++ as-octt
  29. |= tep=tape ^- octs
  30. (as-octs (rap 3 tep))
  31. ++ base58
  32. |%
  33. ++ de de-base58:zeke
  34. ++ en en-base58:zeke
  35. --
  36. --
  37. ::
  38. ++ format ^?
  39. |%
  40. ::
  41. ++ of-wall :: line list to tape
  42. |= a=wall ^- tape
  43. ?~(a ~ "{i.a}\0a{$(a t.a)}")
  44. ::
  45. --
  46. ::
  47. ++ number ^?
  48. |%
  49. :: :: ++fu:number
  50. ++ fu :: modulo (mul p q)
  51. |= a=[p=@ q=@]
  52. =+ b=?:(=([0 0] a) 0 (~(inv fo p.a) (~(sit fo p.a) q.a)))
  53. |%
  54. :: :: ++dif:fu:number
  55. ++ dif :: subtract
  56. |= [c=[@ @] d=[@ @]]
  57. [(~(dif fo p.a) -.c -.d) (~(dif fo q.a) +.c +.d)]
  58. :: :: ++exp:fu:number
  59. ++ exp :: exponent
  60. |= [c=@ d=[@ @]]
  61. :- (~(exp fo p.a) (mod c (dec p.a)) -.d)
  62. (~(exp fo q.a) (mod c (dec q.a)) +.d)
  63. :: :: ++out:fu:number
  64. ++ out :: garner's formula
  65. |= c=[@ @]
  66. %+ add +.c
  67. %+ mul q.a
  68. %+ ~(pro fo p.a) b
  69. (~(dif fo p.a) -.c (~(sit fo p.a) +.c))
  70. :: :: ++pro:fu:number
  71. ++ pro :: multiply
  72. |= [c=[@ @] d=[@ @]]
  73. [(~(pro fo p.a) -.c -.d) (~(pro fo q.a) +.c +.d)]
  74. :: :: ++sum:fu:number
  75. ++ sum :: add
  76. |= [c=[@ @] d=[@ @]]
  77. [(~(sum fo p.a) -.c -.d) (~(sum fo q.a) +.c +.d)]
  78. :: :: ++sit:fu:number
  79. ++ sit :: represent
  80. |= c=@
  81. [(mod c p.a) (mod c q.a)]
  82. -- ::fu
  83. :: :: ++curt:number
  84. ++ curt :: curve25519
  85. |= [a=@ b=@]
  86. => %= .
  87. +
  88. => +
  89. =+ =+ [p=486.662 q=(sub (bex 255) 19)]
  90. =+ fq=~(. fo q)
  91. [p=p q=q fq=fq]
  92. |%
  93. :: :: ++cla:curt:number
  94. ++ cla ::
  95. |= raw=@
  96. =+ low=(dis 248 (cut 3 [0 1] raw))
  97. =+ hih=(con 64 (dis 127 (cut 3 [31 1] raw)))
  98. =+ mid=(cut 3 [1 30] raw)
  99. (can 3 [[1 low] [30 mid] [1 hih] ~])
  100. :: :: ++sqr:curt:number
  101. ++ sqr ::
  102. |=(a=@ (mul a a))
  103. :: :: ++inv:curt:number
  104. ++ inv ::
  105. |=(a=@ (~(exp fo q) (sub q 2) a))
  106. :: :: ++cad:curt:number
  107. ++ cad ::
  108. |= [n=[x=@ z=@] m=[x=@ z=@] d=[x=@ z=@]]
  109. =+ ^= xx
  110. ;: mul 4 z.d
  111. %- sqr %- abs:si
  112. %+ dif:si
  113. (sun:si (mul x.m x.n))
  114. (sun:si (mul z.m z.n))
  115. ==
  116. =+ ^= zz
  117. ;: mul 4 x.d
  118. %- sqr %- abs:si
  119. %+ dif:si
  120. (sun:si (mul x.m z.n))
  121. (sun:si (mul z.m x.n))
  122. ==
  123. [(sit.fq xx) (sit.fq zz)]
  124. :: :: ++cub:curt:number
  125. ++ cub ::
  126. |= [x=@ z=@]
  127. =+ ^= xx
  128. %+ mul
  129. %- sqr %- abs:si
  130. (dif:si (sun:si x) (sun:si z))
  131. (sqr (add x z))
  132. =+ ^= zz
  133. ;: mul 4 x z
  134. :(add (sqr x) :(mul p x z) (sqr z))
  135. ==
  136. [(sit.fq xx) (sit.fq zz)]
  137. -- ::
  138. ==
  139. =+ one=[b 1]
  140. =+ i=253
  141. =+ r=one
  142. =+ s=(cub one)
  143. |-
  144. ?: =(i 0)
  145. =+ x=(cub r)
  146. (sit.fq (mul -.x (inv +.x)))
  147. =+ m=(rsh [0 i] a)
  148. ?: =(0 (mod m 2))
  149. $(i (dec i), s (cad r s one), r (cub r))
  150. $(i (dec i), r (cad r s one), s (cub s))
  151. :: :: ++ga:number
  152. ++ ga :: GF (bex p.a)
  153. |= a=[p=@ q=@ r=@] :: dim poly gen
  154. =+ si=(bex p.a)
  155. =+ ma=(dec si)
  156. => |%
  157. :: :: ++dif:ga:number
  158. ++ dif :: add and sub
  159. |= [b=@ c=@]
  160. ~| [%dif-ga a]
  161. ?> &((lth b si) (lth c si))
  162. (mix b c)
  163. :: :: ++dub:ga:number
  164. ++ dub :: mul by x
  165. |= b=@
  166. ~| [%dub-ga a]
  167. ?> (lth b si)
  168. ?: =(1 (cut 0 [(dec p.a) 1] b))
  169. (dif (sit q.a) (sit (lsh 0 b)))
  170. (lsh 0 b)
  171. :: :: ++pro:ga:number
  172. ++ pro :: slow multiply
  173. |= [b=@ c=@]
  174. ?: =(0 b)
  175. 0
  176. ?: =(1 (dis 1 b))
  177. (dif c $(b (rsh 0 b), c (dub c)))
  178. $(b (rsh 0 b), c (dub c))
  179. :: :: ++toe:ga:number
  180. ++ toe :: exp+log tables
  181. =+ ^= nu
  182. |= [b=@ c=@]
  183. ^- (map @ @)
  184. =+ d=*(map @ @)
  185. |-
  186. ?: =(0 c)
  187. d
  188. %= $
  189. c (dec c)
  190. d (~(put by d) c b)
  191. ==
  192. =+ [p=(nu 0 (bex p.a)) q=(nu ma ma)]
  193. =+ [b=1 c=0]
  194. |- ^- [p=(map @ @) q=(map @ @)]
  195. ?: =(ma c)
  196. [(~(put by p) c b) q]
  197. %= $
  198. b (pro r.a b)
  199. c +(c)
  200. p (~(put by p) c b)
  201. q (~(put by q) b c)
  202. ==
  203. :: :: ++sit:ga:number
  204. ++ sit :: reduce
  205. |= b=@
  206. (mod b (bex p.a))
  207. -- ::
  208. =+ toe
  209. |%
  210. :: :: ++fra:ga:number
  211. ++ fra :: divide
  212. |= [b=@ c=@]
  213. (pro b (inv c))
  214. :: :: ++inv:ga:number
  215. ++ inv :: invert
  216. |= b=@
  217. ~| [%inv-ga a]
  218. =+ c=(~(get by q) b)
  219. ?~ c !!
  220. =+ d=(~(get by p) (sub ma u.c))
  221. (need d)
  222. :: :: ++pow:ga:number
  223. ++ pow :: exponent
  224. |= [b=@ c=@]
  225. =+ [d=1 e=c f=0]
  226. |-
  227. ?: =(p.a f)
  228. d
  229. ?: =(1 (cut 0 [f 1] b))
  230. $(d (pro d e), e (pro e e), f +(f))
  231. $(e (pro e e), f +(f))
  232. :: :: ++pro:ga:number
  233. ++ pro :: multiply
  234. |= [b=@ c=@]
  235. ~| [%pro-ga a]
  236. =+ d=(~(get by q) b)
  237. ?~ d 0
  238. =+ e=(~(get by q) c)
  239. ?~ e 0
  240. =+ f=(~(get by p) (mod (add u.d u.e) ma))
  241. (need f)
  242. -- ::ga
  243. -- ::number
  244. +| %crypto
  245. ::
  246. ++ crypto ^?
  247. |%
  248. :: ::
  249. :::: ++aes:crypto :: (2b1) aes, all sizes
  250. :: ::::
  251. ++ aes !.
  252. |%
  253. :: :: ++ahem:aes:crypto
  254. ++ ahem :: kernel state
  255. |= [nnk=@ nnb=@ nnr=@]
  256. =>
  257. =+ => [gr=(ga:number 8 0x11b 3) few==>(fe .(a 5))]
  258. [pro=pro.gr dif=dif.gr pow=pow.gr ror=ror.few]
  259. => |% ::
  260. ++ cipa $_ ^? :: AES params
  261. |%
  262. ++ co *[p=@ q=@ r=@ s=@] :: column coefficients
  263. ++ ix |~(a=@ *@) :: key index
  264. ++ ro *[p=@ q=@ r=@ s=@] :: row shifts
  265. ++ su *@ :: s-box
  266. -- ::cipa
  267. -- ::
  268. |%
  269. :: :: ++pen:ahem:aes:
  270. ++ pen :: encrypt
  271. ^- cipa
  272. |%
  273. :: :: ++co:pen:ahem:aes:
  274. ++ co :: column coefficients
  275. [0x2 0x3 1 1]
  276. :: :: ++ix:pen:ahem:aes:
  277. ++ ix :: key index
  278. |~(a=@ a)
  279. :: :: ++ro:pen:ahem:aes:
  280. ++ ro :: row shifts
  281. [0 1 2 3]
  282. :: :: ++su:pen:ahem:aes:
  283. ++ su :: s-box
  284. 0x16bb.54b0.0f2d.9941.6842.e6bf.0d89.a18c.
  285. df28.55ce.e987.1e9b.948e.d969.1198.f8e1.
  286. 9e1d.c186.b957.3561.0ef6.0348.66b5.3e70.
  287. 8a8b.bd4b.1f74.dde8.c6b4.a61c.2e25.78ba.
  288. 08ae.7a65.eaf4.566c.a94e.d58d.6d37.c8e7.
  289. 79e4.9591.62ac.d3c2.5c24.0649.0a3a.32e0.
  290. db0b.5ede.14b8.ee46.8890.2a22.dc4f.8160.
  291. 7319.5d64.3d7e.a7c4.1744.975f.ec13.0ccd.
  292. d2f3.ff10.21da.b6bc.f538.9d92.8f40.a351.
  293. a89f.3c50.7f02.f945.8533.4d43.fbaa.efd0.
  294. cf58.4c4a.39be.cb6a.5bb1.fc20.ed00.d153.
  295. 842f.e329.b3d6.3b52.a05a.6e1b.1a2c.8309.
  296. 75b2.27eb.e280.1207.9a05.9618.c323.c704.
  297. 1531.d871.f1e5.a534.ccf7.3f36.2693.fdb7.
  298. c072.a49c.afa2.d4ad.f047.59fa.7dc9.82ca.
  299. 76ab.d7fe.2b67.0130.c56f.6bf2.7b77.7c63
  300. --
  301. :: :: ++pin:ahem:aes:
  302. ++ pin :: decrypt
  303. ^- cipa
  304. |%
  305. :: :: ++co:pin:ahem:aes:
  306. ++ co :: column coefficients
  307. [0xe 0xb 0xd 0x9]
  308. :: :: ++ix:pin:ahem:aes:
  309. ++ ix :: key index
  310. |~(a=@ (sub nnr a))
  311. :: :: ++ro:pin:ahem:aes:
  312. ++ ro :: row shifts
  313. [0 3 2 1]
  314. :: :: ++su:pin:ahem:aes:
  315. ++ su :: s-box
  316. 0x7d0c.2155.6314.69e1.26d6.77ba.7e04.2b17.
  317. 6199.5383.3cbb.ebc8.b0f5.2aae.4d3b.e0a0.
  318. ef9c.c993.9f7a.e52d.0d4a.b519.a97f.5160.
  319. 5fec.8027.5910.12b1.31c7.0788.33a8.dd1f.
  320. f45a.cd78.fec0.db9a.2079.d2c6.4b3e.56fc.
  321. 1bbe.18aa.0e62.b76f.89c5.291d.711a.f147.
  322. 6edf.751c.e837.f9e2.8535.ade7.2274.ac96.
  323. 73e6.b4f0.cecf.f297.eadc.674f.4111.913a.
  324. 6b8a.1301.03bd.afc1.020f.3fca.8f1e.2cd0.
  325. 0645.b3b8.0558.e4f7.0ad3.bc8c.00ab.d890.
  326. 849d.8da7.5746.155e.dab9.edfd.5048.706c.
  327. 92b6.655d.cc5c.a4d4.1698.6886.64f6.f872.
  328. 25d1.8b6d.49a2.5b76.b224.d928.66a1.2e08.
  329. 4ec3.fa42.0b95.4cee.3d23.c2a6.3294.7b54.
  330. cbe9.dec4.4443.8e34.87ff.2f9b.8239.e37c.
  331. fbd7.f381.9ea3.40bf.38a5.3630.d56a.0952
  332. --
  333. :: :: ++mcol:ahem:aes:
  334. ++ mcol ::
  335. |= [a=(list @) b=[p=@ q=@ r=@ s=@]]
  336. ^- (list @)
  337. =+ c=[p=*@ q=*@ r=*@ s=*@]
  338. |- ^- (list @)
  339. ?~ a ~
  340. => .(p.c (cut 3 [0 1] i.a))
  341. => .(q.c (cut 3 [1 1] i.a))
  342. => .(r.c (cut 3 [2 1] i.a))
  343. => .(s.c (cut 3 [3 1] i.a))
  344. :_ $(a t.a)
  345. %+ rep 3
  346. %+ turn
  347. %- limo
  348. :~ [[p.c p.b] [q.c q.b] [r.c r.b] [s.c s.b]]
  349. [[p.c s.b] [q.c p.b] [r.c q.b] [s.c r.b]]
  350. [[p.c r.b] [q.c s.b] [r.c p.b] [s.c q.b]]
  351. [[p.c q.b] [q.c r.b] [r.c s.b] [s.c p.b]]
  352. ==
  353. |= [a=[@ @] b=[@ @] c=[@ @] d=[@ @]]
  354. :(dif (pro a) (pro b) (pro c) (pro d))
  355. :: :: ++pode:ahem:aes:
  356. ++ pode :: explode to block
  357. |= [a=bloq b=@ c=@] ^- (list @)
  358. =+ d=(rip a c)
  359. =+ m=(met a c)
  360. |-
  361. ?: =(m b)
  362. d
  363. $(m +(m), d (weld d (limo [0 ~])))
  364. :: :: ++sube:ahem:aes:
  365. ++ sube :: s-box word
  366. |= [a=@ b=@] ^- @
  367. (rep 3 (turn (pode 3 4 a) |=(c=@ (cut 3 [c 1] b))))
  368. -- ::
  369. |%
  370. :: :: ++be:ahem:aes:crypto
  371. ++ be :: block cipher
  372. |= [a=? b=@ c=@H] ^- @uxH
  373. ~| %be-aesc
  374. => %= .
  375. +
  376. => +
  377. |%
  378. :: :: ++ankh:be:ahem:aes:
  379. ++ ankh ::
  380. |= [a=cipa b=@ c=@]
  381. (pode 5 nnb (cut 5 [(mul (ix.a b) nnb) nnb] c))
  382. :: :: ++sark:be:ahem:aes:
  383. ++ sark ::
  384. |= [c=(list @) d=(list @)]
  385. ^- (list @)
  386. ?~ c ~
  387. ?~ d !!
  388. [(mix i.c i.d) $(c t.c, d t.d)]
  389. :: :: ++srow:be:ahem:aes:
  390. ++ srow ::
  391. |= [a=cipa b=(list @)] ^- (list @)
  392. =+ [c=0 d=~ e=ro.a]
  393. |-
  394. ?: =(c nnb)
  395. d
  396. :_ $(c +(c))
  397. %+ rep 3
  398. %+ turn
  399. (limo [0 p.e] [1 q.e] [2 r.e] [3 s.e] ~)
  400. |= [f=@ g=@]
  401. (cut 3 [f 1] (snag (mod (add g c) nnb) b))
  402. :: :: ++subs:be:ahem:aes:
  403. ++ subs ::
  404. |= [a=cipa b=(list @)] ^- (list @)
  405. ?~ b ~
  406. [(sube i.b su.a) $(b t.b)]
  407. --
  408. ==
  409. =+ [d=?:(a pen pin) e=(pode 5 nnb c) f=1]
  410. => .(e (sark e (ankh d 0 b)))
  411. |-
  412. ?. =(nnr f)
  413. => .(e (subs d e))
  414. => .(e (srow d e))
  415. => .(e (mcol e co.d))
  416. => .(e (sark e (ankh d f b)))
  417. $(f +(f))
  418. => .(e (subs d e))
  419. => .(e (srow d e))
  420. => .(e (sark e (ankh d nnr b)))
  421. (rep 5 e)
  422. :: :: ++ex:ahem:aes:crypto
  423. ++ ex :: key expand
  424. |= a=@I ^- @
  425. =+ [b=a c=0 d=su:pen i=nnk]
  426. |-
  427. ?: =(i (mul nnb +(nnr)))
  428. b
  429. => .(c (cut 5 [(dec i) 1] b))
  430. => ?: =(0 (mod i nnk))
  431. => .(c (ror 3 1 c))
  432. => .(c (sube c d))
  433. .(c (mix c (pow (dec (div i nnk)) 2)))
  434. ?: &((gth nnk 6) =(4 (mod i nnk)))
  435. .(c (sube c d))
  436. .
  437. => .(c (mix c (cut 5 [(sub i nnk) 1] b)))
  438. => .(b (can 5 [i b] [1 c] ~))
  439. $(i +(i))
  440. :: :: ++ix:ahem:aes:crypto
  441. ++ ix :: key expand, inv
  442. |= a=@ ^- @
  443. =+ [i=1 j=*@ b=*@ c=co:pin]
  444. |-
  445. ?: =(nnr i)
  446. a
  447. => .(b (cut 7 [i 1] a))
  448. => .(b (rep 5 (mcol (pode 5 4 b) c)))
  449. => .(j (sub nnr i))
  450. %= $
  451. i +(i)
  452. a
  453. %+ can 7
  454. :~ [i (cut 7 [0 i] a)]
  455. [1 b]
  456. [j (cut 7 [+(i) j] a)]
  457. ==
  458. ==
  459. --
  460. :: :: ++ecba:aes:crypto
  461. ++ ecba :: AES-128 ECB
  462. ~% %ecba +> ~
  463. |_ key=@H
  464. :: :: ++en:ecba:aes:crypto
  465. ++ en :: encrypt
  466. ~/ %en
  467. |= blk=@H ^- @uxH
  468. =+ (ahem 4 4 10)
  469. =:
  470. key (~(net fe 7) key)
  471. blk (~(net fe 7) blk)
  472. ==
  473. %- ~(net fe 7)
  474. (be & (ex key) blk)
  475. :: :: ++de:ecba:aes:crypto
  476. ++ de :: decrypt
  477. ~/ %de
  478. |= blk=@H ^- @uxH
  479. =+ (ahem 4 4 10)
  480. =:
  481. key (~(net fe 7) key)
  482. blk (~(net fe 7) blk)
  483. ==
  484. %- ~(net fe 7)
  485. (be | (ix (ex key)) blk)
  486. -- ::ecba
  487. :: :: ++ecbb:aes:crypto
  488. ++ ecbb :: AES-192 ECB
  489. ~% %ecbb +> ~
  490. |_ key=@I
  491. :: :: ++en:ecbb:aes:crypto
  492. ++ en :: encrypt
  493. ~/ %en
  494. |= blk=@H ^- @uxH
  495. =+ (ahem 6 4 12)
  496. =:
  497. key (rsh 6 (~(net fe 8) key))
  498. blk (~(net fe 7) blk)
  499. ==
  500. %- ~(net fe 7)
  501. (be & (ex key) blk)
  502. :: :: ++de:ecbb:aes:crypto
  503. ++ de :: decrypt
  504. ~/ %de
  505. |= blk=@H ^- @uxH
  506. =+ (ahem 6 4 12)
  507. =:
  508. key (rsh 6 (~(net fe 8) key))
  509. blk (~(net fe 7) blk)
  510. ==
  511. %- ~(net fe 7)
  512. (be | (ix (ex key)) blk)
  513. -- ::ecbb
  514. :: :: ++ecbc:aes:crypto
  515. ++ ecbc :: AES-256 ECB
  516. ~% %ecbc +> ~
  517. |_ key=@I
  518. :: :: ++en:ecbc:aes:crypto
  519. ++ en :: encrypt
  520. ~/ %en
  521. |= blk=@H ^- @uxH
  522. =+ (ahem 8 4 14)
  523. =:
  524. key (~(net fe 8) key)
  525. blk (~(net fe 7) blk)
  526. ==
  527. %- ~(net fe 7)
  528. (be & (ex key) blk)
  529. :: :: ++de:ecbc:aes:crypto
  530. ++ de :: decrypt
  531. ~/ %de
  532. |= blk=@H ^- @uxH
  533. =+ (ahem 8 4 14)
  534. =:
  535. key (~(net fe 8) key)
  536. blk (~(net fe 7) blk)
  537. ==
  538. %- ~(net fe 7)
  539. (be | (ix (ex key)) blk)
  540. -- ::ecbc
  541. :: :: ++cbca:aes:crypto
  542. ++ cbca :: AES-128 CBC
  543. ~% %cbca +> ~
  544. |_ [key=@H prv=@H]
  545. :: :: ++en:cbca:aes:crypto
  546. ++ en :: encrypt
  547. ~/ %en
  548. |= txt=@ ^- @ux
  549. =+ pts=?:(=(txt 0) `(list @)`~[0] (flop (rip 7 txt)))
  550. =| cts=(list @)
  551. %+ rep 7
  552. :: logically, flop twice here
  553. |- ^- (list @)
  554. ?~ pts
  555. cts
  556. =+ cph=(~(en ecba key) (mix prv i.pts))
  557. %= $
  558. cts [cph cts]
  559. pts t.pts
  560. prv cph
  561. ==
  562. :: :: ++de:cbca:aes:crypto
  563. ++ de :: decrypt
  564. ~/ %de
  565. |= txt=@ ^- @ux
  566. =+ cts=?:(=(txt 0) `(list @)`~[0] (flop (rip 7 txt)))
  567. =| pts=(list @)
  568. %+ rep 7
  569. :: logically, flop twice here
  570. |- ^- (list @)
  571. ?~ cts
  572. pts
  573. =+ pln=(mix prv (~(de ecba key) i.cts))
  574. %= $
  575. pts [pln pts]
  576. cts t.cts
  577. prv i.cts
  578. ==
  579. -- ::cbca
  580. :: :: ++cbcb:aes:crypto
  581. ++ cbcb :: AES-192 CBC
  582. ~% %cbcb +> ~
  583. |_ [key=@I prv=@H]
  584. :: :: ++en:cbcb:aes:crypto
  585. ++ en :: encrypt
  586. ~/ %en
  587. |= txt=@ ^- @ux
  588. =+ pts=?:(=(txt 0) `(list @)`~[0] (flop (rip 7 txt)))
  589. =| cts=(list @)
  590. %+ rep 7
  591. :: logically, flop twice here
  592. |- ^- (list @)
  593. ?~ pts
  594. cts
  595. =+ cph=(~(en ecbb key) (mix prv i.pts))
  596. %= $
  597. cts [cph cts]
  598. pts t.pts
  599. prv cph
  600. ==
  601. :: :: ++de:cbcb:aes:crypto
  602. ++ de :: decrypt
  603. ~/ %de
  604. |= txt=@ ^- @ux
  605. =+ cts=?:(=(txt 0) `(list @)`~[0] (flop (rip 7 txt)))
  606. =| pts=(list @)
  607. %+ rep 7
  608. :: logically, flop twice here
  609. |- ^- (list @)
  610. ?~ cts
  611. pts
  612. =+ pln=(mix prv (~(de ecbb key) i.cts))
  613. %= $
  614. pts [pln pts]
  615. cts t.cts
  616. prv i.cts
  617. ==
  618. -- ::cbcb
  619. :: :: ++cbcc:aes:crypto
  620. ++ cbcc :: AES-256 CBC
  621. ~% %cbcc +> ~
  622. |_ [key=@I prv=@H]
  623. :: :: ++en:cbcc:aes:crypto
  624. ++ en :: encrypt
  625. ~/ %en
  626. |= txt=@ ^- @ux
  627. =+ pts=?:(=(txt 0) `(list @)`~[0] (flop (rip 7 txt)))
  628. =| cts=(list @)
  629. %+ rep 7
  630. :: logically, flop twice here
  631. |- ^- (list @)
  632. ?~ pts
  633. cts
  634. =+ cph=(~(en ecbc key) (mix prv i.pts))
  635. %= $
  636. cts [cph cts]
  637. pts t.pts
  638. prv cph
  639. ==
  640. :: :: ++de:cbcc:aes:crypto
  641. ++ de :: decrypt
  642. ~/ %de
  643. |= txt=@ ^- @ux
  644. =+ cts=?:(=(txt 0) `(list @)`~[0] (flop (rip 7 txt)))
  645. =| pts=(list @)
  646. %+ rep 7
  647. :: logically, flop twice here
  648. |- ^- (list @)
  649. ?~ cts
  650. pts
  651. =+ pln=(mix prv (~(de ecbc key) i.cts))
  652. %= $
  653. pts [pln pts]
  654. cts t.cts
  655. prv i.cts
  656. ==
  657. -- ::cbcc
  658. :: :: ++inc:aes:crypto
  659. ++ inc :: inc. low bloq
  660. |= [mod=bloq ctr=@H]
  661. ^- @uxH
  662. =+ bqs=(rip mod ctr)
  663. ?~ bqs 0x1
  664. %+ rep mod
  665. [(~(sum fe mod) i.bqs 1) t.bqs]
  666. :: :: ++ctra:aes:crypto
  667. ++ ctra :: AES-128 CTR
  668. ~% %ctra +> ~
  669. |_ [key=@H mod=bloq len=@ ctr=@H]
  670. :: :: ++en:ctra:aes:crypto
  671. ++ en :: encrypt
  672. ~/ %en
  673. |= txt=@
  674. ^- @ux
  675. =/ encrypt ~(en ecba key)
  676. =/ blocks (add (div len 16) ?:(=((^mod len 16) 0) 0 1))
  677. ?> (gte len (met 3 txt))
  678. %+ mix txt
  679. %+ rsh [3 (sub (mul 16 blocks) len)]
  680. %+ rep 7
  681. =| seed=(list @ux)
  682. |- ^+ seed
  683. ?: =(blocks 0) seed
  684. %= $
  685. seed [(encrypt ctr) seed]
  686. ctr (inc mod ctr)
  687. blocks (dec blocks)
  688. ==
  689. :: :: ++de:ctra:aes:crypto
  690. ++ de :: decrypt
  691. en
  692. -- ::ctra
  693. :: :: ++ctrb:aes:crypto
  694. ++ ctrb :: AES-192 CTR
  695. ~% %ctrb +> ~
  696. |_ [key=@I mod=bloq len=@ ctr=@H]
  697. :: :: ++en:ctrb:aes:crypto
  698. ++ en
  699. ~/ %en
  700. |= txt=@
  701. ^- @ux
  702. =/ encrypt ~(en ecbb key)
  703. =/ blocks (add (div len 16) ?:(=((^mod len 16) 0) 0 1))
  704. ?> (gte len (met 3 txt))
  705. %+ mix txt
  706. %+ rsh [3 (sub (mul 16 blocks) len)]
  707. %+ rep 7
  708. =| seed=(list @ux)
  709. |- ^+ seed
  710. ?: =(blocks 0) seed
  711. %= $
  712. seed [(encrypt ctr) seed]
  713. ctr (inc mod ctr)
  714. blocks (dec blocks)
  715. ==
  716. :: :: ++de:ctrb:aes:crypto
  717. ++ de :: decrypt
  718. en
  719. -- ::ctrb
  720. :: :: ++ctrc:aes:crypto
  721. ++ ctrc :: AES-256 CTR
  722. ~% %ctrc +> ~
  723. |_ [key=@I mod=bloq len=@ ctr=@H]
  724. :: :: ++en:ctrc:aes:crypto
  725. ++ en :: encrypt
  726. ~/ %en
  727. |= txt=@
  728. ^- @ux
  729. =/ encrypt ~(en ecbc key)
  730. =/ blocks (add (div len 16) ?:(=((^mod len 16) 0) 0 1))
  731. ?> (gte len (met 3 txt))
  732. %+ mix txt
  733. %+ rsh [3 (sub (mul 16 blocks) len)]
  734. %+ rep 7
  735. =| seed=(list @ux)
  736. |- ^+ seed
  737. ?: =(blocks 0) seed
  738. %= $
  739. seed [(encrypt ctr) seed]
  740. ctr (inc mod ctr)
  741. blocks (dec blocks)
  742. ==
  743. :: :: ++de:ctrc:aes:crypto
  744. ++ de :: decrypt
  745. en
  746. -- ::ctrc
  747. :: :: ++doub:aes:crypto
  748. ++ doub :: double 128-bit
  749. |= :: string mod finite
  750. ::
  751. str=@H
  752. ::
  753. :: field (see spec)
  754. ::
  755. ^- @uxH
  756. %- ~(sit fe 7)
  757. ?. =((xeb str) 128)
  758. (lsh 0 str)
  759. (mix 0x87 (lsh 0 str))
  760. :: :: ++mpad:aes:crypto
  761. ++ mpad ::
  762. |= [oct=@ txt=@]
  763. ::
  764. :: pad message to multiple of 128 bits
  765. :: by appending 1, then 0s
  766. :: the spec is unclear, but it must be octet based
  767. :: to match the test vectors
  768. ::
  769. ^- @ux
  770. =+ pad=(mod oct 16)
  771. ?: =(pad 0) 0x8000.0000.0000.0000.0000.0000.0000.0000
  772. (lsh [3 (sub 15 pad)] (mix 0x80 (lsh 3 txt)))
  773. :: :: ++suba:aes:crypto
  774. ++ suba :: AES-128 subkeys
  775. |= key=@H
  776. =+ l=(~(en ecba key) 0)
  777. =+ k1=(doub l)
  778. =+ k2=(doub k1)
  779. ^- [@ux @ux]
  780. [k1 k2]
  781. :: :: ++subb:aes:crypto
  782. ++ subb :: AES-192 subkeys
  783. |= key=@I
  784. =+ l=(~(en ecbb key) 0)
  785. =+ k1=(doub l)
  786. =+ k2=(doub k1)
  787. ^- [@ux @ux]
  788. [k1 k2]
  789. :: :: ++subc:aes:crypto
  790. ++ subc :: AES-256 subkeys
  791. |= key=@I
  792. =+ l=(~(en ecbc key) 0)
  793. =+ k1=(doub l)
  794. =+ k2=(doub k1)
  795. ^- [@ux @ux]
  796. [k1 k2]
  797. :: :: ++maca:aes:crypto
  798. ++ maca :: AES-128 CMAC
  799. ~/ %maca
  800. |= [key=@H oct=(unit @) txt=@]
  801. ^- @ux
  802. =+ [sub=(suba key) len=?~(oct (met 3 txt) u.oct)]
  803. =+ ^= pdt
  804. ?: &(=((mod len 16) 0) !=(len 0))
  805. [& txt]
  806. [| (mpad len txt)]
  807. =+ ^= mac
  808. %- ~(en cbca key 0)
  809. %+ mix +.pdt
  810. ?- -.pdt
  811. %& -.sub
  812. %| +.sub
  813. ==
  814. :: spec says MSBs, LSBs match test vectors
  815. ::
  816. (~(sit fe 7) mac)
  817. :: :: ++macb:aes:crypto
  818. ++ macb :: AES-192 CMAC
  819. ~/ %macb
  820. |= [key=@I oct=(unit @) txt=@]
  821. ^- @ux
  822. =+ [sub=(subb key) len=?~(oct (met 3 txt) u.oct)]
  823. =+ ^= pdt
  824. ?: &(=((mod len 16) 0) !=(len 0))
  825. [& txt]
  826. [| (mpad len txt)]
  827. =+ ^= mac
  828. %- ~(en cbcb key 0)
  829. %+ mix +.pdt
  830. ?- -.pdt
  831. %& -.sub
  832. %| +.sub
  833. ==
  834. :: spec says MSBs, LSBs match test vectors
  835. ::
  836. (~(sit fe 7) mac)
  837. :: :: ++macc:aes:crypto
  838. ++ macc :: AES-256 CMAC
  839. ~/ %macc
  840. |= [key=@I oct=(unit @) txt=@]
  841. ^- @ux
  842. =+ [sub=(subc key) len=?~(oct (met 3 txt) u.oct)]
  843. =+ ^= pdt
  844. ?: &(=((mod len 16) 0) !=(len 0))
  845. [& txt]
  846. [| (mpad len txt)]
  847. =+ ^= mac
  848. %- ~(en cbcc key 0)
  849. %+ mix +.pdt
  850. ?- -.pdt
  851. %& -.sub
  852. %| +.sub
  853. ==
  854. :: spec says MSBs, LSBs match test vectors
  855. ::
  856. (~(sit fe 7) mac)
  857. :: :: ++s2va:aes:crypto
  858. ++ s2va :: AES-128 S2V
  859. ~/ %s2va
  860. |= [key=@H ads=(list @)]
  861. ?~ ads (maca key `16 0x1)
  862. =/ res (maca key `16 0x0)
  863. %+ maca key
  864. |- ^- [[~ @ud] @uxH]
  865. ?~ t.ads
  866. =/ wyt (met 3 i.ads)
  867. ?: (gte wyt 16)
  868. [`wyt (mix i.ads res)]
  869. [`16 (mix (doub res) (mpad wyt i.ads))]
  870. %= $
  871. ads t.ads
  872. res (mix (doub res) (maca key ~ i.ads))
  873. ==
  874. :: :: ++s2vb:aes:crypto
  875. ++ s2vb :: AES-192 S2V
  876. ~/ %s2vb
  877. |= [key=@I ads=(list @)]
  878. ?~ ads (macb key `16 0x1)
  879. =/ res (macb key `16 0x0)
  880. %+ macb key
  881. |- ^- [[~ @ud] @uxH]
  882. ?~ t.ads
  883. =/ wyt (met 3 i.ads)
  884. ?: (gte wyt 16)
  885. [`wyt (mix i.ads res)]
  886. [`16 (mix (doub res) (mpad wyt i.ads))]
  887. %= $
  888. ads t.ads
  889. res (mix (doub res) (macb key ~ i.ads))
  890. ==
  891. :: :: ++s2vc:aes:crypto
  892. ++ s2vc :: AES-256 S2V
  893. ~/ %s2vc
  894. |= [key=@I ads=(list @)]
  895. ?~ ads (macc key `16 0x1)
  896. =/ res (macc key `16 0x0)
  897. %+ macc key
  898. |- ^- [[~ @ud] @uxH]
  899. ?~ t.ads
  900. =/ wyt (met 3 i.ads)
  901. ?: (gte wyt 16)
  902. [`wyt (mix i.ads res)]
  903. [`16 (mix (doub res) (mpad wyt i.ads))]
  904. %= $
  905. ads t.ads
  906. res (mix (doub res) (macc key ~ i.ads))
  907. ==
  908. :: :: ++siva:aes:crypto
  909. ++ siva :: AES-128 SIV
  910. ~% %siva +> ~
  911. |_ [key=@I vec=(list @)]
  912. :: :: ++en:siva:aes:crypto
  913. ++ en :: encrypt
  914. ~/ %en
  915. |= txt=@
  916. ^- (trel @uxH @ud @ux)
  917. =+ [k1=(rsh 7 key) k2=(end 7 key)]
  918. =+ iv=(s2va k1 (weld vec (limo ~[txt])))
  919. =+ len=(met 3 txt)
  920. =* hib (dis iv 0xffff.ffff.ffff.ffff.7fff.ffff.7fff.ffff)
  921. :+
  922. iv
  923. len
  924. (~(en ctra k2 7 len hib) txt)
  925. :: :: ++de:siva:aes:crypto
  926. ++ de :: decrypt
  927. ~/ %de
  928. |= [iv=@H len=@ txt=@]
  929. ^- (unit @ux)
  930. =+ [k1=(rsh 7 key) k2=(end 7 key)]
  931. =* hib (dis iv 0xffff.ffff.ffff.ffff.7fff.ffff.7fff.ffff)
  932. =+ ^= pln
  933. (~(de ctra k2 7 len hib) txt)
  934. ?. =((s2va k1 (weld vec (limo ~[pln]))) iv)
  935. ~
  936. `pln
  937. -- ::siva
  938. :: :: ++sivb:aes:crypto
  939. ++ sivb :: AES-192 SIV
  940. ~% %sivb +> ~
  941. |_ [key=@J vec=(list @)]
  942. :: :: ++en:sivb:aes:crypto
  943. ++ en :: encrypt
  944. ~/ %en
  945. |= txt=@
  946. ^- (trel @uxH @ud @ux)
  947. =+ [k1=(rsh [6 3] key) k2=(end [6 3] key)]
  948. =+ iv=(s2vb k1 (weld vec (limo ~[txt])))
  949. =* hib (dis iv 0xffff.ffff.ffff.ffff.7fff.ffff.7fff.ffff)
  950. =+ len=(met 3 txt)
  951. :+ iv
  952. len
  953. (~(en ctrb k2 7 len hib) txt)
  954. :: :: ++de:sivb:aes:crypto
  955. ++ de :: decrypt
  956. ~/ %de
  957. |= [iv=@H len=@ txt=@]
  958. ^- (unit @ux)
  959. =+ [k1=(rsh [6 3] key) k2=(end [6 3] key)]
  960. =* hib (dis iv 0xffff.ffff.ffff.ffff.7fff.ffff.7fff.ffff)
  961. =+ ^= pln
  962. (~(de ctrb k2 7 len hib) txt)
  963. ?. =((s2vb k1 (weld vec (limo ~[pln]))) iv)
  964. ~
  965. `pln
  966. -- ::sivb
  967. :: :: ++sivc:aes:crypto
  968. ++ sivc :: AES-256 SIV
  969. ~% %sivc +> ~
  970. |_ [key=@J vec=(list @)]
  971. :: :: ++en:sivc:aes:crypto
  972. ++ en :: encrypt
  973. ~/ %en
  974. |= txt=@
  975. ^- (trel @uxH @ud @ux)
  976. =+ [k1=(rsh 8 key) k2=(end 8 key)]
  977. =+ iv=(s2vc k1 (weld vec (limo ~[txt])))
  978. =* hib (dis iv 0xffff.ffff.ffff.ffff.7fff.ffff.7fff.ffff)
  979. =+ len=(met 3 txt)
  980. :+
  981. iv
  982. len
  983. (~(en ctrc k2 7 len hib) txt)
  984. :: :: ++de:sivc:aes:crypto
  985. ++ de :: decrypt
  986. ~/ %de
  987. |= [iv=@H len=@ txt=@]
  988. ^- (unit @ux)
  989. =+ [k1=(rsh 8 key) k2=(end 8 key)]
  990. =* hib (dis iv 0xffff.ffff.ffff.ffff.7fff.ffff.7fff.ffff)
  991. =+ ^= pln
  992. (~(de ctrc k2 7 len hib) txt)
  993. ?. =((s2vc k1 (weld vec (limo ~[pln]))) iv)
  994. ~
  995. `pln
  996. -- ::sivc
  997. --
  998. :: ::
  999. :::: ++ed:crypto :: ed25519
  1000. :: ::::
  1001. ++ ed
  1002. =>
  1003. =+ =+ [b=256 q=(sub (bex 255) 19)]
  1004. =+ fq=~(. fo q)
  1005. =+ ^= l
  1006. %+ add
  1007. (bex 252)
  1008. 27.742.317.777.372.353.535.851.937.790.883.648.493
  1009. =+ d=(dif.fq 0 (fra.fq 121.665 121.666))
  1010. =+ ii=(exp.fq (div (dec q) 4) 2)
  1011. [b=b q=q fq=fq l=l d=d ii=ii]
  1012. |%
  1013. :: :: ++norm:ed:crypto
  1014. ++ norm ::
  1015. |=(x=@ ?:(=(0 (mod x 2)) x (sub q x)))
  1016. :: :: ++xrec:ed:crypto
  1017. ++ xrec :: recover x-coord
  1018. |= y=@ ^- @
  1019. =+ ^= xx
  1020. %+ mul (dif.fq (mul y y) 1)
  1021. (inv.fq +(:(mul d y y)))
  1022. =+ x=(exp.fq (div (add 3 q) 8) xx)
  1023. ?: !=(0 (dif.fq (mul x x) (sit.fq xx)))
  1024. (norm (pro.fq x ii))
  1025. (norm x)
  1026. :: :: ++ward:ed:crypto
  1027. ++ ward :: edwards multiply
  1028. |= [pp=[@ @] qq=[@ @]] ^- [@ @]
  1029. =+ dp=:(pro.fq d -.pp -.qq +.pp +.qq)
  1030. =+ ^= xt
  1031. %+ pro.fq
  1032. %+ sum.fq
  1033. (pro.fq -.pp +.qq)
  1034. (pro.fq -.qq +.pp)
  1035. (inv.fq (sum.fq 1 dp))
  1036. =+ ^= yt
  1037. %+ pro.fq
  1038. %+ sum.fq
  1039. (pro.fq +.pp +.qq)
  1040. (pro.fq -.pp -.qq)
  1041. (inv.fq (dif.fq 1 dp))
  1042. [xt yt]
  1043. :: :: ++scam:ed:crypto
  1044. ++ scam :: scalar multiply
  1045. |= [pp=[@ @] e=@] ^- [@ @]
  1046. ?: =(0 e)
  1047. [0 1]
  1048. =+ qq=$(e (div e 2))
  1049. => .(qq (ward qq qq))
  1050. ?: =(1 (dis 1 e))
  1051. (ward qq pp)
  1052. qq
  1053. :: :: ++etch:ed:crypto
  1054. ++ etch :: encode point
  1055. |= pp=[@ @] ^- @
  1056. (can 0 ~[[(sub b 1) +.pp] [1 (dis 1 -.pp)]])
  1057. :: :: ++curv:ed:crypto
  1058. ++ curv :: point on curve?
  1059. |= [x=@ y=@] ^- ?
  1060. .= 0
  1061. %+ dif.fq
  1062. %+ sum.fq
  1063. (pro.fq (sub q (sit.fq x)) x)
  1064. (pro.fq y y)
  1065. (sum.fq 1 :(pro.fq d x x y y))
  1066. :: :: ++deco:ed:crypto
  1067. ++ deco :: decode point
  1068. |= s=@ ^- (unit [@ @])
  1069. =+ y=(cut 0 [0 (dec b)] s)
  1070. =+ si=(cut 0 [(dec b) 1] s)
  1071. =+ x=(xrec y)
  1072. => .(x ?:(!=(si (dis 1 x)) (sub q x) x))
  1073. =+ pp=[x y]
  1074. ?. (curv pp)
  1075. ~
  1076. [~ pp]
  1077. :: :: ++bb:ed:crypto
  1078. ++ bb ::
  1079. =+ bby=(pro.fq 4 (inv.fq 5))
  1080. [(xrec bby) bby]
  1081. -- ::
  1082. ~% %ed + ~
  1083. |%
  1084. ::
  1085. ++ point-add
  1086. ~/ %point-add
  1087. |= [a-point=@udpoint b-point=@udpoint]
  1088. ^- @udpoint
  1089. ::
  1090. =/ a-point-decoded=[@ @] (need (deco a-point))
  1091. =/ b-point-decoded=[@ @] (need (deco b-point))
  1092. ::
  1093. %- etch
  1094. (ward a-point-decoded b-point-decoded)
  1095. ::
  1096. ++ scalarmult
  1097. ~/ %scalarmult
  1098. |= [a=@udscalar a-point=@udpoint]
  1099. ^- @udpoint
  1100. ::
  1101. =/ a-point-decoded=[@ @] (need (deco a-point))
  1102. ::
  1103. %- etch
  1104. (scam a-point-decoded a)
  1105. ::
  1106. ++ scalarmult-base
  1107. ~/ %scalarmult-base
  1108. |= scalar=@udscalar
  1109. ^- @udpoint
  1110. %- etch
  1111. (scam bb scalar)
  1112. ::
  1113. ++ add-scalarmult-scalarmult-base
  1114. ~/ %add-scalarmult-scalarmult-base
  1115. |= [a=@udscalar a-point=@udpoint b=@udscalar]
  1116. ^- @udpoint
  1117. ::
  1118. =/ a-point-decoded=[@ @] (need (deco a-point))
  1119. ::
  1120. %- etch
  1121. %+ ward
  1122. (scam bb b)
  1123. (scam a-point-decoded a)
  1124. ::
  1125. ++ add-double-scalarmult
  1126. ~/ %add-double-scalarmult
  1127. |= [a=@udscalar a-point=@udpoint b=@udscalar b-point=@udpoint]
  1128. ^- @udpoint
  1129. ::
  1130. =/ a-point-decoded=[@ @] (need (deco a-point))
  1131. =/ b-point-decoded=[@ @] (need (deco b-point))
  1132. ::
  1133. %- etch
  1134. %+ ward
  1135. (scam a-point-decoded a)
  1136. (scam b-point-decoded b)
  1137. :: :: ++puck:ed:crypto
  1138. ++ puck :: public key
  1139. ~/ %puck
  1140. |= sk=@I ^- @
  1141. ?: (gth (met 3 sk) 32) !!
  1142. =+ h=(shal (rsh [0 3] b) sk)
  1143. =+ ^= a
  1144. %+ add
  1145. (bex (sub b 2))
  1146. (lsh [0 3] (cut 0 [3 (sub b 5)] h))
  1147. =+ aa=(scam bb a)
  1148. (etch aa)
  1149. :: :: ++suck:ed:crypto
  1150. ++ suck :: keypair from seed
  1151. |= se=@I ^- @uJ
  1152. =+ pu=(puck se)
  1153. (can 0 ~[[b se] [b pu]])
  1154. :: :: ++shar:ed:crypto
  1155. ++ shar :: curve25519 secret
  1156. ~/ %shar
  1157. |= [pub=@ sek=@]
  1158. ^- @ux
  1159. =+ exp=(shal (rsh [0 3] b) (suck sek))
  1160. =. exp (dis exp (can 0 ~[[3 0] [251 (fil 0 251 1)]]))
  1161. =. exp (con exp (lsh [3 31] 0b100.0000))
  1162. =+ prv=(end 8 exp)
  1163. =+ crv=(fra.fq (sum.fq 1 pub) (dif.fq 1 pub))
  1164. (curt:number prv crv)
  1165. :: :: ++sign:ed:crypto
  1166. ++ sign :: certify
  1167. ~/ %sign
  1168. |= [m=@ se=@] ^- @
  1169. =+ sk=(suck se)
  1170. =+ pk=(cut 0 [b b] sk)
  1171. =+ h=(shal (rsh [0 3] b) sk)
  1172. =+ ^= a
  1173. %+ add
  1174. (bex (sub b 2))
  1175. (lsh [0 3] (cut 0 [3 (sub b 5)] h))
  1176. =+ ^= r
  1177. =+ hm=(cut 0 [b b] h)
  1178. =+ ^= i
  1179. %+ can 0
  1180. :~ [b hm]
  1181. [(met 0 m) m]
  1182. ==
  1183. (shaz i)
  1184. =+ rr=(scam bb r)
  1185. =+ ^= ss
  1186. =+ er=(etch rr)
  1187. =+ ^= ha
  1188. %+ can 0
  1189. :~ [b er]
  1190. [b pk]
  1191. [(met 0 m) m]
  1192. ==
  1193. (~(sit fo l) (add r (mul (shaz ha) a)))
  1194. (can 0 ~[[b (etch rr)] [b ss]])
  1195. :: :: ++veri:ed:crypto
  1196. ++ veri :: validate
  1197. ~/ %veri
  1198. |= [s=@ m=@ pk=@] ^- ?
  1199. ?: (gth (div b 4) (met 3 s)) |
  1200. ?: (gth (div b 8) (met 3 pk)) |
  1201. =+ cb=(rsh [0 3] b)
  1202. =+ rr=(deco (cut 0 [0 b] s))
  1203. ?~ rr |
  1204. =+ aa=(deco pk)
  1205. ?~ aa |
  1206. =+ ss=(cut 0 [b b] s)
  1207. =+ ha=(can 3 ~[[cb (etch u.rr)] [cb pk] [(met 3 m) m]])
  1208. =+ h=(shaz ha)
  1209. =((scam bb ss) (ward u.rr (scam u.aa h)))
  1210. -- ::ed
  1211. :: ::
  1212. :::: ++scr:crypto :: (2b3) scrypt
  1213. :: ::::
  1214. ++ scr
  1215. |%
  1216. :: :: ++sal:scr:crypto
  1217. ++ sal :: salsa20 hash
  1218. |= [x=@ r=@] :: with r rounds
  1219. ?> =((mod r 2) 0) ::
  1220. =+ few==>(fe .(a 5))
  1221. =+ ^= rot
  1222. |= [a=@ b=@]
  1223. (mix (end 5 (lsh [0 a] b)) (rsh [0 (sub 32 a)] b))
  1224. =+ ^= lea
  1225. |= [a=@ b=@]
  1226. (net:few (sum:few (net:few a) (net:few b)))
  1227. => |%
  1228. :: :: ++qr:sal:scr:crypto
  1229. ++ qr :: quarterround
  1230. |= y=[@ @ @ @ ~]
  1231. =+ zb=(mix &2.y (rot 7 (sum:few &1.y &4.y)))
  1232. =+ zc=(mix &3.y (rot 9 (sum:few zb &1.y)))
  1233. =+ zd=(mix &4.y (rot 13 (sum:few zc zb)))
  1234. =+ za=(mix &1.y (rot 18 (sum:few zd zc)))
  1235. ~[za zb zc zd]
  1236. :: :: ++rr:sal:scr:crypto
  1237. ++ rr :: rowround
  1238. |= [y=(list @)]
  1239. =+ za=(qr ~[&1.y &2.y &3.y &4.y])
  1240. =+ zb=(qr ~[&6.y &7.y &8.y &5.y])
  1241. =+ zc=(qr ~[&11.y &12.y &9.y &10.y])
  1242. =+ zd=(qr ~[&16.y &13.y &14.y &15.y])
  1243. ^- (list @) :~
  1244. &1.za &2.za &3.za &4.za
  1245. &4.zb &1.zb &2.zb &3.zb
  1246. &3.zc &4.zc &1.zc &2.zc
  1247. &2.zd &3.zd &4.zd &1.zd ==
  1248. :: :: ++cr:sal:scr:crypto
  1249. ++ cr :: columnround
  1250. |= [x=(list @)]
  1251. =+ ya=(qr ~[&1.x &5.x &9.x &13.x])
  1252. =+ yb=(qr ~[&6.x &10.x &14.x &2.x])
  1253. =+ yc=(qr ~[&11.x &15.x &3.x &7.x])
  1254. =+ yd=(qr ~[&16.x &4.x &8.x &12.x])
  1255. ^- (list @) :~
  1256. &1.ya &4.yb &3.yc &2.yd
  1257. &2.ya &1.yb &4.yc &3.yd
  1258. &3.ya &2.yb &1.yc &4.yd
  1259. &4.ya &3.yb &2.yc &1.yd ==
  1260. :: :: ++dr:sal:scr:crypto
  1261. ++ dr :: doubleround
  1262. |= [x=(list @)]
  1263. (rr (cr x))
  1264. :: :: ++al:sal:scr:crypto
  1265. ++ al :: add two lists
  1266. |= [a=(list @) b=(list @)]
  1267. |- ^- (list @)
  1268. ?~ a ~ ?~ b ~
  1269. [i=(sum:few -.a -.b) t=$(a +.a, b +.b)]
  1270. -- ::
  1271. =+ xw=(rpp 5 16 x)
  1272. =+ ^= ow |- ^- (list @)
  1273. ?~ r xw
  1274. $(xw (dr xw), r (sub r 2))
  1275. (rep 5 (al xw ow))
  1276. :: :: ++rpp:scr:crypto
  1277. ++ rpp :: rip+filler blocks
  1278. |= [a=bloq b=@ c=@]
  1279. =+ q=(rip a c)
  1280. =+ w=(lent q)
  1281. ?. =(w b)
  1282. ?. (lth w b) (slag (sub w b) q)
  1283. ^+ q (weld q (reap (sub b (lent q)) 0))
  1284. q
  1285. :: :: ++bls:scr:crypto
  1286. ++ bls :: split to sublists
  1287. |= [a=@ b=(list @)]
  1288. ?> =((mod (lent b) a) 0)
  1289. |- ^- (list (list @))
  1290. ?~ b ~
  1291. [i=(scag a `(list @)`b) t=$(b (slag a `(list @)`b))]
  1292. :: :: ++slb:scr:crypto
  1293. ++ slb ::
  1294. |= [a=(list (list @))]
  1295. |- ^- (list @)
  1296. ?~ a ~
  1297. (weld `(list @)`-.a $(a +.a))
  1298. :: :: ++sbm:scr:crypto
  1299. ++ sbm :: scryptBlockMix
  1300. |= [r=@ b=(list @)]
  1301. ?> =((lent b) (mul 2 r))
  1302. =+ [x=(snag (dec (mul 2 r)) b) c=0]
  1303. =| [ya=(list @) yb=(list @)]
  1304. |- ^- (list @)
  1305. ?~ b (flop (weld yb ya))
  1306. =. x (sal (mix x -.b) 8)
  1307. ?~ (mod c 2)
  1308. $(c +(c), b +.b, ya [i=x t=ya])
  1309. $(c +(c), b +.b, yb [i=x t=yb])
  1310. :: :: ++srm:scr:crypto
  1311. ++ srm :: scryptROMix
  1312. |= [r=@ b=(list @) n=@]
  1313. ?> ?& =((lent b) (mul 2 r))
  1314. =(n (bex (dec (xeb n))))
  1315. (lth n (bex (mul r 16)))
  1316. ==
  1317. =+ [v=*(list (list @)) c=0]
  1318. =. v
  1319. |- ^- (list (list @))
  1320. =+ w=(sbm r b)
  1321. ?: =(c n) (flop v)
  1322. $(c +(c), v [i=[b] t=v], b w)
  1323. =+ x=(sbm r (snag (dec n) v))
  1324. |- ^- (list @)
  1325. ?: =(c n) x
  1326. =+ q=(snag (dec (mul r 2)) x)
  1327. =+ z=`(list @)`(snag (mod q n) v)
  1328. =+ ^= w |- ^- (list @)
  1329. ?~ x ~ ?~ z ~
  1330. [i=(mix -.x -.z) t=$(x +.x, z +.z)]
  1331. $(x (sbm r w), c +(c))
  1332. :: :: ++hmc:scr:crypto
  1333. ++ hmc :: HMAC-SHA-256
  1334. |= [k=@ t=@]
  1335. (hml k (met 3 k) t (met 3 t))
  1336. :: :: ++hml:scr:crypto
  1337. ++ hml :: w+length
  1338. |= [k=@ kl=@ t=@ tl=@]
  1339. => .(k (end [3 kl] k), t (end [3 tl] t))
  1340. =+ b=64
  1341. =? k (gth kl b) (shay kl k)
  1342. =+ ^= q %+ shay (add b tl)
  1343. (add (lsh [3 b] t) (mix k (fil 3 b 0x36)))
  1344. %+ shay (add b 32)
  1345. (add (lsh [3 b] q) (mix k (fil 3 b 0x5c)))
  1346. :: :: ++pbk:scr:crypto
  1347. ++ pbk :: PBKDF2-HMAC-SHA256
  1348. ~/ %pbk
  1349. |= [p=@ s=@ c=@ d=@]
  1350. (pbl p (met 3 p) s (met 3 s) c d)
  1351. :: :: ++pbl:scr:crypto
  1352. ++ pbl :: w+length
  1353. ~/ %pbl
  1354. |= [p=@ pl=@ s=@ sl=@ c=@ d=@]
  1355. => .(p (end [3 pl] p), s (end [3 sl] s))
  1356. =+ h=32
  1357. ::
  1358. :: max key length 1GB
  1359. :: max iterations 2^28
  1360. ::
  1361. ?> ?& (lte d (bex 30))
  1362. (lte c (bex 28))
  1363. !=(c 0)
  1364. ==
  1365. =+ ^= l ?~ (mod d h)
  1366. (div d h)
  1367. +((div d h))
  1368. =+ r=(sub d (mul h (dec l)))
  1369. =+ [t=0 j=1 k=1]
  1370. =. t |- ^- @
  1371. ?: (gth j l) t
  1372. =+ u=(add s (lsh [3 sl] (rep 3 (flop (rpp 3 4 j)))))
  1373. =+ f=0 =. f |- ^- @
  1374. ?: (gth k c) f
  1375. =+ q=(hml p pl u ?:(=(k 1) (add sl 4) h))
  1376. $(u q, f (mix f q), k +(k))
  1377. $(t (add t (lsh [3 (mul (dec j) h)] f)), j +(j))
  1378. (end [3 d] t)
  1379. :: :: ++hsh:scr:crypto
  1380. ++ hsh :: scrypt
  1381. ~/ %hsh
  1382. |= [p=@ s=@ n=@ r=@ z=@ d=@]
  1383. (hsl p (met 3 p) s (met 3 s) n r z d)
  1384. :: :: ++hsl:scr:crypto
  1385. ++ hsl :: w+length
  1386. ~/ %hsl
  1387. |= [p=@ pl=@ s=@ sl=@ n=@ r=@ z=@ d=@]
  1388. =| v=(list (list @))
  1389. => .(p (end [3 pl] p), s (end [3 sl] s))
  1390. =+ u=(mul (mul 128 r) z)
  1391. ::
  1392. :: n is power of 2; max 1GB memory
  1393. ::
  1394. ?> ?& =(n (bex (dec (xeb n))))
  1395. !=(r 0) !=(z 0)
  1396. %+ lte
  1397. (mul (mul 128 r) (dec (add n z)))
  1398. (bex 30)
  1399. (lth pl (bex 31))
  1400. (lth sl (bex 31))
  1401. ==
  1402. =+ ^= b =+ (rpp 3 u (pbl p pl s sl 1 u))
  1403. %+ turn (bls (mul 128 r) -)
  1404. |=(a=(list @) (rpp 9 (mul 2 r) (rep 3 a)))
  1405. ?> =((lent b) z)
  1406. =+ ^= q
  1407. =+ |- ?~ b (flop v)
  1408. $(b +.b, v [i=(srm r -.b n) t=v])
  1409. %+ turn `(list (list @))`-
  1410. |=(a=(list @) (rpp 3 (mul 128 r) (rep 9 a)))
  1411. (pbl p pl (rep 3 (slb q)) u 1 d)
  1412. :: :: ++ypt:scr:crypto
  1413. ++ ypt :: 256bit {salt pass}
  1414. |= [s=@ p=@]
  1415. ^- @
  1416. (hsh p s 16.384 8 1 256)
  1417. -- ::scr
  1418. :: ::
  1419. :::: ++crub:crypto :: (2b4) suite B, Ed
  1420. :: ::::
  1421. ++ crub !:
  1422. ^- acru
  1423. =| [pub=[cry=@ sgn=@] sek=(unit [cry=@ sgn=@])]
  1424. |%
  1425. :: :: ++as:crub:crypto
  1426. ++ as ::
  1427. |%
  1428. :: :: ++sign:as:crub:
  1429. ++ sign ::
  1430. |= msg=@
  1431. ^- @ux
  1432. (jam [(sigh msg) msg])
  1433. :: :: ++sigh:as:crub:
  1434. ++ sigh ::
  1435. |= msg=@
  1436. ^- @ux
  1437. ?~ sek ~| %pubkey-only !!
  1438. (sign:ed msg sgn.u.sek)
  1439. :: :: ++sure:as:crub:
  1440. ++ sure ::
  1441. |= txt=@
  1442. ^- (unit @ux)
  1443. =+ ;;([sig=@ msg=@] (cue txt))
  1444. ?. (safe sig msg) ~
  1445. (some msg)
  1446. :: :: ++safe:as:crub:
  1447. ++ safe
  1448. |= [sig=@ msg=@]
  1449. ^- ?
  1450. (veri:ed sig msg sgn.pub)
  1451. :: :: ++seal:as:crub:
  1452. ++ seal ::
  1453. |= [bpk=pass msg=@]
  1454. ^- @ux
  1455. ?~ sek ~| %pubkey-only !!
  1456. ?> =('b' (end 3 bpk))
  1457. =+ pk=(rsh 8 (rsh 3 bpk))
  1458. =+ shar=(shax (shar:ed pk cry.u.sek))
  1459. =+ smsg=(sign msg)
  1460. (jam (~(en siva:aes shar ~) smsg))
  1461. :: :: ++tear:as:crub:
  1462. ++ tear ::
  1463. |= [bpk=pass txt=@]
  1464. ^- (unit @ux)
  1465. ?~ sek ~| %pubkey-only !!
  1466. ?> =('b' (end 3 bpk))
  1467. =+ pk=(rsh 8 (rsh 3 bpk))
  1468. =+ shar=(shax (shar:ed pk cry.u.sek))
  1469. =+ ;;([iv=@ len=@ cph=@] (cue txt))
  1470. =+ try=(~(de siva:aes shar ~) iv len cph)
  1471. ?~ try ~
  1472. (sure:as:(com:nu:crub bpk) u.try)
  1473. -- ::as
  1474. :: :: ++de:crub:crypto
  1475. ++ de :: decrypt
  1476. |= [key=@J txt=@]
  1477. ^- (unit @ux)
  1478. =+ ;;([iv=@ len=@ cph=@] (cue txt))
  1479. %^ ~(de sivc:aes (shaz key) ~)
  1480. iv
  1481. len
  1482. cph
  1483. :: :: ++dy:crub:crypto
  1484. ++ dy :: need decrypt
  1485. |= [key=@J cph=@]
  1486. (need (de key cph))
  1487. :: :: ++en:crub:crypto
  1488. ++ en :: encrypt
  1489. |= [key=@J msg=@]
  1490. ^- @ux
  1491. (jam (~(en sivc:aes (shaz key) ~) msg))
  1492. :: :: ++ex:crub:crypto
  1493. ++ ex :: extract
  1494. |%
  1495. :: :: ++fig:ex:crub:crypto
  1496. ++ fig :: fingerprint
  1497. ^- @uvH
  1498. (shaf %bfig pub)
  1499. :: :: ++pac:ex:crub:crypto
  1500. ++ pac :: private fingerprint
  1501. ^- @uvG
  1502. ?~ sek ~| %pubkey-only !!
  1503. (end 6 (shaf %bcod sec))
  1504. :: :: ++pub:ex:crub:crypto
  1505. ++ pub :: public key
  1506. ^- pass
  1507. (cat 3 'b' (cat 8 sgn.^pub cry.^pub))
  1508. :: :: ++sec:ex:crub:crypto
  1509. ++ sec :: private key
  1510. ^- ring
  1511. ?~ sek ~| %pubkey-only !!
  1512. (cat 3 'B' (cat 8 sgn.u.sek cry.u.sek))
  1513. -- ::ex
  1514. :: :: ++nu:crub:crypto
  1515. ++ nu ::
  1516. |%
  1517. :: :: ++pit:nu:crub:crypto
  1518. ++ pit :: create keypair
  1519. |= [w=@ seed=@]
  1520. =+ wid=(add (div w 8) ?:(=((mod w 8) 0) 0 1))
  1521. =+ bits=(shal wid seed)
  1522. =+ [c=(rsh 8 bits) s=(end 8 bits)]
  1523. ..nu(pub [cry=(puck:ed c) sgn=(puck:ed s)], sek `[cry=c sgn=s])
  1524. :: :: ++nol:nu:crub:crypto
  1525. ++ nol :: activate secret
  1526. |= a=ring
  1527. =+ [mag=(end 3 a) bod=(rsh 3 a)]
  1528. ~| %not-crub-seckey ?> =('B' mag)
  1529. =+ [c=(rsh 8 bod) s=(end 8 bod)]
  1530. ..nu(pub [cry=(puck:ed c) sgn=(puck:ed s)], sek `[cry=c sgn=s])
  1531. :: :: ++com:nu:crub:crypto
  1532. ++ com :: activate public
  1533. |= a=pass
  1534. =+ [mag=(end 3 a) bod=(rsh 3 a)]
  1535. ~| %not-crub-pubkey ?> =('b' mag)
  1536. ..nu(pub [cry=(rsh 8 bod) sgn=(end 8 bod)], sek ~)
  1537. -- ::nu
  1538. -- ::crub
  1539. :: ::
  1540. :::: ++crua:crypto :: (2b5) suite B, RSA
  1541. :: ::::
  1542. ++ crua !!
  1543. :: ::
  1544. :::: ++test:crypto :: (2b6) test crypto
  1545. :: ::::
  1546. ++ test ^?
  1547. |%
  1548. :: :: ++trub:test:crypto
  1549. ++ trub :: test crub
  1550. |= msg=@t
  1551. ::
  1552. :: make acru cores
  1553. ::
  1554. =/ ali (pit:nu:crub 512 (shaz 'Alice'))
  1555. =/ ali-pub (com:nu:crub pub:ex.ali)
  1556. =/ bob (pit:nu:crub 512 (shaz 'Robert'))
  1557. =/ bob-pub (com:nu:crub pub:ex.bob)
  1558. ::
  1559. :: alice signs and encrypts a symmetric key to bob
  1560. ::
  1561. =/ secret-key %- shaz
  1562. 'Let there be no duplicity when taking a stand against him.'
  1563. =/ signed-key (sign:as.ali secret-key)
  1564. =/ crypted-key (seal:as.ali pub:ex.bob-pub signed-key)
  1565. :: bob decrypts and verifies
  1566. =/ decrypt-key-attempt (tear:as.bob pub:ex.ali-pub crypted-key)
  1567. =/ decrypted-key ~| %decrypt-fail (need decrypt-key-attempt)
  1568. =/ verify-key-attempt (sure:as.ali-pub decrypted-key)
  1569. =/ verified-key ~| %verify-fail (need verify-key-attempt)
  1570. :: bob encrypts with symmetric key
  1571. =/ crypted-msg (en.bob verified-key msg)
  1572. :: alice decrypts with same key
  1573. `@t`(dy.ali secret-key crypted-msg)
  1574. -- ::test
  1575. :: ::
  1576. :::: ++keccak:crypto :: (2b7) keccak family
  1577. :: ::::
  1578. ++ keccak
  1579. |%
  1580. ::
  1581. :: keccak
  1582. ::
  1583. ++ keccak-224 ~/ %k224 |=(a=octs (keccak 1.152 448 224 a))
  1584. ++ keccak-256 ~/ %k256 |=(a=octs (keccak 1.088 512 256 a))
  1585. ++ keccak-384 ~/ %k384 |=(a=octs (keccak 832 768 384 a))
  1586. ++ keccak-512 ~/ %k512 |=(a=octs (keccak 576 1.024 512 a))
  1587. ::
  1588. ++ keccak (cury (cury hash keccak-f) padding-keccak)
  1589. ::
  1590. ++ padding-keccak (multirate-padding 0x1)
  1591. ::
  1592. :: sha3
  1593. ::
  1594. ++ sha3-224 |=(a=octs (sha3 1.152 448 224 a))
  1595. ++ sha3-256 |=(a=octs (sha3 1.088 512 256 a))
  1596. ++ sha3-384 |=(a=octs (sha3 832 768 384 a))
  1597. ++ sha3-512 |=(a=octs (sha3 576 1.024 512 a))
  1598. ::
  1599. ++ sha3 (cury (cury hash keccak-f) padding-sha3)
  1600. ::
  1601. ++ padding-sha3 (multirate-padding 0x6)
  1602. ::
  1603. :: shake
  1604. ::
  1605. ++ shake-128 |=([o=@ud i=octs] (shake 1.344 256 o i))
  1606. ++ shake-256 |=([o=@ud i=octs] (shake 1.088 512 o i))
  1607. ::
  1608. ++ shake (cury (cury hash keccak-f) padding-shake)
  1609. ::
  1610. ++ padding-shake (multirate-padding 0x1f)
  1611. ::
  1612. :: rawshake
  1613. ::
  1614. ++ rawshake-128 |=([o=@ud i=octs] (rawshake 1.344 256 o i))
  1615. ++ rawshake-256 |=([o=@ud i=octs] (rawshake 1.088 512 o i))
  1616. ::
  1617. ++ rawshake (cury (cury hash keccak-f) padding-rawshake)
  1618. ::
  1619. ++ padding-rawshake (multirate-padding 0x7)
  1620. ::
  1621. :: core
  1622. ::
  1623. ++ hash
  1624. :: per: permutation function with configurable width.
  1625. :: pad: padding function.
  1626. :: rat: bitrate, size in bits of blocks to operate on.
  1627. :: cap: capacity, bits of sponge padding.
  1628. :: out: length of desired output, in bits.
  1629. :: inp: input to hash.
  1630. |= $: per=$-(@ud $-(@ @))
  1631. pad=$-([octs @ud] octs)
  1632. rat=@ud
  1633. cap=@ud
  1634. out=@ud
  1635. inp=octs
  1636. ==
  1637. ^- @
  1638. :: urbit's little-endian to keccak's big-endian.
  1639. =. q.inp (rev 3 inp)
  1640. %. [inp out]
  1641. (sponge per pad rat cap)
  1642. ::
  1643. ::NOTE if ++keccak ever needs to be made to operate
  1644. :: on bits rather than bytes, all that needs to
  1645. :: be done is updating the way this padding
  1646. :: function works. (and also "octs" -> "bits")
  1647. ++ multirate-padding
  1648. :: dsb: domain separation byte, reverse bit order.
  1649. |= dsb=@ux
  1650. ?> (lte dsb 0xff)
  1651. |= [inp=octs mut=@ud]
  1652. ^- octs
  1653. =. mut (div mut 8)
  1654. =+ pal=(sub mut (mod p.inp mut))
  1655. =? pal =(pal 0) mut
  1656. =. pal (dec pal)
  1657. :- (add p.inp +(pal))
  1658. :: padding is provided in lane bit ordering,
  1659. :: ie, LSB = left.
  1660. (cat 3 (con (lsh [3 pal] dsb) 0x80) q.inp)
  1661. ::
  1662. ++ sponge
  1663. :: sponge construction
  1664. ::
  1665. :: preperm: permutation function with configurable width.
  1666. :: padding: padding function.
  1667. :: bitrate: size of blocks to operate on.
  1668. :: capacity: sponge padding.
  1669. |= $: preperm=$-(@ud $-(@ @))
  1670. padding=$-([octs @ud] octs)
  1671. bitrate=@ud
  1672. capacity=@ud
  1673. ==
  1674. ::
  1675. :: preparing
  1676. =+ bitrate-bytes=(div bitrate 8)
  1677. =+ blockwidth=(add bitrate capacity)
  1678. =+ permute=(preperm blockwidth)
  1679. ::
  1680. |= [input=octs output=@ud]
  1681. |^ ^- @
  1682. ::
  1683. :: padding
  1684. =. input (padding input bitrate)
  1685. ::
  1686. :: absorbing
  1687. =/ pieces=(list @)
  1688. :: amount of bitrate-sized blocks.
  1689. ?> =(0 (mod p.input bitrate-bytes))
  1690. =+ i=(div p.input bitrate-bytes)
  1691. |-
  1692. ?: =(i 0) ~
  1693. :_ $(i (dec i))
  1694. :: get the bitrate-sized block of bytes
  1695. :: that ends with the byte at -.
  1696. =- (cut 3 [- bitrate-bytes] q.input)
  1697. (mul (dec i) bitrate-bytes)
  1698. =/ state=@
  1699. :: for every piece,
  1700. %+ roll pieces
  1701. |= [p=@ s=@]
  1702. :: pad with capacity,
  1703. =. p (lsh [0 capacity] p)
  1704. :: xor it into the state and permute it.
  1705. (permute (mix s (bytes-to-lanes p)))
  1706. ::
  1707. :: squeezing
  1708. =| res=@
  1709. =| len=@ud
  1710. |-
  1711. :: append a bitrate-sized head of state to the
  1712. :: result.
  1713. =. res
  1714. %+ con (lsh [0 bitrate] res)
  1715. (rsh [0 capacity] (lanes-to-bytes state))
  1716. =. len (add len bitrate)
  1717. ?: (gte len output)
  1718. :: produce the requested bits of output.
  1719. (rsh [0 (sub len output)] res)
  1720. $(res res, state (permute state))
  1721. ::
  1722. ++ bytes-to-lanes
  1723. :: flip byte order in blocks of 8 bytes.
  1724. |= a=@
  1725. %^ run 6 a
  1726. |=(b=@ (lsh [3 (sub 8 (met 3 b))] (swp 3 b)))
  1727. ::
  1728. ++ lanes-to-bytes
  1729. :: unflip byte order in blocks of 8 bytes.
  1730. |= a=@
  1731. %+ can 6
  1732. %+ turn
  1733. =+ (rip 6 a)
  1734. (weld - (reap (sub 25 (lent -)) 0x0))
  1735. |= a=@
  1736. :- 1
  1737. %+ can 3
  1738. =- (turn - |=(a=@ [1 a]))
  1739. =+ (flop (rip 3 a))
  1740. (weld (reap (sub 8 (lent -)) 0x0) -)
  1741. --
  1742. ::
  1743. ++ keccak-f
  1744. :: keccak permutation function
  1745. |= [width=@ud]
  1746. :: assert valid blockwidth.
  1747. ?> =- (~(has in -) width)
  1748. (sy 25 50 100 200 400 800 1.600 ~)
  1749. :: assumes 5x5 lanes state, as is the keccak
  1750. :: standard.
  1751. =+ size=5
  1752. =+ lanes=(mul size size)
  1753. =+ lane-bloq=(dec (xeb (div width lanes)))
  1754. =+ lane-size=(bex lane-bloq)
  1755. =+ rounds=(add 12 (mul 2 lane-bloq))
  1756. |= [input=@]
  1757. ^- @
  1758. =* a input
  1759. =+ round=0
  1760. |^
  1761. ?: =(round rounds) a
  1762. ::
  1763. :: theta
  1764. =/ c=@
  1765. %+ roll (gulf 0 (dec size))
  1766. |= [x=@ud c=@]
  1767. %+ con (lsh [lane-bloq 1] c)
  1768. %+ roll (gulf 0 (dec size))
  1769. |= [y=@ud c=@]
  1770. (mix c (get-lane x y a))
  1771. =/ d=@
  1772. %+ roll (gulf 0 (dec size))
  1773. |= [x=@ud d=@]
  1774. %+ con (lsh [lane-bloq 1] d)
  1775. %+ mix
  1776. =- (get-word - size c)
  1777. ?:(=(x 0) (dec size) (dec x))
  1778. %^ ~(rol fe lane-bloq) 0 1
  1779. (get-word (mod +(x) size) size c)
  1780. =. a
  1781. %+ roll (gulf 0 (dec lanes))
  1782. |= [i=@ud a=_a]
  1783. %+ mix a
  1784. %+ lsh
  1785. [lane-bloq (sub lanes +(i))]
  1786. (get-word i size d)
  1787. ::
  1788. :: rho and pi
  1789. =/ b=@
  1790. %+ roll (gulf 0 (dec lanes))
  1791. |= [i=@ b=@]
  1792. =+ x=(mod i 5)
  1793. =+ y=(div i 5)
  1794. %+ con b
  1795. %+ lsh
  1796. :- lane-bloq
  1797. %+ sub lanes
  1798. %+ add +(y)
  1799. %+ mul size
  1800. (mod (add (mul 2 x) (mul 3 y)) size)
  1801. %^ ~(rol fe lane-bloq) 0
  1802. (rotation-offset i)
  1803. (get-word i lanes a)
  1804. ::
  1805. :: chi
  1806. =. a
  1807. %+ roll (gulf 0 (dec lanes))
  1808. |= [i=@ud a=@]
  1809. %+ con (lsh lane-bloq a)
  1810. =+ x=(mod i 5)
  1811. =+ y=(div i 5)
  1812. %+ mix (get-lane x y b)
  1813. %+ dis
  1814. =- (get-lane - y b)
  1815. (mod (add x 2) size)
  1816. %^ not lane-bloq 1
  1817. (get-lane (mod +(x) size) y b)
  1818. ::
  1819. :: iota
  1820. =. a
  1821. =+ (round-constant round)
  1822. (mix a (lsh [lane-bloq (dec lanes)] -))
  1823. ::
  1824. :: next round
  1825. $(round +(round))
  1826. ::
  1827. ++ get-lane
  1828. :: get the lane with coordinates
  1829. |= [x=@ud y=@ud a=@]
  1830. =+ i=(add x (mul size y))
  1831. (get-word i lanes a)
  1832. ::
  1833. ++ get-word
  1834. :: get word {n} from atom {a} of {m} words.
  1835. |= [n=@ud m=@ud a=@]
  1836. (cut lane-bloq [(sub m +((mod n m))) 1] a)
  1837. ::
  1838. ++ round-constant
  1839. |= c=@ud
  1840. =- (snag (mod c 24) -)
  1841. ^- (list @ux)
  1842. :~ 0x1
  1843. 0x8082
  1844. 0x8000.0000.0000.808a
  1845. 0x8000.0000.8000.8000
  1846. 0x808b
  1847. 0x8000.0001
  1848. 0x8000.0000.8000.8081
  1849. 0x8000.0000.0000.8009
  1850. 0x8a
  1851. 0x88
  1852. 0x8000.8009
  1853. 0x8000.000a
  1854. 0x8000.808b
  1855. 0x8000.0000.0000.008b
  1856. 0x8000.0000.0000.8089
  1857. 0x8000.0000.0000.8003
  1858. 0x8000.0000.0000.8002
  1859. 0x8000.0000.0000.0080
  1860. 0x800a
  1861. 0x8000.0000.8000.000a
  1862. 0x8000.0000.8000.8081
  1863. 0x8000.0000.0000.8080
  1864. 0x8000.0001
  1865. 0x8000.0000.8000.8008
  1866. ==
  1867. ::
  1868. ++ rotation-offset
  1869. |= x=@ud
  1870. =- (snag x -)
  1871. ^- (list @ud)
  1872. :~ 0 1 62 28 27
  1873. 36 44 6 55 20
  1874. 3 10 43 25 39
  1875. 41 45 15 21 8
  1876. 18 2 61 56 14
  1877. ==
  1878. --
  1879. -- ::keccak
  1880. :: ::
  1881. :::: ++hmac:crypto :: (2b8) hmac family
  1882. :: ::::
  1883. ++ hmac
  1884. =, sha
  1885. => |%
  1886. ++ meet |=([k=@ m=@] [[(met 3 k) k] [(met 3 m) m]])
  1887. ++ flip |=([k=@ m=@] [(swp 3 k) (swp 3 m)])
  1888. --
  1889. |%
  1890. ::
  1891. :: use with @
  1892. ::
  1893. ++ hmac-sha1 (cork meet hmac-sha1l)
  1894. ++ hmac-sha256 (cork meet hmac-sha256l)
  1895. ++ hmac-sha512 (cork meet hmac-sha512l)
  1896. ::
  1897. :: use with @t
  1898. ::
  1899. ++ hmac-sha1t (cork flip hmac-sha1)
  1900. ++ hmac-sha256t (cork flip hmac-sha256)
  1901. ++ hmac-sha512t (cork flip hmac-sha512)
  1902. ::
  1903. :: use with byts
  1904. ::
  1905. ++ hmac-sha1l (cury hmac sha-1l 64 20)
  1906. ++ hmac-sha256l (cury hmac sha-256l 64 32)
  1907. ++ hmac-sha512l (cury hmac sha-512l 128 64)
  1908. ::
  1909. :: main logic
  1910. ::
  1911. ++ hmac
  1912. ::~/ %hmac
  1913. :: boq: block size in bytes used by haj
  1914. :: out: bytes output by haj
  1915. |* [[haj=$-([@u @] @) boq=@u out=@u] key=byts msg=byts]
  1916. :: ensure key and message fit signaled lengths
  1917. =. dat.key (end [3 wid.key] dat.key)
  1918. =. dat.msg (end [3 wid.msg] dat.msg)
  1919. :: keys longer than block size are shortened by hashing
  1920. =? dat.key (gth wid.key boq) (haj wid.key dat.key)
  1921. =? wid.key (gth wid.key boq) out
  1922. :: keys shorter than block size are right-padded
  1923. =? dat.key (lth wid.key boq) (lsh [3 (sub boq wid.key)] dat.key)
  1924. :: pad key, inner and outer
  1925. =+ kip=(mix dat.key (fil 3 boq 0x36))
  1926. =+ kop=(mix dat.key (fil 3 boq 0x5c))
  1927. :: append inner padding to message, then hash
  1928. =+ (haj (add wid.msg boq) (add (lsh [3 wid.msg] kip) dat.msg))
  1929. :: prepend outer padding to result, hash again
  1930. (haj (add out boq) (add (lsh [3 out] kop) -))
  1931. -- :: hmac
  1932. :: ::
  1933. :::: ++secp:crypto :: (2b9) secp family
  1934. :: ::::
  1935. ++ secp !.
  1936. :: TODO: as-octs and hmc are outside of jet parent
  1937. => :+ .
  1938. hmc=hmac-sha256l:hmac:crypto
  1939. as-octs=as-octs:wrap
  1940. |%
  1941. +$ jacobian [x=@ y=@ z=@] :: jacobian point
  1942. +$ point [x=@ y=@] :: curve point
  1943. +$ domain
  1944. $: p=@ :: prime modulo
  1945. a=@ :: y^2=x^3+ax+b
  1946. b=@ ::
  1947. g=point :: base point
  1948. n=@ :: prime order of g
  1949. ==
  1950. ++ secp
  1951. |_ [bytes=@ =domain]
  1952. ++ field-p ~(. fo p.domain)
  1953. ++ field-n ~(. fo n.domain)
  1954. ++ compress-point
  1955. |= =point
  1956. ^- @
  1957. %+ can 3
  1958. :~ [bytes x.point]
  1959. [1 (add 2 (cut 0 [0 1] y.point))]
  1960. ==
  1961. ::
  1962. ++ serialize-point
  1963. |= =point
  1964. ^- @
  1965. %+ can 3
  1966. :~ [bytes y.point]
  1967. [bytes x.point]
  1968. [1 4]
  1969. ==
  1970. ::
  1971. ++ decompress-point
  1972. |= compressed=@
  1973. ^- point
  1974. =/ x=@ (end [3 bytes] compressed)
  1975. ?> =(3 (mod p.domain 4))
  1976. =/ fop field-p
  1977. =+ [fadd fmul fpow]=[sum.fop pro.fop exp.fop]
  1978. =/ y=@ %+ fpow (rsh [0 2] +(p.domain))
  1979. %+ fadd b.domain
  1980. %+ fadd (fpow 3 x)
  1981. (fmul a.domain x)
  1982. =/ s=@ (rsh [3 bytes] compressed)
  1983. ~| [`@ux`s `@ux`compressed]
  1984. ?> |(=(2 s) =(3 s))
  1985. :: check parity
  1986. ::
  1987. =? y !=((sub s 2) (mod y 2))
  1988. (sub p.domain y)
  1989. [x y]
  1990. ::
  1991. ++ jc :: jacobian math
  1992. |%
  1993. ++ from
  1994. |= a=jacobian
  1995. ^- point
  1996. =/ fop field-p
  1997. =+ [fmul fpow finv]=[pro.fop exp.fop inv.fop]
  1998. =/ z (finv z.a)
  1999. :- (fmul x.a (fpow 2 z))
  2000. (fmul y.a (fpow 3 z))
  2001. ::
  2002. ++ into
  2003. |= point
  2004. ^- jacobian
  2005. [x y 1]
  2006. ::
  2007. ++ double
  2008. |= jacobian
  2009. ^- jacobian
  2010. ?: =(0 y) [0 0 0]
  2011. =/ fop field-p
  2012. =+ [fadd fsub fmul fpow]=[sum.fop dif.fop pro.fop exp.fop]
  2013. =/ s :(fmul 4 x (fpow 2 y))
  2014. =/ m %+ fadd
  2015. (fmul 3 (fpow 2 x))
  2016. (fmul a.domain (fpow 4 z))
  2017. =/ nx %+ fsub
  2018. (fpow 2 m)
  2019. (fmul 2 s)
  2020. =/ ny %+ fsub
  2021. (fmul m (fsub s nx))
  2022. (fmul 8 (fpow 4 y))
  2023. =/ nz :(fmul 2 y z)
  2024. [nx ny nz]
  2025. ::
  2026. ++ add
  2027. |= [a=jacobian b=jacobian]
  2028. ^- jacobian
  2029. ?: =(0 y.a) b
  2030. ?: =(0 y.b) a
  2031. =/ fop field-p
  2032. =+ [fadd fsub fmul fpow]=[sum.fop dif.fop pro.fop exp.fop]
  2033. =/ u1 :(fmul x.a z.b z.b)
  2034. =/ u2 :(fmul x.b z.a z.a)
  2035. =/ s1 :(fmul y.a z.b z.b z.b)
  2036. =/ s2 :(fmul y.b z.a z.a z.a)
  2037. ?: =(u1 u2)
  2038. ?. =(s1 s2)
  2039. [0 0 1]
  2040. (double a)
  2041. =/ h (fsub u2 u1)
  2042. =/ r (fsub s2 s1)
  2043. =/ h2 (fmul h h)
  2044. =/ h3 (fmul h2 h)
  2045. =/ u1h2 (fmul u1 h2)
  2046. =/ nx %+ fsub
  2047. (fmul r r)
  2048. :(fadd h3 u1h2 u1h2)
  2049. =/ ny %+ fsub
  2050. (fmul r (fsub u1h2 nx))
  2051. (fmul s1 h3)
  2052. =/ nz :(fmul h z.a z.b)
  2053. [nx ny nz]
  2054. ::
  2055. ++ mul
  2056. |= [a=jacobian scalar=@]
  2057. ^- jacobian
  2058. ?: =(0 y.a)
  2059. [0 0 1]
  2060. ?: =(0 scalar)
  2061. [0 0 1]
  2062. ?: =(1 scalar)
  2063. a
  2064. ?: (gte scalar n.domain)
  2065. $(scalar (mod scalar n.domain))
  2066. ?: =(0 (mod scalar 2))
  2067. (double $(scalar (rsh 0 scalar)))
  2068. (add a (double $(scalar (rsh 0 scalar))))
  2069. --
  2070. ++ add-points
  2071. |= [a=point b=point]
  2072. ^- point
  2073. =/ j jc
  2074. (from.j (add.j (into.j a) (into.j b)))
  2075. ++ mul-point-scalar
  2076. |= [p=point scalar=@]
  2077. ^- point
  2078. =/ j jc
  2079. %- from.j
  2080. %+ mul.j
  2081. (into.j p)
  2082. scalar
  2083. ::
  2084. ++ valid-hash
  2085. |= has=@
  2086. (lte (met 3 has) bytes)
  2087. ::
  2088. ++ in-order
  2089. |= i=@
  2090. ?& (gth i 0)
  2091. (lth i n.domain)
  2092. ==
  2093. ++ priv-to-pub
  2094. |= private-key=@
  2095. ^- point
  2096. ?> (in-order private-key)
  2097. (mul-point-scalar g.domain private-key)
  2098. ::
  2099. ++ make-k
  2100. |= [hash=@ private-key=@]
  2101. ^- @
  2102. ?> (in-order private-key)
  2103. ?> (valid-hash hash)
  2104. =/ v (fil 3 bytes 1)
  2105. =/ k 0
  2106. =. k %+ hmc [bytes k]
  2107. %- as-octs
  2108. %+ can 3
  2109. :~ [bytes hash]
  2110. [bytes private-key]
  2111. [1 0]
  2112. [bytes v]
  2113. ==
  2114. =. v (hmc bytes^k bytes^v)
  2115. =. k %+ hmc [bytes k]
  2116. %- as-octs
  2117. %+ can 3
  2118. :~ [bytes hash]
  2119. [bytes private-key]
  2120. [1 1]
  2121. [bytes v]
  2122. ==
  2123. =. v (hmc bytes^k bytes^v)
  2124. (hmc bytes^k bytes^v)
  2125. ::
  2126. ++ ecdsa-raw-sign
  2127. |= [hash=@ private-key=@]
  2128. ^- [r=@ s=@ y=@]
  2129. :: make-k and priv-to pub will validate inputs
  2130. =/ k (make-k hash private-key)
  2131. =/ rp (priv-to-pub k)
  2132. =* r x.rp
  2133. ?< =(0 r)
  2134. =/ fon field-n
  2135. =+ [fadd fmul finv]=[sum.fon pro.fon inv.fon]
  2136. =/ s %+ fmul (finv k)
  2137. %+ fadd hash
  2138. %+ fmul r
  2139. private-key
  2140. ?< =(0 s)
  2141. [r s y.rp]
  2142. :: general recovery omitted, but possible
  2143. --
  2144. ++ secp256k1
  2145. ~% %secp256k1 + ~
  2146. |%
  2147. ++ t :: in the battery for jet matching
  2148. ^- domain
  2149. :* 0xffff.ffff.ffff.ffff.ffff.ffff.ffff.ffff.
  2150. ffff.ffff.ffff.ffff.ffff.fffe.ffff.fc2f
  2151. 0
  2152. 7
  2153. :- 0x79be.667e.f9dc.bbac.55a0.6295.ce87.0b07.
  2154. 029b.fcdb.2dce.28d9.59f2.815b.16f8.1798
  2155. 0x483a.da77.26a3.c465.5da4.fbfc.0e11.08a8.
  2156. fd17.b448.a685.5419.9c47.d08f.fb10.d4b8
  2157. 0xffff.ffff.ffff.ffff.ffff.ffff.ffff.fffe.
  2158. baae.dce6.af48.a03b.bfd2.5e8c.d036.4141
  2159. ==
  2160. ::
  2161. ++ curve ~(. secp 32 t)
  2162. ++ serialize-point serialize-point:curve
  2163. ++ compress-point compress-point:curve
  2164. ++ decompress-point decompress-point:curve
  2165. ++ add-points add-points:curve
  2166. ++ mul-point-scalar mul-point-scalar:curve
  2167. ++ make-k
  2168. ~/ %make
  2169. |= [hash=@uvI private-key=@]
  2170. :: checks sizes
  2171. (make-k:curve hash private-key)
  2172. ++ priv-to-pub
  2173. |= private-key=@
  2174. :: checks sizes
  2175. (priv-to-pub:curve private-key)
  2176. ::
  2177. ++ ecdsa-raw-sign
  2178. ~/ %sign
  2179. |= [hash=@uvI private-key=@]
  2180. ^- [v=@ r=@ s=@]
  2181. =/ c curve
  2182. :: raw-sign checks sizes
  2183. =+ (ecdsa-raw-sign.c hash private-key)
  2184. =/ rp=point [r y]
  2185. =/ s-high (gte (mul 2 s) n.domain.c)
  2186. =? s s-high
  2187. (sub n.domain.c s)
  2188. =? rp s-high
  2189. [x.rp (sub p.domain.c y.rp)]
  2190. =/ v (end 0 y.rp)
  2191. =? v (gte x.rp n.domain.c)
  2192. (add v 2)
  2193. [v x.rp s]
  2194. ::
  2195. ++ ecdsa-raw-recover
  2196. ~/ %reco
  2197. |= [hash=@ sig=[v=@ r=@ s=@]]
  2198. ^- point
  2199. ?> (lte v.sig 3)
  2200. =/ c curve
  2201. ?> (valid-hash.c hash)
  2202. ?> (in-order.c r.sig)
  2203. ?> (in-order.c s.sig)
  2204. =/ x ?: (gte v.sig 2)
  2205. (add r.sig n.domain.c)
  2206. r.sig
  2207. =/ fop field-p.c
  2208. =+ [fadd fmul fpow]=[sum.fop pro.fop exp.fop]
  2209. =/ ysq (fadd (fpow 3 x) b.domain.c)
  2210. =/ beta (fpow (rsh [0 2] +(p.domain.c)) ysq)
  2211. =/ y ?: =((end 0 v.sig) (end 0 beta))
  2212. beta
  2213. (sub p.domain.c beta)
  2214. ?> =(0 (dif.fop ysq (fmul y y)))
  2215. =/ nz (sub n.domain.c hash)
  2216. =/ j jc.c
  2217. =/ gz (mul.j (into.j g.domain.c) nz)
  2218. =/ xy (mul.j (into.j x y) s.sig)
  2219. =/ qr (add.j gz xy)
  2220. =/ qj (mul.j qr (inv:field-n.c x))
  2221. =/ pub (from.j qj)
  2222. ?< =([0 0] pub)
  2223. pub
  2224. ++ schnorr
  2225. ::~% %schnorr ..schnorr ~
  2226. => |%
  2227. ++ tagged-hash
  2228. |= [tag=@ [l=@ x=@]]
  2229. =+ hat=(sha-256:sha (swp 3 tag))
  2230. %- sha-256l:sha
  2231. :- (add 64 l)
  2232. (can 3 ~[[l x] [32 hat] [32 hat]])
  2233. ++ lift-x
  2234. |= x=@I
  2235. ^- (unit point)
  2236. =/ c curve
  2237. ?. (lth x p.domain.c)
  2238. ~
  2239. =/ fop field-p.c
  2240. =+ [fadd fpow]=[sum.fop exp.fop]
  2241. =/ cp (fadd (fpow 3 x) 7)
  2242. =/ y (fpow (rsh [0 2] +(p.domain.c)) cp)
  2243. ?. =(cp (fpow 2 y))
  2244. ~
  2245. %- some :- x
  2246. ?: =(0 (mod y 2))
  2247. y
  2248. (sub p.domain.c y)
  2249. --
  2250. |%
  2251. ::
  2252. ++ sign :: schnorr signature
  2253. ~/ %sosi
  2254. |= [sk=@I m=@I a=@I]
  2255. ^- @J
  2256. ?> (gte 32 (met 3 m))
  2257. ?> (gte 32 (met 3 a))
  2258. =/ c curve
  2259. :: implies (gte 32 (met 3 sk))
  2260. ::
  2261. ?< |(=(0 sk) (gte sk n.domain.c))
  2262. =/ pp
  2263. (mul-point-scalar g.domain.c sk)
  2264. =/ d
  2265. ?: =(0 (mod y.pp 2))
  2266. sk
  2267. (sub n.domain.c sk)
  2268. =/ t
  2269. %+ mix d
  2270. (tagged-hash 'BIP0340/aux' [32 a])
  2271. =/ rand
  2272. %+ tagged-hash 'BIP0340/nonce'
  2273. :- 96
  2274. (rep 8 ~[m x.pp t])
  2275. =/ kp (mod rand n.domain.c)
  2276. ?< =(0 kp)
  2277. =/ rr (mul-point-scalar g.domain.c kp)
  2278. =/ k
  2279. ?: =(0 (mod y.rr 2))
  2280. kp
  2281. (sub n.domain.c kp)
  2282. =/ e
  2283. %- mod
  2284. :_ n.domain.c
  2285. %+ tagged-hash 'BIP0340/challenge'
  2286. :- 96
  2287. (rep 8 ~[m x.pp x.rr])
  2288. =/ sig
  2289. %^ cat 8
  2290. (mod (add k (mul e d)) n.domain.c)
  2291. x.rr
  2292. ?> (verify x.pp m sig)
  2293. sig
  2294. ::
  2295. ++ verify :: schnorr verify
  2296. ~/ %sove
  2297. |= [pk=@I m=@I sig=@J]
  2298. ^- ?
  2299. ?> (gte 32 (met 3 pk))
  2300. ?> (gte 32 (met 3 m))
  2301. ?> (gte 64 (met 3 sig))
  2302. =/ c curve
  2303. =/ pup (lift-x pk)
  2304. ?~ pup
  2305. %.n
  2306. =/ pp u.pup
  2307. =/ r (cut 8 [1 1] sig)
  2308. ?: (gte r p.domain.c)
  2309. %.n
  2310. =/ s (end 8 sig)
  2311. ?: (gte s n.domain.c)
  2312. %.n
  2313. =/ e
  2314. %- mod
  2315. :_ n.domain.c
  2316. %+ tagged-hash 'BIP0340/challenge'
  2317. :- 96
  2318. (rep 8 ~[m x.pp r])
  2319. =/ aa
  2320. (mul-point-scalar g.domain.c s)
  2321. =/ bb
  2322. (mul-point-scalar pp (sub n.domain.c e))
  2323. ?: &(=(x.aa x.bb) !=(y.aa y.bb)) :: infinite?
  2324. %.n
  2325. =/ rr (add-points aa bb)
  2326. ?. =(0 (mod y.rr 2))
  2327. %.n
  2328. =(r x.rr)
  2329. --
  2330. --
  2331. --
  2332. ::
  2333. ++ blake
  2334. ~% %blake ..crypto ~
  2335. |%
  2336. ++ blake3
  2337. =<
  2338. =< hash :: cuter API
  2339. =+ [cv=iv flags=0b0]
  2340. ^? ~/ %blake3
  2341. |%
  2342. ::
  2343. ++ keyed |=(key=octs hash(cv q.key, flags f-keyedhash))
  2344. ::
  2345. ++ kdf
  2346. |= [out=@ud ctx=tape seed=octs]
  2347. ^- @ux
  2348. =/ der (hash(cv iv, flags f-derivekeyctx) 32 (lent ctx)^(crip ctx))
  2349. (hash(cv der, flags f-derivekeymat) out seed)
  2350. ::
  2351. ++ hash
  2352. ~/ %hash
  2353. |= [out=@ud msg=octs]
  2354. ^- @ux
  2355. =/ root (root-output (turn (split-octs 13 msg) chunk-output))
  2356. %+ end [3 out]
  2357. %+ rep 9
  2358. %+ turn (gulf 0 (div out 64))
  2359. |=(i=@ (compress root(counter i)))
  2360. ::
  2361. ++ root-output
  2362. |= outputs=(list output)
  2363. ^- output
  2364. %+ set-flag f-root
  2365. |-
  2366. =/ mid (div (bex (xeb (dec (lent outputs)))) 2)
  2367. =+ [l=(scag mid outputs) r=(slag mid outputs)]
  2368. ?> ?=(^ outputs)
  2369. ?~ t.outputs i.outputs
  2370. %- parent-output
  2371. [(compress $(outputs l)) (compress $(outputs r))]
  2372. ::
  2373. ++ parent-output
  2374. |= [l=@ux r=@ux]
  2375. ^- output
  2376. %+ set-flag f-parent
  2377. [cv 0 (rep 8 ~[l r]) 64 flags]
  2378. ::
  2379. ++ chunk-output
  2380. ~/ %chunk-output
  2381. |= [counter=@ chunk=octs]
  2382. ^- output
  2383. %+ set-flag f-chunkend
  2384. %+ roll (split-octs 9 chunk)
  2385. |= [[i=@ block=octs] prev=output]
  2386. ?: =(0 i) [cv counter q.block p.block (con flags f-chunkstart)]
  2387. [(output-cv prev) counter q.block p.block flags]
  2388. --
  2389. ::~% %blake3-impl ..blake3 ~
  2390. |%
  2391. ::
  2392. +$ output
  2393. $: cv=@ux
  2394. counter=@ud
  2395. block=@ux
  2396. blocklen=@ud
  2397. flags=@ub
  2398. ==
  2399. ::
  2400. ++ compress
  2401. ~/ %compress
  2402. |= output
  2403. ^- @
  2404. |^
  2405. =/ state (can32 [8 cv] [4 iv] [2 counter] [1 blocklen] [1 flags] ~)
  2406. =. state (round state block) =. block (permute block)
  2407. =. state (round state block) =. block (permute block)
  2408. =. state (round state block) =. block (permute block)
  2409. =. state (round state block) =. block (permute block)
  2410. =. state (round state block) =. block (permute block)
  2411. =. state (round state block) =. block (permute block)
  2412. =. state (round state block) (mix state (rep 8 ~[(rsh 8 state) cv]))
  2413. ::
  2414. ++ round
  2415. |= [state=@ block=@]
  2416. ^+ state
  2417. |^
  2418. =. state (g 0x0 0x4 0x8 0xc 0x0 0x1)
  2419. =. state (g 0x1 0x5 0x9 0xd 0x2 0x3)
  2420. =. state (g 0x2 0x6 0xa 0xe 0x4 0x5)
  2421. =. state (g 0x3 0x7 0xb 0xf 0x6 0x7)
  2422. =. state (g 0x0 0x5 0xa 0xf 0x8 0x9)
  2423. =. state (g 0x1 0x6 0xb 0xc 0xa 0xb)
  2424. =. state (g 0x2 0x7 0x8 0xd 0xc 0xd)
  2425. =. state (g 0x3 0x4 0x9 0xe 0xe 0xf)
  2426. state
  2427. ::
  2428. ++ g
  2429. |= [a=@ b=@ c=@ d=@ mx=@ my=@]
  2430. ^+ state
  2431. =. state (set a :(sum32 (get a) (get b) (getb mx)))
  2432. =. state (set d (rox (get d) (get a) 16))
  2433. =. state (set c :(sum32 (get c) (get d)))
  2434. =. state (set b (rox (get b) (get c) 12))
  2435. =. state (set a :(sum32 (get a) (get b) (getb my)))
  2436. =. state (set d (rox (get d) (get a) 8))
  2437. =. state (set c :(sum32 (get c) (get d)))
  2438. =. state (set b (rox (get b) (get c) 7))
  2439. state
  2440. ::
  2441. ++ getb (curr get32 block)
  2442. ++ get (curr get32 state)
  2443. ++ set |=([i=@ w=@] (set32 i w state))
  2444. ++ rox |=([a=@ b=@ n=@] (ror32 n (mix a b)))
  2445. --
  2446. ::
  2447. ++ permute
  2448. |= block=@
  2449. ^+ block
  2450. (rep 5 (turn perm (curr get32 block)))
  2451. --
  2452. :: constants and helpers
  2453. ::
  2454. ++ iv 0x5be0.cd19.1f83.d9ab.9b05.688c.510e.527f.
  2455. a54f.f53a.3c6e.f372.bb67.ae85.6a09.e667
  2456. ++ perm (rip 2 0x8fe9.5cb1.d407.a362)
  2457. ++ f-chunkstart ^~ (bex 0)
  2458. ++ f-chunkend ^~ (bex 1)
  2459. ++ f-parent ^~ (bex 2)
  2460. ++ f-root ^~ (bex 3)
  2461. ++ f-keyedhash ^~ (bex 4)
  2462. ++ f-derivekeyctx ^~ (bex 5)
  2463. ++ f-derivekeymat ^~ (bex 6)
  2464. ++ set-flag |=([f=@ o=output] o(flags (con flags.o f)))
  2465. ++ fe32 ~(. fe 5)
  2466. ++ ror32 (cury ror:fe32 0)
  2467. ++ sum32 sum:fe32
  2468. ++ can32 (cury can 5)
  2469. ++ get32 |=([i=@ a=@] (cut 5 [i 1] a))
  2470. ++ set32 |=([i=@ w=@ a=@] (sew 5 [i 1 w] a))
  2471. ++ output-cv |=(o=output `@ux`(rep 8 ~[(compress o)]))
  2472. ++ split-octs
  2473. |= [a=bloq msg=octs]
  2474. ^- (list [i=@ octs])
  2475. ?> ?=(@ q.msg) :: simplfy jet logic
  2476. =/ per (bex (sub a 3))
  2477. =| chunk-octs=(list [i=@ octs])
  2478. =| i=@
  2479. |-
  2480. ?: (lte p.msg per) [[i msg] chunk-octs]
  2481. :- [i per^(end a q.msg)]
  2482. $(i +(i), msg (sub p.msg per)^(rsh a q.msg))
  2483. -- :: blake3-impl
  2484. ::
  2485. ::TODO generalize for both blake2 variants
  2486. ++ blake2b
  2487. ::~/ %blake2b
  2488. |= [msg=byts key=byts out=@ud]
  2489. ^- @
  2490. :: initialization vector
  2491. =/ iv=@
  2492. 0x6a09.e667.f3bc.c908.
  2493. bb67.ae85.84ca.a73b.
  2494. 3c6e.f372.fe94.f82b.
  2495. a54f.f53a.5f1d.36f1.
  2496. 510e.527f.ade6.82d1.
  2497. 9b05.688c.2b3e.6c1f.
  2498. 1f83.d9ab.fb41.bd6b.
  2499. 5be0.cd19.137e.2179
  2500. :: per-round constants
  2501. =/ sigma=(list (list @ud))
  2502. :~
  2503. :~ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ==
  2504. :~ 14 10 4 8 9 15 13 6 1 12 0 2 11 7 5 3 ==
  2505. :~ 11 8 12 0 5 2 15 13 10 14 3 6 7 1 9 4 ==
  2506. :~ 7 9 3 1 13 12 11 14 2 6 5 10 4 0 15 8 ==
  2507. :~ 9 0 5 7 2 4 10 15 14 1 11 12 6 8 3 13 ==
  2508. :~ 2 12 6 10 0 11 8 3 4 13 7 5 15 14 1 9 ==
  2509. :~ 12 5 1 15 14 13 4 10 0 7 6 3 9 2 8 11 ==
  2510. :~ 13 11 7 14 12 1 3 9 5 0 15 4 8 6 2 10 ==
  2511. :~ 6 15 14 9 11 3 0 8 12 2 13 7 1 4 10 5 ==
  2512. :~ 10 2 8 4 7 6 1 5 15 11 9 14 3 12 13 0 ==
  2513. :~ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ==
  2514. :~ 14 10 4 8 9 15 13 6 1 12 0 2 11 7 5 3 ==
  2515. ==
  2516. => |%
  2517. ++ get-word-list
  2518. |= [h=@ w=@ud]
  2519. ^- (list @)
  2520. %- flop
  2521. =+ l=(rip 6 h)
  2522. =- (weld - l)
  2523. (reap (sub w (lent l)) 0)
  2524. ::
  2525. ++ get-word
  2526. |= [h=@ i=@ud w=@ud]
  2527. ^- @
  2528. %+ snag i
  2529. (get-word-list h w)
  2530. ::
  2531. ++ put-word
  2532. |= [h=@ i=@ud w=@ud d=@]
  2533. ^- @
  2534. %+ rep 6
  2535. =+ l=(get-word-list h w)
  2536. %- flop
  2537. %+ weld (scag i l)
  2538. [d (slag +(i) l)]
  2539. ::
  2540. ++ mod-word
  2541. |* [h=@ i=@ud w=@ud g=$-(@ @)]
  2542. (put-word h i w (g (get-word h i w)))
  2543. ::
  2544. ++ pad
  2545. |= [byts len=@ud]
  2546. (lsh [3 (sub len wid)] dat)
  2547. ::
  2548. ++ compress
  2549. |= [h=@ c=@ t=@ud l=?]
  2550. ^- @
  2551. :: set up local work vector
  2552. =+ v=(add (lsh [6 8] h) iv)
  2553. :: xor the counter t into v
  2554. =. v
  2555. %- mod-word
  2556. :^ v 12 16
  2557. (cury mix (end [0 64] t))
  2558. =. v
  2559. %- mod-word
  2560. :^ v 13 16
  2561. (cury mix (rsh [0 64] t))
  2562. :: for the last block, invert v14
  2563. =? v l
  2564. %- mod-word
  2565. :^ v 14 16
  2566. (cury mix 0xffff.ffff.ffff.ffff)
  2567. :: twelve rounds of message mixing
  2568. =+ i=0
  2569. =| s=(list @)
  2570. |^
  2571. ?: =(i 12)
  2572. :: xor upper and lower halves of v into state h
  2573. =. h (mix h (rsh [6 8] v))
  2574. (mix h (end [6 8] v))
  2575. :: select message mixing schedule and mix v
  2576. =. s (snag (mod i 10) sigma)
  2577. =. v (do-mix 0 4 8 12 0 1)
  2578. =. v (do-mix 1 5 9 13 2 3)
  2579. =. v (do-mix 2 6 10 14 4 5)
  2580. =. v (do-mix 3 7 11 15 6 7)
  2581. =. v (do-mix 0 5 10 15 8 9)
  2582. =. v (do-mix 1 6 11 12 10 11)
  2583. =. v (do-mix 2 7 8 13 12 13)
  2584. =. v (do-mix 3 4 9 14 14 15)
  2585. $(i +(i))
  2586. ::
  2587. ++ do-mix
  2588. |= [na=@ nb=@ nc=@ nd=@ nx=@ ny=@]
  2589. ^- @
  2590. =- =. v (put-word v na 16 a)
  2591. =. v (put-word v nb 16 b)
  2592. =. v (put-word v nc 16 c)
  2593. (put-word v nd 16 d)
  2594. %- b2mix
  2595. :* (get-word v na 16)
  2596. (get-word v nb 16)
  2597. (get-word v nc 16)
  2598. (get-word v nd 16)
  2599. (get-word c (snag nx s) 16)
  2600. (get-word c (snag ny s) 16)
  2601. ==
  2602. --
  2603. ::
  2604. ++ b2mix
  2605. |= [a=@ b=@ c=@ d=@ x=@ y=@]
  2606. ^- [a=@ b=@ c=@ d=@]
  2607. =. x (rev 3 8 x)
  2608. =. y (rev 3 8 y)
  2609. =+ fed=~(. fe 6)
  2610. =. a :(sum:fed a b x)
  2611. =. d (ror:fed 0 32 (mix d a))
  2612. =. c (sum:fed c d)
  2613. =. b (ror:fed 0 24 (mix b c))
  2614. =. a :(sum:fed a b y)
  2615. =. d (ror:fed 0 16 (mix d a))
  2616. =. c (sum:fed c d)
  2617. =. b (ror:fed 0 63 (mix b c))
  2618. [a b c d]
  2619. --
  2620. :: ensure inputs adhere to contraints
  2621. =. out (max 1 (min out 64))
  2622. =. wid.msg (min wid.msg (bex 128))
  2623. =. wid.key (min wid.key 64)
  2624. =. dat.msg (end [3 wid.msg] dat.msg)
  2625. =. dat.key (end [3 wid.key] dat.key)
  2626. :: initialize state vector
  2627. =+ h=iv
  2628. :: mix key length and output length into h0
  2629. =. h
  2630. %- mod-word
  2631. :^ h 0 8
  2632. %+ cury mix
  2633. %+ add 0x101.0000
  2634. (add (lsh 3 wid.key) out)
  2635. :: keep track of how much we've compressed
  2636. =* mes dat.msg
  2637. =+ com=0
  2638. =+ rem=wid.msg
  2639. :: if we have a key, pad it and prepend to msg
  2640. =? mes (gth wid.key 0)
  2641. (can 3 ~[rem^mes 128^(pad key 128)])
  2642. =? rem (gth wid.key 0)
  2643. (add rem 128)
  2644. |-
  2645. :: compress 128-byte chunks of the message
  2646. ?: (gth rem 128)
  2647. =+ c=(cut 3 [(sub rem 128) 128] mes)
  2648. =. com (add com 128)
  2649. %_ $
  2650. rem (sub rem 128)
  2651. h (compress h c com |)
  2652. ==
  2653. :: compress the final bytes of the msg
  2654. =+ c=(cut 3 [0 rem] mes)
  2655. =. com (add com rem)
  2656. =. c (pad [rem c] 128)
  2657. =. h (compress h c com &)
  2658. :: produce output of desired length
  2659. %+ rsh [3 (sub 64 out)]
  2660. :: do some word
  2661. %+ rep 6
  2662. %+ turn (flop (gulf 0 7))
  2663. |= a=@
  2664. (rev 3 8 (get-word h a 8))
  2665. -- ::blake
  2666. ::
  2667. ++ argon2
  2668. ~% %argon ..crypto ~
  2669. |%
  2670. ::
  2671. :: structures
  2672. ::
  2673. +$ argon-type ?(%d %i %id %u)
  2674. ::
  2675. :: shorthands
  2676. ::
  2677. ++ argon2-nockchain
  2678. ^- $-([msg=byts sat=byts] @)
  2679. %: argon2
  2680. out=32
  2681. typ=%d
  2682. version=0x13
  2683. threads=4
  2684. mem-cost=786.432 :: 6GiB
  2685. time-cost=6
  2686. key=*byts
  2687. extra=*byts
  2688. ==
  2689. :: argon2 proper
  2690. ::
  2691. :: main argon2 operation
  2692. ++ argon2
  2693. :: out: desired output size in bytes
  2694. :: typ: argon2 type
  2695. :: version: argon2 version (0x10/v1.0 or 0x13/v1.3)
  2696. :: threads: amount of threads/parallelism
  2697. :: mem-cost: kb of memory to use
  2698. :: time-cost: iterations to run
  2699. :: key: optional secret
  2700. :: extra: optional arbitrary data
  2701. |= $: out=@ud
  2702. typ=argon-type
  2703. version=@ux
  2704. ::
  2705. threads=@ud
  2706. mem-cost=@ud
  2707. time-cost=@ud
  2708. ::
  2709. key=byts
  2710. extra=byts
  2711. ==
  2712. ^- $-([msg=byts sat=byts] @)
  2713. ::
  2714. :: check configuration sanity
  2715. ::
  2716. ?: =(0 threads)
  2717. ~| %parallelism-must-be-above-zero
  2718. !!
  2719. ?: =(0 time-cost)
  2720. ~| %time-cost-must-be-above-zero
  2721. !!
  2722. ?: (lth mem-cost (mul 8 threads))
  2723. ~| :- %memory-cost-must-be-at-least-threads
  2724. [threads %times 8 (mul 8 threads)]
  2725. !!
  2726. ?. |(=(0x10 version) =(0x13 version))
  2727. ~| [%unsupported-version version %want [0x10 0x13]]
  2728. !!
  2729. ::
  2730. :: main function
  2731. ::
  2732. :: msg: the main input
  2733. :: sat: optional salt
  2734. ~% %argon2 ..argon2 ~
  2735. |= [msg=byts sat=byts]
  2736. ^- @
  2737. ::
  2738. :: calculate constants and initialize buffer
  2739. ::
  2740. :: for each thread, there is a row in the buffer.
  2741. :: the amount of columns depends on the memory-cost.
  2742. :: columns are split into groups of four.
  2743. :: a single such quarter section of a row is a segment.
  2744. ::
  2745. :: blocks: (m_prime)
  2746. :: columns: row length (q)
  2747. :: seg-length: segment length
  2748. =/ blocks=@ud
  2749. :: round mem-cost down to the nearest multiple of 4*threads
  2750. =+ (mul 4 threads)
  2751. (mul (div mem-cost -) -)
  2752. =+ columns=(div blocks threads)
  2753. =+ seg-length=(div columns 4)
  2754. ::
  2755. =/ buffer=(list (list @))
  2756. (reap threads (reap columns 0))
  2757. ?: (lth wid.sat 8)
  2758. ~| [%min-salt-length-is-8 wid.sat]
  2759. !!
  2760. ::
  2761. :: h0: initial 64-byte block
  2762. =/ h0=@
  2763. =- (blake2b:blake - 0^0 64)
  2764. :- :(add 40 wid.msg wid.sat wid.key wid.extra)
  2765. %+ can 3
  2766. =+ (cury (cury rev 3) 4)
  2767. :~ (prep-wid extra)
  2768. (prep-wid key)
  2769. (prep-wid sat)
  2770. (prep-wid msg)
  2771. 4^(- (type-to-num typ))
  2772. 4^(- version)
  2773. 4^(- time-cost)
  2774. 4^(- mem-cost)
  2775. 4^(- out)
  2776. 4^(- threads)
  2777. ==
  2778. ::
  2779. :: do time-cost passes over the buffer
  2780. ::
  2781. =+ t=0
  2782. |-
  2783. ?: (lth t time-cost)
  2784. ::
  2785. :: process all four segments in the columns...
  2786. ::
  2787. =+ s=0
  2788. |-
  2789. ?. (lth s 4) ^$(t +(t))
  2790. ::
  2791. :: ...of every row/thread
  2792. ::
  2793. =+ r=0
  2794. |-
  2795. ?. (lth r threads) ^$(s +(s))
  2796. =; new=_buffer
  2797. $(buffer new, r +(r))
  2798. %- fill-segment
  2799. :* buffer h0
  2800. t s r
  2801. blocks columns seg-length
  2802. threads time-cost typ version
  2803. ==
  2804. ::
  2805. :: mix all rows together and hash the result
  2806. ::
  2807. =+ r=0
  2808. =| final=@
  2809. |-
  2810. ?: =(r threads)
  2811. (hash 1.024^final out)
  2812. =- $(final -, r +(r))
  2813. %+ mix final
  2814. (snag (dec columns) (snag r buffer))
  2815. ::
  2816. :: per-segment computation
  2817. ++ fill-segment
  2818. |= $: buffer=(list (list @))
  2819. h0=@
  2820. ::
  2821. itn=@ud
  2822. seg=@ud
  2823. row=@ud
  2824. ::
  2825. blocks=@ud
  2826. columns=@ud
  2827. seg-length=@ud
  2828. ::
  2829. threads=@ud
  2830. time-cost=@ud
  2831. typ=argon-type
  2832. version=@ux
  2833. ==
  2834. ::
  2835. :: fill-segment utilities
  2836. ::
  2837. => |%
  2838. ++ put-word
  2839. |= [rob=(list @) i=@ud d=@]
  2840. %+ weld (scag i rob)
  2841. [d (slag +(i) rob)]
  2842. --
  2843. ^+ buffer
  2844. ::
  2845. :: rob: row buffer to operate on
  2846. :: do-i: whether to use prns from input rather than state
  2847. :: rands: prns generated from input, if we do-i
  2848. =+ rob=(snag row buffer)
  2849. =/ do-i=?
  2850. ?| ?=(%i typ)
  2851. &(?=(%id typ) =(0 itn) (lte seg 1))
  2852. &(?=(%u typ) =(0 itn) (lte seg 2))
  2853. ==
  2854. =/ rands=(list (pair @ @))
  2855. ?. do-i ~
  2856. ::
  2857. :: keep going until we have a list of :seg-length prn pairs
  2858. ::
  2859. =+ l=0
  2860. =+ counter=1
  2861. |- ^- (list (pair @ @))
  2862. ?: (gte l seg-length) ~
  2863. =- (weld - $(counter +(counter), l (add l 128)))
  2864. ::
  2865. :: generate pseudorandom block by compressing metadata
  2866. ::
  2867. =/ random-block=@
  2868. %+ compress 0
  2869. %+ compress 0
  2870. %+ lsh [3 968]
  2871. %+ rep 6
  2872. =+ (cury (cury rev 3) 8)
  2873. :~ (- counter)
  2874. (- (type-to-num typ))
  2875. (- time-cost)
  2876. (- blocks)
  2877. (- seg)
  2878. (- row)
  2879. (- itn)
  2880. ==
  2881. ::
  2882. :: split the random-block into 64-bit sections,
  2883. :: then extract the first two 4-byte sections from each.
  2884. ::
  2885. %+ turn (flop (rip 6 random-block))
  2886. |= a=@
  2887. ^- (pair @ @)
  2888. :- (rev 3 4 (rsh 5 a))
  2889. (rev 3 4 (end 5 a))
  2890. ::
  2891. :: iterate over the entire segment length
  2892. ::
  2893. =+ sin=0
  2894. |-
  2895. ::
  2896. :: when done, produce the updated buffer
  2897. ::
  2898. ?: =(sin seg-length)
  2899. %+ weld (scag row buffer)
  2900. [rob (slag +(row) buffer)]
  2901. ::
  2902. :: col: current column to process
  2903. =/ col=@ud
  2904. (add (mul seg seg-length) sin)
  2905. ::
  2906. :: first two columns are generated from h0
  2907. ::
  2908. ?: &(=(0 itn) (lth col 2))
  2909. =+ (app-num (app-num 64^h0 col) row)
  2910. =+ (hash - 1.024)
  2911. $(rob (put-word rob col -), sin +(sin))
  2912. ::
  2913. :: c1, c2: prns for picking reference block
  2914. =/ [c1=@ c2=@]
  2915. ?: do-i (snag sin rands)
  2916. =+ =- (snag - rob)
  2917. ?: =(0 col) (dec columns)
  2918. (mod (dec col) columns)
  2919. :- (rev 3 4 (cut 3 [1.020 4] -))
  2920. (rev 3 4 (cut 3 [1.016 4] -))
  2921. ::
  2922. :: ref-row: reference block row
  2923. =/ ref-row=@ud
  2924. ?: &(=(0 itn) =(0 seg)) row
  2925. (mod c2 threads)
  2926. ::
  2927. :: ref-col: reference block column
  2928. =/ ref-col=@ud
  2929. =- (mod - columns)
  2930. %+ add
  2931. :: starting index
  2932. ?: |(=(0 itn) =(3 seg)) 0
  2933. (mul +(seg) seg-length)
  2934. :: pseudorandom offset
  2935. =- %+ sub (dec -)
  2936. %+ rsh [0 32]
  2937. %+ mul -
  2938. (rsh [0 32] (mul c1 c1))
  2939. :: reference area size
  2940. ?: =(0 itn)
  2941. ?: |(=(0 seg) =(row ref-row)) (dec col)
  2942. ?: =(0 sin) (dec (mul seg seg-length))
  2943. (mul seg seg-length)
  2944. =+ sul=(sub columns seg-length)
  2945. ?: =(ref-row row) (dec (add sul sin))
  2946. ?: =(0 sin) (dec sul)
  2947. sul
  2948. ::
  2949. :: compress the previous and reference block
  2950. :: to create the new block
  2951. ::
  2952. =/ new=@
  2953. %+ compress
  2954. =- (snag - rob)
  2955. :: previous index, wrap-around
  2956. ?: =(0 col) (dec columns)
  2957. (mod (dec col) columns)
  2958. :: get reference block
  2959. %+ snag ref-col
  2960. ?: =(ref-row row) rob
  2961. (snag ref-row buffer)
  2962. ::
  2963. :: starting from v1.3, we xor the new block in,
  2964. :: rather than directly overwriting the old block
  2965. ::
  2966. =? new &(!=(0 itn) =(0x13 version))
  2967. (mix new (snag col rob))
  2968. $(rob (put-word rob col new), sin +(sin))
  2969. ::
  2970. :: compression function (g)
  2971. ++ compress
  2972. :: x, y: assumed to be 1024 bytes
  2973. |= [x=@ y=@]
  2974. ^- @
  2975. ::
  2976. =+ r=(mix x y)
  2977. =| q=(list @)
  2978. ::
  2979. :: iterate over rows of r to get q
  2980. ::
  2981. =+ i=0
  2982. |-
  2983. ?: (lth i 8)
  2984. =; p=(list @)
  2985. $(q (weld q p), i +(i))
  2986. %- permute
  2987. =- (weld (reap (sub 8 (lent -)) 0) -)
  2988. %- flop
  2989. %+ rip 7
  2990. (cut 10 [(sub 7 i) 1] r)
  2991. ::
  2992. :: iterate over columns of q to get z
  2993. ::
  2994. =/ z=(list @) (reap 64 0)
  2995. =. i 0
  2996. |-
  2997. ::
  2998. :: when done, assemble z and xor it with r
  2999. ::
  3000. ?. (lth i 8)
  3001. (mix (rep 7 (flop z)) r)
  3002. ::
  3003. :: permute the column
  3004. ::
  3005. =/ out=(list @)
  3006. %- permute
  3007. :~ (snag i q)
  3008. (snag (add i 8) q)
  3009. (snag (add i 16) q)
  3010. (snag (add i 24) q)
  3011. (snag (add i 32) q)
  3012. (snag (add i 40) q)
  3013. (snag (add i 48) q)
  3014. (snag (add i 56) q)
  3015. ==
  3016. ::
  3017. :: put the result into z per column
  3018. ::
  3019. =+ j=0
  3020. |-
  3021. ?: =(8 j) ^$(i +(i))
  3022. =- $(z -, j +(j))
  3023. =+ (add i (mul j 8))
  3024. %+ weld (scag - z)
  3025. [(snag j out) (slag +(-) z)]
  3026. ::
  3027. :: permutation function (p)
  3028. ++ permute
  3029. ::NOTE this function really just takes and produces
  3030. :: 8 values, but taking and producing them as
  3031. :: lists helps clean up the code significantly.
  3032. |= s=(list @)
  3033. ?> =(8 (lent s))
  3034. ^- (list @)
  3035. ::
  3036. :: list inputs as 16 8-byte values
  3037. ::
  3038. =/ v=(list @)
  3039. %- zing
  3040. ^- (list (list @))
  3041. %+ turn s
  3042. |= a=@
  3043. :: rev for endianness
  3044. =+ (rip 6 (rev 3 16 a))
  3045. (weld - (reap (sub 2 (lent -)) 0))
  3046. ::
  3047. :: do permutation rounds
  3048. ::
  3049. =. v (do-round v 0 4 8 12)
  3050. =. v (do-round v 1 5 9 13)
  3051. =. v (do-round v 2 6 10 14)
  3052. =. v (do-round v 3 7 11 15)
  3053. =. v (do-round v 0 5 10 15)
  3054. =. v (do-round v 1 6 11 12)
  3055. =. v (do-round v 2 7 8 13)
  3056. =. v (do-round v 3 4 9 14)
  3057. :: rev for endianness
  3058. =. v (turn v (cury (cury rev 3) 8))
  3059. ::
  3060. :: cat v back together into 8 16-byte values
  3061. ::
  3062. %+ turn (gulf 0 7)
  3063. |= i=@
  3064. =+ (mul 2 i)
  3065. (cat 6 (snag +(-) v) (snag - v))
  3066. ::
  3067. :: perform a round and produce updated value list
  3068. ++ do-round
  3069. |= [v=(list @) na=@ nb=@ nc=@ nd=@]
  3070. ^+ v
  3071. => |%
  3072. ++ get-word
  3073. |= i=@ud
  3074. (snag i v)
  3075. ::
  3076. ++ put-word
  3077. |= [i=@ud d=@]
  3078. ^+ v
  3079. %+ weld (scag i v)
  3080. [d (slag +(i) v)]
  3081. --
  3082. =- =. v (put-word na a)
  3083. =. v (put-word nb b)
  3084. =. v (put-word nc c)
  3085. (put-word nd d)
  3086. %- round
  3087. :* (get-word na)
  3088. (get-word nb)
  3089. (get-word nc)
  3090. (get-word nd)
  3091. ==
  3092. ::
  3093. :: perform a round (bg) and produce updated values
  3094. ++ round
  3095. |= [a=@ b=@ c=@ d=@]
  3096. ^- [a=@ b=@ c=@ d=@]
  3097. :: operate on 64 bit words
  3098. =+ fed=~(. fe 6)
  3099. =* sum sum:fed
  3100. =* ror ror:fed
  3101. =+ end=(cury end 5)
  3102. =. a :(sum a b :(mul 2 (end a) (end b)))
  3103. =. d (ror 0 32 (mix d a))
  3104. =. c :(sum c d :(mul 2 (end c) (end d)))
  3105. =. b (ror 0 24 (mix b c))
  3106. =. a :(sum a b :(mul 2 (end a) (end b)))
  3107. =. d (ror 0 16 (mix d a))
  3108. =. c :(sum c d :(mul 2 (end c) (end d)))
  3109. =. b (ror 0 63 (mix b c))
  3110. [a b c d]
  3111. ::
  3112. :: argon2 wrapper around blake2b (h')
  3113. ++ hash
  3114. =, blake
  3115. |= [byts out=@ud]
  3116. ^- @
  3117. ::
  3118. :: msg: input with byte-length prepended
  3119. =+ msg=(prep-num [wid dat] out)
  3120. ::
  3121. :: if requested size is low enough, hash directly
  3122. ::
  3123. ?: (lte out 64)
  3124. (blake2b msg 0^0 out)
  3125. ::
  3126. :: build up the result by hashing and re-hashing
  3127. :: the input message, adding the first 32 bytes
  3128. :: of the hash to the result, until we have the
  3129. :: desired output size.
  3130. ::
  3131. =+ tmp=(blake2b msg 0^0 64)
  3132. =+ res=(rsh [3 32] tmp)
  3133. =. out (sub out 32)
  3134. |-
  3135. ?: (gth out 64)
  3136. =. tmp (blake2b 64^tmp 0^0 64)
  3137. =. res (add (lsh [3 32] res) (rsh [3 32] tmp))
  3138. $(out (sub out 32))
  3139. %+ add (lsh [3 out] res)
  3140. (blake2b 64^tmp 0^0 out)
  3141. ::
  3142. :: utilities
  3143. ::
  3144. ++ type-to-num
  3145. |= t=argon-type
  3146. ?- t
  3147. %d 0
  3148. %i 1
  3149. %id 2
  3150. %u 10
  3151. ==
  3152. ::
  3153. ++ app-num
  3154. |= [byts num=@ud]
  3155. ^- byts
  3156. :- (add wid 4)
  3157. %+ can 3
  3158. ~[4^(rev 3 4 num) wid^dat]
  3159. ::
  3160. ++ prep-num
  3161. |= [byts num=@ud]
  3162. ^- byts
  3163. :- (add wid 4)
  3164. %+ can 3
  3165. ~[wid^dat 4^(rev 3 4 num)]
  3166. ::
  3167. ++ prep-wid
  3168. |= a=byts
  3169. (prep-num a wid.a)
  3170. -- :: argon2
  3171. ::
  3172. ++ ripemd
  3173. |%
  3174. ++ ripemd-160
  3175. ~/ %ripemd160
  3176. |= byts
  3177. ^- @
  3178. :: we operate on bits rather than bytes
  3179. =. wid (mul wid 8)
  3180. :: add padding
  3181. =+ (md5-pad wid dat)
  3182. :: endianness
  3183. =. dat (run 5 dat |=(a=@ (rev 3 4 a)))
  3184. =* x dat
  3185. =+ blocks=(div wid 512)
  3186. =+ fev=~(. fe 5)
  3187. :: initial register values
  3188. =+ h0=0x6745.2301
  3189. =+ h1=0xefcd.ab89
  3190. =+ h2=0x98ba.dcfe
  3191. =+ h3=0x1032.5476
  3192. =+ h4=0xc3d2.e1f0
  3193. :: i: current block
  3194. =+ [i=0 j=0]
  3195. =+ *[a=@ b=@ c=@ d=@ e=@] :: a..e
  3196. =+ *[aa=@ bb=@ cc=@ dd=@ ee=@] :: a'..e'
  3197. |^
  3198. ?: =(i blocks)
  3199. %+ rep 5
  3200. %+ turn `(list @)`~[h4 h3 h2 h1 h0]
  3201. :: endianness
  3202. |=(h=@ (rev 3 4 h))
  3203. =: a h0 aa h0
  3204. b h1 bb h1
  3205. c h2 cc h2
  3206. d h3 dd h3
  3207. e h4 ee h4
  3208. ==
  3209. :: j: current word
  3210. =+ j=0
  3211. |-
  3212. ?: =(j 80)
  3213. %= ^$
  3214. i +(i)
  3215. h1 :(sum:fev h2 d ee)
  3216. h2 :(sum:fev h3 e aa)
  3217. h3 :(sum:fev h4 a bb)
  3218. h4 :(sum:fev h0 b cc)
  3219. h0 :(sum:fev h1 c dd)
  3220. ==
  3221. %= $
  3222. j +(j)
  3223. ::
  3224. a e
  3225. b (fn j a b c d e (get (r j)) (k j) (s j))
  3226. c b
  3227. d (rol 10 c)
  3228. e d
  3229. ::
  3230. aa ee
  3231. bb (fn (sub 79 j) aa bb cc dd ee (get (rr j)) (kk j) (ss j))
  3232. cc bb
  3233. dd (rol 10 cc)
  3234. ee dd
  3235. ==
  3236. ::
  3237. ++ get :: word from x in block i
  3238. |= j=@ud
  3239. =+ (add (mul i 16) +(j))
  3240. (cut 5 [(sub (mul blocks 16) -) 1] x)
  3241. ::
  3242. ++ fn
  3243. |= [j=@ud a=@ b=@ c=@ d=@ e=@ m=@ k=@ s=@]
  3244. =- (sum:fev (rol s :(sum:fev a m k -)) e)
  3245. =. j (div j 16)
  3246. ?: =(0 j) (mix (mix b c) d)
  3247. ?: =(1 j) (con (dis b c) (dis (not 0 32 b) d))
  3248. ?: =(2 j) (mix (con b (not 0 32 c)) d)
  3249. ?: =(3 j) (con (dis b d) (dis c (not 0 32 d)))
  3250. ?: =(4 j) (mix b (con c (not 0 32 d)))
  3251. !!
  3252. ::
  3253. ++ rol (cury rol:fev 0)
  3254. ::
  3255. ++ k
  3256. |= j=@ud
  3257. =. j (div j 16)
  3258. ?: =(0 j) 0x0
  3259. ?: =(1 j) 0x5a82.7999
  3260. ?: =(2 j) 0x6ed9.eba1
  3261. ?: =(3 j) 0x8f1b.bcdc
  3262. ?: =(4 j) 0xa953.fd4e
  3263. !!
  3264. ::
  3265. ++ kk :: k'
  3266. |= j=@ud
  3267. =. j (div j 16)
  3268. ?: =(0 j) 0x50a2.8be6
  3269. ?: =(1 j) 0x5c4d.d124
  3270. ?: =(2 j) 0x6d70.3ef3
  3271. ?: =(3 j) 0x7a6d.76e9
  3272. ?: =(4 j) 0x0
  3273. !!
  3274. ::
  3275. ++ r
  3276. |= j=@ud
  3277. %+ snag j
  3278. ^- (list @)
  3279. :~ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  3280. 7 4 13 1 10 6 15 3 12 0 9 5 2 14 11 8
  3281. 3 10 14 4 9 15 8 1 2 7 0 6 13 11 5 12
  3282. 1 9 11 10 0 8 12 4 13 3 7 15 14 5 6 2
  3283. 4 0 5 9 7 12 2 10 14 1 3 8 11 6 15 13
  3284. ==
  3285. ::
  3286. ++ rr :: r'
  3287. |= j=@ud
  3288. %+ snag j
  3289. ^- (list @)
  3290. :~ 5 14 7 0 9 2 11 4 13 6 15 8 1 10 3 12
  3291. 6 11 3 7 0 13 5 10 14 15 8 12 4 9 1 2
  3292. 15 5 1 3 7 14 6 9 11 8 12 2 10 0 4 13
  3293. 8 6 4 1 3 11 15 0 5 12 2 13 9 7 10 14
  3294. 12 15 10 4 1 5 8 7 6 2 13 14 0 3 9 11
  3295. ==
  3296. ::
  3297. ++ s
  3298. |= j=@ud
  3299. %+ snag j
  3300. ^- (list @)
  3301. :~ 11 14 15 12 5 8 7 9 11 13 14 15 6 7 9 8
  3302. 7 6 8 13 11 9 7 15 7 12 15 9 11 7 13 12
  3303. 11 13 6 7 14 9 13 15 14 8 13 6 5 12 7 5
  3304. 11 12 14 15 14 15 9 8 9 14 5 6 8 6 5 12
  3305. 9 15 5 11 6 8 13 12 5 12 13 14 11 8 5 6
  3306. ==
  3307. ::
  3308. ++ ss :: s'
  3309. |= j=@ud
  3310. %+ snag j
  3311. ^- (list @)
  3312. :~ 8 9 9 11 13 15 15 5 7 7 8 11 14 14 12 6
  3313. 9 13 15 7 12 8 9 11 7 7 12 7 6 15 13 11
  3314. 9 7 15 11 8 6 6 14 12 13 5 14 13 13 7 5
  3315. 15 5 8 11 14 14 6 14 6 9 12 9 12 5 15 8
  3316. 8 5 12 9 12 5 14 6 8 13 6 5 15 13 11 11
  3317. ==
  3318. --
  3319. ::
  3320. ++ md5-pad
  3321. |= byts
  3322. ^- byts
  3323. =+ (sub 511 (mod (add wid 64) 512))
  3324. :- :(add 64 +(-) wid)
  3325. %+ can 0
  3326. ~[64^(rev 3 8 wid) +(-)^(lsh [0 -] 1) wid^dat]
  3327. --
  3328. ::
  3329. ++ pbkdf
  3330. => |%
  3331. ++ meet |=([p=@ s=@ c=@ d=@] [[(met 3 p) p] [(met 3 s) s] c d])
  3332. ++ flip |= [p=byts s=byts c=@ d=@]
  3333. [wid.p^(rev 3 p) wid.s^(rev 3 s) c d]
  3334. --
  3335. |%
  3336. ::
  3337. :: use with @
  3338. ::
  3339. ++ hmac-sha1 (cork meet hmac-sha1l)
  3340. ++ hmac-sha256 (cork meet hmac-sha256l)
  3341. ++ hmac-sha512 (cork meet hmac-sha512l)
  3342. ::
  3343. :: use with @t
  3344. ::
  3345. ++ hmac-sha1t (cork meet hmac-sha1d)
  3346. ++ hmac-sha256t (cork meet hmac-sha256d)
  3347. ++ hmac-sha512t (cork meet hmac-sha512d)
  3348. ::
  3349. :: use with byts
  3350. ::
  3351. ++ hmac-sha1l (cork flip hmac-sha1d)
  3352. ++ hmac-sha256l (cork flip hmac-sha256d)
  3353. ++ hmac-sha512l (cork flip hmac-sha512d)
  3354. ::
  3355. :: main logic
  3356. ::
  3357. ++ hmac-sha1d (cury pbkdf hmac-sha1l:hmac 20)
  3358. ++ hmac-sha256d (cury pbkdf hmac-sha256l:hmac 32)
  3359. ++ hmac-sha512d (cury pbkdf hmac-sha512l:hmac 64)
  3360. ::
  3361. ++ pbkdf
  3362. ::TODO jet me! ++hmac:hmac is an example
  3363. |* [[prf=$-([byts byts] @) out=@u] p=byts s=byts c=@ d=@]
  3364. => .(dat.p (end [3 wid.p] dat.p), dat.s (end [3 wid.s] dat.s))
  3365. ::
  3366. :: max key length 1GB
  3367. :: max iterations 2^28
  3368. ::
  3369. ~| [%invalid-pbkdf-params c d]
  3370. ?> ?& (lte d (bex 30))
  3371. (lte c (bex 28))
  3372. !=(c 0)
  3373. ==
  3374. =/ l
  3375. ?~ (mod d out)
  3376. (div d out)
  3377. +((div d out))
  3378. =+ r=(sub d (mul out (dec l)))
  3379. =+ [t=0 j=1 k=1]
  3380. =. t
  3381. |- ^- @
  3382. ?: (gth j l) t
  3383. =/ u
  3384. %+ add dat.s
  3385. %+ lsh [3 wid.s]
  3386. %+ rep 3
  3387. (flop (rpp:scr 3 4 j))
  3388. =+ f=0
  3389. =. f
  3390. |- ^- @
  3391. ?: (gth k c) f
  3392. =/ q
  3393. %^ rev 3 out
  3394. =+ ?:(=(k 1) (add wid.s 4) out)
  3395. (prf [wid.p (rev 3 p)] [- (rev 3 - u)])
  3396. $(u q, f (mix f q), k +(k))
  3397. $(t (add t (lsh [3 (mul (dec j) out)] f)), j +(j))
  3398. (rev 3 d (end [3 d] t))
  3399. --
  3400. -- ::crypto
  3401. ::
  3402. ++ acru $_ ^? :: asym cryptosuite
  3403. |% :: opaque object
  3404. ++ as ^? :: asym ops
  3405. |% ++ seal |~([a=pass b=@] *@) :: encrypt to a
  3406. ++ sign |~(a=@ *@) :: certify as us
  3407. ++ sigh |~(a=@ *@) :: certification only
  3408. ++ sure |~(a=@ *(unit @)) :: authenticate from us
  3409. ++ safe |~([a=@ b=@] *?) :: authentication only
  3410. ++ tear |~([a=pass b=@] *(unit @)) :: accept from a
  3411. -- ::as ::
  3412. ++ de |~([a=@ b=@] *(unit @)) :: symmetric de, soft
  3413. ++ dy |~([a=@ b=@] *@) :: symmetric de, hard
  3414. ++ en |~([a=@ b=@] *@) :: symmetric en
  3415. ++ ex ^? :: export
  3416. |% ++ fig *@uvH :: fingerprint
  3417. ++ pac *@uvG :: default passcode
  3418. ++ pub *pass :: public key
  3419. ++ sec *ring :: private key
  3420. -- ::ex ::
  3421. ++ nu ^? :: reconstructors
  3422. |% ++ pit |~([a=@ b=@] ^?(..nu)) :: from [width seed]
  3423. ++ nol |~(a=ring ^?(..nu)) :: from ring
  3424. ++ com |~(a=pass ^?(..nu)) :: from pass
  3425. -- ::nu ::
  3426. -- ::acru
  3427. +| %system
  3428. :: $puth: $pith without faces
  3429. ::
  3430. +$ puth (pole iota)
  3431. :: +pith: pith utilities
  3432. ::
  3433. ++ trek
  3434. |^ $+(trek pith)
  3435. ++ en-tape
  3436. |= pit=$
  3437. (spud (pout pit))
  3438. ++ sub
  3439. |= [from=$ del=$]
  3440. ~| pith-sub/[from del]
  3441. !.
  3442. |- ^+ from
  3443. ?~ del from
  3444. ?> ?=(^ from)
  3445. ?> =(-.del -.from)
  3446. $(del +.del, from +.from)
  3447. ::
  3448. ++ en-cord
  3449. |= pit=$
  3450. (spat (pout pit))
  3451. ::
  3452. ++ prefix
  3453. =| res=$
  3454. |= [long=$ curt=$]
  3455. ^- (unit _res)
  3456. ?~ curt `(flop res)
  3457. ?~ long ~
  3458. ?. =(i.long i.curt)
  3459. ~
  3460. $(long t.long, curt t.curt, res [i.long res])
  3461. ::
  3462. ++ suffix
  3463. |= [long=$ curt=$]
  3464. ^- _curt
  3465. ?~ curt
  3466. long
  3467. ?~ long
  3468. ~
  3469. $(curt t.curt, long t.long)
  3470. ++ sort
  3471. |= [a=$ b=$]
  3472. (lte (lent a) (lent b))
  3473. --
  3474. :: $pave: better path to pith
  3475. ::
  3476. ++ pave
  3477. |= p=path
  3478. ^- trek
  3479. %+ turn p
  3480. |= i=@ta
  3481. (fall (rush i spot:stip) [%ta i])
  3482. :: $stip: better typed path parser
  3483. ::
  3484. ++ stip
  3485. =< swot
  3486. |%
  3487. ++ swot |=(n=nail `(like trek)`(;~(pfix fas (more fas spot)) n))
  3488. ::
  3489. ++ spot
  3490. %+ sear
  3491. |= a=*
  3492. ^- (unit iota)
  3493. ?+ a ~
  3494. @ ?:(((sane %tas) a) [~ `@tas`a] ~)
  3495. [@ @] ((soft iota) a)
  3496. ==
  3497. %- stew
  3498. ^. stet ^. limo
  3499. :~ :- 'a'^'z' sym
  3500. :- '$' (cold [%tas %$] buc)
  3501. :- '0'^'9' bisk:so
  3502. :- '-' tash:so
  3503. :- '.' zust:so
  3504. :- '~' ;~(pfix sig ;~(pose (stag %da (cook year when:so)) crub:so (easy [%n ~])))
  3505. :- '\'' (stag %t qut) ::'
  3506. ==
  3507. --
  3508. ++ axal
  3509. |$ [item]
  3510. [fil=(unit item) kid=(map iota $)]
  3511. ++ axil
  3512. |$ [item]
  3513. [fil=(unit item) kid=(map trek item)]
  3514. ::
  3515. ++ of
  3516. =| fat=(axal)
  3517. |@
  3518. ::
  3519. ++ anc-jab
  3520. |* [pax=trek fun=$-(* *)]
  3521. ^+ fat
  3522. ?~ pax
  3523. fat
  3524. =? fil.fat ?=(^ fil.fat)
  3525. `(fun u.fil.fat)
  3526. fat(kid (~(put by kid.fat) i.pax $(fat (~(got by kid.fat) i.pax), pax t.pax)))
  3527. ::
  3528. ++ anc
  3529. =| res=(list trek)
  3530. =| cur=trek
  3531. |= pax=trek
  3532. ^- (set trek)
  3533. ?~ pax
  3534. (~(gas in *(set trek)) res)
  3535. =? res ?=(^ fil.fat)
  3536. [cur res]
  3537. $(fat (~(got by kid.fat) i.pax), pax t.pax, cur (snoc cur i.pax))
  3538. ::
  3539. ++ parent
  3540. =| res=(unit trek)
  3541. =| cur=trek
  3542. |= pax=trek
  3543. |- ^+ res
  3544. ?~ pax
  3545. res
  3546. =? res ?=(^ fil.fat)
  3547. `cur
  3548. =/ nex (~(get by kid.fat) i.pax)
  3549. ?~ nex
  3550. res
  3551. $(fat u.nex, pax t.pax, cur (snoc cur i.pax))
  3552. ::
  3553. ++ snip
  3554. |- ^+ fat
  3555. =* loop $
  3556. %_ fat
  3557. kid
  3558. %- ~(run by kid.fat)
  3559. |= f=_fat
  3560. ?^ fil.f
  3561. [`u.fil.f ~]
  3562. loop(fat f)
  3563. ==
  3564. ::
  3565. ++ kid
  3566. |= pax=trek
  3567. ^- (map trek _?>(?=(^ fil.fat) u.fil.fat))
  3568. =. fat (dip pax)
  3569. =. fat snip
  3570. =. fil.fat ~
  3571. tar
  3572. ::
  3573. ++ kids
  3574. |= pax=trek
  3575. ^- (axil _?>(?=(^ fil.fat) u.fil.fat))
  3576. :- (get pax)
  3577. (kid pax)
  3578. ::
  3579. ++ del
  3580. |= pax=trek
  3581. ^+ fat
  3582. ?~ pax [~ kid.fat]
  3583. =/ kid (~(get by kid.fat) i.pax)
  3584. ?~ kid fat
  3585. fat(kid (~(put by kid.fat) i.pax $(fat u.kid, pax t.pax)))
  3586. ::
  3587. :: Descend to the axal at this path
  3588. ::
  3589. ++ dip
  3590. |= pax=trek
  3591. ^+ fat
  3592. ?~ pax fat
  3593. =/ kid (~(get by kid.fat) i.pax)
  3594. ?~ kid [~ ~]
  3595. $(fat u.kid, pax t.pax)
  3596. ::
  3597. ++ gas
  3598. |* lit=(list (pair trek _?>(?=(^ fil.fat) u.fil.fat)))
  3599. ^+ fat
  3600. ?~ lit fat
  3601. $(fat (put p.i.lit q.i.lit), lit t.lit)
  3602. ++ got
  3603. |= pax=trek
  3604. ~| missing-path/pax
  3605. (need (get pax))
  3606. ++ gut
  3607. |* [pax=trek dat=*]
  3608. => .(dat `_?>(?=(^ fil.fat) u.fil.fat)`dat, pax `trek`pax)
  3609. ^+ dat
  3610. (fall (get pax) dat)
  3611. ::
  3612. ++ get
  3613. |= pax=trek
  3614. fil:(dip pax)
  3615. :: Fetch file at longest existing prefix of the path
  3616. ::
  3617. ++ fit
  3618. |= pax=trek
  3619. ^+ [pax fil.fat]
  3620. ?~ pax [~ fil.fat]
  3621. =/ kid (~(get by kid.fat) i.pax)
  3622. ?~ kid [pax fil.fat]
  3623. =/ low $(fat u.kid, pax t.pax)
  3624. ?~ +.low
  3625. [pax fil.fat]
  3626. low
  3627. ::
  3628. ++ has
  3629. |= pax=trek
  3630. !=(~ (get pax))
  3631. :: Delete subtree
  3632. ::
  3633. ++ lop
  3634. |= pax=trek
  3635. ^+ fat
  3636. ?~ pax fat
  3637. |-
  3638. ?~ t.pax fat(kid (~(del by kid.fat) i.pax))
  3639. =/ kid (~(get by kid.fat) i.pax)
  3640. ?~ kid fat
  3641. fat(kid (~(put by kid.fat) i.pax $(fat u.kid, pax t.pax)))
  3642. ::
  3643. ++ put
  3644. |* [pax=trek dat=*]
  3645. => .(dat `_?>(?=(^ fil.fat) u.fil.fat)`dat, pax `trek`pax)
  3646. |- ^+ fat
  3647. ?~ pax fat(fil `dat)
  3648. =/ kid (~(gut by kid.fat) i.pax ^+(fat [~ ~]))
  3649. fat(kid (~(put by kid.fat) i.pax $(fat kid, pax t.pax)))
  3650. ::
  3651. ++ tap
  3652. =| pax=trek
  3653. =| out=(list (pair trek _?>(?=(^ fil.fat) u.fil.fat)))
  3654. |- ^+ out
  3655. =? out ?=(^ fil.fat) :_(out [pax u.fil.fat])
  3656. =/ kid ~(tap by kid.fat)
  3657. |- ^+ out
  3658. ?~ kid out
  3659. %= $
  3660. kid t.kid
  3661. out ^$(pax (weld pax /[p.i.kid]), fat q.i.kid)
  3662. ==
  3663. :: Serialize to map
  3664. ::
  3665. ++ tar
  3666. (~(gas by *(map trek _?>(?=(^ fil.fat) u.fil.fat))) tap)
  3667. --
  3668. --