2
0

e_aes.c 142 KB

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