e_aes.c 131 KB

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