910-cryptodev_backport.patch 252 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702
  1. --- a/crypto/Kconfig
  2. +++ b/crypto/Kconfig
  3. @@ -65,6 +65,7 @@ config CRYPTO_NULL
  4. config CRYPTO_CRYPTD
  5. tristate "Software async crypto daemon"
  6. select CRYPTO_BLKCIPHER
  7. + select CRYPTO_HASH
  8. select CRYPTO_MANAGER
  9. help
  10. This is a generic software asynchronous crypto daemon that
  11. @@ -212,7 +213,7 @@ comment "Digest"
  12. config CRYPTO_CRC32C
  13. tristate "CRC32c CRC algorithm"
  14. - select CRYPTO_ALGAPI
  15. + select CRYPTO_HASH
  16. select LIBCRC32C
  17. help
  18. Castagnoli, et al Cyclic Redundancy-Check Algorithm. Used
  19. @@ -241,6 +242,57 @@ config CRYPTO_MICHAEL_MIC
  20. should not be used for other purposes because of the weakness
  21. of the algorithm.
  22. +config CRYPTO_RMD128
  23. + tristate "RIPEMD-128 digest algorithm"
  24. + select CRYPTO_ALGAPI
  25. + help
  26. + RIPEMD-128 (ISO/IEC 10118-3:2004).
  27. +
  28. + RIPEMD-128 is a 128-bit cryptographic hash function. It should only
  29. + to be used as a secure replacement for RIPEMD. For other use cases
  30. + RIPEMD-160 should be used.
  31. +
  32. + Developed by Hans Dobbertin, Antoon Bosselaers and Bart Preneel.
  33. + See <http://home.esat.kuleuven.be/~bosselae/ripemd160.html>
  34. +
  35. +config CRYPTO_RMD160
  36. + tristate "RIPEMD-160 digest algorithm"
  37. + select CRYPTO_ALGAPI
  38. + help
  39. + RIPEMD-160 (ISO/IEC 10118-3:2004).
  40. +
  41. + RIPEMD-160 is a 160-bit cryptographic hash function. It is intended
  42. + to be used as a secure replacement for the 128-bit hash functions
  43. + MD4, MD5 and it's predecessor RIPEMD (not to be confused with RIPEMD-128).
  44. +
  45. + It's speed is comparable to SHA1 and there are no known attacks against
  46. + RIPEMD-160.
  47. +
  48. + Developed by Hans Dobbertin, Antoon Bosselaers and Bart Preneel.
  49. + See <http://home.esat.kuleuven.be/~bosselae/ripemd160.html>
  50. +
  51. +config CRYPTO_RMD256
  52. + tristate "RIPEMD-256 digest algorithm"
  53. + select CRYPTO_ALGAPI
  54. + help
  55. + RIPEMD-256 is an optional extension of RIPEMD-128 with a 256 bit hash.
  56. + It is intended for applications that require longer hash-results, without
  57. + needing a larger security level (than RIPEMD-128).
  58. +
  59. + Developed by Hans Dobbertin, Antoon Bosselaers and Bart Preneel.
  60. + See <http://home.esat.kuleuven.be/~bosselae/ripemd160.html>
  61. +
  62. +config CRYPTO_RMD320
  63. + tristate "RIPEMD-320 digest algorithm"
  64. + select CRYPTO_ALGAPI
  65. + help
  66. + RIPEMD-320 is an optional extension of RIPEMD-160 with a 320 bit hash.
  67. + It is intended for applications that require longer hash-results, without
  68. + needing a larger security level (than RIPEMD-160).
  69. +
  70. + Developed by Hans Dobbertin, Antoon Bosselaers and Bart Preneel.
  71. + See <http://home.esat.kuleuven.be/~bosselae/ripemd160.html>
  72. +
  73. config CRYPTO_SHA1
  74. tristate "SHA1 digest algorithm"
  75. select CRYPTO_ALGAPI
  76. @@ -614,6 +666,15 @@ config CRYPTO_LZO
  77. help
  78. This is the LZO algorithm.
  79. +comment "Random Number Generation"
  80. +
  81. +config CRYPTO_PRNG
  82. + tristate "Pseudo Random Number Generation for Cryptographic modules"
  83. + help
  84. + This option enables the generic pseudo random number generator
  85. + for cryptographic modules. Uses the Algorithm specified in
  86. + ANSI X9.31 A.2.4
  87. +
  88. source "drivers/crypto/Kconfig"
  89. endif # if CRYPTO
  90. --- a/crypto/Makefile
  91. +++ b/crypto/Makefile
  92. @@ -19,6 +19,7 @@ obj-$(CONFIG_CRYPTO_BLKCIPHER) += crypto
  93. obj-$(CONFIG_CRYPTO_SEQIV) += seqiv.o
  94. crypto_hash-objs := hash.o
  95. +crypto_hash-objs += ahash.o
  96. obj-$(CONFIG_CRYPTO_HASH) += crypto_hash.o
  97. obj-$(CONFIG_CRYPTO_MANAGER) += cryptomgr.o
  98. @@ -27,6 +28,10 @@ obj-$(CONFIG_CRYPTO_XCBC) += xcbc.o
  99. obj-$(CONFIG_CRYPTO_NULL) += crypto_null.o
  100. obj-$(CONFIG_CRYPTO_MD4) += md4.o
  101. obj-$(CONFIG_CRYPTO_MD5) += md5.o
  102. +obj-$(CONFIG_CRYPTO_RMD128) += rmd128.o
  103. +obj-$(CONFIG_CRYPTO_RMD160) += rmd160.o
  104. +obj-$(CONFIG_CRYPTO_RMD256) += rmd256.o
  105. +obj-$(CONFIG_CRYPTO_RMD320) += rmd320.o
  106. obj-$(CONFIG_CRYPTO_SHA1) += sha1_generic.o
  107. obj-$(CONFIG_CRYPTO_SHA256) += sha256_generic.o
  108. obj-$(CONFIG_CRYPTO_SHA512) += sha512_generic.o
  109. @@ -64,7 +69,7 @@ obj-$(CONFIG_CRYPTO_MICHAEL_MIC) += mich
  110. obj-$(CONFIG_CRYPTO_CRC32C) += crc32c.o
  111. obj-$(CONFIG_CRYPTO_AUTHENC) += authenc.o
  112. obj-$(CONFIG_CRYPTO_LZO) += lzo.o
  113. -
  114. +obj-$(CONFIG_CRYPTO_PRNG) += prng.o
  115. obj-$(CONFIG_CRYPTO_TEST) += tcrypt.o
  116. #
  117. --- /dev/null
  118. +++ b/crypto/ahash.c
  119. @@ -0,0 +1,194 @@
  120. +/*
  121. + * Asynchronous Cryptographic Hash operations.
  122. + *
  123. + * This is the asynchronous version of hash.c with notification of
  124. + * completion via a callback.
  125. + *
  126. + * Copyright (c) 2008 Loc Ho <[email protected]>
  127. + *
  128. + * This program is free software; you can redistribute it and/or modify it
  129. + * under the terms of the GNU General Public License as published by the Free
  130. + * Software Foundation; either version 2 of the License, or (at your option)
  131. + * any later version.
  132. + *
  133. + */
  134. +
  135. +#include <crypto/internal/hash.h>
  136. +#include <crypto/scatterwalk.h>
  137. +#include <linux/err.h>
  138. +#include <linux/kernel.h>
  139. +#include <linux/module.h>
  140. +#include <linux/sched.h>
  141. +#include <linux/slab.h>
  142. +#include <linux/seq_file.h>
  143. +
  144. +#include "internal.h"
  145. +
  146. +static int hash_walk_next(struct crypto_hash_walk *walk)
  147. +{
  148. + unsigned int alignmask = walk->alignmask;
  149. + unsigned int offset = walk->offset;
  150. + unsigned int nbytes = min(walk->entrylen,
  151. + ((unsigned int)(PAGE_SIZE)) - offset);
  152. +
  153. + walk->data = crypto_kmap(walk->pg, 0);
  154. + walk->data += offset;
  155. +
  156. + if (offset & alignmask)
  157. + nbytes = alignmask + 1 - (offset & alignmask);
  158. +
  159. + walk->entrylen -= nbytes;
  160. + return nbytes;
  161. +}
  162. +
  163. +static int hash_walk_new_entry(struct crypto_hash_walk *walk)
  164. +{
  165. + struct scatterlist *sg;
  166. +
  167. + sg = walk->sg;
  168. + walk->pg = sg_page(sg);
  169. + walk->offset = sg->offset;
  170. + walk->entrylen = sg->length;
  171. +
  172. + if (walk->entrylen > walk->total)
  173. + walk->entrylen = walk->total;
  174. + walk->total -= walk->entrylen;
  175. +
  176. + return hash_walk_next(walk);
  177. +}
  178. +
  179. +int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err)
  180. +{
  181. + unsigned int alignmask = walk->alignmask;
  182. + unsigned int nbytes = walk->entrylen;
  183. +
  184. + walk->data -= walk->offset;
  185. +
  186. + if (nbytes && walk->offset & alignmask && !err) {
  187. + walk->offset += alignmask - 1;
  188. + walk->offset = ALIGN(walk->offset, alignmask + 1);
  189. + walk->data += walk->offset;
  190. +
  191. + nbytes = min(nbytes,
  192. + ((unsigned int)(PAGE_SIZE)) - walk->offset);
  193. + walk->entrylen -= nbytes;
  194. +
  195. + return nbytes;
  196. + }
  197. +
  198. + crypto_kunmap(walk->data, 0);
  199. + crypto_yield(walk->flags);
  200. +
  201. + if (err)
  202. + return err;
  203. +
  204. + walk->offset = 0;
  205. +
  206. + if (nbytes)
  207. + return hash_walk_next(walk);
  208. +
  209. + if (!walk->total)
  210. + return 0;
  211. +
  212. + walk->sg = scatterwalk_sg_next(walk->sg);
  213. +
  214. + return hash_walk_new_entry(walk);
  215. +}
  216. +EXPORT_SYMBOL_GPL(crypto_hash_walk_done);
  217. +
  218. +int crypto_hash_walk_first(struct ahash_request *req,
  219. + struct crypto_hash_walk *walk)
  220. +{
  221. + walk->total = req->nbytes;
  222. +
  223. + if (!walk->total)
  224. + return 0;
  225. +
  226. + walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
  227. + walk->sg = req->src;
  228. + walk->flags = req->base.flags;
  229. +
  230. + return hash_walk_new_entry(walk);
  231. +}
  232. +EXPORT_SYMBOL_GPL(crypto_hash_walk_first);
  233. +
  234. +static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
  235. + unsigned int keylen)
  236. +{
  237. + struct ahash_alg *ahash = crypto_ahash_alg(tfm);
  238. + unsigned long alignmask = crypto_ahash_alignmask(tfm);
  239. + int ret;
  240. + u8 *buffer, *alignbuffer;
  241. + unsigned long absize;
  242. +
  243. + absize = keylen + alignmask;
  244. + buffer = kmalloc(absize, GFP_ATOMIC);
  245. + if (!buffer)
  246. + return -ENOMEM;
  247. +
  248. + alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  249. + memcpy(alignbuffer, key, keylen);
  250. + ret = ahash->setkey(tfm, alignbuffer, keylen);
  251. + memset(alignbuffer, 0, keylen);
  252. + kfree(buffer);
  253. + return ret;
  254. +}
  255. +
  256. +static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
  257. + unsigned int keylen)
  258. +{
  259. + struct ahash_alg *ahash = crypto_ahash_alg(tfm);
  260. + unsigned long alignmask = crypto_ahash_alignmask(tfm);
  261. +
  262. + if ((unsigned long)key & alignmask)
  263. + return ahash_setkey_unaligned(tfm, key, keylen);
  264. +
  265. + return ahash->setkey(tfm, key, keylen);
  266. +}
  267. +
  268. +static unsigned int crypto_ahash_ctxsize(struct crypto_alg *alg, u32 type,
  269. + u32 mask)
  270. +{
  271. + return alg->cra_ctxsize;
  272. +}
  273. +
  274. +static int crypto_init_ahash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  275. +{
  276. + struct ahash_alg *alg = &tfm->__crt_alg->cra_ahash;
  277. + struct ahash_tfm *crt = &tfm->crt_ahash;
  278. +
  279. + if (alg->digestsize > PAGE_SIZE / 8)
  280. + return -EINVAL;
  281. +
  282. + crt->init = alg->init;
  283. + crt->update = alg->update;
  284. + crt->final = alg->final;
  285. + crt->digest = alg->digest;
  286. + crt->setkey = ahash_setkey;
  287. + crt->digestsize = alg->digestsize;
  288. +
  289. + return 0;
  290. +}
  291. +
  292. +static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  293. + __attribute__ ((unused));
  294. +static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  295. +{
  296. + seq_printf(m, "type : ahash\n");
  297. + seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  298. + "yes" : "no");
  299. + seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  300. + seq_printf(m, "digestsize : %u\n", alg->cra_hash.digestsize);
  301. +}
  302. +
  303. +const struct crypto_type crypto_ahash_type = {
  304. + .ctxsize = crypto_ahash_ctxsize,
  305. + .init = crypto_init_ahash_ops,
  306. +#ifdef CONFIG_PROC_FS
  307. + .show = crypto_ahash_show,
  308. +#endif
  309. +};
  310. +EXPORT_SYMBOL_GPL(crypto_ahash_type);
  311. +
  312. +MODULE_LICENSE("GPL");
  313. +MODULE_DESCRIPTION("Asynchronous cryptographic hash type");
  314. --- a/crypto/api.c
  315. +++ b/crypto/api.c
  316. @@ -235,8 +235,12 @@ static int crypto_init_ops(struct crypto
  317. return crypto_init_cipher_ops(tfm);
  318. case CRYPTO_ALG_TYPE_DIGEST:
  319. - return crypto_init_digest_ops(tfm);
  320. -
  321. + if ((mask & CRYPTO_ALG_TYPE_HASH_MASK) !=
  322. + CRYPTO_ALG_TYPE_HASH_MASK)
  323. + return crypto_init_digest_ops_async(tfm);
  324. + else
  325. + return crypto_init_digest_ops(tfm);
  326. +
  327. case CRYPTO_ALG_TYPE_COMPRESS:
  328. return crypto_init_compress_ops(tfm);
  329. --- a/crypto/camellia.c
  330. +++ b/crypto/camellia.c
  331. @@ -35,6 +35,8 @@
  332. #include <linux/init.h>
  333. #include <linux/kernel.h>
  334. #include <linux/module.h>
  335. +#include <linux/bitops.h>
  336. +#include <asm/unaligned.h>
  337. static const u32 camellia_sp1110[256] = {
  338. 0x70707000,0x82828200,0x2c2c2c00,0xececec00,
  339. @@ -335,20 +337,6 @@ static const u32 camellia_sp4404[256] =
  340. /*
  341. * macros
  342. */
  343. -#define GETU32(v, pt) \
  344. - do { \
  345. - /* latest breed of gcc is clever enough to use move */ \
  346. - memcpy(&(v), (pt), 4); \
  347. - (v) = be32_to_cpu(v); \
  348. - } while(0)
  349. -
  350. -/* rotation right shift 1byte */
  351. -#define ROR8(x) (((x) >> 8) + ((x) << 24))
  352. -/* rotation left shift 1bit */
  353. -#define ROL1(x) (((x) << 1) + ((x) >> 31))
  354. -/* rotation left shift 1byte */
  355. -#define ROL8(x) (((x) << 8) + ((x) >> 24))
  356. -
  357. #define ROLDQ(ll, lr, rl, rr, w0, w1, bits) \
  358. do { \
  359. w0 = ll; \
  360. @@ -383,7 +371,7 @@ static const u32 camellia_sp4404[256] =
  361. ^ camellia_sp3033[(u8)(il >> 8)] \
  362. ^ camellia_sp4404[(u8)(il )]; \
  363. yl ^= yr; \
  364. - yr = ROR8(yr); \
  365. + yr = ror32(yr, 8); \
  366. yr ^= yl; \
  367. } while(0)
  368. @@ -405,7 +393,7 @@ static void camellia_setup_tail(u32 *sub
  369. subL[7] ^= subL[1]; subR[7] ^= subR[1];
  370. subL[1] ^= subR[1] & ~subR[9];
  371. dw = subL[1] & subL[9],
  372. - subR[1] ^= ROL1(dw); /* modified for FLinv(kl2) */
  373. + subR[1] ^= rol32(dw, 1); /* modified for FLinv(kl2) */
  374. /* round 8 */
  375. subL[11] ^= subL[1]; subR[11] ^= subR[1];
  376. /* round 10 */
  377. @@ -414,7 +402,7 @@ static void camellia_setup_tail(u32 *sub
  378. subL[15] ^= subL[1]; subR[15] ^= subR[1];
  379. subL[1] ^= subR[1] & ~subR[17];
  380. dw = subL[1] & subL[17],
  381. - subR[1] ^= ROL1(dw); /* modified for FLinv(kl4) */
  382. + subR[1] ^= rol32(dw, 1); /* modified for FLinv(kl4) */
  383. /* round 14 */
  384. subL[19] ^= subL[1]; subR[19] ^= subR[1];
  385. /* round 16 */
  386. @@ -430,7 +418,7 @@ static void camellia_setup_tail(u32 *sub
  387. } else {
  388. subL[1] ^= subR[1] & ~subR[25];
  389. dw = subL[1] & subL[25],
  390. - subR[1] ^= ROL1(dw); /* modified for FLinv(kl6) */
  391. + subR[1] ^= rol32(dw, 1); /* modified for FLinv(kl6) */
  392. /* round 20 */
  393. subL[27] ^= subL[1]; subR[27] ^= subR[1];
  394. /* round 22 */
  395. @@ -450,7 +438,7 @@ static void camellia_setup_tail(u32 *sub
  396. subL[26] ^= kw4l; subR[26] ^= kw4r;
  397. kw4l ^= kw4r & ~subR[24];
  398. dw = kw4l & subL[24],
  399. - kw4r ^= ROL1(dw); /* modified for FL(kl5) */
  400. + kw4r ^= rol32(dw, 1); /* modified for FL(kl5) */
  401. }
  402. /* round 17 */
  403. subL[22] ^= kw4l; subR[22] ^= kw4r;
  404. @@ -460,7 +448,7 @@ static void camellia_setup_tail(u32 *sub
  405. subL[18] ^= kw4l; subR[18] ^= kw4r;
  406. kw4l ^= kw4r & ~subR[16];
  407. dw = kw4l & subL[16],
  408. - kw4r ^= ROL1(dw); /* modified for FL(kl3) */
  409. + kw4r ^= rol32(dw, 1); /* modified for FL(kl3) */
  410. /* round 11 */
  411. subL[14] ^= kw4l; subR[14] ^= kw4r;
  412. /* round 9 */
  413. @@ -469,7 +457,7 @@ static void camellia_setup_tail(u32 *sub
  414. subL[10] ^= kw4l; subR[10] ^= kw4r;
  415. kw4l ^= kw4r & ~subR[8];
  416. dw = kw4l & subL[8],
  417. - kw4r ^= ROL1(dw); /* modified for FL(kl1) */
  418. + kw4r ^= rol32(dw, 1); /* modified for FL(kl1) */
  419. /* round 5 */
  420. subL[6] ^= kw4l; subR[6] ^= kw4r;
  421. /* round 3 */
  422. @@ -494,7 +482,7 @@ static void camellia_setup_tail(u32 *sub
  423. SUBKEY_R(6) = subR[5] ^ subR[7];
  424. tl = subL[10] ^ (subR[10] & ~subR[8]);
  425. dw = tl & subL[8], /* FL(kl1) */
  426. - tr = subR[10] ^ ROL1(dw);
  427. + tr = subR[10] ^ rol32(dw, 1);
  428. SUBKEY_L(7) = subL[6] ^ tl; /* round 6 */
  429. SUBKEY_R(7) = subR[6] ^ tr;
  430. SUBKEY_L(8) = subL[8]; /* FL(kl1) */
  431. @@ -503,7 +491,7 @@ static void camellia_setup_tail(u32 *sub
  432. SUBKEY_R(9) = subR[9];
  433. tl = subL[7] ^ (subR[7] & ~subR[9]);
  434. dw = tl & subL[9], /* FLinv(kl2) */
  435. - tr = subR[7] ^ ROL1(dw);
  436. + tr = subR[7] ^ rol32(dw, 1);
  437. SUBKEY_L(10) = tl ^ subL[11]; /* round 7 */
  438. SUBKEY_R(10) = tr ^ subR[11];
  439. SUBKEY_L(11) = subL[10] ^ subL[12]; /* round 8 */
  440. @@ -516,7 +504,7 @@ static void camellia_setup_tail(u32 *sub
  441. SUBKEY_R(14) = subR[13] ^ subR[15];
  442. tl = subL[18] ^ (subR[18] & ~subR[16]);
  443. dw = tl & subL[16], /* FL(kl3) */
  444. - tr = subR[18] ^ ROL1(dw);
  445. + tr = subR[18] ^ rol32(dw, 1);
  446. SUBKEY_L(15) = subL[14] ^ tl; /* round 12 */
  447. SUBKEY_R(15) = subR[14] ^ tr;
  448. SUBKEY_L(16) = subL[16]; /* FL(kl3) */
  449. @@ -525,7 +513,7 @@ static void camellia_setup_tail(u32 *sub
  450. SUBKEY_R(17) = subR[17];
  451. tl = subL[15] ^ (subR[15] & ~subR[17]);
  452. dw = tl & subL[17], /* FLinv(kl4) */
  453. - tr = subR[15] ^ ROL1(dw);
  454. + tr = subR[15] ^ rol32(dw, 1);
  455. SUBKEY_L(18) = tl ^ subL[19]; /* round 13 */
  456. SUBKEY_R(18) = tr ^ subR[19];
  457. SUBKEY_L(19) = subL[18] ^ subL[20]; /* round 14 */
  458. @@ -544,7 +532,7 @@ static void camellia_setup_tail(u32 *sub
  459. } else {
  460. tl = subL[26] ^ (subR[26] & ~subR[24]);
  461. dw = tl & subL[24], /* FL(kl5) */
  462. - tr = subR[26] ^ ROL1(dw);
  463. + tr = subR[26] ^ rol32(dw, 1);
  464. SUBKEY_L(23) = subL[22] ^ tl; /* round 18 */
  465. SUBKEY_R(23) = subR[22] ^ tr;
  466. SUBKEY_L(24) = subL[24]; /* FL(kl5) */
  467. @@ -553,7 +541,7 @@ static void camellia_setup_tail(u32 *sub
  468. SUBKEY_R(25) = subR[25];
  469. tl = subL[23] ^ (subR[23] & ~subR[25]);
  470. dw = tl & subL[25], /* FLinv(kl6) */
  471. - tr = subR[23] ^ ROL1(dw);
  472. + tr = subR[23] ^ rol32(dw, 1);
  473. SUBKEY_L(26) = tl ^ subL[27]; /* round 19 */
  474. SUBKEY_R(26) = tr ^ subR[27];
  475. SUBKEY_L(27) = subL[26] ^ subL[28]; /* round 20 */
  476. @@ -573,17 +561,17 @@ static void camellia_setup_tail(u32 *sub
  477. /* apply the inverse of the last half of P-function */
  478. i = 2;
  479. do {
  480. - dw = SUBKEY_L(i + 0) ^ SUBKEY_R(i + 0); dw = ROL8(dw);/* round 1 */
  481. + dw = SUBKEY_L(i + 0) ^ SUBKEY_R(i + 0); dw = rol32(dw, 8);/* round 1 */
  482. SUBKEY_R(i + 0) = SUBKEY_L(i + 0) ^ dw; SUBKEY_L(i + 0) = dw;
  483. - dw = SUBKEY_L(i + 1) ^ SUBKEY_R(i + 1); dw = ROL8(dw);/* round 2 */
  484. + dw = SUBKEY_L(i + 1) ^ SUBKEY_R(i + 1); dw = rol32(dw, 8);/* round 2 */
  485. SUBKEY_R(i + 1) = SUBKEY_L(i + 1) ^ dw; SUBKEY_L(i + 1) = dw;
  486. - dw = SUBKEY_L(i + 2) ^ SUBKEY_R(i + 2); dw = ROL8(dw);/* round 3 */
  487. + dw = SUBKEY_L(i + 2) ^ SUBKEY_R(i + 2); dw = rol32(dw, 8);/* round 3 */
  488. SUBKEY_R(i + 2) = SUBKEY_L(i + 2) ^ dw; SUBKEY_L(i + 2) = dw;
  489. - dw = SUBKEY_L(i + 3) ^ SUBKEY_R(i + 3); dw = ROL8(dw);/* round 4 */
  490. + dw = SUBKEY_L(i + 3) ^ SUBKEY_R(i + 3); dw = rol32(dw, 8);/* round 4 */
  491. SUBKEY_R(i + 3) = SUBKEY_L(i + 3) ^ dw; SUBKEY_L(i + 3) = dw;
  492. - dw = SUBKEY_L(i + 4) ^ SUBKEY_R(i + 4); dw = ROL8(dw);/* round 5 */
  493. + dw = SUBKEY_L(i + 4) ^ SUBKEY_R(i + 4); dw = rol32(dw, 9);/* round 5 */
  494. SUBKEY_R(i + 4) = SUBKEY_L(i + 4) ^ dw; SUBKEY_L(i + 4) = dw;
  495. - dw = SUBKEY_L(i + 5) ^ SUBKEY_R(i + 5); dw = ROL8(dw);/* round 6 */
  496. + dw = SUBKEY_L(i + 5) ^ SUBKEY_R(i + 5); dw = rol32(dw, 8);/* round 6 */
  497. SUBKEY_R(i + 5) = SUBKEY_L(i + 5) ^ dw; SUBKEY_L(i + 5) = dw;
  498. i += 8;
  499. } while (i < max);
  500. @@ -599,10 +587,10 @@ static void camellia_setup128(const unsi
  501. /**
  502. * k == kll || klr || krl || krr (|| is concatenation)
  503. */
  504. - GETU32(kll, key );
  505. - GETU32(klr, key + 4);
  506. - GETU32(krl, key + 8);
  507. - GETU32(krr, key + 12);
  508. + kll = get_unaligned_be32(key);
  509. + klr = get_unaligned_be32(key + 4);
  510. + krl = get_unaligned_be32(key + 8);
  511. + krr = get_unaligned_be32(key + 12);
  512. /* generate KL dependent subkeys */
  513. /* kw1 */
  514. @@ -707,14 +695,14 @@ static void camellia_setup256(const unsi
  515. * key = (kll || klr || krl || krr || krll || krlr || krrl || krrr)
  516. * (|| is concatenation)
  517. */
  518. - GETU32(kll, key );
  519. - GETU32(klr, key + 4);
  520. - GETU32(krl, key + 8);
  521. - GETU32(krr, key + 12);
  522. - GETU32(krll, key + 16);
  523. - GETU32(krlr, key + 20);
  524. - GETU32(krrl, key + 24);
  525. - GETU32(krrr, key + 28);
  526. + kll = get_unaligned_be32(key);
  527. + klr = get_unaligned_be32(key + 4);
  528. + krl = get_unaligned_be32(key + 8);
  529. + krr = get_unaligned_be32(key + 12);
  530. + krll = get_unaligned_be32(key + 16);
  531. + krlr = get_unaligned_be32(key + 20);
  532. + krrl = get_unaligned_be32(key + 24);
  533. + krrr = get_unaligned_be32(key + 28);
  534. /* generate KL dependent subkeys */
  535. /* kw1 */
  536. @@ -870,13 +858,13 @@ static void camellia_setup192(const unsi
  537. t0 &= ll; \
  538. t2 |= rr; \
  539. rl ^= t2; \
  540. - lr ^= ROL1(t0); \
  541. + lr ^= rol32(t0, 1); \
  542. t3 = krl; \
  543. t1 = klr; \
  544. t3 &= rl; \
  545. t1 |= lr; \
  546. ll ^= t1; \
  547. - rr ^= ROL1(t3); \
  548. + rr ^= rol32(t3, 1); \
  549. } while(0)
  550. #define CAMELLIA_ROUNDSM(xl, xr, kl, kr, yl, yr, il, ir) \
  551. @@ -892,7 +880,7 @@ static void camellia_setup192(const unsi
  552. il ^= kl; \
  553. ir ^= il ^ kr; \
  554. yl ^= ir; \
  555. - yr ^= ROR8(il) ^ ir; \
  556. + yr ^= ror32(il, 8) ^ ir; \
  557. } while(0)
  558. /* max = 24: 128bit encrypt, max = 32: 256bit encrypt */
  559. --- a/crypto/crc32c.c
  560. +++ b/crypto/crc32c.c
  561. @@ -1,24 +1,27 @@
  562. -/*
  563. +/*
  564. * Cryptographic API.
  565. *
  566. * CRC32C chksum
  567. *
  568. * This module file is a wrapper to invoke the lib/crc32c routines.
  569. *
  570. + * Copyright (c) 2008 Herbert Xu <[email protected]>
  571. + *
  572. * This program is free software; you can redistribute it and/or modify it
  573. * under the terms of the GNU General Public License as published by the Free
  574. - * Software Foundation; either version 2 of the License, or (at your option)
  575. + * Software Foundation; either version 2 of the License, or (at your option)
  576. * any later version.
  577. *
  578. */
  579. +
  580. +#include <crypto/internal/hash.h>
  581. #include <linux/init.h>
  582. #include <linux/module.h>
  583. #include <linux/string.h>
  584. -#include <linux/crypto.h>
  585. #include <linux/crc32c.h>
  586. #include <linux/kernel.h>
  587. -#define CHKSUM_BLOCK_SIZE 32
  588. +#define CHKSUM_BLOCK_SIZE 1
  589. #define CHKSUM_DIGEST_SIZE 4
  590. struct chksum_ctx {
  591. @@ -27,7 +30,7 @@ struct chksum_ctx {
  592. };
  593. /*
  594. - * Steps through buffer one byte at at time, calculates reflected
  595. + * Steps through buffer one byte at at time, calculates reflected
  596. * crc using table.
  597. */
  598. @@ -67,11 +70,11 @@ static void chksum_update(struct crypto_
  599. static void chksum_final(struct crypto_tfm *tfm, u8 *out)
  600. {
  601. struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
  602. -
  603. +
  604. *(__le32 *)out = ~cpu_to_le32(mctx->crc);
  605. }
  606. -static int crc32c_cra_init(struct crypto_tfm *tfm)
  607. +static int crc32c_cra_init_old(struct crypto_tfm *tfm)
  608. {
  609. struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
  610. @@ -79,14 +82,14 @@ static int crc32c_cra_init(struct crypto
  611. return 0;
  612. }
  613. -static struct crypto_alg alg = {
  614. +static struct crypto_alg old_alg = {
  615. .cra_name = "crc32c",
  616. .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
  617. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  618. .cra_ctxsize = sizeof(struct chksum_ctx),
  619. .cra_module = THIS_MODULE,
  620. - .cra_list = LIST_HEAD_INIT(alg.cra_list),
  621. - .cra_init = crc32c_cra_init,
  622. + .cra_list = LIST_HEAD_INIT(old_alg.cra_list),
  623. + .cra_init = crc32c_cra_init_old,
  624. .cra_u = {
  625. .digest = {
  626. .dia_digestsize= CHKSUM_DIGEST_SIZE,
  627. @@ -98,14 +101,125 @@ static struct crypto_alg alg = {
  628. }
  629. };
  630. +/*
  631. + * Setting the seed allows arbitrary accumulators and flexible XOR policy
  632. + * If your algorithm starts with ~0, then XOR with ~0 before you set
  633. + * the seed.
  634. + */
  635. +static int crc32c_setkey(struct crypto_ahash *hash, const u8 *key,
  636. + unsigned int keylen)
  637. +{
  638. + u32 *mctx = crypto_ahash_ctx(hash);
  639. +
  640. + if (keylen != sizeof(u32)) {
  641. + crypto_ahash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
  642. + return -EINVAL;
  643. + }
  644. + *mctx = le32_to_cpup((__le32 *)key);
  645. + return 0;
  646. +}
  647. +
  648. +static int crc32c_init(struct ahash_request *req)
  649. +{
  650. + u32 *mctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
  651. + u32 *crcp = ahash_request_ctx(req);
  652. +
  653. + *crcp = *mctx;
  654. + return 0;
  655. +}
  656. +
  657. +static int crc32c_update(struct ahash_request *req)
  658. +{
  659. + struct crypto_hash_walk walk;
  660. + u32 *crcp = ahash_request_ctx(req);
  661. + u32 crc = *crcp;
  662. + int nbytes;
  663. +
  664. + for (nbytes = crypto_hash_walk_first(req, &walk); nbytes;
  665. + nbytes = crypto_hash_walk_done(&walk, 0))
  666. + crc = crc32c(crc, walk.data, nbytes);
  667. +
  668. + *crcp = crc;
  669. + return 0;
  670. +}
  671. +
  672. +static int crc32c_final(struct ahash_request *req)
  673. +{
  674. + u32 *crcp = ahash_request_ctx(req);
  675. +
  676. + *(__le32 *)req->result = ~cpu_to_le32p(crcp);
  677. + return 0;
  678. +}
  679. +
  680. +static int crc32c_digest(struct ahash_request *req)
  681. +{
  682. + struct crypto_hash_walk walk;
  683. + u32 *mctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
  684. + u32 crc = *mctx;
  685. + int nbytes;
  686. +
  687. + for (nbytes = crypto_hash_walk_first(req, &walk); nbytes;
  688. + nbytes = crypto_hash_walk_done(&walk, 0))
  689. + crc = crc32c(crc, walk.data, nbytes);
  690. +
  691. + *(__le32 *)req->result = ~cpu_to_le32(crc);
  692. + return 0;
  693. +}
  694. +
  695. +static int crc32c_cra_init(struct crypto_tfm *tfm)
  696. +{
  697. + u32 *key = crypto_tfm_ctx(tfm);
  698. +
  699. + *key = ~0;
  700. +
  701. + tfm->crt_ahash.reqsize = sizeof(u32);
  702. +
  703. + return 0;
  704. +}
  705. +
  706. +static struct crypto_alg alg = {
  707. + .cra_name = "crc32c",
  708. + .cra_driver_name = "crc32c-generic",
  709. + .cra_priority = 100,
  710. + .cra_flags = CRYPTO_ALG_TYPE_AHASH,
  711. + .cra_blocksize = CHKSUM_BLOCK_SIZE,
  712. + .cra_alignmask = 3,
  713. + .cra_ctxsize = sizeof(u32),
  714. + .cra_module = THIS_MODULE,
  715. + .cra_list = LIST_HEAD_INIT(alg.cra_list),
  716. + .cra_init = crc32c_cra_init,
  717. + .cra_type = &crypto_ahash_type,
  718. + .cra_u = {
  719. + .ahash = {
  720. + .digestsize = CHKSUM_DIGEST_SIZE,
  721. + .setkey = crc32c_setkey,
  722. + .init = crc32c_init,
  723. + .update = crc32c_update,
  724. + .final = crc32c_final,
  725. + .digest = crc32c_digest,
  726. + }
  727. + }
  728. +};
  729. +
  730. static int __init crc32c_mod_init(void)
  731. {
  732. - return crypto_register_alg(&alg);
  733. + int err;
  734. +
  735. + err = crypto_register_alg(&old_alg);
  736. + if (err)
  737. + return err;
  738. +
  739. + err = crypto_register_alg(&alg);
  740. + if (err)
  741. + crypto_unregister_alg(&old_alg);
  742. +
  743. + return err;
  744. }
  745. static void __exit crc32c_mod_fini(void)
  746. {
  747. crypto_unregister_alg(&alg);
  748. + crypto_unregister_alg(&old_alg);
  749. }
  750. module_init(crc32c_mod_init);
  751. --- a/crypto/cryptd.c
  752. +++ b/crypto/cryptd.c
  753. @@ -11,6 +11,7 @@
  754. */
  755. #include <crypto/algapi.h>
  756. +#include <crypto/internal/hash.h>
  757. #include <linux/err.h>
  758. #include <linux/init.h>
  759. #include <linux/kernel.h>
  760. @@ -45,6 +46,13 @@ struct cryptd_blkcipher_request_ctx {
  761. crypto_completion_t complete;
  762. };
  763. +struct cryptd_hash_ctx {
  764. + struct crypto_hash *child;
  765. +};
  766. +
  767. +struct cryptd_hash_request_ctx {
  768. + crypto_completion_t complete;
  769. +};
  770. static inline struct cryptd_state *cryptd_get_state(struct crypto_tfm *tfm)
  771. {
  772. @@ -82,10 +90,8 @@ static void cryptd_blkcipher_crypt(struc
  773. rctx = ablkcipher_request_ctx(req);
  774. - if (unlikely(err == -EINPROGRESS)) {
  775. - rctx->complete(&req->base, err);
  776. - return;
  777. - }
  778. + if (unlikely(err == -EINPROGRESS))
  779. + goto out;
  780. desc.tfm = child;
  781. desc.info = req->info;
  782. @@ -95,8 +101,9 @@ static void cryptd_blkcipher_crypt(struc
  783. req->base.complete = rctx->complete;
  784. +out:
  785. local_bh_disable();
  786. - req->base.complete(&req->base, err);
  787. + rctx->complete(&req->base, err);
  788. local_bh_enable();
  789. }
  790. @@ -261,6 +268,240 @@ out_put_alg:
  791. return inst;
  792. }
  793. +static int cryptd_hash_init_tfm(struct crypto_tfm *tfm)
  794. +{
  795. + struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  796. + struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
  797. + struct crypto_spawn *spawn = &ictx->spawn;
  798. + struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  799. + struct crypto_hash *cipher;
  800. +
  801. + cipher = crypto_spawn_hash(spawn);
  802. + if (IS_ERR(cipher))
  803. + return PTR_ERR(cipher);
  804. +
  805. + ctx->child = cipher;
  806. + tfm->crt_ahash.reqsize =
  807. + sizeof(struct cryptd_hash_request_ctx);
  808. + return 0;
  809. +}
  810. +
  811. +static void cryptd_hash_exit_tfm(struct crypto_tfm *tfm)
  812. +{
  813. + struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  814. + struct cryptd_state *state = cryptd_get_state(tfm);
  815. + int active;
  816. +
  817. + mutex_lock(&state->mutex);
  818. + active = ahash_tfm_in_queue(&state->queue,
  819. + __crypto_ahash_cast(tfm));
  820. + mutex_unlock(&state->mutex);
  821. +
  822. + BUG_ON(active);
  823. +
  824. + crypto_free_hash(ctx->child);
  825. +}
  826. +
  827. +static int cryptd_hash_setkey(struct crypto_ahash *parent,
  828. + const u8 *key, unsigned int keylen)
  829. +{
  830. + struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
  831. + struct crypto_hash *child = ctx->child;
  832. + int err;
  833. +
  834. + crypto_hash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  835. + crypto_hash_set_flags(child, crypto_ahash_get_flags(parent) &
  836. + CRYPTO_TFM_REQ_MASK);
  837. + err = crypto_hash_setkey(child, key, keylen);
  838. + crypto_ahash_set_flags(parent, crypto_hash_get_flags(child) &
  839. + CRYPTO_TFM_RES_MASK);
  840. + return err;
  841. +}
  842. +
  843. +static int cryptd_hash_enqueue(struct ahash_request *req,
  844. + crypto_completion_t complete)
  845. +{
  846. + struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  847. + struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  848. + struct cryptd_state *state =
  849. + cryptd_get_state(crypto_ahash_tfm(tfm));
  850. + int err;
  851. +
  852. + rctx->complete = req->base.complete;
  853. + req->base.complete = complete;
  854. +
  855. + spin_lock_bh(&state->lock);
  856. + err = ahash_enqueue_request(&state->queue, req);
  857. + spin_unlock_bh(&state->lock);
  858. +
  859. + wake_up_process(state->task);
  860. + return err;
  861. +}
  862. +
  863. +static void cryptd_hash_init(struct crypto_async_request *req_async, int err)
  864. +{
  865. + struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  866. + struct crypto_hash *child = ctx->child;
  867. + struct ahash_request *req = ahash_request_cast(req_async);
  868. + struct cryptd_hash_request_ctx *rctx;
  869. + struct hash_desc desc;
  870. +
  871. + rctx = ahash_request_ctx(req);
  872. +
  873. + if (unlikely(err == -EINPROGRESS))
  874. + goto out;
  875. +
  876. + desc.tfm = child;
  877. + desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  878. +
  879. + err = crypto_hash_crt(child)->init(&desc);
  880. +
  881. + req->base.complete = rctx->complete;
  882. +
  883. +out:
  884. + local_bh_disable();
  885. + rctx->complete(&req->base, err);
  886. + local_bh_enable();
  887. +}
  888. +
  889. +static int cryptd_hash_init_enqueue(struct ahash_request *req)
  890. +{
  891. + return cryptd_hash_enqueue(req, cryptd_hash_init);
  892. +}
  893. +
  894. +static void cryptd_hash_update(struct crypto_async_request *req_async, int err)
  895. +{
  896. + struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  897. + struct crypto_hash *child = ctx->child;
  898. + struct ahash_request *req = ahash_request_cast(req_async);
  899. + struct cryptd_hash_request_ctx *rctx;
  900. + struct hash_desc desc;
  901. +
  902. + rctx = ahash_request_ctx(req);
  903. +
  904. + if (unlikely(err == -EINPROGRESS))
  905. + goto out;
  906. +
  907. + desc.tfm = child;
  908. + desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  909. +
  910. + err = crypto_hash_crt(child)->update(&desc,
  911. + req->src,
  912. + req->nbytes);
  913. +
  914. + req->base.complete = rctx->complete;
  915. +
  916. +out:
  917. + local_bh_disable();
  918. + rctx->complete(&req->base, err);
  919. + local_bh_enable();
  920. +}
  921. +
  922. +static int cryptd_hash_update_enqueue(struct ahash_request *req)
  923. +{
  924. + return cryptd_hash_enqueue(req, cryptd_hash_update);
  925. +}
  926. +
  927. +static void cryptd_hash_final(struct crypto_async_request *req_async, int err)
  928. +{
  929. + struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  930. + struct crypto_hash *child = ctx->child;
  931. + struct ahash_request *req = ahash_request_cast(req_async);
  932. + struct cryptd_hash_request_ctx *rctx;
  933. + struct hash_desc desc;
  934. +
  935. + rctx = ahash_request_ctx(req);
  936. +
  937. + if (unlikely(err == -EINPROGRESS))
  938. + goto out;
  939. +
  940. + desc.tfm = child;
  941. + desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  942. +
  943. + err = crypto_hash_crt(child)->final(&desc, req->result);
  944. +
  945. + req->base.complete = rctx->complete;
  946. +
  947. +out:
  948. + local_bh_disable();
  949. + rctx->complete(&req->base, err);
  950. + local_bh_enable();
  951. +}
  952. +
  953. +static int cryptd_hash_final_enqueue(struct ahash_request *req)
  954. +{
  955. + return cryptd_hash_enqueue(req, cryptd_hash_final);
  956. +}
  957. +
  958. +static void cryptd_hash_digest(struct crypto_async_request *req_async, int err)
  959. +{
  960. + struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  961. + struct crypto_hash *child = ctx->child;
  962. + struct ahash_request *req = ahash_request_cast(req_async);
  963. + struct cryptd_hash_request_ctx *rctx;
  964. + struct hash_desc desc;
  965. +
  966. + rctx = ahash_request_ctx(req);
  967. +
  968. + if (unlikely(err == -EINPROGRESS))
  969. + goto out;
  970. +
  971. + desc.tfm = child;
  972. + desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  973. +
  974. + err = crypto_hash_crt(child)->digest(&desc,
  975. + req->src,
  976. + req->nbytes,
  977. + req->result);
  978. +
  979. + req->base.complete = rctx->complete;
  980. +
  981. +out:
  982. + local_bh_disable();
  983. + rctx->complete(&req->base, err);
  984. + local_bh_enable();
  985. +}
  986. +
  987. +static int cryptd_hash_digest_enqueue(struct ahash_request *req)
  988. +{
  989. + return cryptd_hash_enqueue(req, cryptd_hash_digest);
  990. +}
  991. +
  992. +static struct crypto_instance *cryptd_alloc_hash(
  993. + struct rtattr **tb, struct cryptd_state *state)
  994. +{
  995. + struct crypto_instance *inst;
  996. + struct crypto_alg *alg;
  997. +
  998. + alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_HASH,
  999. + CRYPTO_ALG_TYPE_HASH_MASK);
  1000. + if (IS_ERR(alg))
  1001. + return ERR_PTR(PTR_ERR(alg));
  1002. +
  1003. + inst = cryptd_alloc_instance(alg, state);
  1004. + if (IS_ERR(inst))
  1005. + goto out_put_alg;
  1006. +
  1007. + inst->alg.cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC;
  1008. + inst->alg.cra_type = &crypto_ahash_type;
  1009. +
  1010. + inst->alg.cra_ahash.digestsize = alg->cra_hash.digestsize;
  1011. + inst->alg.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
  1012. +
  1013. + inst->alg.cra_init = cryptd_hash_init_tfm;
  1014. + inst->alg.cra_exit = cryptd_hash_exit_tfm;
  1015. +
  1016. + inst->alg.cra_ahash.init = cryptd_hash_init_enqueue;
  1017. + inst->alg.cra_ahash.update = cryptd_hash_update_enqueue;
  1018. + inst->alg.cra_ahash.final = cryptd_hash_final_enqueue;
  1019. + inst->alg.cra_ahash.setkey = cryptd_hash_setkey;
  1020. + inst->alg.cra_ahash.digest = cryptd_hash_digest_enqueue;
  1021. +
  1022. +out_put_alg:
  1023. + crypto_mod_put(alg);
  1024. + return inst;
  1025. +}
  1026. +
  1027. static struct cryptd_state state;
  1028. static struct crypto_instance *cryptd_alloc(struct rtattr **tb)
  1029. @@ -274,6 +515,8 @@ static struct crypto_instance *cryptd_al
  1030. switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
  1031. case CRYPTO_ALG_TYPE_BLKCIPHER:
  1032. return cryptd_alloc_blkcipher(tb, &state);
  1033. + case CRYPTO_ALG_TYPE_DIGEST:
  1034. + return cryptd_alloc_hash(tb, &state);
  1035. }
  1036. return ERR_PTR(-EINVAL);
  1037. --- a/crypto/digest.c
  1038. +++ b/crypto/digest.c
  1039. @@ -12,6 +12,7 @@
  1040. *
  1041. */
  1042. +#include <crypto/internal/hash.h>
  1043. #include <crypto/scatterwalk.h>
  1044. #include <linux/mm.h>
  1045. #include <linux/errno.h>
  1046. @@ -141,7 +142,7 @@ int crypto_init_digest_ops(struct crypto
  1047. struct hash_tfm *ops = &tfm->crt_hash;
  1048. struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
  1049. - if (dalg->dia_digestsize > crypto_tfm_alg_blocksize(tfm))
  1050. + if (dalg->dia_digestsize > PAGE_SIZE / 8)
  1051. return -EINVAL;
  1052. ops->init = init;
  1053. @@ -157,3 +158,83 @@ int crypto_init_digest_ops(struct crypto
  1054. void crypto_exit_digest_ops(struct crypto_tfm *tfm)
  1055. {
  1056. }
  1057. +
  1058. +static int digest_async_nosetkey(struct crypto_ahash *tfm_async, const u8 *key,
  1059. + unsigned int keylen)
  1060. +{
  1061. + crypto_ahash_clear_flags(tfm_async, CRYPTO_TFM_RES_MASK);
  1062. + return -ENOSYS;
  1063. +}
  1064. +
  1065. +static int digest_async_setkey(struct crypto_ahash *tfm_async, const u8 *key,
  1066. + unsigned int keylen)
  1067. +{
  1068. + struct crypto_tfm *tfm = crypto_ahash_tfm(tfm_async);
  1069. + struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
  1070. +
  1071. + crypto_ahash_clear_flags(tfm_async, CRYPTO_TFM_RES_MASK);
  1072. + return dalg->dia_setkey(tfm, key, keylen);
  1073. +}
  1074. +
  1075. +static int digest_async_init(struct ahash_request *req)
  1076. +{
  1077. + struct crypto_tfm *tfm = req->base.tfm;
  1078. + struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
  1079. +
  1080. + dalg->dia_init(tfm);
  1081. + return 0;
  1082. +}
  1083. +
  1084. +static int digest_async_update(struct ahash_request *req)
  1085. +{
  1086. + struct crypto_tfm *tfm = req->base.tfm;
  1087. + struct hash_desc desc = {
  1088. + .tfm = __crypto_hash_cast(tfm),
  1089. + .flags = req->base.flags,
  1090. + };
  1091. +
  1092. + update(&desc, req->src, req->nbytes);
  1093. + return 0;
  1094. +}
  1095. +
  1096. +static int digest_async_final(struct ahash_request *req)
  1097. +{
  1098. + struct crypto_tfm *tfm = req->base.tfm;
  1099. + struct hash_desc desc = {
  1100. + .tfm = __crypto_hash_cast(tfm),
  1101. + .flags = req->base.flags,
  1102. + };
  1103. +
  1104. + final(&desc, req->result);
  1105. + return 0;
  1106. +}
  1107. +
  1108. +static int digest_async_digest(struct ahash_request *req)
  1109. +{
  1110. + struct crypto_tfm *tfm = req->base.tfm;
  1111. + struct hash_desc desc = {
  1112. + .tfm = __crypto_hash_cast(tfm),
  1113. + .flags = req->base.flags,
  1114. + };
  1115. +
  1116. + return digest(&desc, req->src, req->nbytes, req->result);
  1117. +}
  1118. +
  1119. +int crypto_init_digest_ops_async(struct crypto_tfm *tfm)
  1120. +{
  1121. + struct ahash_tfm *crt = &tfm->crt_ahash;
  1122. + struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
  1123. +
  1124. + if (dalg->dia_digestsize > crypto_tfm_alg_blocksize(tfm))
  1125. + return -EINVAL;
  1126. +
  1127. + crt->init = digest_async_init;
  1128. + crt->update = digest_async_update;
  1129. + crt->final = digest_async_final;
  1130. + crt->digest = digest_async_digest;
  1131. + crt->setkey = dalg->dia_setkey ? digest_async_setkey :
  1132. + digest_async_nosetkey;
  1133. + crt->digestsize = dalg->dia_digestsize;
  1134. +
  1135. + return 0;
  1136. +}
  1137. --- a/crypto/hash.c
  1138. +++ b/crypto/hash.c
  1139. @@ -9,6 +9,7 @@
  1140. * any later version.
  1141. */
  1142. +#include <crypto/internal/hash.h>
  1143. #include <linux/errno.h>
  1144. #include <linux/kernel.h>
  1145. #include <linux/module.h>
  1146. @@ -59,24 +60,107 @@ static int hash_setkey(struct crypto_has
  1147. return alg->setkey(crt, key, keylen);
  1148. }
  1149. -static int crypto_init_hash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  1150. +static int hash_async_setkey(struct crypto_ahash *tfm_async, const u8 *key,
  1151. + unsigned int keylen)
  1152. +{
  1153. + struct crypto_tfm *tfm = crypto_ahash_tfm(tfm_async);
  1154. + struct crypto_hash *tfm_hash = __crypto_hash_cast(tfm);
  1155. + struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  1156. +
  1157. + return alg->setkey(tfm_hash, key, keylen);
  1158. +}
  1159. +
  1160. +static int hash_async_init(struct ahash_request *req)
  1161. +{
  1162. + struct crypto_tfm *tfm = req->base.tfm;
  1163. + struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  1164. + struct hash_desc desc = {
  1165. + .tfm = __crypto_hash_cast(tfm),
  1166. + .flags = req->base.flags,
  1167. + };
  1168. +
  1169. + return alg->init(&desc);
  1170. +}
  1171. +
  1172. +static int hash_async_update(struct ahash_request *req)
  1173. +{
  1174. + struct crypto_tfm *tfm = req->base.tfm;
  1175. + struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  1176. + struct hash_desc desc = {
  1177. + .tfm = __crypto_hash_cast(tfm),
  1178. + .flags = req->base.flags,
  1179. + };
  1180. +
  1181. + return alg->update(&desc, req->src, req->nbytes);
  1182. +}
  1183. +
  1184. +static int hash_async_final(struct ahash_request *req)
  1185. +{
  1186. + struct crypto_tfm *tfm = req->base.tfm;
  1187. + struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  1188. + struct hash_desc desc = {
  1189. + .tfm = __crypto_hash_cast(tfm),
  1190. + .flags = req->base.flags,
  1191. + };
  1192. +
  1193. + return alg->final(&desc, req->result);
  1194. +}
  1195. +
  1196. +static int hash_async_digest(struct ahash_request *req)
  1197. +{
  1198. + struct crypto_tfm *tfm = req->base.tfm;
  1199. + struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  1200. + struct hash_desc desc = {
  1201. + .tfm = __crypto_hash_cast(tfm),
  1202. + .flags = req->base.flags,
  1203. + };
  1204. +
  1205. + return alg->digest(&desc, req->src, req->nbytes, req->result);
  1206. +}
  1207. +
  1208. +static int crypto_init_hash_ops_async(struct crypto_tfm *tfm)
  1209. +{
  1210. + struct ahash_tfm *crt = &tfm->crt_ahash;
  1211. + struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  1212. +
  1213. + crt->init = hash_async_init;
  1214. + crt->update = hash_async_update;
  1215. + crt->final = hash_async_final;
  1216. + crt->digest = hash_async_digest;
  1217. + crt->setkey = hash_async_setkey;
  1218. + crt->digestsize = alg->digestsize;
  1219. +
  1220. + return 0;
  1221. +}
  1222. +
  1223. +static int crypto_init_hash_ops_sync(struct crypto_tfm *tfm)
  1224. {
  1225. struct hash_tfm *crt = &tfm->crt_hash;
  1226. struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  1227. - if (alg->digestsize > crypto_tfm_alg_blocksize(tfm))
  1228. - return -EINVAL;
  1229. -
  1230. - crt->init = alg->init;
  1231. - crt->update = alg->update;
  1232. - crt->final = alg->final;
  1233. - crt->digest = alg->digest;
  1234. - crt->setkey = hash_setkey;
  1235. + crt->init = alg->init;
  1236. + crt->update = alg->update;
  1237. + crt->final = alg->final;
  1238. + crt->digest = alg->digest;
  1239. + crt->setkey = hash_setkey;
  1240. crt->digestsize = alg->digestsize;
  1241. return 0;
  1242. }
  1243. +static int crypto_init_hash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  1244. +{
  1245. + struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  1246. +
  1247. + if (alg->digestsize > PAGE_SIZE / 8)
  1248. + return -EINVAL;
  1249. +
  1250. + if ((mask & CRYPTO_ALG_TYPE_HASH_MASK) != CRYPTO_ALG_TYPE_HASH_MASK)
  1251. + return crypto_init_hash_ops_async(tfm);
  1252. + else
  1253. + return crypto_init_hash_ops_sync(tfm);
  1254. +}
  1255. +
  1256. static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg)
  1257. __attribute__ ((unused));
  1258. static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg)
  1259. --- a/crypto/hmac.c
  1260. +++ b/crypto/hmac.c
  1261. @@ -226,6 +226,7 @@ static struct crypto_instance *hmac_allo
  1262. struct crypto_instance *inst;
  1263. struct crypto_alg *alg;
  1264. int err;
  1265. + int ds;
  1266. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_HASH);
  1267. if (err)
  1268. @@ -236,6 +237,13 @@ static struct crypto_instance *hmac_allo
  1269. if (IS_ERR(alg))
  1270. return ERR_CAST(alg);
  1271. + inst = ERR_PTR(-EINVAL);
  1272. + ds = (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  1273. + CRYPTO_ALG_TYPE_HASH ? alg->cra_hash.digestsize :
  1274. + alg->cra_digest.dia_digestsize;
  1275. + if (ds > alg->cra_blocksize)
  1276. + goto out_put_alg;
  1277. +
  1278. inst = crypto_alloc_instance("hmac", alg);
  1279. if (IS_ERR(inst))
  1280. goto out_put_alg;
  1281. @@ -246,14 +254,10 @@ static struct crypto_instance *hmac_allo
  1282. inst->alg.cra_alignmask = alg->cra_alignmask;
  1283. inst->alg.cra_type = &crypto_hash_type;
  1284. - inst->alg.cra_hash.digestsize =
  1285. - (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  1286. - CRYPTO_ALG_TYPE_HASH ? alg->cra_hash.digestsize :
  1287. - alg->cra_digest.dia_digestsize;
  1288. + inst->alg.cra_hash.digestsize = ds;
  1289. inst->alg.cra_ctxsize = sizeof(struct hmac_ctx) +
  1290. - ALIGN(inst->alg.cra_blocksize * 2 +
  1291. - inst->alg.cra_hash.digestsize,
  1292. + ALIGN(inst->alg.cra_blocksize * 2 + ds,
  1293. sizeof(void *));
  1294. inst->alg.cra_init = hmac_init_tfm;
  1295. --- a/crypto/internal.h
  1296. +++ b/crypto/internal.h
  1297. @@ -86,6 +86,7 @@ struct crypto_alg *__crypto_alg_lookup(c
  1298. struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask);
  1299. int crypto_init_digest_ops(struct crypto_tfm *tfm);
  1300. +int crypto_init_digest_ops_async(struct crypto_tfm *tfm);
  1301. int crypto_init_cipher_ops(struct crypto_tfm *tfm);
  1302. int crypto_init_compress_ops(struct crypto_tfm *tfm);
  1303. --- /dev/null
  1304. +++ b/crypto/prng.c
  1305. @@ -0,0 +1,410 @@
  1306. +/*
  1307. + * PRNG: Pseudo Random Number Generator
  1308. + * Based on NIST Recommended PRNG From ANSI X9.31 Appendix A.2.4 using
  1309. + * AES 128 cipher in RFC3686 ctr mode
  1310. + *
  1311. + * (C) Neil Horman <[email protected]>
  1312. + *
  1313. + * This program is free software; you can redistribute it and/or modify it
  1314. + * under the terms of the GNU General Public License as published by the
  1315. + * Free Software Foundation; either version 2 of the License, or (at your
  1316. + * any later version.
  1317. + *
  1318. + *
  1319. + */
  1320. +
  1321. +#include <linux/err.h>
  1322. +#include <linux/init.h>
  1323. +#include <linux/module.h>
  1324. +#include <linux/mm.h>
  1325. +#include <linux/slab.h>
  1326. +#include <linux/fs.h>
  1327. +#include <linux/scatterlist.h>
  1328. +#include <linux/string.h>
  1329. +#include <linux/crypto.h>
  1330. +#include <linux/highmem.h>
  1331. +#include <linux/moduleparam.h>
  1332. +#include <linux/jiffies.h>
  1333. +#include <linux/timex.h>
  1334. +#include <linux/interrupt.h>
  1335. +#include <linux/miscdevice.h>
  1336. +#include "prng.h"
  1337. +
  1338. +#define TEST_PRNG_ON_START 0
  1339. +
  1340. +#define DEFAULT_PRNG_KEY "0123456789abcdef1011"
  1341. +#define DEFAULT_PRNG_KSZ 20
  1342. +#define DEFAULT_PRNG_IV "defaultv"
  1343. +#define DEFAULT_PRNG_IVSZ 8
  1344. +#define DEFAULT_BLK_SZ 16
  1345. +#define DEFAULT_V_SEED "zaybxcwdveuftgsh"
  1346. +
  1347. +/*
  1348. + * Flags for the prng_context flags field
  1349. + */
  1350. +
  1351. +#define PRNG_FIXED_SIZE 0x1
  1352. +#define PRNG_NEED_RESET 0x2
  1353. +
  1354. +/*
  1355. + * Note: DT is our counter value
  1356. + * I is our intermediate value
  1357. + * V is our seed vector
  1358. + * See http://csrc.nist.gov/groups/STM/cavp/documents/rng/931rngext.pdf
  1359. + * for implementation details
  1360. + */
  1361. +
  1362. +
  1363. +struct prng_context {
  1364. + char *prng_key;
  1365. + char *prng_iv;
  1366. + spinlock_t prng_lock;
  1367. + unsigned char rand_data[DEFAULT_BLK_SZ];
  1368. + unsigned char last_rand_data[DEFAULT_BLK_SZ];
  1369. + unsigned char DT[DEFAULT_BLK_SZ];
  1370. + unsigned char I[DEFAULT_BLK_SZ];
  1371. + unsigned char V[DEFAULT_BLK_SZ];
  1372. + u32 rand_data_valid;
  1373. + struct crypto_blkcipher *tfm;
  1374. + u32 flags;
  1375. +};
  1376. +
  1377. +static int dbg;
  1378. +
  1379. +static void hexdump(char *note, unsigned char *buf, unsigned int len)
  1380. +{
  1381. + if (dbg) {
  1382. + printk(KERN_CRIT "%s", note);
  1383. + print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
  1384. + 16, 1,
  1385. + buf, len, false);
  1386. + }
  1387. +}
  1388. +
  1389. +#define dbgprint(format, args...) do {if(dbg) printk(format, ##args);} while(0)
  1390. +
  1391. +static void xor_vectors(unsigned char *in1, unsigned char *in2,
  1392. + unsigned char *out, unsigned int size)
  1393. +{
  1394. + int i;
  1395. +
  1396. + for (i=0;i<size;i++)
  1397. + out[i] = in1[i] ^ in2[i];
  1398. +
  1399. +}
  1400. +/*
  1401. + * Returns DEFAULT_BLK_SZ bytes of random data per call
  1402. + * returns 0 if generation succeded, <0 if something went wrong
  1403. + */
  1404. +static int _get_more_prng_bytes(struct prng_context *ctx)
  1405. +{
  1406. + int i;
  1407. + struct blkcipher_desc desc;
  1408. + struct scatterlist sg_in, sg_out;
  1409. + int ret;
  1410. + unsigned char tmp[DEFAULT_BLK_SZ];
  1411. +
  1412. + desc.tfm = ctx->tfm;
  1413. + desc.flags = 0;
  1414. +
  1415. +
  1416. + dbgprint(KERN_CRIT "Calling _get_more_prng_bytes for context %p\n",ctx);
  1417. +
  1418. + hexdump("Input DT: ", ctx->DT, DEFAULT_BLK_SZ);
  1419. + hexdump("Input I: ", ctx->I, DEFAULT_BLK_SZ);
  1420. + hexdump("Input V: ", ctx->V, DEFAULT_BLK_SZ);
  1421. +
  1422. + /*
  1423. + * This algorithm is a 3 stage state machine
  1424. + */
  1425. + for (i=0;i<3;i++) {
  1426. +
  1427. + desc.tfm = ctx->tfm;
  1428. + desc.flags = 0;
  1429. + switch (i) {
  1430. + case 0:
  1431. + /*
  1432. + * Start by encrypting the counter value
  1433. + * This gives us an intermediate value I
  1434. + */
  1435. + memcpy(tmp, ctx->DT, DEFAULT_BLK_SZ);
  1436. + sg_init_one(&sg_out, &ctx->I[0], DEFAULT_BLK_SZ);
  1437. + hexdump("tmp stage 0: ", tmp, DEFAULT_BLK_SZ);
  1438. + break;
  1439. + case 1:
  1440. +
  1441. + /*
  1442. + * Next xor I with our secret vector V
  1443. + * encrypt that result to obtain our
  1444. + * pseudo random data which we output
  1445. + */
  1446. + xor_vectors(ctx->I, ctx->V, tmp, DEFAULT_BLK_SZ);
  1447. + sg_init_one(&sg_out, &ctx->rand_data[0], DEFAULT_BLK_SZ);
  1448. + hexdump("tmp stage 1: ", tmp, DEFAULT_BLK_SZ);
  1449. + break;
  1450. + case 2:
  1451. + /*
  1452. + * First check that we didn't produce the same random data
  1453. + * that we did last time around through this
  1454. + */
  1455. + if (!memcmp(ctx->rand_data, ctx->last_rand_data, DEFAULT_BLK_SZ)) {
  1456. + printk(KERN_ERR "ctx %p Failed repetition check!\n",
  1457. + ctx);
  1458. + ctx->flags |= PRNG_NEED_RESET;
  1459. + return -1;
  1460. + }
  1461. + memcpy(ctx->last_rand_data, ctx->rand_data, DEFAULT_BLK_SZ);
  1462. +
  1463. + /*
  1464. + * Lastly xor the random data with I
  1465. + * and encrypt that to obtain a new secret vector V
  1466. + */
  1467. + xor_vectors(ctx->rand_data, ctx->I, tmp, DEFAULT_BLK_SZ);
  1468. + sg_init_one(&sg_out, &ctx->V[0], DEFAULT_BLK_SZ);
  1469. + hexdump("tmp stage 2: ", tmp, DEFAULT_BLK_SZ);
  1470. + break;
  1471. + }
  1472. +
  1473. + /* Initialize our input buffer */
  1474. + sg_init_one(&sg_in, &tmp[0], DEFAULT_BLK_SZ);
  1475. +
  1476. + /* do the encryption */
  1477. + ret = crypto_blkcipher_encrypt(&desc, &sg_out, &sg_in, DEFAULT_BLK_SZ);
  1478. +
  1479. + /* And check the result */
  1480. + if (ret) {
  1481. + dbgprint(KERN_CRIT "Encryption of new block failed for context %p\n",ctx);
  1482. + ctx->rand_data_valid = DEFAULT_BLK_SZ;
  1483. + return -1;
  1484. + }
  1485. +
  1486. + }
  1487. +
  1488. + /*
  1489. + * Now update our DT value
  1490. + */
  1491. + for (i=DEFAULT_BLK_SZ-1;i>0;i--) {
  1492. + ctx->DT[i] = ctx->DT[i-1];
  1493. + }
  1494. + ctx->DT[0] += 1;
  1495. +
  1496. + dbgprint("Returning new block for context %p\n",ctx);
  1497. + ctx->rand_data_valid = 0;
  1498. +
  1499. + hexdump("Output DT: ", ctx->DT, DEFAULT_BLK_SZ);
  1500. + hexdump("Output I: ", ctx->I, DEFAULT_BLK_SZ);
  1501. + hexdump("Output V: ", ctx->V, DEFAULT_BLK_SZ);
  1502. + hexdump("New Random Data: ", ctx->rand_data, DEFAULT_BLK_SZ);
  1503. +
  1504. + return 0;
  1505. +}
  1506. +
  1507. +/* Our exported functions */
  1508. +int get_prng_bytes(char *buf, int nbytes, struct prng_context *ctx)
  1509. +{
  1510. + unsigned long flags;
  1511. + unsigned char *ptr = buf;
  1512. + unsigned int byte_count = (unsigned int)nbytes;
  1513. + int err;
  1514. +
  1515. +
  1516. + if (nbytes < 0)
  1517. + return -EINVAL;
  1518. +
  1519. + spin_lock_irqsave(&ctx->prng_lock, flags);
  1520. +
  1521. + err = -EFAULT;
  1522. + if (ctx->flags & PRNG_NEED_RESET)
  1523. + goto done;
  1524. +
  1525. + /*
  1526. + * If the FIXED_SIZE flag is on, only return whole blocks of
  1527. + * pseudo random data
  1528. + */
  1529. + err = -EINVAL;
  1530. + if (ctx->flags & PRNG_FIXED_SIZE) {
  1531. + if (nbytes < DEFAULT_BLK_SZ)
  1532. + goto done;
  1533. + byte_count = DEFAULT_BLK_SZ;
  1534. + }
  1535. +
  1536. + err = byte_count;
  1537. +
  1538. + dbgprint(KERN_CRIT "getting %d random bytes for context %p\n",byte_count, ctx);
  1539. +
  1540. +
  1541. +remainder:
  1542. + if (ctx->rand_data_valid == DEFAULT_BLK_SZ) {
  1543. + if (_get_more_prng_bytes(ctx) < 0) {
  1544. + memset(buf, 0, nbytes);
  1545. + err = -EFAULT;
  1546. + goto done;
  1547. + }
  1548. + }
  1549. +
  1550. + /*
  1551. + * Copy up to the next whole block size
  1552. + */
  1553. + if (byte_count < DEFAULT_BLK_SZ) {
  1554. + for (;ctx->rand_data_valid < DEFAULT_BLK_SZ; ctx->rand_data_valid++) {
  1555. + *ptr = ctx->rand_data[ctx->rand_data_valid];
  1556. + ptr++;
  1557. + byte_count--;
  1558. + if (byte_count == 0)
  1559. + goto done;
  1560. + }
  1561. + }
  1562. +
  1563. + /*
  1564. + * Now copy whole blocks
  1565. + */
  1566. + for(;byte_count >= DEFAULT_BLK_SZ; byte_count -= DEFAULT_BLK_SZ) {
  1567. + if (_get_more_prng_bytes(ctx) < 0) {
  1568. + memset(buf, 0, nbytes);
  1569. + err = -1;
  1570. + goto done;
  1571. + }
  1572. + memcpy(ptr, ctx->rand_data, DEFAULT_BLK_SZ);
  1573. + ctx->rand_data_valid += DEFAULT_BLK_SZ;
  1574. + ptr += DEFAULT_BLK_SZ;
  1575. + }
  1576. +
  1577. + /*
  1578. + * Now copy any extra partial data
  1579. + */
  1580. + if (byte_count)
  1581. + goto remainder;
  1582. +
  1583. +done:
  1584. + spin_unlock_irqrestore(&ctx->prng_lock, flags);
  1585. + dbgprint(KERN_CRIT "returning %d from get_prng_bytes in context %p\n",err, ctx);
  1586. + return err;
  1587. +}
  1588. +EXPORT_SYMBOL_GPL(get_prng_bytes);
  1589. +
  1590. +struct prng_context *alloc_prng_context(void)
  1591. +{
  1592. + struct prng_context *ctx=kzalloc(sizeof(struct prng_context), GFP_KERNEL);
  1593. +
  1594. + spin_lock_init(&ctx->prng_lock);
  1595. +
  1596. + if (reset_prng_context(ctx, NULL, NULL, NULL, NULL)) {
  1597. + kfree(ctx);
  1598. + ctx = NULL;
  1599. + }
  1600. +
  1601. + dbgprint(KERN_CRIT "returning context %p\n",ctx);
  1602. + return ctx;
  1603. +}
  1604. +
  1605. +EXPORT_SYMBOL_GPL(alloc_prng_context);
  1606. +
  1607. +void free_prng_context(struct prng_context *ctx)
  1608. +{
  1609. + crypto_free_blkcipher(ctx->tfm);
  1610. + kfree(ctx);
  1611. +}
  1612. +EXPORT_SYMBOL_GPL(free_prng_context);
  1613. +
  1614. +int reset_prng_context(struct prng_context *ctx,
  1615. + unsigned char *key, unsigned char *iv,
  1616. + unsigned char *V, unsigned char *DT)
  1617. +{
  1618. + int ret;
  1619. + int iv_len;
  1620. + int rc = -EFAULT;
  1621. +
  1622. + spin_lock(&ctx->prng_lock);
  1623. + ctx->flags |= PRNG_NEED_RESET;
  1624. +
  1625. + if (key)
  1626. + memcpy(ctx->prng_key,key,strlen(ctx->prng_key));
  1627. + else
  1628. + ctx->prng_key = DEFAULT_PRNG_KEY;
  1629. +
  1630. + if (iv)
  1631. + memcpy(ctx->prng_iv,iv, strlen(ctx->prng_iv));
  1632. + else
  1633. + ctx->prng_iv = DEFAULT_PRNG_IV;
  1634. +
  1635. + if (V)
  1636. + memcpy(ctx->V,V,DEFAULT_BLK_SZ);
  1637. + else
  1638. + memcpy(ctx->V,DEFAULT_V_SEED,DEFAULT_BLK_SZ);
  1639. +
  1640. + if (DT)
  1641. + memcpy(ctx->DT, DT, DEFAULT_BLK_SZ);
  1642. + else
  1643. + memset(ctx->DT, 0, DEFAULT_BLK_SZ);
  1644. +
  1645. + memset(ctx->rand_data,0,DEFAULT_BLK_SZ);
  1646. + memset(ctx->last_rand_data,0,DEFAULT_BLK_SZ);
  1647. +
  1648. + if (ctx->tfm)
  1649. + crypto_free_blkcipher(ctx->tfm);
  1650. +
  1651. + ctx->tfm = crypto_alloc_blkcipher("rfc3686(ctr(aes))",0,0);
  1652. + if (!ctx->tfm) {
  1653. + dbgprint(KERN_CRIT "Failed to alloc crypto tfm for context %p\n",ctx->tfm);
  1654. + goto out;
  1655. + }
  1656. +
  1657. + ctx->rand_data_valid = DEFAULT_BLK_SZ;
  1658. +
  1659. + ret = crypto_blkcipher_setkey(ctx->tfm, ctx->prng_key, strlen(ctx->prng_key));
  1660. + if (ret) {
  1661. + dbgprint(KERN_CRIT "PRNG: setkey() failed flags=%x\n",
  1662. + crypto_blkcipher_get_flags(ctx->tfm));
  1663. + crypto_free_blkcipher(ctx->tfm);
  1664. + goto out;
  1665. + }
  1666. +
  1667. + iv_len = crypto_blkcipher_ivsize(ctx->tfm);
  1668. + if (iv_len) {
  1669. + crypto_blkcipher_set_iv(ctx->tfm, ctx->prng_iv, iv_len);
  1670. + }
  1671. + rc = 0;
  1672. + ctx->flags &= ~PRNG_NEED_RESET;
  1673. +out:
  1674. + spin_unlock(&ctx->prng_lock);
  1675. +
  1676. + return rc;
  1677. +
  1678. +}
  1679. +EXPORT_SYMBOL_GPL(reset_prng_context);
  1680. +
  1681. +/* Module initalization */
  1682. +static int __init prng_mod_init(void)
  1683. +{
  1684. +
  1685. +#ifdef TEST_PRNG_ON_START
  1686. + int i;
  1687. + unsigned char tmpbuf[DEFAULT_BLK_SZ];
  1688. +
  1689. + struct prng_context *ctx = alloc_prng_context();
  1690. + if (ctx == NULL)
  1691. + return -EFAULT;
  1692. + for (i=0;i<16;i++) {
  1693. + if (get_prng_bytes(tmpbuf, DEFAULT_BLK_SZ, ctx) < 0) {
  1694. + free_prng_context(ctx);
  1695. + return -EFAULT;
  1696. + }
  1697. + }
  1698. + free_prng_context(ctx);
  1699. +#endif
  1700. +
  1701. + return 0;
  1702. +}
  1703. +
  1704. +static void __exit prng_mod_fini(void)
  1705. +{
  1706. + return;
  1707. +}
  1708. +
  1709. +MODULE_LICENSE("GPL");
  1710. +MODULE_DESCRIPTION("Software Pseudo Random Number Generator");
  1711. +MODULE_AUTHOR("Neil Horman <[email protected]>");
  1712. +module_param(dbg, int, 0);
  1713. +MODULE_PARM_DESC(dbg, "Boolean to enable debugging (0/1 == off/on)");
  1714. +module_init(prng_mod_init);
  1715. +module_exit(prng_mod_fini);
  1716. --- /dev/null
  1717. +++ b/crypto/prng.h
  1718. @@ -0,0 +1,27 @@
  1719. +/*
  1720. + * PRNG: Pseudo Random Number Generator
  1721. + *
  1722. + * (C) Neil Horman <[email protected]>
  1723. + *
  1724. + * This program is free software; you can redistribute it and/or modify it
  1725. + * under the terms of the GNU General Public License as published by the
  1726. + * Free Software Foundation; either version 2 of the License, or (at your
  1727. + * any later version.
  1728. + *
  1729. + *
  1730. + */
  1731. +
  1732. +#ifndef _PRNG_H_
  1733. +#define _PRNG_H_
  1734. +struct prng_context;
  1735. +
  1736. +int get_prng_bytes(char *buf, int nbytes, struct prng_context *ctx);
  1737. +struct prng_context *alloc_prng_context(void);
  1738. +int reset_prng_context(struct prng_context *ctx,
  1739. + unsigned char *key, unsigned char *iv,
  1740. + unsigned char *V,
  1741. + unsigned char *DT);
  1742. +void free_prng_context(struct prng_context *ctx);
  1743. +
  1744. +#endif
  1745. +
  1746. --- /dev/null
  1747. +++ b/crypto/ripemd.h
  1748. @@ -0,0 +1,43 @@
  1749. +/*
  1750. + * Common values for RIPEMD algorithms
  1751. + */
  1752. +
  1753. +#ifndef _CRYPTO_RMD_H
  1754. +#define _CRYPTO_RMD_H
  1755. +
  1756. +#define RMD128_DIGEST_SIZE 16
  1757. +#define RMD128_BLOCK_SIZE 64
  1758. +
  1759. +#define RMD160_DIGEST_SIZE 20
  1760. +#define RMD160_BLOCK_SIZE 64
  1761. +
  1762. +#define RMD256_DIGEST_SIZE 32
  1763. +#define RMD256_BLOCK_SIZE 64
  1764. +
  1765. +#define RMD320_DIGEST_SIZE 40
  1766. +#define RMD320_BLOCK_SIZE 64
  1767. +
  1768. +/* initial values */
  1769. +#define RMD_H0 0x67452301UL
  1770. +#define RMD_H1 0xefcdab89UL
  1771. +#define RMD_H2 0x98badcfeUL
  1772. +#define RMD_H3 0x10325476UL
  1773. +#define RMD_H4 0xc3d2e1f0UL
  1774. +#define RMD_H5 0x76543210UL
  1775. +#define RMD_H6 0xfedcba98UL
  1776. +#define RMD_H7 0x89abcdefUL
  1777. +#define RMD_H8 0x01234567UL
  1778. +#define RMD_H9 0x3c2d1e0fUL
  1779. +
  1780. +/* constants */
  1781. +#define RMD_K1 0x00000000UL
  1782. +#define RMD_K2 0x5a827999UL
  1783. +#define RMD_K3 0x6ed9eba1UL
  1784. +#define RMD_K4 0x8f1bbcdcUL
  1785. +#define RMD_K5 0xa953fd4eUL
  1786. +#define RMD_K6 0x50a28be6UL
  1787. +#define RMD_K7 0x5c4dd124UL
  1788. +#define RMD_K8 0x6d703ef3UL
  1789. +#define RMD_K9 0x7a6d76e9UL
  1790. +
  1791. +#endif
  1792. --- /dev/null
  1793. +++ b/crypto/rmd128.c
  1794. @@ -0,0 +1,325 @@
  1795. +/*
  1796. + * Cryptographic API.
  1797. + *
  1798. + * RIPEMD-128 - RACE Integrity Primitives Evaluation Message Digest.
  1799. + *
  1800. + * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
  1801. + *
  1802. + * Copyright (c) 2008 Adrian-Ken Rueegsegger <rueegsegger (at) swiss-it.ch>
  1803. + *
  1804. + * This program is free software; you can redistribute it and/or modify it
  1805. + * under the terms of the GNU General Public License as published by the Free
  1806. + * Software Foundation; either version 2 of the License, or (at your option)
  1807. + * any later version.
  1808. + *
  1809. + */
  1810. +#include <linux/init.h>
  1811. +#include <linux/module.h>
  1812. +#include <linux/mm.h>
  1813. +#include <linux/crypto.h>
  1814. +#include <linux/cryptohash.h>
  1815. +#include <linux/types.h>
  1816. +#include <asm/byteorder.h>
  1817. +
  1818. +#include "ripemd.h"
  1819. +
  1820. +struct rmd128_ctx {
  1821. + u64 byte_count;
  1822. + u32 state[4];
  1823. + __le32 buffer[16];
  1824. +};
  1825. +
  1826. +#define K1 RMD_K1
  1827. +#define K2 RMD_K2
  1828. +#define K3 RMD_K3
  1829. +#define K4 RMD_K4
  1830. +#define KK1 RMD_K6
  1831. +#define KK2 RMD_K7
  1832. +#define KK3 RMD_K8
  1833. +#define KK4 RMD_K1
  1834. +
  1835. +#define F1(x, y, z) (x ^ y ^ z) /* XOR */
  1836. +#define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
  1837. +#define F3(x, y, z) ((x | ~y) ^ z)
  1838. +#define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
  1839. +
  1840. +#define ROUND(a, b, c, d, f, k, x, s) { \
  1841. + (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
  1842. + (a) = rol32((a), (s)); \
  1843. +}
  1844. +
  1845. +static void rmd128_transform(u32 *state, const __le32 *in)
  1846. +{
  1847. + u32 aa, bb, cc, dd, aaa, bbb, ccc, ddd;
  1848. +
  1849. + /* Initialize left lane */
  1850. + aa = state[0];
  1851. + bb = state[1];
  1852. + cc = state[2];
  1853. + dd = state[3];
  1854. +
  1855. + /* Initialize right lane */
  1856. + aaa = state[0];
  1857. + bbb = state[1];
  1858. + ccc = state[2];
  1859. + ddd = state[3];
  1860. +
  1861. + /* round 1: left lane */
  1862. + ROUND(aa, bb, cc, dd, F1, K1, in[0], 11);
  1863. + ROUND(dd, aa, bb, cc, F1, K1, in[1], 14);
  1864. + ROUND(cc, dd, aa, bb, F1, K1, in[2], 15);
  1865. + ROUND(bb, cc, dd, aa, F1, K1, in[3], 12);
  1866. + ROUND(aa, bb, cc, dd, F1, K1, in[4], 5);
  1867. + ROUND(dd, aa, bb, cc, F1, K1, in[5], 8);
  1868. + ROUND(cc, dd, aa, bb, F1, K1, in[6], 7);
  1869. + ROUND(bb, cc, dd, aa, F1, K1, in[7], 9);
  1870. + ROUND(aa, bb, cc, dd, F1, K1, in[8], 11);
  1871. + ROUND(dd, aa, bb, cc, F1, K1, in[9], 13);
  1872. + ROUND(cc, dd, aa, bb, F1, K1, in[10], 14);
  1873. + ROUND(bb, cc, dd, aa, F1, K1, in[11], 15);
  1874. + ROUND(aa, bb, cc, dd, F1, K1, in[12], 6);
  1875. + ROUND(dd, aa, bb, cc, F1, K1, in[13], 7);
  1876. + ROUND(cc, dd, aa, bb, F1, K1, in[14], 9);
  1877. + ROUND(bb, cc, dd, aa, F1, K1, in[15], 8);
  1878. +
  1879. + /* round 2: left lane */
  1880. + ROUND(aa, bb, cc, dd, F2, K2, in[7], 7);
  1881. + ROUND(dd, aa, bb, cc, F2, K2, in[4], 6);
  1882. + ROUND(cc, dd, aa, bb, F2, K2, in[13], 8);
  1883. + ROUND(bb, cc, dd, aa, F2, K2, in[1], 13);
  1884. + ROUND(aa, bb, cc, dd, F2, K2, in[10], 11);
  1885. + ROUND(dd, aa, bb, cc, F2, K2, in[6], 9);
  1886. + ROUND(cc, dd, aa, bb, F2, K2, in[15], 7);
  1887. + ROUND(bb, cc, dd, aa, F2, K2, in[3], 15);
  1888. + ROUND(aa, bb, cc, dd, F2, K2, in[12], 7);
  1889. + ROUND(dd, aa, bb, cc, F2, K2, in[0], 12);
  1890. + ROUND(cc, dd, aa, bb, F2, K2, in[9], 15);
  1891. + ROUND(bb, cc, dd, aa, F2, K2, in[5], 9);
  1892. + ROUND(aa, bb, cc, dd, F2, K2, in[2], 11);
  1893. + ROUND(dd, aa, bb, cc, F2, K2, in[14], 7);
  1894. + ROUND(cc, dd, aa, bb, F2, K2, in[11], 13);
  1895. + ROUND(bb, cc, dd, aa, F2, K2, in[8], 12);
  1896. +
  1897. + /* round 3: left lane */
  1898. + ROUND(aa, bb, cc, dd, F3, K3, in[3], 11);
  1899. + ROUND(dd, aa, bb, cc, F3, K3, in[10], 13);
  1900. + ROUND(cc, dd, aa, bb, F3, K3, in[14], 6);
  1901. + ROUND(bb, cc, dd, aa, F3, K3, in[4], 7);
  1902. + ROUND(aa, bb, cc, dd, F3, K3, in[9], 14);
  1903. + ROUND(dd, aa, bb, cc, F3, K3, in[15], 9);
  1904. + ROUND(cc, dd, aa, bb, F3, K3, in[8], 13);
  1905. + ROUND(bb, cc, dd, aa, F3, K3, in[1], 15);
  1906. + ROUND(aa, bb, cc, dd, F3, K3, in[2], 14);
  1907. + ROUND(dd, aa, bb, cc, F3, K3, in[7], 8);
  1908. + ROUND(cc, dd, aa, bb, F3, K3, in[0], 13);
  1909. + ROUND(bb, cc, dd, aa, F3, K3, in[6], 6);
  1910. + ROUND(aa, bb, cc, dd, F3, K3, in[13], 5);
  1911. + ROUND(dd, aa, bb, cc, F3, K3, in[11], 12);
  1912. + ROUND(cc, dd, aa, bb, F3, K3, in[5], 7);
  1913. + ROUND(bb, cc, dd, aa, F3, K3, in[12], 5);
  1914. +
  1915. + /* round 4: left lane */
  1916. + ROUND(aa, bb, cc, dd, F4, K4, in[1], 11);
  1917. + ROUND(dd, aa, bb, cc, F4, K4, in[9], 12);
  1918. + ROUND(cc, dd, aa, bb, F4, K4, in[11], 14);
  1919. + ROUND(bb, cc, dd, aa, F4, K4, in[10], 15);
  1920. + ROUND(aa, bb, cc, dd, F4, K4, in[0], 14);
  1921. + ROUND(dd, aa, bb, cc, F4, K4, in[8], 15);
  1922. + ROUND(cc, dd, aa, bb, F4, K4, in[12], 9);
  1923. + ROUND(bb, cc, dd, aa, F4, K4, in[4], 8);
  1924. + ROUND(aa, bb, cc, dd, F4, K4, in[13], 9);
  1925. + ROUND(dd, aa, bb, cc, F4, K4, in[3], 14);
  1926. + ROUND(cc, dd, aa, bb, F4, K4, in[7], 5);
  1927. + ROUND(bb, cc, dd, aa, F4, K4, in[15], 6);
  1928. + ROUND(aa, bb, cc, dd, F4, K4, in[14], 8);
  1929. + ROUND(dd, aa, bb, cc, F4, K4, in[5], 6);
  1930. + ROUND(cc, dd, aa, bb, F4, K4, in[6], 5);
  1931. + ROUND(bb, cc, dd, aa, F4, K4, in[2], 12);
  1932. +
  1933. + /* round 1: right lane */
  1934. + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[5], 8);
  1935. + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[14], 9);
  1936. + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[7], 9);
  1937. + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[0], 11);
  1938. + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[9], 13);
  1939. + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[2], 15);
  1940. + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[11], 15);
  1941. + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[4], 5);
  1942. + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[13], 7);
  1943. + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[6], 7);
  1944. + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[15], 8);
  1945. + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[8], 11);
  1946. + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[1], 14);
  1947. + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[10], 14);
  1948. + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[3], 12);
  1949. + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[12], 6);
  1950. +
  1951. + /* round 2: right lane */
  1952. + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[6], 9);
  1953. + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[11], 13);
  1954. + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[3], 15);
  1955. + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[7], 7);
  1956. + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[0], 12);
  1957. + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[13], 8);
  1958. + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[5], 9);
  1959. + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[10], 11);
  1960. + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[14], 7);
  1961. + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[15], 7);
  1962. + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[8], 12);
  1963. + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[12], 7);
  1964. + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[4], 6);
  1965. + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[9], 15);
  1966. + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[1], 13);
  1967. + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[2], 11);
  1968. +
  1969. + /* round 3: right lane */
  1970. + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[15], 9);
  1971. + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[5], 7);
  1972. + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[1], 15);
  1973. + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[3], 11);
  1974. + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[7], 8);
  1975. + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[14], 6);
  1976. + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[6], 6);
  1977. + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[9], 14);
  1978. + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[11], 12);
  1979. + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[8], 13);
  1980. + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[12], 5);
  1981. + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[2], 14);
  1982. + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[10], 13);
  1983. + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[0], 13);
  1984. + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[4], 7);
  1985. + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[13], 5);
  1986. +
  1987. + /* round 4: right lane */
  1988. + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[8], 15);
  1989. + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[6], 5);
  1990. + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[4], 8);
  1991. + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[1], 11);
  1992. + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[3], 14);
  1993. + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[11], 14);
  1994. + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[15], 6);
  1995. + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[0], 14);
  1996. + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[5], 6);
  1997. + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[12], 9);
  1998. + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[2], 12);
  1999. + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[13], 9);
  2000. + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[9], 12);
  2001. + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[7], 5);
  2002. + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[10], 15);
  2003. + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[14], 8);
  2004. +
  2005. + /* combine results */
  2006. + ddd += cc + state[1]; /* final result for state[0] */
  2007. + state[1] = state[2] + dd + aaa;
  2008. + state[2] = state[3] + aa + bbb;
  2009. + state[3] = state[0] + bb + ccc;
  2010. + state[0] = ddd;
  2011. +
  2012. + return;
  2013. +}
  2014. +
  2015. +static void rmd128_init(struct crypto_tfm *tfm)
  2016. +{
  2017. + struct rmd128_ctx *rctx = crypto_tfm_ctx(tfm);
  2018. +
  2019. + rctx->byte_count = 0;
  2020. +
  2021. + rctx->state[0] = RMD_H0;
  2022. + rctx->state[1] = RMD_H1;
  2023. + rctx->state[2] = RMD_H2;
  2024. + rctx->state[3] = RMD_H3;
  2025. +
  2026. + memset(rctx->buffer, 0, sizeof(rctx->buffer));
  2027. +}
  2028. +
  2029. +static void rmd128_update(struct crypto_tfm *tfm, const u8 *data,
  2030. + unsigned int len)
  2031. +{
  2032. + struct rmd128_ctx *rctx = crypto_tfm_ctx(tfm);
  2033. + const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
  2034. +
  2035. + rctx->byte_count += len;
  2036. +
  2037. + /* Enough space in buffer? If so copy and we're done */
  2038. + if (avail > len) {
  2039. + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
  2040. + data, len);
  2041. + return;
  2042. + }
  2043. +
  2044. + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
  2045. + data, avail);
  2046. +
  2047. + rmd128_transform(rctx->state, rctx->buffer);
  2048. + data += avail;
  2049. + len -= avail;
  2050. +
  2051. + while (len >= sizeof(rctx->buffer)) {
  2052. + memcpy(rctx->buffer, data, sizeof(rctx->buffer));
  2053. + rmd128_transform(rctx->state, rctx->buffer);
  2054. + data += sizeof(rctx->buffer);
  2055. + len -= sizeof(rctx->buffer);
  2056. + }
  2057. +
  2058. + memcpy(rctx->buffer, data, len);
  2059. +}
  2060. +
  2061. +/* Add padding and return the message digest. */
  2062. +static void rmd128_final(struct crypto_tfm *tfm, u8 *out)
  2063. +{
  2064. + struct rmd128_ctx *rctx = crypto_tfm_ctx(tfm);
  2065. + u32 i, index, padlen;
  2066. + __le64 bits;
  2067. + __le32 *dst = (__le32 *)out;
  2068. + static const u8 padding[64] = { 0x80, };
  2069. +
  2070. + bits = cpu_to_le64(rctx->byte_count << 3);
  2071. +
  2072. + /* Pad out to 56 mod 64 */
  2073. + index = rctx->byte_count & 0x3f;
  2074. + padlen = (index < 56) ? (56 - index) : ((64+56) - index);
  2075. + rmd128_update(tfm, padding, padlen);
  2076. +
  2077. + /* Append length */
  2078. + rmd128_update(tfm, (const u8 *)&bits, sizeof(bits));
  2079. +
  2080. + /* Store state in digest */
  2081. + for (i = 0; i < 4; i++)
  2082. + dst[i] = cpu_to_le32p(&rctx->state[i]);
  2083. +
  2084. + /* Wipe context */
  2085. + memset(rctx, 0, sizeof(*rctx));
  2086. +}
  2087. +
  2088. +static struct crypto_alg alg = {
  2089. + .cra_name = "rmd128",
  2090. + .cra_driver_name = "rmd128",
  2091. + .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
  2092. + .cra_blocksize = RMD128_BLOCK_SIZE,
  2093. + .cra_ctxsize = sizeof(struct rmd128_ctx),
  2094. + .cra_module = THIS_MODULE,
  2095. + .cra_list = LIST_HEAD_INIT(alg.cra_list),
  2096. + .cra_u = { .digest = {
  2097. + .dia_digestsize = RMD128_DIGEST_SIZE,
  2098. + .dia_init = rmd128_init,
  2099. + .dia_update = rmd128_update,
  2100. + .dia_final = rmd128_final } }
  2101. +};
  2102. +
  2103. +static int __init rmd128_mod_init(void)
  2104. +{
  2105. + return crypto_register_alg(&alg);
  2106. +}
  2107. +
  2108. +static void __exit rmd128_mod_fini(void)
  2109. +{
  2110. + crypto_unregister_alg(&alg);
  2111. +}
  2112. +
  2113. +module_init(rmd128_mod_init);
  2114. +module_exit(rmd128_mod_fini);
  2115. +
  2116. +MODULE_LICENSE("GPL");
  2117. +MODULE_DESCRIPTION("RIPEMD-128 Message Digest");
  2118. +
  2119. +MODULE_ALIAS("rmd128");
  2120. --- /dev/null
  2121. +++ b/crypto/rmd160.c
  2122. @@ -0,0 +1,369 @@
  2123. +/*
  2124. + * Cryptographic API.
  2125. + *
  2126. + * RIPEMD-160 - RACE Integrity Primitives Evaluation Message Digest.
  2127. + *
  2128. + * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
  2129. + *
  2130. + * Copyright (c) 2008 Adrian-Ken Rueegsegger <rueegsegger (at) swiss-it.ch>
  2131. + *
  2132. + * This program is free software; you can redistribute it and/or modify it
  2133. + * under the terms of the GNU General Public License as published by the Free
  2134. + * Software Foundation; either version 2 of the License, or (at your option)
  2135. + * any later version.
  2136. + *
  2137. + */
  2138. +#include <linux/init.h>
  2139. +#include <linux/module.h>
  2140. +#include <linux/mm.h>
  2141. +#include <linux/crypto.h>
  2142. +#include <linux/cryptohash.h>
  2143. +#include <linux/types.h>
  2144. +#include <asm/byteorder.h>
  2145. +
  2146. +#include "ripemd.h"
  2147. +
  2148. +struct rmd160_ctx {
  2149. + u64 byte_count;
  2150. + u32 state[5];
  2151. + __le32 buffer[16];
  2152. +};
  2153. +
  2154. +#define K1 RMD_K1
  2155. +#define K2 RMD_K2
  2156. +#define K3 RMD_K3
  2157. +#define K4 RMD_K4
  2158. +#define K5 RMD_K5
  2159. +#define KK1 RMD_K6
  2160. +#define KK2 RMD_K7
  2161. +#define KK3 RMD_K8
  2162. +#define KK4 RMD_K9
  2163. +#define KK5 RMD_K1
  2164. +
  2165. +#define F1(x, y, z) (x ^ y ^ z) /* XOR */
  2166. +#define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
  2167. +#define F3(x, y, z) ((x | ~y) ^ z)
  2168. +#define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
  2169. +#define F5(x, y, z) (x ^ (y | ~z))
  2170. +
  2171. +#define ROUND(a, b, c, d, e, f, k, x, s) { \
  2172. + (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
  2173. + (a) = rol32((a), (s)) + (e); \
  2174. + (c) = rol32((c), 10); \
  2175. +}
  2176. +
  2177. +static void rmd160_transform(u32 *state, const __le32 *in)
  2178. +{
  2179. + u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee;
  2180. +
  2181. + /* Initialize left lane */
  2182. + aa = state[0];
  2183. + bb = state[1];
  2184. + cc = state[2];
  2185. + dd = state[3];
  2186. + ee = state[4];
  2187. +
  2188. + /* Initialize right lane */
  2189. + aaa = state[0];
  2190. + bbb = state[1];
  2191. + ccc = state[2];
  2192. + ddd = state[3];
  2193. + eee = state[4];
  2194. +
  2195. + /* round 1: left lane */
  2196. + ROUND(aa, bb, cc, dd, ee, F1, K1, in[0], 11);
  2197. + ROUND(ee, aa, bb, cc, dd, F1, K1, in[1], 14);
  2198. + ROUND(dd, ee, aa, bb, cc, F1, K1, in[2], 15);
  2199. + ROUND(cc, dd, ee, aa, bb, F1, K1, in[3], 12);
  2200. + ROUND(bb, cc, dd, ee, aa, F1, K1, in[4], 5);
  2201. + ROUND(aa, bb, cc, dd, ee, F1, K1, in[5], 8);
  2202. + ROUND(ee, aa, bb, cc, dd, F1, K1, in[6], 7);
  2203. + ROUND(dd, ee, aa, bb, cc, F1, K1, in[7], 9);
  2204. + ROUND(cc, dd, ee, aa, bb, F1, K1, in[8], 11);
  2205. + ROUND(bb, cc, dd, ee, aa, F1, K1, in[9], 13);
  2206. + ROUND(aa, bb, cc, dd, ee, F1, K1, in[10], 14);
  2207. + ROUND(ee, aa, bb, cc, dd, F1, K1, in[11], 15);
  2208. + ROUND(dd, ee, aa, bb, cc, F1, K1, in[12], 6);
  2209. + ROUND(cc, dd, ee, aa, bb, F1, K1, in[13], 7);
  2210. + ROUND(bb, cc, dd, ee, aa, F1, K1, in[14], 9);
  2211. + ROUND(aa, bb, cc, dd, ee, F1, K1, in[15], 8);
  2212. +
  2213. + /* round 2: left lane" */
  2214. + ROUND(ee, aa, bb, cc, dd, F2, K2, in[7], 7);
  2215. + ROUND(dd, ee, aa, bb, cc, F2, K2, in[4], 6);
  2216. + ROUND(cc, dd, ee, aa, bb, F2, K2, in[13], 8);
  2217. + ROUND(bb, cc, dd, ee, aa, F2, K2, in[1], 13);
  2218. + ROUND(aa, bb, cc, dd, ee, F2, K2, in[10], 11);
  2219. + ROUND(ee, aa, bb, cc, dd, F2, K2, in[6], 9);
  2220. + ROUND(dd, ee, aa, bb, cc, F2, K2, in[15], 7);
  2221. + ROUND(cc, dd, ee, aa, bb, F2, K2, in[3], 15);
  2222. + ROUND(bb, cc, dd, ee, aa, F2, K2, in[12], 7);
  2223. + ROUND(aa, bb, cc, dd, ee, F2, K2, in[0], 12);
  2224. + ROUND(ee, aa, bb, cc, dd, F2, K2, in[9], 15);
  2225. + ROUND(dd, ee, aa, bb, cc, F2, K2, in[5], 9);
  2226. + ROUND(cc, dd, ee, aa, bb, F2, K2, in[2], 11);
  2227. + ROUND(bb, cc, dd, ee, aa, F2, K2, in[14], 7);
  2228. + ROUND(aa, bb, cc, dd, ee, F2, K2, in[11], 13);
  2229. + ROUND(ee, aa, bb, cc, dd, F2, K2, in[8], 12);
  2230. +
  2231. + /* round 3: left lane" */
  2232. + ROUND(dd, ee, aa, bb, cc, F3, K3, in[3], 11);
  2233. + ROUND(cc, dd, ee, aa, bb, F3, K3, in[10], 13);
  2234. + ROUND(bb, cc, dd, ee, aa, F3, K3, in[14], 6);
  2235. + ROUND(aa, bb, cc, dd, ee, F3, K3, in[4], 7);
  2236. + ROUND(ee, aa, bb, cc, dd, F3, K3, in[9], 14);
  2237. + ROUND(dd, ee, aa, bb, cc, F3, K3, in[15], 9);
  2238. + ROUND(cc, dd, ee, aa, bb, F3, K3, in[8], 13);
  2239. + ROUND(bb, cc, dd, ee, aa, F3, K3, in[1], 15);
  2240. + ROUND(aa, bb, cc, dd, ee, F3, K3, in[2], 14);
  2241. + ROUND(ee, aa, bb, cc, dd, F3, K3, in[7], 8);
  2242. + ROUND(dd, ee, aa, bb, cc, F3, K3, in[0], 13);
  2243. + ROUND(cc, dd, ee, aa, bb, F3, K3, in[6], 6);
  2244. + ROUND(bb, cc, dd, ee, aa, F3, K3, in[13], 5);
  2245. + ROUND(aa, bb, cc, dd, ee, F3, K3, in[11], 12);
  2246. + ROUND(ee, aa, bb, cc, dd, F3, K3, in[5], 7);
  2247. + ROUND(dd, ee, aa, bb, cc, F3, K3, in[12], 5);
  2248. +
  2249. + /* round 4: left lane" */
  2250. + ROUND(cc, dd, ee, aa, bb, F4, K4, in[1], 11);
  2251. + ROUND(bb, cc, dd, ee, aa, F4, K4, in[9], 12);
  2252. + ROUND(aa, bb, cc, dd, ee, F4, K4, in[11], 14);
  2253. + ROUND(ee, aa, bb, cc, dd, F4, K4, in[10], 15);
  2254. + ROUND(dd, ee, aa, bb, cc, F4, K4, in[0], 14);
  2255. + ROUND(cc, dd, ee, aa, bb, F4, K4, in[8], 15);
  2256. + ROUND(bb, cc, dd, ee, aa, F4, K4, in[12], 9);
  2257. + ROUND(aa, bb, cc, dd, ee, F4, K4, in[4], 8);
  2258. + ROUND(ee, aa, bb, cc, dd, F4, K4, in[13], 9);
  2259. + ROUND(dd, ee, aa, bb, cc, F4, K4, in[3], 14);
  2260. + ROUND(cc, dd, ee, aa, bb, F4, K4, in[7], 5);
  2261. + ROUND(bb, cc, dd, ee, aa, F4, K4, in[15], 6);
  2262. + ROUND(aa, bb, cc, dd, ee, F4, K4, in[14], 8);
  2263. + ROUND(ee, aa, bb, cc, dd, F4, K4, in[5], 6);
  2264. + ROUND(dd, ee, aa, bb, cc, F4, K4, in[6], 5);
  2265. + ROUND(cc, dd, ee, aa, bb, F4, K4, in[2], 12);
  2266. +
  2267. + /* round 5: left lane" */
  2268. + ROUND(bb, cc, dd, ee, aa, F5, K5, in[4], 9);
  2269. + ROUND(aa, bb, cc, dd, ee, F5, K5, in[0], 15);
  2270. + ROUND(ee, aa, bb, cc, dd, F5, K5, in[5], 5);
  2271. + ROUND(dd, ee, aa, bb, cc, F5, K5, in[9], 11);
  2272. + ROUND(cc, dd, ee, aa, bb, F5, K5, in[7], 6);
  2273. + ROUND(bb, cc, dd, ee, aa, F5, K5, in[12], 8);
  2274. + ROUND(aa, bb, cc, dd, ee, F5, K5, in[2], 13);
  2275. + ROUND(ee, aa, bb, cc, dd, F5, K5, in[10], 12);
  2276. + ROUND(dd, ee, aa, bb, cc, F5, K5, in[14], 5);
  2277. + ROUND(cc, dd, ee, aa, bb, F5, K5, in[1], 12);
  2278. + ROUND(bb, cc, dd, ee, aa, F5, K5, in[3], 13);
  2279. + ROUND(aa, bb, cc, dd, ee, F5, K5, in[8], 14);
  2280. + ROUND(ee, aa, bb, cc, dd, F5, K5, in[11], 11);
  2281. + ROUND(dd, ee, aa, bb, cc, F5, K5, in[6], 8);
  2282. + ROUND(cc, dd, ee, aa, bb, F5, K5, in[15], 5);
  2283. + ROUND(bb, cc, dd, ee, aa, F5, K5, in[13], 6);
  2284. +
  2285. + /* round 1: right lane */
  2286. + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[5], 8);
  2287. + ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[14], 9);
  2288. + ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[7], 9);
  2289. + ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[0], 11);
  2290. + ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[9], 13);
  2291. + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[2], 15);
  2292. + ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[11], 15);
  2293. + ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[4], 5);
  2294. + ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[13], 7);
  2295. + ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[6], 7);
  2296. + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[15], 8);
  2297. + ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[8], 11);
  2298. + ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[1], 14);
  2299. + ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[10], 14);
  2300. + ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[3], 12);
  2301. + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[12], 6);
  2302. +
  2303. + /* round 2: right lane */
  2304. + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[6], 9);
  2305. + ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[11], 13);
  2306. + ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[3], 15);
  2307. + ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[7], 7);
  2308. + ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[0], 12);
  2309. + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[13], 8);
  2310. + ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[5], 9);
  2311. + ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[10], 11);
  2312. + ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[14], 7);
  2313. + ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[15], 7);
  2314. + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[8], 12);
  2315. + ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[12], 7);
  2316. + ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[4], 6);
  2317. + ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[9], 15);
  2318. + ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[1], 13);
  2319. + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[2], 11);
  2320. +
  2321. + /* round 3: right lane */
  2322. + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[15], 9);
  2323. + ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[5], 7);
  2324. + ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[1], 15);
  2325. + ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[3], 11);
  2326. + ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[7], 8);
  2327. + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[14], 6);
  2328. + ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[6], 6);
  2329. + ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[9], 14);
  2330. + ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[11], 12);
  2331. + ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[8], 13);
  2332. + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[12], 5);
  2333. + ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[2], 14);
  2334. + ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[10], 13);
  2335. + ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[0], 13);
  2336. + ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[4], 7);
  2337. + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[13], 5);
  2338. +
  2339. + /* round 4: right lane */
  2340. + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[8], 15);
  2341. + ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[6], 5);
  2342. + ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[4], 8);
  2343. + ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[1], 11);
  2344. + ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[3], 14);
  2345. + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[11], 14);
  2346. + ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[15], 6);
  2347. + ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[0], 14);
  2348. + ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[5], 6);
  2349. + ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[12], 9);
  2350. + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[2], 12);
  2351. + ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[13], 9);
  2352. + ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[9], 12);
  2353. + ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[7], 5);
  2354. + ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[10], 15);
  2355. + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[14], 8);
  2356. +
  2357. + /* round 5: right lane */
  2358. + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[12], 8);
  2359. + ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[15], 5);
  2360. + ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[10], 12);
  2361. + ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[4], 9);
  2362. + ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[1], 12);
  2363. + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[5], 5);
  2364. + ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[8], 14);
  2365. + ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[7], 6);
  2366. + ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[6], 8);
  2367. + ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[2], 13);
  2368. + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[13], 6);
  2369. + ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[14], 5);
  2370. + ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[0], 15);
  2371. + ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[3], 13);
  2372. + ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[9], 11);
  2373. + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[11], 11);
  2374. +
  2375. + /* combine results */
  2376. + ddd += cc + state[1]; /* final result for state[0] */
  2377. + state[1] = state[2] + dd + eee;
  2378. + state[2] = state[3] + ee + aaa;
  2379. + state[3] = state[4] + aa + bbb;
  2380. + state[4] = state[0] + bb + ccc;
  2381. + state[0] = ddd;
  2382. +
  2383. + return;
  2384. +}
  2385. +
  2386. +static void rmd160_init(struct crypto_tfm *tfm)
  2387. +{
  2388. + struct rmd160_ctx *rctx = crypto_tfm_ctx(tfm);
  2389. +
  2390. + rctx->byte_count = 0;
  2391. +
  2392. + rctx->state[0] = RMD_H0;
  2393. + rctx->state[1] = RMD_H1;
  2394. + rctx->state[2] = RMD_H2;
  2395. + rctx->state[3] = RMD_H3;
  2396. + rctx->state[4] = RMD_H4;
  2397. +
  2398. + memset(rctx->buffer, 0, sizeof(rctx->buffer));
  2399. +}
  2400. +
  2401. +static void rmd160_update(struct crypto_tfm *tfm, const u8 *data,
  2402. + unsigned int len)
  2403. +{
  2404. + struct rmd160_ctx *rctx = crypto_tfm_ctx(tfm);
  2405. + const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
  2406. +
  2407. + rctx->byte_count += len;
  2408. +
  2409. + /* Enough space in buffer? If so copy and we're done */
  2410. + if (avail > len) {
  2411. + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
  2412. + data, len);
  2413. + return;
  2414. + }
  2415. +
  2416. + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
  2417. + data, avail);
  2418. +
  2419. + rmd160_transform(rctx->state, rctx->buffer);
  2420. + data += avail;
  2421. + len -= avail;
  2422. +
  2423. + while (len >= sizeof(rctx->buffer)) {
  2424. + memcpy(rctx->buffer, data, sizeof(rctx->buffer));
  2425. + rmd160_transform(rctx->state, rctx->buffer);
  2426. + data += sizeof(rctx->buffer);
  2427. + len -= sizeof(rctx->buffer);
  2428. + }
  2429. +
  2430. + memcpy(rctx->buffer, data, len);
  2431. +}
  2432. +
  2433. +/* Add padding and return the message digest. */
  2434. +static void rmd160_final(struct crypto_tfm *tfm, u8 *out)
  2435. +{
  2436. + struct rmd160_ctx *rctx = crypto_tfm_ctx(tfm);
  2437. + u32 i, index, padlen;
  2438. + __le64 bits;
  2439. + __le32 *dst = (__le32 *)out;
  2440. + static const u8 padding[64] = { 0x80, };
  2441. +
  2442. + bits = cpu_to_le64(rctx->byte_count << 3);
  2443. +
  2444. + /* Pad out to 56 mod 64 */
  2445. + index = rctx->byte_count & 0x3f;
  2446. + padlen = (index < 56) ? (56 - index) : ((64+56) - index);
  2447. + rmd160_update(tfm, padding, padlen);
  2448. +
  2449. + /* Append length */
  2450. + rmd160_update(tfm, (const u8 *)&bits, sizeof(bits));
  2451. +
  2452. + /* Store state in digest */
  2453. + for (i = 0; i < 5; i++)
  2454. + dst[i] = cpu_to_le32p(&rctx->state[i]);
  2455. +
  2456. + /* Wipe context */
  2457. + memset(rctx, 0, sizeof(*rctx));
  2458. +}
  2459. +
  2460. +static struct crypto_alg alg = {
  2461. + .cra_name = "rmd160",
  2462. + .cra_driver_name = "rmd160",
  2463. + .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
  2464. + .cra_blocksize = RMD160_BLOCK_SIZE,
  2465. + .cra_ctxsize = sizeof(struct rmd160_ctx),
  2466. + .cra_module = THIS_MODULE,
  2467. + .cra_list = LIST_HEAD_INIT(alg.cra_list),
  2468. + .cra_u = { .digest = {
  2469. + .dia_digestsize = RMD160_DIGEST_SIZE,
  2470. + .dia_init = rmd160_init,
  2471. + .dia_update = rmd160_update,
  2472. + .dia_final = rmd160_final } }
  2473. +};
  2474. +
  2475. +static int __init rmd160_mod_init(void)
  2476. +{
  2477. + return crypto_register_alg(&alg);
  2478. +}
  2479. +
  2480. +static void __exit rmd160_mod_fini(void)
  2481. +{
  2482. + crypto_unregister_alg(&alg);
  2483. +}
  2484. +
  2485. +module_init(rmd160_mod_init);
  2486. +module_exit(rmd160_mod_fini);
  2487. +
  2488. +MODULE_LICENSE("GPL");
  2489. +MODULE_DESCRIPTION("RIPEMD-160 Message Digest");
  2490. +
  2491. +MODULE_ALIAS("rmd160");
  2492. --- /dev/null
  2493. +++ b/crypto/rmd256.c
  2494. @@ -0,0 +1,344 @@
  2495. +/*
  2496. + * Cryptographic API.
  2497. + *
  2498. + * RIPEMD-256 - RACE Integrity Primitives Evaluation Message Digest.
  2499. + *
  2500. + * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
  2501. + *
  2502. + * Copyright (c) 2008 Adrian-Ken Rueegsegger <rueegsegger (at) swiss-it.ch>
  2503. + *
  2504. + * This program is free software; you can redistribute it and/or modify it
  2505. + * under the terms of the GNU General Public License as published by the Free
  2506. + * Software Foundation; either version 2 of the License, or (at your option)
  2507. + * any later version.
  2508. + *
  2509. + */
  2510. +#include <linux/init.h>
  2511. +#include <linux/module.h>
  2512. +#include <linux/mm.h>
  2513. +#include <linux/crypto.h>
  2514. +#include <linux/cryptohash.h>
  2515. +#include <linux/types.h>
  2516. +#include <asm/byteorder.h>
  2517. +
  2518. +#include "ripemd.h"
  2519. +
  2520. +struct rmd256_ctx {
  2521. + u64 byte_count;
  2522. + u32 state[8];
  2523. + __le32 buffer[16];
  2524. +};
  2525. +
  2526. +#define K1 RMD_K1
  2527. +#define K2 RMD_K2
  2528. +#define K3 RMD_K3
  2529. +#define K4 RMD_K4
  2530. +#define KK1 RMD_K6
  2531. +#define KK2 RMD_K7
  2532. +#define KK3 RMD_K8
  2533. +#define KK4 RMD_K1
  2534. +
  2535. +#define F1(x, y, z) (x ^ y ^ z) /* XOR */
  2536. +#define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
  2537. +#define F3(x, y, z) ((x | ~y) ^ z)
  2538. +#define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
  2539. +
  2540. +#define ROUND(a, b, c, d, f, k, x, s) { \
  2541. + (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
  2542. + (a) = rol32((a), (s)); \
  2543. +}
  2544. +
  2545. +static void rmd256_transform(u32 *state, const __le32 *in)
  2546. +{
  2547. + u32 aa, bb, cc, dd, aaa, bbb, ccc, ddd, tmp;
  2548. +
  2549. + /* Initialize left lane */
  2550. + aa = state[0];
  2551. + bb = state[1];
  2552. + cc = state[2];
  2553. + dd = state[3];
  2554. +
  2555. + /* Initialize right lane */
  2556. + aaa = state[4];
  2557. + bbb = state[5];
  2558. + ccc = state[6];
  2559. + ddd = state[7];
  2560. +
  2561. + /* round 1: left lane */
  2562. + ROUND(aa, bb, cc, dd, F1, K1, in[0], 11);
  2563. + ROUND(dd, aa, bb, cc, F1, K1, in[1], 14);
  2564. + ROUND(cc, dd, aa, bb, F1, K1, in[2], 15);
  2565. + ROUND(bb, cc, dd, aa, F1, K1, in[3], 12);
  2566. + ROUND(aa, bb, cc, dd, F1, K1, in[4], 5);
  2567. + ROUND(dd, aa, bb, cc, F1, K1, in[5], 8);
  2568. + ROUND(cc, dd, aa, bb, F1, K1, in[6], 7);
  2569. + ROUND(bb, cc, dd, aa, F1, K1, in[7], 9);
  2570. + ROUND(aa, bb, cc, dd, F1, K1, in[8], 11);
  2571. + ROUND(dd, aa, bb, cc, F1, K1, in[9], 13);
  2572. + ROUND(cc, dd, aa, bb, F1, K1, in[10], 14);
  2573. + ROUND(bb, cc, dd, aa, F1, K1, in[11], 15);
  2574. + ROUND(aa, bb, cc, dd, F1, K1, in[12], 6);
  2575. + ROUND(dd, aa, bb, cc, F1, K1, in[13], 7);
  2576. + ROUND(cc, dd, aa, bb, F1, K1, in[14], 9);
  2577. + ROUND(bb, cc, dd, aa, F1, K1, in[15], 8);
  2578. +
  2579. + /* round 1: right lane */
  2580. + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[5], 8);
  2581. + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[14], 9);
  2582. + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[7], 9);
  2583. + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[0], 11);
  2584. + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[9], 13);
  2585. + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[2], 15);
  2586. + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[11], 15);
  2587. + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[4], 5);
  2588. + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[13], 7);
  2589. + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[6], 7);
  2590. + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[15], 8);
  2591. + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[8], 11);
  2592. + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[1], 14);
  2593. + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[10], 14);
  2594. + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[3], 12);
  2595. + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[12], 6);
  2596. +
  2597. + /* Swap contents of "a" registers */
  2598. + tmp = aa; aa = aaa; aaa = tmp;
  2599. +
  2600. + /* round 2: left lane */
  2601. + ROUND(aa, bb, cc, dd, F2, K2, in[7], 7);
  2602. + ROUND(dd, aa, bb, cc, F2, K2, in[4], 6);
  2603. + ROUND(cc, dd, aa, bb, F2, K2, in[13], 8);
  2604. + ROUND(bb, cc, dd, aa, F2, K2, in[1], 13);
  2605. + ROUND(aa, bb, cc, dd, F2, K2, in[10], 11);
  2606. + ROUND(dd, aa, bb, cc, F2, K2, in[6], 9);
  2607. + ROUND(cc, dd, aa, bb, F2, K2, in[15], 7);
  2608. + ROUND(bb, cc, dd, aa, F2, K2, in[3], 15);
  2609. + ROUND(aa, bb, cc, dd, F2, K2, in[12], 7);
  2610. + ROUND(dd, aa, bb, cc, F2, K2, in[0], 12);
  2611. + ROUND(cc, dd, aa, bb, F2, K2, in[9], 15);
  2612. + ROUND(bb, cc, dd, aa, F2, K2, in[5], 9);
  2613. + ROUND(aa, bb, cc, dd, F2, K2, in[2], 11);
  2614. + ROUND(dd, aa, bb, cc, F2, K2, in[14], 7);
  2615. + ROUND(cc, dd, aa, bb, F2, K2, in[11], 13);
  2616. + ROUND(bb, cc, dd, aa, F2, K2, in[8], 12);
  2617. +
  2618. + /* round 2: right lane */
  2619. + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[6], 9);
  2620. + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[11], 13);
  2621. + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[3], 15);
  2622. + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[7], 7);
  2623. + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[0], 12);
  2624. + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[13], 8);
  2625. + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[5], 9);
  2626. + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[10], 11);
  2627. + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[14], 7);
  2628. + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[15], 7);
  2629. + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[8], 12);
  2630. + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[12], 7);
  2631. + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[4], 6);
  2632. + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[9], 15);
  2633. + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[1], 13);
  2634. + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[2], 11);
  2635. +
  2636. + /* Swap contents of "b" registers */
  2637. + tmp = bb; bb = bbb; bbb = tmp;
  2638. +
  2639. + /* round 3: left lane */
  2640. + ROUND(aa, bb, cc, dd, F3, K3, in[3], 11);
  2641. + ROUND(dd, aa, bb, cc, F3, K3, in[10], 13);
  2642. + ROUND(cc, dd, aa, bb, F3, K3, in[14], 6);
  2643. + ROUND(bb, cc, dd, aa, F3, K3, in[4], 7);
  2644. + ROUND(aa, bb, cc, dd, F3, K3, in[9], 14);
  2645. + ROUND(dd, aa, bb, cc, F3, K3, in[15], 9);
  2646. + ROUND(cc, dd, aa, bb, F3, K3, in[8], 13);
  2647. + ROUND(bb, cc, dd, aa, F3, K3, in[1], 15);
  2648. + ROUND(aa, bb, cc, dd, F3, K3, in[2], 14);
  2649. + ROUND(dd, aa, bb, cc, F3, K3, in[7], 8);
  2650. + ROUND(cc, dd, aa, bb, F3, K3, in[0], 13);
  2651. + ROUND(bb, cc, dd, aa, F3, K3, in[6], 6);
  2652. + ROUND(aa, bb, cc, dd, F3, K3, in[13], 5);
  2653. + ROUND(dd, aa, bb, cc, F3, K3, in[11], 12);
  2654. + ROUND(cc, dd, aa, bb, F3, K3, in[5], 7);
  2655. + ROUND(bb, cc, dd, aa, F3, K3, in[12], 5);
  2656. +
  2657. + /* round 3: right lane */
  2658. + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[15], 9);
  2659. + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[5], 7);
  2660. + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[1], 15);
  2661. + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[3], 11);
  2662. + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[7], 8);
  2663. + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[14], 6);
  2664. + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[6], 6);
  2665. + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[9], 14);
  2666. + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[11], 12);
  2667. + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[8], 13);
  2668. + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[12], 5);
  2669. + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[2], 14);
  2670. + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[10], 13);
  2671. + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[0], 13);
  2672. + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[4], 7);
  2673. + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[13], 5);
  2674. +
  2675. + /* Swap contents of "c" registers */
  2676. + tmp = cc; cc = ccc; ccc = tmp;
  2677. +
  2678. + /* round 4: left lane */
  2679. + ROUND(aa, bb, cc, dd, F4, K4, in[1], 11);
  2680. + ROUND(dd, aa, bb, cc, F4, K4, in[9], 12);
  2681. + ROUND(cc, dd, aa, bb, F4, K4, in[11], 14);
  2682. + ROUND(bb, cc, dd, aa, F4, K4, in[10], 15);
  2683. + ROUND(aa, bb, cc, dd, F4, K4, in[0], 14);
  2684. + ROUND(dd, aa, bb, cc, F4, K4, in[8], 15);
  2685. + ROUND(cc, dd, aa, bb, F4, K4, in[12], 9);
  2686. + ROUND(bb, cc, dd, aa, F4, K4, in[4], 8);
  2687. + ROUND(aa, bb, cc, dd, F4, K4, in[13], 9);
  2688. + ROUND(dd, aa, bb, cc, F4, K4, in[3], 14);
  2689. + ROUND(cc, dd, aa, bb, F4, K4, in[7], 5);
  2690. + ROUND(bb, cc, dd, aa, F4, K4, in[15], 6);
  2691. + ROUND(aa, bb, cc, dd, F4, K4, in[14], 8);
  2692. + ROUND(dd, aa, bb, cc, F4, K4, in[5], 6);
  2693. + ROUND(cc, dd, aa, bb, F4, K4, in[6], 5);
  2694. + ROUND(bb, cc, dd, aa, F4, K4, in[2], 12);
  2695. +
  2696. + /* round 4: right lane */
  2697. + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[8], 15);
  2698. + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[6], 5);
  2699. + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[4], 8);
  2700. + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[1], 11);
  2701. + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[3], 14);
  2702. + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[11], 14);
  2703. + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[15], 6);
  2704. + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[0], 14);
  2705. + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[5], 6);
  2706. + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[12], 9);
  2707. + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[2], 12);
  2708. + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[13], 9);
  2709. + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[9], 12);
  2710. + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[7], 5);
  2711. + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[10], 15);
  2712. + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[14], 8);
  2713. +
  2714. + /* Swap contents of "d" registers */
  2715. + tmp = dd; dd = ddd; ddd = tmp;
  2716. +
  2717. + /* combine results */
  2718. + state[0] += aa;
  2719. + state[1] += bb;
  2720. + state[2] += cc;
  2721. + state[3] += dd;
  2722. + state[4] += aaa;
  2723. + state[5] += bbb;
  2724. + state[6] += ccc;
  2725. + state[7] += ddd;
  2726. +
  2727. + return;
  2728. +}
  2729. +
  2730. +static void rmd256_init(struct crypto_tfm *tfm)
  2731. +{
  2732. + struct rmd256_ctx *rctx = crypto_tfm_ctx(tfm);
  2733. +
  2734. + rctx->byte_count = 0;
  2735. +
  2736. + rctx->state[0] = RMD_H0;
  2737. + rctx->state[1] = RMD_H1;
  2738. + rctx->state[2] = RMD_H2;
  2739. + rctx->state[3] = RMD_H3;
  2740. + rctx->state[4] = RMD_H5;
  2741. + rctx->state[5] = RMD_H6;
  2742. + rctx->state[6] = RMD_H7;
  2743. + rctx->state[7] = RMD_H8;
  2744. +
  2745. + memset(rctx->buffer, 0, sizeof(rctx->buffer));
  2746. +}
  2747. +
  2748. +static void rmd256_update(struct crypto_tfm *tfm, const u8 *data,
  2749. + unsigned int len)
  2750. +{
  2751. + struct rmd256_ctx *rctx = crypto_tfm_ctx(tfm);
  2752. + const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
  2753. +
  2754. + rctx->byte_count += len;
  2755. +
  2756. + /* Enough space in buffer? If so copy and we're done */
  2757. + if (avail > len) {
  2758. + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
  2759. + data, len);
  2760. + return;
  2761. + }
  2762. +
  2763. + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
  2764. + data, avail);
  2765. +
  2766. + rmd256_transform(rctx->state, rctx->buffer);
  2767. + data += avail;
  2768. + len -= avail;
  2769. +
  2770. + while (len >= sizeof(rctx->buffer)) {
  2771. + memcpy(rctx->buffer, data, sizeof(rctx->buffer));
  2772. + rmd256_transform(rctx->state, rctx->buffer);
  2773. + data += sizeof(rctx->buffer);
  2774. + len -= sizeof(rctx->buffer);
  2775. + }
  2776. +
  2777. + memcpy(rctx->buffer, data, len);
  2778. +}
  2779. +
  2780. +/* Add padding and return the message digest. */
  2781. +static void rmd256_final(struct crypto_tfm *tfm, u8 *out)
  2782. +{
  2783. + struct rmd256_ctx *rctx = crypto_tfm_ctx(tfm);
  2784. + u32 i, index, padlen;
  2785. + __le64 bits;
  2786. + __le32 *dst = (__le32 *)out;
  2787. + static const u8 padding[64] = { 0x80, };
  2788. +
  2789. + bits = cpu_to_le64(rctx->byte_count << 3);
  2790. +
  2791. + /* Pad out to 56 mod 64 */
  2792. + index = rctx->byte_count & 0x3f;
  2793. + padlen = (index < 56) ? (56 - index) : ((64+56) - index);
  2794. + rmd256_update(tfm, padding, padlen);
  2795. +
  2796. + /* Append length */
  2797. + rmd256_update(tfm, (const u8 *)&bits, sizeof(bits));
  2798. +
  2799. + /* Store state in digest */
  2800. + for (i = 0; i < 8; i++)
  2801. + dst[i] = cpu_to_le32p(&rctx->state[i]);
  2802. +
  2803. + /* Wipe context */
  2804. + memset(rctx, 0, sizeof(*rctx));
  2805. +}
  2806. +
  2807. +static struct crypto_alg alg = {
  2808. + .cra_name = "rmd256",
  2809. + .cra_driver_name = "rmd256",
  2810. + .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
  2811. + .cra_blocksize = RMD256_BLOCK_SIZE,
  2812. + .cra_ctxsize = sizeof(struct rmd256_ctx),
  2813. + .cra_module = THIS_MODULE,
  2814. + .cra_list = LIST_HEAD_INIT(alg.cra_list),
  2815. + .cra_u = { .digest = {
  2816. + .dia_digestsize = RMD256_DIGEST_SIZE,
  2817. + .dia_init = rmd256_init,
  2818. + .dia_update = rmd256_update,
  2819. + .dia_final = rmd256_final } }
  2820. +};
  2821. +
  2822. +static int __init rmd256_mod_init(void)
  2823. +{
  2824. + return crypto_register_alg(&alg);
  2825. +}
  2826. +
  2827. +static void __exit rmd256_mod_fini(void)
  2828. +{
  2829. + crypto_unregister_alg(&alg);
  2830. +}
  2831. +
  2832. +module_init(rmd256_mod_init);
  2833. +module_exit(rmd256_mod_fini);
  2834. +
  2835. +MODULE_LICENSE("GPL");
  2836. +MODULE_DESCRIPTION("RIPEMD-256 Message Digest");
  2837. +
  2838. +MODULE_ALIAS("rmd256");
  2839. --- /dev/null
  2840. +++ b/crypto/rmd320.c
  2841. @@ -0,0 +1,393 @@
  2842. +/*
  2843. + * Cryptographic API.
  2844. + *
  2845. + * RIPEMD-320 - RACE Integrity Primitives Evaluation Message Digest.
  2846. + *
  2847. + * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
  2848. + *
  2849. + * Copyright (c) 2008 Adrian-Ken Rueegsegger <rueegsegger (at) swiss-it.ch>
  2850. + *
  2851. + * This program is free software; you can redistribute it and/or modify it
  2852. + * under the terms of the GNU General Public License as published by the Free
  2853. + * Software Foundation; either version 2 of the License, or (at your option)
  2854. + * any later version.
  2855. + *
  2856. + */
  2857. +#include <linux/init.h>
  2858. +#include <linux/module.h>
  2859. +#include <linux/mm.h>
  2860. +#include <linux/crypto.h>
  2861. +#include <linux/cryptohash.h>
  2862. +#include <linux/types.h>
  2863. +#include <asm/byteorder.h>
  2864. +
  2865. +#include "ripemd.h"
  2866. +
  2867. +struct rmd320_ctx {
  2868. + u64 byte_count;
  2869. + u32 state[10];
  2870. + __le32 buffer[16];
  2871. +};
  2872. +
  2873. +#define K1 RMD_K1
  2874. +#define K2 RMD_K2
  2875. +#define K3 RMD_K3
  2876. +#define K4 RMD_K4
  2877. +#define K5 RMD_K5
  2878. +#define KK1 RMD_K6
  2879. +#define KK2 RMD_K7
  2880. +#define KK3 RMD_K8
  2881. +#define KK4 RMD_K9
  2882. +#define KK5 RMD_K1
  2883. +
  2884. +#define F1(x, y, z) (x ^ y ^ z) /* XOR */
  2885. +#define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
  2886. +#define F3(x, y, z) ((x | ~y) ^ z)
  2887. +#define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
  2888. +#define F5(x, y, z) (x ^ (y | ~z))
  2889. +
  2890. +#define ROUND(a, b, c, d, e, f, k, x, s) { \
  2891. + (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
  2892. + (a) = rol32((a), (s)) + (e); \
  2893. + (c) = rol32((c), 10); \
  2894. +}
  2895. +
  2896. +static void rmd320_transform(u32 *state, const __le32 *in)
  2897. +{
  2898. + u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee, tmp;
  2899. +
  2900. + /* Initialize left lane */
  2901. + aa = state[0];
  2902. + bb = state[1];
  2903. + cc = state[2];
  2904. + dd = state[3];
  2905. + ee = state[4];
  2906. +
  2907. + /* Initialize right lane */
  2908. + aaa = state[5];
  2909. + bbb = state[6];
  2910. + ccc = state[7];
  2911. + ddd = state[8];
  2912. + eee = state[9];
  2913. +
  2914. + /* round 1: left lane */
  2915. + ROUND(aa, bb, cc, dd, ee, F1, K1, in[0], 11);
  2916. + ROUND(ee, aa, bb, cc, dd, F1, K1, in[1], 14);
  2917. + ROUND(dd, ee, aa, bb, cc, F1, K1, in[2], 15);
  2918. + ROUND(cc, dd, ee, aa, bb, F1, K1, in[3], 12);
  2919. + ROUND(bb, cc, dd, ee, aa, F1, K1, in[4], 5);
  2920. + ROUND(aa, bb, cc, dd, ee, F1, K1, in[5], 8);
  2921. + ROUND(ee, aa, bb, cc, dd, F1, K1, in[6], 7);
  2922. + ROUND(dd, ee, aa, bb, cc, F1, K1, in[7], 9);
  2923. + ROUND(cc, dd, ee, aa, bb, F1, K1, in[8], 11);
  2924. + ROUND(bb, cc, dd, ee, aa, F1, K1, in[9], 13);
  2925. + ROUND(aa, bb, cc, dd, ee, F1, K1, in[10], 14);
  2926. + ROUND(ee, aa, bb, cc, dd, F1, K1, in[11], 15);
  2927. + ROUND(dd, ee, aa, bb, cc, F1, K1, in[12], 6);
  2928. + ROUND(cc, dd, ee, aa, bb, F1, K1, in[13], 7);
  2929. + ROUND(bb, cc, dd, ee, aa, F1, K1, in[14], 9);
  2930. + ROUND(aa, bb, cc, dd, ee, F1, K1, in[15], 8);
  2931. +
  2932. + /* round 1: right lane */
  2933. + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[5], 8);
  2934. + ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[14], 9);
  2935. + ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[7], 9);
  2936. + ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[0], 11);
  2937. + ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[9], 13);
  2938. + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[2], 15);
  2939. + ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[11], 15);
  2940. + ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[4], 5);
  2941. + ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[13], 7);
  2942. + ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[6], 7);
  2943. + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[15], 8);
  2944. + ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[8], 11);
  2945. + ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[1], 14);
  2946. + ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[10], 14);
  2947. + ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[3], 12);
  2948. + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[12], 6);
  2949. +
  2950. + /* Swap contents of "a" registers */
  2951. + tmp = aa; aa = aaa; aaa = tmp;
  2952. +
  2953. + /* round 2: left lane" */
  2954. + ROUND(ee, aa, bb, cc, dd, F2, K2, in[7], 7);
  2955. + ROUND(dd, ee, aa, bb, cc, F2, K2, in[4], 6);
  2956. + ROUND(cc, dd, ee, aa, bb, F2, K2, in[13], 8);
  2957. + ROUND(bb, cc, dd, ee, aa, F2, K2, in[1], 13);
  2958. + ROUND(aa, bb, cc, dd, ee, F2, K2, in[10], 11);
  2959. + ROUND(ee, aa, bb, cc, dd, F2, K2, in[6], 9);
  2960. + ROUND(dd, ee, aa, bb, cc, F2, K2, in[15], 7);
  2961. + ROUND(cc, dd, ee, aa, bb, F2, K2, in[3], 15);
  2962. + ROUND(bb, cc, dd, ee, aa, F2, K2, in[12], 7);
  2963. + ROUND(aa, bb, cc, dd, ee, F2, K2, in[0], 12);
  2964. + ROUND(ee, aa, bb, cc, dd, F2, K2, in[9], 15);
  2965. + ROUND(dd, ee, aa, bb, cc, F2, K2, in[5], 9);
  2966. + ROUND(cc, dd, ee, aa, bb, F2, K2, in[2], 11);
  2967. + ROUND(bb, cc, dd, ee, aa, F2, K2, in[14], 7);
  2968. + ROUND(aa, bb, cc, dd, ee, F2, K2, in[11], 13);
  2969. + ROUND(ee, aa, bb, cc, dd, F2, K2, in[8], 12);
  2970. +
  2971. + /* round 2: right lane */
  2972. + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[6], 9);
  2973. + ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[11], 13);
  2974. + ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[3], 15);
  2975. + ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[7], 7);
  2976. + ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[0], 12);
  2977. + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[13], 8);
  2978. + ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[5], 9);
  2979. + ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[10], 11);
  2980. + ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[14], 7);
  2981. + ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[15], 7);
  2982. + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[8], 12);
  2983. + ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[12], 7);
  2984. + ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[4], 6);
  2985. + ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[9], 15);
  2986. + ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[1], 13);
  2987. + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[2], 11);
  2988. +
  2989. + /* Swap contents of "b" registers */
  2990. + tmp = bb; bb = bbb; bbb = tmp;
  2991. +
  2992. + /* round 3: left lane" */
  2993. + ROUND(dd, ee, aa, bb, cc, F3, K3, in[3], 11);
  2994. + ROUND(cc, dd, ee, aa, bb, F3, K3, in[10], 13);
  2995. + ROUND(bb, cc, dd, ee, aa, F3, K3, in[14], 6);
  2996. + ROUND(aa, bb, cc, dd, ee, F3, K3, in[4], 7);
  2997. + ROUND(ee, aa, bb, cc, dd, F3, K3, in[9], 14);
  2998. + ROUND(dd, ee, aa, bb, cc, F3, K3, in[15], 9);
  2999. + ROUND(cc, dd, ee, aa, bb, F3, K3, in[8], 13);
  3000. + ROUND(bb, cc, dd, ee, aa, F3, K3, in[1], 15);
  3001. + ROUND(aa, bb, cc, dd, ee, F3, K3, in[2], 14);
  3002. + ROUND(ee, aa, bb, cc, dd, F3, K3, in[7], 8);
  3003. + ROUND(dd, ee, aa, bb, cc, F3, K3, in[0], 13);
  3004. + ROUND(cc, dd, ee, aa, bb, F3, K3, in[6], 6);
  3005. + ROUND(bb, cc, dd, ee, aa, F3, K3, in[13], 5);
  3006. + ROUND(aa, bb, cc, dd, ee, F3, K3, in[11], 12);
  3007. + ROUND(ee, aa, bb, cc, dd, F3, K3, in[5], 7);
  3008. + ROUND(dd, ee, aa, bb, cc, F3, K3, in[12], 5);
  3009. +
  3010. + /* round 3: right lane */
  3011. + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[15], 9);
  3012. + ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[5], 7);
  3013. + ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[1], 15);
  3014. + ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[3], 11);
  3015. + ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[7], 8);
  3016. + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[14], 6);
  3017. + ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[6], 6);
  3018. + ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[9], 14);
  3019. + ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[11], 12);
  3020. + ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[8], 13);
  3021. + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[12], 5);
  3022. + ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[2], 14);
  3023. + ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[10], 13);
  3024. + ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[0], 13);
  3025. + ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[4], 7);
  3026. + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[13], 5);
  3027. +
  3028. + /* Swap contents of "c" registers */
  3029. + tmp = cc; cc = ccc; ccc = tmp;
  3030. +
  3031. + /* round 4: left lane" */
  3032. + ROUND(cc, dd, ee, aa, bb, F4, K4, in[1], 11);
  3033. + ROUND(bb, cc, dd, ee, aa, F4, K4, in[9], 12);
  3034. + ROUND(aa, bb, cc, dd, ee, F4, K4, in[11], 14);
  3035. + ROUND(ee, aa, bb, cc, dd, F4, K4, in[10], 15);
  3036. + ROUND(dd, ee, aa, bb, cc, F4, K4, in[0], 14);
  3037. + ROUND(cc, dd, ee, aa, bb, F4, K4, in[8], 15);
  3038. + ROUND(bb, cc, dd, ee, aa, F4, K4, in[12], 9);
  3039. + ROUND(aa, bb, cc, dd, ee, F4, K4, in[4], 8);
  3040. + ROUND(ee, aa, bb, cc, dd, F4, K4, in[13], 9);
  3041. + ROUND(dd, ee, aa, bb, cc, F4, K4, in[3], 14);
  3042. + ROUND(cc, dd, ee, aa, bb, F4, K4, in[7], 5);
  3043. + ROUND(bb, cc, dd, ee, aa, F4, K4, in[15], 6);
  3044. + ROUND(aa, bb, cc, dd, ee, F4, K4, in[14], 8);
  3045. + ROUND(ee, aa, bb, cc, dd, F4, K4, in[5], 6);
  3046. + ROUND(dd, ee, aa, bb, cc, F4, K4, in[6], 5);
  3047. + ROUND(cc, dd, ee, aa, bb, F4, K4, in[2], 12);
  3048. +
  3049. + /* round 4: right lane */
  3050. + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[8], 15);
  3051. + ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[6], 5);
  3052. + ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[4], 8);
  3053. + ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[1], 11);
  3054. + ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[3], 14);
  3055. + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[11], 14);
  3056. + ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[15], 6);
  3057. + ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[0], 14);
  3058. + ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[5], 6);
  3059. + ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[12], 9);
  3060. + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[2], 12);
  3061. + ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[13], 9);
  3062. + ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[9], 12);
  3063. + ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[7], 5);
  3064. + ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[10], 15);
  3065. + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[14], 8);
  3066. +
  3067. + /* Swap contents of "d" registers */
  3068. + tmp = dd; dd = ddd; ddd = tmp;
  3069. +
  3070. + /* round 5: left lane" */
  3071. + ROUND(bb, cc, dd, ee, aa, F5, K5, in[4], 9);
  3072. + ROUND(aa, bb, cc, dd, ee, F5, K5, in[0], 15);
  3073. + ROUND(ee, aa, bb, cc, dd, F5, K5, in[5], 5);
  3074. + ROUND(dd, ee, aa, bb, cc, F5, K5, in[9], 11);
  3075. + ROUND(cc, dd, ee, aa, bb, F5, K5, in[7], 6);
  3076. + ROUND(bb, cc, dd, ee, aa, F5, K5, in[12], 8);
  3077. + ROUND(aa, bb, cc, dd, ee, F5, K5, in[2], 13);
  3078. + ROUND(ee, aa, bb, cc, dd, F5, K5, in[10], 12);
  3079. + ROUND(dd, ee, aa, bb, cc, F5, K5, in[14], 5);
  3080. + ROUND(cc, dd, ee, aa, bb, F5, K5, in[1], 12);
  3081. + ROUND(bb, cc, dd, ee, aa, F5, K5, in[3], 13);
  3082. + ROUND(aa, bb, cc, dd, ee, F5, K5, in[8], 14);
  3083. + ROUND(ee, aa, bb, cc, dd, F5, K5, in[11], 11);
  3084. + ROUND(dd, ee, aa, bb, cc, F5, K5, in[6], 8);
  3085. + ROUND(cc, dd, ee, aa, bb, F5, K5, in[15], 5);
  3086. + ROUND(bb, cc, dd, ee, aa, F5, K5, in[13], 6);
  3087. +
  3088. + /* round 5: right lane */
  3089. + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[12], 8);
  3090. + ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[15], 5);
  3091. + ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[10], 12);
  3092. + ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[4], 9);
  3093. + ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[1], 12);
  3094. + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[5], 5);
  3095. + ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[8], 14);
  3096. + ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[7], 6);
  3097. + ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[6], 8);
  3098. + ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[2], 13);
  3099. + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[13], 6);
  3100. + ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[14], 5);
  3101. + ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[0], 15);
  3102. + ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[3], 13);
  3103. + ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[9], 11);
  3104. + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[11], 11);
  3105. +
  3106. + /* Swap contents of "e" registers */
  3107. + tmp = ee; ee = eee; eee = tmp;
  3108. +
  3109. + /* combine results */
  3110. + state[0] += aa;
  3111. + state[1] += bb;
  3112. + state[2] += cc;
  3113. + state[3] += dd;
  3114. + state[4] += ee;
  3115. + state[5] += aaa;
  3116. + state[6] += bbb;
  3117. + state[7] += ccc;
  3118. + state[8] += ddd;
  3119. + state[9] += eee;
  3120. +
  3121. + return;
  3122. +}
  3123. +
  3124. +static void rmd320_init(struct crypto_tfm *tfm)
  3125. +{
  3126. + struct rmd320_ctx *rctx = crypto_tfm_ctx(tfm);
  3127. +
  3128. + rctx->byte_count = 0;
  3129. +
  3130. + rctx->state[0] = RMD_H0;
  3131. + rctx->state[1] = RMD_H1;
  3132. + rctx->state[2] = RMD_H2;
  3133. + rctx->state[3] = RMD_H3;
  3134. + rctx->state[4] = RMD_H4;
  3135. + rctx->state[5] = RMD_H5;
  3136. + rctx->state[6] = RMD_H6;
  3137. + rctx->state[7] = RMD_H7;
  3138. + rctx->state[8] = RMD_H8;
  3139. + rctx->state[9] = RMD_H9;
  3140. +
  3141. + memset(rctx->buffer, 0, sizeof(rctx->buffer));
  3142. +}
  3143. +
  3144. +static void rmd320_update(struct crypto_tfm *tfm, const u8 *data,
  3145. + unsigned int len)
  3146. +{
  3147. + struct rmd320_ctx *rctx = crypto_tfm_ctx(tfm);
  3148. + const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
  3149. +
  3150. + rctx->byte_count += len;
  3151. +
  3152. + /* Enough space in buffer? If so copy and we're done */
  3153. + if (avail > len) {
  3154. + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
  3155. + data, len);
  3156. + return;
  3157. + }
  3158. +
  3159. + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
  3160. + data, avail);
  3161. +
  3162. + rmd320_transform(rctx->state, rctx->buffer);
  3163. + data += avail;
  3164. + len -= avail;
  3165. +
  3166. + while (len >= sizeof(rctx->buffer)) {
  3167. + memcpy(rctx->buffer, data, sizeof(rctx->buffer));
  3168. + rmd320_transform(rctx->state, rctx->buffer);
  3169. + data += sizeof(rctx->buffer);
  3170. + len -= sizeof(rctx->buffer);
  3171. + }
  3172. +
  3173. + memcpy(rctx->buffer, data, len);
  3174. +}
  3175. +
  3176. +/* Add padding and return the message digest. */
  3177. +static void rmd320_final(struct crypto_tfm *tfm, u8 *out)
  3178. +{
  3179. + struct rmd320_ctx *rctx = crypto_tfm_ctx(tfm);
  3180. + u32 i, index, padlen;
  3181. + __le64 bits;
  3182. + __le32 *dst = (__le32 *)out;
  3183. + static const u8 padding[64] = { 0x80, };
  3184. +
  3185. + bits = cpu_to_le64(rctx->byte_count << 3);
  3186. +
  3187. + /* Pad out to 56 mod 64 */
  3188. + index = rctx->byte_count & 0x3f;
  3189. + padlen = (index < 56) ? (56 - index) : ((64+56) - index);
  3190. + rmd320_update(tfm, padding, padlen);
  3191. +
  3192. + /* Append length */
  3193. + rmd320_update(tfm, (const u8 *)&bits, sizeof(bits));
  3194. +
  3195. + /* Store state in digest */
  3196. + for (i = 0; i < 10; i++)
  3197. + dst[i] = cpu_to_le32p(&rctx->state[i]);
  3198. +
  3199. + /* Wipe context */
  3200. + memset(rctx, 0, sizeof(*rctx));
  3201. +}
  3202. +
  3203. +static struct crypto_alg alg = {
  3204. + .cra_name = "rmd320",
  3205. + .cra_driver_name = "rmd320",
  3206. + .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
  3207. + .cra_blocksize = RMD320_BLOCK_SIZE,
  3208. + .cra_ctxsize = sizeof(struct rmd320_ctx),
  3209. + .cra_module = THIS_MODULE,
  3210. + .cra_list = LIST_HEAD_INIT(alg.cra_list),
  3211. + .cra_u = { .digest = {
  3212. + .dia_digestsize = RMD320_DIGEST_SIZE,
  3213. + .dia_init = rmd320_init,
  3214. + .dia_update = rmd320_update,
  3215. + .dia_final = rmd320_final } }
  3216. +};
  3217. +
  3218. +static int __init rmd320_mod_init(void)
  3219. +{
  3220. + return crypto_register_alg(&alg);
  3221. +}
  3222. +
  3223. +static void __exit rmd320_mod_fini(void)
  3224. +{
  3225. + crypto_unregister_alg(&alg);
  3226. +}
  3227. +
  3228. +module_init(rmd320_mod_init);
  3229. +module_exit(rmd320_mod_fini);
  3230. +
  3231. +MODULE_LICENSE("GPL");
  3232. +MODULE_DESCRIPTION("RIPEMD-320 Message Digest");
  3233. +
  3234. +MODULE_ALIAS("rmd320");
  3235. --- a/crypto/tcrypt.c
  3236. +++ b/crypto/tcrypt.c
  3237. @@ -13,15 +13,9 @@
  3238. * Software Foundation; either version 2 of the License, or (at your option)
  3239. * any later version.
  3240. *
  3241. - * 2007-11-13 Added GCM tests
  3242. - * 2007-11-13 Added AEAD support
  3243. - * 2007-11-06 Added SHA-224 and SHA-224-HMAC tests
  3244. - * 2006-12-07 Added SHA384 HMAC and SHA512 HMAC tests
  3245. - * 2004-08-09 Added cipher speed tests (Reyk Floeter <[email protected]>)
  3246. - * 2003-09-14 Rewritten by Kartikey Mahendra Bhatt
  3247. - *
  3248. */
  3249. +#include <crypto/hash.h>
  3250. #include <linux/err.h>
  3251. #include <linux/init.h>
  3252. #include <linux/module.h>
  3253. @@ -30,7 +24,6 @@
  3254. #include <linux/scatterlist.h>
  3255. #include <linux/string.h>
  3256. #include <linux/crypto.h>
  3257. -#include <linux/highmem.h>
  3258. #include <linux/moduleparam.h>
  3259. #include <linux/jiffies.h>
  3260. #include <linux/timex.h>
  3261. @@ -38,7 +31,7 @@
  3262. #include "tcrypt.h"
  3263. /*
  3264. - * Need to kmalloc() memory for testing kmap().
  3265. + * Need to kmalloc() memory for testing.
  3266. */
  3267. #define TVMEMSIZE 16384
  3268. #define XBUFSIZE 32768
  3269. @@ -46,7 +39,7 @@
  3270. /*
  3271. * Indexes into the xbuf to simulate cross-page access.
  3272. */
  3273. -#define IDX1 37
  3274. +#define IDX1 32
  3275. #define IDX2 32400
  3276. #define IDX3 1
  3277. #define IDX4 8193
  3278. @@ -83,7 +76,8 @@ static char *check[] = {
  3279. "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
  3280. "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
  3281. "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
  3282. - "camellia", "seed", "salsa20", "lzo", "cts", NULL
  3283. + "camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
  3284. + "lzo", "cts", NULL
  3285. };
  3286. static void hexdump(unsigned char *buf, unsigned int len)
  3287. @@ -110,22 +104,30 @@ static void test_hash(char *algo, struct
  3288. unsigned int i, j, k, temp;
  3289. struct scatterlist sg[8];
  3290. char result[64];
  3291. - struct crypto_hash *tfm;
  3292. - struct hash_desc desc;
  3293. + struct crypto_ahash *tfm;
  3294. + struct ahash_request *req;
  3295. + struct tcrypt_result tresult;
  3296. int ret;
  3297. void *hash_buff;
  3298. printk("\ntesting %s\n", algo);
  3299. - tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
  3300. + init_completion(&tresult.completion);
  3301. +
  3302. + tfm = crypto_alloc_ahash(algo, 0, 0);
  3303. if (IS_ERR(tfm)) {
  3304. printk("failed to load transform for %s: %ld\n", algo,
  3305. PTR_ERR(tfm));
  3306. return;
  3307. }
  3308. - desc.tfm = tfm;
  3309. - desc.flags = 0;
  3310. + req = ahash_request_alloc(tfm, GFP_KERNEL);
  3311. + if (!req) {
  3312. + printk(KERN_ERR "failed to allocate request for %s\n", algo);
  3313. + goto out_noreq;
  3314. + }
  3315. + ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  3316. + tcrypt_complete, &tresult);
  3317. for (i = 0; i < tcount; i++) {
  3318. printk("test %u:\n", i + 1);
  3319. @@ -139,8 +141,9 @@ static void test_hash(char *algo, struct
  3320. sg_init_one(&sg[0], hash_buff, template[i].psize);
  3321. if (template[i].ksize) {
  3322. - ret = crypto_hash_setkey(tfm, template[i].key,
  3323. - template[i].ksize);
  3324. + crypto_ahash_clear_flags(tfm, ~0);
  3325. + ret = crypto_ahash_setkey(tfm, template[i].key,
  3326. + template[i].ksize);
  3327. if (ret) {
  3328. printk("setkey() failed ret=%d\n", ret);
  3329. kfree(hash_buff);
  3330. @@ -148,17 +151,30 @@ static void test_hash(char *algo, struct
  3331. }
  3332. }
  3333. - ret = crypto_hash_digest(&desc, sg, template[i].psize, result);
  3334. - if (ret) {
  3335. + ahash_request_set_crypt(req, sg, result, template[i].psize);
  3336. + ret = crypto_ahash_digest(req);
  3337. + switch (ret) {
  3338. + case 0:
  3339. + break;
  3340. + case -EINPROGRESS:
  3341. + case -EBUSY:
  3342. + ret = wait_for_completion_interruptible(
  3343. + &tresult.completion);
  3344. + if (!ret && !(ret = tresult.err)) {
  3345. + INIT_COMPLETION(tresult.completion);
  3346. + break;
  3347. + }
  3348. + /* fall through */
  3349. + default:
  3350. printk("digest () failed ret=%d\n", ret);
  3351. kfree(hash_buff);
  3352. goto out;
  3353. }
  3354. - hexdump(result, crypto_hash_digestsize(tfm));
  3355. + hexdump(result, crypto_ahash_digestsize(tfm));
  3356. printk("%s\n",
  3357. memcmp(result, template[i].digest,
  3358. - crypto_hash_digestsize(tfm)) ?
  3359. + crypto_ahash_digestsize(tfm)) ?
  3360. "fail" : "pass");
  3361. kfree(hash_buff);
  3362. }
  3363. @@ -187,8 +203,9 @@ static void test_hash(char *algo, struct
  3364. }
  3365. if (template[i].ksize) {
  3366. - ret = crypto_hash_setkey(tfm, template[i].key,
  3367. - template[i].ksize);
  3368. + crypto_ahash_clear_flags(tfm, ~0);
  3369. + ret = crypto_ahash_setkey(tfm, template[i].key,
  3370. + template[i].ksize);
  3371. if (ret) {
  3372. printk("setkey() failed ret=%d\n", ret);
  3373. @@ -196,29 +213,44 @@ static void test_hash(char *algo, struct
  3374. }
  3375. }
  3376. - ret = crypto_hash_digest(&desc, sg, template[i].psize,
  3377. - result);
  3378. - if (ret) {
  3379. + ahash_request_set_crypt(req, sg, result,
  3380. + template[i].psize);
  3381. + ret = crypto_ahash_digest(req);
  3382. + switch (ret) {
  3383. + case 0:
  3384. + break;
  3385. + case -EINPROGRESS:
  3386. + case -EBUSY:
  3387. + ret = wait_for_completion_interruptible(
  3388. + &tresult.completion);
  3389. + if (!ret && !(ret = tresult.err)) {
  3390. + INIT_COMPLETION(tresult.completion);
  3391. + break;
  3392. + }
  3393. + /* fall through */
  3394. + default:
  3395. printk("digest () failed ret=%d\n", ret);
  3396. goto out;
  3397. }
  3398. - hexdump(result, crypto_hash_digestsize(tfm));
  3399. + hexdump(result, crypto_ahash_digestsize(tfm));
  3400. printk("%s\n",
  3401. memcmp(result, template[i].digest,
  3402. - crypto_hash_digestsize(tfm)) ?
  3403. + crypto_ahash_digestsize(tfm)) ?
  3404. "fail" : "pass");
  3405. }
  3406. }
  3407. out:
  3408. - crypto_free_hash(tfm);
  3409. + ahash_request_free(req);
  3410. +out_noreq:
  3411. + crypto_free_ahash(tfm);
  3412. }
  3413. static void test_aead(char *algo, int enc, struct aead_testvec *template,
  3414. unsigned int tcount)
  3415. {
  3416. - unsigned int ret, i, j, k, temp;
  3417. + unsigned int ret, i, j, k, n, temp;
  3418. char *q;
  3419. struct crypto_aead *tfm;
  3420. char *key;
  3421. @@ -344,13 +376,12 @@ static void test_aead(char *algo, int en
  3422. goto next_one;
  3423. }
  3424. - q = kmap(sg_page(&sg[0])) + sg[0].offset;
  3425. + q = input;
  3426. hexdump(q, template[i].rlen);
  3427. printk(KERN_INFO "enc/dec: %s\n",
  3428. memcmp(q, template[i].result,
  3429. template[i].rlen) ? "fail" : "pass");
  3430. - kunmap(sg_page(&sg[0]));
  3431. next_one:
  3432. if (!template[i].key)
  3433. kfree(key);
  3434. @@ -360,7 +391,6 @@ next_one:
  3435. }
  3436. printk(KERN_INFO "\ntesting %s %s across pages (chunking)\n", algo, e);
  3437. - memset(xbuf, 0, XBUFSIZE);
  3438. memset(axbuf, 0, XBUFSIZE);
  3439. for (i = 0, j = 0; i < tcount; i++) {
  3440. @@ -388,6 +418,7 @@ next_one:
  3441. goto out;
  3442. }
  3443. + memset(xbuf, 0, XBUFSIZE);
  3444. sg_init_table(sg, template[i].np);
  3445. for (k = 0, temp = 0; k < template[i].np; k++) {
  3446. memcpy(&xbuf[IDX[k]],
  3447. @@ -450,7 +481,7 @@ next_one:
  3448. for (k = 0, temp = 0; k < template[i].np; k++) {
  3449. printk(KERN_INFO "page %u\n", k);
  3450. - q = kmap(sg_page(&sg[k])) + sg[k].offset;
  3451. + q = &axbuf[IDX[k]];
  3452. hexdump(q, template[i].tap[k]);
  3453. printk(KERN_INFO "%s\n",
  3454. memcmp(q, template[i].result + temp,
  3455. @@ -459,8 +490,15 @@ next_one:
  3456. 0 : authsize)) ?
  3457. "fail" : "pass");
  3458. + for (n = 0; q[template[i].tap[k] + n]; n++)
  3459. + ;
  3460. + if (n) {
  3461. + printk("Result buffer corruption %u "
  3462. + "bytes:\n", n);
  3463. + hexdump(&q[template[i].tap[k]], n);
  3464. + }
  3465. +
  3466. temp += template[i].tap[k];
  3467. - kunmap(sg_page(&sg[k]));
  3468. }
  3469. }
  3470. }
  3471. @@ -473,7 +511,7 @@ out:
  3472. static void test_cipher(char *algo, int enc,
  3473. struct cipher_testvec *template, unsigned int tcount)
  3474. {
  3475. - unsigned int ret, i, j, k, temp;
  3476. + unsigned int ret, i, j, k, n, temp;
  3477. char *q;
  3478. struct crypto_ablkcipher *tfm;
  3479. struct ablkcipher_request *req;
  3480. @@ -569,19 +607,17 @@ static void test_cipher(char *algo, int
  3481. goto out;
  3482. }
  3483. - q = kmap(sg_page(&sg[0])) + sg[0].offset;
  3484. + q = data;
  3485. hexdump(q, template[i].rlen);
  3486. printk("%s\n",
  3487. memcmp(q, template[i].result,
  3488. template[i].rlen) ? "fail" : "pass");
  3489. - kunmap(sg_page(&sg[0]));
  3490. }
  3491. kfree(data);
  3492. }
  3493. printk("\ntesting %s %s across pages (chunking)\n", algo, e);
  3494. - memset(xbuf, 0, XBUFSIZE);
  3495. j = 0;
  3496. for (i = 0; i < tcount; i++) {
  3497. @@ -596,6 +632,7 @@ static void test_cipher(char *algo, int
  3498. printk("test %u (%d bit key):\n",
  3499. j, template[i].klen * 8);
  3500. + memset(xbuf, 0, XBUFSIZE);
  3501. crypto_ablkcipher_clear_flags(tfm, ~0);
  3502. if (template[i].wk)
  3503. crypto_ablkcipher_set_flags(
  3504. @@ -649,14 +686,21 @@ static void test_cipher(char *algo, int
  3505. temp = 0;
  3506. for (k = 0; k < template[i].np; k++) {
  3507. printk("page %u\n", k);
  3508. - q = kmap(sg_page(&sg[k])) + sg[k].offset;
  3509. + q = &xbuf[IDX[k]];
  3510. hexdump(q, template[i].tap[k]);
  3511. printk("%s\n",
  3512. memcmp(q, template[i].result + temp,
  3513. template[i].tap[k]) ? "fail" :
  3514. "pass");
  3515. +
  3516. + for (n = 0; q[template[i].tap[k] + n]; n++)
  3517. + ;
  3518. + if (n) {
  3519. + printk("Result buffer corruption %u "
  3520. + "bytes:\n", n);
  3521. + hexdump(&q[template[i].tap[k]], n);
  3522. + }
  3523. temp += template[i].tap[k];
  3524. - kunmap(sg_page(&sg[k]));
  3525. }
  3526. }
  3527. }
  3528. @@ -1172,6 +1216,14 @@ static void do_test(void)
  3529. test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
  3530. DES3_EDE_DEC_TEST_VECTORS);
  3531. + test_cipher("cbc(des3_ede)", ENCRYPT,
  3532. + des3_ede_cbc_enc_tv_template,
  3533. + DES3_EDE_CBC_ENC_TEST_VECTORS);
  3534. +
  3535. + test_cipher("cbc(des3_ede)", DECRYPT,
  3536. + des3_ede_cbc_dec_tv_template,
  3537. + DES3_EDE_CBC_DEC_TEST_VECTORS);
  3538. +
  3539. test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
  3540. test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);
  3541. @@ -1382,6 +1434,14 @@ static void do_test(void)
  3542. DES3_EDE_ENC_TEST_VECTORS);
  3543. test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
  3544. DES3_EDE_DEC_TEST_VECTORS);
  3545. +
  3546. + test_cipher("cbc(des3_ede)", ENCRYPT,
  3547. + des3_ede_cbc_enc_tv_template,
  3548. + DES3_EDE_CBC_ENC_TEST_VECTORS);
  3549. +
  3550. + test_cipher("cbc(des3_ede)", DECRYPT,
  3551. + des3_ede_cbc_dec_tv_template,
  3552. + DES3_EDE_CBC_DEC_TEST_VECTORS);
  3553. break;
  3554. case 5:
  3555. @@ -1550,7 +1610,7 @@ static void do_test(void)
  3556. case 29:
  3557. test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
  3558. break;
  3559. -
  3560. +
  3561. case 30:
  3562. test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
  3563. XETA_ENC_TEST_VECTORS);
  3564. @@ -1615,6 +1675,22 @@ static void do_test(void)
  3565. CTS_MODE_DEC_TEST_VECTORS);
  3566. break;
  3567. + case 39:
  3568. + test_hash("rmd128", rmd128_tv_template, RMD128_TEST_VECTORS);
  3569. + break;
  3570. +
  3571. + case 40:
  3572. + test_hash("rmd160", rmd160_tv_template, RMD160_TEST_VECTORS);
  3573. + break;
  3574. +
  3575. + case 41:
  3576. + test_hash("rmd256", rmd256_tv_template, RMD256_TEST_VECTORS);
  3577. + break;
  3578. +
  3579. + case 42:
  3580. + test_hash("rmd320", rmd320_tv_template, RMD320_TEST_VECTORS);
  3581. + break;
  3582. +
  3583. case 100:
  3584. test_hash("hmac(md5)", hmac_md5_tv_template,
  3585. HMAC_MD5_TEST_VECTORS);
  3586. @@ -1650,6 +1726,16 @@ static void do_test(void)
  3587. XCBC_AES_TEST_VECTORS);
  3588. break;
  3589. + case 107:
  3590. + test_hash("hmac(rmd128)", hmac_rmd128_tv_template,
  3591. + HMAC_RMD128_TEST_VECTORS);
  3592. + break;
  3593. +
  3594. + case 108:
  3595. + test_hash("hmac(rmd160)", hmac_rmd160_tv_template,
  3596. + HMAC_RMD160_TEST_VECTORS);
  3597. + break;
  3598. +
  3599. case 200:
  3600. test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
  3601. speed_template_16_24_32);
  3602. @@ -1788,6 +1874,22 @@ static void do_test(void)
  3603. test_hash_speed("sha224", sec, generic_hash_speed_template);
  3604. if (mode > 300 && mode < 400) break;
  3605. + case 314:
  3606. + test_hash_speed("rmd128", sec, generic_hash_speed_template);
  3607. + if (mode > 300 && mode < 400) break;
  3608. +
  3609. + case 315:
  3610. + test_hash_speed("rmd160", sec, generic_hash_speed_template);
  3611. + if (mode > 300 && mode < 400) break;
  3612. +
  3613. + case 316:
  3614. + test_hash_speed("rmd256", sec, generic_hash_speed_template);
  3615. + if (mode > 300 && mode < 400) break;
  3616. +
  3617. + case 317:
  3618. + test_hash_speed("rmd320", sec, generic_hash_speed_template);
  3619. + if (mode > 300 && mode < 400) break;
  3620. +
  3621. case 399:
  3622. break;
  3623. --- a/crypto/tcrypt.h
  3624. +++ b/crypto/tcrypt.h
  3625. @@ -13,12 +13,6 @@
  3626. * Software Foundation; either version 2 of the License, or (at your option)
  3627. * any later version.
  3628. *
  3629. - * 2007-11-13 Added GCM tests
  3630. - * 2007-11-13 Added AEAD support
  3631. - * 2006-12-07 Added SHA384 HMAC and SHA512 HMAC tests
  3632. - * 2004-08-09 Cipher speed tests by Reyk Floeter <[email protected]>
  3633. - * 2003-09-14 Changes by Kartikey Mahendra Bhatt
  3634. - *
  3635. */
  3636. #ifndef _CRYPTO_TCRYPT_H
  3637. #define _CRYPTO_TCRYPT_H
  3638. @@ -168,6 +162,271 @@ static struct hash_testvec md5_tv_templa
  3639. .digest = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55"
  3640. "\xac\x49\xda\x2e\x21\x07\xb6\x7a",
  3641. }
  3642. +
  3643. +};
  3644. +
  3645. +/*
  3646. + * RIPEMD-128 test vectors from ISO/IEC 10118-3:2004(E)
  3647. + */
  3648. +#define RMD128_TEST_VECTORS 10
  3649. +
  3650. +static struct hash_testvec rmd128_tv_template[] = {
  3651. + {
  3652. + .digest = "\xcd\xf2\x62\x13\xa1\x50\xdc\x3e"
  3653. + "\xcb\x61\x0f\x18\xf6\xb3\x8b\x46",
  3654. + }, {
  3655. + .plaintext = "a",
  3656. + .psize = 1,
  3657. + .digest = "\x86\xbe\x7a\xfa\x33\x9d\x0f\xc7"
  3658. + "\xcf\xc7\x85\xe7\x2f\x57\x8d\x33",
  3659. + }, {
  3660. + .plaintext = "abc",
  3661. + .psize = 3,
  3662. + .digest = "\xc1\x4a\x12\x19\x9c\x66\xe4\xba"
  3663. + "\x84\x63\x6b\x0f\x69\x14\x4c\x77",
  3664. + }, {
  3665. + .plaintext = "message digest",
  3666. + .psize = 14,
  3667. + .digest = "\x9e\x32\x7b\x3d\x6e\x52\x30\x62"
  3668. + "\xaf\xc1\x13\x2d\x7d\xf9\xd1\xb8",
  3669. + }, {
  3670. + .plaintext = "abcdefghijklmnopqrstuvwxyz",
  3671. + .psize = 26,
  3672. + .digest = "\xfd\x2a\xa6\x07\xf7\x1d\xc8\xf5"
  3673. + "\x10\x71\x49\x22\xb3\x71\x83\x4e",
  3674. + }, {
  3675. + .plaintext = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"
  3676. + "fghijklmnopqrstuvwxyz0123456789",
  3677. + .psize = 62,
  3678. + .digest = "\xd1\xe9\x59\xeb\x17\x9c\x91\x1f"
  3679. + "\xae\xa4\x62\x4c\x60\xc5\xc7\x02",
  3680. + }, {
  3681. + .plaintext = "1234567890123456789012345678901234567890"
  3682. + "1234567890123456789012345678901234567890",
  3683. + .psize = 80,
  3684. + .digest = "\x3f\x45\xef\x19\x47\x32\xc2\xdb"
  3685. + "\xb2\xc4\xa2\xc7\x69\x79\x5f\xa3",
  3686. + }, {
  3687. + .plaintext = "abcdbcdecdefdefgefghfghighij"
  3688. + "hijkijkljklmklmnlmnomnopnopq",
  3689. + .psize = 56,
  3690. + .digest = "\xa1\xaa\x06\x89\xd0\xfa\xfa\x2d"
  3691. + "\xdc\x22\xe8\x8b\x49\x13\x3a\x06",
  3692. + .np = 2,
  3693. + .tap = { 28, 28 },
  3694. + }, {
  3695. + .plaintext = "abcdefghbcdefghicdefghijdefghijkefghijklfghi"
  3696. + "jklmghijklmnhijklmnoijklmnopjklmnopqklmnopqr"
  3697. + "lmnopqrsmnopqrstnopqrstu",
  3698. + .psize = 112,
  3699. + .digest = "\xd4\xec\xc9\x13\xe1\xdf\x77\x6b"
  3700. + "\xf4\x8d\xe9\xd5\x5b\x1f\x25\x46",
  3701. + }, {
  3702. + .plaintext = "abcdbcdecdefdefgefghfghighijhijk",
  3703. + .psize = 32,
  3704. + .digest = "\x13\xfc\x13\xe8\xef\xff\x34\x7d"
  3705. + "\xe1\x93\xff\x46\xdb\xac\xcf\xd4",
  3706. + }
  3707. +};
  3708. +
  3709. +/*
  3710. + * RIPEMD-160 test vectors from ISO/IEC 10118-3:2004(E)
  3711. + */
  3712. +#define RMD160_TEST_VECTORS 10
  3713. +
  3714. +static struct hash_testvec rmd160_tv_template[] = {
  3715. + {
  3716. + .digest = "\x9c\x11\x85\xa5\xc5\xe9\xfc\x54\x61\x28"
  3717. + "\x08\x97\x7e\xe8\xf5\x48\xb2\x25\x8d\x31",
  3718. + }, {
  3719. + .plaintext = "a",
  3720. + .psize = 1,
  3721. + .digest = "\x0b\xdc\x9d\x2d\x25\x6b\x3e\xe9\xda\xae"
  3722. + "\x34\x7b\xe6\xf4\xdc\x83\x5a\x46\x7f\xfe",
  3723. + }, {
  3724. + .plaintext = "abc",
  3725. + .psize = 3,
  3726. + .digest = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04"
  3727. + "\x4a\x8e\x98\xc6\xb0\x87\xf1\x5a\x0b\xfc",
  3728. + }, {
  3729. + .plaintext = "message digest",
  3730. + .psize = 14,
  3731. + .digest = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8"
  3732. + "\x81\xb1\x23\xa8\x5f\xfa\x21\x59\x5f\x36",
  3733. + }, {
  3734. + .plaintext = "abcdefghijklmnopqrstuvwxyz",
  3735. + .psize = 26,
  3736. + .digest = "\xf7\x1c\x27\x10\x9c\x69\x2c\x1b\x56\xbb"
  3737. + "\xdc\xeb\x5b\x9d\x28\x65\xb3\x70\x8d\xbc",
  3738. + }, {
  3739. + .plaintext = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"
  3740. + "fghijklmnopqrstuvwxyz0123456789",
  3741. + .psize = 62,
  3742. + .digest = "\xb0\xe2\x0b\x6e\x31\x16\x64\x02\x86\xed"
  3743. + "\x3a\x87\xa5\x71\x30\x79\xb2\x1f\x51\x89",
  3744. + }, {
  3745. + .plaintext = "1234567890123456789012345678901234567890"
  3746. + "1234567890123456789012345678901234567890",
  3747. + .psize = 80,
  3748. + .digest = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb"
  3749. + "\xd3\x32\x3c\xab\x82\xbf\x63\x32\x6b\xfb",
  3750. + }, {
  3751. + .plaintext = "abcdbcdecdefdefgefghfghighij"
  3752. + "hijkijkljklmklmnlmnomnopnopq",
  3753. + .psize = 56,
  3754. + .digest = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05"
  3755. + "\xa0\x6c\x27\xdc\xf4\x9a\xda\x62\xeb\x2b",
  3756. + .np = 2,
  3757. + .tap = { 28, 28 },
  3758. + }, {
  3759. + .plaintext = "abcdefghbcdefghicdefghijdefghijkefghijklfghi"
  3760. + "jklmghijklmnhijklmnoijklmnopjklmnopqklmnopqr"
  3761. + "lmnopqrsmnopqrstnopqrstu",
  3762. + .psize = 112,
  3763. + .digest = "\x6f\x3f\xa3\x9b\x6b\x50\x3c\x38\x4f\x91"
  3764. + "\x9a\x49\xa7\xaa\x5c\x2c\x08\xbd\xfb\x45",
  3765. + }, {
  3766. + .plaintext = "abcdbcdecdefdefgefghfghighijhijk",
  3767. + .psize = 32,
  3768. + .digest = "\x94\xc2\x64\x11\x54\x04\xe6\x33\x79\x0d"
  3769. + "\xfc\xc8\x7b\x58\x7d\x36\x77\x06\x7d\x9f",
  3770. + }
  3771. +};
  3772. +
  3773. +/*
  3774. + * RIPEMD-256 test vectors
  3775. + */
  3776. +#define RMD256_TEST_VECTORS 8
  3777. +
  3778. +static struct hash_testvec rmd256_tv_template[] = {
  3779. + {
  3780. + .digest = "\x02\xba\x4c\x4e\x5f\x8e\xcd\x18"
  3781. + "\x77\xfc\x52\xd6\x4d\x30\xe3\x7a"
  3782. + "\x2d\x97\x74\xfb\x1e\x5d\x02\x63"
  3783. + "\x80\xae\x01\x68\xe3\xc5\x52\x2d",
  3784. + }, {
  3785. + .plaintext = "a",
  3786. + .psize = 1,
  3787. + .digest = "\xf9\x33\x3e\x45\xd8\x57\xf5\xd9"
  3788. + "\x0a\x91\xba\xb7\x0a\x1e\xba\x0c"
  3789. + "\xfb\x1b\xe4\xb0\x78\x3c\x9a\xcf"
  3790. + "\xcd\x88\x3a\x91\x34\x69\x29\x25",
  3791. + }, {
  3792. + .plaintext = "abc",
  3793. + .psize = 3,
  3794. + .digest = "\xaf\xbd\x6e\x22\x8b\x9d\x8c\xbb"
  3795. + "\xce\xf5\xca\x2d\x03\xe6\xdb\xa1"
  3796. + "\x0a\xc0\xbc\x7d\xcb\xe4\x68\x0e"
  3797. + "\x1e\x42\xd2\xe9\x75\x45\x9b\x65",
  3798. + }, {
  3799. + .plaintext = "message digest",
  3800. + .psize = 14,
  3801. + .digest = "\x87\xe9\x71\x75\x9a\x1c\xe4\x7a"
  3802. + "\x51\x4d\x5c\x91\x4c\x39\x2c\x90"
  3803. + "\x18\xc7\xc4\x6b\xc1\x44\x65\x55"
  3804. + "\x4a\xfc\xdf\x54\xa5\x07\x0c\x0e",
  3805. + }, {
  3806. + .plaintext = "abcdefghijklmnopqrstuvwxyz",
  3807. + .psize = 26,
  3808. + .digest = "\x64\x9d\x30\x34\x75\x1e\xa2\x16"
  3809. + "\x77\x6b\xf9\xa1\x8a\xcc\x81\xbc"
  3810. + "\x78\x96\x11\x8a\x51\x97\x96\x87"
  3811. + "\x82\xdd\x1f\xd9\x7d\x8d\x51\x33",
  3812. + }, {
  3813. + .plaintext = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"
  3814. + "fghijklmnopqrstuvwxyz0123456789",
  3815. + .psize = 62,
  3816. + .digest = "\x57\x40\xa4\x08\xac\x16\xb7\x20"
  3817. + "\xb8\x44\x24\xae\x93\x1c\xbb\x1f"
  3818. + "\xe3\x63\xd1\xd0\xbf\x40\x17\xf1"
  3819. + "\xa8\x9f\x7e\xa6\xde\x77\xa0\xb8",
  3820. + }, {
  3821. + .plaintext = "1234567890123456789012345678901234567890"
  3822. + "1234567890123456789012345678901234567890",
  3823. + .psize = 80,
  3824. + .digest = "\x06\xfd\xcc\x7a\x40\x95\x48\xaa"
  3825. + "\xf9\x13\x68\xc0\x6a\x62\x75\xb5"
  3826. + "\x53\xe3\xf0\x99\xbf\x0e\xa4\xed"
  3827. + "\xfd\x67\x78\xdf\x89\xa8\x90\xdd",
  3828. + }, {
  3829. + .plaintext = "abcdbcdecdefdefgefghfghighij"
  3830. + "hijkijkljklmklmnlmnomnopnopq",
  3831. + .psize = 56,
  3832. + .digest = "\x38\x43\x04\x55\x83\xaa\xc6\xc8"
  3833. + "\xc8\xd9\x12\x85\x73\xe7\xa9\x80"
  3834. + "\x9a\xfb\x2a\x0f\x34\xcc\xc3\x6e"
  3835. + "\xa9\xe7\x2f\x16\xf6\x36\x8e\x3f",
  3836. + .np = 2,
  3837. + .tap = { 28, 28 },
  3838. + }
  3839. +};
  3840. +
  3841. +/*
  3842. + * RIPEMD-320 test vectors
  3843. + */
  3844. +#define RMD320_TEST_VECTORS 8
  3845. +
  3846. +static struct hash_testvec rmd320_tv_template[] = {
  3847. + {
  3848. + .digest = "\x22\xd6\x5d\x56\x61\x53\x6c\xdc\x75\xc1"
  3849. + "\xfd\xf5\xc6\xde\x7b\x41\xb9\xf2\x73\x25"
  3850. + "\xeb\xc6\x1e\x85\x57\x17\x7d\x70\x5a\x0e"
  3851. + "\xc8\x80\x15\x1c\x3a\x32\xa0\x08\x99\xb8",
  3852. + }, {
  3853. + .plaintext = "a",
  3854. + .psize = 1,
  3855. + .digest = "\xce\x78\x85\x06\x38\xf9\x26\x58\xa5\xa5"
  3856. + "\x85\x09\x75\x79\x92\x6d\xda\x66\x7a\x57"
  3857. + "\x16\x56\x2c\xfc\xf6\xfb\xe7\x7f\x63\x54"
  3858. + "\x2f\x99\xb0\x47\x05\xd6\x97\x0d\xff\x5d",
  3859. + }, {
  3860. + .plaintext = "abc",
  3861. + .psize = 3,
  3862. + .digest = "\xde\x4c\x01\xb3\x05\x4f\x89\x30\xa7\x9d"
  3863. + "\x09\xae\x73\x8e\x92\x30\x1e\x5a\x17\x08"
  3864. + "\x5b\xef\xfd\xc1\xb8\xd1\x16\x71\x3e\x74"
  3865. + "\xf8\x2f\xa9\x42\xd6\x4c\xdb\xc4\x68\x2d",
  3866. + }, {
  3867. + .plaintext = "message digest",
  3868. + .psize = 14,
  3869. + .digest = "\x3a\x8e\x28\x50\x2e\xd4\x5d\x42\x2f\x68"
  3870. + "\x84\x4f\x9d\xd3\x16\xe7\xb9\x85\x33\xfa"
  3871. + "\x3f\x2a\x91\xd2\x9f\x84\xd4\x25\xc8\x8d"
  3872. + "\x6b\x4e\xff\x72\x7d\xf6\x6a\x7c\x01\x97",
  3873. + }, {
  3874. + .plaintext = "abcdefghijklmnopqrstuvwxyz",
  3875. + .psize = 26,
  3876. + .digest = "\xca\xbd\xb1\x81\x0b\x92\x47\x0a\x20\x93"
  3877. + "\xaa\x6b\xce\x05\x95\x2c\x28\x34\x8c\xf4"
  3878. + "\x3f\xf6\x08\x41\x97\x51\x66\xbb\x40\xed"
  3879. + "\x23\x40\x04\xb8\x82\x44\x63\xe6\xb0\x09",
  3880. + }, {
  3881. + .plaintext = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"
  3882. + "fghijklmnopqrstuvwxyz0123456789",
  3883. + .psize = 62,
  3884. + .digest = "\xed\x54\x49\x40\xc8\x6d\x67\xf2\x50\xd2"
  3885. + "\x32\xc3\x0b\x7b\x3e\x57\x70\xe0\xc6\x0c"
  3886. + "\x8c\xb9\xa4\xca\xfe\x3b\x11\x38\x8a\xf9"
  3887. + "\x92\x0e\x1b\x99\x23\x0b\x84\x3c\x86\xa4",
  3888. + }, {
  3889. + .plaintext = "1234567890123456789012345678901234567890"
  3890. + "1234567890123456789012345678901234567890",
  3891. + .psize = 80,
  3892. + .digest = "\x55\x78\x88\xaf\x5f\x6d\x8e\xd6\x2a\xb6"
  3893. + "\x69\x45\xc6\xd2\xa0\xa4\x7e\xcd\x53\x41"
  3894. + "\xe9\x15\xeb\x8f\xea\x1d\x05\x24\x95\x5f"
  3895. + "\x82\x5d\xc7\x17\xe4\xa0\x08\xab\x2d\x42",
  3896. + }, {
  3897. + .plaintext = "abcdbcdecdefdefgefghfghighij"
  3898. + "hijkijkljklmklmnlmnomnopnopq",
  3899. + .psize = 56,
  3900. + .digest = "\xd0\x34\xa7\x95\x0c\xf7\x22\x02\x1b\xa4"
  3901. + "\xb8\x4d\xf7\x69\xa5\xde\x20\x60\xe2\x59"
  3902. + "\xdf\x4c\x9b\xb4\xa4\x26\x8c\x0e\x93\x5b"
  3903. + "\xbc\x74\x70\xa9\x69\xc9\xd0\x72\xa1\xac",
  3904. + .np = 2,
  3905. + .tap = { 28, 28 },
  3906. + }
  3907. };
  3908. /*
  3909. @@ -817,6 +1076,168 @@ static struct hash_testvec hmac_md5_tv_t
  3910. };
  3911. /*
  3912. + * HMAC-RIPEMD128 test vectors from RFC2286
  3913. + */
  3914. +#define HMAC_RMD128_TEST_VECTORS 7
  3915. +
  3916. +static struct hash_testvec hmac_rmd128_tv_template[] = {
  3917. + {
  3918. + .key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  3919. + .ksize = 16,
  3920. + .plaintext = "Hi There",
  3921. + .psize = 8,
  3922. + .digest = "\xfb\xf6\x1f\x94\x92\xaa\x4b\xbf"
  3923. + "\x81\xc1\x72\xe8\x4e\x07\x34\xdb",
  3924. + }, {
  3925. + .key = "Jefe",
  3926. + .ksize = 4,
  3927. + .plaintext = "what do ya want for nothing?",
  3928. + .psize = 28,
  3929. + .digest = "\x87\x5f\x82\x88\x62\xb6\xb3\x34"
  3930. + "\xb4\x27\xc5\x5f\x9f\x7f\xf0\x9b",
  3931. + .np = 2,
  3932. + .tap = { 14, 14 },
  3933. + }, {
  3934. + .key = "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  3935. + .ksize = 16,
  3936. + .plaintext = "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3937. + "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3938. + "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  3939. + "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  3940. + .psize = 50,
  3941. + .digest = "\x09\xf0\xb2\x84\x6d\x2f\x54\x3d"
  3942. + "\xa3\x63\xcb\xec\x8d\x62\xa3\x8d",
  3943. + }, {
  3944. + .key = "\x01\x02\x03\x04\x05\x06\x07\x08"
  3945. + "\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
  3946. + "\x11\x12\x13\x14\x15\x16\x17\x18\x19",
  3947. + .ksize = 25,
  3948. + .plaintext = "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
  3949. + "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
  3950. + "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
  3951. + "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd",
  3952. + .psize = 50,
  3953. + .digest = "\xbd\xbb\xd7\xcf\x03\xe4\x4b\x5a"
  3954. + "\xa6\x0a\xf8\x15\xbe\x4d\x22\x94",
  3955. + }, {
  3956. + .key = "\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c",
  3957. + .ksize = 16,
  3958. + .plaintext = "Test With Truncation",
  3959. + .psize = 20,
  3960. + .digest = "\xe7\x98\x08\xf2\x4b\x25\xfd\x03"
  3961. + "\x1c\x15\x5f\x0d\x55\x1d\x9a\x3a",
  3962. + }, {
  3963. + .key = "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  3964. + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  3965. + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  3966. + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  3967. + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  3968. + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  3969. + "\xaa\xaa",
  3970. + .ksize = 80,
  3971. + .plaintext = "Test Using Larger Than Block-Size Key - Hash Key First",
  3972. + .psize = 54,
  3973. + .digest = "\xdc\x73\x29\x28\xde\x98\x10\x4a"
  3974. + "\x1f\x59\xd3\x73\xc1\x50\xac\xbb",
  3975. + }, {
  3976. + .key = "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  3977. + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  3978. + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  3979. + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  3980. + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  3981. + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  3982. + "\xaa\xaa",
  3983. + .ksize = 80,
  3984. + .plaintext = "Test Using Larger Than Block-Size Key and Larger Than One "
  3985. + "Block-Size Data",
  3986. + .psize = 73,
  3987. + .digest = "\x5c\x6b\xec\x96\x79\x3e\x16\xd4"
  3988. + "\x06\x90\xc2\x37\x63\x5f\x30\xc5",
  3989. + },
  3990. +};
  3991. +
  3992. +/*
  3993. + * HMAC-RIPEMD160 test vectors from RFC2286
  3994. + */
  3995. +#define HMAC_RMD160_TEST_VECTORS 7
  3996. +
  3997. +static struct hash_testvec hmac_rmd160_tv_template[] = {
  3998. + {
  3999. + .key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  4000. + .ksize = 20,
  4001. + .plaintext = "Hi There",
  4002. + .psize = 8,
  4003. + .digest = "\x24\xcb\x4b\xd6\x7d\x20\xfc\x1a\x5d\x2e"
  4004. + "\xd7\x73\x2d\xcc\x39\x37\x7f\x0a\x56\x68",
  4005. + }, {
  4006. + .key = "Jefe",
  4007. + .ksize = 4,
  4008. + .plaintext = "what do ya want for nothing?",
  4009. + .psize = 28,
  4010. + .digest = "\xdd\xa6\xc0\x21\x3a\x48\x5a\x9e\x24\xf4"
  4011. + "\x74\x20\x64\xa7\xf0\x33\xb4\x3c\x40\x69",
  4012. + .np = 2,
  4013. + .tap = { 14, 14 },
  4014. + }, {
  4015. + .key = "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  4016. + .ksize = 20,
  4017. + .plaintext = "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4018. + "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4019. + "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4020. + "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  4021. + .psize = 50,
  4022. + .digest = "\xb0\xb1\x05\x36\x0d\xe7\x59\x96\x0a\xb4"
  4023. + "\xf3\x52\x98\xe1\x16\xe2\x95\xd8\xe7\xc1",
  4024. + }, {
  4025. + .key = "\x01\x02\x03\x04\x05\x06\x07\x08"
  4026. + "\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
  4027. + "\x11\x12\x13\x14\x15\x16\x17\x18\x19",
  4028. + .ksize = 25,
  4029. + .plaintext = "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
  4030. + "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
  4031. + "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
  4032. + "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd",
  4033. + .psize = 50,
  4034. + .digest = "\xd5\xca\x86\x2f\x4d\x21\xd5\xe6\x10\xe1"
  4035. + "\x8b\x4c\xf1\xbe\xb9\x7a\x43\x65\xec\xf4",
  4036. + }, {
  4037. + .key = "\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c",
  4038. + .ksize = 20,
  4039. + .plaintext = "Test With Truncation",
  4040. + .psize = 20,
  4041. + .digest = "\x76\x19\x69\x39\x78\xf9\x1d\x90\x53\x9a"
  4042. + "\xe7\x86\x50\x0f\xf3\xd8\xe0\x51\x8e\x39",
  4043. + }, {
  4044. + .key = "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  4045. + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  4046. + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  4047. + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  4048. + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  4049. + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  4050. + "\xaa\xaa",
  4051. + .ksize = 80,
  4052. + .plaintext = "Test Using Larger Than Block-Size Key - Hash Key First",
  4053. + .psize = 54,
  4054. + .digest = "\x64\x66\xca\x07\xac\x5e\xac\x29\xe1\xbd"
  4055. + "\x52\x3e\x5a\xda\x76\x05\xb7\x91\xfd\x8b",
  4056. + }, {
  4057. + .key = "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  4058. + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  4059. + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  4060. + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  4061. + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  4062. + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  4063. + "\xaa\xaa",
  4064. + .ksize = 80,
  4065. + .plaintext = "Test Using Larger Than Block-Size Key and Larger Than One "
  4066. + "Block-Size Data",
  4067. + .psize = 73,
  4068. + .digest = "\x69\xea\x60\x79\x8d\x71\x61\x6c\xce\x5f"
  4069. + "\xd0\x87\x1e\x23\x75\x4c\xd7\x5d\x5a\x0a",
  4070. + },
  4071. +};
  4072. +
  4073. +/*
  4074. * HMAC-SHA1 test vectors from RFC2202
  4075. */
  4076. #define HMAC_SHA1_TEST_VECTORS 7
  4077. @@ -1442,6 +1863,8 @@ static struct hash_testvec hmac_sha512_t
  4078. #define DES_CBC_DEC_TEST_VECTORS 4
  4079. #define DES3_EDE_ENC_TEST_VECTORS 3
  4080. #define DES3_EDE_DEC_TEST_VECTORS 3
  4081. +#define DES3_EDE_CBC_ENC_TEST_VECTORS 1
  4082. +#define DES3_EDE_CBC_DEC_TEST_VECTORS 1
  4083. static struct cipher_testvec des_enc_tv_template[] = {
  4084. { /* From Applied Cryptography */
  4085. @@ -1680,9 +2103,6 @@ static struct cipher_testvec des_cbc_dec
  4086. },
  4087. };
  4088. -/*
  4089. - * We really need some more test vectors, especially for DES3 CBC.
  4090. - */
  4091. static struct cipher_testvec des3_ede_enc_tv_template[] = {
  4092. { /* These are from openssl */
  4093. .key = "\x01\x23\x45\x67\x89\xab\xcd\xef"
  4094. @@ -1745,6 +2165,94 @@ static struct cipher_testvec des3_ede_de
  4095. },
  4096. };
  4097. +static struct cipher_testvec des3_ede_cbc_enc_tv_template[] = {
  4098. + { /* Generated from openssl */
  4099. + .key = "\xE9\xC0\xFF\x2E\x76\x0B\x64\x24"
  4100. + "\x44\x4D\x99\x5A\x12\xD6\x40\xC0"
  4101. + "\xEA\xC2\x84\xE8\x14\x95\xDB\xE8",
  4102. + .klen = 24,
  4103. + .iv = "\x7D\x33\x88\x93\x0F\x93\xB2\x42",
  4104. + .input = "\x6f\x54\x20\x6f\x61\x4d\x79\x6e"
  4105. + "\x53\x20\x63\x65\x65\x72\x73\x74"
  4106. + "\x54\x20\x6f\x6f\x4d\x20\x6e\x61"
  4107. + "\x20\x79\x65\x53\x72\x63\x74\x65"
  4108. + "\x20\x73\x6f\x54\x20\x6f\x61\x4d"
  4109. + "\x79\x6e\x53\x20\x63\x65\x65\x72"
  4110. + "\x73\x74\x54\x20\x6f\x6f\x4d\x20"
  4111. + "\x6e\x61\x20\x79\x65\x53\x72\x63"
  4112. + "\x74\x65\x20\x73\x6f\x54\x20\x6f"
  4113. + "\x61\x4d\x79\x6e\x53\x20\x63\x65"
  4114. + "\x65\x72\x73\x74\x54\x20\x6f\x6f"
  4115. + "\x4d\x20\x6e\x61\x20\x79\x65\x53"
  4116. + "\x72\x63\x74\x65\x20\x73\x6f\x54"
  4117. + "\x20\x6f\x61\x4d\x79\x6e\x53\x20"
  4118. + "\x63\x65\x65\x72\x73\x74\x54\x20"
  4119. + "\x6f\x6f\x4d\x20\x6e\x61\x0a\x79",
  4120. + .ilen = 128,
  4121. + .result = "\x0e\x2d\xb6\x97\x3c\x56\x33\xf4"
  4122. + "\x67\x17\x21\xc7\x6e\x8a\xd5\x49"
  4123. + "\x74\xb3\x49\x05\xc5\x1c\xd0\xed"
  4124. + "\x12\x56\x5c\x53\x96\xb6\x00\x7d"
  4125. + "\x90\x48\xfc\xf5\x8d\x29\x39\xcc"
  4126. + "\x8a\xd5\x35\x18\x36\x23\x4e\xd7"
  4127. + "\x76\xd1\xda\x0c\x94\x67\xbb\x04"
  4128. + "\x8b\xf2\x03\x6c\xa8\xcf\xb6\xea"
  4129. + "\x22\x64\x47\xaa\x8f\x75\x13\xbf"
  4130. + "\x9f\xc2\xc3\xf0\xc9\x56\xc5\x7a"
  4131. + "\x71\x63\x2e\x89\x7b\x1e\x12\xca"
  4132. + "\xe2\x5f\xaf\xd8\xa4\xf8\xc9\x7a"
  4133. + "\xd6\xf9\x21\x31\x62\x44\x45\xa6"
  4134. + "\xd6\xbc\x5a\xd3\x2d\x54\x43\xcc"
  4135. + "\x9d\xde\xa5\x70\xe9\x42\x45\x8a"
  4136. + "\x6b\xfa\xb1\x91\x13\xb0\xd9\x19",
  4137. + .rlen = 128,
  4138. + },
  4139. +};
  4140. +
  4141. +static struct cipher_testvec des3_ede_cbc_dec_tv_template[] = {
  4142. + { /* Generated from openssl */
  4143. + .key = "\xE9\xC0\xFF\x2E\x76\x0B\x64\x24"
  4144. + "\x44\x4D\x99\x5A\x12\xD6\x40\xC0"
  4145. + "\xEA\xC2\x84\xE8\x14\x95\xDB\xE8",
  4146. + .klen = 24,
  4147. + .iv = "\x7D\x33\x88\x93\x0F\x93\xB2\x42",
  4148. + .input = "\x0e\x2d\xb6\x97\x3c\x56\x33\xf4"
  4149. + "\x67\x17\x21\xc7\x6e\x8a\xd5\x49"
  4150. + "\x74\xb3\x49\x05\xc5\x1c\xd0\xed"
  4151. + "\x12\x56\x5c\x53\x96\xb6\x00\x7d"
  4152. + "\x90\x48\xfc\xf5\x8d\x29\x39\xcc"
  4153. + "\x8a\xd5\x35\x18\x36\x23\x4e\xd7"
  4154. + "\x76\xd1\xda\x0c\x94\x67\xbb\x04"
  4155. + "\x8b\xf2\x03\x6c\xa8\xcf\xb6\xea"
  4156. + "\x22\x64\x47\xaa\x8f\x75\x13\xbf"
  4157. + "\x9f\xc2\xc3\xf0\xc9\x56\xc5\x7a"
  4158. + "\x71\x63\x2e\x89\x7b\x1e\x12\xca"
  4159. + "\xe2\x5f\xaf\xd8\xa4\xf8\xc9\x7a"
  4160. + "\xd6\xf9\x21\x31\x62\x44\x45\xa6"
  4161. + "\xd6\xbc\x5a\xd3\x2d\x54\x43\xcc"
  4162. + "\x9d\xde\xa5\x70\xe9\x42\x45\x8a"
  4163. + "\x6b\xfa\xb1\x91\x13\xb0\xd9\x19",
  4164. + .ilen = 128,
  4165. + .result = "\x6f\x54\x20\x6f\x61\x4d\x79\x6e"
  4166. + "\x53\x20\x63\x65\x65\x72\x73\x74"
  4167. + "\x54\x20\x6f\x6f\x4d\x20\x6e\x61"
  4168. + "\x20\x79\x65\x53\x72\x63\x74\x65"
  4169. + "\x20\x73\x6f\x54\x20\x6f\x61\x4d"
  4170. + "\x79\x6e\x53\x20\x63\x65\x65\x72"
  4171. + "\x73\x74\x54\x20\x6f\x6f\x4d\x20"
  4172. + "\x6e\x61\x20\x79\x65\x53\x72\x63"
  4173. + "\x74\x65\x20\x73\x6f\x54\x20\x6f"
  4174. + "\x61\x4d\x79\x6e\x53\x20\x63\x65"
  4175. + "\x65\x72\x73\x74\x54\x20\x6f\x6f"
  4176. + "\x4d\x20\x6e\x61\x20\x79\x65\x53"
  4177. + "\x72\x63\x74\x65\x20\x73\x6f\x54"
  4178. + "\x20\x6f\x61\x4d\x79\x6e\x53\x20"
  4179. + "\x63\x65\x65\x72\x73\x74\x54\x20"
  4180. + "\x6f\x6f\x4d\x20\x6e\x61\x0a\x79",
  4181. + .rlen = 128,
  4182. + },
  4183. +};
  4184. +
  4185. /*
  4186. * Blowfish test vectors.
  4187. */
  4188. --- a/drivers/crypto/Kconfig
  4189. +++ b/drivers/crypto/Kconfig
  4190. @@ -174,4 +174,30 @@ config CRYPTO_DEV_HIFN_795X_RNG
  4191. Select this option if you want to enable the random number generator
  4192. on the HIFN 795x crypto adapters.
  4193. +config CRYPTO_DEV_TALITOS
  4194. + tristate "Talitos Freescale Security Engine (SEC)"
  4195. + select CRYPTO_ALGAPI
  4196. + select CRYPTO_AUTHENC
  4197. + select HW_RANDOM
  4198. + depends on FSL_SOC
  4199. + help
  4200. + Say 'Y' here to use the Freescale Security Engine (SEC)
  4201. + to offload cryptographic algorithm computation.
  4202. +
  4203. + The Freescale SEC is present on PowerQUICC 'E' processors, such
  4204. + as the MPC8349E and MPC8548E.
  4205. +
  4206. + To compile this driver as a module, choose M here: the module
  4207. + will be called talitos.
  4208. +
  4209. +config CRYPTO_DEV_IXP4XX
  4210. + tristate "Driver for IXP4xx crypto hardware acceleration"
  4211. + depends on ARCH_IXP4XX
  4212. + select CRYPTO_DES
  4213. + select CRYPTO_ALGAPI
  4214. + select CRYPTO_AUTHENC
  4215. + select CRYPTO_BLKCIPHER
  4216. + help
  4217. + Driver for the IXP4xx NPE crypto engine.
  4218. +
  4219. endif # CRYPTO_HW
  4220. --- a/drivers/crypto/Makefile
  4221. +++ b/drivers/crypto/Makefile
  4222. @@ -2,3 +2,5 @@ obj-$(CONFIG_CRYPTO_DEV_PADLOCK_AES) +=
  4223. obj-$(CONFIG_CRYPTO_DEV_PADLOCK_SHA) += padlock-sha.o
  4224. obj-$(CONFIG_CRYPTO_DEV_GEODE) += geode-aes.o
  4225. obj-$(CONFIG_CRYPTO_DEV_HIFN_795X) += hifn_795x.o
  4226. +obj-$(CONFIG_CRYPTO_DEV_TALITOS) += talitos.o
  4227. +obj-$(CONFIG_CRYPTO_DEV_IXP4XX) += ixp4xx_crypto.o
  4228. --- a/drivers/crypto/hifn_795x.c
  4229. +++ b/drivers/crypto/hifn_795x.c
  4230. @@ -29,7 +29,6 @@
  4231. #include <linux/dma-mapping.h>
  4232. #include <linux/scatterlist.h>
  4233. #include <linux/highmem.h>
  4234. -#include <linux/interrupt.h>
  4235. #include <linux/crypto.h>
  4236. #include <linux/hw_random.h>
  4237. #include <linux/ktime.h>
  4238. @@ -369,7 +368,9 @@ static atomic_t hifn_dev_number;
  4239. #define HIFN_D_DST_RSIZE 80*4
  4240. #define HIFN_D_RES_RSIZE 24*4
  4241. -#define HIFN_QUEUE_LENGTH HIFN_D_CMD_RSIZE-5
  4242. +#define HIFN_D_DST_DALIGN 4
  4243. +
  4244. +#define HIFN_QUEUE_LENGTH HIFN_D_CMD_RSIZE-1
  4245. #define AES_MIN_KEY_SIZE 16
  4246. #define AES_MAX_KEY_SIZE 32
  4247. @@ -535,10 +536,10 @@ struct hifn_crypt_command
  4248. */
  4249. struct hifn_mac_command
  4250. {
  4251. - volatile u16 masks;
  4252. - volatile u16 header_skip;
  4253. - volatile u16 source_count;
  4254. - volatile u16 reserved;
  4255. + volatile __le16 masks;
  4256. + volatile __le16 header_skip;
  4257. + volatile __le16 source_count;
  4258. + volatile __le16 reserved;
  4259. };
  4260. #define HIFN_MAC_CMD_ALG_MASK 0x0001
  4261. @@ -564,10 +565,10 @@ struct hifn_mac_command
  4262. struct hifn_comp_command
  4263. {
  4264. - volatile u16 masks;
  4265. - volatile u16 header_skip;
  4266. - volatile u16 source_count;
  4267. - volatile u16 reserved;
  4268. + volatile __le16 masks;
  4269. + volatile __le16 header_skip;
  4270. + volatile __le16 source_count;
  4271. + volatile __le16 reserved;
  4272. };
  4273. #define HIFN_COMP_CMD_SRCLEN_M 0xc000
  4274. @@ -583,10 +584,10 @@ struct hifn_comp_command
  4275. struct hifn_base_result
  4276. {
  4277. - volatile u16 flags;
  4278. - volatile u16 session;
  4279. - volatile u16 src_cnt; /* 15:0 of source count */
  4280. - volatile u16 dst_cnt; /* 15:0 of dest count */
  4281. + volatile __le16 flags;
  4282. + volatile __le16 session;
  4283. + volatile __le16 src_cnt; /* 15:0 of source count */
  4284. + volatile __le16 dst_cnt; /* 15:0 of dest count */
  4285. };
  4286. #define HIFN_BASE_RES_DSTOVERRUN 0x0200 /* destination overrun */
  4287. @@ -597,8 +598,8 @@ struct hifn_base_result
  4288. struct hifn_comp_result
  4289. {
  4290. - volatile u16 flags;
  4291. - volatile u16 crc;
  4292. + volatile __le16 flags;
  4293. + volatile __le16 crc;
  4294. };
  4295. #define HIFN_COMP_RES_LCB_M 0xff00 /* longitudinal check byte */
  4296. @@ -609,8 +610,8 @@ struct hifn_comp_result
  4297. struct hifn_mac_result
  4298. {
  4299. - volatile u16 flags;
  4300. - volatile u16 reserved;
  4301. + volatile __le16 flags;
  4302. + volatile __le16 reserved;
  4303. /* followed by 0, 6, 8, or 10 u16's of the MAC, then crypt */
  4304. };
  4305. @@ -619,8 +620,8 @@ struct hifn_mac_result
  4306. struct hifn_crypt_result
  4307. {
  4308. - volatile u16 flags;
  4309. - volatile u16 reserved;
  4310. + volatile __le16 flags;
  4311. + volatile __le16 reserved;
  4312. };
  4313. #define HIFN_CRYPT_RES_SRC_NOTZERO 0x0001 /* source expired */
  4314. @@ -686,12 +687,12 @@ static inline u32 hifn_read_1(struct hif
  4315. static inline void hifn_write_0(struct hifn_device *dev, u32 reg, u32 val)
  4316. {
  4317. - writel(val, dev->bar[0] + reg);
  4318. + writel((__force u32)cpu_to_le32(val), dev->bar[0] + reg);
  4319. }
  4320. static inline void hifn_write_1(struct hifn_device *dev, u32 reg, u32 val)
  4321. {
  4322. - writel(val, dev->bar[1] + reg);
  4323. + writel((__force u32)cpu_to_le32(val), dev->bar[1] + reg);
  4324. }
  4325. static void hifn_wait_puc(struct hifn_device *dev)
  4326. @@ -894,7 +895,7 @@ static int hifn_enable_crypto(struct hif
  4327. char *offtbl = NULL;
  4328. int i;
  4329. - for (i = 0; i < sizeof(pci2id)/sizeof(pci2id[0]); i++) {
  4330. + for (i = 0; i < ARRAY_SIZE(pci2id); i++) {
  4331. if (pci2id[i].pci_vendor == dev->pdev->vendor &&
  4332. pci2id[i].pci_prod == dev->pdev->device) {
  4333. offtbl = pci2id[i].card_id;
  4334. @@ -1037,14 +1038,14 @@ static void hifn_init_registers(struct h
  4335. hifn_write_0(dev, HIFN_0_PUIER, HIFN_PUIER_DSTOVER);
  4336. /* write all 4 ring address registers */
  4337. - hifn_write_1(dev, HIFN_1_DMA_CRAR, __cpu_to_le32(dptr +
  4338. - offsetof(struct hifn_dma, cmdr[0])));
  4339. - hifn_write_1(dev, HIFN_1_DMA_SRAR, __cpu_to_le32(dptr +
  4340. - offsetof(struct hifn_dma, srcr[0])));
  4341. - hifn_write_1(dev, HIFN_1_DMA_DRAR, __cpu_to_le32(dptr +
  4342. - offsetof(struct hifn_dma, dstr[0])));
  4343. - hifn_write_1(dev, HIFN_1_DMA_RRAR, __cpu_to_le32(dptr +
  4344. - offsetof(struct hifn_dma, resr[0])));
  4345. + hifn_write_1(dev, HIFN_1_DMA_CRAR, dptr +
  4346. + offsetof(struct hifn_dma, cmdr[0]));
  4347. + hifn_write_1(dev, HIFN_1_DMA_SRAR, dptr +
  4348. + offsetof(struct hifn_dma, srcr[0]));
  4349. + hifn_write_1(dev, HIFN_1_DMA_DRAR, dptr +
  4350. + offsetof(struct hifn_dma, dstr[0]));
  4351. + hifn_write_1(dev, HIFN_1_DMA_RRAR, dptr +
  4352. + offsetof(struct hifn_dma, resr[0]));
  4353. mdelay(2);
  4354. #if 0
  4355. @@ -1166,109 +1167,15 @@ static int hifn_setup_crypto_command(str
  4356. return cmd_len;
  4357. }
  4358. -static int hifn_setup_src_desc(struct hifn_device *dev, struct page *page,
  4359. - unsigned int offset, unsigned int size)
  4360. -{
  4361. - struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
  4362. - int idx;
  4363. - dma_addr_t addr;
  4364. -
  4365. - addr = pci_map_page(dev->pdev, page, offset, size, PCI_DMA_TODEVICE);
  4366. -
  4367. - idx = dma->srci;
  4368. -
  4369. - dma->srcr[idx].p = __cpu_to_le32(addr);
  4370. - dma->srcr[idx].l = __cpu_to_le32(size) | HIFN_D_VALID |
  4371. - HIFN_D_MASKDONEIRQ | HIFN_D_NOINVALID | HIFN_D_LAST;
  4372. -
  4373. - if (++idx == HIFN_D_SRC_RSIZE) {
  4374. - dma->srcr[idx].l = __cpu_to_le32(HIFN_D_VALID |
  4375. - HIFN_D_JUMP |
  4376. - HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
  4377. - idx = 0;
  4378. - }
  4379. -
  4380. - dma->srci = idx;
  4381. - dma->srcu++;
  4382. -
  4383. - if (!(dev->flags & HIFN_FLAG_SRC_BUSY)) {
  4384. - hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_S_CTRL_ENA);
  4385. - dev->flags |= HIFN_FLAG_SRC_BUSY;
  4386. - }
  4387. -
  4388. - return size;
  4389. -}
  4390. -
  4391. -static void hifn_setup_res_desc(struct hifn_device *dev)
  4392. -{
  4393. - struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
  4394. -
  4395. - dma->resr[dma->resi].l = __cpu_to_le32(HIFN_USED_RESULT |
  4396. - HIFN_D_VALID | HIFN_D_LAST);
  4397. - /*
  4398. - * dma->resr[dma->resi].l = __cpu_to_le32(HIFN_MAX_RESULT | HIFN_D_VALID |
  4399. - * HIFN_D_LAST | HIFN_D_NOINVALID);
  4400. - */
  4401. -
  4402. - if (++dma->resi == HIFN_D_RES_RSIZE) {
  4403. - dma->resr[HIFN_D_RES_RSIZE].l = __cpu_to_le32(HIFN_D_VALID |
  4404. - HIFN_D_JUMP | HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
  4405. - dma->resi = 0;
  4406. - }
  4407. -
  4408. - dma->resu++;
  4409. -
  4410. - if (!(dev->flags & HIFN_FLAG_RES_BUSY)) {
  4411. - hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_R_CTRL_ENA);
  4412. - dev->flags |= HIFN_FLAG_RES_BUSY;
  4413. - }
  4414. -}
  4415. -
  4416. -static void hifn_setup_dst_desc(struct hifn_device *dev, struct page *page,
  4417. - unsigned offset, unsigned size)
  4418. -{
  4419. - struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
  4420. - int idx;
  4421. - dma_addr_t addr;
  4422. -
  4423. - addr = pci_map_page(dev->pdev, page, offset, size, PCI_DMA_FROMDEVICE);
  4424. -
  4425. - idx = dma->dsti;
  4426. - dma->dstr[idx].p = __cpu_to_le32(addr);
  4427. - dma->dstr[idx].l = __cpu_to_le32(size | HIFN_D_VALID |
  4428. - HIFN_D_MASKDONEIRQ | HIFN_D_NOINVALID | HIFN_D_LAST);
  4429. -
  4430. - if (++idx == HIFN_D_DST_RSIZE) {
  4431. - dma->dstr[idx].l = __cpu_to_le32(HIFN_D_VALID |
  4432. - HIFN_D_JUMP | HIFN_D_MASKDONEIRQ |
  4433. - HIFN_D_LAST | HIFN_D_NOINVALID);
  4434. - idx = 0;
  4435. - }
  4436. - dma->dsti = idx;
  4437. - dma->dstu++;
  4438. -
  4439. - if (!(dev->flags & HIFN_FLAG_DST_BUSY)) {
  4440. - hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_D_CTRL_ENA);
  4441. - dev->flags |= HIFN_FLAG_DST_BUSY;
  4442. - }
  4443. -}
  4444. -
  4445. -static int hifn_setup_dma(struct hifn_device *dev, struct page *spage, unsigned int soff,
  4446. - struct page *dpage, unsigned int doff, unsigned int nbytes, void *priv,
  4447. - struct hifn_context *ctx)
  4448. +static int hifn_setup_cmd_desc(struct hifn_device *dev,
  4449. + struct hifn_context *ctx, void *priv, unsigned int nbytes)
  4450. {
  4451. struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
  4452. int cmd_len, sa_idx;
  4453. u8 *buf, *buf_pos;
  4454. u16 mask;
  4455. - dprintk("%s: spage: %p, soffset: %u, dpage: %p, doffset: %u, nbytes: %u, priv: %p, ctx: %p.\n",
  4456. - dev->name, spage, soff, dpage, doff, nbytes, priv, ctx);
  4457. -
  4458. - sa_idx = dma->resi;
  4459. -
  4460. - hifn_setup_src_desc(dev, spage, soff, nbytes);
  4461. -
  4462. + sa_idx = dma->cmdi;
  4463. buf_pos = buf = dma->command_bufs[dma->cmdi];
  4464. mask = 0;
  4465. @@ -1370,16 +1277,113 @@ static int hifn_setup_dma(struct hifn_de
  4466. hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_C_CTRL_ENA);
  4467. dev->flags |= HIFN_FLAG_CMD_BUSY;
  4468. }
  4469. -
  4470. - hifn_setup_dst_desc(dev, dpage, doff, nbytes);
  4471. - hifn_setup_res_desc(dev);
  4472. -
  4473. return 0;
  4474. err_out:
  4475. return -EINVAL;
  4476. }
  4477. +static int hifn_setup_src_desc(struct hifn_device *dev, struct page *page,
  4478. + unsigned int offset, unsigned int size)
  4479. +{
  4480. + struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
  4481. + int idx;
  4482. + dma_addr_t addr;
  4483. +
  4484. + addr = pci_map_page(dev->pdev, page, offset, size, PCI_DMA_TODEVICE);
  4485. +
  4486. + idx = dma->srci;
  4487. +
  4488. + dma->srcr[idx].p = __cpu_to_le32(addr);
  4489. + dma->srcr[idx].l = __cpu_to_le32(size | HIFN_D_VALID |
  4490. + HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
  4491. +
  4492. + if (++idx == HIFN_D_SRC_RSIZE) {
  4493. + dma->srcr[idx].l = __cpu_to_le32(HIFN_D_VALID |
  4494. + HIFN_D_JUMP |
  4495. + HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
  4496. + idx = 0;
  4497. + }
  4498. +
  4499. + dma->srci = idx;
  4500. + dma->srcu++;
  4501. +
  4502. + if (!(dev->flags & HIFN_FLAG_SRC_BUSY)) {
  4503. + hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_S_CTRL_ENA);
  4504. + dev->flags |= HIFN_FLAG_SRC_BUSY;
  4505. + }
  4506. +
  4507. + return size;
  4508. +}
  4509. +
  4510. +static void hifn_setup_res_desc(struct hifn_device *dev)
  4511. +{
  4512. + struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
  4513. +
  4514. + dma->resr[dma->resi].l = __cpu_to_le32(HIFN_USED_RESULT |
  4515. + HIFN_D_VALID | HIFN_D_LAST);
  4516. + /*
  4517. + * dma->resr[dma->resi].l = __cpu_to_le32(HIFN_MAX_RESULT | HIFN_D_VALID |
  4518. + * HIFN_D_LAST);
  4519. + */
  4520. +
  4521. + if (++dma->resi == HIFN_D_RES_RSIZE) {
  4522. + dma->resr[HIFN_D_RES_RSIZE].l = __cpu_to_le32(HIFN_D_VALID |
  4523. + HIFN_D_JUMP | HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
  4524. + dma->resi = 0;
  4525. + }
  4526. +
  4527. + dma->resu++;
  4528. +
  4529. + if (!(dev->flags & HIFN_FLAG_RES_BUSY)) {
  4530. + hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_R_CTRL_ENA);
  4531. + dev->flags |= HIFN_FLAG_RES_BUSY;
  4532. + }
  4533. +}
  4534. +
  4535. +static void hifn_setup_dst_desc(struct hifn_device *dev, struct page *page,
  4536. + unsigned offset, unsigned size)
  4537. +{
  4538. + struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
  4539. + int idx;
  4540. + dma_addr_t addr;
  4541. +
  4542. + addr = pci_map_page(dev->pdev, page, offset, size, PCI_DMA_FROMDEVICE);
  4543. +
  4544. + idx = dma->dsti;
  4545. + dma->dstr[idx].p = __cpu_to_le32(addr);
  4546. + dma->dstr[idx].l = __cpu_to_le32(size | HIFN_D_VALID |
  4547. + HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
  4548. +
  4549. + if (++idx == HIFN_D_DST_RSIZE) {
  4550. + dma->dstr[idx].l = __cpu_to_le32(HIFN_D_VALID |
  4551. + HIFN_D_JUMP | HIFN_D_MASKDONEIRQ |
  4552. + HIFN_D_LAST);
  4553. + idx = 0;
  4554. + }
  4555. + dma->dsti = idx;
  4556. + dma->dstu++;
  4557. +
  4558. + if (!(dev->flags & HIFN_FLAG_DST_BUSY)) {
  4559. + hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_D_CTRL_ENA);
  4560. + dev->flags |= HIFN_FLAG_DST_BUSY;
  4561. + }
  4562. +}
  4563. +
  4564. +static int hifn_setup_dma(struct hifn_device *dev, struct page *spage, unsigned int soff,
  4565. + struct page *dpage, unsigned int doff, unsigned int nbytes, void *priv,
  4566. + struct hifn_context *ctx)
  4567. +{
  4568. + dprintk("%s: spage: %p, soffset: %u, dpage: %p, doffset: %u, nbytes: %u, priv: %p, ctx: %p.\n",
  4569. + dev->name, spage, soff, dpage, doff, nbytes, priv, ctx);
  4570. +
  4571. + hifn_setup_src_desc(dev, spage, soff, nbytes);
  4572. + hifn_setup_cmd_desc(dev, ctx, priv, nbytes);
  4573. + hifn_setup_dst_desc(dev, dpage, doff, nbytes);
  4574. + hifn_setup_res_desc(dev);
  4575. + return 0;
  4576. +}
  4577. +
  4578. static int ablkcipher_walk_init(struct ablkcipher_walk *w,
  4579. int num, gfp_t gfp_flags)
  4580. {
  4581. @@ -1431,7 +1435,7 @@ static int ablkcipher_add(void *daddr, u
  4582. return -EINVAL;
  4583. while (size) {
  4584. - copy = min(drest, src->length);
  4585. + copy = min(drest, min(size, src->length));
  4586. saddr = kmap_atomic(sg_page(src), KM_SOFTIRQ1);
  4587. memcpy(daddr, saddr + src->offset, copy);
  4588. @@ -1458,10 +1462,6 @@ static int ablkcipher_add(void *daddr, u
  4589. static int ablkcipher_walk(struct ablkcipher_request *req,
  4590. struct ablkcipher_walk *w)
  4591. {
  4592. - unsigned blocksize =
  4593. - crypto_ablkcipher_blocksize(crypto_ablkcipher_reqtfm(req));
  4594. - unsigned alignmask =
  4595. - crypto_ablkcipher_alignmask(crypto_ablkcipher_reqtfm(req));
  4596. struct scatterlist *src, *dst, *t;
  4597. void *daddr;
  4598. unsigned int nbytes = req->nbytes, offset, copy, diff;
  4599. @@ -1477,16 +1477,14 @@ static int ablkcipher_walk(struct ablkci
  4600. dst = &req->dst[idx];
  4601. dprintk("\n%s: slen: %u, dlen: %u, soff: %u, doff: %u, offset: %u, "
  4602. - "blocksize: %u, nbytes: %u.\n",
  4603. + "nbytes: %u.\n",
  4604. __func__, src->length, dst->length, src->offset,
  4605. - dst->offset, offset, blocksize, nbytes);
  4606. + dst->offset, offset, nbytes);
  4607. - if (src->length & (blocksize - 1) ||
  4608. - src->offset & (alignmask - 1) ||
  4609. - dst->length & (blocksize - 1) ||
  4610. - dst->offset & (alignmask - 1) ||
  4611. - offset) {
  4612. - unsigned slen = src->length - offset;
  4613. + if (!IS_ALIGNED(dst->offset, HIFN_D_DST_DALIGN) ||
  4614. + !IS_ALIGNED(dst->length, HIFN_D_DST_DALIGN) ||
  4615. + offset) {
  4616. + unsigned slen = min(src->length - offset, nbytes);
  4617. unsigned dlen = PAGE_SIZE;
  4618. t = &w->cache[idx];
  4619. @@ -1498,8 +1496,8 @@ static int ablkcipher_walk(struct ablkci
  4620. idx += err;
  4621. - copy = slen & ~(blocksize - 1);
  4622. - diff = slen & (blocksize - 1);
  4623. + copy = slen & ~(HIFN_D_DST_DALIGN - 1);
  4624. + diff = slen & (HIFN_D_DST_DALIGN - 1);
  4625. if (dlen < nbytes) {
  4626. /*
  4627. @@ -1507,7 +1505,7 @@ static int ablkcipher_walk(struct ablkci
  4628. * to put there additional blocksized chunk,
  4629. * so we mark that page as containing only
  4630. * blocksize aligned chunks:
  4631. - * t->length = (slen & ~(blocksize - 1));
  4632. + * t->length = (slen & ~(HIFN_D_DST_DALIGN - 1));
  4633. * and increase number of bytes to be processed
  4634. * in next chunk:
  4635. * nbytes += diff;
  4636. @@ -1544,7 +1542,7 @@ static int ablkcipher_walk(struct ablkci
  4637. kunmap_atomic(daddr, KM_SOFTIRQ0);
  4638. } else {
  4639. - nbytes -= src->length;
  4640. + nbytes -= min(src->length, nbytes);
  4641. idx++;
  4642. }
  4643. @@ -1563,14 +1561,10 @@ static int hifn_setup_session(struct abl
  4644. struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
  4645. struct hifn_device *dev = ctx->dev;
  4646. struct page *spage, *dpage;
  4647. - unsigned long soff, doff, flags;
  4648. + unsigned long soff, doff, dlen, flags;
  4649. unsigned int nbytes = req->nbytes, idx = 0, len;
  4650. int err = -EINVAL, sg_num;
  4651. struct scatterlist *src, *dst, *t;
  4652. - unsigned blocksize =
  4653. - crypto_ablkcipher_blocksize(crypto_ablkcipher_reqtfm(req));
  4654. - unsigned alignmask =
  4655. - crypto_ablkcipher_alignmask(crypto_ablkcipher_reqtfm(req));
  4656. if (ctx->iv && !ctx->ivsize && ctx->mode != ACRYPTO_MODE_ECB)
  4657. goto err_out_exit;
  4658. @@ -1578,17 +1572,14 @@ static int hifn_setup_session(struct abl
  4659. ctx->walk.flags = 0;
  4660. while (nbytes) {
  4661. - src = &req->src[idx];
  4662. dst = &req->dst[idx];
  4663. + dlen = min(dst->length, nbytes);
  4664. - if (src->length & (blocksize - 1) ||
  4665. - src->offset & (alignmask - 1) ||
  4666. - dst->length & (blocksize - 1) ||
  4667. - dst->offset & (alignmask - 1)) {
  4668. + if (!IS_ALIGNED(dst->offset, HIFN_D_DST_DALIGN) ||
  4669. + !IS_ALIGNED(dlen, HIFN_D_DST_DALIGN))
  4670. ctx->walk.flags |= ASYNC_FLAGS_MISALIGNED;
  4671. - }
  4672. - nbytes -= src->length;
  4673. + nbytes -= dlen;
  4674. idx++;
  4675. }
  4676. @@ -1602,7 +1593,10 @@ static int hifn_setup_session(struct abl
  4677. idx = 0;
  4678. sg_num = ablkcipher_walk(req, &ctx->walk);
  4679. -
  4680. + if (sg_num < 0) {
  4681. + err = sg_num;
  4682. + goto err_out_exit;
  4683. + }
  4684. atomic_set(&ctx->sg_num, sg_num);
  4685. spin_lock_irqsave(&dev->lock, flags);
  4686. @@ -1640,7 +1634,7 @@ static int hifn_setup_session(struct abl
  4687. if (err)
  4688. goto err_out;
  4689. - nbytes -= len;
  4690. + nbytes -= min(len, nbytes);
  4691. }
  4692. dev->active = HIFN_DEFAULT_ACTIVE_NUM;
  4693. @@ -1651,7 +1645,7 @@ static int hifn_setup_session(struct abl
  4694. err_out:
  4695. spin_unlock_irqrestore(&dev->lock, flags);
  4696. err_out_exit:
  4697. - if (err && printk_ratelimit())
  4698. + if (err)
  4699. dprintk("%s: iv: %p [%d], key: %p [%d], mode: %u, op: %u, "
  4700. "type: %u, err: %d.\n",
  4701. dev->name, ctx->iv, ctx->ivsize,
  4702. @@ -1745,8 +1739,7 @@ static int ablkcipher_get(void *saddr, u
  4703. return -EINVAL;
  4704. while (size) {
  4705. -
  4706. - copy = min(dst->length, srest);
  4707. + copy = min(srest, min(dst->length, size));
  4708. daddr = kmap_atomic(sg_page(dst), KM_IRQ0);
  4709. memcpy(daddr + dst->offset + offset, saddr, copy);
  4710. @@ -1803,7 +1796,7 @@ static void hifn_process_ready(struct ab
  4711. sg_page(dst), dst->length, nbytes);
  4712. if (!t->length) {
  4713. - nbytes -= dst->length;
  4714. + nbytes -= min(dst->length, nbytes);
  4715. idx++;
  4716. continue;
  4717. }
  4718. @@ -2202,9 +2195,9 @@ static int hifn_setup_crypto(struct ablk
  4719. return err;
  4720. if (dev->started < HIFN_QUEUE_LENGTH && dev->queue.qlen)
  4721. - err = hifn_process_queue(dev);
  4722. + hifn_process_queue(dev);
  4723. - return err;
  4724. + return -EINPROGRESS;
  4725. }
  4726. /*
  4727. @@ -2364,7 +2357,7 @@ static struct hifn_alg_template hifn_alg
  4728. * 3DES ECB, CBC, CFB and OFB modes.
  4729. */
  4730. {
  4731. - .name = "cfb(des3_ede)", .drv_name = "hifn-3des", .bsize = 8,
  4732. + .name = "cfb(des3_ede)", .drv_name = "cfb-3des", .bsize = 8,
  4733. .ablkcipher = {
  4734. .min_keysize = HIFN_3DES_KEY_LENGTH,
  4735. .max_keysize = HIFN_3DES_KEY_LENGTH,
  4736. @@ -2374,7 +2367,7 @@ static struct hifn_alg_template hifn_alg
  4737. },
  4738. },
  4739. {
  4740. - .name = "ofb(des3_ede)", .drv_name = "hifn-3des", .bsize = 8,
  4741. + .name = "ofb(des3_ede)", .drv_name = "ofb-3des", .bsize = 8,
  4742. .ablkcipher = {
  4743. .min_keysize = HIFN_3DES_KEY_LENGTH,
  4744. .max_keysize = HIFN_3DES_KEY_LENGTH,
  4745. @@ -2384,8 +2377,9 @@ static struct hifn_alg_template hifn_alg
  4746. },
  4747. },
  4748. {
  4749. - .name = "cbc(des3_ede)", .drv_name = "hifn-3des", .bsize = 8,
  4750. + .name = "cbc(des3_ede)", .drv_name = "cbc-3des", .bsize = 8,
  4751. .ablkcipher = {
  4752. + .ivsize = HIFN_IV_LENGTH,
  4753. .min_keysize = HIFN_3DES_KEY_LENGTH,
  4754. .max_keysize = HIFN_3DES_KEY_LENGTH,
  4755. .setkey = hifn_setkey,
  4756. @@ -2394,7 +2388,7 @@ static struct hifn_alg_template hifn_alg
  4757. },
  4758. },
  4759. {
  4760. - .name = "ecb(des3_ede)", .drv_name = "hifn-3des", .bsize = 8,
  4761. + .name = "ecb(des3_ede)", .drv_name = "ecb-3des", .bsize = 8,
  4762. .ablkcipher = {
  4763. .min_keysize = HIFN_3DES_KEY_LENGTH,
  4764. .max_keysize = HIFN_3DES_KEY_LENGTH,
  4765. @@ -2408,7 +2402,7 @@ static struct hifn_alg_template hifn_alg
  4766. * DES ECB, CBC, CFB and OFB modes.
  4767. */
  4768. {
  4769. - .name = "cfb(des)", .drv_name = "hifn-des", .bsize = 8,
  4770. + .name = "cfb(des)", .drv_name = "cfb-des", .bsize = 8,
  4771. .ablkcipher = {
  4772. .min_keysize = HIFN_DES_KEY_LENGTH,
  4773. .max_keysize = HIFN_DES_KEY_LENGTH,
  4774. @@ -2418,7 +2412,7 @@ static struct hifn_alg_template hifn_alg
  4775. },
  4776. },
  4777. {
  4778. - .name = "ofb(des)", .drv_name = "hifn-des", .bsize = 8,
  4779. + .name = "ofb(des)", .drv_name = "ofb-des", .bsize = 8,
  4780. .ablkcipher = {
  4781. .min_keysize = HIFN_DES_KEY_LENGTH,
  4782. .max_keysize = HIFN_DES_KEY_LENGTH,
  4783. @@ -2428,8 +2422,9 @@ static struct hifn_alg_template hifn_alg
  4784. },
  4785. },
  4786. {
  4787. - .name = "cbc(des)", .drv_name = "hifn-des", .bsize = 8,
  4788. + .name = "cbc(des)", .drv_name = "cbc-des", .bsize = 8,
  4789. .ablkcipher = {
  4790. + .ivsize = HIFN_IV_LENGTH,
  4791. .min_keysize = HIFN_DES_KEY_LENGTH,
  4792. .max_keysize = HIFN_DES_KEY_LENGTH,
  4793. .setkey = hifn_setkey,
  4794. @@ -2438,7 +2433,7 @@ static struct hifn_alg_template hifn_alg
  4795. },
  4796. },
  4797. {
  4798. - .name = "ecb(des)", .drv_name = "hifn-des", .bsize = 8,
  4799. + .name = "ecb(des)", .drv_name = "ecb-des", .bsize = 8,
  4800. .ablkcipher = {
  4801. .min_keysize = HIFN_DES_KEY_LENGTH,
  4802. .max_keysize = HIFN_DES_KEY_LENGTH,
  4803. @@ -2452,7 +2447,7 @@ static struct hifn_alg_template hifn_alg
  4804. * AES ECB, CBC, CFB and OFB modes.
  4805. */
  4806. {
  4807. - .name = "ecb(aes)", .drv_name = "hifn-aes", .bsize = 16,
  4808. + .name = "ecb(aes)", .drv_name = "ecb-aes", .bsize = 16,
  4809. .ablkcipher = {
  4810. .min_keysize = AES_MIN_KEY_SIZE,
  4811. .max_keysize = AES_MAX_KEY_SIZE,
  4812. @@ -2462,8 +2457,9 @@ static struct hifn_alg_template hifn_alg
  4813. },
  4814. },
  4815. {
  4816. - .name = "cbc(aes)", .drv_name = "hifn-aes", .bsize = 16,
  4817. + .name = "cbc(aes)", .drv_name = "cbc-aes", .bsize = 16,
  4818. .ablkcipher = {
  4819. + .ivsize = HIFN_AES_IV_LENGTH,
  4820. .min_keysize = AES_MIN_KEY_SIZE,
  4821. .max_keysize = AES_MAX_KEY_SIZE,
  4822. .setkey = hifn_setkey,
  4823. @@ -2472,7 +2468,7 @@ static struct hifn_alg_template hifn_alg
  4824. },
  4825. },
  4826. {
  4827. - .name = "cfb(aes)", .drv_name = "hifn-aes", .bsize = 16,
  4828. + .name = "cfb(aes)", .drv_name = "cfb-aes", .bsize = 16,
  4829. .ablkcipher = {
  4830. .min_keysize = AES_MIN_KEY_SIZE,
  4831. .max_keysize = AES_MAX_KEY_SIZE,
  4832. @@ -2482,7 +2478,7 @@ static struct hifn_alg_template hifn_alg
  4833. },
  4834. },
  4835. {
  4836. - .name = "ofb(aes)", .drv_name = "hifn-aes", .bsize = 16,
  4837. + .name = "ofb(aes)", .drv_name = "ofb-aes", .bsize = 16,
  4838. .ablkcipher = {
  4839. .min_keysize = AES_MIN_KEY_SIZE,
  4840. .max_keysize = AES_MAX_KEY_SIZE,
  4841. @@ -2514,15 +2510,14 @@ static int hifn_alg_alloc(struct hifn_de
  4842. return -ENOMEM;
  4843. snprintf(alg->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s", t->name);
  4844. - snprintf(alg->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s", t->drv_name);
  4845. + snprintf(alg->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s-%s",
  4846. + t->drv_name, dev->name);
  4847. alg->alg.cra_priority = 300;
  4848. alg->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC;
  4849. alg->alg.cra_blocksize = t->bsize;
  4850. alg->alg.cra_ctxsize = sizeof(struct hifn_context);
  4851. - alg->alg.cra_alignmask = 15;
  4852. - if (t->bsize == 8)
  4853. - alg->alg.cra_alignmask = 3;
  4854. + alg->alg.cra_alignmask = 0;
  4855. alg->alg.cra_type = &crypto_ablkcipher_type;
  4856. alg->alg.cra_module = THIS_MODULE;
  4857. alg->alg.cra_u.ablkcipher = t->ablkcipher;
  4858. --- /dev/null
  4859. +++ b/drivers/crypto/ixp4xx_crypto.c
  4860. @@ -0,0 +1,1506 @@
  4861. +/*
  4862. + * Intel IXP4xx NPE-C crypto driver
  4863. + *
  4864. + * Copyright (C) 2008 Christian Hohnstaedt <[email protected]>
  4865. + *
  4866. + * This program is free software; you can redistribute it and/or modify it
  4867. + * under the terms of version 2 of the GNU General Public License
  4868. + * as published by the Free Software Foundation.
  4869. + *
  4870. + */
  4871. +
  4872. +#include <linux/platform_device.h>
  4873. +#include <linux/dma-mapping.h>
  4874. +#include <linux/dmapool.h>
  4875. +#include <linux/crypto.h>
  4876. +#include <linux/kernel.h>
  4877. +#include <linux/rtnetlink.h>
  4878. +#include <linux/interrupt.h>
  4879. +#include <linux/spinlock.h>
  4880. +
  4881. +#include <crypto/ctr.h>
  4882. +#include <crypto/des.h>
  4883. +#include <crypto/aes.h>
  4884. +#include <crypto/sha.h>
  4885. +#include <crypto/algapi.h>
  4886. +#include <crypto/aead.h>
  4887. +#include <crypto/authenc.h>
  4888. +#include <crypto/scatterwalk.h>
  4889. +
  4890. +#include <asm/arch/npe.h>
  4891. +#include <asm/arch/qmgr.h>
  4892. +
  4893. +#define MAX_KEYLEN 32
  4894. +
  4895. +/* hash: cfgword + 2 * digestlen; crypt: keylen + cfgword */
  4896. +#define NPE_CTX_LEN 80
  4897. +#define AES_BLOCK128 16
  4898. +
  4899. +#define NPE_OP_HASH_VERIFY 0x01
  4900. +#define NPE_OP_CCM_ENABLE 0x04
  4901. +#define NPE_OP_CRYPT_ENABLE 0x08
  4902. +#define NPE_OP_HASH_ENABLE 0x10
  4903. +#define NPE_OP_NOT_IN_PLACE 0x20
  4904. +#define NPE_OP_HMAC_DISABLE 0x40
  4905. +#define NPE_OP_CRYPT_ENCRYPT 0x80
  4906. +
  4907. +#define NPE_OP_CCM_GEN_MIC 0xcc
  4908. +#define NPE_OP_HASH_GEN_ICV 0x50
  4909. +#define NPE_OP_ENC_GEN_KEY 0xc9
  4910. +
  4911. +#define MOD_ECB 0x0000
  4912. +#define MOD_CTR 0x1000
  4913. +#define MOD_CBC_ENC 0x2000
  4914. +#define MOD_CBC_DEC 0x3000
  4915. +#define MOD_CCM_ENC 0x4000
  4916. +#define MOD_CCM_DEC 0x5000
  4917. +
  4918. +#define KEYLEN_128 4
  4919. +#define KEYLEN_192 6
  4920. +#define KEYLEN_256 8
  4921. +
  4922. +#define CIPH_DECR 0x0000
  4923. +#define CIPH_ENCR 0x0400
  4924. +
  4925. +#define MOD_DES 0x0000
  4926. +#define MOD_TDEA2 0x0100
  4927. +#define MOD_3DES 0x0200
  4928. +#define MOD_AES 0x0800
  4929. +#define MOD_AES128 (0x0800 | KEYLEN_128)
  4930. +#define MOD_AES192 (0x0900 | KEYLEN_192)
  4931. +#define MOD_AES256 (0x0a00 | KEYLEN_256)
  4932. +
  4933. +#define MAX_IVLEN 16
  4934. +#define NPE_ID 2 /* NPE C */
  4935. +#define NPE_QLEN 16
  4936. +/* Space for registering when the first
  4937. + * NPE_QLEN crypt_ctl are busy */
  4938. +#define NPE_QLEN_TOTAL 64
  4939. +
  4940. +#define SEND_QID 29
  4941. +#define RECV_QID 30
  4942. +
  4943. +#define CTL_FLAG_UNUSED 0x0000
  4944. +#define CTL_FLAG_USED 0x1000
  4945. +#define CTL_FLAG_PERFORM_ABLK 0x0001
  4946. +#define CTL_FLAG_GEN_ICV 0x0002
  4947. +#define CTL_FLAG_GEN_REVAES 0x0004
  4948. +#define CTL_FLAG_PERFORM_AEAD 0x0008
  4949. +#define CTL_FLAG_MASK 0x000f
  4950. +
  4951. +#define HMAC_IPAD_VALUE 0x36
  4952. +#define HMAC_OPAD_VALUE 0x5C
  4953. +#define HMAC_PAD_BLOCKLEN SHA1_BLOCK_SIZE
  4954. +
  4955. +#define MD5_DIGEST_SIZE 16
  4956. +
  4957. +struct buffer_desc {
  4958. + u32 phys_next;
  4959. + u16 buf_len;
  4960. + u16 pkt_len;
  4961. + u32 phys_addr;
  4962. + u32 __reserved[4];
  4963. + struct buffer_desc *next;
  4964. +};
  4965. +
  4966. +struct crypt_ctl {
  4967. + u8 mode; /* NPE_OP_* operation mode */
  4968. + u8 init_len;
  4969. + u16 reserved;
  4970. + u8 iv[MAX_IVLEN]; /* IV for CBC mode or CTR IV for CTR mode */
  4971. + u32 icv_rev_aes; /* icv or rev aes */
  4972. + u32 src_buf;
  4973. + u32 dst_buf;
  4974. + u16 auth_offs; /* Authentication start offset */
  4975. + u16 auth_len; /* Authentication data length */
  4976. + u16 crypt_offs; /* Cryption start offset */
  4977. + u16 crypt_len; /* Cryption data length */
  4978. + u32 aadAddr; /* Additional Auth Data Addr for CCM mode */
  4979. + u32 crypto_ctx; /* NPE Crypto Param structure address */
  4980. +
  4981. + /* Used by Host: 4*4 bytes*/
  4982. + unsigned ctl_flags;
  4983. + union {
  4984. + struct ablkcipher_request *ablk_req;
  4985. + struct aead_request *aead_req;
  4986. + struct crypto_tfm *tfm;
  4987. + } data;
  4988. + struct buffer_desc *regist_buf;
  4989. + u8 *regist_ptr;
  4990. +};
  4991. +
  4992. +struct ablk_ctx {
  4993. + struct buffer_desc *src;
  4994. + struct buffer_desc *dst;
  4995. + unsigned src_nents;
  4996. + unsigned dst_nents;
  4997. +};
  4998. +
  4999. +struct aead_ctx {
  5000. + struct buffer_desc *buffer;
  5001. + unsigned short assoc_nents;
  5002. + unsigned short src_nents;
  5003. + struct scatterlist ivlist;
  5004. + /* used when the hmac is not on one sg entry */
  5005. + u8 *hmac_virt;
  5006. + int encrypt;
  5007. +};
  5008. +
  5009. +struct ix_hash_algo {
  5010. + u32 cfgword;
  5011. + unsigned char *icv;
  5012. +};
  5013. +
  5014. +struct ix_sa_dir {
  5015. + unsigned char *npe_ctx;
  5016. + dma_addr_t npe_ctx_phys;
  5017. + int npe_ctx_idx;
  5018. + u8 npe_mode;
  5019. +};
  5020. +
  5021. +struct ixp_ctx {
  5022. + struct ix_sa_dir encrypt;
  5023. + struct ix_sa_dir decrypt;
  5024. + int authkey_len;
  5025. + u8 authkey[MAX_KEYLEN];
  5026. + int enckey_len;
  5027. + u8 enckey[MAX_KEYLEN];
  5028. + u8 salt[MAX_IVLEN];
  5029. + u8 nonce[CTR_RFC3686_NONCE_SIZE];
  5030. + unsigned salted;
  5031. + atomic_t configuring;
  5032. + struct completion completion;
  5033. +};
  5034. +
  5035. +struct ixp_alg {
  5036. + struct crypto_alg crypto;
  5037. + const struct ix_hash_algo *hash;
  5038. + u32 cfg_enc;
  5039. + u32 cfg_dec;
  5040. +
  5041. + int registered;
  5042. +};
  5043. +
  5044. +static const struct ix_hash_algo hash_alg_md5 = {
  5045. + .cfgword = 0xAA010004,
  5046. + .icv = "\x01\x23\x45\x67\x89\xAB\xCD\xEF"
  5047. + "\xFE\xDC\xBA\x98\x76\x54\x32\x10",
  5048. +};
  5049. +static const struct ix_hash_algo hash_alg_sha1 = {
  5050. + .cfgword = 0x00000005,
  5051. + .icv = "\x67\x45\x23\x01\xEF\xCD\xAB\x89\x98\xBA"
  5052. + "\xDC\xFE\x10\x32\x54\x76\xC3\xD2\xE1\xF0",
  5053. +};
  5054. +
  5055. +static struct npe *npe_c;
  5056. +static struct dma_pool *buffer_pool = NULL;
  5057. +static struct dma_pool *ctx_pool = NULL;
  5058. +
  5059. +static struct crypt_ctl *crypt_virt = NULL;
  5060. +static dma_addr_t crypt_phys;
  5061. +
  5062. +static int support_aes = 1;
  5063. +
  5064. +static void dev_release(struct device *dev)
  5065. +{
  5066. + return;
  5067. +}
  5068. +
  5069. +#define DRIVER_NAME "ixp4xx_crypto"
  5070. +static struct platform_device pseudo_dev = {
  5071. + .name = DRIVER_NAME,
  5072. + .id = 0,
  5073. + .num_resources = 0,
  5074. + .dev = {
  5075. + .coherent_dma_mask = DMA_32BIT_MASK,
  5076. + .release = dev_release,
  5077. + }
  5078. +};
  5079. +
  5080. +static struct device *dev = &pseudo_dev.dev;
  5081. +
  5082. +static inline dma_addr_t crypt_virt2phys(struct crypt_ctl *virt)
  5083. +{
  5084. + return crypt_phys + (virt - crypt_virt) * sizeof(struct crypt_ctl);
  5085. +}
  5086. +
  5087. +static inline struct crypt_ctl *crypt_phys2virt(dma_addr_t phys)
  5088. +{
  5089. + return crypt_virt + (phys - crypt_phys) / sizeof(struct crypt_ctl);
  5090. +}
  5091. +
  5092. +static inline u32 cipher_cfg_enc(struct crypto_tfm *tfm)
  5093. +{
  5094. + return container_of(tfm->__crt_alg, struct ixp_alg,crypto)->cfg_enc;
  5095. +}
  5096. +
  5097. +static inline u32 cipher_cfg_dec(struct crypto_tfm *tfm)
  5098. +{
  5099. + return container_of(tfm->__crt_alg, struct ixp_alg,crypto)->cfg_dec;
  5100. +}
  5101. +
  5102. +static inline const struct ix_hash_algo *ix_hash(struct crypto_tfm *tfm)
  5103. +{
  5104. + return container_of(tfm->__crt_alg, struct ixp_alg, crypto)->hash;
  5105. +}
  5106. +
  5107. +static int setup_crypt_desc(void)
  5108. +{
  5109. + BUILD_BUG_ON(sizeof(struct crypt_ctl) != 64);
  5110. + crypt_virt = dma_alloc_coherent(dev,
  5111. + NPE_QLEN * sizeof(struct crypt_ctl),
  5112. + &crypt_phys, GFP_KERNEL);
  5113. + if (!crypt_virt)
  5114. + return -ENOMEM;
  5115. + memset(crypt_virt, 0, NPE_QLEN * sizeof(struct crypt_ctl));
  5116. + return 0;
  5117. +}
  5118. +
  5119. +static spinlock_t desc_lock;
  5120. +static struct crypt_ctl *get_crypt_desc(void)
  5121. +{
  5122. + int i;
  5123. + static int idx = 0;
  5124. + unsigned long flags;
  5125. +
  5126. + spin_lock_irqsave(&desc_lock, flags);
  5127. +
  5128. + if (unlikely(!crypt_virt))
  5129. + setup_crypt_desc();
  5130. + if (unlikely(!crypt_virt)) {
  5131. + spin_unlock_irqrestore(&desc_lock, flags);
  5132. + return NULL;
  5133. + }
  5134. + i = idx;
  5135. + if (crypt_virt[i].ctl_flags == CTL_FLAG_UNUSED) {
  5136. + if (++idx >= NPE_QLEN)
  5137. + idx = 0;
  5138. + crypt_virt[i].ctl_flags = CTL_FLAG_USED;
  5139. + spin_unlock_irqrestore(&desc_lock, flags);
  5140. + return crypt_virt +i;
  5141. + } else {
  5142. + spin_unlock_irqrestore(&desc_lock, flags);
  5143. + return NULL;
  5144. + }
  5145. +}
  5146. +
  5147. +static spinlock_t emerg_lock;
  5148. +static struct crypt_ctl *get_crypt_desc_emerg(void)
  5149. +{
  5150. + int i;
  5151. + static int idx = NPE_QLEN;
  5152. + struct crypt_ctl *desc;
  5153. + unsigned long flags;
  5154. +
  5155. + desc = get_crypt_desc();
  5156. + if (desc)
  5157. + return desc;
  5158. + if (unlikely(!crypt_virt))
  5159. + return NULL;
  5160. +
  5161. + spin_lock_irqsave(&emerg_lock, flags);
  5162. + i = idx;
  5163. + if (crypt_virt[i].ctl_flags == CTL_FLAG_UNUSED) {
  5164. + if (++idx >= NPE_QLEN_TOTAL)
  5165. + idx = NPE_QLEN;
  5166. + crypt_virt[i].ctl_flags = CTL_FLAG_USED;
  5167. + spin_unlock_irqrestore(&emerg_lock, flags);
  5168. + return crypt_virt +i;
  5169. + } else {
  5170. + spin_unlock_irqrestore(&emerg_lock, flags);
  5171. + return NULL;
  5172. + }
  5173. +}
  5174. +
  5175. +static void free_buf_chain(struct buffer_desc *buf, u32 phys)
  5176. +{
  5177. + while (buf) {
  5178. + struct buffer_desc *buf1;
  5179. + u32 phys1;
  5180. +
  5181. + buf1 = buf->next;
  5182. + phys1 = buf->phys_next;
  5183. + dma_pool_free(buffer_pool, buf, phys);
  5184. + buf = buf1;
  5185. + phys = phys1;
  5186. + }
  5187. +}
  5188. +
  5189. +static struct tasklet_struct crypto_done_tasklet;
  5190. +
  5191. +static void finish_scattered_hmac(struct crypt_ctl *crypt)
  5192. +{
  5193. + struct aead_request *req = crypt->data.aead_req;
  5194. + struct aead_ctx *req_ctx = aead_request_ctx(req);
  5195. + struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  5196. + int authsize = crypto_aead_authsize(tfm);
  5197. + int decryptlen = req->cryptlen - authsize;
  5198. +
  5199. + if (req_ctx->encrypt) {
  5200. + scatterwalk_map_and_copy(req_ctx->hmac_virt,
  5201. + req->src, decryptlen, authsize, 1);
  5202. + }
  5203. + dma_pool_free(buffer_pool, req_ctx->hmac_virt, crypt->icv_rev_aes);
  5204. +}
  5205. +
  5206. +static void one_packet(dma_addr_t phys)
  5207. +{
  5208. + struct crypt_ctl *crypt;
  5209. + struct ixp_ctx *ctx;
  5210. + int failed;
  5211. + enum dma_data_direction src_direction = DMA_BIDIRECTIONAL;
  5212. +
  5213. + failed = phys & 0x1 ? -EBADMSG : 0;
  5214. + phys &= ~0x3;
  5215. + crypt = crypt_phys2virt(phys);
  5216. +
  5217. + switch (crypt->ctl_flags & CTL_FLAG_MASK) {
  5218. + case CTL_FLAG_PERFORM_AEAD: {
  5219. + struct aead_request *req = crypt->data.aead_req;
  5220. + struct aead_ctx *req_ctx = aead_request_ctx(req);
  5221. + dma_unmap_sg(dev, req->assoc, req_ctx->assoc_nents,
  5222. + DMA_TO_DEVICE);
  5223. + dma_unmap_sg(dev, &req_ctx->ivlist, 1, DMA_BIDIRECTIONAL);
  5224. + dma_unmap_sg(dev, req->src, req_ctx->src_nents,
  5225. + DMA_BIDIRECTIONAL);
  5226. +
  5227. + free_buf_chain(req_ctx->buffer, crypt->src_buf);
  5228. + if (req_ctx->hmac_virt) {
  5229. + finish_scattered_hmac(crypt);
  5230. + }
  5231. + req->base.complete(&req->base, failed);
  5232. + break;
  5233. + }
  5234. + case CTL_FLAG_PERFORM_ABLK: {
  5235. + struct ablkcipher_request *req = crypt->data.ablk_req;
  5236. + struct ablk_ctx *req_ctx = ablkcipher_request_ctx(req);
  5237. + int nents;
  5238. + if (req_ctx->dst) {
  5239. + nents = req_ctx->dst_nents;
  5240. + dma_unmap_sg(dev, req->dst, nents, DMA_FROM_DEVICE);
  5241. + free_buf_chain(req_ctx->dst, crypt->dst_buf);
  5242. + src_direction = DMA_TO_DEVICE;
  5243. + }
  5244. + nents = req_ctx->src_nents;
  5245. + dma_unmap_sg(dev, req->src, nents, src_direction);
  5246. + free_buf_chain(req_ctx->src, crypt->src_buf);
  5247. + req->base.complete(&req->base, failed);
  5248. + break;
  5249. + }
  5250. + case CTL_FLAG_GEN_ICV:
  5251. + ctx = crypto_tfm_ctx(crypt->data.tfm);
  5252. + dma_pool_free(ctx_pool, crypt->regist_ptr,
  5253. + crypt->regist_buf->phys_addr);
  5254. + dma_pool_free(buffer_pool, crypt->regist_buf, crypt->src_buf);
  5255. + if (atomic_dec_and_test(&ctx->configuring))
  5256. + complete(&ctx->completion);
  5257. + break;
  5258. + case CTL_FLAG_GEN_REVAES:
  5259. + ctx = crypto_tfm_ctx(crypt->data.tfm);
  5260. + *(u32*)ctx->decrypt.npe_ctx &= cpu_to_be32(~CIPH_ENCR);
  5261. + if (atomic_dec_and_test(&ctx->configuring))
  5262. + complete(&ctx->completion);
  5263. + break;
  5264. + default:
  5265. + BUG();
  5266. + }
  5267. + crypt->ctl_flags = CTL_FLAG_UNUSED;
  5268. +}
  5269. +
  5270. +static void irqhandler(void *_unused)
  5271. +{
  5272. + tasklet_schedule(&crypto_done_tasklet);
  5273. +}
  5274. +
  5275. +static void crypto_done_action(unsigned long arg)
  5276. +{
  5277. + int i;
  5278. +
  5279. + for(i=0; i<4; i++) {
  5280. + dma_addr_t phys = qmgr_get_entry(RECV_QID);
  5281. + if (!phys)
  5282. + return;
  5283. + one_packet(phys);
  5284. + }
  5285. + tasklet_schedule(&crypto_done_tasklet);
  5286. +}
  5287. +
  5288. +static int init_ixp_crypto(void)
  5289. +{
  5290. + int ret = -ENODEV;
  5291. +
  5292. + if (! ( ~(*IXP4XX_EXP_CFG2) & (IXP4XX_FEATURE_HASH |
  5293. + IXP4XX_FEATURE_AES | IXP4XX_FEATURE_DES))) {
  5294. + printk(KERN_ERR "ixp_crypto: No HW crypto available\n");
  5295. + return ret;
  5296. + }
  5297. + npe_c = npe_request(NPE_ID);
  5298. + if (!npe_c)
  5299. + return ret;
  5300. +
  5301. + if (!npe_running(npe_c)) {
  5302. + npe_load_firmware(npe_c, npe_name(npe_c), dev);
  5303. + }
  5304. +
  5305. + /* buffer_pool will also be used to sometimes store the hmac,
  5306. + * so assure it is large enough
  5307. + */
  5308. + BUILD_BUG_ON(SHA1_DIGEST_SIZE > sizeof(struct buffer_desc));
  5309. + buffer_pool = dma_pool_create("buffer", dev,
  5310. + sizeof(struct buffer_desc), 32, 0);
  5311. + ret = -ENOMEM;
  5312. + if (!buffer_pool) {
  5313. + goto err;
  5314. + }
  5315. + ctx_pool = dma_pool_create("context", dev,
  5316. + NPE_CTX_LEN, 16, 0);
  5317. + if (!ctx_pool) {
  5318. + goto err;
  5319. + }
  5320. + ret = qmgr_request_queue(SEND_QID, NPE_QLEN_TOTAL, 0, 0);
  5321. + if (ret)
  5322. + goto err;
  5323. + ret = qmgr_request_queue(RECV_QID, NPE_QLEN, 0, 0);
  5324. + if (ret) {
  5325. + qmgr_release_queue(SEND_QID);
  5326. + goto err;
  5327. + }
  5328. + qmgr_set_irq(RECV_QID, QUEUE_IRQ_SRC_NOT_EMPTY, irqhandler, NULL);
  5329. + tasklet_init(&crypto_done_tasklet, crypto_done_action, 0);
  5330. +
  5331. + qmgr_enable_irq(RECV_QID);
  5332. + return 0;
  5333. +err:
  5334. + if (ctx_pool)
  5335. + dma_pool_destroy(ctx_pool);
  5336. + if (buffer_pool)
  5337. + dma_pool_destroy(buffer_pool);
  5338. + npe_release(npe_c);
  5339. + return ret;
  5340. +}
  5341. +
  5342. +static void release_ixp_crypto(void)
  5343. +{
  5344. + qmgr_disable_irq(RECV_QID);
  5345. + tasklet_kill(&crypto_done_tasklet);
  5346. +
  5347. + qmgr_release_queue(SEND_QID);
  5348. + qmgr_release_queue(RECV_QID);
  5349. +
  5350. + dma_pool_destroy(ctx_pool);
  5351. + dma_pool_destroy(buffer_pool);
  5352. +
  5353. + npe_release(npe_c);
  5354. +
  5355. + if (crypt_virt) {
  5356. + dma_free_coherent(dev,
  5357. + NPE_QLEN_TOTAL * sizeof( struct crypt_ctl),
  5358. + crypt_virt, crypt_phys);
  5359. + }
  5360. + return;
  5361. +}
  5362. +
  5363. +static void reset_sa_dir(struct ix_sa_dir *dir)
  5364. +{
  5365. + memset(dir->npe_ctx, 0, NPE_CTX_LEN);
  5366. + dir->npe_ctx_idx = 0;
  5367. + dir->npe_mode = 0;
  5368. +}
  5369. +
  5370. +static int init_sa_dir(struct ix_sa_dir *dir)
  5371. +{
  5372. + dir->npe_ctx = dma_pool_alloc(ctx_pool, GFP_KERNEL, &dir->npe_ctx_phys);
  5373. + if (!dir->npe_ctx) {
  5374. + return -ENOMEM;
  5375. + }
  5376. + reset_sa_dir(dir);
  5377. + return 0;
  5378. +}
  5379. +
  5380. +static void free_sa_dir(struct ix_sa_dir *dir)
  5381. +{
  5382. + memset(dir->npe_ctx, 0, NPE_CTX_LEN);
  5383. + dma_pool_free(ctx_pool, dir->npe_ctx, dir->npe_ctx_phys);
  5384. +}
  5385. +
  5386. +static int init_tfm(struct crypto_tfm *tfm)
  5387. +{
  5388. + struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
  5389. + int ret;
  5390. +
  5391. + atomic_set(&ctx->configuring, 0);
  5392. + ret = init_sa_dir(&ctx->encrypt);
  5393. + if (ret)
  5394. + return ret;
  5395. + ret = init_sa_dir(&ctx->decrypt);
  5396. + if (ret) {
  5397. + free_sa_dir(&ctx->encrypt);
  5398. + }
  5399. + return ret;
  5400. +}
  5401. +
  5402. +static int init_tfm_ablk(struct crypto_tfm *tfm)
  5403. +{
  5404. + tfm->crt_ablkcipher.reqsize = sizeof(struct ablk_ctx);
  5405. + return init_tfm(tfm);
  5406. +}
  5407. +
  5408. +static int init_tfm_aead(struct crypto_tfm *tfm)
  5409. +{
  5410. + tfm->crt_aead.reqsize = sizeof(struct aead_ctx);
  5411. + return init_tfm(tfm);
  5412. +}
  5413. +
  5414. +static void exit_tfm(struct crypto_tfm *tfm)
  5415. +{
  5416. + struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
  5417. + free_sa_dir(&ctx->encrypt);
  5418. + free_sa_dir(&ctx->decrypt);
  5419. +}
  5420. +
  5421. +static int register_chain_var(struct crypto_tfm *tfm, u8 xpad, u32 target,
  5422. + int init_len, u32 ctx_addr, const u8 *key, int key_len)
  5423. +{
  5424. + struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
  5425. + struct crypt_ctl *crypt;
  5426. + struct buffer_desc *buf;
  5427. + int i;
  5428. + u8 *pad;
  5429. + u32 pad_phys, buf_phys;
  5430. +
  5431. + BUILD_BUG_ON(NPE_CTX_LEN < HMAC_PAD_BLOCKLEN);
  5432. + pad = dma_pool_alloc(ctx_pool, GFP_KERNEL, &pad_phys);
  5433. + if (!pad)
  5434. + return -ENOMEM;
  5435. + buf = dma_pool_alloc(buffer_pool, GFP_KERNEL, &buf_phys);
  5436. + if (!buf) {
  5437. + dma_pool_free(ctx_pool, pad, pad_phys);
  5438. + return -ENOMEM;
  5439. + }
  5440. + crypt = get_crypt_desc_emerg();
  5441. + if (!crypt) {
  5442. + dma_pool_free(ctx_pool, pad, pad_phys);
  5443. + dma_pool_free(buffer_pool, buf, buf_phys);
  5444. + return -EAGAIN;
  5445. + }
  5446. +
  5447. + memcpy(pad, key, key_len);
  5448. + memset(pad + key_len, 0, HMAC_PAD_BLOCKLEN - key_len);
  5449. + for (i = 0; i < HMAC_PAD_BLOCKLEN; i++) {
  5450. + pad[i] ^= xpad;
  5451. + }
  5452. +
  5453. + crypt->data.tfm = tfm;
  5454. + crypt->regist_ptr = pad;
  5455. + crypt->regist_buf = buf;
  5456. +
  5457. + crypt->auth_offs = 0;
  5458. + crypt->auth_len = HMAC_PAD_BLOCKLEN;
  5459. + crypt->crypto_ctx = ctx_addr;
  5460. + crypt->src_buf = buf_phys;
  5461. + crypt->icv_rev_aes = target;
  5462. + crypt->mode = NPE_OP_HASH_GEN_ICV;
  5463. + crypt->init_len = init_len;
  5464. + crypt->ctl_flags |= CTL_FLAG_GEN_ICV;
  5465. +
  5466. + buf->next = 0;
  5467. + buf->buf_len = HMAC_PAD_BLOCKLEN;
  5468. + buf->pkt_len = 0;
  5469. + buf->phys_addr = pad_phys;
  5470. +
  5471. + atomic_inc(&ctx->configuring);
  5472. + qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
  5473. + BUG_ON(qmgr_stat_overflow(SEND_QID));
  5474. + return 0;
  5475. +}
  5476. +
  5477. +static int setup_auth(struct crypto_tfm *tfm, int encrypt, unsigned authsize,
  5478. + const u8 *key, int key_len, unsigned digest_len)
  5479. +{
  5480. + u32 itarget, otarget, npe_ctx_addr;
  5481. + unsigned char *cinfo;
  5482. + int init_len, ret = 0;
  5483. + u32 cfgword;
  5484. + struct ix_sa_dir *dir;
  5485. + struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
  5486. + const struct ix_hash_algo *algo;
  5487. +
  5488. + dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
  5489. + cinfo = dir->npe_ctx + dir->npe_ctx_idx;
  5490. + algo = ix_hash(tfm);
  5491. +
  5492. + /* write cfg word to cryptinfo */
  5493. + cfgword = algo->cfgword | ( authsize << 6); /* (authsize/4) << 8 */
  5494. + *(u32*)cinfo = cpu_to_be32(cfgword);
  5495. + cinfo += sizeof(cfgword);
  5496. +
  5497. + /* write ICV to cryptinfo */
  5498. + memcpy(cinfo, algo->icv, digest_len);
  5499. + cinfo += digest_len;
  5500. +
  5501. + itarget = dir->npe_ctx_phys + dir->npe_ctx_idx
  5502. + + sizeof(algo->cfgword);
  5503. + otarget = itarget + digest_len;
  5504. + init_len = cinfo - (dir->npe_ctx + dir->npe_ctx_idx);
  5505. + npe_ctx_addr = dir->npe_ctx_phys + dir->npe_ctx_idx;
  5506. +
  5507. + dir->npe_ctx_idx += init_len;
  5508. + dir->npe_mode |= NPE_OP_HASH_ENABLE;
  5509. +
  5510. + if (!encrypt)
  5511. + dir->npe_mode |= NPE_OP_HASH_VERIFY;
  5512. +
  5513. + ret = register_chain_var(tfm, HMAC_OPAD_VALUE, otarget,
  5514. + init_len, npe_ctx_addr, key, key_len);
  5515. + if (ret)
  5516. + return ret;
  5517. + return register_chain_var(tfm, HMAC_IPAD_VALUE, itarget,
  5518. + init_len, npe_ctx_addr, key, key_len);
  5519. +}
  5520. +
  5521. +static int gen_rev_aes_key(struct crypto_tfm *tfm)
  5522. +{
  5523. + struct crypt_ctl *crypt;
  5524. + struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
  5525. + struct ix_sa_dir *dir = &ctx->decrypt;
  5526. +
  5527. + crypt = get_crypt_desc_emerg();
  5528. + if (!crypt) {
  5529. + return -EAGAIN;
  5530. + }
  5531. + *(u32*)dir->npe_ctx |= cpu_to_be32(CIPH_ENCR);
  5532. +
  5533. + crypt->data.tfm = tfm;
  5534. + crypt->crypt_offs = 0;
  5535. + crypt->crypt_len = AES_BLOCK128;
  5536. + crypt->src_buf = 0;
  5537. + crypt->crypto_ctx = dir->npe_ctx_phys;
  5538. + crypt->icv_rev_aes = dir->npe_ctx_phys + sizeof(u32);
  5539. + crypt->mode = NPE_OP_ENC_GEN_KEY;
  5540. + crypt->init_len = dir->npe_ctx_idx;
  5541. + crypt->ctl_flags |= CTL_FLAG_GEN_REVAES;
  5542. +
  5543. + atomic_inc(&ctx->configuring);
  5544. + qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
  5545. + BUG_ON(qmgr_stat_overflow(SEND_QID));
  5546. + return 0;
  5547. +}
  5548. +
  5549. +static int setup_cipher(struct crypto_tfm *tfm, int encrypt,
  5550. + const u8 *key, int key_len)
  5551. +{
  5552. + u8 *cinfo;
  5553. + u32 cipher_cfg;
  5554. + u32 keylen_cfg = 0;
  5555. + struct ix_sa_dir *dir;
  5556. + struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
  5557. + u32 *flags = &tfm->crt_flags;
  5558. +
  5559. + dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
  5560. + cinfo = dir->npe_ctx;
  5561. +
  5562. + if (encrypt) {
  5563. + cipher_cfg = cipher_cfg_enc(tfm);
  5564. + dir->npe_mode |= NPE_OP_CRYPT_ENCRYPT;
  5565. + } else {
  5566. + cipher_cfg = cipher_cfg_dec(tfm);
  5567. + }
  5568. + if (cipher_cfg & MOD_AES) {
  5569. + switch (key_len) {
  5570. + case 16: keylen_cfg = MOD_AES128 | KEYLEN_128; break;
  5571. + case 24: keylen_cfg = MOD_AES192 | KEYLEN_192; break;
  5572. + case 32: keylen_cfg = MOD_AES256 | KEYLEN_256; break;
  5573. + default:
  5574. + *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  5575. + return -EINVAL;
  5576. + }
  5577. + cipher_cfg |= keylen_cfg;
  5578. + } else if (cipher_cfg & MOD_3DES) {
  5579. + const u32 *K = (const u32 *)key;
  5580. + if (unlikely(!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
  5581. + !((K[2] ^ K[4]) | (K[3] ^ K[5]))))
  5582. + {
  5583. + *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
  5584. + return -EINVAL;
  5585. + }
  5586. + } else {
  5587. + u32 tmp[DES_EXPKEY_WORDS];
  5588. + if (des_ekey(tmp, key) == 0) {
  5589. + *flags |= CRYPTO_TFM_RES_WEAK_KEY;
  5590. + }
  5591. + }
  5592. + /* write cfg word to cryptinfo */
  5593. + *(u32*)cinfo = cpu_to_be32(cipher_cfg);
  5594. + cinfo += sizeof(cipher_cfg);
  5595. +
  5596. + /* write cipher key to cryptinfo */
  5597. + memcpy(cinfo, key, key_len);
  5598. + /* NPE wants keylen set to DES3_EDE_KEY_SIZE even for single DES */
  5599. + if (key_len < DES3_EDE_KEY_SIZE && !(cipher_cfg & MOD_AES)) {
  5600. + memset(cinfo + key_len, 0, DES3_EDE_KEY_SIZE -key_len);
  5601. + key_len = DES3_EDE_KEY_SIZE;
  5602. + }
  5603. + dir->npe_ctx_idx = sizeof(cipher_cfg) + key_len;
  5604. + dir->npe_mode |= NPE_OP_CRYPT_ENABLE;
  5605. + if ((cipher_cfg & MOD_AES) && !encrypt) {
  5606. + return gen_rev_aes_key(tfm);
  5607. + }
  5608. + return 0;
  5609. +}
  5610. +
  5611. +static int count_sg(struct scatterlist *sg, int nbytes)
  5612. +{
  5613. + int i;
  5614. + for (i = 0; nbytes > 0; i++, sg = sg_next(sg))
  5615. + nbytes -= sg->length;
  5616. + return i;
  5617. +}
  5618. +
  5619. +static struct buffer_desc *chainup_buffers(struct scatterlist *sg,
  5620. + unsigned nbytes, struct buffer_desc *buf, gfp_t flags)
  5621. +{
  5622. + int nents = 0;
  5623. +
  5624. + while (nbytes > 0) {
  5625. + struct buffer_desc *next_buf;
  5626. + u32 next_buf_phys;
  5627. + unsigned len = min(nbytes, sg_dma_len(sg));
  5628. +
  5629. + nents++;
  5630. + nbytes -= len;
  5631. + if (!buf->phys_addr) {
  5632. + buf->phys_addr = sg_dma_address(sg);
  5633. + buf->buf_len = len;
  5634. + buf->next = NULL;
  5635. + buf->phys_next = 0;
  5636. + goto next;
  5637. + }
  5638. + /* Two consecutive chunks on one page may be handled by the old
  5639. + * buffer descriptor, increased by the length of the new one
  5640. + */
  5641. + if (sg_dma_address(sg) == buf->phys_addr + buf->buf_len) {
  5642. + buf->buf_len += len;
  5643. + goto next;
  5644. + }
  5645. + next_buf = dma_pool_alloc(buffer_pool, flags, &next_buf_phys);
  5646. + if (!next_buf)
  5647. + return NULL;
  5648. + buf->next = next_buf;
  5649. + buf->phys_next = next_buf_phys;
  5650. +
  5651. + buf = next_buf;
  5652. + buf->next = NULL;
  5653. + buf->phys_next = 0;
  5654. + buf->phys_addr = sg_dma_address(sg);
  5655. + buf->buf_len = len;
  5656. +next:
  5657. + if (nbytes > 0) {
  5658. + sg = sg_next(sg);
  5659. + }
  5660. + }
  5661. + return buf;
  5662. +}
  5663. +
  5664. +static int ablk_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  5665. + unsigned int key_len)
  5666. +{
  5667. + struct ixp_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  5668. + u32 *flags = &tfm->base.crt_flags;
  5669. + int ret;
  5670. +
  5671. + init_completion(&ctx->completion);
  5672. + atomic_inc(&ctx->configuring);
  5673. +
  5674. + reset_sa_dir(&ctx->encrypt);
  5675. + reset_sa_dir(&ctx->decrypt);
  5676. +
  5677. + ctx->encrypt.npe_mode = NPE_OP_HMAC_DISABLE;
  5678. + ctx->decrypt.npe_mode = NPE_OP_HMAC_DISABLE;
  5679. +
  5680. + ret = setup_cipher(&tfm->base, 0, key, key_len);
  5681. + if (ret)
  5682. + goto out;
  5683. + ret = setup_cipher(&tfm->base, 1, key, key_len);
  5684. + if (ret)
  5685. + goto out;
  5686. +
  5687. + if (*flags & CRYPTO_TFM_RES_WEAK_KEY) {
  5688. + if (*flags & CRYPTO_TFM_REQ_WEAK_KEY) {
  5689. + ret = -EINVAL;
  5690. + } else {
  5691. + *flags &= ~CRYPTO_TFM_RES_WEAK_KEY;
  5692. + }
  5693. + }
  5694. +out:
  5695. + if (!atomic_dec_and_test(&ctx->configuring))
  5696. + wait_for_completion(&ctx->completion);
  5697. + return ret;
  5698. +}
  5699. +
  5700. +static int ablk_rfc3686_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  5701. + unsigned int key_len)
  5702. +{
  5703. + struct ixp_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  5704. +
  5705. + /* the nonce is stored in bytes at end of key */
  5706. + if (key_len < CTR_RFC3686_NONCE_SIZE)
  5707. + return -EINVAL;
  5708. +
  5709. + memcpy(ctx->nonce, key + (key_len - CTR_RFC3686_NONCE_SIZE),
  5710. + CTR_RFC3686_NONCE_SIZE);
  5711. +
  5712. + key_len -= CTR_RFC3686_NONCE_SIZE;
  5713. + return ablk_setkey(tfm, key, key_len);
  5714. +}
  5715. +
  5716. +static int ablk_perform(struct ablkcipher_request *req, int encrypt)
  5717. +{
  5718. + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  5719. + struct ixp_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  5720. + unsigned ivsize = crypto_ablkcipher_ivsize(tfm);
  5721. + int ret = -ENOMEM;
  5722. + struct ix_sa_dir *dir;
  5723. + struct crypt_ctl *crypt;
  5724. + unsigned int nbytes = req->nbytes, nents;
  5725. + enum dma_data_direction src_direction = DMA_BIDIRECTIONAL;
  5726. + struct ablk_ctx *req_ctx = ablkcipher_request_ctx(req);
  5727. + gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
  5728. + GFP_KERNEL : GFP_ATOMIC;
  5729. +
  5730. + if (qmgr_stat_full(SEND_QID))
  5731. + return -EAGAIN;
  5732. + if (atomic_read(&ctx->configuring))
  5733. + return -EAGAIN;
  5734. +
  5735. + dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
  5736. +
  5737. + crypt = get_crypt_desc();
  5738. + if (!crypt)
  5739. + return ret;
  5740. +
  5741. + crypt->data.ablk_req = req;
  5742. + crypt->crypto_ctx = dir->npe_ctx_phys;
  5743. + crypt->mode = dir->npe_mode;
  5744. + crypt->init_len = dir->npe_ctx_idx;
  5745. +
  5746. + crypt->crypt_offs = 0;
  5747. + crypt->crypt_len = nbytes;
  5748. +
  5749. + BUG_ON(ivsize && !req->info);
  5750. + memcpy(crypt->iv, req->info, ivsize);
  5751. + if (req->src != req->dst) {
  5752. + crypt->mode |= NPE_OP_NOT_IN_PLACE;
  5753. + nents = count_sg(req->dst, nbytes);
  5754. + /* This was never tested by Intel
  5755. + * for more than one dst buffer, I think. */
  5756. + BUG_ON(nents != 1);
  5757. + req_ctx->dst_nents = nents;
  5758. + dma_map_sg(dev, req->dst, nents, DMA_FROM_DEVICE);
  5759. + req_ctx->dst = dma_pool_alloc(buffer_pool, flags,&crypt->dst_buf);
  5760. + if (!req_ctx->dst)
  5761. + goto unmap_sg_dest;
  5762. + req_ctx->dst->phys_addr = 0;
  5763. + if (!chainup_buffers(req->dst, nbytes, req_ctx->dst, flags))
  5764. + goto free_buf_dest;
  5765. + src_direction = DMA_TO_DEVICE;
  5766. + } else {
  5767. + req_ctx->dst = NULL;
  5768. + req_ctx->dst_nents = 0;
  5769. + }
  5770. + nents = count_sg(req->src, nbytes);
  5771. + req_ctx->src_nents = nents;
  5772. + dma_map_sg(dev, req->src, nents, src_direction);
  5773. +
  5774. + req_ctx->src = dma_pool_alloc(buffer_pool, flags, &crypt->src_buf);
  5775. + if (!req_ctx->src)
  5776. + goto unmap_sg_src;
  5777. + req_ctx->src->phys_addr = 0;
  5778. + if (!chainup_buffers(req->src, nbytes, req_ctx->src, flags))
  5779. + goto free_buf_src;
  5780. +
  5781. + crypt->ctl_flags |= CTL_FLAG_PERFORM_ABLK;
  5782. + qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
  5783. + BUG_ON(qmgr_stat_overflow(SEND_QID));
  5784. + return -EINPROGRESS;
  5785. +
  5786. +free_buf_src:
  5787. + free_buf_chain(req_ctx->src, crypt->src_buf);
  5788. +unmap_sg_src:
  5789. + dma_unmap_sg(dev, req->src, req_ctx->src_nents, src_direction);
  5790. +free_buf_dest:
  5791. + if (req->src != req->dst) {
  5792. + free_buf_chain(req_ctx->dst, crypt->dst_buf);
  5793. +unmap_sg_dest:
  5794. + dma_unmap_sg(dev, req->src, req_ctx->dst_nents,
  5795. + DMA_FROM_DEVICE);
  5796. + }
  5797. + crypt->ctl_flags = CTL_FLAG_UNUSED;
  5798. + return ret;
  5799. +}
  5800. +
  5801. +static int ablk_encrypt(struct ablkcipher_request *req)
  5802. +{
  5803. + return ablk_perform(req, 1);
  5804. +}
  5805. +
  5806. +static int ablk_decrypt(struct ablkcipher_request *req)
  5807. +{
  5808. + return ablk_perform(req, 0);
  5809. +}
  5810. +
  5811. +static int ablk_rfc3686_crypt(struct ablkcipher_request *req)
  5812. +{
  5813. + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  5814. + struct ixp_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  5815. + u8 iv[CTR_RFC3686_BLOCK_SIZE];
  5816. + u8 *info = req->info;
  5817. + int ret;
  5818. +
  5819. + /* set up counter block */
  5820. + memcpy(iv, ctx->nonce, CTR_RFC3686_NONCE_SIZE);
  5821. + memcpy(iv + CTR_RFC3686_NONCE_SIZE, info, CTR_RFC3686_IV_SIZE);
  5822. +
  5823. + /* initialize counter portion of counter block */
  5824. + *(__be32 *)(iv + CTR_RFC3686_NONCE_SIZE + CTR_RFC3686_IV_SIZE) =
  5825. + cpu_to_be32(1);
  5826. +
  5827. + req->info = iv;
  5828. + ret = ablk_perform(req, 1);
  5829. + req->info = info;
  5830. + return ret;
  5831. +}
  5832. +
  5833. +static int hmac_inconsistent(struct scatterlist *sg, unsigned start,
  5834. + unsigned int nbytes)
  5835. +{
  5836. + int offset = 0;
  5837. +
  5838. + if (!nbytes)
  5839. + return 0;
  5840. +
  5841. + for (;;) {
  5842. + if (start < offset + sg->length)
  5843. + break;
  5844. +
  5845. + offset += sg->length;
  5846. + sg = sg_next(sg);
  5847. + }
  5848. + return (start + nbytes > offset + sg->length);
  5849. +}
  5850. +
  5851. +static int aead_perform(struct aead_request *req, int encrypt,
  5852. + int cryptoffset, int eff_cryptlen, u8 *iv)
  5853. +{
  5854. + struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  5855. + struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
  5856. + unsigned ivsize = crypto_aead_ivsize(tfm);
  5857. + unsigned authsize = crypto_aead_authsize(tfm);
  5858. + int ret = -ENOMEM;
  5859. + struct ix_sa_dir *dir;
  5860. + struct crypt_ctl *crypt;
  5861. + unsigned int cryptlen, nents;
  5862. + struct buffer_desc *buf;
  5863. + struct aead_ctx *req_ctx = aead_request_ctx(req);
  5864. + gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
  5865. + GFP_KERNEL : GFP_ATOMIC;
  5866. +
  5867. + if (qmgr_stat_full(SEND_QID))
  5868. + return -EAGAIN;
  5869. + if (atomic_read(&ctx->configuring))
  5870. + return -EAGAIN;
  5871. +
  5872. + if (encrypt) {
  5873. + dir = &ctx->encrypt;
  5874. + cryptlen = req->cryptlen;
  5875. + } else {
  5876. + dir = &ctx->decrypt;
  5877. + /* req->cryptlen includes the authsize when decrypting */
  5878. + cryptlen = req->cryptlen -authsize;
  5879. + eff_cryptlen -= authsize;
  5880. + }
  5881. + crypt = get_crypt_desc();
  5882. + if (!crypt)
  5883. + return ret;
  5884. +
  5885. + crypt->data.aead_req = req;
  5886. + crypt->crypto_ctx = dir->npe_ctx_phys;
  5887. + crypt->mode = dir->npe_mode;
  5888. + crypt->init_len = dir->npe_ctx_idx;
  5889. +
  5890. + crypt->crypt_offs = cryptoffset;
  5891. + crypt->crypt_len = eff_cryptlen;
  5892. +
  5893. + crypt->auth_offs = 0;
  5894. + crypt->auth_len = req->assoclen + ivsize + cryptlen;
  5895. + BUG_ON(ivsize && !req->iv);
  5896. + memcpy(crypt->iv, req->iv, ivsize);
  5897. +
  5898. + if (req->src != req->dst) {
  5899. + BUG(); /* -ENOTSUP because of my lazyness */
  5900. + }
  5901. +
  5902. + req_ctx->buffer = dma_pool_alloc(buffer_pool, flags, &crypt->src_buf);
  5903. + if (!req_ctx->buffer)
  5904. + goto out;
  5905. + req_ctx->buffer->phys_addr = 0;
  5906. + /* ASSOC data */
  5907. + nents = count_sg(req->assoc, req->assoclen);
  5908. + req_ctx->assoc_nents = nents;
  5909. + dma_map_sg(dev, req->assoc, nents, DMA_TO_DEVICE);
  5910. + buf = chainup_buffers(req->assoc, req->assoclen, req_ctx->buffer,flags);
  5911. + if (!buf)
  5912. + goto unmap_sg_assoc;
  5913. + /* IV */
  5914. + sg_init_table(&req_ctx->ivlist, 1);
  5915. + sg_set_buf(&req_ctx->ivlist, iv, ivsize);
  5916. + dma_map_sg(dev, &req_ctx->ivlist, 1, DMA_BIDIRECTIONAL);
  5917. + buf = chainup_buffers(&req_ctx->ivlist, ivsize, buf, flags);
  5918. + if (!buf)
  5919. + goto unmap_sg_iv;
  5920. + if (unlikely(hmac_inconsistent(req->src, cryptlen, authsize))) {
  5921. + /* The 12 hmac bytes are scattered,
  5922. + * we need to copy them into a safe buffer */
  5923. + req_ctx->hmac_virt = dma_pool_alloc(buffer_pool, flags,
  5924. + &crypt->icv_rev_aes);
  5925. + if (unlikely(!req_ctx->hmac_virt))
  5926. + goto unmap_sg_iv;
  5927. + if (!encrypt) {
  5928. + scatterwalk_map_and_copy(req_ctx->hmac_virt,
  5929. + req->src, cryptlen, authsize, 0);
  5930. + }
  5931. + req_ctx->encrypt = encrypt;
  5932. + } else {
  5933. + req_ctx->hmac_virt = NULL;
  5934. + }
  5935. + /* Crypt */
  5936. + nents = count_sg(req->src, cryptlen + authsize);
  5937. + req_ctx->src_nents = nents;
  5938. + dma_map_sg(dev, req->src, nents, DMA_BIDIRECTIONAL);
  5939. + buf = chainup_buffers(req->src, cryptlen + authsize, buf, flags);
  5940. + if (!buf)
  5941. + goto unmap_sg_src;
  5942. + if (!req_ctx->hmac_virt) {
  5943. + crypt->icv_rev_aes = buf->phys_addr + buf->buf_len - authsize;
  5944. + }
  5945. + crypt->ctl_flags |= CTL_FLAG_PERFORM_AEAD;
  5946. + qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
  5947. + BUG_ON(qmgr_stat_overflow(SEND_QID));
  5948. + return -EINPROGRESS;
  5949. +unmap_sg_src:
  5950. + dma_unmap_sg(dev, req->src, req_ctx->src_nents, DMA_BIDIRECTIONAL);
  5951. + if (req_ctx->hmac_virt) {
  5952. + dma_pool_free(buffer_pool, req_ctx->hmac_virt,
  5953. + crypt->icv_rev_aes);
  5954. + }
  5955. +unmap_sg_iv:
  5956. + dma_unmap_sg(dev, &req_ctx->ivlist, 1, DMA_BIDIRECTIONAL);
  5957. +unmap_sg_assoc:
  5958. + dma_unmap_sg(dev, req->assoc, req_ctx->assoc_nents, DMA_TO_DEVICE);
  5959. + free_buf_chain(req_ctx->buffer, crypt->src_buf);
  5960. +out:
  5961. + crypt->ctl_flags = CTL_FLAG_UNUSED;
  5962. + return ret;
  5963. +}
  5964. +
  5965. +static int aead_setup(struct crypto_aead *tfm, unsigned int authsize)
  5966. +{
  5967. + struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
  5968. + u32 *flags = &tfm->base.crt_flags;
  5969. + unsigned digest_len = crypto_aead_alg(tfm)->maxauthsize;
  5970. + int ret;
  5971. +
  5972. + if (!ctx->enckey_len && !ctx->authkey_len)
  5973. + return 0;
  5974. + init_completion(&ctx->completion);
  5975. + atomic_inc(&ctx->configuring);
  5976. +
  5977. + reset_sa_dir(&ctx->encrypt);
  5978. + reset_sa_dir(&ctx->decrypt);
  5979. +
  5980. + ret = setup_cipher(&tfm->base, 0, ctx->enckey, ctx->enckey_len);
  5981. + if (ret)
  5982. + goto out;
  5983. + ret = setup_cipher(&tfm->base, 1, ctx->enckey, ctx->enckey_len);
  5984. + if (ret)
  5985. + goto out;
  5986. + ret = setup_auth(&tfm->base, 0, authsize, ctx->authkey,
  5987. + ctx->authkey_len, digest_len);
  5988. + if (ret)
  5989. + goto out;
  5990. + ret = setup_auth(&tfm->base, 1, authsize, ctx->authkey,
  5991. + ctx->authkey_len, digest_len);
  5992. + if (ret)
  5993. + goto out;
  5994. +
  5995. + if (*flags & CRYPTO_TFM_RES_WEAK_KEY) {
  5996. + if (*flags & CRYPTO_TFM_REQ_WEAK_KEY) {
  5997. + ret = -EINVAL;
  5998. + goto out;
  5999. + } else {
  6000. + *flags &= ~CRYPTO_TFM_RES_WEAK_KEY;
  6001. + }
  6002. + }
  6003. +out:
  6004. + if (!atomic_dec_and_test(&ctx->configuring))
  6005. + wait_for_completion(&ctx->completion);
  6006. + return ret;
  6007. +}
  6008. +
  6009. +static int aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
  6010. +{
  6011. + int max = crypto_aead_alg(tfm)->maxauthsize >> 2;
  6012. +
  6013. + if ((authsize>>2) < 1 || (authsize>>2) > max || (authsize & 3))
  6014. + return -EINVAL;
  6015. + return aead_setup(tfm, authsize);
  6016. +}
  6017. +
  6018. +static int aead_setkey(struct crypto_aead *tfm, const u8 *key,
  6019. + unsigned int keylen)
  6020. +{
  6021. + struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
  6022. + struct rtattr *rta = (struct rtattr *)key;
  6023. + struct crypto_authenc_key_param *param;
  6024. +
  6025. + if (!RTA_OK(rta, keylen))
  6026. + goto badkey;
  6027. + if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
  6028. + goto badkey;
  6029. + if (RTA_PAYLOAD(rta) < sizeof(*param))
  6030. + goto badkey;
  6031. +
  6032. + param = RTA_DATA(rta);
  6033. + ctx->enckey_len = be32_to_cpu(param->enckeylen);
  6034. +
  6035. + key += RTA_ALIGN(rta->rta_len);
  6036. + keylen -= RTA_ALIGN(rta->rta_len);
  6037. +
  6038. + if (keylen < ctx->enckey_len)
  6039. + goto badkey;
  6040. +
  6041. + ctx->authkey_len = keylen - ctx->enckey_len;
  6042. + memcpy(ctx->enckey, key + ctx->authkey_len, ctx->enckey_len);
  6043. + memcpy(ctx->authkey, key, ctx->authkey_len);
  6044. +
  6045. + return aead_setup(tfm, crypto_aead_authsize(tfm));
  6046. +badkey:
  6047. + ctx->enckey_len = 0;
  6048. + crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  6049. + return -EINVAL;
  6050. +}
  6051. +
  6052. +static int aead_encrypt(struct aead_request *req)
  6053. +{
  6054. + unsigned ivsize = crypto_aead_ivsize(crypto_aead_reqtfm(req));
  6055. + return aead_perform(req, 1, req->assoclen + ivsize,
  6056. + req->cryptlen, req->iv);
  6057. +}
  6058. +
  6059. +static int aead_decrypt(struct aead_request *req)
  6060. +{
  6061. + unsigned ivsize = crypto_aead_ivsize(crypto_aead_reqtfm(req));
  6062. + return aead_perform(req, 0, req->assoclen + ivsize,
  6063. + req->cryptlen, req->iv);
  6064. +}
  6065. +
  6066. +static int aead_givencrypt(struct aead_givcrypt_request *req)
  6067. +{
  6068. + struct crypto_aead *tfm = aead_givcrypt_reqtfm(req);
  6069. + struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
  6070. + unsigned len, ivsize = crypto_aead_ivsize(tfm);
  6071. + __be64 seq;
  6072. +
  6073. + /* copied from eseqiv.c */
  6074. + if (!ctx->salted) {
  6075. + get_random_bytes(ctx->salt, ivsize);
  6076. + ctx->salted = 1;
  6077. + }
  6078. + memcpy(req->areq.iv, ctx->salt, ivsize);
  6079. + len = ivsize;
  6080. + if (ivsize > sizeof(u64)) {
  6081. + memset(req->giv, 0, ivsize - sizeof(u64));
  6082. + len = sizeof(u64);
  6083. + }
  6084. + seq = cpu_to_be64(req->seq);
  6085. + memcpy(req->giv + ivsize - len, &seq, len);
  6086. + return aead_perform(&req->areq, 1, req->areq.assoclen,
  6087. + req->areq.cryptlen +ivsize, req->giv);
  6088. +}
  6089. +
  6090. +static struct ixp_alg ixp4xx_algos[] = {
  6091. +{
  6092. + .crypto = {
  6093. + .cra_name = "cbc(des)",
  6094. + .cra_blocksize = DES_BLOCK_SIZE,
  6095. + .cra_u = { .ablkcipher = {
  6096. + .min_keysize = DES_KEY_SIZE,
  6097. + .max_keysize = DES_KEY_SIZE,
  6098. + .ivsize = DES_BLOCK_SIZE,
  6099. + .geniv = "eseqiv",
  6100. + }
  6101. + }
  6102. + },
  6103. + .cfg_enc = CIPH_ENCR | MOD_DES | MOD_CBC_ENC | KEYLEN_192,
  6104. + .cfg_dec = CIPH_DECR | MOD_DES | MOD_CBC_DEC | KEYLEN_192,
  6105. +
  6106. +}, {
  6107. + .crypto = {
  6108. + .cra_name = "ecb(des)",
  6109. + .cra_blocksize = DES_BLOCK_SIZE,
  6110. + .cra_u = { .ablkcipher = {
  6111. + .min_keysize = DES_KEY_SIZE,
  6112. + .max_keysize = DES_KEY_SIZE,
  6113. + }
  6114. + }
  6115. + },
  6116. + .cfg_enc = CIPH_ENCR | MOD_DES | MOD_ECB | KEYLEN_192,
  6117. + .cfg_dec = CIPH_DECR | MOD_DES | MOD_ECB | KEYLEN_192,
  6118. +}, {
  6119. + .crypto = {
  6120. + .cra_name = "cbc(des3_ede)",
  6121. + .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  6122. + .cra_u = { .ablkcipher = {
  6123. + .min_keysize = DES3_EDE_KEY_SIZE,
  6124. + .max_keysize = DES3_EDE_KEY_SIZE,
  6125. + .ivsize = DES3_EDE_BLOCK_SIZE,
  6126. + .geniv = "eseqiv",
  6127. + }
  6128. + }
  6129. + },
  6130. + .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_CBC_ENC | KEYLEN_192,
  6131. + .cfg_dec = CIPH_DECR | MOD_3DES | MOD_CBC_DEC | KEYLEN_192,
  6132. +}, {
  6133. + .crypto = {
  6134. + .cra_name = "ecb(des3_ede)",
  6135. + .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  6136. + .cra_u = { .ablkcipher = {
  6137. + .min_keysize = DES3_EDE_KEY_SIZE,
  6138. + .max_keysize = DES3_EDE_KEY_SIZE,
  6139. + }
  6140. + }
  6141. + },
  6142. + .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_ECB | KEYLEN_192,
  6143. + .cfg_dec = CIPH_DECR | MOD_3DES | MOD_ECB | KEYLEN_192,
  6144. +}, {
  6145. + .crypto = {
  6146. + .cra_name = "cbc(aes)",
  6147. + .cra_blocksize = AES_BLOCK_SIZE,
  6148. + .cra_u = { .ablkcipher = {
  6149. + .min_keysize = AES_MIN_KEY_SIZE,
  6150. + .max_keysize = AES_MAX_KEY_SIZE,
  6151. + .ivsize = AES_BLOCK_SIZE,
  6152. + .geniv = "eseqiv",
  6153. + }
  6154. + }
  6155. + },
  6156. + .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
  6157. + .cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
  6158. +}, {
  6159. + .crypto = {
  6160. + .cra_name = "ecb(aes)",
  6161. + .cra_blocksize = AES_BLOCK_SIZE,
  6162. + .cra_u = { .ablkcipher = {
  6163. + .min_keysize = AES_MIN_KEY_SIZE,
  6164. + .max_keysize = AES_MAX_KEY_SIZE,
  6165. + }
  6166. + }
  6167. + },
  6168. + .cfg_enc = CIPH_ENCR | MOD_AES | MOD_ECB,
  6169. + .cfg_dec = CIPH_DECR | MOD_AES | MOD_ECB,
  6170. +}, {
  6171. + .crypto = {
  6172. + .cra_name = "ctr(aes)",
  6173. + .cra_blocksize = AES_BLOCK_SIZE,
  6174. + .cra_u = { .ablkcipher = {
  6175. + .min_keysize = AES_MIN_KEY_SIZE,
  6176. + .max_keysize = AES_MAX_KEY_SIZE,
  6177. + .ivsize = AES_BLOCK_SIZE,
  6178. + .geniv = "eseqiv",
  6179. + }
  6180. + }
  6181. + },
  6182. + .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CTR,
  6183. + .cfg_dec = CIPH_ENCR | MOD_AES | MOD_CTR,
  6184. +}, {
  6185. + .crypto = {
  6186. + .cra_name = "rfc3686(ctr(aes))",
  6187. + .cra_blocksize = AES_BLOCK_SIZE,
  6188. + .cra_u = { .ablkcipher = {
  6189. + .min_keysize = AES_MIN_KEY_SIZE,
  6190. + .max_keysize = AES_MAX_KEY_SIZE,
  6191. + .ivsize = AES_BLOCK_SIZE,
  6192. + .geniv = "eseqiv",
  6193. + .setkey = ablk_rfc3686_setkey,
  6194. + .encrypt = ablk_rfc3686_crypt,
  6195. + .decrypt = ablk_rfc3686_crypt }
  6196. + }
  6197. + },
  6198. + .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CTR,
  6199. + .cfg_dec = CIPH_ENCR | MOD_AES | MOD_CTR,
  6200. +}, {
  6201. + .crypto = {
  6202. + .cra_name = "authenc(hmac(md5),cbc(des))",
  6203. + .cra_blocksize = DES_BLOCK_SIZE,
  6204. + .cra_u = { .aead = {
  6205. + .ivsize = DES_BLOCK_SIZE,
  6206. + .maxauthsize = MD5_DIGEST_SIZE,
  6207. + }
  6208. + }
  6209. + },
  6210. + .hash = &hash_alg_md5,
  6211. + .cfg_enc = CIPH_ENCR | MOD_DES | MOD_CBC_ENC | KEYLEN_192,
  6212. + .cfg_dec = CIPH_DECR | MOD_DES | MOD_CBC_DEC | KEYLEN_192,
  6213. +}, {
  6214. + .crypto = {
  6215. + .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
  6216. + .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  6217. + .cra_u = { .aead = {
  6218. + .ivsize = DES3_EDE_BLOCK_SIZE,
  6219. + .maxauthsize = MD5_DIGEST_SIZE,
  6220. + }
  6221. + }
  6222. + },
  6223. + .hash = &hash_alg_md5,
  6224. + .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_CBC_ENC | KEYLEN_192,
  6225. + .cfg_dec = CIPH_DECR | MOD_3DES | MOD_CBC_DEC | KEYLEN_192,
  6226. +}, {
  6227. + .crypto = {
  6228. + .cra_name = "authenc(hmac(sha1),cbc(des))",
  6229. + .cra_blocksize = DES_BLOCK_SIZE,
  6230. + .cra_u = { .aead = {
  6231. + .ivsize = DES_BLOCK_SIZE,
  6232. + .maxauthsize = SHA1_DIGEST_SIZE,
  6233. + }
  6234. + }
  6235. + },
  6236. + .hash = &hash_alg_sha1,
  6237. + .cfg_enc = CIPH_ENCR | MOD_DES | MOD_CBC_ENC | KEYLEN_192,
  6238. + .cfg_dec = CIPH_DECR | MOD_DES | MOD_CBC_DEC | KEYLEN_192,
  6239. +}, {
  6240. + .crypto = {
  6241. + .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
  6242. + .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  6243. + .cra_u = { .aead = {
  6244. + .ivsize = DES3_EDE_BLOCK_SIZE,
  6245. + .maxauthsize = SHA1_DIGEST_SIZE,
  6246. + }
  6247. + }
  6248. + },
  6249. + .hash = &hash_alg_sha1,
  6250. + .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_CBC_ENC | KEYLEN_192,
  6251. + .cfg_dec = CIPH_DECR | MOD_3DES | MOD_CBC_DEC | KEYLEN_192,
  6252. +}, {
  6253. + .crypto = {
  6254. + .cra_name = "authenc(hmac(md5),cbc(aes))",
  6255. + .cra_blocksize = AES_BLOCK_SIZE,
  6256. + .cra_u = { .aead = {
  6257. + .ivsize = AES_BLOCK_SIZE,
  6258. + .maxauthsize = MD5_DIGEST_SIZE,
  6259. + }
  6260. + }
  6261. + },
  6262. + .hash = &hash_alg_md5,
  6263. + .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
  6264. + .cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
  6265. +}, {
  6266. + .crypto = {
  6267. + .cra_name = "authenc(hmac(sha1),cbc(aes))",
  6268. + .cra_blocksize = AES_BLOCK_SIZE,
  6269. + .cra_u = { .aead = {
  6270. + .ivsize = AES_BLOCK_SIZE,
  6271. + .maxauthsize = SHA1_DIGEST_SIZE,
  6272. + }
  6273. + }
  6274. + },
  6275. + .hash = &hash_alg_sha1,
  6276. + .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
  6277. + .cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
  6278. +} };
  6279. +
  6280. +#define IXP_POSTFIX "-ixp4xx"
  6281. +static int __init ixp_module_init(void)
  6282. +{
  6283. + int num = ARRAY_SIZE(ixp4xx_algos);
  6284. + int i,err ;
  6285. +
  6286. + if (platform_device_register(&pseudo_dev))
  6287. + return -ENODEV;
  6288. +
  6289. + spin_lock_init(&desc_lock);
  6290. + spin_lock_init(&emerg_lock);
  6291. +
  6292. + err = init_ixp_crypto();
  6293. + if (err) {
  6294. + platform_device_unregister(&pseudo_dev);
  6295. + return err;
  6296. + }
  6297. + for (i=0; i< num; i++) {
  6298. + struct crypto_alg *cra = &ixp4xx_algos[i].crypto;
  6299. +
  6300. + if (snprintf(cra->cra_driver_name, CRYPTO_MAX_ALG_NAME,
  6301. + "%s"IXP_POSTFIX, cra->cra_name) >=
  6302. + CRYPTO_MAX_ALG_NAME)
  6303. + {
  6304. + continue;
  6305. + }
  6306. + if (!support_aes && (ixp4xx_algos[i].cfg_enc & MOD_AES)) {
  6307. + continue;
  6308. + }
  6309. + if (!ixp4xx_algos[i].hash) {
  6310. + /* block ciphers */
  6311. + cra->cra_type = &crypto_ablkcipher_type;
  6312. + cra->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  6313. + CRYPTO_ALG_ASYNC;
  6314. + if (!cra->cra_ablkcipher.setkey)
  6315. + cra->cra_ablkcipher.setkey = ablk_setkey;
  6316. + if (!cra->cra_ablkcipher.encrypt)
  6317. + cra->cra_ablkcipher.encrypt = ablk_encrypt;
  6318. + if (!cra->cra_ablkcipher.decrypt)
  6319. + cra->cra_ablkcipher.decrypt = ablk_decrypt;
  6320. + cra->cra_init = init_tfm_ablk;
  6321. + } else {
  6322. + /* authenc */
  6323. + cra->cra_type = &crypto_aead_type;
  6324. + cra->cra_flags = CRYPTO_ALG_TYPE_AEAD |
  6325. + CRYPTO_ALG_ASYNC;
  6326. + cra->cra_aead.setkey = aead_setkey;
  6327. + cra->cra_aead.setauthsize = aead_setauthsize;
  6328. + cra->cra_aead.encrypt = aead_encrypt;
  6329. + cra->cra_aead.decrypt = aead_decrypt;
  6330. + cra->cra_aead.givencrypt = aead_givencrypt;
  6331. + cra->cra_init = init_tfm_aead;
  6332. + }
  6333. + cra->cra_ctxsize = sizeof(struct ixp_ctx);
  6334. + cra->cra_module = THIS_MODULE;
  6335. + cra->cra_alignmask = 3;
  6336. + cra->cra_priority = 300;
  6337. + cra->cra_exit = exit_tfm;
  6338. + if (crypto_register_alg(cra))
  6339. + printk(KERN_ERR "Failed to register '%s'\n",
  6340. + cra->cra_name);
  6341. + else
  6342. + ixp4xx_algos[i].registered = 1;
  6343. + }
  6344. + return 0;
  6345. +}
  6346. +
  6347. +static void __exit ixp_module_exit(void)
  6348. +{
  6349. + int num = ARRAY_SIZE(ixp4xx_algos);
  6350. + int i;
  6351. +
  6352. + for (i=0; i< num; i++) {
  6353. + if (ixp4xx_algos[i].registered)
  6354. + crypto_unregister_alg(&ixp4xx_algos[i].crypto);
  6355. + }
  6356. + release_ixp_crypto();
  6357. + platform_device_unregister(&pseudo_dev);
  6358. +}
  6359. +
  6360. +module_init(ixp_module_init);
  6361. +module_exit(ixp_module_exit);
  6362. +
  6363. +MODULE_LICENSE("GPL");
  6364. +MODULE_AUTHOR("Christian Hohnstaedt <[email protected]>");
  6365. +MODULE_DESCRIPTION("IXP4xx hardware crypto");
  6366. +
  6367. --- a/drivers/crypto/padlock-aes.c
  6368. +++ b/drivers/crypto/padlock-aes.c
  6369. @@ -411,12 +411,12 @@ static int __init padlock_init(void)
  6370. int ret;
  6371. if (!cpu_has_xcrypt) {
  6372. - printk(KERN_ERR PFX "VIA PadLock not detected.\n");
  6373. + printk(KERN_NOTICE PFX "VIA PadLock not detected.\n");
  6374. return -ENODEV;
  6375. }
  6376. if (!cpu_has_xcrypt_enabled) {
  6377. - printk(KERN_ERR PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
  6378. + printk(KERN_NOTICE PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
  6379. return -ENODEV;
  6380. }
  6381. --- a/drivers/crypto/padlock-sha.c
  6382. +++ b/drivers/crypto/padlock-sha.c
  6383. @@ -263,12 +263,12 @@ static int __init padlock_init(void)
  6384. int rc = -ENODEV;
  6385. if (!cpu_has_phe) {
  6386. - printk(KERN_ERR PFX "VIA PadLock Hash Engine not detected.\n");
  6387. + printk(KERN_NOTICE PFX "VIA PadLock Hash Engine not detected.\n");
  6388. return -ENODEV;
  6389. }
  6390. if (!cpu_has_phe_enabled) {
  6391. - printk(KERN_ERR PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
  6392. + printk(KERN_NOTICE PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
  6393. return -ENODEV;
  6394. }
  6395. --- /dev/null
  6396. +++ b/drivers/crypto/talitos.c
  6397. @@ -0,0 +1,1597 @@
  6398. +/*
  6399. + * talitos - Freescale Integrated Security Engine (SEC) device driver
  6400. + *
  6401. + * Copyright (c) 2008 Freescale Semiconductor, Inc.
  6402. + *
  6403. + * Scatterlist Crypto API glue code copied from files with the following:
  6404. + * Copyright (c) 2006-2007 Herbert Xu <[email protected]>
  6405. + *
  6406. + * Crypto algorithm registration code copied from hifn driver:
  6407. + * 2007+ Copyright (c) Evgeniy Polyakov <[email protected]>
  6408. + * All rights reserved.
  6409. + *
  6410. + * This program is free software; you can redistribute it and/or modify
  6411. + * it under the terms of the GNU General Public License as published by
  6412. + * the Free Software Foundation; either version 2 of the License, or
  6413. + * (at your option) any later version.
  6414. + *
  6415. + * This program is distributed in the hope that it will be useful,
  6416. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6417. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6418. + * GNU General Public License for more details.
  6419. + *
  6420. + * You should have received a copy of the GNU General Public License
  6421. + * along with this program; if not, write to the Free Software
  6422. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6423. + */
  6424. +
  6425. +#include <linux/kernel.h>
  6426. +#include <linux/module.h>
  6427. +#include <linux/mod_devicetable.h>
  6428. +#include <linux/device.h>
  6429. +#include <linux/interrupt.h>
  6430. +#include <linux/crypto.h>
  6431. +#include <linux/hw_random.h>
  6432. +#include <linux/of_platform.h>
  6433. +#include <linux/dma-mapping.h>
  6434. +#include <linux/io.h>
  6435. +#include <linux/spinlock.h>
  6436. +#include <linux/rtnetlink.h>
  6437. +
  6438. +#include <crypto/algapi.h>
  6439. +#include <crypto/aes.h>
  6440. +#include <crypto/des.h>
  6441. +#include <crypto/sha.h>
  6442. +#include <crypto/aead.h>
  6443. +#include <crypto/authenc.h>
  6444. +
  6445. +#include "talitos.h"
  6446. +
  6447. +#define TALITOS_TIMEOUT 100000
  6448. +#define TALITOS_MAX_DATA_LEN 65535
  6449. +
  6450. +#define DESC_TYPE(desc_hdr) ((be32_to_cpu(desc_hdr) >> 3) & 0x1f)
  6451. +#define PRIMARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 28) & 0xf)
  6452. +#define SECONDARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 16) & 0xf)
  6453. +
  6454. +/* descriptor pointer entry */
  6455. +struct talitos_ptr {
  6456. + __be16 len; /* length */
  6457. + u8 j_extent; /* jump to sg link table and/or extent */
  6458. + u8 eptr; /* extended address */
  6459. + __be32 ptr; /* address */
  6460. +};
  6461. +
  6462. +/* descriptor */
  6463. +struct talitos_desc {
  6464. + __be32 hdr; /* header high bits */
  6465. + __be32 hdr_lo; /* header low bits */
  6466. + struct talitos_ptr ptr[7]; /* ptr/len pair array */
  6467. +};
  6468. +
  6469. +/**
  6470. + * talitos_request - descriptor submission request
  6471. + * @desc: descriptor pointer (kernel virtual)
  6472. + * @dma_desc: descriptor's physical bus address
  6473. + * @callback: whom to call when descriptor processing is done
  6474. + * @context: caller context (optional)
  6475. + */
  6476. +struct talitos_request {
  6477. + struct talitos_desc *desc;
  6478. + dma_addr_t dma_desc;
  6479. + void (*callback) (struct device *dev, struct talitos_desc *desc,
  6480. + void *context, int error);
  6481. + void *context;
  6482. +};
  6483. +
  6484. +struct talitos_private {
  6485. + struct device *dev;
  6486. + struct of_device *ofdev;
  6487. + void __iomem *reg;
  6488. + int irq;
  6489. +
  6490. + /* SEC version geometry (from device tree node) */
  6491. + unsigned int num_channels;
  6492. + unsigned int chfifo_len;
  6493. + unsigned int exec_units;
  6494. + unsigned int desc_types;
  6495. +
  6496. + /* next channel to be assigned next incoming descriptor */
  6497. + atomic_t last_chan;
  6498. +
  6499. + /* per-channel request fifo */
  6500. + struct talitos_request **fifo;
  6501. +
  6502. + /*
  6503. + * length of the request fifo
  6504. + * fifo_len is chfifo_len rounded up to next power of 2
  6505. + * so we can use bitwise ops to wrap
  6506. + */
  6507. + unsigned int fifo_len;
  6508. +
  6509. + /* per-channel index to next free descriptor request */
  6510. + int *head;
  6511. +
  6512. + /* per-channel index to next in-progress/done descriptor request */
  6513. + int *tail;
  6514. +
  6515. + /* per-channel request submission (head) and release (tail) locks */
  6516. + spinlock_t *head_lock;
  6517. + spinlock_t *tail_lock;
  6518. +
  6519. + /* request callback tasklet */
  6520. + struct tasklet_struct done_task;
  6521. + struct tasklet_struct error_task;
  6522. +
  6523. + /* list of registered algorithms */
  6524. + struct list_head alg_list;
  6525. +
  6526. + /* hwrng device */
  6527. + struct hwrng rng;
  6528. +};
  6529. +
  6530. +/*
  6531. + * map virtual single (contiguous) pointer to h/w descriptor pointer
  6532. + */
  6533. +static void map_single_talitos_ptr(struct device *dev,
  6534. + struct talitos_ptr *talitos_ptr,
  6535. + unsigned short len, void *data,
  6536. + unsigned char extent,
  6537. + enum dma_data_direction dir)
  6538. +{
  6539. + talitos_ptr->len = cpu_to_be16(len);
  6540. + talitos_ptr->ptr = cpu_to_be32(dma_map_single(dev, data, len, dir));
  6541. + talitos_ptr->j_extent = extent;
  6542. +}
  6543. +
  6544. +/*
  6545. + * unmap bus single (contiguous) h/w descriptor pointer
  6546. + */
  6547. +static void unmap_single_talitos_ptr(struct device *dev,
  6548. + struct talitos_ptr *talitos_ptr,
  6549. + enum dma_data_direction dir)
  6550. +{
  6551. + dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr),
  6552. + be16_to_cpu(talitos_ptr->len), dir);
  6553. +}
  6554. +
  6555. +static int reset_channel(struct device *dev, int ch)
  6556. +{
  6557. + struct talitos_private *priv = dev_get_drvdata(dev);
  6558. + unsigned int timeout = TALITOS_TIMEOUT;
  6559. +
  6560. + setbits32(priv->reg + TALITOS_CCCR(ch), TALITOS_CCCR_RESET);
  6561. +
  6562. + while ((in_be32(priv->reg + TALITOS_CCCR(ch)) & TALITOS_CCCR_RESET)
  6563. + && --timeout)
  6564. + cpu_relax();
  6565. +
  6566. + if (timeout == 0) {
  6567. + dev_err(dev, "failed to reset channel %d\n", ch);
  6568. + return -EIO;
  6569. + }
  6570. +
  6571. + /* set done writeback and IRQ */
  6572. + setbits32(priv->reg + TALITOS_CCCR_LO(ch), TALITOS_CCCR_LO_CDWE |
  6573. + TALITOS_CCCR_LO_CDIE);
  6574. +
  6575. + return 0;
  6576. +}
  6577. +
  6578. +static int reset_device(struct device *dev)
  6579. +{
  6580. + struct talitos_private *priv = dev_get_drvdata(dev);
  6581. + unsigned int timeout = TALITOS_TIMEOUT;
  6582. +
  6583. + setbits32(priv->reg + TALITOS_MCR, TALITOS_MCR_SWR);
  6584. +
  6585. + while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR)
  6586. + && --timeout)
  6587. + cpu_relax();
  6588. +
  6589. + if (timeout == 0) {
  6590. + dev_err(dev, "failed to reset device\n");
  6591. + return -EIO;
  6592. + }
  6593. +
  6594. + return 0;
  6595. +}
  6596. +
  6597. +/*
  6598. + * Reset and initialize the device
  6599. + */
  6600. +static int init_device(struct device *dev)
  6601. +{
  6602. + struct talitos_private *priv = dev_get_drvdata(dev);
  6603. + int ch, err;
  6604. +
  6605. + /*
  6606. + * Master reset
  6607. + * errata documentation: warning: certain SEC interrupts
  6608. + * are not fully cleared by writing the MCR:SWR bit,
  6609. + * set bit twice to completely reset
  6610. + */
  6611. + err = reset_device(dev);
  6612. + if (err)
  6613. + return err;
  6614. +
  6615. + err = reset_device(dev);
  6616. + if (err)
  6617. + return err;
  6618. +
  6619. + /* reset channels */
  6620. + for (ch = 0; ch < priv->num_channels; ch++) {
  6621. + err = reset_channel(dev, ch);
  6622. + if (err)
  6623. + return err;
  6624. + }
  6625. +
  6626. + /* enable channel done and error interrupts */
  6627. + setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
  6628. + setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
  6629. +
  6630. + return 0;
  6631. +}
  6632. +
  6633. +/**
  6634. + * talitos_submit - submits a descriptor to the device for processing
  6635. + * @dev: the SEC device to be used
  6636. + * @desc: the descriptor to be processed by the device
  6637. + * @callback: whom to call when processing is complete
  6638. + * @context: a handle for use by caller (optional)
  6639. + *
  6640. + * desc must contain valid dma-mapped (bus physical) address pointers.
  6641. + * callback must check err and feedback in descriptor header
  6642. + * for device processing status.
  6643. + */
  6644. +static int talitos_submit(struct device *dev, struct talitos_desc *desc,
  6645. + void (*callback)(struct device *dev,
  6646. + struct talitos_desc *desc,
  6647. + void *context, int error),
  6648. + void *context)
  6649. +{
  6650. + struct talitos_private *priv = dev_get_drvdata(dev);
  6651. + struct talitos_request *request;
  6652. + unsigned long flags, ch;
  6653. + int head;
  6654. +
  6655. + /* select done notification */
  6656. + desc->hdr |= DESC_HDR_DONE_NOTIFY;
  6657. +
  6658. + /* emulate SEC's round-robin channel fifo polling scheme */
  6659. + ch = atomic_inc_return(&priv->last_chan) & (priv->num_channels - 1);
  6660. +
  6661. + spin_lock_irqsave(&priv->head_lock[ch], flags);
  6662. +
  6663. + head = priv->head[ch];
  6664. + request = &priv->fifo[ch][head];
  6665. +
  6666. + if (request->desc) {
  6667. + /* request queue is full */
  6668. + spin_unlock_irqrestore(&priv->head_lock[ch], flags);
  6669. + return -EAGAIN;
  6670. + }
  6671. +
  6672. + /* map descriptor and save caller data */
  6673. + request->dma_desc = dma_map_single(dev, desc, sizeof(*desc),
  6674. + DMA_BIDIRECTIONAL);
  6675. + request->callback = callback;
  6676. + request->context = context;
  6677. +
  6678. + /* increment fifo head */
  6679. + priv->head[ch] = (priv->head[ch] + 1) & (priv->fifo_len - 1);
  6680. +
  6681. + smp_wmb();
  6682. + request->desc = desc;
  6683. +
  6684. + /* GO! */
  6685. + wmb();
  6686. + out_be32(priv->reg + TALITOS_FF_LO(ch), request->dma_desc);
  6687. +
  6688. + spin_unlock_irqrestore(&priv->head_lock[ch], flags);
  6689. +
  6690. + return -EINPROGRESS;
  6691. +}
  6692. +
  6693. +/*
  6694. + * process what was done, notify callback of error if not
  6695. + */
  6696. +static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
  6697. +{
  6698. + struct talitos_private *priv = dev_get_drvdata(dev);
  6699. + struct talitos_request *request, saved_req;
  6700. + unsigned long flags;
  6701. + int tail, status;
  6702. +
  6703. + spin_lock_irqsave(&priv->tail_lock[ch], flags);
  6704. +
  6705. + tail = priv->tail[ch];
  6706. + while (priv->fifo[ch][tail].desc) {
  6707. + request = &priv->fifo[ch][tail];
  6708. +
  6709. + /* descriptors with their done bits set don't get the error */
  6710. + rmb();
  6711. + if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
  6712. + status = 0;
  6713. + else
  6714. + if (!error)
  6715. + break;
  6716. + else
  6717. + status = error;
  6718. +
  6719. + dma_unmap_single(dev, request->dma_desc,
  6720. + sizeof(struct talitos_desc), DMA_BIDIRECTIONAL);
  6721. +
  6722. + /* copy entries so we can call callback outside lock */
  6723. + saved_req.desc = request->desc;
  6724. + saved_req.callback = request->callback;
  6725. + saved_req.context = request->context;
  6726. +
  6727. + /* release request entry in fifo */
  6728. + smp_wmb();
  6729. + request->desc = NULL;
  6730. +
  6731. + /* increment fifo tail */
  6732. + priv->tail[ch] = (tail + 1) & (priv->fifo_len - 1);
  6733. +
  6734. + spin_unlock_irqrestore(&priv->tail_lock[ch], flags);
  6735. + saved_req.callback(dev, saved_req.desc, saved_req.context,
  6736. + status);
  6737. + /* channel may resume processing in single desc error case */
  6738. + if (error && !reset_ch && status == error)
  6739. + return;
  6740. + spin_lock_irqsave(&priv->tail_lock[ch], flags);
  6741. + tail = priv->tail[ch];
  6742. + }
  6743. +
  6744. + spin_unlock_irqrestore(&priv->tail_lock[ch], flags);
  6745. +}
  6746. +
  6747. +/*
  6748. + * process completed requests for channels that have done status
  6749. + */
  6750. +static void talitos_done(unsigned long data)
  6751. +{
  6752. + struct device *dev = (struct device *)data;
  6753. + struct talitos_private *priv = dev_get_drvdata(dev);
  6754. + int ch;
  6755. +
  6756. + for (ch = 0; ch < priv->num_channels; ch++)
  6757. + flush_channel(dev, ch, 0, 0);
  6758. +}
  6759. +
  6760. +/*
  6761. + * locate current (offending) descriptor
  6762. + */
  6763. +static struct talitos_desc *current_desc(struct device *dev, int ch)
  6764. +{
  6765. + struct talitos_private *priv = dev_get_drvdata(dev);
  6766. + int tail = priv->tail[ch];
  6767. + dma_addr_t cur_desc;
  6768. +
  6769. + cur_desc = in_be32(priv->reg + TALITOS_CDPR_LO(ch));
  6770. +
  6771. + while (priv->fifo[ch][tail].dma_desc != cur_desc) {
  6772. + tail = (tail + 1) & (priv->fifo_len - 1);
  6773. + if (tail == priv->tail[ch]) {
  6774. + dev_err(dev, "couldn't locate current descriptor\n");
  6775. + return NULL;
  6776. + }
  6777. + }
  6778. +
  6779. + return priv->fifo[ch][tail].desc;
  6780. +}
  6781. +
  6782. +/*
  6783. + * user diagnostics; report root cause of error based on execution unit status
  6784. + */
  6785. +static void report_eu_error(struct device *dev, int ch, struct talitos_desc *desc)
  6786. +{
  6787. + struct talitos_private *priv = dev_get_drvdata(dev);
  6788. + int i;
  6789. +
  6790. + switch (desc->hdr & DESC_HDR_SEL0_MASK) {
  6791. + case DESC_HDR_SEL0_AFEU:
  6792. + dev_err(dev, "AFEUISR 0x%08x_%08x\n",
  6793. + in_be32(priv->reg + TALITOS_AFEUISR),
  6794. + in_be32(priv->reg + TALITOS_AFEUISR_LO));
  6795. + break;
  6796. + case DESC_HDR_SEL0_DEU:
  6797. + dev_err(dev, "DEUISR 0x%08x_%08x\n",
  6798. + in_be32(priv->reg + TALITOS_DEUISR),
  6799. + in_be32(priv->reg + TALITOS_DEUISR_LO));
  6800. + break;
  6801. + case DESC_HDR_SEL0_MDEUA:
  6802. + case DESC_HDR_SEL0_MDEUB:
  6803. + dev_err(dev, "MDEUISR 0x%08x_%08x\n",
  6804. + in_be32(priv->reg + TALITOS_MDEUISR),
  6805. + in_be32(priv->reg + TALITOS_MDEUISR_LO));
  6806. + break;
  6807. + case DESC_HDR_SEL0_RNG:
  6808. + dev_err(dev, "RNGUISR 0x%08x_%08x\n",
  6809. + in_be32(priv->reg + TALITOS_RNGUISR),
  6810. + in_be32(priv->reg + TALITOS_RNGUISR_LO));
  6811. + break;
  6812. + case DESC_HDR_SEL0_PKEU:
  6813. + dev_err(dev, "PKEUISR 0x%08x_%08x\n",
  6814. + in_be32(priv->reg + TALITOS_PKEUISR),
  6815. + in_be32(priv->reg + TALITOS_PKEUISR_LO));
  6816. + break;
  6817. + case DESC_HDR_SEL0_AESU:
  6818. + dev_err(dev, "AESUISR 0x%08x_%08x\n",
  6819. + in_be32(priv->reg + TALITOS_AESUISR),
  6820. + in_be32(priv->reg + TALITOS_AESUISR_LO));
  6821. + break;
  6822. + case DESC_HDR_SEL0_CRCU:
  6823. + dev_err(dev, "CRCUISR 0x%08x_%08x\n",
  6824. + in_be32(priv->reg + TALITOS_CRCUISR),
  6825. + in_be32(priv->reg + TALITOS_CRCUISR_LO));
  6826. + break;
  6827. + case DESC_HDR_SEL0_KEU:
  6828. + dev_err(dev, "KEUISR 0x%08x_%08x\n",
  6829. + in_be32(priv->reg + TALITOS_KEUISR),
  6830. + in_be32(priv->reg + TALITOS_KEUISR_LO));
  6831. + break;
  6832. + }
  6833. +
  6834. + switch (desc->hdr & DESC_HDR_SEL1_MASK) {
  6835. + case DESC_HDR_SEL1_MDEUA:
  6836. + case DESC_HDR_SEL1_MDEUB:
  6837. + dev_err(dev, "MDEUISR 0x%08x_%08x\n",
  6838. + in_be32(priv->reg + TALITOS_MDEUISR),
  6839. + in_be32(priv->reg + TALITOS_MDEUISR_LO));
  6840. + break;
  6841. + case DESC_HDR_SEL1_CRCU:
  6842. + dev_err(dev, "CRCUISR 0x%08x_%08x\n",
  6843. + in_be32(priv->reg + TALITOS_CRCUISR),
  6844. + in_be32(priv->reg + TALITOS_CRCUISR_LO));
  6845. + break;
  6846. + }
  6847. +
  6848. + for (i = 0; i < 8; i++)
  6849. + dev_err(dev, "DESCBUF 0x%08x_%08x\n",
  6850. + in_be32(priv->reg + TALITOS_DESCBUF(ch) + 8*i),
  6851. + in_be32(priv->reg + TALITOS_DESCBUF_LO(ch) + 8*i));
  6852. +}
  6853. +
  6854. +/*
  6855. + * recover from error interrupts
  6856. + */
  6857. +static void talitos_error(unsigned long data)
  6858. +{
  6859. + struct device *dev = (struct device *)data;
  6860. + struct talitos_private *priv = dev_get_drvdata(dev);
  6861. + unsigned int timeout = TALITOS_TIMEOUT;
  6862. + int ch, error, reset_dev = 0, reset_ch = 0;
  6863. + u32 isr, isr_lo, v, v_lo;
  6864. +
  6865. + isr = in_be32(priv->reg + TALITOS_ISR);
  6866. + isr_lo = in_be32(priv->reg + TALITOS_ISR_LO);
  6867. +
  6868. + for (ch = 0; ch < priv->num_channels; ch++) {
  6869. + /* skip channels without errors */
  6870. + if (!(isr & (1 << (ch * 2 + 1))))
  6871. + continue;
  6872. +
  6873. + error = -EINVAL;
  6874. +
  6875. + v = in_be32(priv->reg + TALITOS_CCPSR(ch));
  6876. + v_lo = in_be32(priv->reg + TALITOS_CCPSR_LO(ch));
  6877. +
  6878. + if (v_lo & TALITOS_CCPSR_LO_DOF) {
  6879. + dev_err(dev, "double fetch fifo overflow error\n");
  6880. + error = -EAGAIN;
  6881. + reset_ch = 1;
  6882. + }
  6883. + if (v_lo & TALITOS_CCPSR_LO_SOF) {
  6884. + /* h/w dropped descriptor */
  6885. + dev_err(dev, "single fetch fifo overflow error\n");
  6886. + error = -EAGAIN;
  6887. + }
  6888. + if (v_lo & TALITOS_CCPSR_LO_MDTE)
  6889. + dev_err(dev, "master data transfer error\n");
  6890. + if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
  6891. + dev_err(dev, "s/g data length zero error\n");
  6892. + if (v_lo & TALITOS_CCPSR_LO_FPZ)
  6893. + dev_err(dev, "fetch pointer zero error\n");
  6894. + if (v_lo & TALITOS_CCPSR_LO_IDH)
  6895. + dev_err(dev, "illegal descriptor header error\n");
  6896. + if (v_lo & TALITOS_CCPSR_LO_IEU)
  6897. + dev_err(dev, "invalid execution unit error\n");
  6898. + if (v_lo & TALITOS_CCPSR_LO_EU)
  6899. + report_eu_error(dev, ch, current_desc(dev, ch));
  6900. + if (v_lo & TALITOS_CCPSR_LO_GB)
  6901. + dev_err(dev, "gather boundary error\n");
  6902. + if (v_lo & TALITOS_CCPSR_LO_GRL)
  6903. + dev_err(dev, "gather return/length error\n");
  6904. + if (v_lo & TALITOS_CCPSR_LO_SB)
  6905. + dev_err(dev, "scatter boundary error\n");
  6906. + if (v_lo & TALITOS_CCPSR_LO_SRL)
  6907. + dev_err(dev, "scatter return/length error\n");
  6908. +
  6909. + flush_channel(dev, ch, error, reset_ch);
  6910. +
  6911. + if (reset_ch) {
  6912. + reset_channel(dev, ch);
  6913. + } else {
  6914. + setbits32(priv->reg + TALITOS_CCCR(ch),
  6915. + TALITOS_CCCR_CONT);
  6916. + setbits32(priv->reg + TALITOS_CCCR_LO(ch), 0);
  6917. + while ((in_be32(priv->reg + TALITOS_CCCR(ch)) &
  6918. + TALITOS_CCCR_CONT) && --timeout)
  6919. + cpu_relax();
  6920. + if (timeout == 0) {
  6921. + dev_err(dev, "failed to restart channel %d\n",
  6922. + ch);
  6923. + reset_dev = 1;
  6924. + }
  6925. + }
  6926. + }
  6927. + if (reset_dev || isr & ~TALITOS_ISR_CHERR || isr_lo) {
  6928. + dev_err(dev, "done overflow, internal time out, or rngu error: "
  6929. + "ISR 0x%08x_%08x\n", isr, isr_lo);
  6930. +
  6931. + /* purge request queues */
  6932. + for (ch = 0; ch < priv->num_channels; ch++)
  6933. + flush_channel(dev, ch, -EIO, 1);
  6934. +
  6935. + /* reset and reinitialize the device */
  6936. + init_device(dev);
  6937. + }
  6938. +}
  6939. +
  6940. +static irqreturn_t talitos_interrupt(int irq, void *data)
  6941. +{
  6942. + struct device *dev = data;
  6943. + struct talitos_private *priv = dev_get_drvdata(dev);
  6944. + u32 isr, isr_lo;
  6945. +
  6946. + isr = in_be32(priv->reg + TALITOS_ISR);
  6947. + isr_lo = in_be32(priv->reg + TALITOS_ISR_LO);
  6948. +
  6949. + /* ack */
  6950. + out_be32(priv->reg + TALITOS_ICR, isr);
  6951. + out_be32(priv->reg + TALITOS_ICR_LO, isr_lo);
  6952. +
  6953. + if (unlikely((isr & ~TALITOS_ISR_CHDONE) || isr_lo))
  6954. + talitos_error((unsigned long)data);
  6955. + else
  6956. + if (likely(isr & TALITOS_ISR_CHDONE))
  6957. + tasklet_schedule(&priv->done_task);
  6958. +
  6959. + return (isr || isr_lo) ? IRQ_HANDLED : IRQ_NONE;
  6960. +}
  6961. +
  6962. +/*
  6963. + * hwrng
  6964. + */
  6965. +static int talitos_rng_data_present(struct hwrng *rng, int wait)
  6966. +{
  6967. + struct device *dev = (struct device *)rng->priv;
  6968. + struct talitos_private *priv = dev_get_drvdata(dev);
  6969. + u32 ofl;
  6970. + int i;
  6971. +
  6972. + for (i = 0; i < 20; i++) {
  6973. + ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) &
  6974. + TALITOS_RNGUSR_LO_OFL;
  6975. + if (ofl || !wait)
  6976. + break;
  6977. + udelay(10);
  6978. + }
  6979. +
  6980. + return !!ofl;
  6981. +}
  6982. +
  6983. +static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
  6984. +{
  6985. + struct device *dev = (struct device *)rng->priv;
  6986. + struct talitos_private *priv = dev_get_drvdata(dev);
  6987. +
  6988. + /* rng fifo requires 64-bit accesses */
  6989. + *data = in_be32(priv->reg + TALITOS_RNGU_FIFO);
  6990. + *data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO);
  6991. +
  6992. + return sizeof(u32);
  6993. +}
  6994. +
  6995. +static int talitos_rng_init(struct hwrng *rng)
  6996. +{
  6997. + struct device *dev = (struct device *)rng->priv;
  6998. + struct talitos_private *priv = dev_get_drvdata(dev);
  6999. + unsigned int timeout = TALITOS_TIMEOUT;
  7000. +
  7001. + setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR);
  7002. + while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD)
  7003. + && --timeout)
  7004. + cpu_relax();
  7005. + if (timeout == 0) {
  7006. + dev_err(dev, "failed to reset rng hw\n");
  7007. + return -ENODEV;
  7008. + }
  7009. +
  7010. + /* start generating */
  7011. + setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0);
  7012. +
  7013. + return 0;
  7014. +}
  7015. +
  7016. +static int talitos_register_rng(struct device *dev)
  7017. +{
  7018. + struct talitos_private *priv = dev_get_drvdata(dev);
  7019. +
  7020. + priv->rng.name = dev_driver_string(dev),
  7021. + priv->rng.init = talitos_rng_init,
  7022. + priv->rng.data_present = talitos_rng_data_present,
  7023. + priv->rng.data_read = talitos_rng_data_read,
  7024. + priv->rng.priv = (unsigned long)dev;
  7025. +
  7026. + return hwrng_register(&priv->rng);
  7027. +}
  7028. +
  7029. +static void talitos_unregister_rng(struct device *dev)
  7030. +{
  7031. + struct talitos_private *priv = dev_get_drvdata(dev);
  7032. +
  7033. + hwrng_unregister(&priv->rng);
  7034. +}
  7035. +
  7036. +/*
  7037. + * crypto alg
  7038. + */
  7039. +#define TALITOS_CRA_PRIORITY 3000
  7040. +#define TALITOS_MAX_KEY_SIZE 64
  7041. +#define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
  7042. +
  7043. +#define MD5_DIGEST_SIZE 16
  7044. +
  7045. +struct talitos_ctx {
  7046. + struct device *dev;
  7047. + __be32 desc_hdr_template;
  7048. + u8 key[TALITOS_MAX_KEY_SIZE];
  7049. + u8 iv[TALITOS_MAX_IV_LENGTH];
  7050. + unsigned int keylen;
  7051. + unsigned int enckeylen;
  7052. + unsigned int authkeylen;
  7053. + unsigned int authsize;
  7054. +};
  7055. +
  7056. +static int aead_authenc_setauthsize(struct crypto_aead *authenc,
  7057. + unsigned int authsize)
  7058. +{
  7059. + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
  7060. +
  7061. + ctx->authsize = authsize;
  7062. +
  7063. + return 0;
  7064. +}
  7065. +
  7066. +static int aead_authenc_setkey(struct crypto_aead *authenc,
  7067. + const u8 *key, unsigned int keylen)
  7068. +{
  7069. + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
  7070. + struct rtattr *rta = (void *)key;
  7071. + struct crypto_authenc_key_param *param;
  7072. + unsigned int authkeylen;
  7073. + unsigned int enckeylen;
  7074. +
  7075. + if (!RTA_OK(rta, keylen))
  7076. + goto badkey;
  7077. +
  7078. + if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
  7079. + goto badkey;
  7080. +
  7081. + if (RTA_PAYLOAD(rta) < sizeof(*param))
  7082. + goto badkey;
  7083. +
  7084. + param = RTA_DATA(rta);
  7085. + enckeylen = be32_to_cpu(param->enckeylen);
  7086. +
  7087. + key += RTA_ALIGN(rta->rta_len);
  7088. + keylen -= RTA_ALIGN(rta->rta_len);
  7089. +
  7090. + if (keylen < enckeylen)
  7091. + goto badkey;
  7092. +
  7093. + authkeylen = keylen - enckeylen;
  7094. +
  7095. + if (keylen > TALITOS_MAX_KEY_SIZE)
  7096. + goto badkey;
  7097. +
  7098. + memcpy(&ctx->key, key, keylen);
  7099. +
  7100. + ctx->keylen = keylen;
  7101. + ctx->enckeylen = enckeylen;
  7102. + ctx->authkeylen = authkeylen;
  7103. +
  7104. + return 0;
  7105. +
  7106. +badkey:
  7107. + crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
  7108. + return -EINVAL;
  7109. +}
  7110. +
  7111. +/*
  7112. + * ipsec_esp_edesc - s/w-extended ipsec_esp descriptor
  7113. + * @src_nents: number of segments in input scatterlist
  7114. + * @dst_nents: number of segments in output scatterlist
  7115. + * @dma_len: length of dma mapped link_tbl space
  7116. + * @dma_link_tbl: bus physical address of link_tbl
  7117. + * @desc: h/w descriptor
  7118. + * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
  7119. + *
  7120. + * if decrypting (with authcheck), or either one of src_nents or dst_nents
  7121. + * is greater than 1, an integrity check value is concatenated to the end
  7122. + * of link_tbl data
  7123. + */
  7124. +struct ipsec_esp_edesc {
  7125. + int src_nents;
  7126. + int dst_nents;
  7127. + int dma_len;
  7128. + dma_addr_t dma_link_tbl;
  7129. + struct talitos_desc desc;
  7130. + struct talitos_ptr link_tbl[0];
  7131. +};
  7132. +
  7133. +static void ipsec_esp_unmap(struct device *dev,
  7134. + struct ipsec_esp_edesc *edesc,
  7135. + struct aead_request *areq)
  7136. +{
  7137. + unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
  7138. + unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
  7139. + unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
  7140. + unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
  7141. +
  7142. + dma_unmap_sg(dev, areq->assoc, 1, DMA_TO_DEVICE);
  7143. +
  7144. + if (areq->src != areq->dst) {
  7145. + dma_unmap_sg(dev, areq->src, edesc->src_nents ? : 1,
  7146. + DMA_TO_DEVICE);
  7147. + dma_unmap_sg(dev, areq->dst, edesc->dst_nents ? : 1,
  7148. + DMA_FROM_DEVICE);
  7149. + } else {
  7150. + dma_unmap_sg(dev, areq->src, edesc->src_nents ? : 1,
  7151. + DMA_BIDIRECTIONAL);
  7152. + }
  7153. +
  7154. + if (edesc->dma_len)
  7155. + dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
  7156. + DMA_BIDIRECTIONAL);
  7157. +}
  7158. +
  7159. +/*
  7160. + * ipsec_esp descriptor callbacks
  7161. + */
  7162. +static void ipsec_esp_encrypt_done(struct device *dev,
  7163. + struct talitos_desc *desc, void *context,
  7164. + int err)
  7165. +{
  7166. + struct aead_request *areq = context;
  7167. + struct ipsec_esp_edesc *edesc =
  7168. + container_of(desc, struct ipsec_esp_edesc, desc);
  7169. + struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
  7170. + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
  7171. + struct scatterlist *sg;
  7172. + void *icvdata;
  7173. +
  7174. + ipsec_esp_unmap(dev, edesc, areq);
  7175. +
  7176. + /* copy the generated ICV to dst */
  7177. + if (edesc->dma_len) {
  7178. + icvdata = &edesc->link_tbl[edesc->src_nents +
  7179. + edesc->dst_nents + 1];
  7180. + sg = sg_last(areq->dst, edesc->dst_nents);
  7181. + memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
  7182. + icvdata, ctx->authsize);
  7183. + }
  7184. +
  7185. + kfree(edesc);
  7186. +
  7187. + aead_request_complete(areq, err);
  7188. +}
  7189. +
  7190. +static void ipsec_esp_decrypt_done(struct device *dev,
  7191. + struct talitos_desc *desc, void *context,
  7192. + int err)
  7193. +{
  7194. + struct aead_request *req = context;
  7195. + struct ipsec_esp_edesc *edesc =
  7196. + container_of(desc, struct ipsec_esp_edesc, desc);
  7197. + struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  7198. + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
  7199. + struct scatterlist *sg;
  7200. + void *icvdata;
  7201. +
  7202. + ipsec_esp_unmap(dev, edesc, req);
  7203. +
  7204. + if (!err) {
  7205. + /* auth check */
  7206. + if (edesc->dma_len)
  7207. + icvdata = &edesc->link_tbl[edesc->src_nents +
  7208. + edesc->dst_nents + 1];
  7209. + else
  7210. + icvdata = &edesc->link_tbl[0];
  7211. +
  7212. + sg = sg_last(req->dst, edesc->dst_nents ? : 1);
  7213. + err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
  7214. + ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
  7215. + }
  7216. +
  7217. + kfree(edesc);
  7218. +
  7219. + aead_request_complete(req, err);
  7220. +}
  7221. +
  7222. +/*
  7223. + * convert scatterlist to SEC h/w link table format
  7224. + * stop at cryptlen bytes
  7225. + */
  7226. +static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
  7227. + int cryptlen, struct talitos_ptr *link_tbl_ptr)
  7228. +{
  7229. + int n_sg = sg_count;
  7230. +
  7231. + while (n_sg--) {
  7232. + link_tbl_ptr->ptr = cpu_to_be32(sg_dma_address(sg));
  7233. + link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
  7234. + link_tbl_ptr->j_extent = 0;
  7235. + link_tbl_ptr++;
  7236. + cryptlen -= sg_dma_len(sg);
  7237. + sg = sg_next(sg);
  7238. + }
  7239. +
  7240. + /* adjust (decrease) last one (or two) entry's len to cryptlen */
  7241. + link_tbl_ptr--;
  7242. + while (link_tbl_ptr->len <= (-cryptlen)) {
  7243. + /* Empty this entry, and move to previous one */
  7244. + cryptlen += be16_to_cpu(link_tbl_ptr->len);
  7245. + link_tbl_ptr->len = 0;
  7246. + sg_count--;
  7247. + link_tbl_ptr--;
  7248. + }
  7249. + link_tbl_ptr->len = cpu_to_be16(be16_to_cpu(link_tbl_ptr->len)
  7250. + + cryptlen);
  7251. +
  7252. + /* tag end of link table */
  7253. + link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
  7254. +
  7255. + return sg_count;
  7256. +}
  7257. +
  7258. +/*
  7259. + * fill in and submit ipsec_esp descriptor
  7260. + */
  7261. +static int ipsec_esp(struct ipsec_esp_edesc *edesc, struct aead_request *areq,
  7262. + u8 *giv, u64 seq,
  7263. + void (*callback) (struct device *dev,
  7264. + struct talitos_desc *desc,
  7265. + void *context, int error))
  7266. +{
  7267. + struct crypto_aead *aead = crypto_aead_reqtfm(areq);
  7268. + struct talitos_ctx *ctx = crypto_aead_ctx(aead);
  7269. + struct device *dev = ctx->dev;
  7270. + struct talitos_desc *desc = &edesc->desc;
  7271. + unsigned int cryptlen = areq->cryptlen;
  7272. + unsigned int authsize = ctx->authsize;
  7273. + unsigned int ivsize;
  7274. + int sg_count;
  7275. +
  7276. + /* hmac key */
  7277. + map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
  7278. + 0, DMA_TO_DEVICE);
  7279. + /* hmac data */
  7280. + map_single_talitos_ptr(dev, &desc->ptr[1], sg_virt(areq->src) -
  7281. + sg_virt(areq->assoc), sg_virt(areq->assoc), 0,
  7282. + DMA_TO_DEVICE);
  7283. + /* cipher iv */
  7284. + ivsize = crypto_aead_ivsize(aead);
  7285. + map_single_talitos_ptr(dev, &desc->ptr[2], ivsize, giv ?: areq->iv, 0,
  7286. + DMA_TO_DEVICE);
  7287. +
  7288. + /* cipher key */
  7289. + map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
  7290. + (char *)&ctx->key + ctx->authkeylen, 0,
  7291. + DMA_TO_DEVICE);
  7292. +
  7293. + /*
  7294. + * cipher in
  7295. + * map and adjust cipher len to aead request cryptlen.
  7296. + * extent is bytes of HMAC postpended to ciphertext,
  7297. + * typically 12 for ipsec
  7298. + */
  7299. + desc->ptr[4].len = cpu_to_be16(cryptlen);
  7300. + desc->ptr[4].j_extent = authsize;
  7301. +
  7302. + if (areq->src == areq->dst)
  7303. + sg_count = dma_map_sg(dev, areq->src, edesc->src_nents ? : 1,
  7304. + DMA_BIDIRECTIONAL);
  7305. + else
  7306. + sg_count = dma_map_sg(dev, areq->src, edesc->src_nents ? : 1,
  7307. + DMA_TO_DEVICE);
  7308. +
  7309. + if (sg_count == 1) {
  7310. + desc->ptr[4].ptr = cpu_to_be32(sg_dma_address(areq->src));
  7311. + } else {
  7312. + sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen,
  7313. + &edesc->link_tbl[0]);
  7314. + if (sg_count > 1) {
  7315. + desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
  7316. + desc->ptr[4].ptr = cpu_to_be32(edesc->dma_link_tbl);
  7317. + dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
  7318. + edesc->dma_len, DMA_BIDIRECTIONAL);
  7319. + } else {
  7320. + /* Only one segment now, so no link tbl needed */
  7321. + desc->ptr[4].ptr = cpu_to_be32(sg_dma_address(areq->src));
  7322. + }
  7323. + }
  7324. +
  7325. + /* cipher out */
  7326. + desc->ptr[5].len = cpu_to_be16(cryptlen);
  7327. + desc->ptr[5].j_extent = authsize;
  7328. +
  7329. + if (areq->src != areq->dst) {
  7330. + sg_count = dma_map_sg(dev, areq->dst, edesc->dst_nents ? : 1,
  7331. + DMA_FROM_DEVICE);
  7332. + }
  7333. +
  7334. + if (sg_count == 1) {
  7335. + desc->ptr[5].ptr = cpu_to_be32(sg_dma_address(areq->dst));
  7336. + } else {
  7337. + struct talitos_ptr *link_tbl_ptr =
  7338. + &edesc->link_tbl[edesc->src_nents];
  7339. + struct scatterlist *sg;
  7340. +
  7341. + desc->ptr[5].ptr = cpu_to_be32((struct talitos_ptr *)
  7342. + edesc->dma_link_tbl +
  7343. + edesc->src_nents);
  7344. + if (areq->src == areq->dst) {
  7345. + memcpy(link_tbl_ptr, &edesc->link_tbl[0],
  7346. + edesc->src_nents * sizeof(struct talitos_ptr));
  7347. + } else {
  7348. + sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
  7349. + link_tbl_ptr);
  7350. + }
  7351. + link_tbl_ptr += sg_count - 1;
  7352. +
  7353. + /* handle case where sg_last contains the ICV exclusively */
  7354. + sg = sg_last(areq->dst, edesc->dst_nents);
  7355. + if (sg->length == ctx->authsize)
  7356. + link_tbl_ptr--;
  7357. +
  7358. + link_tbl_ptr->j_extent = 0;
  7359. + link_tbl_ptr++;
  7360. + link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
  7361. + link_tbl_ptr->len = cpu_to_be16(authsize);
  7362. +
  7363. + /* icv data follows link tables */
  7364. + link_tbl_ptr->ptr = cpu_to_be32((struct talitos_ptr *)
  7365. + edesc->dma_link_tbl +
  7366. + edesc->src_nents +
  7367. + edesc->dst_nents + 1);
  7368. +
  7369. + desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
  7370. + dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
  7371. + edesc->dma_len, DMA_BIDIRECTIONAL);
  7372. + }
  7373. +
  7374. + /* iv out */
  7375. + map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0,
  7376. + DMA_FROM_DEVICE);
  7377. +
  7378. + return talitos_submit(dev, desc, callback, areq);
  7379. +}
  7380. +
  7381. +
  7382. +/*
  7383. + * derive number of elements in scatterlist
  7384. + */
  7385. +static int sg_count(struct scatterlist *sg_list, int nbytes)
  7386. +{
  7387. + struct scatterlist *sg = sg_list;
  7388. + int sg_nents = 0;
  7389. +
  7390. + while (nbytes) {
  7391. + sg_nents++;
  7392. + nbytes -= sg->length;
  7393. + sg = sg_next(sg);
  7394. + }
  7395. +
  7396. + return sg_nents;
  7397. +}
  7398. +
  7399. +/*
  7400. + * allocate and map the ipsec_esp extended descriptor
  7401. + */
  7402. +static struct ipsec_esp_edesc *ipsec_esp_edesc_alloc(struct aead_request *areq,
  7403. + int icv_stashing)
  7404. +{
  7405. + struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
  7406. + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
  7407. + struct ipsec_esp_edesc *edesc;
  7408. + int src_nents, dst_nents, alloc_len, dma_len;
  7409. +
  7410. + if (areq->cryptlen + ctx->authsize > TALITOS_MAX_DATA_LEN) {
  7411. + dev_err(ctx->dev, "cryptlen exceeds h/w max limit\n");
  7412. + return ERR_PTR(-EINVAL);
  7413. + }
  7414. +
  7415. + src_nents = sg_count(areq->src, areq->cryptlen + ctx->authsize);
  7416. + src_nents = (src_nents == 1) ? 0 : src_nents;
  7417. +
  7418. + if (areq->dst == areq->src) {
  7419. + dst_nents = src_nents;
  7420. + } else {
  7421. + dst_nents = sg_count(areq->dst, areq->cryptlen + ctx->authsize);
  7422. + dst_nents = (dst_nents == 1) ? 0 : src_nents;
  7423. + }
  7424. +
  7425. + /*
  7426. + * allocate space for base edesc plus the link tables,
  7427. + * allowing for a separate entry for the generated ICV (+ 1),
  7428. + * and the ICV data itself
  7429. + */
  7430. + alloc_len = sizeof(struct ipsec_esp_edesc);
  7431. + if (src_nents || dst_nents) {
  7432. + dma_len = (src_nents + dst_nents + 1) *
  7433. + sizeof(struct talitos_ptr) + ctx->authsize;
  7434. + alloc_len += dma_len;
  7435. + } else {
  7436. + dma_len = 0;
  7437. + alloc_len += icv_stashing ? ctx->authsize : 0;
  7438. + }
  7439. +
  7440. + edesc = kmalloc(alloc_len, GFP_DMA);
  7441. + if (!edesc) {
  7442. + dev_err(ctx->dev, "could not allocate edescriptor\n");
  7443. + return ERR_PTR(-ENOMEM);
  7444. + }
  7445. +
  7446. + edesc->src_nents = src_nents;
  7447. + edesc->dst_nents = dst_nents;
  7448. + edesc->dma_len = dma_len;
  7449. + edesc->dma_link_tbl = dma_map_single(ctx->dev, &edesc->link_tbl[0],
  7450. + edesc->dma_len, DMA_BIDIRECTIONAL);
  7451. +
  7452. + return edesc;
  7453. +}
  7454. +
  7455. +static int aead_authenc_encrypt(struct aead_request *req)
  7456. +{
  7457. + struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  7458. + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
  7459. + struct ipsec_esp_edesc *edesc;
  7460. +
  7461. + /* allocate extended descriptor */
  7462. + edesc = ipsec_esp_edesc_alloc(req, 0);
  7463. + if (IS_ERR(edesc))
  7464. + return PTR_ERR(edesc);
  7465. +
  7466. + /* set encrypt */
  7467. + edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
  7468. +
  7469. + return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_encrypt_done);
  7470. +}
  7471. +
  7472. +static int aead_authenc_decrypt(struct aead_request *req)
  7473. +{
  7474. + struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  7475. + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
  7476. + unsigned int authsize = ctx->authsize;
  7477. + struct ipsec_esp_edesc *edesc;
  7478. + struct scatterlist *sg;
  7479. + void *icvdata;
  7480. +
  7481. + req->cryptlen -= authsize;
  7482. +
  7483. + /* allocate extended descriptor */
  7484. + edesc = ipsec_esp_edesc_alloc(req, 1);
  7485. + if (IS_ERR(edesc))
  7486. + return PTR_ERR(edesc);
  7487. +
  7488. + /* stash incoming ICV for later cmp with ICV generated by the h/w */
  7489. + if (edesc->dma_len)
  7490. + icvdata = &edesc->link_tbl[edesc->src_nents +
  7491. + edesc->dst_nents + 1];
  7492. + else
  7493. + icvdata = &edesc->link_tbl[0];
  7494. +
  7495. + sg = sg_last(req->src, edesc->src_nents ? : 1);
  7496. +
  7497. + memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
  7498. + ctx->authsize);
  7499. +
  7500. + /* decrypt */
  7501. + edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
  7502. +
  7503. + return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_done);
  7504. +}
  7505. +
  7506. +static int aead_authenc_givencrypt(
  7507. + struct aead_givcrypt_request *req)
  7508. +{
  7509. + struct aead_request *areq = &req->areq;
  7510. + struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
  7511. + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
  7512. + struct ipsec_esp_edesc *edesc;
  7513. +
  7514. + /* allocate extended descriptor */
  7515. + edesc = ipsec_esp_edesc_alloc(areq, 0);
  7516. + if (IS_ERR(edesc))
  7517. + return PTR_ERR(edesc);
  7518. +
  7519. + /* set encrypt */
  7520. + edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
  7521. +
  7522. + memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
  7523. +
  7524. + return ipsec_esp(edesc, areq, req->giv, req->seq,
  7525. + ipsec_esp_encrypt_done);
  7526. +}
  7527. +
  7528. +struct talitos_alg_template {
  7529. + char name[CRYPTO_MAX_ALG_NAME];
  7530. + char driver_name[CRYPTO_MAX_ALG_NAME];
  7531. + unsigned int blocksize;
  7532. + struct aead_alg aead;
  7533. + struct device *dev;
  7534. + __be32 desc_hdr_template;
  7535. +};
  7536. +
  7537. +static struct talitos_alg_template driver_algs[] = {
  7538. + /* single-pass ipsec_esp descriptor */
  7539. + {
  7540. + .name = "authenc(hmac(sha1),cbc(aes))",
  7541. + .driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
  7542. + .blocksize = AES_BLOCK_SIZE,
  7543. + .aead = {
  7544. + .setkey = aead_authenc_setkey,
  7545. + .setauthsize = aead_authenc_setauthsize,
  7546. + .encrypt = aead_authenc_encrypt,
  7547. + .decrypt = aead_authenc_decrypt,
  7548. + .givencrypt = aead_authenc_givencrypt,
  7549. + .geniv = "<built-in>",
  7550. + .ivsize = AES_BLOCK_SIZE,
  7551. + .maxauthsize = SHA1_DIGEST_SIZE,
  7552. + },
  7553. + .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  7554. + DESC_HDR_SEL0_AESU |
  7555. + DESC_HDR_MODE0_AESU_CBC |
  7556. + DESC_HDR_SEL1_MDEUA |
  7557. + DESC_HDR_MODE1_MDEU_INIT |
  7558. + DESC_HDR_MODE1_MDEU_PAD |
  7559. + DESC_HDR_MODE1_MDEU_SHA1_HMAC,
  7560. + },
  7561. + {
  7562. + .name = "authenc(hmac(sha1),cbc(des3_ede))",
  7563. + .driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
  7564. + .blocksize = DES3_EDE_BLOCK_SIZE,
  7565. + .aead = {
  7566. + .setkey = aead_authenc_setkey,
  7567. + .setauthsize = aead_authenc_setauthsize,
  7568. + .encrypt = aead_authenc_encrypt,
  7569. + .decrypt = aead_authenc_decrypt,
  7570. + .givencrypt = aead_authenc_givencrypt,
  7571. + .geniv = "<built-in>",
  7572. + .ivsize = DES3_EDE_BLOCK_SIZE,
  7573. + .maxauthsize = SHA1_DIGEST_SIZE,
  7574. + },
  7575. + .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  7576. + DESC_HDR_SEL0_DEU |
  7577. + DESC_HDR_MODE0_DEU_CBC |
  7578. + DESC_HDR_MODE0_DEU_3DES |
  7579. + DESC_HDR_SEL1_MDEUA |
  7580. + DESC_HDR_MODE1_MDEU_INIT |
  7581. + DESC_HDR_MODE1_MDEU_PAD |
  7582. + DESC_HDR_MODE1_MDEU_SHA1_HMAC,
  7583. + },
  7584. + {
  7585. + .name = "authenc(hmac(sha256),cbc(aes))",
  7586. + .driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
  7587. + .blocksize = AES_BLOCK_SIZE,
  7588. + .aead = {
  7589. + .setkey = aead_authenc_setkey,
  7590. + .setauthsize = aead_authenc_setauthsize,
  7591. + .encrypt = aead_authenc_encrypt,
  7592. + .decrypt = aead_authenc_decrypt,
  7593. + .givencrypt = aead_authenc_givencrypt,
  7594. + .geniv = "<built-in>",
  7595. + .ivsize = AES_BLOCK_SIZE,
  7596. + .maxauthsize = SHA256_DIGEST_SIZE,
  7597. + },
  7598. + .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  7599. + DESC_HDR_SEL0_AESU |
  7600. + DESC_HDR_MODE0_AESU_CBC |
  7601. + DESC_HDR_SEL1_MDEUA |
  7602. + DESC_HDR_MODE1_MDEU_INIT |
  7603. + DESC_HDR_MODE1_MDEU_PAD |
  7604. + DESC_HDR_MODE1_MDEU_SHA256_HMAC,
  7605. + },
  7606. + {
  7607. + .name = "authenc(hmac(sha256),cbc(des3_ede))",
  7608. + .driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
  7609. + .blocksize = DES3_EDE_BLOCK_SIZE,
  7610. + .aead = {
  7611. + .setkey = aead_authenc_setkey,
  7612. + .setauthsize = aead_authenc_setauthsize,
  7613. + .encrypt = aead_authenc_encrypt,
  7614. + .decrypt = aead_authenc_decrypt,
  7615. + .givencrypt = aead_authenc_givencrypt,
  7616. + .geniv = "<built-in>",
  7617. + .ivsize = DES3_EDE_BLOCK_SIZE,
  7618. + .maxauthsize = SHA256_DIGEST_SIZE,
  7619. + },
  7620. + .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  7621. + DESC_HDR_SEL0_DEU |
  7622. + DESC_HDR_MODE0_DEU_CBC |
  7623. + DESC_HDR_MODE0_DEU_3DES |
  7624. + DESC_HDR_SEL1_MDEUA |
  7625. + DESC_HDR_MODE1_MDEU_INIT |
  7626. + DESC_HDR_MODE1_MDEU_PAD |
  7627. + DESC_HDR_MODE1_MDEU_SHA256_HMAC,
  7628. + },
  7629. + {
  7630. + .name = "authenc(hmac(md5),cbc(aes))",
  7631. + .driver_name = "authenc-hmac-md5-cbc-aes-talitos",
  7632. + .blocksize = AES_BLOCK_SIZE,
  7633. + .aead = {
  7634. + .setkey = aead_authenc_setkey,
  7635. + .setauthsize = aead_authenc_setauthsize,
  7636. + .encrypt = aead_authenc_encrypt,
  7637. + .decrypt = aead_authenc_decrypt,
  7638. + .givencrypt = aead_authenc_givencrypt,
  7639. + .geniv = "<built-in>",
  7640. + .ivsize = AES_BLOCK_SIZE,
  7641. + .maxauthsize = MD5_DIGEST_SIZE,
  7642. + },
  7643. + .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  7644. + DESC_HDR_SEL0_AESU |
  7645. + DESC_HDR_MODE0_AESU_CBC |
  7646. + DESC_HDR_SEL1_MDEUA |
  7647. + DESC_HDR_MODE1_MDEU_INIT |
  7648. + DESC_HDR_MODE1_MDEU_PAD |
  7649. + DESC_HDR_MODE1_MDEU_MD5_HMAC,
  7650. + },
  7651. + {
  7652. + .name = "authenc(hmac(md5),cbc(des3_ede))",
  7653. + .driver_name = "authenc-hmac-md5-cbc-3des-talitos",
  7654. + .blocksize = DES3_EDE_BLOCK_SIZE,
  7655. + .aead = {
  7656. + .setkey = aead_authenc_setkey,
  7657. + .setauthsize = aead_authenc_setauthsize,
  7658. + .encrypt = aead_authenc_encrypt,
  7659. + .decrypt = aead_authenc_decrypt,
  7660. + .givencrypt = aead_authenc_givencrypt,
  7661. + .geniv = "<built-in>",
  7662. + .ivsize = DES3_EDE_BLOCK_SIZE,
  7663. + .maxauthsize = MD5_DIGEST_SIZE,
  7664. + },
  7665. + .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  7666. + DESC_HDR_SEL0_DEU |
  7667. + DESC_HDR_MODE0_DEU_CBC |
  7668. + DESC_HDR_MODE0_DEU_3DES |
  7669. + DESC_HDR_SEL1_MDEUA |
  7670. + DESC_HDR_MODE1_MDEU_INIT |
  7671. + DESC_HDR_MODE1_MDEU_PAD |
  7672. + DESC_HDR_MODE1_MDEU_MD5_HMAC,
  7673. + }
  7674. +};
  7675. +
  7676. +struct talitos_crypto_alg {
  7677. + struct list_head entry;
  7678. + struct device *dev;
  7679. + __be32 desc_hdr_template;
  7680. + struct crypto_alg crypto_alg;
  7681. +};
  7682. +
  7683. +static int talitos_cra_init(struct crypto_tfm *tfm)
  7684. +{
  7685. + struct crypto_alg *alg = tfm->__crt_alg;
  7686. + struct talitos_crypto_alg *talitos_alg =
  7687. + container_of(alg, struct talitos_crypto_alg, crypto_alg);
  7688. + struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
  7689. +
  7690. + /* update context with ptr to dev */
  7691. + ctx->dev = talitos_alg->dev;
  7692. + /* copy descriptor header template value */
  7693. + ctx->desc_hdr_template = talitos_alg->desc_hdr_template;
  7694. +
  7695. + /* random first IV */
  7696. + get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
  7697. +
  7698. + return 0;
  7699. +}
  7700. +
  7701. +/*
  7702. + * given the alg's descriptor header template, determine whether descriptor
  7703. + * type and primary/secondary execution units required match the hw
  7704. + * capabilities description provided in the device tree node.
  7705. + */
  7706. +static int hw_supports(struct device *dev, __be32 desc_hdr_template)
  7707. +{
  7708. + struct talitos_private *priv = dev_get_drvdata(dev);
  7709. + int ret;
  7710. +
  7711. + ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
  7712. + (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
  7713. +
  7714. + if (SECONDARY_EU(desc_hdr_template))
  7715. + ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
  7716. + & priv->exec_units);
  7717. +
  7718. + return ret;
  7719. +}
  7720. +
  7721. +static int __devexit talitos_remove(struct of_device *ofdev)
  7722. +{
  7723. + struct device *dev = &ofdev->dev;
  7724. + struct talitos_private *priv = dev_get_drvdata(dev);
  7725. + struct talitos_crypto_alg *t_alg, *n;
  7726. + int i;
  7727. +
  7728. + list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
  7729. + crypto_unregister_alg(&t_alg->crypto_alg);
  7730. + list_del(&t_alg->entry);
  7731. + kfree(t_alg);
  7732. + }
  7733. +
  7734. + if (hw_supports(dev, DESC_HDR_SEL0_RNG))
  7735. + talitos_unregister_rng(dev);
  7736. +
  7737. + kfree(priv->tail);
  7738. + kfree(priv->head);
  7739. +
  7740. + if (priv->fifo)
  7741. + for (i = 0; i < priv->num_channels; i++)
  7742. + kfree(priv->fifo[i]);
  7743. +
  7744. + kfree(priv->fifo);
  7745. + kfree(priv->head_lock);
  7746. + kfree(priv->tail_lock);
  7747. +
  7748. + if (priv->irq != NO_IRQ) {
  7749. + free_irq(priv->irq, dev);
  7750. + irq_dispose_mapping(priv->irq);
  7751. + }
  7752. +
  7753. + tasklet_kill(&priv->done_task);
  7754. + tasklet_kill(&priv->error_task);
  7755. +
  7756. + iounmap(priv->reg);
  7757. +
  7758. + dev_set_drvdata(dev, NULL);
  7759. +
  7760. + kfree(priv);
  7761. +
  7762. + return 0;
  7763. +}
  7764. +
  7765. +static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
  7766. + struct talitos_alg_template
  7767. + *template)
  7768. +{
  7769. + struct talitos_crypto_alg *t_alg;
  7770. + struct crypto_alg *alg;
  7771. +
  7772. + t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
  7773. + if (!t_alg)
  7774. + return ERR_PTR(-ENOMEM);
  7775. +
  7776. + alg = &t_alg->crypto_alg;
  7777. +
  7778. + snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", template->name);
  7779. + snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  7780. + template->driver_name);
  7781. + alg->cra_module = THIS_MODULE;
  7782. + alg->cra_init = talitos_cra_init;
  7783. + alg->cra_priority = TALITOS_CRA_PRIORITY;
  7784. + alg->cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC;
  7785. + alg->cra_blocksize = template->blocksize;
  7786. + alg->cra_alignmask = 0;
  7787. + alg->cra_type = &crypto_aead_type;
  7788. + alg->cra_ctxsize = sizeof(struct talitos_ctx);
  7789. + alg->cra_u.aead = template->aead;
  7790. +
  7791. + t_alg->desc_hdr_template = template->desc_hdr_template;
  7792. + t_alg->dev = dev;
  7793. +
  7794. + return t_alg;
  7795. +}
  7796. +
  7797. +static int talitos_probe(struct of_device *ofdev,
  7798. + const struct of_device_id *match)
  7799. +{
  7800. + struct device *dev = &ofdev->dev;
  7801. + struct device_node *np = ofdev->node;
  7802. + struct talitos_private *priv;
  7803. + const unsigned int *prop;
  7804. + int i, err;
  7805. +
  7806. + priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
  7807. + if (!priv)
  7808. + return -ENOMEM;
  7809. +
  7810. + dev_set_drvdata(dev, priv);
  7811. +
  7812. + priv->ofdev = ofdev;
  7813. +
  7814. + tasklet_init(&priv->done_task, talitos_done, (unsigned long)dev);
  7815. + tasklet_init(&priv->error_task, talitos_error, (unsigned long)dev);
  7816. +
  7817. + priv->irq = irq_of_parse_and_map(np, 0);
  7818. +
  7819. + if (priv->irq == NO_IRQ) {
  7820. + dev_err(dev, "failed to map irq\n");
  7821. + err = -EINVAL;
  7822. + goto err_out;
  7823. + }
  7824. +
  7825. + /* get the irq line */
  7826. + err = request_irq(priv->irq, talitos_interrupt, 0,
  7827. + dev_driver_string(dev), dev);
  7828. + if (err) {
  7829. + dev_err(dev, "failed to request irq %d\n", priv->irq);
  7830. + irq_dispose_mapping(priv->irq);
  7831. + priv->irq = NO_IRQ;
  7832. + goto err_out;
  7833. + }
  7834. +
  7835. + priv->reg = of_iomap(np, 0);
  7836. + if (!priv->reg) {
  7837. + dev_err(dev, "failed to of_iomap\n");
  7838. + err = -ENOMEM;
  7839. + goto err_out;
  7840. + }
  7841. +
  7842. + /* get SEC version capabilities from device tree */
  7843. + prop = of_get_property(np, "fsl,num-channels", NULL);
  7844. + if (prop)
  7845. + priv->num_channels = *prop;
  7846. +
  7847. + prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
  7848. + if (prop)
  7849. + priv->chfifo_len = *prop;
  7850. +
  7851. + prop = of_get_property(np, "fsl,exec-units-mask", NULL);
  7852. + if (prop)
  7853. + priv->exec_units = *prop;
  7854. +
  7855. + prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
  7856. + if (prop)
  7857. + priv->desc_types = *prop;
  7858. +
  7859. + if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
  7860. + !priv->exec_units || !priv->desc_types) {
  7861. + dev_err(dev, "invalid property data in device tree node\n");
  7862. + err = -EINVAL;
  7863. + goto err_out;
  7864. + }
  7865. +
  7866. + of_node_put(np);
  7867. + np = NULL;
  7868. +
  7869. + priv->head_lock = kmalloc(sizeof(spinlock_t) * priv->num_channels,
  7870. + GFP_KERNEL);
  7871. + priv->tail_lock = kmalloc(sizeof(spinlock_t) * priv->num_channels,
  7872. + GFP_KERNEL);
  7873. + if (!priv->head_lock || !priv->tail_lock) {
  7874. + dev_err(dev, "failed to allocate fifo locks\n");
  7875. + err = -ENOMEM;
  7876. + goto err_out;
  7877. + }
  7878. +
  7879. + for (i = 0; i < priv->num_channels; i++) {
  7880. + spin_lock_init(&priv->head_lock[i]);
  7881. + spin_lock_init(&priv->tail_lock[i]);
  7882. + }
  7883. +
  7884. + priv->fifo = kmalloc(sizeof(struct talitos_request *) *
  7885. + priv->num_channels, GFP_KERNEL);
  7886. + if (!priv->fifo) {
  7887. + dev_err(dev, "failed to allocate request fifo\n");
  7888. + err = -ENOMEM;
  7889. + goto err_out;
  7890. + }
  7891. +
  7892. + priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
  7893. +
  7894. + for (i = 0; i < priv->num_channels; i++) {
  7895. + priv->fifo[i] = kzalloc(sizeof(struct talitos_request) *
  7896. + priv->fifo_len, GFP_KERNEL);
  7897. + if (!priv->fifo[i]) {
  7898. + dev_err(dev, "failed to allocate request fifo %d\n", i);
  7899. + err = -ENOMEM;
  7900. + goto err_out;
  7901. + }
  7902. + }
  7903. +
  7904. + priv->head = kzalloc(sizeof(int) * priv->num_channels, GFP_KERNEL);
  7905. + priv->tail = kzalloc(sizeof(int) * priv->num_channels, GFP_KERNEL);
  7906. + if (!priv->head || !priv->tail) {
  7907. + dev_err(dev, "failed to allocate request index space\n");
  7908. + err = -ENOMEM;
  7909. + goto err_out;
  7910. + }
  7911. +
  7912. + /* reset and initialize the h/w */
  7913. + err = init_device(dev);
  7914. + if (err) {
  7915. + dev_err(dev, "failed to initialize device\n");
  7916. + goto err_out;
  7917. + }
  7918. +
  7919. + /* register the RNG, if available */
  7920. + if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
  7921. + err = talitos_register_rng(dev);
  7922. + if (err) {
  7923. + dev_err(dev, "failed to register hwrng: %d\n", err);
  7924. + goto err_out;
  7925. + } else
  7926. + dev_info(dev, "hwrng\n");
  7927. + }
  7928. +
  7929. + /* register crypto algorithms the device supports */
  7930. + INIT_LIST_HEAD(&priv->alg_list);
  7931. +
  7932. + for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
  7933. + if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
  7934. + struct talitos_crypto_alg *t_alg;
  7935. +
  7936. + t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
  7937. + if (IS_ERR(t_alg)) {
  7938. + err = PTR_ERR(t_alg);
  7939. + goto err_out;
  7940. + }
  7941. +
  7942. + err = crypto_register_alg(&t_alg->crypto_alg);
  7943. + if (err) {
  7944. + dev_err(dev, "%s alg registration failed\n",
  7945. + t_alg->crypto_alg.cra_driver_name);
  7946. + kfree(t_alg);
  7947. + } else {
  7948. + list_add_tail(&t_alg->entry, &priv->alg_list);
  7949. + dev_info(dev, "%s\n",
  7950. + t_alg->crypto_alg.cra_driver_name);
  7951. + }
  7952. + }
  7953. + }
  7954. +
  7955. + return 0;
  7956. +
  7957. +err_out:
  7958. + talitos_remove(ofdev);
  7959. + if (np)
  7960. + of_node_put(np);
  7961. +
  7962. + return err;
  7963. +}
  7964. +
  7965. +static struct of_device_id talitos_match[] = {
  7966. + {
  7967. + .compatible = "fsl,sec2.0",
  7968. + },
  7969. + {},
  7970. +};
  7971. +MODULE_DEVICE_TABLE(of, talitos_match);
  7972. +
  7973. +static struct of_platform_driver talitos_driver = {
  7974. + .name = "talitos",
  7975. + .match_table = talitos_match,
  7976. + .probe = talitos_probe,
  7977. + .remove = __devexit_p(talitos_remove),
  7978. +};
  7979. +
  7980. +static int __init talitos_init(void)
  7981. +{
  7982. + return of_register_platform_driver(&talitos_driver);
  7983. +}
  7984. +module_init(talitos_init);
  7985. +
  7986. +static void __exit talitos_exit(void)
  7987. +{
  7988. + of_unregister_platform_driver(&talitos_driver);
  7989. +}
  7990. +module_exit(talitos_exit);
  7991. +
  7992. +MODULE_LICENSE("GPL");
  7993. +MODULE_AUTHOR("Kim Phillips <[email protected]>");
  7994. +MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");
  7995. --- /dev/null
  7996. +++ b/drivers/crypto/talitos.h
  7997. @@ -0,0 +1,199 @@
  7998. +/*
  7999. + * Freescale SEC (talitos) device register and descriptor header defines
  8000. + *
  8001. + * Copyright (c) 2006-2008 Freescale Semiconductor, Inc.
  8002. + *
  8003. + * Redistribution and use in source and binary forms, with or without
  8004. + * modification, are permitted provided that the following conditions
  8005. + * are met:
  8006. + *
  8007. + * 1. Redistributions of source code must retain the above copyright
  8008. + * notice, this list of conditions and the following disclaimer.
  8009. + * 2. Redistributions in binary form must reproduce the above copyright
  8010. + * notice, this list of conditions and the following disclaimer in the
  8011. + * documentation and/or other materials provided with the distribution.
  8012. + * 3. The name of the author may not be used to endorse or promote products
  8013. + * derived from this software without specific prior written permission.
  8014. + *
  8015. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  8016. + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  8017. + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  8018. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  8019. + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  8020. + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  8021. + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  8022. + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  8023. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  8024. + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  8025. + *
  8026. + */
  8027. +
  8028. +/*
  8029. + * TALITOS_xxx_LO addresses point to the low data bits (32-63) of the register
  8030. + */
  8031. +
  8032. +/* global register offset addresses */
  8033. +#define TALITOS_MCR 0x1030 /* master control register */
  8034. +#define TALITOS_MCR_LO 0x1038
  8035. +#define TALITOS_MCR_SWR 0x1 /* s/w reset */
  8036. +#define TALITOS_IMR 0x1008 /* interrupt mask register */
  8037. +#define TALITOS_IMR_INIT 0x10fff /* enable channel IRQs */
  8038. +#define TALITOS_IMR_LO 0x100C
  8039. +#define TALITOS_IMR_LO_INIT 0x20000 /* allow RNGU error IRQs */
  8040. +#define TALITOS_ISR 0x1010 /* interrupt status register */
  8041. +#define TALITOS_ISR_CHERR 0xaa /* channel errors mask */
  8042. +#define TALITOS_ISR_CHDONE 0x55 /* channel done mask */
  8043. +#define TALITOS_ISR_LO 0x1014
  8044. +#define TALITOS_ICR 0x1018 /* interrupt clear register */
  8045. +#define TALITOS_ICR_LO 0x101C
  8046. +
  8047. +/* channel register address stride */
  8048. +#define TALITOS_CH_STRIDE 0x100
  8049. +
  8050. +/* channel configuration register */
  8051. +#define TALITOS_CCCR(ch) (ch * TALITOS_CH_STRIDE + 0x1108)
  8052. +#define TALITOS_CCCR_CONT 0x2 /* channel continue */
  8053. +#define TALITOS_CCCR_RESET 0x1 /* channel reset */
  8054. +#define TALITOS_CCCR_LO(ch) (ch * TALITOS_CH_STRIDE + 0x110c)
  8055. +#define TALITOS_CCCR_LO_CDWE 0x10 /* chan. done writeback enab. */
  8056. +#define TALITOS_CCCR_LO_NT 0x4 /* notification type */
  8057. +#define TALITOS_CCCR_LO_CDIE 0x2 /* channel done IRQ enable */
  8058. +
  8059. +/* CCPSR: channel pointer status register */
  8060. +#define TALITOS_CCPSR(ch) (ch * TALITOS_CH_STRIDE + 0x1110)
  8061. +#define TALITOS_CCPSR_LO(ch) (ch * TALITOS_CH_STRIDE + 0x1114)
  8062. +#define TALITOS_CCPSR_LO_DOF 0x8000 /* double FF write oflow error */
  8063. +#define TALITOS_CCPSR_LO_SOF 0x4000 /* single FF write oflow error */
  8064. +#define TALITOS_CCPSR_LO_MDTE 0x2000 /* master data transfer error */
  8065. +#define TALITOS_CCPSR_LO_SGDLZ 0x1000 /* s/g data len zero error */
  8066. +#define TALITOS_CCPSR_LO_FPZ 0x0800 /* fetch ptr zero error */
  8067. +#define TALITOS_CCPSR_LO_IDH 0x0400 /* illegal desc hdr error */
  8068. +#define TALITOS_CCPSR_LO_IEU 0x0200 /* invalid EU error */
  8069. +#define TALITOS_CCPSR_LO_EU 0x0100 /* EU error detected */
  8070. +#define TALITOS_CCPSR_LO_GB 0x0080 /* gather boundary error */
  8071. +#define TALITOS_CCPSR_LO_GRL 0x0040 /* gather return/length error */
  8072. +#define TALITOS_CCPSR_LO_SB 0x0020 /* scatter boundary error */
  8073. +#define TALITOS_CCPSR_LO_SRL 0x0010 /* scatter return/length error */
  8074. +
  8075. +/* channel fetch fifo register */
  8076. +#define TALITOS_FF(ch) (ch * TALITOS_CH_STRIDE + 0x1148)
  8077. +#define TALITOS_FF_LO(ch) (ch * TALITOS_CH_STRIDE + 0x114c)
  8078. +
  8079. +/* current descriptor pointer register */
  8080. +#define TALITOS_CDPR(ch) (ch * TALITOS_CH_STRIDE + 0x1140)
  8081. +#define TALITOS_CDPR_LO(ch) (ch * TALITOS_CH_STRIDE + 0x1144)
  8082. +
  8083. +/* descriptor buffer register */
  8084. +#define TALITOS_DESCBUF(ch) (ch * TALITOS_CH_STRIDE + 0x1180)
  8085. +#define TALITOS_DESCBUF_LO(ch) (ch * TALITOS_CH_STRIDE + 0x1184)
  8086. +
  8087. +/* gather link table */
  8088. +#define TALITOS_GATHER(ch) (ch * TALITOS_CH_STRIDE + 0x11c0)
  8089. +#define TALITOS_GATHER_LO(ch) (ch * TALITOS_CH_STRIDE + 0x11c4)
  8090. +
  8091. +/* scatter link table */
  8092. +#define TALITOS_SCATTER(ch) (ch * TALITOS_CH_STRIDE + 0x11e0)
  8093. +#define TALITOS_SCATTER_LO(ch) (ch * TALITOS_CH_STRIDE + 0x11e4)
  8094. +
  8095. +/* execution unit interrupt status registers */
  8096. +#define TALITOS_DEUISR 0x2030 /* DES unit */
  8097. +#define TALITOS_DEUISR_LO 0x2034
  8098. +#define TALITOS_AESUISR 0x4030 /* AES unit */
  8099. +#define TALITOS_AESUISR_LO 0x4034
  8100. +#define TALITOS_MDEUISR 0x6030 /* message digest unit */
  8101. +#define TALITOS_MDEUISR_LO 0x6034
  8102. +#define TALITOS_AFEUISR 0x8030 /* arc4 unit */
  8103. +#define TALITOS_AFEUISR_LO 0x8034
  8104. +#define TALITOS_RNGUISR 0xa030 /* random number unit */
  8105. +#define TALITOS_RNGUISR_LO 0xa034
  8106. +#define TALITOS_RNGUSR 0xa028 /* rng status */
  8107. +#define TALITOS_RNGUSR_LO 0xa02c
  8108. +#define TALITOS_RNGUSR_LO_RD 0x1 /* reset done */
  8109. +#define TALITOS_RNGUSR_LO_OFL 0xff0000/* output FIFO length */
  8110. +#define TALITOS_RNGUDSR 0xa010 /* data size */
  8111. +#define TALITOS_RNGUDSR_LO 0xa014
  8112. +#define TALITOS_RNGU_FIFO 0xa800 /* output FIFO */
  8113. +#define TALITOS_RNGU_FIFO_LO 0xa804 /* output FIFO */
  8114. +#define TALITOS_RNGURCR 0xa018 /* reset control */
  8115. +#define TALITOS_RNGURCR_LO 0xa01c
  8116. +#define TALITOS_RNGURCR_LO_SR 0x1 /* software reset */
  8117. +#define TALITOS_PKEUISR 0xc030 /* public key unit */
  8118. +#define TALITOS_PKEUISR_LO 0xc034
  8119. +#define TALITOS_KEUISR 0xe030 /* kasumi unit */
  8120. +#define TALITOS_KEUISR_LO 0xe034
  8121. +#define TALITOS_CRCUISR 0xf030 /* cyclic redundancy check unit*/
  8122. +#define TALITOS_CRCUISR_LO 0xf034
  8123. +
  8124. +/*
  8125. + * talitos descriptor header (hdr) bits
  8126. + */
  8127. +
  8128. +/* written back when done */
  8129. +#define DESC_HDR_DONE __constant_cpu_to_be32(0xff000000)
  8130. +
  8131. +/* primary execution unit select */
  8132. +#define DESC_HDR_SEL0_MASK __constant_cpu_to_be32(0xf0000000)
  8133. +#define DESC_HDR_SEL0_AFEU __constant_cpu_to_be32(0x10000000)
  8134. +#define DESC_HDR_SEL0_DEU __constant_cpu_to_be32(0x20000000)
  8135. +#define DESC_HDR_SEL0_MDEUA __constant_cpu_to_be32(0x30000000)
  8136. +#define DESC_HDR_SEL0_MDEUB __constant_cpu_to_be32(0xb0000000)
  8137. +#define DESC_HDR_SEL0_RNG __constant_cpu_to_be32(0x40000000)
  8138. +#define DESC_HDR_SEL0_PKEU __constant_cpu_to_be32(0x50000000)
  8139. +#define DESC_HDR_SEL0_AESU __constant_cpu_to_be32(0x60000000)
  8140. +#define DESC_HDR_SEL0_KEU __constant_cpu_to_be32(0x70000000)
  8141. +#define DESC_HDR_SEL0_CRCU __constant_cpu_to_be32(0x80000000)
  8142. +
  8143. +/* primary execution unit mode (MODE0) and derivatives */
  8144. +#define DESC_HDR_MODE0_ENCRYPT __constant_cpu_to_be32(0x00100000)
  8145. +#define DESC_HDR_MODE0_AESU_CBC __constant_cpu_to_be32(0x00200000)
  8146. +#define DESC_HDR_MODE0_DEU_CBC __constant_cpu_to_be32(0x00400000)
  8147. +#define DESC_HDR_MODE0_DEU_3DES __constant_cpu_to_be32(0x00200000)
  8148. +#define DESC_HDR_MODE0_MDEU_INIT __constant_cpu_to_be32(0x01000000)
  8149. +#define DESC_HDR_MODE0_MDEU_HMAC __constant_cpu_to_be32(0x00800000)
  8150. +#define DESC_HDR_MODE0_MDEU_PAD __constant_cpu_to_be32(0x00400000)
  8151. +#define DESC_HDR_MODE0_MDEU_MD5 __constant_cpu_to_be32(0x00200000)
  8152. +#define DESC_HDR_MODE0_MDEU_SHA256 __constant_cpu_to_be32(0x00100000)
  8153. +#define DESC_HDR_MODE0_MDEU_SHA1 __constant_cpu_to_be32(0x00000000)
  8154. +#define DESC_HDR_MODE0_MDEU_MD5_HMAC (DESC_HDR_MODE0_MDEU_MD5 | \
  8155. + DESC_HDR_MODE0_MDEU_HMAC)
  8156. +#define DESC_HDR_MODE0_MDEU_SHA256_HMAC (DESC_HDR_MODE0_MDEU_SHA256 | \
  8157. + DESC_HDR_MODE0_MDEU_HMAC)
  8158. +#define DESC_HDR_MODE0_MDEU_SHA1_HMAC (DESC_HDR_MODE0_MDEU_SHA1 | \
  8159. + DESC_HDR_MODE0_MDEU_HMAC)
  8160. +
  8161. +/* secondary execution unit select (SEL1) */
  8162. +#define DESC_HDR_SEL1_MASK __constant_cpu_to_be32(0x000f0000)
  8163. +#define DESC_HDR_SEL1_MDEUA __constant_cpu_to_be32(0x00030000)
  8164. +#define DESC_HDR_SEL1_MDEUB __constant_cpu_to_be32(0x000b0000)
  8165. +#define DESC_HDR_SEL1_CRCU __constant_cpu_to_be32(0x00080000)
  8166. +
  8167. +/* secondary execution unit mode (MODE1) and derivatives */
  8168. +#define DESC_HDR_MODE1_MDEU_INIT __constant_cpu_to_be32(0x00001000)
  8169. +#define DESC_HDR_MODE1_MDEU_HMAC __constant_cpu_to_be32(0x00000800)
  8170. +#define DESC_HDR_MODE1_MDEU_PAD __constant_cpu_to_be32(0x00000400)
  8171. +#define DESC_HDR_MODE1_MDEU_MD5 __constant_cpu_to_be32(0x00000200)
  8172. +#define DESC_HDR_MODE1_MDEU_SHA256 __constant_cpu_to_be32(0x00000100)
  8173. +#define DESC_HDR_MODE1_MDEU_SHA1 __constant_cpu_to_be32(0x00000000)
  8174. +#define DESC_HDR_MODE1_MDEU_MD5_HMAC (DESC_HDR_MODE1_MDEU_MD5 | \
  8175. + DESC_HDR_MODE1_MDEU_HMAC)
  8176. +#define DESC_HDR_MODE1_MDEU_SHA256_HMAC (DESC_HDR_MODE1_MDEU_SHA256 | \
  8177. + DESC_HDR_MODE1_MDEU_HMAC)
  8178. +#define DESC_HDR_MODE1_MDEU_SHA1_HMAC (DESC_HDR_MODE1_MDEU_SHA1 | \
  8179. + DESC_HDR_MODE1_MDEU_HMAC)
  8180. +
  8181. +/* direction of overall data flow (DIR) */
  8182. +#define DESC_HDR_DIR_INBOUND __constant_cpu_to_be32(0x00000002)
  8183. +
  8184. +/* request done notification (DN) */
  8185. +#define DESC_HDR_DONE_NOTIFY __constant_cpu_to_be32(0x00000001)
  8186. +
  8187. +/* descriptor types */
  8188. +#define DESC_HDR_TYPE_AESU_CTR_NONSNOOP __constant_cpu_to_be32(0 << 3)
  8189. +#define DESC_HDR_TYPE_IPSEC_ESP __constant_cpu_to_be32(1 << 3)
  8190. +#define DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU __constant_cpu_to_be32(2 << 3)
  8191. +#define DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU __constant_cpu_to_be32(4 << 3)
  8192. +
  8193. +/* link table extent field bits */
  8194. +#define DESC_PTR_LNKTBL_JUMP 0x80
  8195. +#define DESC_PTR_LNKTBL_RETURN 0x02
  8196. +#define DESC_PTR_LNKTBL_NEXT 0x01
  8197. --- /dev/null
  8198. +++ b/include/crypto/hash.h
  8199. @@ -0,0 +1,154 @@
  8200. +/*
  8201. + * Hash: Hash algorithms under the crypto API
  8202. + *
  8203. + * Copyright (c) 2008 Herbert Xu <[email protected]>
  8204. + *
  8205. + * This program is free software; you can redistribute it and/or modify it
  8206. + * under the terms of the GNU General Public License as published by the Free
  8207. + * Software Foundation; either version 2 of the License, or (at your option)
  8208. + * any later version.
  8209. + *
  8210. + */
  8211. +
  8212. +#ifndef _CRYPTO_HASH_H
  8213. +#define _CRYPTO_HASH_H
  8214. +
  8215. +#include <linux/crypto.h>
  8216. +
  8217. +struct crypto_ahash {
  8218. + struct crypto_tfm base;
  8219. +};
  8220. +
  8221. +static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
  8222. +{
  8223. + return (struct crypto_ahash *)tfm;
  8224. +}
  8225. +
  8226. +static inline struct crypto_ahash *crypto_alloc_ahash(const char *alg_name,
  8227. + u32 type, u32 mask)
  8228. +{
  8229. + type &= ~CRYPTO_ALG_TYPE_MASK;
  8230. + mask &= ~CRYPTO_ALG_TYPE_MASK;
  8231. + type |= CRYPTO_ALG_TYPE_AHASH;
  8232. + mask |= CRYPTO_ALG_TYPE_AHASH_MASK;
  8233. +
  8234. + return __crypto_ahash_cast(crypto_alloc_base(alg_name, type, mask));
  8235. +}
  8236. +
  8237. +static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
  8238. +{
  8239. + return &tfm->base;
  8240. +}
  8241. +
  8242. +static inline void crypto_free_ahash(struct crypto_ahash *tfm)
  8243. +{
  8244. + crypto_free_tfm(crypto_ahash_tfm(tfm));
  8245. +}
  8246. +
  8247. +static inline unsigned int crypto_ahash_alignmask(
  8248. + struct crypto_ahash *tfm)
  8249. +{
  8250. + return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
  8251. +}
  8252. +
  8253. +static inline struct ahash_tfm *crypto_ahash_crt(struct crypto_ahash *tfm)
  8254. +{
  8255. + return &crypto_ahash_tfm(tfm)->crt_ahash;
  8256. +}
  8257. +
  8258. +static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
  8259. +{
  8260. + return crypto_ahash_crt(tfm)->digestsize;
  8261. +}
  8262. +
  8263. +static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
  8264. +{
  8265. + return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
  8266. +}
  8267. +
  8268. +static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
  8269. +{
  8270. + crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
  8271. +}
  8272. +
  8273. +static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
  8274. +{
  8275. + crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
  8276. +}
  8277. +
  8278. +static inline struct crypto_ahash *crypto_ahash_reqtfm(
  8279. + struct ahash_request *req)
  8280. +{
  8281. + return __crypto_ahash_cast(req->base.tfm);
  8282. +}
  8283. +
  8284. +static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
  8285. +{
  8286. + return crypto_ahash_crt(tfm)->reqsize;
  8287. +}
  8288. +
  8289. +static inline int crypto_ahash_setkey(struct crypto_ahash *tfm,
  8290. + const u8 *key, unsigned int keylen)
  8291. +{
  8292. + struct ahash_tfm *crt = crypto_ahash_crt(tfm);
  8293. +
  8294. + return crt->setkey(tfm, key, keylen);
  8295. +}
  8296. +
  8297. +static inline int crypto_ahash_digest(struct ahash_request *req)
  8298. +{
  8299. + struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
  8300. + return crt->digest(req);
  8301. +}
  8302. +
  8303. +static inline void ahash_request_set_tfm(struct ahash_request *req,
  8304. + struct crypto_ahash *tfm)
  8305. +{
  8306. + req->base.tfm = crypto_ahash_tfm(tfm);
  8307. +}
  8308. +
  8309. +static inline struct ahash_request *ahash_request_alloc(
  8310. + struct crypto_ahash *tfm, gfp_t gfp)
  8311. +{
  8312. + struct ahash_request *req;
  8313. +
  8314. + req = kmalloc(sizeof(struct ahash_request) +
  8315. + crypto_ahash_reqsize(tfm), gfp);
  8316. +
  8317. + if (likely(req))
  8318. + ahash_request_set_tfm(req, tfm);
  8319. +
  8320. + return req;
  8321. +}
  8322. +
  8323. +static inline void ahash_request_free(struct ahash_request *req)
  8324. +{
  8325. + kfree(req);
  8326. +}
  8327. +
  8328. +static inline struct ahash_request *ahash_request_cast(
  8329. + struct crypto_async_request *req)
  8330. +{
  8331. + return container_of(req, struct ahash_request, base);
  8332. +}
  8333. +
  8334. +static inline void ahash_request_set_callback(struct ahash_request *req,
  8335. + u32 flags,
  8336. + crypto_completion_t complete,
  8337. + void *data)
  8338. +{
  8339. + req->base.complete = complete;
  8340. + req->base.data = data;
  8341. + req->base.flags = flags;
  8342. +}
  8343. +
  8344. +static inline void ahash_request_set_crypt(struct ahash_request *req,
  8345. + struct scatterlist *src, u8 *result,
  8346. + unsigned int nbytes)
  8347. +{
  8348. + req->src = src;
  8349. + req->nbytes = nbytes;
  8350. + req->result = result;
  8351. +}
  8352. +
  8353. +#endif /* _CRYPTO_HASH_H */
  8354. --- /dev/null
  8355. +++ b/include/crypto/internal/hash.h
  8356. @@ -0,0 +1,78 @@
  8357. +/*
  8358. + * Hash algorithms.
  8359. + *
  8360. + * Copyright (c) 2008 Herbert Xu <[email protected]>
  8361. + *
  8362. + * This program is free software; you can redistribute it and/or modify it
  8363. + * under the terms of the GNU General Public License as published by the Free
  8364. + * Software Foundation; either version 2 of the License, or (at your option)
  8365. + * any later version.
  8366. + *
  8367. + */
  8368. +
  8369. +#ifndef _CRYPTO_INTERNAL_HASH_H
  8370. +#define _CRYPTO_INTERNAL_HASH_H
  8371. +
  8372. +#include <crypto/algapi.h>
  8373. +#include <crypto/hash.h>
  8374. +
  8375. +struct ahash_request;
  8376. +struct scatterlist;
  8377. +
  8378. +struct crypto_hash_walk {
  8379. + char *data;
  8380. +
  8381. + unsigned int offset;
  8382. + unsigned int alignmask;
  8383. +
  8384. + struct page *pg;
  8385. + unsigned int entrylen;
  8386. +
  8387. + unsigned int total;
  8388. + struct scatterlist *sg;
  8389. +
  8390. + unsigned int flags;
  8391. +};
  8392. +
  8393. +extern const struct crypto_type crypto_ahash_type;
  8394. +
  8395. +int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err);
  8396. +int crypto_hash_walk_first(struct ahash_request *req,
  8397. + struct crypto_hash_walk *walk);
  8398. +
  8399. +static inline void *crypto_ahash_ctx(struct crypto_ahash *tfm)
  8400. +{
  8401. + return crypto_tfm_ctx(&tfm->base);
  8402. +}
  8403. +
  8404. +static inline struct ahash_alg *crypto_ahash_alg(
  8405. + struct crypto_ahash *tfm)
  8406. +{
  8407. + return &crypto_ahash_tfm(tfm)->__crt_alg->cra_ahash;
  8408. +}
  8409. +
  8410. +static inline int ahash_enqueue_request(struct crypto_queue *queue,
  8411. + struct ahash_request *request)
  8412. +{
  8413. + return crypto_enqueue_request(queue, &request->base);
  8414. +}
  8415. +
  8416. +static inline struct ahash_request *ahash_dequeue_request(
  8417. + struct crypto_queue *queue)
  8418. +{
  8419. + return ahash_request_cast(crypto_dequeue_request(queue));
  8420. +}
  8421. +
  8422. +static inline void *ahash_request_ctx(struct ahash_request *req)
  8423. +{
  8424. + return req->__ctx;
  8425. +}
  8426. +
  8427. +static inline int ahash_tfm_in_queue(struct crypto_queue *queue,
  8428. + struct crypto_ahash *tfm)
  8429. +{
  8430. + return crypto_tfm_in_queue(queue, crypto_ahash_tfm(tfm));
  8431. +}
  8432. +
  8433. +#endif /* _CRYPTO_INTERNAL_HASH_H */
  8434. +
  8435. --- a/include/linux/crypto.h
  8436. +++ b/include/linux/crypto.h
  8437. @@ -30,15 +30,17 @@
  8438. */
  8439. #define CRYPTO_ALG_TYPE_MASK 0x0000000f
  8440. #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
  8441. -#define CRYPTO_ALG_TYPE_DIGEST 0x00000002
  8442. -#define CRYPTO_ALG_TYPE_HASH 0x00000003
  8443. +#define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
  8444. +#define CRYPTO_ALG_TYPE_AEAD 0x00000003
  8445. #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
  8446. #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
  8447. #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
  8448. -#define CRYPTO_ALG_TYPE_COMPRESS 0x00000008
  8449. -#define CRYPTO_ALG_TYPE_AEAD 0x00000009
  8450. +#define CRYPTO_ALG_TYPE_DIGEST 0x00000008
  8451. +#define CRYPTO_ALG_TYPE_HASH 0x00000009
  8452. +#define CRYPTO_ALG_TYPE_AHASH 0x0000000a
  8453. #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
  8454. +#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
  8455. #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
  8456. #define CRYPTO_ALG_LARVAL 0x00000010
  8457. @@ -102,6 +104,7 @@ struct crypto_async_request;
  8458. struct crypto_aead;
  8459. struct crypto_blkcipher;
  8460. struct crypto_hash;
  8461. +struct crypto_ahash;
  8462. struct crypto_tfm;
  8463. struct crypto_type;
  8464. struct aead_givcrypt_request;
  8465. @@ -131,6 +134,16 @@ struct ablkcipher_request {
  8466. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  8467. };
  8468. +struct ahash_request {
  8469. + struct crypto_async_request base;
  8470. +
  8471. + unsigned int nbytes;
  8472. + struct scatterlist *src;
  8473. + u8 *result;
  8474. +
  8475. + void *__ctx[] CRYPTO_MINALIGN_ATTR;
  8476. +};
  8477. +
  8478. /**
  8479. * struct aead_request - AEAD request
  8480. * @base: Common attributes for async crypto requests
  8481. @@ -195,6 +208,17 @@ struct ablkcipher_alg {
  8482. unsigned int ivsize;
  8483. };
  8484. +struct ahash_alg {
  8485. + int (*init)(struct ahash_request *req);
  8486. + int (*update)(struct ahash_request *req);
  8487. + int (*final)(struct ahash_request *req);
  8488. + int (*digest)(struct ahash_request *req);
  8489. + int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
  8490. + unsigned int keylen);
  8491. +
  8492. + unsigned int digestsize;
  8493. +};
  8494. +
  8495. struct aead_alg {
  8496. int (*setkey)(struct crypto_aead *tfm, const u8 *key,
  8497. unsigned int keylen);
  8498. @@ -272,6 +296,7 @@ struct compress_alg {
  8499. #define cra_cipher cra_u.cipher
  8500. #define cra_digest cra_u.digest
  8501. #define cra_hash cra_u.hash
  8502. +#define cra_ahash cra_u.ahash
  8503. #define cra_compress cra_u.compress
  8504. struct crypto_alg {
  8505. @@ -298,6 +323,7 @@ struct crypto_alg {
  8506. struct cipher_alg cipher;
  8507. struct digest_alg digest;
  8508. struct hash_alg hash;
  8509. + struct ahash_alg ahash;
  8510. struct compress_alg compress;
  8511. } cra_u;
  8512. @@ -383,6 +409,18 @@ struct hash_tfm {
  8513. unsigned int digestsize;
  8514. };
  8515. +struct ahash_tfm {
  8516. + int (*init)(struct ahash_request *req);
  8517. + int (*update)(struct ahash_request *req);
  8518. + int (*final)(struct ahash_request *req);
  8519. + int (*digest)(struct ahash_request *req);
  8520. + int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
  8521. + unsigned int keylen);
  8522. +
  8523. + unsigned int digestsize;
  8524. + unsigned int reqsize;
  8525. +};
  8526. +
  8527. struct compress_tfm {
  8528. int (*cot_compress)(struct crypto_tfm *tfm,
  8529. const u8 *src, unsigned int slen,
  8530. @@ -397,6 +435,7 @@ struct compress_tfm {
  8531. #define crt_blkcipher crt_u.blkcipher
  8532. #define crt_cipher crt_u.cipher
  8533. #define crt_hash crt_u.hash
  8534. +#define crt_ahash crt_u.ahash
  8535. #define crt_compress crt_u.compress
  8536. struct crypto_tfm {
  8537. @@ -409,6 +448,7 @@ struct crypto_tfm {
  8538. struct blkcipher_tfm blkcipher;
  8539. struct cipher_tfm cipher;
  8540. struct hash_tfm hash;
  8541. + struct ahash_tfm ahash;
  8542. struct compress_tfm compress;
  8543. } crt_u;