2
0

e_aes.c 140 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208
  1. /*
  2. * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/opensslconf.h>
  10. #include <openssl/crypto.h>
  11. #include <openssl/evp.h>
  12. #include <openssl/err.h>
  13. #include <string.h>
  14. #include <assert.h>
  15. #include <openssl/aes.h>
  16. #include "internal/evp_int.h"
  17. #include "modes_lcl.h"
  18. #include <openssl/rand.h>
  19. #include "evp_locl.h"
  20. typedef struct {
  21. union {
  22. double align;
  23. AES_KEY ks;
  24. } ks;
  25. block128_f block;
  26. union {
  27. cbc128_f cbc;
  28. ctr128_f ctr;
  29. } stream;
  30. } EVP_AES_KEY;
  31. typedef struct {
  32. union {
  33. double align;
  34. AES_KEY ks;
  35. } ks; /* AES key schedule to use */
  36. int key_set; /* Set if key initialised */
  37. int iv_set; /* Set if an iv is set */
  38. GCM128_CONTEXT gcm;
  39. unsigned char *iv; /* Temporary IV store */
  40. int ivlen; /* IV length */
  41. int taglen;
  42. int iv_gen; /* It is OK to generate IVs */
  43. int tls_aad_len; /* TLS AAD length */
  44. ctr128_f ctr;
  45. } EVP_AES_GCM_CTX;
  46. typedef struct {
  47. union {
  48. double align;
  49. AES_KEY ks;
  50. } ks1, ks2; /* AES key schedules to use */
  51. XTS128_CONTEXT xts;
  52. void (*stream) (const unsigned char *in,
  53. unsigned char *out, size_t length,
  54. const AES_KEY *key1, const AES_KEY *key2,
  55. const unsigned char iv[16]);
  56. } EVP_AES_XTS_CTX;
  57. typedef struct {
  58. union {
  59. double align;
  60. AES_KEY ks;
  61. } ks; /* AES key schedule to use */
  62. int key_set; /* Set if key initialised */
  63. int iv_set; /* Set if an iv is set */
  64. int tag_set; /* Set if tag is valid */
  65. int len_set; /* Set if message length set */
  66. int L, M; /* L and M parameters from RFC3610 */
  67. int tls_aad_len; /* TLS AAD length */
  68. CCM128_CONTEXT ccm;
  69. ccm128_f str;
  70. } EVP_AES_CCM_CTX;
  71. #ifndef OPENSSL_NO_OCB
  72. typedef struct {
  73. union {
  74. double align;
  75. AES_KEY ks;
  76. } ksenc; /* AES key schedule to use for encryption */
  77. union {
  78. double align;
  79. AES_KEY ks;
  80. } ksdec; /* AES key schedule to use for decryption */
  81. int key_set; /* Set if key initialised */
  82. int iv_set; /* Set if an iv is set */
  83. OCB128_CONTEXT ocb;
  84. unsigned char *iv; /* Temporary IV store */
  85. unsigned char tag[16];
  86. unsigned char data_buf[16]; /* Store partial data blocks */
  87. unsigned char aad_buf[16]; /* Store partial AAD blocks */
  88. int data_buf_len;
  89. int aad_buf_len;
  90. int ivlen; /* IV length */
  91. int taglen;
  92. } EVP_AES_OCB_CTX;
  93. #endif
  94. #define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4))
  95. #ifdef VPAES_ASM
  96. int vpaes_set_encrypt_key(const unsigned char *userKey, int bits,
  97. AES_KEY *key);
  98. int vpaes_set_decrypt_key(const unsigned char *userKey, int bits,
  99. AES_KEY *key);
  100. void vpaes_encrypt(const unsigned char *in, unsigned char *out,
  101. const AES_KEY *key);
  102. void vpaes_decrypt(const unsigned char *in, unsigned char *out,
  103. const AES_KEY *key);
  104. void vpaes_cbc_encrypt(const unsigned char *in,
  105. unsigned char *out,
  106. size_t length,
  107. const AES_KEY *key, unsigned char *ivec, int enc);
  108. #endif
  109. #ifdef BSAES_ASM
  110. void bsaes_cbc_encrypt(const unsigned char *in, unsigned char *out,
  111. size_t length, const AES_KEY *key,
  112. unsigned char ivec[16], int enc);
  113. void bsaes_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
  114. size_t len, const AES_KEY *key,
  115. const unsigned char ivec[16]);
  116. void bsaes_xts_encrypt(const unsigned char *inp, unsigned char *out,
  117. size_t len, const AES_KEY *key1,
  118. const AES_KEY *key2, const unsigned char iv[16]);
  119. void bsaes_xts_decrypt(const unsigned char *inp, unsigned char *out,
  120. size_t len, const AES_KEY *key1,
  121. const AES_KEY *key2, const unsigned char iv[16]);
  122. #endif
  123. #ifdef AES_CTR_ASM
  124. void AES_ctr32_encrypt(const unsigned char *in, unsigned char *out,
  125. size_t blocks, const AES_KEY *key,
  126. const unsigned char ivec[AES_BLOCK_SIZE]);
  127. #endif
  128. #ifdef AES_XTS_ASM
  129. void AES_xts_encrypt(const unsigned char *inp, unsigned char *out, size_t len,
  130. const AES_KEY *key1, const AES_KEY *key2,
  131. const unsigned char iv[16]);
  132. void AES_xts_decrypt(const unsigned char *inp, unsigned char *out, size_t len,
  133. const AES_KEY *key1, const AES_KEY *key2,
  134. const unsigned char iv[16]);
  135. #endif
  136. #if defined(OPENSSL_CPUID_OBJ) && (defined(__powerpc__) || defined(__ppc__) || defined(_ARCH_PPC))
  137. # include "ppc_arch.h"
  138. # ifdef VPAES_ASM
  139. # define VPAES_CAPABLE (OPENSSL_ppccap_P & PPC_ALTIVEC)
  140. # endif
  141. # define HWAES_CAPABLE (OPENSSL_ppccap_P & PPC_CRYPTO207)
  142. # define HWAES_set_encrypt_key aes_p8_set_encrypt_key
  143. # define HWAES_set_decrypt_key aes_p8_set_decrypt_key
  144. # define HWAES_encrypt aes_p8_encrypt
  145. # define HWAES_decrypt aes_p8_decrypt
  146. # define HWAES_cbc_encrypt aes_p8_cbc_encrypt
  147. # define HWAES_ctr32_encrypt_blocks aes_p8_ctr32_encrypt_blocks
  148. # define HWAES_xts_encrypt aes_p8_xts_encrypt
  149. # define HWAES_xts_decrypt aes_p8_xts_decrypt
  150. #endif
  151. #if defined(AES_ASM) && !defined(I386_ONLY) && ( \
  152. ((defined(__i386) || defined(__i386__) || \
  153. defined(_M_IX86)) && defined(OPENSSL_IA32_SSE2))|| \
  154. defined(__x86_64) || defined(__x86_64__) || \
  155. defined(_M_AMD64) || defined(_M_X64) )
  156. extern unsigned int OPENSSL_ia32cap_P[];
  157. # ifdef VPAES_ASM
  158. # define VPAES_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(41-32)))
  159. # endif
  160. # ifdef BSAES_ASM
  161. # define BSAES_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(41-32)))
  162. # endif
  163. /*
  164. * AES-NI section
  165. */
  166. # define AESNI_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(57-32)))
  167. int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
  168. AES_KEY *key);
  169. int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
  170. AES_KEY *key);
  171. void aesni_encrypt(const unsigned char *in, unsigned char *out,
  172. const AES_KEY *key);
  173. void aesni_decrypt(const unsigned char *in, unsigned char *out,
  174. const AES_KEY *key);
  175. void aesni_ecb_encrypt(const unsigned char *in,
  176. unsigned char *out,
  177. size_t length, const AES_KEY *key, int enc);
  178. void aesni_cbc_encrypt(const unsigned char *in,
  179. unsigned char *out,
  180. size_t length,
  181. const AES_KEY *key, unsigned char *ivec, int enc);
  182. void aesni_ctr32_encrypt_blocks(const unsigned char *in,
  183. unsigned char *out,
  184. size_t blocks,
  185. const void *key, const unsigned char *ivec);
  186. void aesni_xts_encrypt(const unsigned char *in,
  187. unsigned char *out,
  188. size_t length,
  189. const AES_KEY *key1, const AES_KEY *key2,
  190. const unsigned char iv[16]);
  191. void aesni_xts_decrypt(const unsigned char *in,
  192. unsigned char *out,
  193. size_t length,
  194. const AES_KEY *key1, const AES_KEY *key2,
  195. const unsigned char iv[16]);
  196. void aesni_ccm64_encrypt_blocks(const unsigned char *in,
  197. unsigned char *out,
  198. size_t blocks,
  199. const void *key,
  200. const unsigned char ivec[16],
  201. unsigned char cmac[16]);
  202. void aesni_ccm64_decrypt_blocks(const unsigned char *in,
  203. unsigned char *out,
  204. size_t blocks,
  205. const void *key,
  206. const unsigned char ivec[16],
  207. unsigned char cmac[16]);
  208. # if defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)
  209. size_t aesni_gcm_encrypt(const unsigned char *in,
  210. unsigned char *out,
  211. size_t len,
  212. const void *key, unsigned char ivec[16], u64 *Xi);
  213. # define AES_gcm_encrypt aesni_gcm_encrypt
  214. size_t aesni_gcm_decrypt(const unsigned char *in,
  215. unsigned char *out,
  216. size_t len,
  217. const void *key, unsigned char ivec[16], u64 *Xi);
  218. # define AES_gcm_decrypt aesni_gcm_decrypt
  219. void gcm_ghash_avx(u64 Xi[2], const u128 Htable[16], const u8 *in,
  220. size_t len);
  221. # define AES_GCM_ASM(gctx) (gctx->ctr==aesni_ctr32_encrypt_blocks && \
  222. gctx->gcm.ghash==gcm_ghash_avx)
  223. # define AES_GCM_ASM2(gctx) (gctx->gcm.block==(block128_f)aesni_encrypt && \
  224. gctx->gcm.ghash==gcm_ghash_avx)
  225. # undef AES_GCM_ASM2 /* minor size optimization */
  226. # endif
  227. static int aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  228. const unsigned char *iv, int enc)
  229. {
  230. int ret, mode;
  231. EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
  232. mode = EVP_CIPHER_CTX_mode(ctx);
  233. if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
  234. && !enc) {
  235. ret = aesni_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  236. &dat->ks.ks);
  237. dat->block = (block128_f) aesni_decrypt;
  238. dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
  239. (cbc128_f) aesni_cbc_encrypt : NULL;
  240. } else {
  241. ret = aesni_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  242. &dat->ks.ks);
  243. dat->block = (block128_f) aesni_encrypt;
  244. if (mode == EVP_CIPH_CBC_MODE)
  245. dat->stream.cbc = (cbc128_f) aesni_cbc_encrypt;
  246. else if (mode == EVP_CIPH_CTR_MODE)
  247. dat->stream.ctr = (ctr128_f) aesni_ctr32_encrypt_blocks;
  248. else
  249. dat->stream.cbc = NULL;
  250. }
  251. if (ret < 0) {
  252. EVPerr(EVP_F_AESNI_INIT_KEY, EVP_R_AES_KEY_SETUP_FAILED);
  253. return 0;
  254. }
  255. return 1;
  256. }
  257. static int aesni_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  258. const unsigned char *in, size_t len)
  259. {
  260. aesni_cbc_encrypt(in, out, len, &EVP_C_DATA(EVP_AES_KEY,ctx)->ks.ks,
  261. EVP_CIPHER_CTX_iv_noconst(ctx),
  262. EVP_CIPHER_CTX_encrypting(ctx));
  263. return 1;
  264. }
  265. static int aesni_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  266. const unsigned char *in, size_t len)
  267. {
  268. size_t bl = EVP_CIPHER_CTX_block_size(ctx);
  269. if (len < bl)
  270. return 1;
  271. aesni_ecb_encrypt(in, out, len, &EVP_C_DATA(EVP_AES_KEY,ctx)->ks.ks,
  272. EVP_CIPHER_CTX_encrypting(ctx));
  273. return 1;
  274. }
  275. # define aesni_ofb_cipher aes_ofb_cipher
  276. static int aesni_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  277. const unsigned char *in, size_t len);
  278. # define aesni_cfb_cipher aes_cfb_cipher
  279. static int aesni_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  280. const unsigned char *in, size_t len);
  281. # define aesni_cfb8_cipher aes_cfb8_cipher
  282. static int aesni_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  283. const unsigned char *in, size_t len);
  284. # define aesni_cfb1_cipher aes_cfb1_cipher
  285. static int aesni_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  286. const unsigned char *in, size_t len);
  287. # define aesni_ctr_cipher aes_ctr_cipher
  288. static int aesni_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  289. const unsigned char *in, size_t len);
  290. static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  291. const unsigned char *iv, int enc)
  292. {
  293. EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,ctx);
  294. if (!iv && !key)
  295. return 1;
  296. if (key) {
  297. aesni_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  298. &gctx->ks.ks);
  299. CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f) aesni_encrypt);
  300. gctx->ctr = (ctr128_f) aesni_ctr32_encrypt_blocks;
  301. /*
  302. * If we have an iv can set it directly, otherwise use saved IV.
  303. */
  304. if (iv == NULL && gctx->iv_set)
  305. iv = gctx->iv;
  306. if (iv) {
  307. CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
  308. gctx->iv_set = 1;
  309. }
  310. gctx->key_set = 1;
  311. } else {
  312. /* If key set use IV, otherwise copy */
  313. if (gctx->key_set)
  314. CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
  315. else
  316. memcpy(gctx->iv, iv, gctx->ivlen);
  317. gctx->iv_set = 1;
  318. gctx->iv_gen = 0;
  319. }
  320. return 1;
  321. }
  322. # define aesni_gcm_cipher aes_gcm_cipher
  323. static int aesni_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  324. const unsigned char *in, size_t len);
  325. static int aesni_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  326. const unsigned char *iv, int enc)
  327. {
  328. EVP_AES_XTS_CTX *xctx = EVP_C_DATA(EVP_AES_XTS_CTX,ctx);
  329. if (!iv && !key)
  330. return 1;
  331. if (key) {
  332. /* key_len is two AES keys */
  333. if (enc) {
  334. aesni_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 4,
  335. &xctx->ks1.ks);
  336. xctx->xts.block1 = (block128_f) aesni_encrypt;
  337. xctx->stream = aesni_xts_encrypt;
  338. } else {
  339. aesni_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 4,
  340. &xctx->ks1.ks);
  341. xctx->xts.block1 = (block128_f) aesni_decrypt;
  342. xctx->stream = aesni_xts_decrypt;
  343. }
  344. aesni_set_encrypt_key(key + EVP_CIPHER_CTX_key_length(ctx) / 2,
  345. EVP_CIPHER_CTX_key_length(ctx) * 4,
  346. &xctx->ks2.ks);
  347. xctx->xts.block2 = (block128_f) aesni_encrypt;
  348. xctx->xts.key1 = &xctx->ks1;
  349. }
  350. if (iv) {
  351. xctx->xts.key2 = &xctx->ks2;
  352. memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 16);
  353. }
  354. return 1;
  355. }
  356. # define aesni_xts_cipher aes_xts_cipher
  357. static int aesni_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  358. const unsigned char *in, size_t len);
  359. static int aesni_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  360. const unsigned char *iv, int enc)
  361. {
  362. EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,ctx);
  363. if (!iv && !key)
  364. return 1;
  365. if (key) {
  366. aesni_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  367. &cctx->ks.ks);
  368. CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
  369. &cctx->ks, (block128_f) aesni_encrypt);
  370. cctx->str = enc ? (ccm128_f) aesni_ccm64_encrypt_blocks :
  371. (ccm128_f) aesni_ccm64_decrypt_blocks;
  372. cctx->key_set = 1;
  373. }
  374. if (iv) {
  375. memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 15 - cctx->L);
  376. cctx->iv_set = 1;
  377. }
  378. return 1;
  379. }
  380. # define aesni_ccm_cipher aes_ccm_cipher
  381. static int aesni_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  382. const unsigned char *in, size_t len);
  383. # ifndef OPENSSL_NO_OCB
  384. void aesni_ocb_encrypt(const unsigned char *in, unsigned char *out,
  385. size_t blocks, const void *key,
  386. size_t start_block_num,
  387. unsigned char offset_i[16],
  388. const unsigned char L_[][16],
  389. unsigned char checksum[16]);
  390. void aesni_ocb_decrypt(const unsigned char *in, unsigned char *out,
  391. size_t blocks, const void *key,
  392. size_t start_block_num,
  393. unsigned char offset_i[16],
  394. const unsigned char L_[][16],
  395. unsigned char checksum[16]);
  396. static int aesni_ocb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  397. const unsigned char *iv, int enc)
  398. {
  399. EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,ctx);
  400. if (!iv && !key)
  401. return 1;
  402. if (key) {
  403. do {
  404. /*
  405. * We set both the encrypt and decrypt key here because decrypt
  406. * needs both. We could possibly optimise to remove setting the
  407. * decrypt for an encryption operation.
  408. */
  409. aesni_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  410. &octx->ksenc.ks);
  411. aesni_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  412. &octx->ksdec.ks);
  413. if (!CRYPTO_ocb128_init(&octx->ocb,
  414. &octx->ksenc.ks, &octx->ksdec.ks,
  415. (block128_f) aesni_encrypt,
  416. (block128_f) aesni_decrypt,
  417. enc ? aesni_ocb_encrypt
  418. : aesni_ocb_decrypt))
  419. return 0;
  420. }
  421. while (0);
  422. /*
  423. * If we have an iv we can set it directly, otherwise use saved IV.
  424. */
  425. if (iv == NULL && octx->iv_set)
  426. iv = octx->iv;
  427. if (iv) {
  428. if (CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen)
  429. != 1)
  430. return 0;
  431. octx->iv_set = 1;
  432. }
  433. octx->key_set = 1;
  434. } else {
  435. /* If key set use IV, otherwise copy */
  436. if (octx->key_set)
  437. CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen);
  438. else
  439. memcpy(octx->iv, iv, octx->ivlen);
  440. octx->iv_set = 1;
  441. }
  442. return 1;
  443. }
  444. # define aesni_ocb_cipher aes_ocb_cipher
  445. static int aesni_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  446. const unsigned char *in, size_t len);
  447. # endif /* OPENSSL_NO_OCB */
  448. # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
  449. static const EVP_CIPHER aesni_##keylen##_##mode = { \
  450. nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
  451. flags|EVP_CIPH_##MODE##_MODE, \
  452. aesni_init_key, \
  453. aesni_##mode##_cipher, \
  454. NULL, \
  455. sizeof(EVP_AES_KEY), \
  456. NULL,NULL,NULL,NULL }; \
  457. static const EVP_CIPHER aes_##keylen##_##mode = { \
  458. nid##_##keylen##_##nmode,blocksize, \
  459. keylen/8,ivlen, \
  460. flags|EVP_CIPH_##MODE##_MODE, \
  461. aes_init_key, \
  462. aes_##mode##_cipher, \
  463. NULL, \
  464. sizeof(EVP_AES_KEY), \
  465. NULL,NULL,NULL,NULL }; \
  466. const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
  467. { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
  468. # define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
  469. static const EVP_CIPHER aesni_##keylen##_##mode = { \
  470. nid##_##keylen##_##mode,blocksize, \
  471. (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
  472. flags|EVP_CIPH_##MODE##_MODE, \
  473. aesni_##mode##_init_key, \
  474. aesni_##mode##_cipher, \
  475. aes_##mode##_cleanup, \
  476. sizeof(EVP_AES_##MODE##_CTX), \
  477. NULL,NULL,aes_##mode##_ctrl,NULL }; \
  478. static const EVP_CIPHER aes_##keylen##_##mode = { \
  479. nid##_##keylen##_##mode,blocksize, \
  480. (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
  481. flags|EVP_CIPH_##MODE##_MODE, \
  482. aes_##mode##_init_key, \
  483. aes_##mode##_cipher, \
  484. aes_##mode##_cleanup, \
  485. sizeof(EVP_AES_##MODE##_CTX), \
  486. NULL,NULL,aes_##mode##_ctrl,NULL }; \
  487. const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
  488. { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
  489. #elif defined(AES_ASM) && (defined(__sparc) || defined(__sparc__))
  490. # include "sparc_arch.h"
  491. extern unsigned int OPENSSL_sparcv9cap_P[];
  492. /*
  493. * Initial Fujitsu SPARC64 X support
  494. */
  495. # define HWAES_CAPABLE (OPENSSL_sparcv9cap_P[0] & SPARCV9_FJAESX)
  496. # define HWAES_set_encrypt_key aes_fx_set_encrypt_key
  497. # define HWAES_set_decrypt_key aes_fx_set_decrypt_key
  498. # define HWAES_encrypt aes_fx_encrypt
  499. # define HWAES_decrypt aes_fx_decrypt
  500. # define HWAES_cbc_encrypt aes_fx_cbc_encrypt
  501. # define HWAES_ctr32_encrypt_blocks aes_fx_ctr32_encrypt_blocks
  502. # define SPARC_AES_CAPABLE (OPENSSL_sparcv9cap_P[1] & CFR_AES)
  503. void aes_t4_set_encrypt_key(const unsigned char *key, int bits, AES_KEY *ks);
  504. void aes_t4_set_decrypt_key(const unsigned char *key, int bits, AES_KEY *ks);
  505. void aes_t4_encrypt(const unsigned char *in, unsigned char *out,
  506. const AES_KEY *key);
  507. void aes_t4_decrypt(const unsigned char *in, unsigned char *out,
  508. const AES_KEY *key);
  509. /*
  510. * Key-length specific subroutines were chosen for following reason.
  511. * Each SPARC T4 core can execute up to 8 threads which share core's
  512. * resources. Loading as much key material to registers allows to
  513. * minimize references to shared memory interface, as well as amount
  514. * of instructions in inner loops [much needed on T4]. But then having
  515. * non-key-length specific routines would require conditional branches
  516. * either in inner loops or on subroutines' entries. Former is hardly
  517. * acceptable, while latter means code size increase to size occupied
  518. * by multiple key-length specific subroutines, so why fight?
  519. */
  520. void aes128_t4_cbc_encrypt(const unsigned char *in, unsigned char *out,
  521. size_t len, const AES_KEY *key,
  522. unsigned char *ivec);
  523. void aes128_t4_cbc_decrypt(const unsigned char *in, unsigned char *out,
  524. size_t len, const AES_KEY *key,
  525. unsigned char *ivec);
  526. void aes192_t4_cbc_encrypt(const unsigned char *in, unsigned char *out,
  527. size_t len, const AES_KEY *key,
  528. unsigned char *ivec);
  529. void aes192_t4_cbc_decrypt(const unsigned char *in, unsigned char *out,
  530. size_t len, const AES_KEY *key,
  531. unsigned char *ivec);
  532. void aes256_t4_cbc_encrypt(const unsigned char *in, unsigned char *out,
  533. size_t len, const AES_KEY *key,
  534. unsigned char *ivec);
  535. void aes256_t4_cbc_decrypt(const unsigned char *in, unsigned char *out,
  536. size_t len, const AES_KEY *key,
  537. unsigned char *ivec);
  538. void aes128_t4_ctr32_encrypt(const unsigned char *in, unsigned char *out,
  539. size_t blocks, const AES_KEY *key,
  540. unsigned char *ivec);
  541. void aes192_t4_ctr32_encrypt(const unsigned char *in, unsigned char *out,
  542. size_t blocks, const AES_KEY *key,
  543. unsigned char *ivec);
  544. void aes256_t4_ctr32_encrypt(const unsigned char *in, unsigned char *out,
  545. size_t blocks, const AES_KEY *key,
  546. unsigned char *ivec);
  547. void aes128_t4_xts_encrypt(const unsigned char *in, unsigned char *out,
  548. size_t blocks, const AES_KEY *key1,
  549. const AES_KEY *key2, const unsigned char *ivec);
  550. void aes128_t4_xts_decrypt(const unsigned char *in, unsigned char *out,
  551. size_t blocks, const AES_KEY *key1,
  552. const AES_KEY *key2, const unsigned char *ivec);
  553. void aes256_t4_xts_encrypt(const unsigned char *in, unsigned char *out,
  554. size_t blocks, const AES_KEY *key1,
  555. const AES_KEY *key2, const unsigned char *ivec);
  556. void aes256_t4_xts_decrypt(const unsigned char *in, unsigned char *out,
  557. size_t blocks, const AES_KEY *key1,
  558. const AES_KEY *key2, const unsigned char *ivec);
  559. static int aes_t4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  560. const unsigned char *iv, int enc)
  561. {
  562. int ret, mode, bits;
  563. EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
  564. mode = EVP_CIPHER_CTX_mode(ctx);
  565. bits = EVP_CIPHER_CTX_key_length(ctx) * 8;
  566. if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
  567. && !enc) {
  568. ret = 0;
  569. aes_t4_set_decrypt_key(key, bits, &dat->ks.ks);
  570. dat->block = (block128_f) aes_t4_decrypt;
  571. switch (bits) {
  572. case 128:
  573. dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
  574. (cbc128_f) aes128_t4_cbc_decrypt : NULL;
  575. break;
  576. case 192:
  577. dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
  578. (cbc128_f) aes192_t4_cbc_decrypt : NULL;
  579. break;
  580. case 256:
  581. dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
  582. (cbc128_f) aes256_t4_cbc_decrypt : NULL;
  583. break;
  584. default:
  585. ret = -1;
  586. }
  587. } else {
  588. ret = 0;
  589. aes_t4_set_encrypt_key(key, bits, &dat->ks.ks);
  590. dat->block = (block128_f) aes_t4_encrypt;
  591. switch (bits) {
  592. case 128:
  593. if (mode == EVP_CIPH_CBC_MODE)
  594. dat->stream.cbc = (cbc128_f) aes128_t4_cbc_encrypt;
  595. else if (mode == EVP_CIPH_CTR_MODE)
  596. dat->stream.ctr = (ctr128_f) aes128_t4_ctr32_encrypt;
  597. else
  598. dat->stream.cbc = NULL;
  599. break;
  600. case 192:
  601. if (mode == EVP_CIPH_CBC_MODE)
  602. dat->stream.cbc = (cbc128_f) aes192_t4_cbc_encrypt;
  603. else if (mode == EVP_CIPH_CTR_MODE)
  604. dat->stream.ctr = (ctr128_f) aes192_t4_ctr32_encrypt;
  605. else
  606. dat->stream.cbc = NULL;
  607. break;
  608. case 256:
  609. if (mode == EVP_CIPH_CBC_MODE)
  610. dat->stream.cbc = (cbc128_f) aes256_t4_cbc_encrypt;
  611. else if (mode == EVP_CIPH_CTR_MODE)
  612. dat->stream.ctr = (ctr128_f) aes256_t4_ctr32_encrypt;
  613. else
  614. dat->stream.cbc = NULL;
  615. break;
  616. default:
  617. ret = -1;
  618. }
  619. }
  620. if (ret < 0) {
  621. EVPerr(EVP_F_AES_T4_INIT_KEY, EVP_R_AES_KEY_SETUP_FAILED);
  622. return 0;
  623. }
  624. return 1;
  625. }
  626. # define aes_t4_cbc_cipher aes_cbc_cipher
  627. static int aes_t4_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  628. const unsigned char *in, size_t len);
  629. # define aes_t4_ecb_cipher aes_ecb_cipher
  630. static int aes_t4_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  631. const unsigned char *in, size_t len);
  632. # define aes_t4_ofb_cipher aes_ofb_cipher
  633. static int aes_t4_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  634. const unsigned char *in, size_t len);
  635. # define aes_t4_cfb_cipher aes_cfb_cipher
  636. static int aes_t4_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  637. const unsigned char *in, size_t len);
  638. # define aes_t4_cfb8_cipher aes_cfb8_cipher
  639. static int aes_t4_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  640. const unsigned char *in, size_t len);
  641. # define aes_t4_cfb1_cipher aes_cfb1_cipher
  642. static int aes_t4_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  643. const unsigned char *in, size_t len);
  644. # define aes_t4_ctr_cipher aes_ctr_cipher
  645. static int aes_t4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  646. const unsigned char *in, size_t len);
  647. static int aes_t4_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  648. const unsigned char *iv, int enc)
  649. {
  650. EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,ctx);
  651. if (!iv && !key)
  652. return 1;
  653. if (key) {
  654. int bits = EVP_CIPHER_CTX_key_length(ctx) * 8;
  655. aes_t4_set_encrypt_key(key, bits, &gctx->ks.ks);
  656. CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
  657. (block128_f) aes_t4_encrypt);
  658. switch (bits) {
  659. case 128:
  660. gctx->ctr = (ctr128_f) aes128_t4_ctr32_encrypt;
  661. break;
  662. case 192:
  663. gctx->ctr = (ctr128_f) aes192_t4_ctr32_encrypt;
  664. break;
  665. case 256:
  666. gctx->ctr = (ctr128_f) aes256_t4_ctr32_encrypt;
  667. break;
  668. default:
  669. return 0;
  670. }
  671. /*
  672. * If we have an iv can set it directly, otherwise use saved IV.
  673. */
  674. if (iv == NULL && gctx->iv_set)
  675. iv = gctx->iv;
  676. if (iv) {
  677. CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
  678. gctx->iv_set = 1;
  679. }
  680. gctx->key_set = 1;
  681. } else {
  682. /* If key set use IV, otherwise copy */
  683. if (gctx->key_set)
  684. CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
  685. else
  686. memcpy(gctx->iv, iv, gctx->ivlen);
  687. gctx->iv_set = 1;
  688. gctx->iv_gen = 0;
  689. }
  690. return 1;
  691. }
  692. # define aes_t4_gcm_cipher aes_gcm_cipher
  693. static int aes_t4_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  694. const unsigned char *in, size_t len);
  695. static int aes_t4_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  696. const unsigned char *iv, int enc)
  697. {
  698. EVP_AES_XTS_CTX *xctx = EVP_C_DATA(EVP_AES_XTS_CTX,ctx);
  699. if (!iv && !key)
  700. return 1;
  701. if (key) {
  702. int bits = EVP_CIPHER_CTX_key_length(ctx) * 4;
  703. xctx->stream = NULL;
  704. /* key_len is two AES keys */
  705. if (enc) {
  706. aes_t4_set_encrypt_key(key, bits, &xctx->ks1.ks);
  707. xctx->xts.block1 = (block128_f) aes_t4_encrypt;
  708. switch (bits) {
  709. case 128:
  710. xctx->stream = aes128_t4_xts_encrypt;
  711. break;
  712. case 256:
  713. xctx->stream = aes256_t4_xts_encrypt;
  714. break;
  715. default:
  716. return 0;
  717. }
  718. } else {
  719. aes_t4_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 4,
  720. &xctx->ks1.ks);
  721. xctx->xts.block1 = (block128_f) aes_t4_decrypt;
  722. switch (bits) {
  723. case 128:
  724. xctx->stream = aes128_t4_xts_decrypt;
  725. break;
  726. case 256:
  727. xctx->stream = aes256_t4_xts_decrypt;
  728. break;
  729. default:
  730. return 0;
  731. }
  732. }
  733. aes_t4_set_encrypt_key(key + EVP_CIPHER_CTX_key_length(ctx) / 2,
  734. EVP_CIPHER_CTX_key_length(ctx) * 4,
  735. &xctx->ks2.ks);
  736. xctx->xts.block2 = (block128_f) aes_t4_encrypt;
  737. xctx->xts.key1 = &xctx->ks1;
  738. }
  739. if (iv) {
  740. xctx->xts.key2 = &xctx->ks2;
  741. memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 16);
  742. }
  743. return 1;
  744. }
  745. # define aes_t4_xts_cipher aes_xts_cipher
  746. static int aes_t4_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  747. const unsigned char *in, size_t len);
  748. static int aes_t4_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  749. const unsigned char *iv, int enc)
  750. {
  751. EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,ctx);
  752. if (!iv && !key)
  753. return 1;
  754. if (key) {
  755. int bits = EVP_CIPHER_CTX_key_length(ctx) * 8;
  756. aes_t4_set_encrypt_key(key, bits, &cctx->ks.ks);
  757. CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
  758. &cctx->ks, (block128_f) aes_t4_encrypt);
  759. cctx->str = NULL;
  760. cctx->key_set = 1;
  761. }
  762. if (iv) {
  763. memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 15 - cctx->L);
  764. cctx->iv_set = 1;
  765. }
  766. return 1;
  767. }
  768. # define aes_t4_ccm_cipher aes_ccm_cipher
  769. static int aes_t4_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  770. const unsigned char *in, size_t len);
  771. # ifndef OPENSSL_NO_OCB
  772. static int aes_t4_ocb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  773. const unsigned char *iv, int enc)
  774. {
  775. EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,ctx);
  776. if (!iv && !key)
  777. return 1;
  778. if (key) {
  779. do {
  780. /*
  781. * We set both the encrypt and decrypt key here because decrypt
  782. * needs both. We could possibly optimise to remove setting the
  783. * decrypt for an encryption operation.
  784. */
  785. aes_t4_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  786. &octx->ksenc.ks);
  787. aes_t4_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  788. &octx->ksdec.ks);
  789. if (!CRYPTO_ocb128_init(&octx->ocb,
  790. &octx->ksenc.ks, &octx->ksdec.ks,
  791. (block128_f) aes_t4_encrypt,
  792. (block128_f) aes_t4_decrypt,
  793. NULL))
  794. return 0;
  795. }
  796. while (0);
  797. /*
  798. * If we have an iv we can set it directly, otherwise use saved IV.
  799. */
  800. if (iv == NULL && octx->iv_set)
  801. iv = octx->iv;
  802. if (iv) {
  803. if (CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen)
  804. != 1)
  805. return 0;
  806. octx->iv_set = 1;
  807. }
  808. octx->key_set = 1;
  809. } else {
  810. /* If key set use IV, otherwise copy */
  811. if (octx->key_set)
  812. CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen);
  813. else
  814. memcpy(octx->iv, iv, octx->ivlen);
  815. octx->iv_set = 1;
  816. }
  817. return 1;
  818. }
  819. # define aes_t4_ocb_cipher aes_ocb_cipher
  820. static int aes_t4_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  821. const unsigned char *in, size_t len);
  822. # endif /* OPENSSL_NO_OCB */
  823. # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
  824. static const EVP_CIPHER aes_t4_##keylen##_##mode = { \
  825. nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
  826. flags|EVP_CIPH_##MODE##_MODE, \
  827. aes_t4_init_key, \
  828. aes_t4_##mode##_cipher, \
  829. NULL, \
  830. sizeof(EVP_AES_KEY), \
  831. NULL,NULL,NULL,NULL }; \
  832. static const EVP_CIPHER aes_##keylen##_##mode = { \
  833. nid##_##keylen##_##nmode,blocksize, \
  834. keylen/8,ivlen, \
  835. flags|EVP_CIPH_##MODE##_MODE, \
  836. aes_init_key, \
  837. aes_##mode##_cipher, \
  838. NULL, \
  839. sizeof(EVP_AES_KEY), \
  840. NULL,NULL,NULL,NULL }; \
  841. const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
  842. { return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; }
  843. # define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
  844. static const EVP_CIPHER aes_t4_##keylen##_##mode = { \
  845. nid##_##keylen##_##mode,blocksize, \
  846. (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
  847. flags|EVP_CIPH_##MODE##_MODE, \
  848. aes_t4_##mode##_init_key, \
  849. aes_t4_##mode##_cipher, \
  850. aes_##mode##_cleanup, \
  851. sizeof(EVP_AES_##MODE##_CTX), \
  852. NULL,NULL,aes_##mode##_ctrl,NULL }; \
  853. static const EVP_CIPHER aes_##keylen##_##mode = { \
  854. nid##_##keylen##_##mode,blocksize, \
  855. (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
  856. flags|EVP_CIPH_##MODE##_MODE, \
  857. aes_##mode##_init_key, \
  858. aes_##mode##_cipher, \
  859. aes_##mode##_cleanup, \
  860. sizeof(EVP_AES_##MODE##_CTX), \
  861. NULL,NULL,aes_##mode##_ctrl,NULL }; \
  862. const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
  863. { return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; }
  864. #elif defined(OPENSSL_CPUID_OBJ) && defined(__s390__)
  865. /*
  866. * IBM S390X support
  867. */
  868. # include "s390x_arch.h"
  869. typedef struct {
  870. union {
  871. double align;
  872. /*-
  873. * KM-AES parameter block - begin
  874. * (see z/Architecture Principles of Operation >= SA22-7832-06)
  875. */
  876. struct {
  877. unsigned char k[32];
  878. } param;
  879. /* KM-AES parameter block - end */
  880. } km;
  881. unsigned int fc;
  882. } S390X_AES_ECB_CTX;
  883. typedef struct {
  884. union {
  885. double align;
  886. /*-
  887. * KMO-AES parameter block - begin
  888. * (see z/Architecture Principles of Operation >= SA22-7832-08)
  889. */
  890. struct {
  891. unsigned char cv[16];
  892. unsigned char k[32];
  893. } param;
  894. /* KMO-AES parameter block - end */
  895. } kmo;
  896. unsigned int fc;
  897. int res;
  898. } S390X_AES_OFB_CTX;
  899. typedef struct {
  900. union {
  901. double align;
  902. /*-
  903. * KMF-AES parameter block - begin
  904. * (see z/Architecture Principles of Operation >= SA22-7832-08)
  905. */
  906. struct {
  907. unsigned char cv[16];
  908. unsigned char k[32];
  909. } param;
  910. /* KMF-AES parameter block - end */
  911. } kmf;
  912. unsigned int fc;
  913. int res;
  914. } S390X_AES_CFB_CTX;
  915. typedef struct {
  916. union {
  917. double align;
  918. /*-
  919. * KMA-GCM-AES parameter block - begin
  920. * (see z/Architecture Principles of Operation >= SA22-7832-11)
  921. */
  922. struct {
  923. unsigned char reserved[12];
  924. union {
  925. unsigned int w;
  926. unsigned char b[4];
  927. } cv;
  928. union {
  929. unsigned long long g[2];
  930. unsigned char b[16];
  931. } t;
  932. unsigned char h[16];
  933. unsigned long long taadl;
  934. unsigned long long tpcl;
  935. union {
  936. unsigned long long g[2];
  937. unsigned int w[4];
  938. } j0;
  939. unsigned char k[32];
  940. } param;
  941. /* KMA-GCM-AES parameter block - end */
  942. } kma;
  943. unsigned int fc;
  944. int key_set;
  945. unsigned char *iv;
  946. int ivlen;
  947. int iv_set;
  948. int iv_gen;
  949. int taglen;
  950. unsigned char ares[16];
  951. unsigned char mres[16];
  952. unsigned char kres[16];
  953. int areslen;
  954. int mreslen;
  955. int kreslen;
  956. int tls_aad_len;
  957. } S390X_AES_GCM_CTX;
  958. typedef struct {
  959. union {
  960. double align;
  961. /*-
  962. * Padding is chosen so that ccm.kmac_param.k overlaps with key.k and
  963. * ccm.fc with key.k.rounds. Remember that on s390x, an AES_KEY's
  964. * rounds field is used to store the function code and that the key
  965. * schedule is not stored (if aes hardware support is detected).
  966. */
  967. struct {
  968. unsigned char pad[16];
  969. AES_KEY k;
  970. } key;
  971. struct {
  972. /*-
  973. * KMAC-AES parameter block - begin
  974. * (see z/Architecture Principles of Operation >= SA22-7832-08)
  975. */
  976. struct {
  977. union {
  978. unsigned long long g[2];
  979. unsigned char b[16];
  980. } icv;
  981. unsigned char k[32];
  982. } kmac_param;
  983. /* KMAC-AES paramater block - end */
  984. union {
  985. unsigned long long g[2];
  986. unsigned char b[16];
  987. } nonce;
  988. union {
  989. unsigned long long g[2];
  990. unsigned char b[16];
  991. } buf;
  992. unsigned long long blocks;
  993. int l;
  994. int m;
  995. int tls_aad_len;
  996. int iv_set;
  997. int tag_set;
  998. int len_set;
  999. int key_set;
  1000. unsigned char pad[140];
  1001. unsigned int fc;
  1002. } ccm;
  1003. } aes;
  1004. } S390X_AES_CCM_CTX;
  1005. /* Convert key size to function code: [16,24,32] -> [18,19,20]. */
  1006. # define S390X_AES_FC(keylen) (S390X_AES_128 + ((((keylen) << 3) - 128) >> 6))
  1007. /* Most modes of operation need km for partial block processing. */
  1008. # define S390X_aes_128_CAPABLE (OPENSSL_s390xcap_P.km[0] & \
  1009. S390X_CAPBIT(S390X_AES_128))
  1010. # define S390X_aes_192_CAPABLE (OPENSSL_s390xcap_P.km[0] & \
  1011. S390X_CAPBIT(S390X_AES_192))
  1012. # define S390X_aes_256_CAPABLE (OPENSSL_s390xcap_P.km[0] & \
  1013. S390X_CAPBIT(S390X_AES_256))
  1014. # define s390x_aes_init_key aes_init_key
  1015. static int s390x_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  1016. const unsigned char *iv, int enc);
  1017. # define S390X_aes_128_cbc_CAPABLE 1 /* checked by callee */
  1018. # define S390X_aes_192_cbc_CAPABLE 1
  1019. # define S390X_aes_256_cbc_CAPABLE 1
  1020. # define S390X_AES_CBC_CTX EVP_AES_KEY
  1021. # define s390x_aes_cbc_init_key aes_init_key
  1022. # define s390x_aes_cbc_cipher aes_cbc_cipher
  1023. static int s390x_aes_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1024. const unsigned char *in, size_t len);
  1025. # define S390X_aes_128_ecb_CAPABLE S390X_aes_128_CAPABLE
  1026. # define S390X_aes_192_ecb_CAPABLE S390X_aes_192_CAPABLE
  1027. # define S390X_aes_256_ecb_CAPABLE S390X_aes_256_CAPABLE
  1028. static int s390x_aes_ecb_init_key(EVP_CIPHER_CTX *ctx,
  1029. const unsigned char *key,
  1030. const unsigned char *iv, int enc)
  1031. {
  1032. S390X_AES_ECB_CTX *cctx = EVP_C_DATA(S390X_AES_ECB_CTX, ctx);
  1033. const int keylen = EVP_CIPHER_CTX_key_length(ctx);
  1034. cctx->fc = S390X_AES_FC(keylen);
  1035. if (!enc)
  1036. cctx->fc |= S390X_DECRYPT;
  1037. memcpy(cctx->km.param.k, key, keylen);
  1038. return 1;
  1039. }
  1040. static int s390x_aes_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1041. const unsigned char *in, size_t len)
  1042. {
  1043. S390X_AES_ECB_CTX *cctx = EVP_C_DATA(S390X_AES_ECB_CTX, ctx);
  1044. s390x_km(in, len, out, cctx->fc, &cctx->km.param);
  1045. return 1;
  1046. }
  1047. # define S390X_aes_128_ofb_CAPABLE (S390X_aes_128_CAPABLE && \
  1048. (OPENSSL_s390xcap_P.kmo[0] & \
  1049. S390X_CAPBIT(S390X_AES_128)))
  1050. # define S390X_aes_192_ofb_CAPABLE (S390X_aes_192_CAPABLE && \
  1051. (OPENSSL_s390xcap_P.kmo[0] & \
  1052. S390X_CAPBIT(S390X_AES_192)))
  1053. # define S390X_aes_256_ofb_CAPABLE (S390X_aes_256_CAPABLE && \
  1054. (OPENSSL_s390xcap_P.kmo[0] & \
  1055. S390X_CAPBIT(S390X_AES_256)))
  1056. static int s390x_aes_ofb_init_key(EVP_CIPHER_CTX *ctx,
  1057. const unsigned char *key,
  1058. const unsigned char *ivec, int enc)
  1059. {
  1060. S390X_AES_OFB_CTX *cctx = EVP_C_DATA(S390X_AES_OFB_CTX, ctx);
  1061. const unsigned char *iv = EVP_CIPHER_CTX_original_iv(ctx);
  1062. const int keylen = EVP_CIPHER_CTX_key_length(ctx);
  1063. const int ivlen = EVP_CIPHER_CTX_iv_length(ctx);
  1064. memcpy(cctx->kmo.param.cv, iv, ivlen);
  1065. memcpy(cctx->kmo.param.k, key, keylen);
  1066. cctx->fc = S390X_AES_FC(keylen);
  1067. cctx->res = 0;
  1068. return 1;
  1069. }
  1070. static int s390x_aes_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1071. const unsigned char *in, size_t len)
  1072. {
  1073. S390X_AES_OFB_CTX *cctx = EVP_C_DATA(S390X_AES_OFB_CTX, ctx);
  1074. int n = cctx->res;
  1075. int rem;
  1076. while (n && len) {
  1077. *out = *in ^ cctx->kmo.param.cv[n];
  1078. n = (n + 1) & 0xf;
  1079. --len;
  1080. ++in;
  1081. ++out;
  1082. }
  1083. rem = len & 0xf;
  1084. len &= ~(size_t)0xf;
  1085. if (len) {
  1086. s390x_kmo(in, len, out, cctx->fc, &cctx->kmo.param);
  1087. out += len;
  1088. in += len;
  1089. }
  1090. if (rem) {
  1091. s390x_km(cctx->kmo.param.cv, 16, cctx->kmo.param.cv, cctx->fc,
  1092. cctx->kmo.param.k);
  1093. while (rem--) {
  1094. out[n] = in[n] ^ cctx->kmo.param.cv[n];
  1095. ++n;
  1096. }
  1097. }
  1098. cctx->res = n;
  1099. return 1;
  1100. }
  1101. # define S390X_aes_128_cfb_CAPABLE (S390X_aes_128_CAPABLE && \
  1102. (OPENSSL_s390xcap_P.kmf[0] & \
  1103. S390X_CAPBIT(S390X_AES_128)))
  1104. # define S390X_aes_192_cfb_CAPABLE (S390X_aes_192_CAPABLE && \
  1105. (OPENSSL_s390xcap_P.kmf[0] & \
  1106. S390X_CAPBIT(S390X_AES_192)))
  1107. # define S390X_aes_256_cfb_CAPABLE (S390X_aes_256_CAPABLE && \
  1108. (OPENSSL_s390xcap_P.kmf[0] & \
  1109. S390X_CAPBIT(S390X_AES_256)))
  1110. static int s390x_aes_cfb_init_key(EVP_CIPHER_CTX *ctx,
  1111. const unsigned char *key,
  1112. const unsigned char *ivec, int enc)
  1113. {
  1114. S390X_AES_CFB_CTX *cctx = EVP_C_DATA(S390X_AES_CFB_CTX, ctx);
  1115. const unsigned char *iv = EVP_CIPHER_CTX_original_iv(ctx);
  1116. const int keylen = EVP_CIPHER_CTX_key_length(ctx);
  1117. const int ivlen = EVP_CIPHER_CTX_iv_length(ctx);
  1118. cctx->fc = S390X_AES_FC(keylen);
  1119. cctx->fc |= 16 << 24; /* 16 bytes cipher feedback */
  1120. if (!enc)
  1121. cctx->fc |= S390X_DECRYPT;
  1122. cctx->res = 0;
  1123. memcpy(cctx->kmf.param.cv, iv, ivlen);
  1124. memcpy(cctx->kmf.param.k, key, keylen);
  1125. return 1;
  1126. }
  1127. static int s390x_aes_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1128. const unsigned char *in, size_t len)
  1129. {
  1130. S390X_AES_CFB_CTX *cctx = EVP_C_DATA(S390X_AES_CFB_CTX, ctx);
  1131. const int keylen = EVP_CIPHER_CTX_key_length(ctx);
  1132. const int enc = EVP_CIPHER_CTX_encrypting(ctx);
  1133. int n = cctx->res;
  1134. int rem;
  1135. unsigned char tmp;
  1136. while (n && len) {
  1137. tmp = *in;
  1138. *out = cctx->kmf.param.cv[n] ^ tmp;
  1139. cctx->kmf.param.cv[n] = enc ? *out : tmp;
  1140. n = (n + 1) & 0xf;
  1141. --len;
  1142. ++in;
  1143. ++out;
  1144. }
  1145. rem = len & 0xf;
  1146. len &= ~(size_t)0xf;
  1147. if (len) {
  1148. s390x_kmf(in, len, out, cctx->fc, &cctx->kmf.param);
  1149. out += len;
  1150. in += len;
  1151. }
  1152. if (rem) {
  1153. s390x_km(cctx->kmf.param.cv, 16, cctx->kmf.param.cv,
  1154. S390X_AES_FC(keylen), cctx->kmf.param.k);
  1155. while (rem--) {
  1156. tmp = in[n];
  1157. out[n] = cctx->kmf.param.cv[n] ^ tmp;
  1158. cctx->kmf.param.cv[n] = enc ? out[n] : tmp;
  1159. ++n;
  1160. }
  1161. }
  1162. cctx->res = n;
  1163. return 1;
  1164. }
  1165. # define S390X_aes_128_cfb8_CAPABLE (OPENSSL_s390xcap_P.kmf[0] & \
  1166. S390X_CAPBIT(S390X_AES_128))
  1167. # define S390X_aes_192_cfb8_CAPABLE (OPENSSL_s390xcap_P.kmf[0] & \
  1168. S390X_CAPBIT(S390X_AES_192))
  1169. # define S390X_aes_256_cfb8_CAPABLE (OPENSSL_s390xcap_P.kmf[0] & \
  1170. S390X_CAPBIT(S390X_AES_256))
  1171. static int s390x_aes_cfb8_init_key(EVP_CIPHER_CTX *ctx,
  1172. const unsigned char *key,
  1173. const unsigned char *ivec, int enc)
  1174. {
  1175. S390X_AES_CFB_CTX *cctx = EVP_C_DATA(S390X_AES_CFB_CTX, ctx);
  1176. const unsigned char *iv = EVP_CIPHER_CTX_original_iv(ctx);
  1177. const int keylen = EVP_CIPHER_CTX_key_length(ctx);
  1178. const int ivlen = EVP_CIPHER_CTX_iv_length(ctx);
  1179. cctx->fc = S390X_AES_FC(keylen);
  1180. cctx->fc |= 1 << 24; /* 1 byte cipher feedback */
  1181. if (!enc)
  1182. cctx->fc |= S390X_DECRYPT;
  1183. memcpy(cctx->kmf.param.cv, iv, ivlen);
  1184. memcpy(cctx->kmf.param.k, key, keylen);
  1185. return 1;
  1186. }
  1187. static int s390x_aes_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1188. const unsigned char *in, size_t len)
  1189. {
  1190. S390X_AES_CFB_CTX *cctx = EVP_C_DATA(S390X_AES_CFB_CTX, ctx);
  1191. s390x_kmf(in, len, out, cctx->fc, &cctx->kmf.param);
  1192. return 1;
  1193. }
  1194. # define S390X_aes_128_cfb1_CAPABLE 0
  1195. # define S390X_aes_192_cfb1_CAPABLE 0
  1196. # define S390X_aes_256_cfb1_CAPABLE 0
  1197. # define s390x_aes_cfb1_init_key aes_init_key
  1198. # define s390x_aes_cfb1_cipher aes_cfb1_cipher
  1199. static int s390x_aes_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1200. const unsigned char *in, size_t len);
  1201. # define S390X_aes_128_ctr_CAPABLE 1 /* checked by callee */
  1202. # define S390X_aes_192_ctr_CAPABLE 1
  1203. # define S390X_aes_256_ctr_CAPABLE 1
  1204. # define S390X_AES_CTR_CTX EVP_AES_KEY
  1205. # define s390x_aes_ctr_init_key aes_init_key
  1206. # define s390x_aes_ctr_cipher aes_ctr_cipher
  1207. static int s390x_aes_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1208. const unsigned char *in, size_t len);
  1209. # define S390X_aes_128_gcm_CAPABLE (S390X_aes_128_CAPABLE && \
  1210. (OPENSSL_s390xcap_P.kma[0] & \
  1211. S390X_CAPBIT(S390X_AES_128)))
  1212. # define S390X_aes_192_gcm_CAPABLE (S390X_aes_192_CAPABLE && \
  1213. (OPENSSL_s390xcap_P.kma[0] & \
  1214. S390X_CAPBIT(S390X_AES_192)))
  1215. # define S390X_aes_256_gcm_CAPABLE (S390X_aes_256_CAPABLE && \
  1216. (OPENSSL_s390xcap_P.kma[0] & \
  1217. S390X_CAPBIT(S390X_AES_256)))
  1218. /* iv + padding length for iv lenghts != 12 */
  1219. # define S390X_gcm_ivpadlen(i) ((((i) + 15) >> 4 << 4) + 16)
  1220. /*-
  1221. * Process additional authenticated data. Returns 0 on success. Code is
  1222. * big-endian.
  1223. */
  1224. static int s390x_aes_gcm_aad(S390X_AES_GCM_CTX *ctx, const unsigned char *aad,
  1225. size_t len)
  1226. {
  1227. unsigned long long alen;
  1228. int n, rem;
  1229. if (ctx->kma.param.tpcl)
  1230. return -2;
  1231. alen = ctx->kma.param.taadl + len;
  1232. if (alen > (U64(1) << 61) || (sizeof(len) == 8 && alen < len))
  1233. return -1;
  1234. ctx->kma.param.taadl = alen;
  1235. n = ctx->areslen;
  1236. if (n) {
  1237. while (n && len) {
  1238. ctx->ares[n] = *aad;
  1239. n = (n + 1) & 0xf;
  1240. ++aad;
  1241. --len;
  1242. }
  1243. /* ctx->ares contains a complete block if offset has wrapped around */
  1244. if (!n) {
  1245. s390x_kma(ctx->ares, 16, NULL, 0, NULL, ctx->fc, &ctx->kma.param);
  1246. ctx->fc |= S390X_KMA_HS;
  1247. }
  1248. ctx->areslen = n;
  1249. }
  1250. rem = len & 0xf;
  1251. len &= ~(size_t)0xf;
  1252. if (len) {
  1253. s390x_kma(aad, len, NULL, 0, NULL, ctx->fc, &ctx->kma.param);
  1254. aad += len;
  1255. ctx->fc |= S390X_KMA_HS;
  1256. }
  1257. if (rem) {
  1258. ctx->areslen = rem;
  1259. do {
  1260. --rem;
  1261. ctx->ares[rem] = aad[rem];
  1262. } while (rem);
  1263. }
  1264. return 0;
  1265. }
  1266. /*-
  1267. * En/de-crypt plain/cipher-text and authenticate ciphertext. Returns 0 for
  1268. * success. Code is big-endian.
  1269. */
  1270. static int s390x_aes_gcm(S390X_AES_GCM_CTX *ctx, const unsigned char *in,
  1271. unsigned char *out, size_t len)
  1272. {
  1273. const unsigned char *inptr;
  1274. unsigned long long mlen;
  1275. union {
  1276. unsigned int w[4];
  1277. unsigned char b[16];
  1278. } buf;
  1279. size_t inlen;
  1280. int n, rem, i;
  1281. mlen = ctx->kma.param.tpcl + len;
  1282. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  1283. return -1;
  1284. ctx->kma.param.tpcl = mlen;
  1285. n = ctx->mreslen;
  1286. if (n) {
  1287. inptr = in;
  1288. inlen = len;
  1289. while (n && inlen) {
  1290. ctx->mres[n] = *inptr;
  1291. n = (n + 1) & 0xf;
  1292. ++inptr;
  1293. --inlen;
  1294. }
  1295. /* ctx->mres contains a complete block if offset has wrapped around */
  1296. if (!n) {
  1297. s390x_kma(ctx->ares, ctx->areslen, ctx->mres, 16, buf.b,
  1298. ctx->fc | S390X_KMA_LAAD, &ctx->kma.param);
  1299. ctx->fc |= S390X_KMA_HS;
  1300. ctx->areslen = 0;
  1301. /* previous call already encrypted/decrypted its remainder,
  1302. * see comment below */
  1303. n = ctx->mreslen;
  1304. while (n) {
  1305. *out = buf.b[n];
  1306. n = (n + 1) & 0xf;
  1307. ++out;
  1308. ++in;
  1309. --len;
  1310. }
  1311. ctx->mreslen = 0;
  1312. }
  1313. }
  1314. rem = len & 0xf;
  1315. len &= ~(size_t)0xf;
  1316. if (len) {
  1317. s390x_kma(ctx->ares, ctx->areslen, in, len, out,
  1318. ctx->fc | S390X_KMA_LAAD, &ctx->kma.param);
  1319. in += len;
  1320. out += len;
  1321. ctx->fc |= S390X_KMA_HS;
  1322. ctx->areslen = 0;
  1323. }
  1324. /*-
  1325. * If there is a remainder, it has to be saved such that it can be
  1326. * processed by kma later. However, we also have to do the for-now
  1327. * unauthenticated encryption/decryption part here and now...
  1328. */
  1329. if (rem) {
  1330. if (!ctx->mreslen) {
  1331. buf.w[0] = ctx->kma.param.j0.w[0];
  1332. buf.w[1] = ctx->kma.param.j0.w[1];
  1333. buf.w[2] = ctx->kma.param.j0.w[2];
  1334. buf.w[3] = ctx->kma.param.cv.w + 1;
  1335. s390x_km(buf.b, 16, ctx->kres, ctx->fc & 0x1f, &ctx->kma.param.k);
  1336. }
  1337. n = ctx->mreslen;
  1338. for (i = 0; i < rem; i++) {
  1339. ctx->mres[n + i] = in[i];
  1340. out[i] = in[i] ^ ctx->kres[n + i];
  1341. }
  1342. ctx->mreslen += rem;
  1343. }
  1344. return 0;
  1345. }
  1346. /*-
  1347. * Initialize context structure. Code is big-endian.
  1348. */
  1349. static void s390x_aes_gcm_setiv(S390X_AES_GCM_CTX *ctx,
  1350. const unsigned char *iv)
  1351. {
  1352. ctx->kma.param.t.g[0] = 0;
  1353. ctx->kma.param.t.g[1] = 0;
  1354. ctx->kma.param.tpcl = 0;
  1355. ctx->kma.param.taadl = 0;
  1356. ctx->mreslen = 0;
  1357. ctx->areslen = 0;
  1358. ctx->kreslen = 0;
  1359. if (ctx->ivlen == 12) {
  1360. memcpy(&ctx->kma.param.j0, iv, ctx->ivlen);
  1361. ctx->kma.param.j0.w[3] = 1;
  1362. ctx->kma.param.cv.w = 1;
  1363. } else {
  1364. /* ctx->iv has the right size and is already padded. */
  1365. memcpy(ctx->iv, iv, ctx->ivlen);
  1366. s390x_kma(ctx->iv, S390X_gcm_ivpadlen(ctx->ivlen), NULL, 0, NULL,
  1367. ctx->fc, &ctx->kma.param);
  1368. ctx->fc |= S390X_KMA_HS;
  1369. ctx->kma.param.j0.g[0] = ctx->kma.param.t.g[0];
  1370. ctx->kma.param.j0.g[1] = ctx->kma.param.t.g[1];
  1371. ctx->kma.param.cv.w = ctx->kma.param.j0.w[3];
  1372. ctx->kma.param.t.g[0] = 0;
  1373. ctx->kma.param.t.g[1] = 0;
  1374. }
  1375. }
  1376. /*-
  1377. * Performs various operations on the context structure depending on control
  1378. * type. Returns 1 for success, 0 for failure and -1 for unknown control type.
  1379. * Code is big-endian.
  1380. */
  1381. static int s390x_aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
  1382. {
  1383. S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, c);
  1384. S390X_AES_GCM_CTX *gctx_out;
  1385. EVP_CIPHER_CTX *out;
  1386. unsigned char *buf, *iv;
  1387. int ivlen, enc, len;
  1388. switch (type) {
  1389. case EVP_CTRL_INIT:
  1390. ivlen = EVP_CIPHER_CTX_iv_length(c);
  1391. iv = EVP_CIPHER_CTX_iv_noconst(c);
  1392. gctx->key_set = 0;
  1393. gctx->iv_set = 0;
  1394. gctx->ivlen = ivlen;
  1395. gctx->iv = iv;
  1396. gctx->taglen = -1;
  1397. gctx->iv_gen = 0;
  1398. gctx->tls_aad_len = -1;
  1399. return 1;
  1400. case EVP_CTRL_AEAD_SET_IVLEN:
  1401. if (arg <= 0)
  1402. return 0;
  1403. if (arg != 12) {
  1404. iv = EVP_CIPHER_CTX_iv_noconst(c);
  1405. len = S390X_gcm_ivpadlen(arg);
  1406. /* Allocate memory for iv if needed. */
  1407. if (gctx->ivlen == 12 || len > S390X_gcm_ivpadlen(gctx->ivlen)) {
  1408. if (gctx->iv != iv)
  1409. OPENSSL_free(gctx->iv);
  1410. if ((gctx->iv = OPENSSL_malloc(len)) == NULL) {
  1411. EVPerr(EVP_F_S390X_AES_GCM_CTRL, ERR_R_MALLOC_FAILURE);
  1412. return 0;
  1413. }
  1414. }
  1415. /* Add padding. */
  1416. memset(gctx->iv + arg, 0, len - arg - 8);
  1417. *((unsigned long long *)(gctx->iv + len - 8)) = arg << 3;
  1418. }
  1419. gctx->ivlen = arg;
  1420. return 1;
  1421. case EVP_CTRL_AEAD_SET_TAG:
  1422. buf = EVP_CIPHER_CTX_buf_noconst(c);
  1423. enc = EVP_CIPHER_CTX_encrypting(c);
  1424. if (arg <= 0 || arg > 16 || enc)
  1425. return 0;
  1426. memcpy(buf, ptr, arg);
  1427. gctx->taglen = arg;
  1428. return 1;
  1429. case EVP_CTRL_AEAD_GET_TAG:
  1430. enc = EVP_CIPHER_CTX_encrypting(c);
  1431. if (arg <= 0 || arg > 16 || !enc || gctx->taglen < 0)
  1432. return 0;
  1433. memcpy(ptr, gctx->kma.param.t.b, arg);
  1434. return 1;
  1435. case EVP_CTRL_GCM_SET_IV_FIXED:
  1436. /* Special case: -1 length restores whole iv */
  1437. if (arg == -1) {
  1438. memcpy(gctx->iv, ptr, gctx->ivlen);
  1439. gctx->iv_gen = 1;
  1440. return 1;
  1441. }
  1442. /*
  1443. * Fixed field must be at least 4 bytes and invocation field at least
  1444. * 8.
  1445. */
  1446. if ((arg < 4) || (gctx->ivlen - arg) < 8)
  1447. return 0;
  1448. if (arg)
  1449. memcpy(gctx->iv, ptr, arg);
  1450. enc = EVP_CIPHER_CTX_encrypting(c);
  1451. if (enc && RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
  1452. return 0;
  1453. gctx->iv_gen = 1;
  1454. return 1;
  1455. case EVP_CTRL_GCM_IV_GEN:
  1456. if (gctx->iv_gen == 0 || gctx->key_set == 0)
  1457. return 0;
  1458. s390x_aes_gcm_setiv(gctx, gctx->iv);
  1459. if (arg <= 0 || arg > gctx->ivlen)
  1460. arg = gctx->ivlen;
  1461. memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
  1462. /*
  1463. * Invocation field will be at least 8 bytes in size and so no need
  1464. * to check wrap around or increment more than last 8 bytes.
  1465. */
  1466. (*(unsigned long long *)(gctx->iv + gctx->ivlen - 8))++;
  1467. gctx->iv_set = 1;
  1468. return 1;
  1469. case EVP_CTRL_GCM_SET_IV_INV:
  1470. enc = EVP_CIPHER_CTX_encrypting(c);
  1471. if (gctx->iv_gen == 0 || gctx->key_set == 0 || enc)
  1472. return 0;
  1473. memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
  1474. s390x_aes_gcm_setiv(gctx, gctx->iv);
  1475. gctx->iv_set = 1;
  1476. return 1;
  1477. case EVP_CTRL_AEAD_TLS1_AAD:
  1478. /* Save the aad for later use. */
  1479. if (arg != EVP_AEAD_TLS1_AAD_LEN)
  1480. return 0;
  1481. buf = EVP_CIPHER_CTX_buf_noconst(c);
  1482. memcpy(buf, ptr, arg);
  1483. gctx->tls_aad_len = arg;
  1484. len = buf[arg - 2] << 8 | buf[arg - 1];
  1485. /* Correct length for explicit iv. */
  1486. if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
  1487. return 0;
  1488. len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
  1489. /* If decrypting correct for tag too. */
  1490. enc = EVP_CIPHER_CTX_encrypting(c);
  1491. if (!enc) {
  1492. if (len < EVP_GCM_TLS_TAG_LEN)
  1493. return 0;
  1494. len -= EVP_GCM_TLS_TAG_LEN;
  1495. }
  1496. buf[arg - 2] = len >> 8;
  1497. buf[arg - 1] = len & 0xff;
  1498. /* Extra padding: tag appended to record. */
  1499. return EVP_GCM_TLS_TAG_LEN;
  1500. case EVP_CTRL_COPY:
  1501. out = ptr;
  1502. gctx_out = EVP_C_DATA(S390X_AES_GCM_CTX, out);
  1503. iv = EVP_CIPHER_CTX_iv_noconst(c);
  1504. if (gctx->iv == iv) {
  1505. gctx_out->iv = EVP_CIPHER_CTX_iv_noconst(out);
  1506. } else {
  1507. len = S390X_gcm_ivpadlen(gctx->ivlen);
  1508. if ((gctx_out->iv = OPENSSL_malloc(len)) == NULL) {
  1509. EVPerr(EVP_F_S390X_AES_GCM_CTRL, ERR_R_MALLOC_FAILURE);
  1510. return 0;
  1511. }
  1512. memcpy(gctx_out->iv, gctx->iv, len);
  1513. }
  1514. return 1;
  1515. default:
  1516. return -1;
  1517. }
  1518. }
  1519. /*-
  1520. * Set key and/or iv. Returns 1 on success. Otherwise 0 is returned.
  1521. */
  1522. static int s390x_aes_gcm_init_key(EVP_CIPHER_CTX *ctx,
  1523. const unsigned char *key,
  1524. const unsigned char *iv, int enc)
  1525. {
  1526. S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, ctx);
  1527. int keylen;
  1528. if (iv == NULL && key == NULL)
  1529. return 1;
  1530. if (key != NULL) {
  1531. keylen = EVP_CIPHER_CTX_key_length(ctx);
  1532. memcpy(&gctx->kma.param.k, key, keylen);
  1533. gctx->fc = S390X_AES_FC(keylen);
  1534. if (!enc)
  1535. gctx->fc |= S390X_DECRYPT;
  1536. if (iv == NULL && gctx->iv_set)
  1537. iv = gctx->iv;
  1538. if (iv != NULL) {
  1539. s390x_aes_gcm_setiv(gctx, iv);
  1540. gctx->iv_set = 1;
  1541. }
  1542. gctx->key_set = 1;
  1543. } else {
  1544. if (gctx->key_set)
  1545. s390x_aes_gcm_setiv(gctx, iv);
  1546. else
  1547. memcpy(gctx->iv, iv, gctx->ivlen);
  1548. gctx->iv_set = 1;
  1549. gctx->iv_gen = 0;
  1550. }
  1551. return 1;
  1552. }
  1553. /*-
  1554. * En/de-crypt and authenticate TLS packet. Returns the number of bytes written
  1555. * if successful. Otherwise -1 is returned. Code is big-endian.
  1556. */
  1557. static int s390x_aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1558. const unsigned char *in, size_t len)
  1559. {
  1560. S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, ctx);
  1561. const unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
  1562. const int enc = EVP_CIPHER_CTX_encrypting(ctx);
  1563. int rv = -1;
  1564. if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
  1565. return -1;
  1566. if (EVP_CIPHER_CTX_ctrl(ctx, enc ? EVP_CTRL_GCM_IV_GEN
  1567. : EVP_CTRL_GCM_SET_IV_INV,
  1568. EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
  1569. goto err;
  1570. in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  1571. out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  1572. len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
  1573. gctx->kma.param.taadl = gctx->tls_aad_len << 3;
  1574. gctx->kma.param.tpcl = len << 3;
  1575. s390x_kma(buf, gctx->tls_aad_len, in, len, out,
  1576. gctx->fc | S390X_KMA_LAAD | S390X_KMA_LPC, &gctx->kma.param);
  1577. if (enc) {
  1578. memcpy(out + len, gctx->kma.param.t.b, EVP_GCM_TLS_TAG_LEN);
  1579. rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
  1580. } else {
  1581. if (CRYPTO_memcmp(gctx->kma.param.t.b, in + len,
  1582. EVP_GCM_TLS_TAG_LEN)) {
  1583. OPENSSL_cleanse(out, len);
  1584. goto err;
  1585. }
  1586. rv = len;
  1587. }
  1588. err:
  1589. gctx->iv_set = 0;
  1590. gctx->tls_aad_len = -1;
  1591. return rv;
  1592. }
  1593. /*-
  1594. * Called from EVP layer to initialize context, process additional
  1595. * authenticated data, en/de-crypt plain/cipher-text and authenticate
  1596. * ciphertext or process a TLS packet, depending on context. Returns bytes
  1597. * written on success. Otherwise -1 is returned. Code is big-endian.
  1598. */
  1599. static int s390x_aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1600. const unsigned char *in, size_t len)
  1601. {
  1602. S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, ctx);
  1603. unsigned char *buf, tmp[16];
  1604. int enc;
  1605. if (!gctx->key_set)
  1606. return -1;
  1607. if (gctx->tls_aad_len >= 0)
  1608. return s390x_aes_gcm_tls_cipher(ctx, out, in, len);
  1609. if (!gctx->iv_set)
  1610. return -1;
  1611. if (in != NULL) {
  1612. if (out == NULL) {
  1613. if (s390x_aes_gcm_aad(gctx, in, len))
  1614. return -1;
  1615. } else {
  1616. if (s390x_aes_gcm(gctx, in, out, len))
  1617. return -1;
  1618. }
  1619. return len;
  1620. } else {
  1621. gctx->kma.param.taadl <<= 3;
  1622. gctx->kma.param.tpcl <<= 3;
  1623. s390x_kma(gctx->ares, gctx->areslen, gctx->mres, gctx->mreslen, tmp,
  1624. gctx->fc | S390X_KMA_LAAD | S390X_KMA_LPC, &gctx->kma.param);
  1625. /* recall that we already did en-/decrypt gctx->mres
  1626. * and returned it to caller... */
  1627. OPENSSL_cleanse(tmp, gctx->mreslen);
  1628. gctx->iv_set = 0;
  1629. enc = EVP_CIPHER_CTX_encrypting(ctx);
  1630. if (enc) {
  1631. gctx->taglen = 16;
  1632. } else {
  1633. if (gctx->taglen < 0)
  1634. return -1;
  1635. buf = EVP_CIPHER_CTX_buf_noconst(ctx);
  1636. if (CRYPTO_memcmp(buf, gctx->kma.param.t.b, gctx->taglen))
  1637. return -1;
  1638. }
  1639. return 0;
  1640. }
  1641. }
  1642. static int s390x_aes_gcm_cleanup(EVP_CIPHER_CTX *c)
  1643. {
  1644. S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, c);
  1645. const unsigned char *iv;
  1646. if (gctx == NULL)
  1647. return 0;
  1648. iv = EVP_CIPHER_CTX_iv(c);
  1649. if (iv != gctx->iv)
  1650. OPENSSL_free(gctx->iv);
  1651. OPENSSL_cleanse(gctx, sizeof(*gctx));
  1652. return 1;
  1653. }
  1654. # define S390X_AES_XTS_CTX EVP_AES_XTS_CTX
  1655. # define S390X_aes_128_xts_CAPABLE 1 /* checked by callee */
  1656. # define S390X_aes_256_xts_CAPABLE 1
  1657. # define s390x_aes_xts_init_key aes_xts_init_key
  1658. static int s390x_aes_xts_init_key(EVP_CIPHER_CTX *ctx,
  1659. const unsigned char *key,
  1660. const unsigned char *iv, int enc);
  1661. # define s390x_aes_xts_cipher aes_xts_cipher
  1662. static int s390x_aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1663. const unsigned char *in, size_t len);
  1664. # define s390x_aes_xts_ctrl aes_xts_ctrl
  1665. static int s390x_aes_xts_ctrl(EVP_CIPHER_CTX *, int type, int arg, void *ptr);
  1666. # define s390x_aes_xts_cleanup aes_xts_cleanup
  1667. # define S390X_aes_128_ccm_CAPABLE (S390X_aes_128_CAPABLE && \
  1668. (OPENSSL_s390xcap_P.kmac[0] & \
  1669. S390X_CAPBIT(S390X_AES_128)))
  1670. # define S390X_aes_192_ccm_CAPABLE (S390X_aes_192_CAPABLE && \
  1671. (OPENSSL_s390xcap_P.kmac[0] & \
  1672. S390X_CAPBIT(S390X_AES_192)))
  1673. # define S390X_aes_256_ccm_CAPABLE (S390X_aes_256_CAPABLE && \
  1674. (OPENSSL_s390xcap_P.kmac[0] & \
  1675. S390X_CAPBIT(S390X_AES_256)))
  1676. # define S390X_CCM_AAD_FLAG 0x40
  1677. /*-
  1678. * Set nonce and length fields. Code is big-endian.
  1679. */
  1680. static inline void s390x_aes_ccm_setiv(S390X_AES_CCM_CTX *ctx,
  1681. const unsigned char *nonce,
  1682. size_t mlen)
  1683. {
  1684. ctx->aes.ccm.nonce.b[0] &= ~S390X_CCM_AAD_FLAG;
  1685. ctx->aes.ccm.nonce.g[1] = mlen;
  1686. memcpy(ctx->aes.ccm.nonce.b + 1, nonce, 15 - ctx->aes.ccm.l);
  1687. }
  1688. /*-
  1689. * Process additional authenticated data. Code is big-endian.
  1690. */
  1691. static void s390x_aes_ccm_aad(S390X_AES_CCM_CTX *ctx, const unsigned char *aad,
  1692. size_t alen)
  1693. {
  1694. unsigned char *ptr;
  1695. int i, rem;
  1696. if (!alen)
  1697. return;
  1698. ctx->aes.ccm.nonce.b[0] |= S390X_CCM_AAD_FLAG;
  1699. /* Suppress 'type-punned pointer dereference' warning. */
  1700. ptr = ctx->aes.ccm.buf.b;
  1701. if (alen < ((1 << 16) - (1 << 8))) {
  1702. *(uint16_t *)ptr = alen;
  1703. i = 2;
  1704. } else if (sizeof(alen) == 8
  1705. && alen >= (size_t)1 << (32 % (sizeof(alen) * 8))) {
  1706. *(uint16_t *)ptr = 0xffff;
  1707. *(uint64_t *)(ptr + 2) = alen;
  1708. i = 10;
  1709. } else {
  1710. *(uint16_t *)ptr = 0xfffe;
  1711. *(uint32_t *)(ptr + 2) = alen;
  1712. i = 6;
  1713. }
  1714. while (i < 16 && alen) {
  1715. ctx->aes.ccm.buf.b[i] = *aad;
  1716. ++aad;
  1717. --alen;
  1718. ++i;
  1719. }
  1720. while (i < 16) {
  1721. ctx->aes.ccm.buf.b[i] = 0;
  1722. ++i;
  1723. }
  1724. ctx->aes.ccm.kmac_param.icv.g[0] = 0;
  1725. ctx->aes.ccm.kmac_param.icv.g[1] = 0;
  1726. s390x_kmac(ctx->aes.ccm.nonce.b, 32, ctx->aes.ccm.fc,
  1727. &ctx->aes.ccm.kmac_param);
  1728. ctx->aes.ccm.blocks += 2;
  1729. rem = alen & 0xf;
  1730. alen &= ~(size_t)0xf;
  1731. if (alen) {
  1732. s390x_kmac(aad, alen, ctx->aes.ccm.fc, &ctx->aes.ccm.kmac_param);
  1733. ctx->aes.ccm.blocks += alen >> 4;
  1734. aad += alen;
  1735. }
  1736. if (rem) {
  1737. for (i = 0; i < rem; i++)
  1738. ctx->aes.ccm.kmac_param.icv.b[i] ^= aad[i];
  1739. s390x_km(ctx->aes.ccm.kmac_param.icv.b, 16,
  1740. ctx->aes.ccm.kmac_param.icv.b, ctx->aes.ccm.fc,
  1741. ctx->aes.ccm.kmac_param.k);
  1742. ctx->aes.ccm.blocks++;
  1743. }
  1744. }
  1745. /*-
  1746. * En/de-crypt plain/cipher-text. Compute tag from plaintext. Returns 0 for
  1747. * success.
  1748. */
  1749. static int s390x_aes_ccm(S390X_AES_CCM_CTX *ctx, const unsigned char *in,
  1750. unsigned char *out, size_t len, int enc)
  1751. {
  1752. size_t n, rem;
  1753. unsigned int i, l, num;
  1754. unsigned char flags;
  1755. flags = ctx->aes.ccm.nonce.b[0];
  1756. if (!(flags & S390X_CCM_AAD_FLAG)) {
  1757. s390x_km(ctx->aes.ccm.nonce.b, 16, ctx->aes.ccm.kmac_param.icv.b,
  1758. ctx->aes.ccm.fc, ctx->aes.ccm.kmac_param.k);
  1759. ctx->aes.ccm.blocks++;
  1760. }
  1761. l = flags & 0x7;
  1762. ctx->aes.ccm.nonce.b[0] = l;
  1763. /*-
  1764. * Reconstruct length from encoded length field
  1765. * and initialize it with counter value.
  1766. */
  1767. n = 0;
  1768. for (i = 15 - l; i < 15; i++) {
  1769. n |= ctx->aes.ccm.nonce.b[i];
  1770. ctx->aes.ccm.nonce.b[i] = 0;
  1771. n <<= 8;
  1772. }
  1773. n |= ctx->aes.ccm.nonce.b[15];
  1774. ctx->aes.ccm.nonce.b[15] = 1;
  1775. if (n != len)
  1776. return -1; /* length mismatch */
  1777. if (enc) {
  1778. /* Two operations per block plus one for tag encryption */
  1779. ctx->aes.ccm.blocks += (((len + 15) >> 4) << 1) + 1;
  1780. if (ctx->aes.ccm.blocks > (1ULL << 61))
  1781. return -2; /* too much data */
  1782. }
  1783. num = 0;
  1784. rem = len & 0xf;
  1785. len &= ~(size_t)0xf;
  1786. if (enc) {
  1787. /* mac-then-encrypt */
  1788. if (len)
  1789. s390x_kmac(in, len, ctx->aes.ccm.fc, &ctx->aes.ccm.kmac_param);
  1790. if (rem) {
  1791. for (i = 0; i < rem; i++)
  1792. ctx->aes.ccm.kmac_param.icv.b[i] ^= in[len + i];
  1793. s390x_km(ctx->aes.ccm.kmac_param.icv.b, 16,
  1794. ctx->aes.ccm.kmac_param.icv.b, ctx->aes.ccm.fc,
  1795. ctx->aes.ccm.kmac_param.k);
  1796. }
  1797. CRYPTO_ctr128_encrypt_ctr32(in, out, len + rem, &ctx->aes.key.k,
  1798. ctx->aes.ccm.nonce.b, ctx->aes.ccm.buf.b,
  1799. &num, (ctr128_f)AES_ctr32_encrypt);
  1800. } else {
  1801. /* decrypt-then-mac */
  1802. CRYPTO_ctr128_encrypt_ctr32(in, out, len + rem, &ctx->aes.key.k,
  1803. ctx->aes.ccm.nonce.b, ctx->aes.ccm.buf.b,
  1804. &num, (ctr128_f)AES_ctr32_encrypt);
  1805. if (len)
  1806. s390x_kmac(out, len, ctx->aes.ccm.fc, &ctx->aes.ccm.kmac_param);
  1807. if (rem) {
  1808. for (i = 0; i < rem; i++)
  1809. ctx->aes.ccm.kmac_param.icv.b[i] ^= out[len + i];
  1810. s390x_km(ctx->aes.ccm.kmac_param.icv.b, 16,
  1811. ctx->aes.ccm.kmac_param.icv.b, ctx->aes.ccm.fc,
  1812. ctx->aes.ccm.kmac_param.k);
  1813. }
  1814. }
  1815. /* encrypt tag */
  1816. for (i = 15 - l; i < 16; i++)
  1817. ctx->aes.ccm.nonce.b[i] = 0;
  1818. s390x_km(ctx->aes.ccm.nonce.b, 16, ctx->aes.ccm.buf.b, ctx->aes.ccm.fc,
  1819. ctx->aes.ccm.kmac_param.k);
  1820. ctx->aes.ccm.kmac_param.icv.g[0] ^= ctx->aes.ccm.buf.g[0];
  1821. ctx->aes.ccm.kmac_param.icv.g[1] ^= ctx->aes.ccm.buf.g[1];
  1822. ctx->aes.ccm.nonce.b[0] = flags; /* restore flags field */
  1823. return 0;
  1824. }
  1825. /*-
  1826. * En/de-crypt and authenticate TLS packet. Returns the number of bytes written
  1827. * if successful. Otherwise -1 is returned.
  1828. */
  1829. static int s390x_aes_ccm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1830. const unsigned char *in, size_t len)
  1831. {
  1832. S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, ctx);
  1833. unsigned char *ivec = EVP_CIPHER_CTX_iv_noconst(ctx);
  1834. unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
  1835. const int enc = EVP_CIPHER_CTX_encrypting(ctx);
  1836. if (out != in
  1837. || len < (EVP_CCM_TLS_EXPLICIT_IV_LEN + (size_t)cctx->aes.ccm.m))
  1838. return -1;
  1839. if (enc) {
  1840. /* Set explicit iv (sequence number). */
  1841. memcpy(out, buf, EVP_CCM_TLS_EXPLICIT_IV_LEN);
  1842. }
  1843. len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->aes.ccm.m;
  1844. /*-
  1845. * Get explicit iv (sequence number). We already have fixed iv
  1846. * (server/client_write_iv) here.
  1847. */
  1848. memcpy(ivec + EVP_CCM_TLS_FIXED_IV_LEN, in, EVP_CCM_TLS_EXPLICIT_IV_LEN);
  1849. s390x_aes_ccm_setiv(cctx, ivec, len);
  1850. /* Process aad (sequence number|type|version|length) */
  1851. s390x_aes_ccm_aad(cctx, buf, cctx->aes.ccm.tls_aad_len);
  1852. in += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  1853. out += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  1854. if (enc) {
  1855. if (s390x_aes_ccm(cctx, in, out, len, enc))
  1856. return -1;
  1857. memcpy(out + len, cctx->aes.ccm.kmac_param.icv.b, cctx->aes.ccm.m);
  1858. return len + EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->aes.ccm.m;
  1859. } else {
  1860. if (!s390x_aes_ccm(cctx, in, out, len, enc)) {
  1861. if (!CRYPTO_memcmp(cctx->aes.ccm.kmac_param.icv.b, in + len,
  1862. cctx->aes.ccm.m))
  1863. return len;
  1864. }
  1865. OPENSSL_cleanse(out, len);
  1866. return -1;
  1867. }
  1868. }
  1869. /*-
  1870. * Set key and flag field and/or iv. Returns 1 if successful. Otherwise 0 is
  1871. * returned.
  1872. */
  1873. static int s390x_aes_ccm_init_key(EVP_CIPHER_CTX *ctx,
  1874. const unsigned char *key,
  1875. const unsigned char *iv, int enc)
  1876. {
  1877. S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, ctx);
  1878. unsigned char *ivec;
  1879. int keylen;
  1880. if (iv == NULL && key == NULL)
  1881. return 1;
  1882. if (key != NULL) {
  1883. keylen = EVP_CIPHER_CTX_key_length(ctx);
  1884. cctx->aes.ccm.fc = S390X_AES_FC(keylen);
  1885. memcpy(cctx->aes.ccm.kmac_param.k, key, keylen);
  1886. /* Store encoded m and l. */
  1887. cctx->aes.ccm.nonce.b[0] = ((cctx->aes.ccm.l - 1) & 0x7)
  1888. | (((cctx->aes.ccm.m - 2) >> 1) & 0x7) << 3;
  1889. memset(cctx->aes.ccm.nonce.b + 1, 0,
  1890. sizeof(cctx->aes.ccm.nonce.b));
  1891. cctx->aes.ccm.blocks = 0;
  1892. cctx->aes.ccm.key_set = 1;
  1893. }
  1894. if (iv != NULL) {
  1895. ivec = EVP_CIPHER_CTX_iv_noconst(ctx);
  1896. memcpy(ivec, iv, 15 - cctx->aes.ccm.l);
  1897. cctx->aes.ccm.iv_set = 1;
  1898. }
  1899. return 1;
  1900. }
  1901. /*-
  1902. * Called from EVP layer to initialize context, process additional
  1903. * authenticated data, en/de-crypt plain/cipher-text and authenticate
  1904. * plaintext or process a TLS packet, depending on context. Returns bytes
  1905. * written on success. Otherwise -1 is returned.
  1906. */
  1907. static int s390x_aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1908. const unsigned char *in, size_t len)
  1909. {
  1910. S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, ctx);
  1911. const int enc = EVP_CIPHER_CTX_encrypting(ctx);
  1912. int rv;
  1913. unsigned char *buf, *ivec;
  1914. if (!cctx->aes.ccm.key_set)
  1915. return -1;
  1916. if (cctx->aes.ccm.tls_aad_len >= 0)
  1917. return s390x_aes_ccm_tls_cipher(ctx, out, in, len);
  1918. /*-
  1919. * Final(): Does not return any data. Recall that ccm is mac-then-encrypt
  1920. * so integrity must be checked already at Update() i.e., before
  1921. * potentially corrupted data is output.
  1922. */
  1923. if (in == NULL && out != NULL)
  1924. return 0;
  1925. if (!cctx->aes.ccm.iv_set)
  1926. return -1;
  1927. if (!enc && !cctx->aes.ccm.tag_set)
  1928. return -1;
  1929. if (out == NULL) {
  1930. /* Update(): Pass message length. */
  1931. if (in == NULL) {
  1932. ivec = EVP_CIPHER_CTX_iv_noconst(ctx);
  1933. s390x_aes_ccm_setiv(cctx, ivec, len);
  1934. cctx->aes.ccm.len_set = 1;
  1935. return len;
  1936. }
  1937. /* Update(): Process aad. */
  1938. if (!cctx->aes.ccm.len_set && len)
  1939. return -1;
  1940. s390x_aes_ccm_aad(cctx, in, len);
  1941. return len;
  1942. }
  1943. /* Update(): Process message. */
  1944. if (!cctx->aes.ccm.len_set) {
  1945. /*-
  1946. * In case message length was not previously set explicitely via
  1947. * Update(), set it now.
  1948. */
  1949. ivec = EVP_CIPHER_CTX_iv_noconst(ctx);
  1950. s390x_aes_ccm_setiv(cctx, ivec, len);
  1951. cctx->aes.ccm.len_set = 1;
  1952. }
  1953. if (enc) {
  1954. if (s390x_aes_ccm(cctx, in, out, len, enc))
  1955. return -1;
  1956. cctx->aes.ccm.tag_set = 1;
  1957. return len;
  1958. } else {
  1959. rv = -1;
  1960. if (!s390x_aes_ccm(cctx, in, out, len, enc)) {
  1961. buf = EVP_CIPHER_CTX_buf_noconst(ctx);
  1962. if (!CRYPTO_memcmp(cctx->aes.ccm.kmac_param.icv.b, buf,
  1963. cctx->aes.ccm.m))
  1964. rv = len;
  1965. }
  1966. if (rv == -1)
  1967. OPENSSL_cleanse(out, len);
  1968. cctx->aes.ccm.iv_set = 0;
  1969. cctx->aes.ccm.tag_set = 0;
  1970. cctx->aes.ccm.len_set = 0;
  1971. return rv;
  1972. }
  1973. }
  1974. /*-
  1975. * Performs various operations on the context structure depending on control
  1976. * type. Returns 1 for success, 0 for failure and -1 for unknown control type.
  1977. * Code is big-endian.
  1978. */
  1979. static int s390x_aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
  1980. {
  1981. S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, c);
  1982. unsigned char *buf, *iv;
  1983. int enc, len;
  1984. switch (type) {
  1985. case EVP_CTRL_INIT:
  1986. cctx->aes.ccm.key_set = 0;
  1987. cctx->aes.ccm.iv_set = 0;
  1988. cctx->aes.ccm.l = 8;
  1989. cctx->aes.ccm.m = 12;
  1990. cctx->aes.ccm.tag_set = 0;
  1991. cctx->aes.ccm.len_set = 0;
  1992. cctx->aes.ccm.tls_aad_len = -1;
  1993. return 1;
  1994. case EVP_CTRL_AEAD_TLS1_AAD:
  1995. if (arg != EVP_AEAD_TLS1_AAD_LEN)
  1996. return 0;
  1997. /* Save the aad for later use. */
  1998. buf = EVP_CIPHER_CTX_buf_noconst(c);
  1999. memcpy(buf, ptr, arg);
  2000. cctx->aes.ccm.tls_aad_len = arg;
  2001. len = *(uint16_t *)(buf + arg - 2);
  2002. if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN)
  2003. return 0;
  2004. /* Correct length for explicit iv. */
  2005. len -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
  2006. enc = EVP_CIPHER_CTX_encrypting(c);
  2007. if (!enc) {
  2008. if (len < cctx->aes.ccm.m)
  2009. return 0;
  2010. /* Correct length for tag. */
  2011. len -= cctx->aes.ccm.m;
  2012. }
  2013. *(uint16_t *)(buf + arg - 2) = len;
  2014. /* Extra padding: tag appended to record. */
  2015. return cctx->aes.ccm.m;
  2016. case EVP_CTRL_CCM_SET_IV_FIXED:
  2017. if (arg != EVP_CCM_TLS_FIXED_IV_LEN)
  2018. return 0;
  2019. /* Copy to first part of the iv. */
  2020. iv = EVP_CIPHER_CTX_iv_noconst(c);
  2021. memcpy(iv, ptr, arg);
  2022. return 1;
  2023. case EVP_CTRL_AEAD_SET_IVLEN:
  2024. arg = 15 - arg;
  2025. /* fall-through */
  2026. case EVP_CTRL_CCM_SET_L:
  2027. if (arg < 2 || arg > 8)
  2028. return 0;
  2029. cctx->aes.ccm.l = arg;
  2030. return 1;
  2031. case EVP_CTRL_AEAD_SET_TAG:
  2032. if ((arg & 1) || arg < 4 || arg > 16)
  2033. return 0;
  2034. enc = EVP_CIPHER_CTX_encrypting(c);
  2035. if (enc && ptr)
  2036. return 0;
  2037. if (ptr) {
  2038. cctx->aes.ccm.tag_set = 1;
  2039. buf = EVP_CIPHER_CTX_buf_noconst(c);
  2040. memcpy(buf, ptr, arg);
  2041. }
  2042. cctx->aes.ccm.m = arg;
  2043. return 1;
  2044. case EVP_CTRL_AEAD_GET_TAG:
  2045. enc = EVP_CIPHER_CTX_encrypting(c);
  2046. if (!enc || !cctx->aes.ccm.tag_set)
  2047. return 0;
  2048. if(arg < cctx->aes.ccm.m)
  2049. return 0;
  2050. memcpy(ptr, cctx->aes.ccm.kmac_param.icv.b, cctx->aes.ccm.m);
  2051. cctx->aes.ccm.tag_set = 0;
  2052. cctx->aes.ccm.iv_set = 0;
  2053. cctx->aes.ccm.len_set = 0;
  2054. return 1;
  2055. case EVP_CTRL_COPY:
  2056. return 1;
  2057. default:
  2058. return -1;
  2059. }
  2060. }
  2061. # define s390x_aes_ccm_cleanup aes_ccm_cleanup
  2062. # ifndef OPENSSL_NO_OCB
  2063. # define S390X_AES_OCB_CTX EVP_AES_OCB_CTX
  2064. # define S390X_aes_128_ocb_CAPABLE 0
  2065. # define S390X_aes_192_ocb_CAPABLE 0
  2066. # define S390X_aes_256_ocb_CAPABLE 0
  2067. # define s390x_aes_ocb_init_key aes_ocb_init_key
  2068. static int s390x_aes_ocb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  2069. const unsigned char *iv, int enc);
  2070. # define s390x_aes_ocb_cipher aes_ocb_cipher
  2071. static int s390x_aes_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  2072. const unsigned char *in, size_t len);
  2073. # define s390x_aes_ocb_cleanup aes_ocb_cleanup
  2074. static int s390x_aes_ocb_cleanup(EVP_CIPHER_CTX *);
  2075. # define s390x_aes_ocb_ctrl aes_ocb_ctrl
  2076. static int s390x_aes_ocb_ctrl(EVP_CIPHER_CTX *, int type, int arg, void *ptr);
  2077. # endif
  2078. # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode, \
  2079. MODE,flags) \
  2080. static const EVP_CIPHER s390x_aes_##keylen##_##mode = { \
  2081. nid##_##keylen##_##nmode,blocksize, \
  2082. keylen / 8, \
  2083. ivlen, \
  2084. flags | EVP_CIPH_##MODE##_MODE, \
  2085. s390x_aes_##mode##_init_key, \
  2086. s390x_aes_##mode##_cipher, \
  2087. NULL, \
  2088. sizeof(S390X_AES_##MODE##_CTX), \
  2089. NULL, \
  2090. NULL, \
  2091. NULL, \
  2092. NULL \
  2093. }; \
  2094. static const EVP_CIPHER aes_##keylen##_##mode = { \
  2095. nid##_##keylen##_##nmode, \
  2096. blocksize, \
  2097. keylen / 8, \
  2098. ivlen, \
  2099. flags | EVP_CIPH_##MODE##_MODE, \
  2100. aes_init_key, \
  2101. aes_##mode##_cipher, \
  2102. NULL, \
  2103. sizeof(EVP_AES_KEY), \
  2104. NULL, \
  2105. NULL, \
  2106. NULL, \
  2107. NULL \
  2108. }; \
  2109. const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
  2110. { \
  2111. return S390X_aes_##keylen##_##mode##_CAPABLE ? \
  2112. &s390x_aes_##keylen##_##mode : &aes_##keylen##_##mode; \
  2113. }
  2114. # define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags)\
  2115. static const EVP_CIPHER s390x_aes_##keylen##_##mode = { \
  2116. nid##_##keylen##_##mode, \
  2117. blocksize, \
  2118. (EVP_CIPH_##MODE##_MODE == EVP_CIPH_XTS_MODE ? 2 : 1) * keylen / 8, \
  2119. ivlen, \
  2120. flags | EVP_CIPH_##MODE##_MODE, \
  2121. s390x_aes_##mode##_init_key, \
  2122. s390x_aes_##mode##_cipher, \
  2123. s390x_aes_##mode##_cleanup, \
  2124. sizeof(S390X_AES_##MODE##_CTX), \
  2125. NULL, \
  2126. NULL, \
  2127. s390x_aes_##mode##_ctrl, \
  2128. NULL \
  2129. }; \
  2130. static const EVP_CIPHER aes_##keylen##_##mode = { \
  2131. nid##_##keylen##_##mode,blocksize, \
  2132. (EVP_CIPH_##MODE##_MODE == EVP_CIPH_XTS_MODE ? 2 : 1) * keylen / 8, \
  2133. ivlen, \
  2134. flags | EVP_CIPH_##MODE##_MODE, \
  2135. aes_##mode##_init_key, \
  2136. aes_##mode##_cipher, \
  2137. aes_##mode##_cleanup, \
  2138. sizeof(EVP_AES_##MODE##_CTX), \
  2139. NULL, \
  2140. NULL, \
  2141. aes_##mode##_ctrl, \
  2142. NULL \
  2143. }; \
  2144. const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
  2145. { \
  2146. return S390X_aes_##keylen##_##mode##_CAPABLE ? \
  2147. &s390x_aes_##keylen##_##mode : &aes_##keylen##_##mode; \
  2148. }
  2149. #else
  2150. # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
  2151. static const EVP_CIPHER aes_##keylen##_##mode = { \
  2152. nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
  2153. flags|EVP_CIPH_##MODE##_MODE, \
  2154. aes_init_key, \
  2155. aes_##mode##_cipher, \
  2156. NULL, \
  2157. sizeof(EVP_AES_KEY), \
  2158. NULL,NULL,NULL,NULL }; \
  2159. const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
  2160. { return &aes_##keylen##_##mode; }
  2161. # define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
  2162. static const EVP_CIPHER aes_##keylen##_##mode = { \
  2163. nid##_##keylen##_##mode,blocksize, \
  2164. (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
  2165. flags|EVP_CIPH_##MODE##_MODE, \
  2166. aes_##mode##_init_key, \
  2167. aes_##mode##_cipher, \
  2168. aes_##mode##_cleanup, \
  2169. sizeof(EVP_AES_##MODE##_CTX), \
  2170. NULL,NULL,aes_##mode##_ctrl,NULL }; \
  2171. const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
  2172. { return &aes_##keylen##_##mode; }
  2173. #endif
  2174. #if defined(OPENSSL_CPUID_OBJ) && (defined(__arm__) || defined(__arm) || defined(__aarch64__))
  2175. # include "arm_arch.h"
  2176. # if __ARM_MAX_ARCH__>=7
  2177. # if defined(BSAES_ASM)
  2178. # define BSAES_CAPABLE (OPENSSL_armcap_P & ARMV7_NEON)
  2179. # endif
  2180. # if defined(VPAES_ASM)
  2181. # define VPAES_CAPABLE (OPENSSL_armcap_P & ARMV7_NEON)
  2182. # endif
  2183. # define HWAES_CAPABLE (OPENSSL_armcap_P & ARMV8_AES)
  2184. # define HWAES_set_encrypt_key aes_v8_set_encrypt_key
  2185. # define HWAES_set_decrypt_key aes_v8_set_decrypt_key
  2186. # define HWAES_encrypt aes_v8_encrypt
  2187. # define HWAES_decrypt aes_v8_decrypt
  2188. # define HWAES_cbc_encrypt aes_v8_cbc_encrypt
  2189. # define HWAES_ctr32_encrypt_blocks aes_v8_ctr32_encrypt_blocks
  2190. # endif
  2191. #endif
  2192. #if defined(HWAES_CAPABLE)
  2193. int HWAES_set_encrypt_key(const unsigned char *userKey, const int bits,
  2194. AES_KEY *key);
  2195. int HWAES_set_decrypt_key(const unsigned char *userKey, const int bits,
  2196. AES_KEY *key);
  2197. void HWAES_encrypt(const unsigned char *in, unsigned char *out,
  2198. const AES_KEY *key);
  2199. void HWAES_decrypt(const unsigned char *in, unsigned char *out,
  2200. const AES_KEY *key);
  2201. void HWAES_cbc_encrypt(const unsigned char *in, unsigned char *out,
  2202. size_t length, const AES_KEY *key,
  2203. unsigned char *ivec, const int enc);
  2204. void HWAES_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
  2205. size_t len, const AES_KEY *key,
  2206. const unsigned char ivec[16]);
  2207. void HWAES_xts_encrypt(const unsigned char *inp, unsigned char *out,
  2208. size_t len, const AES_KEY *key1,
  2209. const AES_KEY *key2, const unsigned char iv[16]);
  2210. void HWAES_xts_decrypt(const unsigned char *inp, unsigned char *out,
  2211. size_t len, const AES_KEY *key1,
  2212. const AES_KEY *key2, const unsigned char iv[16]);
  2213. #endif
  2214. #define BLOCK_CIPHER_generic_pack(nid,keylen,flags) \
  2215. BLOCK_CIPHER_generic(nid,keylen,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
  2216. BLOCK_CIPHER_generic(nid,keylen,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
  2217. BLOCK_CIPHER_generic(nid,keylen,1,16,ofb128,ofb,OFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
  2218. BLOCK_CIPHER_generic(nid,keylen,1,16,cfb128,cfb,CFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
  2219. BLOCK_CIPHER_generic(nid,keylen,1,16,cfb1,cfb1,CFB,flags) \
  2220. BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags) \
  2221. BLOCK_CIPHER_generic(nid,keylen,1,16,ctr,ctr,CTR,flags)
  2222. static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  2223. const unsigned char *iv, int enc)
  2224. {
  2225. int ret, mode;
  2226. EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
  2227. mode = EVP_CIPHER_CTX_mode(ctx);
  2228. if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
  2229. && !enc) {
  2230. #ifdef HWAES_CAPABLE
  2231. if (HWAES_CAPABLE) {
  2232. ret = HWAES_set_decrypt_key(key,
  2233. EVP_CIPHER_CTX_key_length(ctx) * 8,
  2234. &dat->ks.ks);
  2235. dat->block = (block128_f) HWAES_decrypt;
  2236. dat->stream.cbc = NULL;
  2237. # ifdef HWAES_cbc_encrypt
  2238. if (mode == EVP_CIPH_CBC_MODE)
  2239. dat->stream.cbc = (cbc128_f) HWAES_cbc_encrypt;
  2240. # endif
  2241. } else
  2242. #endif
  2243. #ifdef BSAES_CAPABLE
  2244. if (BSAES_CAPABLE && mode == EVP_CIPH_CBC_MODE) {
  2245. ret = AES_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  2246. &dat->ks.ks);
  2247. dat->block = (block128_f) AES_decrypt;
  2248. dat->stream.cbc = (cbc128_f) bsaes_cbc_encrypt;
  2249. } else
  2250. #endif
  2251. #ifdef VPAES_CAPABLE
  2252. if (VPAES_CAPABLE) {
  2253. ret = vpaes_set_decrypt_key(key,
  2254. EVP_CIPHER_CTX_key_length(ctx) * 8,
  2255. &dat->ks.ks);
  2256. dat->block = (block128_f) vpaes_decrypt;
  2257. dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
  2258. (cbc128_f) vpaes_cbc_encrypt : NULL;
  2259. } else
  2260. #endif
  2261. {
  2262. ret = AES_set_decrypt_key(key,
  2263. EVP_CIPHER_CTX_key_length(ctx) * 8,
  2264. &dat->ks.ks);
  2265. dat->block = (block128_f) AES_decrypt;
  2266. dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
  2267. (cbc128_f) AES_cbc_encrypt : NULL;
  2268. }
  2269. } else
  2270. #ifdef HWAES_CAPABLE
  2271. if (HWAES_CAPABLE) {
  2272. ret = HWAES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  2273. &dat->ks.ks);
  2274. dat->block = (block128_f) HWAES_encrypt;
  2275. dat->stream.cbc = NULL;
  2276. # ifdef HWAES_cbc_encrypt
  2277. if (mode == EVP_CIPH_CBC_MODE)
  2278. dat->stream.cbc = (cbc128_f) HWAES_cbc_encrypt;
  2279. else
  2280. # endif
  2281. # ifdef HWAES_ctr32_encrypt_blocks
  2282. if (mode == EVP_CIPH_CTR_MODE)
  2283. dat->stream.ctr = (ctr128_f) HWAES_ctr32_encrypt_blocks;
  2284. else
  2285. # endif
  2286. (void)0; /* terminate potentially open 'else' */
  2287. } else
  2288. #endif
  2289. #ifdef BSAES_CAPABLE
  2290. if (BSAES_CAPABLE && mode == EVP_CIPH_CTR_MODE) {
  2291. ret = AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  2292. &dat->ks.ks);
  2293. dat->block = (block128_f) AES_encrypt;
  2294. dat->stream.ctr = (ctr128_f) bsaes_ctr32_encrypt_blocks;
  2295. } else
  2296. #endif
  2297. #ifdef VPAES_CAPABLE
  2298. if (VPAES_CAPABLE) {
  2299. ret = vpaes_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  2300. &dat->ks.ks);
  2301. dat->block = (block128_f) vpaes_encrypt;
  2302. dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
  2303. (cbc128_f) vpaes_cbc_encrypt : NULL;
  2304. } else
  2305. #endif
  2306. {
  2307. ret = AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  2308. &dat->ks.ks);
  2309. dat->block = (block128_f) AES_encrypt;
  2310. dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
  2311. (cbc128_f) AES_cbc_encrypt : NULL;
  2312. #ifdef AES_CTR_ASM
  2313. if (mode == EVP_CIPH_CTR_MODE)
  2314. dat->stream.ctr = (ctr128_f) AES_ctr32_encrypt;
  2315. #endif
  2316. }
  2317. if (ret < 0) {
  2318. EVPerr(EVP_F_AES_INIT_KEY, EVP_R_AES_KEY_SETUP_FAILED);
  2319. return 0;
  2320. }
  2321. return 1;
  2322. }
  2323. static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  2324. const unsigned char *in, size_t len)
  2325. {
  2326. EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
  2327. if (dat->stream.cbc)
  2328. (*dat->stream.cbc) (in, out, len, &dat->ks,
  2329. EVP_CIPHER_CTX_iv_noconst(ctx),
  2330. EVP_CIPHER_CTX_encrypting(ctx));
  2331. else if (EVP_CIPHER_CTX_encrypting(ctx))
  2332. CRYPTO_cbc128_encrypt(in, out, len, &dat->ks,
  2333. EVP_CIPHER_CTX_iv_noconst(ctx), dat->block);
  2334. else
  2335. CRYPTO_cbc128_decrypt(in, out, len, &dat->ks,
  2336. EVP_CIPHER_CTX_iv_noconst(ctx), dat->block);
  2337. return 1;
  2338. }
  2339. static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  2340. const unsigned char *in, size_t len)
  2341. {
  2342. size_t bl = EVP_CIPHER_CTX_block_size(ctx);
  2343. size_t i;
  2344. EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
  2345. if (len < bl)
  2346. return 1;
  2347. for (i = 0, len -= bl; i <= len; i += bl)
  2348. (*dat->block) (in + i, out + i, &dat->ks);
  2349. return 1;
  2350. }
  2351. static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  2352. const unsigned char *in, size_t len)
  2353. {
  2354. EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
  2355. int num = EVP_CIPHER_CTX_num(ctx);
  2356. CRYPTO_ofb128_encrypt(in, out, len, &dat->ks,
  2357. EVP_CIPHER_CTX_iv_noconst(ctx), &num, dat->block);
  2358. EVP_CIPHER_CTX_set_num(ctx, num);
  2359. return 1;
  2360. }
  2361. static int aes_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  2362. const unsigned char *in, size_t len)
  2363. {
  2364. EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
  2365. int num = EVP_CIPHER_CTX_num(ctx);
  2366. CRYPTO_cfb128_encrypt(in, out, len, &dat->ks,
  2367. EVP_CIPHER_CTX_iv_noconst(ctx), &num,
  2368. EVP_CIPHER_CTX_encrypting(ctx), dat->block);
  2369. EVP_CIPHER_CTX_set_num(ctx, num);
  2370. return 1;
  2371. }
  2372. static int aes_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  2373. const unsigned char *in, size_t len)
  2374. {
  2375. EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
  2376. int num = EVP_CIPHER_CTX_num(ctx);
  2377. CRYPTO_cfb128_8_encrypt(in, out, len, &dat->ks,
  2378. EVP_CIPHER_CTX_iv_noconst(ctx), &num,
  2379. EVP_CIPHER_CTX_encrypting(ctx), dat->block);
  2380. EVP_CIPHER_CTX_set_num(ctx, num);
  2381. return 1;
  2382. }
  2383. static int aes_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  2384. const unsigned char *in, size_t len)
  2385. {
  2386. EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
  2387. if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS)) {
  2388. int num = EVP_CIPHER_CTX_num(ctx);
  2389. CRYPTO_cfb128_1_encrypt(in, out, len, &dat->ks,
  2390. EVP_CIPHER_CTX_iv_noconst(ctx), &num,
  2391. EVP_CIPHER_CTX_encrypting(ctx), dat->block);
  2392. EVP_CIPHER_CTX_set_num(ctx, num);
  2393. return 1;
  2394. }
  2395. while (len >= MAXBITCHUNK) {
  2396. int num = EVP_CIPHER_CTX_num(ctx);
  2397. CRYPTO_cfb128_1_encrypt(in, out, MAXBITCHUNK * 8, &dat->ks,
  2398. EVP_CIPHER_CTX_iv_noconst(ctx), &num,
  2399. EVP_CIPHER_CTX_encrypting(ctx), dat->block);
  2400. EVP_CIPHER_CTX_set_num(ctx, num);
  2401. len -= MAXBITCHUNK;
  2402. out += MAXBITCHUNK;
  2403. in += MAXBITCHUNK;
  2404. }
  2405. if (len) {
  2406. int num = EVP_CIPHER_CTX_num(ctx);
  2407. CRYPTO_cfb128_1_encrypt(in, out, len * 8, &dat->ks,
  2408. EVP_CIPHER_CTX_iv_noconst(ctx), &num,
  2409. EVP_CIPHER_CTX_encrypting(ctx), dat->block);
  2410. EVP_CIPHER_CTX_set_num(ctx, num);
  2411. }
  2412. return 1;
  2413. }
  2414. static int aes_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  2415. const unsigned char *in, size_t len)
  2416. {
  2417. unsigned int num = EVP_CIPHER_CTX_num(ctx);
  2418. EVP_AES_KEY *dat = EVP_C_DATA(EVP_AES_KEY,ctx);
  2419. if (dat->stream.ctr)
  2420. CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks,
  2421. EVP_CIPHER_CTX_iv_noconst(ctx),
  2422. EVP_CIPHER_CTX_buf_noconst(ctx),
  2423. &num, dat->stream.ctr);
  2424. else
  2425. CRYPTO_ctr128_encrypt(in, out, len, &dat->ks,
  2426. EVP_CIPHER_CTX_iv_noconst(ctx),
  2427. EVP_CIPHER_CTX_buf_noconst(ctx), &num,
  2428. dat->block);
  2429. EVP_CIPHER_CTX_set_num(ctx, num);
  2430. return 1;
  2431. }
  2432. BLOCK_CIPHER_generic_pack(NID_aes, 128, 0)
  2433. BLOCK_CIPHER_generic_pack(NID_aes, 192, 0)
  2434. BLOCK_CIPHER_generic_pack(NID_aes, 256, 0)
  2435. static int aes_gcm_cleanup(EVP_CIPHER_CTX *c)
  2436. {
  2437. EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,c);
  2438. if (gctx == NULL)
  2439. return 0;
  2440. OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm));
  2441. if (gctx->iv != EVP_CIPHER_CTX_iv_noconst(c))
  2442. OPENSSL_free(gctx->iv);
  2443. return 1;
  2444. }
  2445. /* increment counter (64-bit int) by 1 */
  2446. static void ctr64_inc(unsigned char *counter)
  2447. {
  2448. int n = 8;
  2449. unsigned char c;
  2450. do {
  2451. --n;
  2452. c = counter[n];
  2453. ++c;
  2454. counter[n] = c;
  2455. if (c)
  2456. return;
  2457. } while (n);
  2458. }
  2459. static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
  2460. {
  2461. EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,c);
  2462. switch (type) {
  2463. case EVP_CTRL_INIT:
  2464. gctx->key_set = 0;
  2465. gctx->iv_set = 0;
  2466. gctx->ivlen = c->cipher->iv_len;
  2467. gctx->iv = c->iv;
  2468. gctx->taglen = -1;
  2469. gctx->iv_gen = 0;
  2470. gctx->tls_aad_len = -1;
  2471. return 1;
  2472. case EVP_CTRL_AEAD_SET_IVLEN:
  2473. if (arg <= 0)
  2474. return 0;
  2475. /* Allocate memory for IV if needed */
  2476. if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen)) {
  2477. if (gctx->iv != c->iv)
  2478. OPENSSL_free(gctx->iv);
  2479. if ((gctx->iv = OPENSSL_malloc(arg)) == NULL) {
  2480. EVPerr(EVP_F_AES_GCM_CTRL, ERR_R_MALLOC_FAILURE);
  2481. return 0;
  2482. }
  2483. }
  2484. gctx->ivlen = arg;
  2485. return 1;
  2486. case EVP_CTRL_AEAD_SET_TAG:
  2487. if (arg <= 0 || arg > 16 || c->encrypt)
  2488. return 0;
  2489. memcpy(c->buf, ptr, arg);
  2490. gctx->taglen = arg;
  2491. return 1;
  2492. case EVP_CTRL_AEAD_GET_TAG:
  2493. if (arg <= 0 || arg > 16 || !c->encrypt
  2494. || gctx->taglen < 0)
  2495. return 0;
  2496. memcpy(ptr, c->buf, arg);
  2497. return 1;
  2498. case EVP_CTRL_GCM_SET_IV_FIXED:
  2499. /* Special case: -1 length restores whole IV */
  2500. if (arg == -1) {
  2501. memcpy(gctx->iv, ptr, gctx->ivlen);
  2502. gctx->iv_gen = 1;
  2503. return 1;
  2504. }
  2505. /*
  2506. * Fixed field must be at least 4 bytes and invocation field at least
  2507. * 8.
  2508. */
  2509. if ((arg < 4) || (gctx->ivlen - arg) < 8)
  2510. return 0;
  2511. if (arg)
  2512. memcpy(gctx->iv, ptr, arg);
  2513. if (c->encrypt && RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
  2514. return 0;
  2515. gctx->iv_gen = 1;
  2516. return 1;
  2517. case EVP_CTRL_GCM_IV_GEN:
  2518. if (gctx->iv_gen == 0 || gctx->key_set == 0)
  2519. return 0;
  2520. CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
  2521. if (arg <= 0 || arg > gctx->ivlen)
  2522. arg = gctx->ivlen;
  2523. memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
  2524. /*
  2525. * Invocation field will be at least 8 bytes in size and so no need
  2526. * to check wrap around or increment more than last 8 bytes.
  2527. */
  2528. ctr64_inc(gctx->iv + gctx->ivlen - 8);
  2529. gctx->iv_set = 1;
  2530. return 1;
  2531. case EVP_CTRL_GCM_SET_IV_INV:
  2532. if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt)
  2533. return 0;
  2534. memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
  2535. CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
  2536. gctx->iv_set = 1;
  2537. return 1;
  2538. case EVP_CTRL_AEAD_TLS1_AAD:
  2539. /* Save the AAD for later use */
  2540. if (arg != EVP_AEAD_TLS1_AAD_LEN)
  2541. return 0;
  2542. memcpy(c->buf, ptr, arg);
  2543. gctx->tls_aad_len = arg;
  2544. {
  2545. unsigned int len = c->buf[arg - 2] << 8 | c->buf[arg - 1];
  2546. /* Correct length for explicit IV */
  2547. if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
  2548. return 0;
  2549. len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
  2550. /* If decrypting correct for tag too */
  2551. if (!c->encrypt) {
  2552. if (len < EVP_GCM_TLS_TAG_LEN)
  2553. return 0;
  2554. len -= EVP_GCM_TLS_TAG_LEN;
  2555. }
  2556. c->buf[arg - 2] = len >> 8;
  2557. c->buf[arg - 1] = len & 0xff;
  2558. }
  2559. /* Extra padding: tag appended to record */
  2560. return EVP_GCM_TLS_TAG_LEN;
  2561. case EVP_CTRL_COPY:
  2562. {
  2563. EVP_CIPHER_CTX *out = ptr;
  2564. EVP_AES_GCM_CTX *gctx_out = EVP_C_DATA(EVP_AES_GCM_CTX,out);
  2565. if (gctx->gcm.key) {
  2566. if (gctx->gcm.key != &gctx->ks)
  2567. return 0;
  2568. gctx_out->gcm.key = &gctx_out->ks;
  2569. }
  2570. if (gctx->iv == c->iv)
  2571. gctx_out->iv = out->iv;
  2572. else {
  2573. if ((gctx_out->iv = OPENSSL_malloc(gctx->ivlen)) == NULL) {
  2574. EVPerr(EVP_F_AES_GCM_CTRL, ERR_R_MALLOC_FAILURE);
  2575. return 0;
  2576. }
  2577. memcpy(gctx_out->iv, gctx->iv, gctx->ivlen);
  2578. }
  2579. return 1;
  2580. }
  2581. default:
  2582. return -1;
  2583. }
  2584. }
  2585. static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  2586. const unsigned char *iv, int enc)
  2587. {
  2588. EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,ctx);
  2589. if (!iv && !key)
  2590. return 1;
  2591. if (key) {
  2592. do {
  2593. #ifdef HWAES_CAPABLE
  2594. if (HWAES_CAPABLE) {
  2595. HWAES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks);
  2596. CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
  2597. (block128_f) HWAES_encrypt);
  2598. # ifdef HWAES_ctr32_encrypt_blocks
  2599. gctx->ctr = (ctr128_f) HWAES_ctr32_encrypt_blocks;
  2600. # else
  2601. gctx->ctr = NULL;
  2602. # endif
  2603. break;
  2604. } else
  2605. #endif
  2606. #ifdef BSAES_CAPABLE
  2607. if (BSAES_CAPABLE) {
  2608. AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks);
  2609. CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
  2610. (block128_f) AES_encrypt);
  2611. gctx->ctr = (ctr128_f) bsaes_ctr32_encrypt_blocks;
  2612. break;
  2613. } else
  2614. #endif
  2615. #ifdef VPAES_CAPABLE
  2616. if (VPAES_CAPABLE) {
  2617. vpaes_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks);
  2618. CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
  2619. (block128_f) vpaes_encrypt);
  2620. gctx->ctr = NULL;
  2621. break;
  2622. } else
  2623. #endif
  2624. (void)0; /* terminate potentially open 'else' */
  2625. AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks);
  2626. CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
  2627. (block128_f) AES_encrypt);
  2628. #ifdef AES_CTR_ASM
  2629. gctx->ctr = (ctr128_f) AES_ctr32_encrypt;
  2630. #else
  2631. gctx->ctr = NULL;
  2632. #endif
  2633. } while (0);
  2634. /*
  2635. * If we have an iv can set it directly, otherwise use saved IV.
  2636. */
  2637. if (iv == NULL && gctx->iv_set)
  2638. iv = gctx->iv;
  2639. if (iv) {
  2640. CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
  2641. gctx->iv_set = 1;
  2642. }
  2643. gctx->key_set = 1;
  2644. } else {
  2645. /* If key set use IV, otherwise copy */
  2646. if (gctx->key_set)
  2647. CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
  2648. else
  2649. memcpy(gctx->iv, iv, gctx->ivlen);
  2650. gctx->iv_set = 1;
  2651. gctx->iv_gen = 0;
  2652. }
  2653. return 1;
  2654. }
  2655. /*
  2656. * Handle TLS GCM packet format. This consists of the last portion of the IV
  2657. * followed by the payload and finally the tag. On encrypt generate IV,
  2658. * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
  2659. * and verify tag.
  2660. */
  2661. static int aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  2662. const unsigned char *in, size_t len)
  2663. {
  2664. EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,ctx);
  2665. int rv = -1;
  2666. /* Encrypt/decrypt must be performed in place */
  2667. if (out != in
  2668. || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
  2669. return -1;
  2670. /*
  2671. * Set IV from start of buffer or generate IV and write to start of
  2672. * buffer.
  2673. */
  2674. if (EVP_CIPHER_CTX_ctrl(ctx, ctx->encrypt ? EVP_CTRL_GCM_IV_GEN
  2675. : EVP_CTRL_GCM_SET_IV_INV,
  2676. EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
  2677. goto err;
  2678. /* Use saved AAD */
  2679. if (CRYPTO_gcm128_aad(&gctx->gcm, ctx->buf, gctx->tls_aad_len))
  2680. goto err;
  2681. /* Fix buffer and length to point to payload */
  2682. in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  2683. out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  2684. len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
  2685. if (ctx->encrypt) {
  2686. /* Encrypt payload */
  2687. if (gctx->ctr) {
  2688. size_t bulk = 0;
  2689. #if defined(AES_GCM_ASM)
  2690. if (len >= 32 && AES_GCM_ASM(gctx)) {
  2691. if (CRYPTO_gcm128_encrypt(&gctx->gcm, NULL, NULL, 0))
  2692. return -1;
  2693. bulk = AES_gcm_encrypt(in, out, len,
  2694. gctx->gcm.key,
  2695. gctx->gcm.Yi.c, gctx->gcm.Xi.u);
  2696. gctx->gcm.len.u[1] += bulk;
  2697. }
  2698. #endif
  2699. if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
  2700. in + bulk,
  2701. out + bulk,
  2702. len - bulk, gctx->ctr))
  2703. goto err;
  2704. } else {
  2705. size_t bulk = 0;
  2706. #if defined(AES_GCM_ASM2)
  2707. if (len >= 32 && AES_GCM_ASM2(gctx)) {
  2708. if (CRYPTO_gcm128_encrypt(&gctx->gcm, NULL, NULL, 0))
  2709. return -1;
  2710. bulk = AES_gcm_encrypt(in, out, len,
  2711. gctx->gcm.key,
  2712. gctx->gcm.Yi.c, gctx->gcm.Xi.u);
  2713. gctx->gcm.len.u[1] += bulk;
  2714. }
  2715. #endif
  2716. if (CRYPTO_gcm128_encrypt(&gctx->gcm,
  2717. in + bulk, out + bulk, len - bulk))
  2718. goto err;
  2719. }
  2720. out += len;
  2721. /* Finally write tag */
  2722. CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN);
  2723. rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
  2724. } else {
  2725. /* Decrypt */
  2726. if (gctx->ctr) {
  2727. size_t bulk = 0;
  2728. #if defined(AES_GCM_ASM)
  2729. if (len >= 16 && AES_GCM_ASM(gctx)) {
  2730. if (CRYPTO_gcm128_decrypt(&gctx->gcm, NULL, NULL, 0))
  2731. return -1;
  2732. bulk = AES_gcm_decrypt(in, out, len,
  2733. gctx->gcm.key,
  2734. gctx->gcm.Yi.c, gctx->gcm.Xi.u);
  2735. gctx->gcm.len.u[1] += bulk;
  2736. }
  2737. #endif
  2738. if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
  2739. in + bulk,
  2740. out + bulk,
  2741. len - bulk, gctx->ctr))
  2742. goto err;
  2743. } else {
  2744. size_t bulk = 0;
  2745. #if defined(AES_GCM_ASM2)
  2746. if (len >= 16 && AES_GCM_ASM2(gctx)) {
  2747. if (CRYPTO_gcm128_decrypt(&gctx->gcm, NULL, NULL, 0))
  2748. return -1;
  2749. bulk = AES_gcm_decrypt(in, out, len,
  2750. gctx->gcm.key,
  2751. gctx->gcm.Yi.c, gctx->gcm.Xi.u);
  2752. gctx->gcm.len.u[1] += bulk;
  2753. }
  2754. #endif
  2755. if (CRYPTO_gcm128_decrypt(&gctx->gcm,
  2756. in + bulk, out + bulk, len - bulk))
  2757. goto err;
  2758. }
  2759. /* Retrieve tag */
  2760. CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, EVP_GCM_TLS_TAG_LEN);
  2761. /* If tag mismatch wipe buffer */
  2762. if (CRYPTO_memcmp(ctx->buf, in + len, EVP_GCM_TLS_TAG_LEN)) {
  2763. OPENSSL_cleanse(out, len);
  2764. goto err;
  2765. }
  2766. rv = len;
  2767. }
  2768. err:
  2769. gctx->iv_set = 0;
  2770. gctx->tls_aad_len = -1;
  2771. return rv;
  2772. }
  2773. static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  2774. const unsigned char *in, size_t len)
  2775. {
  2776. EVP_AES_GCM_CTX *gctx = EVP_C_DATA(EVP_AES_GCM_CTX,ctx);
  2777. /* If not set up, return error */
  2778. if (!gctx->key_set)
  2779. return -1;
  2780. if (gctx->tls_aad_len >= 0)
  2781. return aes_gcm_tls_cipher(ctx, out, in, len);
  2782. if (!gctx->iv_set)
  2783. return -1;
  2784. if (in) {
  2785. if (out == NULL) {
  2786. if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
  2787. return -1;
  2788. } else if (ctx->encrypt) {
  2789. if (gctx->ctr) {
  2790. size_t bulk = 0;
  2791. #if defined(AES_GCM_ASM)
  2792. if (len >= 32 && AES_GCM_ASM(gctx)) {
  2793. size_t res = (16 - gctx->gcm.mres) % 16;
  2794. if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, res))
  2795. return -1;
  2796. bulk = AES_gcm_encrypt(in + res,
  2797. out + res, len - res,
  2798. gctx->gcm.key, gctx->gcm.Yi.c,
  2799. gctx->gcm.Xi.u);
  2800. gctx->gcm.len.u[1] += bulk;
  2801. bulk += res;
  2802. }
  2803. #endif
  2804. if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
  2805. in + bulk,
  2806. out + bulk,
  2807. len - bulk, gctx->ctr))
  2808. return -1;
  2809. } else {
  2810. size_t bulk = 0;
  2811. #if defined(AES_GCM_ASM2)
  2812. if (len >= 32 && AES_GCM_ASM2(gctx)) {
  2813. size_t res = (16 - gctx->gcm.mres) % 16;
  2814. if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, res))
  2815. return -1;
  2816. bulk = AES_gcm_encrypt(in + res,
  2817. out + res, len - res,
  2818. gctx->gcm.key, gctx->gcm.Yi.c,
  2819. gctx->gcm.Xi.u);
  2820. gctx->gcm.len.u[1] += bulk;
  2821. bulk += res;
  2822. }
  2823. #endif
  2824. if (CRYPTO_gcm128_encrypt(&gctx->gcm,
  2825. in + bulk, out + bulk, len - bulk))
  2826. return -1;
  2827. }
  2828. } else {
  2829. if (gctx->ctr) {
  2830. size_t bulk = 0;
  2831. #if defined(AES_GCM_ASM)
  2832. if (len >= 16 && AES_GCM_ASM(gctx)) {
  2833. size_t res = (16 - gctx->gcm.mres) % 16;
  2834. if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, res))
  2835. return -1;
  2836. bulk = AES_gcm_decrypt(in + res,
  2837. out + res, len - res,
  2838. gctx->gcm.key,
  2839. gctx->gcm.Yi.c, gctx->gcm.Xi.u);
  2840. gctx->gcm.len.u[1] += bulk;
  2841. bulk += res;
  2842. }
  2843. #endif
  2844. if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
  2845. in + bulk,
  2846. out + bulk,
  2847. len - bulk, gctx->ctr))
  2848. return -1;
  2849. } else {
  2850. size_t bulk = 0;
  2851. #if defined(AES_GCM_ASM2)
  2852. if (len >= 16 && AES_GCM_ASM2(gctx)) {
  2853. size_t res = (16 - gctx->gcm.mres) % 16;
  2854. if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, res))
  2855. return -1;
  2856. bulk = AES_gcm_decrypt(in + res,
  2857. out + res, len - res,
  2858. gctx->gcm.key,
  2859. gctx->gcm.Yi.c, gctx->gcm.Xi.u);
  2860. gctx->gcm.len.u[1] += bulk;
  2861. bulk += res;
  2862. }
  2863. #endif
  2864. if (CRYPTO_gcm128_decrypt(&gctx->gcm,
  2865. in + bulk, out + bulk, len - bulk))
  2866. return -1;
  2867. }
  2868. }
  2869. return len;
  2870. } else {
  2871. if (!ctx->encrypt) {
  2872. if (gctx->taglen < 0)
  2873. return -1;
  2874. if (CRYPTO_gcm128_finish(&gctx->gcm, ctx->buf, gctx->taglen) != 0)
  2875. return -1;
  2876. gctx->iv_set = 0;
  2877. return 0;
  2878. }
  2879. CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16);
  2880. gctx->taglen = 16;
  2881. /* Don't reuse the IV */
  2882. gctx->iv_set = 0;
  2883. return 0;
  2884. }
  2885. }
  2886. #define CUSTOM_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \
  2887. | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
  2888. | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
  2889. | EVP_CIPH_CUSTOM_COPY)
  2890. BLOCK_CIPHER_custom(NID_aes, 128, 1, 12, gcm, GCM,
  2891. EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
  2892. BLOCK_CIPHER_custom(NID_aes, 192, 1, 12, gcm, GCM,
  2893. EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
  2894. BLOCK_CIPHER_custom(NID_aes, 256, 1, 12, gcm, GCM,
  2895. EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
  2896. static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
  2897. {
  2898. EVP_AES_XTS_CTX *xctx = EVP_C_DATA(EVP_AES_XTS_CTX,c);
  2899. if (type == EVP_CTRL_COPY) {
  2900. EVP_CIPHER_CTX *out = ptr;
  2901. EVP_AES_XTS_CTX *xctx_out = EVP_C_DATA(EVP_AES_XTS_CTX,out);
  2902. if (xctx->xts.key1) {
  2903. if (xctx->xts.key1 != &xctx->ks1)
  2904. return 0;
  2905. xctx_out->xts.key1 = &xctx_out->ks1;
  2906. }
  2907. if (xctx->xts.key2) {
  2908. if (xctx->xts.key2 != &xctx->ks2)
  2909. return 0;
  2910. xctx_out->xts.key2 = &xctx_out->ks2;
  2911. }
  2912. return 1;
  2913. } else if (type != EVP_CTRL_INIT)
  2914. return -1;
  2915. /* key1 and key2 are used as an indicator both key and IV are set */
  2916. xctx->xts.key1 = NULL;
  2917. xctx->xts.key2 = NULL;
  2918. return 1;
  2919. }
  2920. static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  2921. const unsigned char *iv, int enc)
  2922. {
  2923. EVP_AES_XTS_CTX *xctx = EVP_C_DATA(EVP_AES_XTS_CTX,ctx);
  2924. if (!iv && !key)
  2925. return 1;
  2926. if (key)
  2927. do {
  2928. #ifdef AES_XTS_ASM
  2929. xctx->stream = enc ? AES_xts_encrypt : AES_xts_decrypt;
  2930. #else
  2931. xctx->stream = NULL;
  2932. #endif
  2933. /* key_len is two AES keys */
  2934. #ifdef HWAES_CAPABLE
  2935. if (HWAES_CAPABLE) {
  2936. if (enc) {
  2937. HWAES_set_encrypt_key(key,
  2938. EVP_CIPHER_CTX_key_length(ctx) * 4,
  2939. &xctx->ks1.ks);
  2940. xctx->xts.block1 = (block128_f) HWAES_encrypt;
  2941. # ifdef HWAES_xts_encrypt
  2942. xctx->stream = HWAES_xts_encrypt;
  2943. # endif
  2944. } else {
  2945. HWAES_set_decrypt_key(key,
  2946. EVP_CIPHER_CTX_key_length(ctx) * 4,
  2947. &xctx->ks1.ks);
  2948. xctx->xts.block1 = (block128_f) HWAES_decrypt;
  2949. # ifdef HWAES_xts_decrypt
  2950. xctx->stream = HWAES_xts_decrypt;
  2951. #endif
  2952. }
  2953. HWAES_set_encrypt_key(key + EVP_CIPHER_CTX_key_length(ctx) / 2,
  2954. EVP_CIPHER_CTX_key_length(ctx) * 4,
  2955. &xctx->ks2.ks);
  2956. xctx->xts.block2 = (block128_f) HWAES_encrypt;
  2957. xctx->xts.key1 = &xctx->ks1;
  2958. break;
  2959. } else
  2960. #endif
  2961. #ifdef BSAES_CAPABLE
  2962. if (BSAES_CAPABLE)
  2963. xctx->stream = enc ? bsaes_xts_encrypt : bsaes_xts_decrypt;
  2964. else
  2965. #endif
  2966. #ifdef VPAES_CAPABLE
  2967. if (VPAES_CAPABLE) {
  2968. if (enc) {
  2969. vpaes_set_encrypt_key(key,
  2970. EVP_CIPHER_CTX_key_length(ctx) * 4,
  2971. &xctx->ks1.ks);
  2972. xctx->xts.block1 = (block128_f) vpaes_encrypt;
  2973. } else {
  2974. vpaes_set_decrypt_key(key,
  2975. EVP_CIPHER_CTX_key_length(ctx) * 4,
  2976. &xctx->ks1.ks);
  2977. xctx->xts.block1 = (block128_f) vpaes_decrypt;
  2978. }
  2979. vpaes_set_encrypt_key(key + EVP_CIPHER_CTX_key_length(ctx) / 2,
  2980. EVP_CIPHER_CTX_key_length(ctx) * 4,
  2981. &xctx->ks2.ks);
  2982. xctx->xts.block2 = (block128_f) vpaes_encrypt;
  2983. xctx->xts.key1 = &xctx->ks1;
  2984. break;
  2985. } else
  2986. #endif
  2987. (void)0; /* terminate potentially open 'else' */
  2988. if (enc) {
  2989. AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 4,
  2990. &xctx->ks1.ks);
  2991. xctx->xts.block1 = (block128_f) AES_encrypt;
  2992. } else {
  2993. AES_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 4,
  2994. &xctx->ks1.ks);
  2995. xctx->xts.block1 = (block128_f) AES_decrypt;
  2996. }
  2997. AES_set_encrypt_key(key + EVP_CIPHER_CTX_key_length(ctx) / 2,
  2998. EVP_CIPHER_CTX_key_length(ctx) * 4,
  2999. &xctx->ks2.ks);
  3000. xctx->xts.block2 = (block128_f) AES_encrypt;
  3001. xctx->xts.key1 = &xctx->ks1;
  3002. } while (0);
  3003. if (iv) {
  3004. xctx->xts.key2 = &xctx->ks2;
  3005. memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 16);
  3006. }
  3007. return 1;
  3008. }
  3009. static int aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  3010. const unsigned char *in, size_t len)
  3011. {
  3012. EVP_AES_XTS_CTX *xctx = EVP_C_DATA(EVP_AES_XTS_CTX,ctx);
  3013. if (!xctx->xts.key1 || !xctx->xts.key2)
  3014. return 0;
  3015. if (!out || !in || len < AES_BLOCK_SIZE)
  3016. return 0;
  3017. if (xctx->stream)
  3018. (*xctx->stream) (in, out, len,
  3019. xctx->xts.key1, xctx->xts.key2,
  3020. EVP_CIPHER_CTX_iv_noconst(ctx));
  3021. else if (CRYPTO_xts128_encrypt(&xctx->xts, EVP_CIPHER_CTX_iv_noconst(ctx),
  3022. in, out, len,
  3023. EVP_CIPHER_CTX_encrypting(ctx)))
  3024. return 0;
  3025. return 1;
  3026. }
  3027. #define aes_xts_cleanup NULL
  3028. #define XTS_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \
  3029. | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
  3030. | EVP_CIPH_CUSTOM_COPY)
  3031. BLOCK_CIPHER_custom(NID_aes, 128, 1, 16, xts, XTS, XTS_FLAGS)
  3032. BLOCK_CIPHER_custom(NID_aes, 256, 1, 16, xts, XTS, XTS_FLAGS)
  3033. static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
  3034. {
  3035. EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,c);
  3036. switch (type) {
  3037. case EVP_CTRL_INIT:
  3038. cctx->key_set = 0;
  3039. cctx->iv_set = 0;
  3040. cctx->L = 8;
  3041. cctx->M = 12;
  3042. cctx->tag_set = 0;
  3043. cctx->len_set = 0;
  3044. cctx->tls_aad_len = -1;
  3045. return 1;
  3046. case EVP_CTRL_AEAD_TLS1_AAD:
  3047. /* Save the AAD for later use */
  3048. if (arg != EVP_AEAD_TLS1_AAD_LEN)
  3049. return 0;
  3050. memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
  3051. cctx->tls_aad_len = arg;
  3052. {
  3053. uint16_t len =
  3054. EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] << 8
  3055. | EVP_CIPHER_CTX_buf_noconst(c)[arg - 1];
  3056. /* Correct length for explicit IV */
  3057. if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN)
  3058. return 0;
  3059. len -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
  3060. /* If decrypting correct for tag too */
  3061. if (!EVP_CIPHER_CTX_encrypting(c)) {
  3062. if (len < cctx->M)
  3063. return 0;
  3064. len -= cctx->M;
  3065. }
  3066. EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] = len >> 8;
  3067. EVP_CIPHER_CTX_buf_noconst(c)[arg - 1] = len & 0xff;
  3068. }
  3069. /* Extra padding: tag appended to record */
  3070. return cctx->M;
  3071. case EVP_CTRL_CCM_SET_IV_FIXED:
  3072. /* Sanity check length */
  3073. if (arg != EVP_CCM_TLS_FIXED_IV_LEN)
  3074. return 0;
  3075. /* Just copy to first part of IV */
  3076. memcpy(EVP_CIPHER_CTX_iv_noconst(c), ptr, arg);
  3077. return 1;
  3078. case EVP_CTRL_AEAD_SET_IVLEN:
  3079. arg = 15 - arg;
  3080. /* fall thru */
  3081. case EVP_CTRL_CCM_SET_L:
  3082. if (arg < 2 || arg > 8)
  3083. return 0;
  3084. cctx->L = arg;
  3085. return 1;
  3086. case EVP_CTRL_AEAD_SET_TAG:
  3087. if ((arg & 1) || arg < 4 || arg > 16)
  3088. return 0;
  3089. if (EVP_CIPHER_CTX_encrypting(c) && ptr)
  3090. return 0;
  3091. if (ptr) {
  3092. cctx->tag_set = 1;
  3093. memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
  3094. }
  3095. cctx->M = arg;
  3096. return 1;
  3097. case EVP_CTRL_AEAD_GET_TAG:
  3098. if (!EVP_CIPHER_CTX_encrypting(c) || !cctx->tag_set)
  3099. return 0;
  3100. if (!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg))
  3101. return 0;
  3102. cctx->tag_set = 0;
  3103. cctx->iv_set = 0;
  3104. cctx->len_set = 0;
  3105. return 1;
  3106. case EVP_CTRL_COPY:
  3107. {
  3108. EVP_CIPHER_CTX *out = ptr;
  3109. EVP_AES_CCM_CTX *cctx_out = EVP_C_DATA(EVP_AES_CCM_CTX,out);
  3110. if (cctx->ccm.key) {
  3111. if (cctx->ccm.key != &cctx->ks)
  3112. return 0;
  3113. cctx_out->ccm.key = &cctx_out->ks;
  3114. }
  3115. return 1;
  3116. }
  3117. default:
  3118. return -1;
  3119. }
  3120. }
  3121. static int aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  3122. const unsigned char *iv, int enc)
  3123. {
  3124. EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,ctx);
  3125. if (!iv && !key)
  3126. return 1;
  3127. if (key)
  3128. do {
  3129. #ifdef HWAES_CAPABLE
  3130. if (HWAES_CAPABLE) {
  3131. HWAES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  3132. &cctx->ks.ks);
  3133. CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
  3134. &cctx->ks, (block128_f) HWAES_encrypt);
  3135. cctx->str = NULL;
  3136. cctx->key_set = 1;
  3137. break;
  3138. } else
  3139. #endif
  3140. #ifdef VPAES_CAPABLE
  3141. if (VPAES_CAPABLE) {
  3142. vpaes_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  3143. &cctx->ks.ks);
  3144. CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
  3145. &cctx->ks, (block128_f) vpaes_encrypt);
  3146. cctx->str = NULL;
  3147. cctx->key_set = 1;
  3148. break;
  3149. }
  3150. #endif
  3151. AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  3152. &cctx->ks.ks);
  3153. CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
  3154. &cctx->ks, (block128_f) AES_encrypt);
  3155. cctx->str = NULL;
  3156. cctx->key_set = 1;
  3157. } while (0);
  3158. if (iv) {
  3159. memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 15 - cctx->L);
  3160. cctx->iv_set = 1;
  3161. }
  3162. return 1;
  3163. }
  3164. static int aes_ccm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  3165. const unsigned char *in, size_t len)
  3166. {
  3167. EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,ctx);
  3168. CCM128_CONTEXT *ccm = &cctx->ccm;
  3169. /* Encrypt/decrypt must be performed in place */
  3170. if (out != in || len < (EVP_CCM_TLS_EXPLICIT_IV_LEN + (size_t)cctx->M))
  3171. return -1;
  3172. /* If encrypting set explicit IV from sequence number (start of AAD) */
  3173. if (EVP_CIPHER_CTX_encrypting(ctx))
  3174. memcpy(out, EVP_CIPHER_CTX_buf_noconst(ctx),
  3175. EVP_CCM_TLS_EXPLICIT_IV_LEN);
  3176. /* Get rest of IV from explicit IV */
  3177. memcpy(EVP_CIPHER_CTX_iv_noconst(ctx) + EVP_CCM_TLS_FIXED_IV_LEN, in,
  3178. EVP_CCM_TLS_EXPLICIT_IV_LEN);
  3179. /* Correct length value */
  3180. len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M;
  3181. if (CRYPTO_ccm128_setiv(ccm, EVP_CIPHER_CTX_iv_noconst(ctx), 15 - cctx->L,
  3182. len))
  3183. return -1;
  3184. /* Use saved AAD */
  3185. CRYPTO_ccm128_aad(ccm, EVP_CIPHER_CTX_buf_noconst(ctx), cctx->tls_aad_len);
  3186. /* Fix buffer to point to payload */
  3187. in += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  3188. out += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  3189. if (EVP_CIPHER_CTX_encrypting(ctx)) {
  3190. if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len,
  3191. cctx->str) :
  3192. CRYPTO_ccm128_encrypt(ccm, in, out, len))
  3193. return -1;
  3194. if (!CRYPTO_ccm128_tag(ccm, out + len, cctx->M))
  3195. return -1;
  3196. return len + EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M;
  3197. } else {
  3198. if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
  3199. cctx->str) :
  3200. !CRYPTO_ccm128_decrypt(ccm, in, out, len)) {
  3201. unsigned char tag[16];
  3202. if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) {
  3203. if (!CRYPTO_memcmp(tag, in + len, cctx->M))
  3204. return len;
  3205. }
  3206. }
  3207. OPENSSL_cleanse(out, len);
  3208. return -1;
  3209. }
  3210. }
  3211. static int aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  3212. const unsigned char *in, size_t len)
  3213. {
  3214. EVP_AES_CCM_CTX *cctx = EVP_C_DATA(EVP_AES_CCM_CTX,ctx);
  3215. CCM128_CONTEXT *ccm = &cctx->ccm;
  3216. /* If not set up, return error */
  3217. if (!cctx->key_set)
  3218. return -1;
  3219. if (cctx->tls_aad_len >= 0)
  3220. return aes_ccm_tls_cipher(ctx, out, in, len);
  3221. /* EVP_*Final() doesn't return any data */
  3222. if (in == NULL && out != NULL)
  3223. return 0;
  3224. if (!cctx->iv_set)
  3225. return -1;
  3226. if (!EVP_CIPHER_CTX_encrypting(ctx) && !cctx->tag_set)
  3227. return -1;
  3228. if (!out) {
  3229. if (!in) {
  3230. if (CRYPTO_ccm128_setiv(ccm, EVP_CIPHER_CTX_iv_noconst(ctx),
  3231. 15 - cctx->L, len))
  3232. return -1;
  3233. cctx->len_set = 1;
  3234. return len;
  3235. }
  3236. /* If have AAD need message length */
  3237. if (!cctx->len_set && len)
  3238. return -1;
  3239. CRYPTO_ccm128_aad(ccm, in, len);
  3240. return len;
  3241. }
  3242. /* If not set length yet do it */
  3243. if (!cctx->len_set) {
  3244. if (CRYPTO_ccm128_setiv(ccm, EVP_CIPHER_CTX_iv_noconst(ctx),
  3245. 15 - cctx->L, len))
  3246. return -1;
  3247. cctx->len_set = 1;
  3248. }
  3249. if (EVP_CIPHER_CTX_encrypting(ctx)) {
  3250. if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len,
  3251. cctx->str) :
  3252. CRYPTO_ccm128_encrypt(ccm, in, out, len))
  3253. return -1;
  3254. cctx->tag_set = 1;
  3255. return len;
  3256. } else {
  3257. int rv = -1;
  3258. if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
  3259. cctx->str) :
  3260. !CRYPTO_ccm128_decrypt(ccm, in, out, len)) {
  3261. unsigned char tag[16];
  3262. if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) {
  3263. if (!CRYPTO_memcmp(tag, EVP_CIPHER_CTX_buf_noconst(ctx),
  3264. cctx->M))
  3265. rv = len;
  3266. }
  3267. }
  3268. if (rv == -1)
  3269. OPENSSL_cleanse(out, len);
  3270. cctx->iv_set = 0;
  3271. cctx->tag_set = 0;
  3272. cctx->len_set = 0;
  3273. return rv;
  3274. }
  3275. }
  3276. #define aes_ccm_cleanup NULL
  3277. BLOCK_CIPHER_custom(NID_aes, 128, 1, 12, ccm, CCM,
  3278. EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
  3279. BLOCK_CIPHER_custom(NID_aes, 192, 1, 12, ccm, CCM,
  3280. EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
  3281. BLOCK_CIPHER_custom(NID_aes, 256, 1, 12, ccm, CCM,
  3282. EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
  3283. typedef struct {
  3284. union {
  3285. double align;
  3286. AES_KEY ks;
  3287. } ks;
  3288. /* Indicates if IV has been set */
  3289. unsigned char *iv;
  3290. } EVP_AES_WRAP_CTX;
  3291. static int aes_wrap_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  3292. const unsigned char *iv, int enc)
  3293. {
  3294. EVP_AES_WRAP_CTX *wctx = EVP_C_DATA(EVP_AES_WRAP_CTX,ctx);
  3295. if (!iv && !key)
  3296. return 1;
  3297. if (key) {
  3298. if (EVP_CIPHER_CTX_encrypting(ctx))
  3299. AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  3300. &wctx->ks.ks);
  3301. else
  3302. AES_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  3303. &wctx->ks.ks);
  3304. if (!iv)
  3305. wctx->iv = NULL;
  3306. }
  3307. if (iv) {
  3308. memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, EVP_CIPHER_CTX_iv_length(ctx));
  3309. wctx->iv = EVP_CIPHER_CTX_iv_noconst(ctx);
  3310. }
  3311. return 1;
  3312. }
  3313. static int aes_wrap_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  3314. const unsigned char *in, size_t inlen)
  3315. {
  3316. EVP_AES_WRAP_CTX *wctx = EVP_C_DATA(EVP_AES_WRAP_CTX,ctx);
  3317. size_t rv;
  3318. /* AES wrap with padding has IV length of 4, without padding 8 */
  3319. int pad = EVP_CIPHER_CTX_iv_length(ctx) == 4;
  3320. /* No final operation so always return zero length */
  3321. if (!in)
  3322. return 0;
  3323. /* Input length must always be non-zero */
  3324. if (!inlen)
  3325. return -1;
  3326. /* If decrypting need at least 16 bytes and multiple of 8 */
  3327. if (!EVP_CIPHER_CTX_encrypting(ctx) && (inlen < 16 || inlen & 0x7))
  3328. return -1;
  3329. /* If not padding input must be multiple of 8 */
  3330. if (!pad && inlen & 0x7)
  3331. return -1;
  3332. if (is_partially_overlapping(out, in, inlen)) {
  3333. EVPerr(EVP_F_AES_WRAP_CIPHER, EVP_R_PARTIALLY_OVERLAPPING);
  3334. return 0;
  3335. }
  3336. if (!out) {
  3337. if (EVP_CIPHER_CTX_encrypting(ctx)) {
  3338. /* If padding round up to multiple of 8 */
  3339. if (pad)
  3340. inlen = (inlen + 7) / 8 * 8;
  3341. /* 8 byte prefix */
  3342. return inlen + 8;
  3343. } else {
  3344. /*
  3345. * If not padding output will be exactly 8 bytes smaller than
  3346. * input. If padding it will be at least 8 bytes smaller but we
  3347. * don't know how much.
  3348. */
  3349. return inlen - 8;
  3350. }
  3351. }
  3352. if (pad) {
  3353. if (EVP_CIPHER_CTX_encrypting(ctx))
  3354. rv = CRYPTO_128_wrap_pad(&wctx->ks.ks, wctx->iv,
  3355. out, in, inlen,
  3356. (block128_f) AES_encrypt);
  3357. else
  3358. rv = CRYPTO_128_unwrap_pad(&wctx->ks.ks, wctx->iv,
  3359. out, in, inlen,
  3360. (block128_f) AES_decrypt);
  3361. } else {
  3362. if (EVP_CIPHER_CTX_encrypting(ctx))
  3363. rv = CRYPTO_128_wrap(&wctx->ks.ks, wctx->iv,
  3364. out, in, inlen, (block128_f) AES_encrypt);
  3365. else
  3366. rv = CRYPTO_128_unwrap(&wctx->ks.ks, wctx->iv,
  3367. out, in, inlen, (block128_f) AES_decrypt);
  3368. }
  3369. return rv ? (int)rv : -1;
  3370. }
  3371. #define WRAP_FLAGS (EVP_CIPH_WRAP_MODE \
  3372. | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
  3373. | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_FLAG_DEFAULT_ASN1)
  3374. static const EVP_CIPHER aes_128_wrap = {
  3375. NID_id_aes128_wrap,
  3376. 8, 16, 8, WRAP_FLAGS,
  3377. aes_wrap_init_key, aes_wrap_cipher,
  3378. NULL,
  3379. sizeof(EVP_AES_WRAP_CTX),
  3380. NULL, NULL, NULL, NULL
  3381. };
  3382. const EVP_CIPHER *EVP_aes_128_wrap(void)
  3383. {
  3384. return &aes_128_wrap;
  3385. }
  3386. static const EVP_CIPHER aes_192_wrap = {
  3387. NID_id_aes192_wrap,
  3388. 8, 24, 8, WRAP_FLAGS,
  3389. aes_wrap_init_key, aes_wrap_cipher,
  3390. NULL,
  3391. sizeof(EVP_AES_WRAP_CTX),
  3392. NULL, NULL, NULL, NULL
  3393. };
  3394. const EVP_CIPHER *EVP_aes_192_wrap(void)
  3395. {
  3396. return &aes_192_wrap;
  3397. }
  3398. static const EVP_CIPHER aes_256_wrap = {
  3399. NID_id_aes256_wrap,
  3400. 8, 32, 8, WRAP_FLAGS,
  3401. aes_wrap_init_key, aes_wrap_cipher,
  3402. NULL,
  3403. sizeof(EVP_AES_WRAP_CTX),
  3404. NULL, NULL, NULL, NULL
  3405. };
  3406. const EVP_CIPHER *EVP_aes_256_wrap(void)
  3407. {
  3408. return &aes_256_wrap;
  3409. }
  3410. static const EVP_CIPHER aes_128_wrap_pad = {
  3411. NID_id_aes128_wrap_pad,
  3412. 8, 16, 4, WRAP_FLAGS,
  3413. aes_wrap_init_key, aes_wrap_cipher,
  3414. NULL,
  3415. sizeof(EVP_AES_WRAP_CTX),
  3416. NULL, NULL, NULL, NULL
  3417. };
  3418. const EVP_CIPHER *EVP_aes_128_wrap_pad(void)
  3419. {
  3420. return &aes_128_wrap_pad;
  3421. }
  3422. static const EVP_CIPHER aes_192_wrap_pad = {
  3423. NID_id_aes192_wrap_pad,
  3424. 8, 24, 4, WRAP_FLAGS,
  3425. aes_wrap_init_key, aes_wrap_cipher,
  3426. NULL,
  3427. sizeof(EVP_AES_WRAP_CTX),
  3428. NULL, NULL, NULL, NULL
  3429. };
  3430. const EVP_CIPHER *EVP_aes_192_wrap_pad(void)
  3431. {
  3432. return &aes_192_wrap_pad;
  3433. }
  3434. static const EVP_CIPHER aes_256_wrap_pad = {
  3435. NID_id_aes256_wrap_pad,
  3436. 8, 32, 4, WRAP_FLAGS,
  3437. aes_wrap_init_key, aes_wrap_cipher,
  3438. NULL,
  3439. sizeof(EVP_AES_WRAP_CTX),
  3440. NULL, NULL, NULL, NULL
  3441. };
  3442. const EVP_CIPHER *EVP_aes_256_wrap_pad(void)
  3443. {
  3444. return &aes_256_wrap_pad;
  3445. }
  3446. #ifndef OPENSSL_NO_OCB
  3447. static int aes_ocb_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
  3448. {
  3449. EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,c);
  3450. EVP_CIPHER_CTX *newc;
  3451. EVP_AES_OCB_CTX *new_octx;
  3452. switch (type) {
  3453. case EVP_CTRL_INIT:
  3454. octx->key_set = 0;
  3455. octx->iv_set = 0;
  3456. octx->ivlen = EVP_CIPHER_CTX_iv_length(c);
  3457. octx->iv = EVP_CIPHER_CTX_iv_noconst(c);
  3458. octx->taglen = 16;
  3459. octx->data_buf_len = 0;
  3460. octx->aad_buf_len = 0;
  3461. return 1;
  3462. case EVP_CTRL_AEAD_SET_IVLEN:
  3463. /* IV len must be 1 to 15 */
  3464. if (arg <= 0 || arg > 15)
  3465. return 0;
  3466. octx->ivlen = arg;
  3467. return 1;
  3468. case EVP_CTRL_AEAD_SET_TAG:
  3469. if (!ptr) {
  3470. /* Tag len must be 0 to 16 */
  3471. if (arg < 0 || arg > 16)
  3472. return 0;
  3473. octx->taglen = arg;
  3474. return 1;
  3475. }
  3476. if (arg != octx->taglen || EVP_CIPHER_CTX_encrypting(c))
  3477. return 0;
  3478. memcpy(octx->tag, ptr, arg);
  3479. return 1;
  3480. case EVP_CTRL_AEAD_GET_TAG:
  3481. if (arg != octx->taglen || !EVP_CIPHER_CTX_encrypting(c))
  3482. return 0;
  3483. memcpy(ptr, octx->tag, arg);
  3484. return 1;
  3485. case EVP_CTRL_COPY:
  3486. newc = (EVP_CIPHER_CTX *)ptr;
  3487. new_octx = EVP_C_DATA(EVP_AES_OCB_CTX,newc);
  3488. return CRYPTO_ocb128_copy_ctx(&new_octx->ocb, &octx->ocb,
  3489. &new_octx->ksenc.ks,
  3490. &new_octx->ksdec.ks);
  3491. default:
  3492. return -1;
  3493. }
  3494. }
  3495. # ifdef HWAES_CAPABLE
  3496. # ifdef HWAES_ocb_encrypt
  3497. void HWAES_ocb_encrypt(const unsigned char *in, unsigned char *out,
  3498. size_t blocks, const void *key,
  3499. size_t start_block_num,
  3500. unsigned char offset_i[16],
  3501. const unsigned char L_[][16],
  3502. unsigned char checksum[16]);
  3503. # else
  3504. # define HWAES_ocb_encrypt ((ocb128_f)NULL)
  3505. # endif
  3506. # ifdef HWAES_ocb_decrypt
  3507. void HWAES_ocb_decrypt(const unsigned char *in, unsigned char *out,
  3508. size_t blocks, const void *key,
  3509. size_t start_block_num,
  3510. unsigned char offset_i[16],
  3511. const unsigned char L_[][16],
  3512. unsigned char checksum[16]);
  3513. # else
  3514. # define HWAES_ocb_decrypt ((ocb128_f)NULL)
  3515. # endif
  3516. # endif
  3517. static int aes_ocb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  3518. const unsigned char *iv, int enc)
  3519. {
  3520. EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,ctx);
  3521. if (!iv && !key)
  3522. return 1;
  3523. if (key) {
  3524. do {
  3525. /*
  3526. * We set both the encrypt and decrypt key here because decrypt
  3527. * needs both. We could possibly optimise to remove setting the
  3528. * decrypt for an encryption operation.
  3529. */
  3530. # ifdef HWAES_CAPABLE
  3531. if (HWAES_CAPABLE) {
  3532. HWAES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  3533. &octx->ksenc.ks);
  3534. HWAES_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  3535. &octx->ksdec.ks);
  3536. if (!CRYPTO_ocb128_init(&octx->ocb,
  3537. &octx->ksenc.ks, &octx->ksdec.ks,
  3538. (block128_f) HWAES_encrypt,
  3539. (block128_f) HWAES_decrypt,
  3540. enc ? HWAES_ocb_encrypt
  3541. : HWAES_ocb_decrypt))
  3542. return 0;
  3543. break;
  3544. }
  3545. # endif
  3546. # ifdef VPAES_CAPABLE
  3547. if (VPAES_CAPABLE) {
  3548. vpaes_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  3549. &octx->ksenc.ks);
  3550. vpaes_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  3551. &octx->ksdec.ks);
  3552. if (!CRYPTO_ocb128_init(&octx->ocb,
  3553. &octx->ksenc.ks, &octx->ksdec.ks,
  3554. (block128_f) vpaes_encrypt,
  3555. (block128_f) vpaes_decrypt,
  3556. NULL))
  3557. return 0;
  3558. break;
  3559. }
  3560. # endif
  3561. AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  3562. &octx->ksenc.ks);
  3563. AES_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
  3564. &octx->ksdec.ks);
  3565. if (!CRYPTO_ocb128_init(&octx->ocb,
  3566. &octx->ksenc.ks, &octx->ksdec.ks,
  3567. (block128_f) AES_encrypt,
  3568. (block128_f) AES_decrypt,
  3569. NULL))
  3570. return 0;
  3571. }
  3572. while (0);
  3573. /*
  3574. * If we have an iv we can set it directly, otherwise use saved IV.
  3575. */
  3576. if (iv == NULL && octx->iv_set)
  3577. iv = octx->iv;
  3578. if (iv) {
  3579. if (CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen)
  3580. != 1)
  3581. return 0;
  3582. octx->iv_set = 1;
  3583. }
  3584. octx->key_set = 1;
  3585. } else {
  3586. /* If key set use IV, otherwise copy */
  3587. if (octx->key_set)
  3588. CRYPTO_ocb128_setiv(&octx->ocb, iv, octx->ivlen, octx->taglen);
  3589. else
  3590. memcpy(octx->iv, iv, octx->ivlen);
  3591. octx->iv_set = 1;
  3592. }
  3593. return 1;
  3594. }
  3595. static int aes_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  3596. const unsigned char *in, size_t len)
  3597. {
  3598. unsigned char *buf;
  3599. int *buf_len;
  3600. int written_len = 0;
  3601. size_t trailing_len;
  3602. EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,ctx);
  3603. /* If IV or Key not set then return error */
  3604. if (!octx->iv_set)
  3605. return -1;
  3606. if (!octx->key_set)
  3607. return -1;
  3608. if (in != NULL) {
  3609. /*
  3610. * Need to ensure we are only passing full blocks to low level OCB
  3611. * routines. We do it here rather than in EVP_EncryptUpdate/
  3612. * EVP_DecryptUpdate because we need to pass full blocks of AAD too
  3613. * and those routines don't support that
  3614. */
  3615. /* Are we dealing with AAD or normal data here? */
  3616. if (out == NULL) {
  3617. buf = octx->aad_buf;
  3618. buf_len = &(octx->aad_buf_len);
  3619. } else {
  3620. buf = octx->data_buf;
  3621. buf_len = &(octx->data_buf_len);
  3622. if (is_partially_overlapping(out + *buf_len, in, len)) {
  3623. EVPerr(EVP_F_AES_OCB_CIPHER, EVP_R_PARTIALLY_OVERLAPPING);
  3624. return 0;
  3625. }
  3626. }
  3627. /*
  3628. * If we've got a partially filled buffer from a previous call then
  3629. * use that data first
  3630. */
  3631. if (*buf_len > 0) {
  3632. unsigned int remaining;
  3633. remaining = AES_BLOCK_SIZE - (*buf_len);
  3634. if (remaining > len) {
  3635. memcpy(buf + (*buf_len), in, len);
  3636. *(buf_len) += len;
  3637. return 0;
  3638. }
  3639. memcpy(buf + (*buf_len), in, remaining);
  3640. /*
  3641. * If we get here we've filled the buffer, so process it
  3642. */
  3643. len -= remaining;
  3644. in += remaining;
  3645. if (out == NULL) {
  3646. if (!CRYPTO_ocb128_aad(&octx->ocb, buf, AES_BLOCK_SIZE))
  3647. return -1;
  3648. } else if (EVP_CIPHER_CTX_encrypting(ctx)) {
  3649. if (!CRYPTO_ocb128_encrypt(&octx->ocb, buf, out,
  3650. AES_BLOCK_SIZE))
  3651. return -1;
  3652. } else {
  3653. if (!CRYPTO_ocb128_decrypt(&octx->ocb, buf, out,
  3654. AES_BLOCK_SIZE))
  3655. return -1;
  3656. }
  3657. written_len = AES_BLOCK_SIZE;
  3658. *buf_len = 0;
  3659. if (out != NULL)
  3660. out += AES_BLOCK_SIZE;
  3661. }
  3662. /* Do we have a partial block to handle at the end? */
  3663. trailing_len = len % AES_BLOCK_SIZE;
  3664. /*
  3665. * If we've got some full blocks to handle, then process these first
  3666. */
  3667. if (len != trailing_len) {
  3668. if (out == NULL) {
  3669. if (!CRYPTO_ocb128_aad(&octx->ocb, in, len - trailing_len))
  3670. return -1;
  3671. } else if (EVP_CIPHER_CTX_encrypting(ctx)) {
  3672. if (!CRYPTO_ocb128_encrypt
  3673. (&octx->ocb, in, out, len - trailing_len))
  3674. return -1;
  3675. } else {
  3676. if (!CRYPTO_ocb128_decrypt
  3677. (&octx->ocb, in, out, len - trailing_len))
  3678. return -1;
  3679. }
  3680. written_len += len - trailing_len;
  3681. in += len - trailing_len;
  3682. }
  3683. /* Handle any trailing partial block */
  3684. if (trailing_len > 0) {
  3685. memcpy(buf, in, trailing_len);
  3686. *buf_len = trailing_len;
  3687. }
  3688. return written_len;
  3689. } else {
  3690. /*
  3691. * First of all empty the buffer of any partial block that we might
  3692. * have been provided - both for data and AAD
  3693. */
  3694. if (octx->data_buf_len > 0) {
  3695. if (EVP_CIPHER_CTX_encrypting(ctx)) {
  3696. if (!CRYPTO_ocb128_encrypt(&octx->ocb, octx->data_buf, out,
  3697. octx->data_buf_len))
  3698. return -1;
  3699. } else {
  3700. if (!CRYPTO_ocb128_decrypt(&octx->ocb, octx->data_buf, out,
  3701. octx->data_buf_len))
  3702. return -1;
  3703. }
  3704. written_len = octx->data_buf_len;
  3705. octx->data_buf_len = 0;
  3706. }
  3707. if (octx->aad_buf_len > 0) {
  3708. if (!CRYPTO_ocb128_aad
  3709. (&octx->ocb, octx->aad_buf, octx->aad_buf_len))
  3710. return -1;
  3711. octx->aad_buf_len = 0;
  3712. }
  3713. /* If decrypting then verify */
  3714. if (!EVP_CIPHER_CTX_encrypting(ctx)) {
  3715. if (octx->taglen < 0)
  3716. return -1;
  3717. if (CRYPTO_ocb128_finish(&octx->ocb,
  3718. octx->tag, octx->taglen) != 0)
  3719. return -1;
  3720. octx->iv_set = 0;
  3721. return written_len;
  3722. }
  3723. /* If encrypting then just get the tag */
  3724. if (CRYPTO_ocb128_tag(&octx->ocb, octx->tag, 16) != 1)
  3725. return -1;
  3726. /* Don't reuse the IV */
  3727. octx->iv_set = 0;
  3728. return written_len;
  3729. }
  3730. }
  3731. static int aes_ocb_cleanup(EVP_CIPHER_CTX *c)
  3732. {
  3733. EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,c);
  3734. CRYPTO_ocb128_cleanup(&octx->ocb);
  3735. return 1;
  3736. }
  3737. BLOCK_CIPHER_custom(NID_aes, 128, 16, 12, ocb, OCB,
  3738. EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
  3739. BLOCK_CIPHER_custom(NID_aes, 192, 16, 12, ocb, OCB,
  3740. EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
  3741. BLOCK_CIPHER_custom(NID_aes, 256, 16, 12, ocb, OCB,
  3742. EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS)
  3743. #endif /* OPENSSL_NO_OCB */