e_aes.c 143 KB

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