e_aes.c 143 KB

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