e_aes.c 130 KB

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