| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215 |
- // SPDX-License-Identifier: GPL-2.0
- /*
- * AM6 SA2UL crypto accelerator driver
- *
- * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com
- *
- * Authors: Keerthy
- * Vitaly Andrianov
- */
- #include <linux/clk.h>
- #include <linux/module.h>
- #include <linux/dmapool.h>
- #include <linux/platform_device.h>
- #include <linux/pm_runtime.h>
- #include <linux/dmaengine.h>
- #include <linux/cryptohash.h>
- #include <linux/mod_devicetable.h>
- #include <crypto/authenc.h>
- #include <crypto/des.h>
- #include <crypto/internal/aead.h>
- #include <crypto/internal/skcipher.h>
- #include <crypto/internal/hash.h>
- #include <crypto/scatterwalk.h>
- #include <crypto/sha.h>
- #include "sa2ul.h"
- /* Byte offset for key in encryption security context */
- #define SC_ENC_KEY_OFFSET (1 + 27 + 4)
- /* Byte offset for Aux-1 in encryption security context */
- #define SC_ENC_AUX1_OFFSET (1 + 27 + 4 + 32)
- #define SA_CMDL_UPD_ENC 0x0001
- #define SA_CMDL_UPD_AUTH 0x0002
- #define SA_CMDL_UPD_ENC_IV 0x0004
- #define SA_CMDL_UPD_AUTH_IV 0x0008
- #define SA_CMDL_UPD_AUX_KEY 0x0010
- #define SA_AUTH_SUBKEY_LEN 16
- #define SA_CMDL_PAYLOAD_LENGTH_MASK 0xFFFF
- #define SA_CMDL_SOP_BYPASS_LEN_MASK 0xFF000000
- #define MODE_CONTROL_BYTES 27
- #define SA_HASH_PROCESSING 0
- #define SA_CRYPTO_PROCESSING 0
- #define SA_UPLOAD_HASH_TO_TLR BIT(6)
- #define SA_SW0_FLAGS_MASK 0xF0000
- #define SA_SW0_CMDL_INFO_MASK 0x1F00000
- #define SA_SW0_CMDL_PRESENT BIT(4)
- #define SA_SW0_ENG_ID_MASK 0x3E000000
- #define SA_SW0_DEST_INFO_PRESENT BIT(30)
- #define SA_SW2_EGRESS_LENGTH 0xFF000000
- #define SHA256_DIGEST_WORDS 8
- /* Make 32-bit word from 4 bytes */
- #define SA_MK_U32(b0, b1, b2, b3) (((b0) << 24) | ((b1) << 16) | \
- ((b2) << 8) | (b3))
- /* size of SCCTL structure in bytes */
- #define SA_SCCTL_SZ 16
- /* Max Authentication tag size */
- #define SA_MAX_AUTH_TAG_SZ 64
- static struct device *sa_k3_dev;
- /**
- * struct sa_cmdl_cfg - Command label configuration descriptor
- * @enc1st: If the iteration needs encryption before authentication
- * @aalg: authentication algorithm ID
- * @enc_eng_id: Encryption Engine ID supported by the SA hardware
- * @auth_eng_id: authentication Engine ID
- * @iv_size: Initialization Vector size
- * @akey: Authentication key
- * @akey_len: Authentication key length
- */
- struct sa_cmdl_cfg {
- int enc1st;
- int aalg;
- u8 enc_eng_id;
- u8 auth_eng_id;
- u8 iv_size;
- const u8 *akey;
- u16 akey_len;
- u16 auth_subkey_len;
- };
- /**
- * struct algo_data - Crypto algorithm specific data
- * @enc_eng: Encryption engine info structure
- * @auth_eng: Authentication engine info structure
- * @auth_ctrl: Authentication control word
- * @hash_size: Size of Digest
- * @ealg_id: Encryption Algorithm ID
- * @aalg_id: Authentication algorithm ID
- * @mci_enc: Mode Control Instruction for Encryption algorithm
- * @mci_dec: Mode Control Instruction for Decryption
- * @inv_key: Whether the encryption algorithm demands key inversion
- * @keyed_mac: Whether the Authentication algorithm has Key
- * @prep_iopad: Function pointer to generate intermediate ipad/opad
- */
- struct algo_data {
- struct sa_eng_info enc_eng;
- struct sa_eng_info auth_eng;
- u8 auth_ctrl;
- u8 hash_size;
- u8 ealg_id;
- u8 aalg_id;
- u8 *mci_enc;
- u8 *mci_dec;
- bool inv_key;
- bool keyed_mac;
- void (*prep_iopad)(const u8 *key, u16 key_sz, u32 *ipad, u32 *opad);
- };
- /**
- * struct sa_alg_tmpl: A generic template encompassing crypto/aead algorithms
- * @alg: A union of aead/crypto algorithm type.
- * @registered: Flag indicating if the crypto algorithm is already registered
- */
- struct sa_alg_tmpl {
- u32 type; /* CRYPTO_ALG_TYPE from <linux/crypto.h> */
- union {
- struct crypto_alg crypto;
- struct aead_alg aead;
- } alg;
- int registered;
- };
- /**
- * struct sa_rx_data: RX Packet miscellaneous data place holder
- * @req: crypto request data pointer
- * @tx_in: dma_async_tx_descriptor pointer for rx channel
- * @enc: Flag indicating either encryption or decryption
- */
- struct sa_rx_data {
- void *req;
- struct dma_async_tx_descriptor *tx_in;
- u8 enc;
- };
- /*
- * Mode Control Instructions for various Key lengths 128, 192, 256
- * For CBC (Cipher Block Chaining) mode for encryption
- */
- static u8 mci_cbc_enc_array[3][MODE_CONTROL_BYTES] = {
- { 0x21, 0x00, 0x00, 0x18, 0x88, 0x0a, 0xaa, 0x4b, 0x7e, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
- { 0x21, 0x00, 0x00, 0x18, 0x88, 0x4a, 0xaa, 0x4b, 0x7e, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
- { 0x21, 0x00, 0x00, 0x18, 0x88, 0x8a, 0xaa, 0x4b, 0x7e, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
- };
- /*
- * Mode Control Instructions for various Key lengths 128, 192, 256
- * For CBC (Cipher Block Chaining) mode for decryption
- */
- static u8 mci_cbc_dec_array[3][MODE_CONTROL_BYTES] = {
- { 0x31, 0x00, 0x00, 0x80, 0x8a, 0xca, 0x98, 0xf4, 0x40, 0xc0,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
- { 0x31, 0x00, 0x00, 0x84, 0x8a, 0xca, 0x98, 0xf4, 0x40, 0xc0,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
- { 0x31, 0x00, 0x00, 0x88, 0x8a, 0xca, 0x98, 0xf4, 0x40, 0xc0,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
- };
- /*
- * Mode Control Instructions for various Key lengths 128, 192, 256
- * For ECB (Electronic Code Book) mode for encryption
- */
- static u8 mci_ecb_enc_array[3][27] = {
- { 0x21, 0x00, 0x00, 0x80, 0x8a, 0x04, 0xb7, 0x90, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
- { 0x21, 0x00, 0x00, 0x84, 0x8a, 0x04, 0xb7, 0x90, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
- { 0x21, 0x00, 0x00, 0x88, 0x8a, 0x04, 0xb7, 0x90, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
- };
- /*
- * Mode Control Instructions for various Key lengths 128, 192, 256
- * For ECB (Electronic Code Book) mode for decryption
- */
- static u8 mci_ecb_dec_array[3][27] = {
- { 0x31, 0x00, 0x00, 0x80, 0x8a, 0x04, 0xb7, 0x90, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
- { 0x31, 0x00, 0x00, 0x84, 0x8a, 0x04, 0xb7, 0x90, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
- { 0x31, 0x00, 0x00, 0x88, 0x8a, 0x04, 0xb7, 0x90, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
- };
- /*
- * Mode Control Instructions for DES algorithm
- * For CBC (Cipher Block Chaining) mode and ECB mode
- * encryption and for decryption respectively
- */
- static u8 mci_cbc_3des_enc_array[MODE_CONTROL_BYTES] = {
- 0x20, 0x00, 0x00, 0x18, 0x88, 0x52, 0xaa, 0x4b, 0x7e, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00,
- };
- static u8 mci_cbc_3des_dec_array[MODE_CONTROL_BYTES] = {
- 0x30, 0x00, 0x00, 0x85, 0x0a, 0xca, 0x98, 0xf4, 0x40, 0xc0, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00,
- };
- static u8 mci_ecb_3des_enc_array[MODE_CONTROL_BYTES] = {
- 0x20, 0x00, 0x00, 0x85, 0x0a, 0x04, 0xb7, 0x90, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00,
- };
- static u8 mci_ecb_3des_dec_array[MODE_CONTROL_BYTES] = {
- 0x30, 0x00, 0x00, 0x85, 0x0a, 0x04, 0xb7, 0x90, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00,
- };
- /*
- * Perform 16 byte or 128 bit swizzling
- * The SA2UL Expects the security context to
- * be in little Endian and the bus width is 128 bits or 16 bytes
- * Hence swap 16 bytes at a time from higher to lower address
- */
- static void sa_swiz_128(u8 *in, u16 len)
- {
- u8 data[16];
- int i, j;
- for (i = 0; i < len; i += 16) {
- memcpy(data, &in[i], 16);
- for (j = 0; j < 16; j++)
- in[i + j] = data[15 - j];
- }
- }
- /* Prepare the ipad and opad from key as per SHA algorithm step 1*/
- static void prepare_kiopad(u8 *k_ipad, u8 *k_opad, const u8 *key, u16 key_sz)
- {
- int i;
- for (i = 0; i < key_sz; i++) {
- k_ipad[i] = key[i] ^ 0x36;
- k_opad[i] = key[i] ^ 0x5c;
- }
- /* Instead of XOR with 0 */
- for (; i < SHA_MESSAGE_BYTES; i++) {
- k_ipad[i] = 0x36;
- k_opad[i] = 0x5c;
- }
- }
- /* Generate HMAC-SHA1 intermediate Hash */
- static
- void sa_hmac_sha1_get_pad(const u8 *key, u16 key_sz, u32 *ipad, u32 *opad)
- {
- u32 ws[SHA_WORKSPACE_WORDS];
- u8 k_ipad[SHA_MESSAGE_BYTES];
- u8 k_opad[SHA_MESSAGE_BYTES];
- int i;
- prepare_kiopad(k_ipad, k_opad, key, key_sz);
- /* SHA-1 on k_ipad */
- sha_init(ipad);
- sha_transform(ipad, k_ipad, ws);
- for (i = 0; i < SHA_DIGEST_WORDS; i++)
- ipad[i] = cpu_to_be32(ipad[i]);
- /* SHA-1 on k_opad */
- sha_init(opad);
- sha_transform(opad, k_opad, ws);
- for (i = 0; i < SHA_DIGEST_WORDS; i++)
- opad[i] = cpu_to_be32(opad[i]);
- }
- void sha256_init(u32 *buf)
- {
- buf[0] = SHA256_H0;
- buf[1] = SHA256_H1;
- buf[2] = SHA256_H2;
- buf[3] = SHA256_H3;
- buf[4] = SHA256_H4;
- buf[5] = SHA256_H5;
- buf[6] = SHA256_H6;
- buf[7] = SHA256_H7;
- }
- static void sa_hmac_sha256_get_pad(const u8 *key, u16 key_sz, u32 *ipad,
- u32 *opad)
- {
- u8 k_ipad[SHA_MESSAGE_BYTES];
- u8 k_opad[SHA_MESSAGE_BYTES];
- int i;
- prepare_kiopad(k_ipad, k_opad, key, key_sz);
- /* SHA-256 on k_ipad */
- sha256_init(ipad);
- sha256_transform(ipad, k_ipad);
- for (i = 0; i < SHA256_DIGEST_WORDS; i++)
- ipad[i] = cpu_to_be32(ipad[i]);
- /* SHA-256 on k_opad */
- sha256_init(opad);
- sha256_transform(opad, k_opad);
- for (i = 0; i < SHA256_DIGEST_WORDS; i++)
- opad[i] = cpu_to_be32(opad[i]);
- }
- /* Derive the inverse key used in AES-CBC decryption operation */
- static inline int sa_aes_inv_key(u8 *inv_key, const u8 *key, u16 key_sz)
- {
- struct crypto_aes_ctx ctx;
- int key_pos;
- if (crypto_aes_expand_key(&ctx, key, key_sz)) {
- pr_err("%s: bad key len(%d)\n", __func__, key_sz);
- return -EINVAL;
- }
- /* Based crypto_aes_expand_key logic */
- switch (key_sz) {
- case AES_KEYSIZE_128:
- case AES_KEYSIZE_192:
- key_pos = key_sz + 24;
- break;
- case AES_KEYSIZE_256:
- key_pos = key_sz + 24 - 4;
- break;
- default:
- pr_err("%s: bad key len(%d)\n", __func__, key_sz);
- return -EINVAL;
- }
- memcpy(inv_key, &ctx.key_enc[key_pos], key_sz);
- return 0;
- }
- /* Set Security context for the encryption engine */
- static int sa_set_sc_enc(struct algo_data *ad, const u8 *key, u16 key_sz,
- u16 aad_len, u8 enc, u8 *sc_buf)
- {
- const u8 *mci = NULL;
- /* Set Encryption mode selector to crypto processing */
- sc_buf[0] = SA_CRYPTO_PROCESSING;
- if (enc)
- mci = ad->mci_enc;
- else
- mci = ad->mci_dec;
- /* Set the mode control instructions in security context */
- if (mci)
- memcpy(&sc_buf[1], mci, MODE_CONTROL_BYTES);
- /* For AES-CBC decryption get the inverse key */
- if (ad->inv_key && !enc) {
- if (sa_aes_inv_key(&sc_buf[SC_ENC_KEY_OFFSET], key, key_sz))
- return -EINVAL;
- /* For all other cases: key is used */
- } else {
- memcpy(&sc_buf[SC_ENC_KEY_OFFSET], key, key_sz);
- }
- return 0;
- }
- /* Set Security context for the authentication engine */
- static void sa_set_sc_auth(struct algo_data *ad, const u8 *key, u16 key_sz,
- u8 *sc_buf)
- {
- u32 ipad[64], opad[64];
- /* Set Authentication mode selector to hash processing */
- sc_buf[0] = SA_HASH_PROCESSING;
- /* Auth SW ctrl word: bit[6]=1 (upload computed hash to TLR section) */
- sc_buf[1] = SA_UPLOAD_HASH_TO_TLR;
- sc_buf[1] |= ad->auth_ctrl;
- /* Copy the keys or ipad/opad */
- if (ad->keyed_mac) {
- ad->prep_iopad(key, key_sz, ipad, opad);
- /* Copy ipad to AuthKey */
- memcpy(&sc_buf[32], ipad, ad->hash_size);
- /* Copy opad to Aux-1 */
- memcpy(&sc_buf[64], opad, ad->hash_size);
- }
- }
- static inline void sa_copy_iv(u32 *out, const u8 *iv, bool size16)
- {
- int j;
- for (j = 0; j < ((size16) ? 4 : 2); j++) {
- *out = cpu_to_be32(*((u32 *)iv));
- iv += 4;
- out++;
- }
- }
- /* Format general command label */
- static int sa_format_cmdl_gen(struct sa_cmdl_cfg *cfg, u8 *cmdl,
- struct sa_cmdl_upd_info *upd_info)
- {
- u8 enc_offset = 0, auth_offset = 0, total = 0;
- u8 enc_next_eng = SA_ENG_ID_OUTPORT2;
- u8 auth_next_eng = SA_ENG_ID_OUTPORT2;
- u32 *word_ptr = (u32 *)cmdl;
- int i;
- /* Clear the command label */
- memzero_explicit(cmdl, (SA_MAX_CMDL_WORDS * sizeof(u32)));
- /* Iniialize the command update structure */
- memzero_explicit(upd_info, sizeof(*upd_info));
- if (cfg->enc1st) {
- if (cfg->enc_eng_id != SA_ENG_ID_NONE)
- auth_offset = SA_CMDL_HEADER_SIZE_BYTES;
- if (cfg->iv_size)
- auth_offset += cfg->iv_size;
- if (cfg->auth_eng_id != SA_ENG_ID_NONE)
- enc_next_eng = cfg->auth_eng_id;
- else
- enc_next_eng = SA_ENG_ID_OUTPORT2;
- } else {
- if (cfg->auth_eng_id != SA_ENG_ID_NONE)
- enc_offset = SA_CMDL_HEADER_SIZE_BYTES;
- if (cfg->auth_subkey_len)
- enc_offset += cfg->auth_subkey_len;
- if (cfg->enc_eng_id != SA_ENG_ID_NONE)
- auth_next_eng = cfg->enc_eng_id;
- else
- auth_next_eng = SA_ENG_ID_OUTPORT2;
- }
- if (cfg->enc_eng_id != SA_ENG_ID_NONE) {
- upd_info->flags |= SA_CMDL_UPD_ENC;
- upd_info->enc_size.index = enc_offset >> 2;
- upd_info->enc_offset.index = upd_info->enc_size.index + 1;
- /* Encryption command label */
- cmdl[enc_offset + SA_CMDL_OFFSET_NESC] = enc_next_eng;
- /* Encryption modes requiring IV */
- if (cfg->iv_size) {
- upd_info->flags |= SA_CMDL_UPD_ENC_IV;
- upd_info->enc_iv.index =
- (enc_offset + SA_CMDL_HEADER_SIZE_BYTES) >> 2;
- upd_info->enc_iv.size = cfg->iv_size;
- cmdl[enc_offset + SA_CMDL_OFFSET_LABEL_LEN] =
- SA_CMDL_HEADER_SIZE_BYTES + cfg->iv_size;
- cmdl[enc_offset + SA_CMDL_OFFSET_OPTION_CTRL1] =
- (SA_CTX_ENC_AUX2_OFFSET | (cfg->iv_size >> 3));
- enc_offset += SA_CMDL_HEADER_SIZE_BYTES + cfg->iv_size;
- } else {
- cmdl[enc_offset + SA_CMDL_OFFSET_LABEL_LEN] =
- SA_CMDL_HEADER_SIZE_BYTES;
- enc_offset += SA_CMDL_HEADER_SIZE_BYTES;
- }
- }
- if (cfg->auth_eng_id != SA_ENG_ID_NONE) {
- upd_info->flags |= SA_CMDL_UPD_AUTH;
- upd_info->auth_size.index = auth_offset >> 2;
- upd_info->auth_offset.index = upd_info->auth_size.index + 1;
- cmdl[auth_offset + SA_CMDL_OFFSET_NESC] = auth_next_eng;
- /* Algorithm with subkeys */
- if (cfg->aalg == SA_AALG_ID_AES_XCBC ||
- cfg->aalg == SA_AALG_ID_CMAC) {
- upd_info->flags |= SA_CMDL_UPD_AUX_KEY;
- upd_info->aux_key_info.index =
- (auth_offset + SA_CMDL_HEADER_SIZE_BYTES) >> 2;
- cmdl[auth_offset + SA_CMDL_OFFSET_LABEL_LEN] =
- SA_CMDL_HEADER_SIZE_BYTES +
- cfg->auth_subkey_len;
- cmdl[auth_offset + SA_CMDL_OFFSET_OPTION_CTRL1] =
- (SA_CTX_ENC_AUX1_OFFSET |
- (cfg->auth_subkey_len >> 3));
- auth_offset += SA_CMDL_HEADER_SIZE_BYTES +
- cfg->auth_subkey_len;
- } else {
- cmdl[auth_offset + SA_CMDL_OFFSET_LABEL_LEN] =
- SA_CMDL_HEADER_SIZE_BYTES;
- auth_offset += SA_CMDL_HEADER_SIZE_BYTES;
- }
- }
- if (cfg->enc1st)
- total = auth_offset;
- else
- total = enc_offset;
- total = roundup(total, 8);
- for (i = 0; i < total / 4; i++)
- word_ptr[i] = be32_to_cpu(word_ptr[i]);
- return total;
- }
- /* Update Command label */
- static inline void
- sa_update_cmdl(struct device *dev, u8 enc_offset, u16 enc_size, u8 *enc_iv,
- u8 auth_offset, u16 auth_size, u8 *auth_iv, u8 aad_size,
- u8 *aad, struct sa_cmdl_upd_info *upd_info, u32 *cmdl)
- {
- int i = 0, j;
- if (upd_info->submode != SA_MODE_GEN) {
- dev_err(dev, "unsupported mode(%d)\n", upd_info->submode);
- return;
- }
- if (likely(upd_info->flags & SA_CMDL_UPD_ENC)) {
- cmdl[upd_info->enc_size.index] &= ~SA_CMDL_PAYLOAD_LENGTH_MASK;
- cmdl[upd_info->enc_size.index] |= enc_size;
- cmdl[upd_info->enc_offset.index] &=
- ~SA_CMDL_SOP_BYPASS_LEN_MASK;
- cmdl[upd_info->enc_offset.index] |=
- ((u32)enc_offset << __ffs(SA_CMDL_SOP_BYPASS_LEN_MASK));
- if (likely(upd_info->flags & SA_CMDL_UPD_ENC_IV)) {
- u32 *data = &cmdl[upd_info->enc_iv.index];
- for (j = 0; i < upd_info->enc_iv.size; i += 4, j++) {
- data[j] = cpu_to_be32(*((u32 *)enc_iv));
- enc_iv += 4;
- }
- }
- }
- if (likely(upd_info->flags & SA_CMDL_UPD_AUTH)) {
- cmdl[upd_info->auth_size.index] &= ~SA_CMDL_PAYLOAD_LENGTH_MASK;
- cmdl[upd_info->auth_size.index] |= auth_size;
- cmdl[upd_info->auth_offset.index] &=
- ~SA_CMDL_SOP_BYPASS_LEN_MASK;
- cmdl[upd_info->auth_offset.index] |= ((u32)auth_offset <<
- __ffs(SA_CMDL_SOP_BYPASS_LEN_MASK));
- if (upd_info->flags & SA_CMDL_UPD_AUTH_IV) {
- sa_copy_iv(&cmdl[upd_info->auth_iv.index], auth_iv,
- (upd_info->auth_iv.size > 8));
- }
- if (upd_info->flags & SA_CMDL_UPD_AUX_KEY) {
- int offset = (auth_size & 0xF) ? 4 : 0;
- memcpy(&cmdl[upd_info->aux_key_info.index],
- &upd_info->aux_key[offset], 16);
- }
- }
- }
- /* Format SWINFO words to be sent to SA */
- static
- void sa_set_swinfo(u8 eng_id, u16 sc_id, dma_addr_t sc_phys,
- u8 cmdl_present, u8 cmdl_offset, u8 flags,
- u8 hash_size, u32 *swinfo)
- {
- swinfo[0] = sc_id;
- swinfo[0] |= (flags << __ffs(SA_SW0_FLAGS_MASK));
- if (likely(cmdl_present))
- swinfo[0] |= ((cmdl_offset | SA_SW0_CMDL_PRESENT) <<
- __ffs(SA_SW0_CMDL_INFO_MASK));
- swinfo[0] |= (eng_id << __ffs(SA_SW0_ENG_ID_MASK));
- swinfo[0] |= SA_SW0_DEST_INFO_PRESENT;
- swinfo[1] = (u32)(sc_phys & 0xFFFFFFFFULL);
- swinfo[2] = (u32)((sc_phys & 0xFFFFFFFF00000000ULL) >> 32);
- swinfo[2] |= (hash_size << __ffs(SA_SW2_EGRESS_LENGTH));
- }
- /* Dump the security context */
- static void sa_dump_sc(u8 *buf, dma_addr_t dma_addr)
- {
- #ifdef DEBUG
- dev_info(sa_k3_dev, "Security context dump:: 0x%pad\n", &dma_addr);
- print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
- 16, 1, buf, SA_CTX_MAX_SZ, false);
- #endif
- }
- static
- int sa_init_sc(struct sa_ctx_info *ctx, const u8 *enc_key,
- u16 enc_key_sz, const u8 *auth_key, u16 auth_key_sz,
- struct algo_data *ad, u8 enc, u32 *swinfo, bool auth_req)
- {
- int use_enc = 0;
- int enc_sc_offset = 0, auth_sc_offset = 0;
- u8 *sc_buf = ctx->sc;
- u16 sc_id = ctx->sc_id;
- u16 aad_len = 0; /* Currently not supporting AEAD algo */
- u8 first_engine;
- memzero_explicit(sc_buf, SA_CTX_MAX_SZ);
- if (ad->auth_eng.eng_id <= SA_ENG_ID_EM2 || !auth_req)
- use_enc = 1;
- /* Determine the order of encryption & Authentication contexts */
- if (enc || !use_enc) {
- if (auth_req) {
- enc_sc_offset = SA_CTX_PHP_PE_CTX_SZ;
- auth_sc_offset = enc_sc_offset + ad->enc_eng.sc_size;
- } else {
- enc_sc_offset = SA_CTX_PHP_PE_CTX_SZ;
- }
- } else {
- auth_sc_offset = SA_CTX_PHP_PE_CTX_SZ;
- enc_sc_offset = auth_sc_offset + ad->auth_eng.sc_size;
- }
- /* SCCTL Owner info: 0=host, 1=CP_ACE */
- sc_buf[SA_CTX_SCCTL_OWNER_OFFSET] = 0;
- /* SCCTL F/E control */
- if (auth_req)
- sc_buf[1] = SA_SCCTL_FE_AUTH_ENC;
- else
- sc_buf[1] = SA_SCCTL_FE_ENC;
- memcpy(&sc_buf[2], &sc_id, 2);
- sc_buf[4] = 0x0;
- sc_buf[5] = 0x0;
- sc_buf[6] = 0x0;
- sc_buf[7] = 0x0;
- /* Initialize the rest of PHP context */
- memzero_explicit(sc_buf + SA_SCCTL_SZ, SA_CTX_PHP_PE_CTX_SZ -
- SA_SCCTL_SZ);
- /* Prepare context for encryption engine */
- if (ad->enc_eng.sc_size) {
- if (sa_set_sc_enc(ad, enc_key, enc_key_sz, aad_len,
- enc, &sc_buf[enc_sc_offset]))
- return -EINVAL;
- }
- /* Prepare context for authentication engine */
- if (ad->auth_eng.sc_size) {
- if (use_enc) {
- if (sa_set_sc_enc(ad, auth_key, auth_key_sz,
- aad_len, 0, &sc_buf[auth_sc_offset]))
- return -EINVAL;
- } else {
- sa_set_sc_auth(ad, auth_key, auth_key_sz,
- &sc_buf[auth_sc_offset]);
- }
- }
- /* Set the ownership of context to CP_ACE */
- sc_buf[SA_CTX_SCCTL_OWNER_OFFSET] = 0x80;
- /* swizzle the security context */
- sa_swiz_128(sc_buf, SA_CTX_MAX_SZ);
- /* Setup SWINFO */
- if (!auth_req)
- first_engine = ad->enc_eng.eng_id;
- else
- first_engine = enc ? ad->enc_eng.eng_id : ad->auth_eng.eng_id;
- if (auth_req) {
- if (!ad->hash_size)
- return -EINVAL;
- /* Round up the tag size to multiple of 4 */
- ad->hash_size = roundup(ad->hash_size, 8);
- }
- sa_set_swinfo(first_engine, ctx->sc_id, ctx->sc_phys, 1, 0,
- SA_SW_INFO_FLAG_EVICT, ad->hash_size, swinfo);
- sa_dump_sc(sc_buf, ctx->sc_phys);
- return 0;
- }
- /* Free the per direction context memory */
- static void sa_free_ctx_info(struct sa_ctx_info *ctx,
- struct sa_crypto_data *data)
- {
- unsigned long bn;
- bn = ctx->sc_id - data->sc_id_start;
- spin_lock(&data->scid_lock);
- __clear_bit(bn, data->ctx_bm);
- data->sc_id--;
- spin_unlock(&data->scid_lock);
- if (ctx->sc) {
- dma_pool_free(data->sc_pool, ctx->sc, ctx->sc_phys);
- ctx->sc = NULL;
- }
- }
- static int sa_init_ctx_info(struct sa_ctx_info *ctx,
- struct sa_crypto_data *data)
- {
- unsigned long bn;
- int err;
- spin_lock(&data->scid_lock);
- bn = find_first_zero_bit(data->ctx_bm, SA_MAX_NUM_CTX);
- __set_bit(bn, data->ctx_bm);
- data->sc_id++;
- spin_unlock(&data->scid_lock);
- ctx->sc_id = (u16)(data->sc_id_start + bn);
- ctx->sc = dma_pool_alloc(data->sc_pool, GFP_KERNEL, &ctx->sc_phys);
- if (!ctx->sc) {
- dev_err(&data->pdev->dev, "Failed to allocate SC memory\n");
- err = -ENOMEM;
- goto scid_rollback;
- }
- return 0;
- scid_rollback:
- spin_lock(&data->scid_lock);
- __clear_bit(bn, data->ctx_bm);
- data->sc_id--;
- spin_unlock(&data->scid_lock);
- return err;
- }
- static void sa_aes_cra_exit(struct crypto_tfm *tfm)
- {
- struct crypto_alg *alg = tfm->__crt_alg;
- struct sa_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
- struct sa_crypto_data *data = dev_get_drvdata(sa_k3_dev);
- dev_dbg(sa_k3_dev, "%s(0x%p) sc-ids(0x%x(0x%pad), 0x%x(0x%pad))\n",
- __func__, tfm, ctx->enc.sc_id, &ctx->enc.sc_phys,
- ctx->dec.sc_id, &ctx->dec.sc_phys);
- if ((alg->cra_flags & CRYPTO_ALG_TYPE_ABLKCIPHER)
- == CRYPTO_ALG_TYPE_ABLKCIPHER) {
- sa_free_ctx_info(&ctx->enc, data);
- sa_free_ctx_info(&ctx->dec, data);
- }
- }
- static int sa_aes_cra_init(struct crypto_tfm *tfm)
- {
- struct sa_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
- struct crypto_alg *alg = tfm->__crt_alg;
- struct sa_crypto_data *data = dev_get_drvdata(sa_k3_dev);
- int ret;
- if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
- CRYPTO_ALG_TYPE_ABLKCIPHER) {
- memzero_explicit(ctx, sizeof(*ctx));
- ctx->dev_data = data;
- ret = sa_init_ctx_info(&ctx->enc, data);
- if (ret)
- return ret;
- ret = sa_init_ctx_info(&ctx->dec, data);
- if (ret) {
- sa_free_ctx_info(&ctx->enc, data);
- return ret;
- }
- }
- dev_dbg(sa_k3_dev, "%s(0x%p) sc-ids(0x%x(0x%pad), 0x%x(0x%pad))\n",
- __func__, tfm, ctx->enc.sc_id, &ctx->enc.sc_phys,
- ctx->dec.sc_id, &ctx->dec.sc_phys);
- return 0;
- }
- static int sa_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
- unsigned int keylen, struct algo_data *ad)
- {
- struct sa_tfm_ctx *ctx = crypto_ablkcipher_ctx(tfm);
- const char *cra_name;
- int cmdl_len;
- struct sa_cmdl_cfg cfg;
- if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
- keylen != AES_KEYSIZE_256)
- return -EINVAL;
- cra_name = crypto_tfm_alg_name(&tfm->base);
- memzero_explicit(&cfg, sizeof(cfg));
- cfg.enc1st = 1;
- cfg.enc_eng_id = ad->enc_eng.eng_id;
- cfg.iv_size = crypto_ablkcipher_ivsize(tfm);
- cfg.auth_eng_id = SA_ENG_ID_NONE;
- cfg.auth_subkey_len = 0;
- /* Setup Encryption Security Context & Command label template */
- if (sa_init_sc(&ctx->enc, key, keylen,
- NULL, 0, ad, 1, &ctx->enc.epib[1], false))
- goto badkey;
- cmdl_len = sa_format_cmdl_gen(&cfg,
- (u8 *)ctx->enc.cmdl,
- &ctx->enc.cmdl_upd_info);
- if (cmdl_len <= 0 || (cmdl_len > SA_MAX_CMDL_WORDS * sizeof(u32)))
- goto badkey;
- ctx->enc.cmdl_size = cmdl_len;
- /* Setup Decryption Security Context & Command label template */
- if (sa_init_sc(&ctx->dec, key, keylen,
- NULL, 0, ad, 0, &ctx->dec.epib[1], false))
- goto badkey;
- cfg.enc1st = 0;
- cfg.enc_eng_id = ad->enc_eng.eng_id;
- cfg.auth_eng_id = SA_ENG_ID_NONE;
- cfg.auth_subkey_len = 0;
- cmdl_len = sa_format_cmdl_gen(&cfg, (u8 *)ctx->dec.cmdl,
- &ctx->dec.cmdl_upd_info);
- if (cmdl_len <= 0 || (cmdl_len > SA_MAX_CMDL_WORDS * sizeof(u32)))
- goto badkey;
- ctx->dec.cmdl_size = cmdl_len;
- kfree(ad);
- return 0;
- badkey:
- dev_err(sa_k3_dev, "%s: badkey\n", __func__);
- return -EINVAL;
- }
- static int sa_aes_cbc_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
- unsigned int keylen)
- {
- struct algo_data *ad = kzalloc(sizeof(*ad), GFP_KERNEL);
- /* Convert the key size (16/24/32) to the key size index (0/1/2) */
- int key_idx = (keylen >> 3) - 2;
- ad->enc_eng.eng_id = SA_ENG_ID_EM1;
- ad->enc_eng.sc_size = SA_CTX_ENC_TYPE1_SZ;
- ad->auth_eng.eng_id = SA_ENG_ID_NONE;
- ad->auth_eng.sc_size = 0;
- ad->mci_enc = mci_cbc_enc_array[key_idx];
- ad->mci_dec = mci_cbc_dec_array[key_idx];
- ad->inv_key = true;
- ad->ealg_id = SA_EALG_ID_AES_CBC;
- ad->aalg_id = SA_AALG_ID_NONE;
- return sa_aes_setkey(tfm, key, keylen, ad);
- }
- static int sa_aes_ecb_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
- unsigned int keylen)
- {
- struct algo_data *ad = kzalloc(sizeof(*ad), GFP_KERNEL);
- /* Convert the key size (16/24/32) to the key size index (0/1/2) */
- int key_idx = (keylen >> 3) - 2;
- ad->enc_eng.eng_id = SA_ENG_ID_EM1;
- ad->enc_eng.sc_size = SA_CTX_ENC_TYPE1_SZ;
- ad->auth_eng.eng_id = SA_ENG_ID_NONE;
- ad->auth_eng.sc_size = 0;
- ad->mci_enc = mci_ecb_enc_array[key_idx];
- ad->mci_dec = mci_ecb_dec_array[key_idx];
- ad->inv_key = true;
- ad->ealg_id = SA_EALG_ID_AES_ECB;
- ad->aalg_id = SA_AALG_ID_NONE;
- return sa_aes_setkey(tfm, key, keylen, ad);
- }
- static int sa_3des_cbc_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
- unsigned int keylen)
- {
- struct algo_data *ad = kzalloc(sizeof(*ad), GFP_KERNEL);
- ad->enc_eng.eng_id = SA_ENG_ID_EM1;
- ad->enc_eng.sc_size = SA_CTX_ENC_TYPE1_SZ;
- ad->auth_eng.eng_id = SA_ENG_ID_NONE;
- ad->auth_eng.sc_size = 0;
- ad->mci_enc = mci_cbc_3des_enc_array;
- ad->mci_dec = mci_cbc_3des_dec_array;
- ad->ealg_id = SA_EALG_ID_3DES_CBC;
- ad->aalg_id = SA_AALG_ID_NONE;
- return sa_aes_setkey(tfm, key, keylen, ad);
- }
- static int sa_3des_ecb_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
- unsigned int keylen)
- {
- struct algo_data *ad = kzalloc(sizeof(*ad), GFP_KERNEL);
- ad->enc_eng.eng_id = SA_ENG_ID_EM1;
- ad->enc_eng.sc_size = SA_CTX_ENC_TYPE1_SZ;
- ad->auth_eng.eng_id = SA_ENG_ID_NONE;
- ad->auth_eng.sc_size = 0;
- ad->mci_enc = mci_ecb_3des_enc_array;
- ad->mci_dec = mci_ecb_3des_dec_array;
- ad->aalg_id = SA_AALG_ID_NONE;
- return sa_aes_setkey(tfm, key, keylen, ad);
- }
- static void sa_aes_dma_in_callback(void *data)
- {
- struct sa_rx_data *rxd = (struct sa_rx_data *)data;
- struct ablkcipher_request *req = (struct ablkcipher_request *)rxd->req;
- int sglen = sg_nents_for_len(req->dst, req->nbytes);
- kfree(rxd);
- dma_unmap_sg(sa_k3_dev, req->src, sglen, DMA_TO_DEVICE);
- if (req->src != req->dst)
- dma_unmap_sg(sa_k3_dev, req->dst, sglen, DMA_FROM_DEVICE);
- ablkcipher_request_complete(req, 0);
- }
- static void sa_aead_dma_in_callback(void *data)
- {
- struct sa_rx_data *rxd = (struct sa_rx_data *)data;
- struct aead_request *req = (struct aead_request *)rxd->req;
- struct crypto_aead *tfm = crypto_aead_reqtfm(req);
- u32 *mdptr;
- unsigned int start = req->assoclen + req->cryptlen;
- unsigned int authsize = crypto_aead_authsize(tfm);
- u8 auth_tag[SA_MAX_AUTH_TAG_SZ];
- int i, sglen, err = 0;
- size_t pl, ml;
- mdptr = (u32 *)dmaengine_desc_get_metadata_ptr(rxd->tx_in, &pl, &ml);
- for (i = 0; i < (authsize / 4); i++)
- mdptr[i + 4] = htonl(mdptr[i + 4]);
- if (rxd->enc) {
- scatterwalk_map_and_copy((void *)&mdptr[4], req->dst,
- start, crypto_aead_authsize(tfm), 1);
- } else {
- start -= authsize;
- scatterwalk_map_and_copy(auth_tag, req->src,
- start, crypto_aead_authsize(tfm), 0);
- err = memcmp((void *)&mdptr[4],
- auth_tag, authsize) ? -EBADMSG : 0;
- }
- kfree(rxd);
- sglen = sg_nents_for_len(req->dst, req->cryptlen + authsize);
- dma_unmap_sg(sa_k3_dev, req->dst, sglen, DMA_FROM_DEVICE);
- sglen = sg_nents_for_len(req->src, req->assoclen + req->cryptlen);
- dma_unmap_sg(sa_k3_dev, req->src, sglen, DMA_TO_DEVICE);
- aead_request_complete(req, err);
- }
- static void
- sa_prepare_tx_desc(u32 *mdptr, u32 pslen, u32 *psdata, u32 epiblen, u32 *epib)
- {
- u32 *out, *in;
- int i;
- for (out = mdptr, in = epib, i = 0; i < epiblen / sizeof(u32); i++)
- *out++ = *in++;
- mdptr[4] = (0xFFFF << 16);
- for (out = &mdptr[5], in = psdata, i = 0;
- i < pslen / sizeof(u32); i++)
- *out++ = *in++;
- }
- static int sa_aes_run(struct ablkcipher_request *req, u8 *iv, int enc)
- {
- struct sa_tfm_ctx *ctx =
- crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
- struct sa_ctx_info *sa_ctx = enc ? &ctx->enc : &ctx->dec;
- struct sa_crypto_data *pdata = dev_get_drvdata(sa_k3_dev);
- struct sa_dma_req_ctx req_ctx;
- struct dma_async_tx_descriptor *tx_in, *tx_out;
- struct sa_rx_data *rxd;
- u8 enc_offset;
- int sg_nents, dst_nents;
- int psdata_offset;
- u8 auth_offset = 0;
- u8 *auth_iv = NULL;
- u8 *aad = NULL;
- u8 aad_len = 0;
- u16 enc_len;
- u16 auth_len = 0;
- u32 req_type;
- u32 *mdptr;
- size_t pl, ml;
- struct dma_chan *dma_rx;
- gfp_t flags;
- flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
- GFP_KERNEL : GFP_ATOMIC;
- enc_offset = 0x0;
- enc_len = req->nbytes;
- /* Allocate descriptor & submit packet */
- sg_nents = sg_nents_for_len(req->src, enc_len);
- dst_nents = sg_nents_for_len(req->dst, enc_len);
- memcpy(req_ctx.cmdl, sa_ctx->cmdl, sa_ctx->cmdl_size);
- /* Update Command Label */
- sa_update_cmdl(sa_k3_dev, enc_offset, enc_len,
- iv, auth_offset, auth_len,
- auth_iv, aad_len, aad,
- &sa_ctx->cmdl_upd_info, req_ctx.cmdl);
- /*
- * Last 2 words in PSDATA will have the crypto alg type &
- * crypto request pointer
- */
- req_type = CRYPTO_ALG_TYPE_ABLKCIPHER;
- if (enc)
- req_type |= (SA_REQ_SUBTYPE_ENC << SA_REQ_SUBTYPE_SHIFT);
- else
- req_type |= (SA_REQ_SUBTYPE_DEC << SA_REQ_SUBTYPE_SHIFT);
- psdata_offset = sa_ctx->cmdl_size / sizeof(u32);
- req_ctx.cmdl[psdata_offset++] = req_type;
- /* map the packet */
- req_ctx.src = req->src;
- req_ctx.src_nents = dma_map_sg(sa_k3_dev, req_ctx.src,
- sg_nents, DMA_TO_DEVICE);
- if (req->src != req->dst)
- dst_nents = dma_map_sg(sa_k3_dev, req->dst,
- sg_nents, DMA_FROM_DEVICE);
- else
- dst_nents = req_ctx.src_nents;
- if (unlikely(req_ctx.src_nents != sg_nents)) {
- dev_warn_ratelimited(sa_k3_dev, "failed to map tx pkt\n");
- return -EIO;
- }
- req_ctx.dev_data = pdata;
- req_ctx.pkt = true;
- dma_sync_sg_for_device(pdata->dev, req->src, req_ctx.src_nents,
- DMA_TO_DEVICE);
- if (enc_len >= 256)
- dma_rx = pdata->dma_rx2;
- else
- dma_rx = pdata->dma_rx1;
- tx_in = dmaengine_prep_slave_sg(dma_rx, req->dst, dst_nents,
- DMA_DEV_TO_MEM,
- DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
- if (!tx_in) {
- dev_err(pdata->dev, "IN prep_slave_sg() failed\n");
- return -EINVAL;
- }
- rxd = kzalloc(sizeof(*rxd), GFP_KERNEL);
- rxd->req = (void *)req;
- /* IN */
- tx_in->callback = sa_aes_dma_in_callback;
- tx_in->callback_param = rxd;
- tx_out = dmaengine_prep_slave_sg(pdata->dma_tx, req->src,
- req_ctx.src_nents, DMA_MEM_TO_DEV,
- DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
- if (!tx_out) {
- dev_err(pdata->dev, "OUT prep_slave_sg() failed\n");
- return -EINVAL;
- }
- mdptr = (u32 *)dmaengine_desc_get_metadata_ptr(tx_out, &pl, &ml);
- sa_prepare_tx_desc(mdptr, (sa_ctx->cmdl_size + (SA_PSDATA_CTX_WORDS *
- sizeof(u32))), req_ctx.cmdl,
- sizeof(sa_ctx->epib), sa_ctx->epib);
- ml = sa_ctx->cmdl_size + (SA_PSDATA_CTX_WORDS * sizeof(u32));
- dmaengine_desc_set_metadata_len(tx_out, 44);
- dmaengine_submit(tx_out);
- dmaengine_submit(tx_in);
- dma_async_issue_pending(dma_rx);
- dma_async_issue_pending(pdata->dma_tx);
- return -EINPROGRESS;
- }
- static int sa_aes_cbc_encrypt(struct ablkcipher_request *req)
- {
- return sa_aes_run(req, req->info, 1);
- }
- static int sa_aes_cbc_decrypt(struct ablkcipher_request *req)
- {
- return sa_aes_run(req, req->info, 0);
- }
- static int sa_init_tfm(struct crypto_tfm *tfm)
- {
- struct crypto_alg *alg = tfm->__crt_alg;
- struct sa_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
- struct sa_crypto_data *data = dev_get_drvdata(sa_k3_dev);
- int ret;
- if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AEAD) {
- memset(ctx, 0, sizeof(*ctx));
- ctx->dev_data = data;
- ret = sa_init_ctx_info(&ctx->enc, data);
- if (ret)
- return ret;
- ret = sa_init_ctx_info(&ctx->dec, data);
- if (ret) {
- sa_free_ctx_info(&ctx->enc, data);
- return ret;
- }
- }
- dev_dbg(sa_k3_dev, "%s(0x%p) sc-ids(0x%x(0x%pad), 0x%x(0x%pad))\n",
- __func__, tfm, ctx->enc.sc_id, &ctx->enc.sc_phys,
- ctx->dec.sc_id, &ctx->dec.sc_phys);
- return 0;
- }
- /* Algorithm init */
- static int sa_cra_init_aead(struct crypto_aead *tfm)
- {
- return sa_init_tfm(crypto_aead_tfm(tfm));
- }
- /* Algorithm context teardown */
- static void sa_exit_tfm(struct crypto_tfm *tfm)
- {
- struct crypto_alg *alg = tfm->__crt_alg;
- struct sa_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
- struct sa_crypto_data *data = dev_get_drvdata(sa_k3_dev);
- dev_dbg(sa_k3_dev, "%s(0x%p) sc-ids(0x%x(0x%pad), 0x%x(0x%pad))\n",
- __func__, tfm, ctx->enc.sc_id, &ctx->enc.sc_phys,
- ctx->dec.sc_id, &ctx->dec.sc_phys);
- if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK)
- == CRYPTO_ALG_TYPE_AEAD) {
- sa_free_ctx_info(&ctx->enc, data);
- sa_free_ctx_info(&ctx->dec, data);
- }
- }
- static void sa_exit_tfm_aead(struct crypto_aead *tfm)
- {
- return sa_exit_tfm(crypto_aead_tfm(tfm));
- }
- /* AEAD algorithm configuration interface function */
- static int sa_aead_setkey(struct crypto_aead *authenc,
- const u8 *key, unsigned int keylen,
- struct algo_data *ad)
- {
- struct sa_tfm_ctx *ctx = crypto_aead_ctx(authenc);
- struct crypto_authenc_keys keys;
- const char *cra_name;
- int cmdl_len;
- struct sa_cmdl_cfg cfg;
- if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
- goto badkey;
- cra_name = crypto_tfm_alg_name(crypto_aead_tfm(authenc));
- memset(&cfg, 0, sizeof(cfg));
- cfg.enc1st = 1;
- cfg.aalg = ad->aalg_id;
- cfg.enc_eng_id = ad->enc_eng.eng_id;
- cfg.auth_eng_id = ad->auth_eng.eng_id;
- cfg.iv_size = crypto_aead_ivsize(authenc);
- cfg.akey = keys.authkey;
- cfg.akey_len = keys.authkeylen;
- /* Setup Encryption Security Context & Command label template */
- if (sa_init_sc(&ctx->enc, keys.enckey, keys.enckeylen,
- keys.authkey, keys.authkeylen,
- ad, 1, &ctx->enc.epib[1], true))
- goto badkey;
- cmdl_len = sa_format_cmdl_gen(&cfg,
- (u8 *)ctx->enc.cmdl,
- &ctx->enc.cmdl_upd_info);
- if (cmdl_len <= 0 || (cmdl_len > SA_MAX_CMDL_WORDS * sizeof(u32)))
- goto badkey;
- ctx->enc.cmdl_size = cmdl_len;
- /* Setup Decryption Security Context & Command label template */
- if (sa_init_sc(&ctx->dec, keys.enckey, keys.enckeylen,
- keys.authkey, keys.authkeylen,
- ad, 0, &ctx->dec.epib[1], true))
- goto badkey;
- cfg.enc1st = 0;
- cfg.enc_eng_id = ad->enc_eng.eng_id;
- cmdl_len = sa_format_cmdl_gen(&cfg, (u8 *)ctx->dec.cmdl,
- &ctx->dec.cmdl_upd_info);
- if (cmdl_len <= 0 || (cmdl_len > SA_MAX_CMDL_WORDS * sizeof(u32)))
- goto badkey;
- ctx->dec.cmdl_size = cmdl_len;
- kfree(ad);
- return 0;
- badkey:
- dev_err(sa_k3_dev, "%s: badkey\n", __func__);
- return -EINVAL;
- }
- static int sa_aead_cbc_sha1_setkey(struct crypto_aead *authenc,
- const u8 *key, unsigned int keylen)
- {
- struct algo_data *ad = kzalloc(sizeof(*ad), GFP_KERNEL);
- struct crypto_authenc_keys keys;
- int ret = 0, key_idx;
- ret = crypto_authenc_extractkeys(&keys, key, keylen);
- if (ret)
- return ret;
- /* Convert the key size (16/24/32) to the key size index (0/1/2) */
- key_idx = (keys.enckeylen >> 3) - 2;
- ad->enc_eng.eng_id = SA_ENG_ID_EM1;
- ad->enc_eng.sc_size = SA_CTX_ENC_TYPE1_SZ;
- ad->auth_eng.eng_id = SA_ENG_ID_AM1;
- ad->auth_eng.sc_size = SA_CTX_AUTH_TYPE2_SZ;
- ad->mci_enc = mci_cbc_enc_array[key_idx];
- ad->mci_dec = mci_cbc_dec_array[key_idx];
- ad->inv_key = true;
- ad->keyed_mac = true;
- ad->ealg_id = SA_EALG_ID_AES_CBC;
- ad->aalg_id = SA_AALG_ID_HMAC_SHA1;
- ad->hash_size = SHA1_DIGEST_SIZE;
- ad->auth_ctrl = 0x2;
- ad->prep_iopad = sa_hmac_sha1_get_pad;
- return sa_aead_setkey(authenc, key, keylen, ad);
- }
- static int sa_aead_cbc_sha256_setkey(struct crypto_aead *authenc,
- const u8 *key, unsigned int keylen)
- {
- struct algo_data *ad = kzalloc(sizeof(*ad), GFP_KERNEL);
- struct crypto_authenc_keys keys;
- int ret = 0, key_idx;
- ret = crypto_authenc_extractkeys(&keys, key, keylen);
- if (ret)
- return ret;
- /* Convert the key size (16/24/32) to the key size index (0/1/2) */
- key_idx = (keys.enckeylen >> 3) - 2;
- ad->enc_eng.eng_id = SA_ENG_ID_EM1;
- ad->enc_eng.sc_size = SA_CTX_ENC_TYPE1_SZ;
- ad->auth_eng.eng_id = SA_ENG_ID_AM1;
- ad->auth_eng.sc_size = SA_CTX_AUTH_TYPE2_SZ;
- ad->mci_enc = mci_cbc_enc_array[key_idx];
- ad->mci_dec = mci_cbc_dec_array[key_idx];
- ad->inv_key = true;
- ad->keyed_mac = true;
- ad->ealg_id = SA_EALG_ID_AES_CBC;
- ad->aalg_id = SA_AALG_ID_HMAC_SHA2_256;
- ad->hash_size = SHA256_DIGEST_SIZE;
- ad->auth_ctrl = 0x4;
- ad->prep_iopad = sa_hmac_sha256_get_pad;
- return sa_aead_setkey(authenc, key, keylen, ad);
- }
- static int sa_aead_run(struct aead_request *req, u8 *iv, int enc)
- {
- struct crypto_aead *tfm = crypto_aead_reqtfm(req);
- struct sa_tfm_ctx *ctx = crypto_aead_ctx(tfm);
- struct sa_ctx_info *sa_ctx = enc ? &ctx->enc : &ctx->dec;
- struct sa_rx_data *rxd;
- struct dma_async_tx_descriptor *tx_in, *tx_out;
- struct sa_crypto_data *pdata = dev_get_drvdata(sa_k3_dev);
- struct sa_dma_req_ctx req_ctx;
- u8 enc_offset;
- int sg_nents, dst_nents;
- int psdata_offset;
- u8 auth_offset = 0;
- u8 *auth_iv = NULL;
- u8 *aad = NULL;
- u8 aad_len = 0;
- u16 enc_len;
- u16 auth_len = 0;
- u32 *mdptr;
- u32 req_type;
- struct dma_chan *dma_rx;
- gfp_t flags;
- size_t pl, ml;
- flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
- GFP_KERNEL : GFP_ATOMIC;
- if (enc) {
- iv = (u8 *)(req->iv);
- enc_offset = req->assoclen;
- enc_len = req->cryptlen;
- auth_len = req->assoclen + req->cryptlen;
- } else {
- enc_offset = req->assoclen;
- enc_len = req->cryptlen - crypto_aead_authsize(tfm);
- auth_len = req->assoclen + req->cryptlen -
- crypto_aead_authsize(tfm);
- }
- /* Allocate descriptor & submit packet */
- sg_nents = sg_nents_for_len(req->src, enc_len + req->assoclen);
- dst_nents = sg_nents_for_len(req->dst, enc_len +
- crypto_aead_authsize(tfm));
- memcpy(req_ctx.cmdl, sa_ctx->cmdl, sa_ctx->cmdl_size);
- /* Update Command Label */
- sa_update_cmdl(sa_k3_dev, enc_offset, enc_len,
- iv, auth_offset, auth_len,
- auth_iv, aad_len, aad,
- &sa_ctx->cmdl_upd_info, req_ctx.cmdl);
- /*
- * Last 2 words in PSDATA will have the crypto alg type &
- * crypto request pointer
- */
- req_type = CRYPTO_ALG_TYPE_AEAD;
- if (enc)
- req_type |= (SA_REQ_SUBTYPE_ENC << SA_REQ_SUBTYPE_SHIFT);
- else
- req_type |= (SA_REQ_SUBTYPE_DEC << SA_REQ_SUBTYPE_SHIFT);
- psdata_offset = sa_ctx->cmdl_size / sizeof(u32);
- /* map the packet */
- req_ctx.src = req->src;
- req_ctx.src_nents = dma_map_sg(sa_k3_dev, req_ctx.src,
- sg_nents, DMA_TO_DEVICE);
- dst_nents = dma_map_sg(sa_k3_dev, req->dst,
- dst_nents, DMA_FROM_DEVICE);
- if (unlikely(req_ctx.src_nents != sg_nents)) {
- dev_warn_ratelimited(sa_k3_dev, "failed to map tx pkt\n");
- return -EIO;
- }
- req_ctx.dev_data = pdata;
- req_ctx.pkt = true;
- dma_sync_sg_for_device(pdata->dev, req->src, req_ctx.src_nents,
- DMA_TO_DEVICE);
- if (enc_len >= 256)
- dma_rx = pdata->dma_rx2;
- else
- dma_rx = pdata->dma_rx1;
- tx_in = dmaengine_prep_slave_sg(dma_rx, req->dst, dst_nents,
- DMA_DEV_TO_MEM,
- DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
- if (!tx_in) {
- dev_err(pdata->dev, "IN prep_slave_sg() failed\n");
- return -EINVAL;
- }
- rxd = kzalloc(sizeof(*rxd), GFP_KERNEL);
- rxd->req = (void *)req;
- rxd->enc = enc;
- rxd->tx_in = tx_in;
- /* IN */
- tx_in->callback = sa_aead_dma_in_callback;
- tx_in->callback_param = rxd;
- tx_out = dmaengine_prep_slave_sg(pdata->dma_tx, req->src,
- req_ctx.src_nents, DMA_MEM_TO_DEV,
- DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
- if (!tx_out) {
- dev_err(pdata->dev, "OUT prep_slave_sg() failed\n");
- return -EINVAL;
- }
- mdptr = (u32 *)dmaengine_desc_get_metadata_ptr(tx_out, &pl, &ml);
- sa_prepare_tx_desc(mdptr, (sa_ctx->cmdl_size + (SA_PSDATA_CTX_WORDS *
- sizeof(u32))), req_ctx.cmdl,
- sizeof(sa_ctx->epib), sa_ctx->epib);
- ml = sa_ctx->cmdl_size + (SA_PSDATA_CTX_WORDS * sizeof(u32));
- dmaengine_desc_set_metadata_len(tx_out, 52);
- dmaengine_submit(tx_out);
- dmaengine_submit(tx_in);
- dma_async_issue_pending(dma_rx);
- dma_async_issue_pending(pdata->dma_tx);
- return -EINPROGRESS;
- }
- /* AEAD algorithm encrypt interface function */
- static int sa_aead_encrypt(struct aead_request *req)
- {
- return sa_aead_run(req, req->iv, 1);
- }
- /* AEAD algorithm decrypt interface function */
- static int sa_aead_decrypt(struct aead_request *req)
- {
- return sa_aead_run(req, req->iv, 0);
- }
- static int sa_sham_cra_init_alg(struct crypto_tfm *tfm, const char *alg_base)
- {
- struct sa_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
- struct crypto_alg *alg = tfm->__crt_alg;
- struct sa_crypto_data *data = dev_get_drvdata(sa_k3_dev);
- int ret;
- if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
- CRYPTO_ALG_TYPE_AHASH) {
- memset(ctx, 0, sizeof(*ctx));
- ctx->dev_data = data;
- ret = sa_init_ctx_info(&ctx->enc, data);
- if (ret)
- return ret;
- }
- if (alg_base) {
- ctx->shash = crypto_alloc_shash(alg_base, 0,
- CRYPTO_ALG_NEED_FALLBACK);
- if (IS_ERR(ctx->shash)) {
- pr_err("base driver %s couldn't be loaded\n", alg_base);
- return PTR_ERR(ctx->shash);
- }
- }
- dev_dbg(sa_k3_dev, "%s(0x%p) sc-ids(0x%x(0x%pad), 0x%x(0x%pad))\n",
- __func__, tfm, ctx->enc.sc_id, &ctx->enc.sc_phys,
- ctx->dec.sc_id, &ctx->dec.sc_phys);
- crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
- sizeof(struct sa_dma_req_ctx) +
- SHA512_BLOCK_SIZE);
- return 0;
- }
- static void sa_sham_dma_in_callback(void *data)
- {
- struct sa_rx_data *rxd = (struct sa_rx_data *)data;
- struct ahash_request *req = (struct ahash_request *)rxd->req;
- struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
- unsigned int authsize = crypto_ahash_digestsize(tfm);
- int i;
- size_t ml, pl;
- u32 *mdptr, *result;
- mdptr = (u32 *)dmaengine_desc_get_metadata_ptr(rxd->tx_in, &pl, &ml);
- result = (u32 *)req->result;
- kfree(rxd);
- for (i = 0; i < (authsize / 4); i++)
- result[i] = htonl(mdptr[i + 4]);
- ahash_request_complete(req, 0);
- }
- static int sa_sham_digest(struct ahash_request *req)
- {
- struct sa_tfm_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
- struct sa_ctx_info *sa_ctx = &ctx->enc;
- struct dma_async_tx_descriptor *tx_in, *tx_out;
- struct sa_crypto_data *pdata = dev_get_drvdata(sa_k3_dev);
- struct sa_dma_req_ctx req_ctx;
- struct sa_rx_data *rxd;
- u8 enc_offset;
- int sg_nents;
- int psdata_offset;
- u8 auth_offset = 0;
- u8 *auth_iv = NULL;
- u8 *aad = NULL;
- u8 aad_len = 0;
- u16 enc_len;
- u16 auth_len = 0;
- u32 req_type;
- u32 *mdptr;
- struct dma_chan *dma_rx;
- gfp_t flags;
- size_t pl, ml;
- flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
- GFP_KERNEL : GFP_ATOMIC;
- enc_len = 0;
- auth_len = req->nbytes;
- enc_offset = 0;
- /* Allocate descriptor & submit packet */
- sg_nents = sg_nents_for_len(req->src, req->nbytes);
- memcpy(req_ctx.cmdl, sa_ctx->cmdl, sa_ctx->cmdl_size);
- /* Update Command Label */
- sa_update_cmdl(sa_k3_dev, enc_offset, enc_len,
- NULL, auth_offset, auth_len,
- auth_iv, aad_len, aad,
- &sa_ctx->cmdl_upd_info, req_ctx.cmdl);
- /*
- * Last 2 words in PSDATA will have the crypto alg type &
- * crypto request pointer
- */
- req_type = CRYPTO_ALG_TYPE_AHASH;
- psdata_offset = sa_ctx->cmdl_size / sizeof(u32);
- req_ctx.cmdl[psdata_offset++] = req_type;
- /* map the packet */
- req_ctx.src = req->src;
- req_ctx.src_nents = dma_map_sg(sa_k3_dev, req_ctx.src,
- sg_nents, DMA_TO_DEVICE);
- if (unlikely(req_ctx.src_nents != sg_nents)) {
- dev_warn_ratelimited(sa_k3_dev, "failed to map tx pkt\n");
- return -EIO;
- }
- req_ctx.dev_data = pdata;
- req_ctx.pkt = true;
- dma_sync_sg_for_device(pdata->dev, req->src, req_ctx.src_nents,
- DMA_TO_DEVICE);
- if (enc_len > 256)
- dma_rx = pdata->dma_rx2;
- else
- dma_rx = pdata->dma_rx1;
- tx_in = dmaengine_prep_slave_sg(dma_rx, req->src, req_ctx.src_nents,
- DMA_DEV_TO_MEM,
- DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
- if (!tx_in) {
- dev_err(pdata->dev, "IN prep_slave_sg() failed\n");
- return -EINVAL;
- }
- rxd = kzalloc(sizeof(*rxd), GFP_KERNEL);
- rxd->req = (void *)req;
- rxd->tx_in = tx_in;
- tx_in->callback = sa_sham_dma_in_callback;
- tx_in->callback_param = rxd;
- tx_out = dmaengine_prep_slave_sg(pdata->dma_tx, req->src,
- req_ctx.src_nents, DMA_MEM_TO_DEV,
- DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
- if (!tx_out) {
- dev_err(pdata->dev, "OUT prep_slave_sg() failed\n");
- return -EINVAL;
- }
- mdptr = (u32 *)dmaengine_desc_get_metadata_ptr(tx_out, &pl, &ml);
- sa_prepare_tx_desc(mdptr, (sa_ctx->cmdl_size + (SA_PSDATA_CTX_WORDS *
- sizeof(u32))), req_ctx.cmdl,
- sizeof(sa_ctx->epib), sa_ctx->epib);
- dmaengine_desc_set_metadata_len(tx_out, 28);
- dmaengine_submit(tx_out);
- dmaengine_submit(tx_in);
- dma_async_issue_pending(dma_rx);
- dma_async_issue_pending(pdata->dma_tx);
- return -EINPROGRESS;
- }
- static int sa_sham_init(struct ahash_request *req)
- {
- struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
- dev_dbg(sa_k3_dev, "init: digest size: %d\n",
- crypto_ahash_digestsize(tfm));
- return 0;
- }
- static int sa_sham_shash_digest(struct crypto_shash *tfm, u32 flags,
- const u8 *data, unsigned int len, u8 *out)
- {
- SHASH_DESC_ON_STACK(shash, tfm);
- shash->tfm = tfm;
- shash->flags = flags & CRYPTO_TFM_REQ_MAY_SLEEP;
- return crypto_shash_digest(shash, data, len, out);
- }
- static int sa_sham_setkey(struct crypto_ahash *tfm, const u8 *key,
- unsigned int keylen, struct algo_data *ad)
- {
- struct sa_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
- int bs = crypto_shash_blocksize(ctx->shash);
- int ds = crypto_shash_digestsize(ctx->shash);
- int cmdl_len;
- struct sa_cmdl_cfg cfg;
- int err;
- if (keylen > bs) {
- err = sa_sham_shash_digest(ctx->shash,
- crypto_shash_get_flags(ctx->shash),
- key, keylen, ctx->authkey);
- if (err)
- return err;
- keylen = ds;
- } else {
- memcpy(ctx->authkey, key, keylen);
- }
- memset(ctx->authkey + keylen, 0, bs - keylen);
- memset(&cfg, 0, sizeof(cfg));
- cfg.enc1st = 0;
- cfg.aalg = ad->aalg_id;
- cfg.enc_eng_id = ad->enc_eng.eng_id;
- cfg.auth_eng_id = ad->auth_eng.eng_id;
- cfg.iv_size = 0;
- cfg.akey = ctx->authkey;
- cfg.akey_len = keylen;
- /* Setup Encryption Security Context & Command label template */
- if (sa_init_sc(&ctx->enc, NULL, 0, ctx->authkey, keylen, ad, 0,
- &ctx->enc.epib[1], true))
- goto badkey;
- cmdl_len = sa_format_cmdl_gen(&cfg,
- (u8 *)ctx->enc.cmdl,
- &ctx->enc.cmdl_upd_info);
- if (cmdl_len <= 0 || (cmdl_len > SA_MAX_CMDL_WORDS * sizeof(u32)))
- goto badkey;
- ctx->enc.cmdl_size = cmdl_len;
- kfree(ad);
- return 0;
- badkey:
- dev_err(sa_k3_dev, "%s: badkey\n", __func__);
- return -EINVAL;
- }
- static int sa_sham_sha1_setkey(struct crypto_ahash *tfm, const u8 *key,
- unsigned int keylen)
- {
- struct algo_data *ad = kzalloc(sizeof(*ad), GFP_KERNEL);
- ad->enc_eng.eng_id = SA_ENG_ID_NONE;
- ad->enc_eng.sc_size = SA_CTX_ENC_TYPE1_SZ;
- ad->auth_eng.eng_id = SA_ENG_ID_AM1;
- ad->auth_eng.sc_size = SA_CTX_AUTH_TYPE2_SZ;
- ad->mci_enc = NULL;
- ad->mci_dec = NULL;
- ad->inv_key = false;
- ad->keyed_mac = true;
- ad->ealg_id = SA_EALG_ID_NONE;
- ad->aalg_id = SA_AALG_ID_HMAC_SHA1;
- ad->hash_size = SHA1_DIGEST_SIZE;
- ad->auth_ctrl = 0x2;
- ad->prep_iopad = sa_hmac_sha1_get_pad;
- return sa_sham_setkey(tfm, key, keylen, ad);
- }
- static int sa_sham_sha256_setkey(struct crypto_ahash *tfm, const u8 *key,
- unsigned int keylen)
- {
- struct algo_data *ad = kzalloc(sizeof(*ad), GFP_KERNEL);
- ad->enc_eng.eng_id = SA_ENG_ID_NONE;
- ad->enc_eng.sc_size = SA_CTX_ENC_TYPE1_SZ;
- ad->auth_eng.eng_id = SA_ENG_ID_AM1;
- ad->auth_eng.sc_size = SA_CTX_AUTH_TYPE2_SZ;
- ad->mci_enc = NULL;
- ad->mci_dec = NULL;
- ad->inv_key = false;
- ad->keyed_mac = true;
- ad->ealg_id = SA_EALG_ID_NONE;
- ad->aalg_id = SA_AALG_ID_HMAC_SHA2_256;
- ad->hash_size = SHA256_DIGEST_SIZE;
- ad->auth_ctrl = 0x4;
- ad->prep_iopad = sa_hmac_sha256_get_pad;
- return sa_sham_setkey(tfm, key, keylen, ad);
- }
- static int sa_sham_cra_sha1_init(struct crypto_tfm *tfm)
- {
- return sa_sham_cra_init_alg(tfm, "sha1");
- }
- static int sa_sham_cra_sha256_init(struct crypto_tfm *tfm)
- {
- return sa_sham_cra_init_alg(tfm, "sha256");
- }
- static void sa_sham_cra_exit(struct crypto_tfm *tfm)
- {
- struct crypto_alg *alg = tfm->__crt_alg;
- struct sa_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
- struct sa_crypto_data *data = dev_get_drvdata(sa_k3_dev);
- dev_dbg(sa_k3_dev, "%s(0x%p) sc-ids(0x%x(0x%pad), 0x%x(0x%pad))\n",
- __func__, tfm, ctx->enc.sc_id, &ctx->enc.sc_phys,
- ctx->dec.sc_id, &ctx->dec.sc_phys);
- if ((alg->cra_flags & CRYPTO_ALG_TYPE_AHASH)
- == CRYPTO_ALG_TYPE_AHASH) {
- sa_free_ctx_info(&ctx->enc, data);
- }
- }
- static int sa_sham_update(struct ahash_request *req)
- {
- return 0;
- }
- static int sa_sham_final(struct ahash_request *req)
- {
- return sa_sham_digest(req);
- }
- static int sa_sham_finup(struct ahash_request *req)
- {
- return sa_sham_digest(req);
- }
- static struct sa_alg_tmpl sa_algs[] = {
- {.type = CRYPTO_ALG_TYPE_ABLKCIPHER,
- .alg.crypto = {
- .cra_name = "cbc(aes)",
- .cra_driver_name = "cbc-aes-sa2ul",
- .cra_priority = 30000,
- .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
- CRYPTO_ALG_KERN_DRIVER_ONLY |
- CRYPTO_ALG_ASYNC |
- CRYPTO_ALG_NEED_FALLBACK,
- .cra_blocksize = AES_BLOCK_SIZE,
- .cra_ctxsize = sizeof(struct sa_tfm_ctx),
- .cra_alignmask = 0,
- .cra_type = &crypto_ablkcipher_type,
- .cra_module = THIS_MODULE,
- .cra_init = sa_aes_cra_init,
- .cra_exit = sa_aes_cra_exit,
- .cra_u.ablkcipher = {
- .min_keysize = AES_MIN_KEY_SIZE,
- .max_keysize = AES_MAX_KEY_SIZE,
- .ivsize = AES_BLOCK_SIZE,
- .setkey = sa_aes_cbc_setkey,
- .encrypt = sa_aes_cbc_encrypt,
- .decrypt = sa_aes_cbc_decrypt,
- }
- }
- },
- { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
- .alg.crypto = {
- .cra_name = "ecb(aes)",
- .cra_driver_name = "ecb-aes-sa2ul",
- .cra_priority = 30000,
- .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
- CRYPTO_ALG_KERN_DRIVER_ONLY |
- CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
- .cra_blocksize = AES_BLOCK_SIZE,
- .cra_ctxsize = sizeof(struct sa_tfm_ctx),
- .cra_alignmask = 0,
- .cra_type = &crypto_ablkcipher_type,
- .cra_module = THIS_MODULE,
- .cra_init = sa_aes_cra_init,
- .cra_exit = sa_aes_cra_exit,
- .cra_u.ablkcipher = {
- .min_keysize = AES_MIN_KEY_SIZE,
- .max_keysize = AES_MAX_KEY_SIZE,
- .setkey = sa_aes_ecb_setkey,
- .encrypt = sa_aes_cbc_encrypt,
- .decrypt = sa_aes_cbc_decrypt,
- }
- }
- },
- { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
- .alg.crypto = {
- .cra_name = "cbc(des3_ede)",
- .cra_driver_name = "cbc-des3-sa2ul",
- .cra_priority = 30000,
- .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
- CRYPTO_ALG_KERN_DRIVER_ONLY |
- CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
- .cra_blocksize = DES_BLOCK_SIZE,
- .cra_ctxsize = sizeof(struct sa_tfm_ctx),
- .cra_alignmask = 0,
- .cra_type = &crypto_ablkcipher_type,
- .cra_module = THIS_MODULE,
- .cra_init = sa_aes_cra_init,
- .cra_exit = sa_aes_cra_exit,
- .cra_u.ablkcipher = {
- .min_keysize = 3 * DES_KEY_SIZE,
- .max_keysize = 3 * DES_KEY_SIZE,
- .ivsize = DES_BLOCK_SIZE,
- .setkey = sa_3des_cbc_setkey,
- .encrypt = sa_aes_cbc_encrypt,
- .decrypt = sa_aes_cbc_decrypt,
- }
- }
- },
- { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
- .alg.crypto = {
- .cra_name = "ecb(des3_ede)",
- .cra_driver_name = "ecb-des3-sa2ul",
- .cra_priority = 30000,
- .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
- CRYPTO_ALG_KERN_DRIVER_ONLY |
- CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
- .cra_blocksize = DES_BLOCK_SIZE,
- .cra_ctxsize = sizeof(struct sa_tfm_ctx),
- .cra_alignmask = 0,
- .cra_type = &crypto_ablkcipher_type,
- .cra_module = THIS_MODULE,
- .cra_init = sa_aes_cra_init,
- .cra_exit = sa_aes_cra_exit,
- .cra_u.ablkcipher = {
- .min_keysize = 3 * DES_KEY_SIZE,
- .max_keysize = 3 * DES_KEY_SIZE,
- .ivsize = DES_BLOCK_SIZE,
- .setkey = sa_3des_ecb_setkey,
- .encrypt = sa_aes_cbc_encrypt,
- .decrypt = sa_aes_cbc_decrypt,
- }
- }
- },
- /* AEAD algorithms */
- {.type = CRYPTO_ALG_TYPE_AEAD,
- .alg.aead = {
- .base = {
- .cra_name = "authenc(hmac(sha1),cbc(aes))",
- .cra_driver_name =
- "authenc(hmac(sha1),cbc(aes))-keystone-sa",
- .cra_blocksize = AES_BLOCK_SIZE,
- .cra_flags = CRYPTO_ALG_TYPE_AEAD |
- CRYPTO_ALG_KERN_DRIVER_ONLY |
- CRYPTO_ALG_ASYNC,
- .cra_ctxsize = sizeof(struct sa_tfm_ctx),
- .cra_module = THIS_MODULE,
- .cra_alignmask = 0,
- .cra_priority = 3000,
- },
- .ivsize = AES_BLOCK_SIZE,
- .maxauthsize = SHA1_DIGEST_SIZE,
- .init = sa_cra_init_aead,
- .exit = sa_exit_tfm_aead,
- .setkey = sa_aead_cbc_sha1_setkey,
- .encrypt = sa_aead_encrypt,
- .decrypt = sa_aead_decrypt,
- }
- },
- {.type = CRYPTO_ALG_TYPE_AEAD,
- .alg.aead = {
- .base = {
- .cra_name = "authenc(hmac(sha256),cbc(aes))",
- .cra_driver_name =
- "authenc(hmac(sha256),cbc(aes))-keystone-sa",
- .cra_blocksize = AES_BLOCK_SIZE,
- .cra_flags = CRYPTO_ALG_TYPE_AEAD |
- CRYPTO_ALG_KERN_DRIVER_ONLY |
- CRYPTO_ALG_ASYNC,
- .cra_ctxsize = sizeof(struct sa_tfm_ctx),
- .cra_module = THIS_MODULE,
- .cra_alignmask = 0,
- .cra_priority = 3000,
- },
- .ivsize = AES_BLOCK_SIZE,
- .maxauthsize = SHA256_DIGEST_SIZE,
- .init = sa_cra_init_aead,
- .exit = sa_exit_tfm_aead,
- .setkey = sa_aead_cbc_sha256_setkey,
- .encrypt = sa_aead_encrypt,
- .decrypt = sa_aead_decrypt,
- }
- },
- };
- static struct ahash_alg algs_sha[] = {
- {
- .init = sa_sham_init,
- .update = sa_sham_update,
- .final = sa_sham_final,
- .finup = sa_sham_finup,
- .digest = sa_sham_digest,
- .setkey = sa_sham_sha1_setkey,
- .halg.digestsize = SHA1_DIGEST_SIZE,
- .halg.statesize = 128,
- .halg.base = {
- .cra_name = "hmac(sha1)",
- .cra_driver_name = "sa-hmac-sha1",
- .cra_priority = 400,
- .cra_flags = CRYPTO_ALG_TYPE_AHASH |
- CRYPTO_ALG_ASYNC |
- CRYPTO_ALG_KERN_DRIVER_ONLY |
- CRYPTO_ALG_NEED_FALLBACK,
- .cra_blocksize = SHA1_BLOCK_SIZE,
- .cra_ctxsize = sizeof(struct sa_tfm_ctx),
- .cra_alignmask = SA_ALIGN_MASK,
- .cra_module = THIS_MODULE,
- .cra_init = sa_sham_cra_sha1_init,
- .cra_exit = sa_sham_cra_exit,
- }
- },
- {
- .init = sa_sham_init,
- .update = sa_sham_update,
- .final = sa_sham_final,
- .finup = sa_sham_finup,
- .digest = sa_sham_digest,
- .setkey = sa_sham_sha256_setkey,
- .halg.digestsize = SHA256_DIGEST_SIZE,
- .halg.statesize = 128,
- .halg.base = {
- .cra_name = "hmac(sha256)",
- .cra_driver_name = "sa-hmac-sha256",
- .cra_priority = 400,
- .cra_flags = CRYPTO_ALG_TYPE_AHASH |
- CRYPTO_ALG_ASYNC |
- CRYPTO_ALG_KERN_DRIVER_ONLY |
- CRYPTO_ALG_NEED_FALLBACK,
- .cra_blocksize = SHA256_BLOCK_SIZE,
- .cra_ctxsize = sizeof(struct sa_tfm_ctx),
- .cra_alignmask = SA_ALIGN_MASK,
- .cra_module = THIS_MODULE,
- .cra_init = sa_sham_cra_sha256_init,
- .cra_exit = sa_sham_cra_exit,
- }
- },
- };
- /* Register the algorithms in crypto framework */
- void sa_register_algos(const struct device *dev)
- {
- char *alg_name;
- u32 type;
- int i, err, num_algs = ARRAY_SIZE(sa_algs);
- for (i = 0; i < num_algs; i++) {
- type = sa_algs[i].type;
- if (type == CRYPTO_ALG_TYPE_AEAD) {
- alg_name = sa_algs[i].alg.aead.base.cra_name;
- err = crypto_register_aead(&sa_algs[i].alg.aead);
- } else if (type == CRYPTO_ALG_TYPE_ABLKCIPHER) {
- alg_name = sa_algs[i].alg.crypto.cra_name;
- err = crypto_register_alg(&sa_algs[i].alg.crypto);
- } else {
- dev_err(dev,
- "un-supported crypto algorithm (%d)",
- sa_algs[i].type);
- continue;
- }
- if (err)
- dev_err(dev, "Failed to register '%s'\n", alg_name);
- else
- sa_algs[i].registered = 1;
- }
- num_algs = ARRAY_SIZE(algs_sha);
- for (i = 0; i < num_algs; i++) {
- alg_name = algs_sha[i].halg.base.cra_name;
- err = crypto_register_ahash(&algs_sha[i]);
- if (err)
- dev_err(dev, "Failed to register '%s'\n",
- alg_name);
- }
- }
- /* Unregister the algorithms in crypto framework */
- void sa_unregister_algos(const struct device *dev)
- {
- char *alg_name;
- u32 type;
- int i, err = 0, num_algs = ARRAY_SIZE(sa_algs);
- for (i = 0; i < num_algs; i++) {
- type = sa_algs[i].type;
- if (type == CRYPTO_ALG_TYPE_AEAD) {
- alg_name = sa_algs[i].alg.aead.base.cra_name;
- crypto_unregister_aead(&sa_algs[i].alg.aead);
- } else {
- alg_name = sa_algs[i].alg.crypto.cra_name;
- err = crypto_unregister_alg(&sa_algs[i].alg.crypto);
- }
- sa_algs[i].registered = 0;
- }
- num_algs = ARRAY_SIZE(algs_sha);
- for (i = 0; i < num_algs; i++) {
- alg_name = algs_sha[i].halg.base.cra_name;
- err = crypto_unregister_ahash(&algs_sha[i]);
- if (err)
- dev_err(dev, "Failed to register '%s'\n",
- alg_name);
- }
- }
- static int sa_init_mem(struct sa_crypto_data *dev_data)
- {
- struct device *dev = &dev_data->pdev->dev;
- /* Setup dma pool for security context buffers */
- dev_data->sc_pool = dma_pool_create("keystone-sc", dev,
- SA_CTX_MAX_SZ, 64, 0);
- if (!dev_data->sc_pool) {
- dev_err(dev, "Failed to create dma pool");
- return -ENOMEM;
- }
- return 0;
- }
- static int sa_dma_init(struct sa_crypto_data *dd)
- {
- int ret;
- struct dma_slave_config cfg;
- dd->dma_rx1 = NULL;
- dd->dma_tx = NULL;
- dd->dma_rx2 = NULL;
- ret = dma_coerce_mask_and_coherent(dd->dev, DMA_BIT_MASK(48));
- if (ret)
- return ret;
- dd->dma_rx1 = dma_request_chan(dd->dev, "rx1");
- if (IS_ERR(dd->dma_rx1)) {
- if (PTR_ERR(dd->dma_rx1) != -EPROBE_DEFER)
- dev_err(dd->dev, "Unable to request rx1 DMA channel\n");
- return PTR_ERR(dd->dma_rx1);
- }
- dd->dma_rx2 = dma_request_chan(dd->dev, "rx2");
- if (IS_ERR(dd->dma_rx2)) {
- dma_release_channel(dd->dma_rx1);
- if (PTR_ERR(dd->dma_rx1) != -EPROBE_DEFER)
- dev_err(dd->dev, "Unable to request rx2 DMA channel\n");
- return PTR_ERR(dd->dma_rx2);
- }
- dd->dma_tx = dma_request_chan(dd->dev, "tx");
- if (IS_ERR(dd->dma_tx)) {
- if (PTR_ERR(dd->dma_rx1) != -EPROBE_DEFER)
- dev_err(dd->dev, "Unable to request tx DMA channel\n");
- ret = PTR_ERR(dd->dma_tx);
- goto err_dma_tx;
- }
- memzero_explicit(&cfg, sizeof(cfg));
- cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
- cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
- cfg.src_maxburst = 4;
- cfg.dst_maxburst = 4;
- ret = dmaengine_slave_config(dd->dma_rx1, &cfg);
- if (ret) {
- dev_err(dd->dev, "can't configure IN dmaengine slave: %d\n",
- ret);
- return ret;
- }
- ret = dmaengine_slave_config(dd->dma_rx2, &cfg);
- if (ret) {
- dev_err(dd->dev, "can't configure IN dmaengine slave: %d\n",
- ret);
- return ret;
- }
- ret = dmaengine_slave_config(dd->dma_tx, &cfg);
- if (ret) {
- dev_err(dd->dev, "can't configure OUT dmaengine slave: %d\n",
- ret);
- return ret;
- }
- return 0;
- err_dma_tx:
- dma_release_channel(dd->dma_rx1);
- dma_release_channel(dd->dma_rx2);
- return ret;
- }
- static int sa_ul_probe(struct platform_device *pdev)
- {
- struct device *dev = &pdev->dev;
- struct resource *res;
- static void __iomem *saul_base;
- struct sa_crypto_data *dev_data;
- u32 val;
- int ret;
- dev_data = devm_kzalloc(dev, sizeof(*dev_data), GFP_KERNEL);
- if (!dev_data)
- return -ENOMEM;
- sa_k3_dev = dev;
- dev_data->dev = dev;
- dev_data->pdev = pdev;
- platform_set_drvdata(pdev, dev_data);
- dev_set_drvdata(sa_k3_dev, dev_data);
- sa_init_mem(dev_data);
- ret = sa_dma_init(dev_data);
- if (ret)
- return ret;
- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
- saul_base = devm_ioremap_resource(dev, res);
- val = SA_EEC_ENCSS_EN | SA_EEC_AUTHSS_EN | SA_EEC_CTXCACH_EN |
- SA_EEC_CPPI_PORT_IN_EN | SA_EEC_CPPI_PORT_OUT_EN | SA_EEC_TRNG_EN;
- writel_relaxed(val, saul_base + SA_ENGINE_ENABLE_CONTROL);
- sa_register_algos(dev);
- return 0;
- }
- static int sa_ul_remove(struct platform_device *pdev)
- {
- struct sa_crypto_data *dev_data = platform_get_drvdata(pdev);
- sa_unregister_algos(&pdev->dev);
- dma_release_channel(dev_data->dma_rx2);
- dma_release_channel(dev_data->dma_rx1);
- dma_release_channel(dev_data->dma_tx);
- dma_pool_destroy(dev_data->sc_pool);
- platform_set_drvdata(pdev, NULL);
- return 0;
- }
- static const struct of_device_id of_match[] = {
- {.compatible = "ti,sa2ul-crypto",},
- {},
- };
- MODULE_DEVICE_TABLE(of, of_match);
- static struct platform_driver sa_ul_driver = {
- .probe = sa_ul_probe,
- .remove = sa_ul_remove,
- .driver = {
- .name = "saul-crypto",
- .of_match_table = of_match,
- },
- };
- module_platform_driver(sa_ul_driver);
- MODULE_LICENSE("GPL v2");
|