From patchwork Fri Jun 3 17:59:11 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikhil Agarwal X-Patchwork-Id: 69244 Delivered-To: patch@linaro.org Received: by 10.140.106.246 with SMTP id e109csp238820qgf; Fri, 3 Jun 2016 05:30:46 -0700 (PDT) X-Received: by 10.140.91.18 with SMTP id y18mr2788708qgd.81.1464957046124; Fri, 03 Jun 2016 05:30:46 -0700 (PDT) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id a102si2215674qgf.100.2016.06.03.05.30.45; Fri, 03 Jun 2016 05:30:46 -0700 (PDT) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=pass (p=NONE dis=NONE) header.from=linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id A1F6E617B2; Fri, 3 Jun 2016 12:30:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: * X-Spam-Status: No, score=1.1 required=5.0 tests=BAYES_00, DATE_IN_FUTURE_03_06, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 9886A6169F; Fri, 3 Jun 2016 12:30:39 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 0A0DC61703; Fri, 3 Jun 2016 12:30:36 +0000 (UTC) Received: from na01-by2-obe.outbound.protection.outlook.com (mail-by2on0057.outbound.protection.outlook.com [207.46.100.57]) by lists.linaro.org (Postfix) with ESMTPS id DDFD461703 for ; Fri, 3 Jun 2016 12:29:45 +0000 (UTC) Received: from BN3PR0301CA0080.namprd03.prod.outlook.com (10.160.152.176) by DM2PR03MB383.namprd03.prod.outlook.com (10.141.55.17) with Microsoft SMTP Server (TLS) id 15.1.501.7; Fri, 3 Jun 2016 12:29:44 +0000 Received: from BL2FFO11FD041.protection.gbl (2a01:111:f400:7c09::181) by BN3PR0301CA0080.outlook.office365.com (2a01:111:e400:401e::48) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.506.9 via Frontend Transport; Fri, 3 Jun 2016 12:29:45 +0000 Received-SPF: SoftFail (protection.outlook.com: domain of transitioning linaro.org discourages use of 192.88.158.2 as permitted sender) Received: from az84smr01.freescale.net (192.88.158.2) by BL2FFO11FD041.mail.protection.outlook.com (10.173.161.137) with Microsoft SMTP Server (TLS) id 15.1.497.8 via Frontend Transport; Fri, 3 Jun 2016 12:29:44 +0000 Received: from netperf2.ap.freescale.net ([10.232.133.164]) by az84smr01.freescale.net (8.14.3/8.14.0) with ESMTP id u53CTaKm012936 for ; Fri, 3 Jun 2016 05:29:37 -0700 From: Nikhil Agarwal To: Date: Fri, 3 Jun 2016 23:29:11 +0530 Message-ID: <1464976751-11984-1-git-send-email-nikhil.agarwal@linaro.org> X-Mailer: git-send-email 2.8.2 X-EOPAttributedMessage: 0 X-Matching-Connectors: 131094305843789385; (91ab9b29-cfa4-454e-5278-08d120cd25b8); () X-Forefront-Antispam-Report: CIP:192.88.158.2; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(2980300002)(189002)(199003)(9170700003)(77096005)(86362001)(19580395003)(92566002)(2906002)(8676002)(107886002)(2351001)(8936002)(81166006)(47776003)(450100001)(5003940100001)(189998001)(110136002)(33646002)(229853001)(105596002)(106466001)(586003)(50466002)(5008740100001)(48376002)(50226002)(50986999)(6806005)(87936001)(104016004)(36756003); DIR:OUT; SFP:1101; SCL:1; SRVR:DM2PR03MB383; H:az84smr01.freescale.net; FPR:; SPF:SoftFail; MLV:sfv; MX:1; A:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BL2FFO11FD041; 1:LUKyWa5JiyE6pVFxX3/sKpRp4o6jva8Ii1MVFiUA5NdefpOVgl2Wzv7YMrOm5PCPTzgIQ4l2Utx1FjSIvUooUDNaDcExnYKRck678Cb0rR699dQ23fRTAWUeZ7Z1bWEvuIvnQXGSks0aayFSRQ9yDow7VGsaf+nvNMXaVFxT2E+HGo2av3oy2QtVlITkyov2T02iNzNMJLGQjz2WvbdKZIuB1i5mmTXtU5y95B4y28CYbNmMycUYBKFbFB3PYr1UpaPK2ZjDtNuDRlbVxJN/W0NGrZD+JOeM9DGIxB61x2DQFw8V+feWLI/N8L3lYV55F8bGi2oyXzgNmS8rqtn3bCFMXkSl2PFAnqYAO6yNO7/+oY14wA2p4vXr2iNjP0u5DnTOxd0AMt26vwE1kXDhWnYDO+1wVj4m7NBzCIStm5YHs0ZZ19G4Fh82vGS0ipv3+M0ELNHFuPEglGfJvxIzHQCbVwh4xUfK5F65VXveybdsqevRGKQuT2svNv68pkWtebbJje2llfcOg8CVfunSTw== MIME-Version: 1.0 X-MS-Office365-Filtering-Correlation-Id: fc6ff765-0807-40f0-dce8-08d38baabe59 X-Microsoft-Exchange-Diagnostics: 1; DM2PR03MB383; 2:61UdiqdPfMfTbj2q4H/+R6jFnxatfIdfgMrARgxhGrrTask+Yr0oN/PeYCUV+Xaw2md3X/G05iWI4q+1WQD4xYcOPL4ZNhmb+A34JPcwX7Q4yQTl43to81VUbPgOwIaaLnKhS0mnk3QssObz9PXge8sSGN8gPgyiFJprixJtTt7Vf6NyLdczrZMof498pjn7; 3:8syp4c9fVv4um3FfXbycXDHwhxMud0EoIxV8YllFrjQRzbVPIZ6k1Q7425plJfld8EitaSezjmHLGhML5YFu00sSbpoA9fHXGzp+GCmK3o0C3I+wlKALfSu6DaMOcVVUSuLcOeiOiJFGNIhJ1OJjeLvblxDfP+s3U5D7mssuRaIVx7WTysQkrqVAi5jz0e/bYfStvk0iKTTDpFutp9r6L/j84Vj4NrdSgVE1yibBz40= X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:DM2PR03MB383; X-Microsoft-Exchange-Diagnostics: 1; DM2PR03MB383; 25:fdbgt29inz3hAKWvTD4jUHI1LYMIt07P5K+nixFXCiKOC48F37RZH3pIhLEI3v0zZpg0zimjltT+4bBQdGoyq+huZCvOnEEaskffVnBMC0b6XQ0yoVRvJn57EAxGGF6CpnU9Ct7HUMS0KzBV1EWnaWWJgQzAuXVkx/NdETyYSKGOuQ5Q3qr3CwbLiQJi0CyUJdQak7fohZg6crfNUI5XBVYkDIB6LArkxYcjLJDJHxGYcb2cnWzyPqUKPJzcTbP/5tRtsbmLdgxd9lQNPqCkklnmQBkvPNv2jw+gZ0qMNK9XrlGuUVC68Dj5uSsycnyrYvGaYtreWlPjR0fEHmUh5BJ+H6nAbrzbZsJJdogH+oEHYfiepM9F/DWWNLUeQpdW+r743pOt0sjienj6X5FPIMNEAmIxozKRTeGgP8PsUfRu5REH3wOH3GgQUHesDkwa4E0saOHcjQrYv7TjgsYhBaM7i03uQr88Sc8aLkCYhfY2JyyxgpLGHmmIR9K/yj4naQRHywMcOPL6dxJViL0z/s10zwCbeqjeZsntRAgVjkXb1RyoXPH29BmoFLGDEWTldO0TSowAkAETndvwuRZsidDI0fLF2XuGOSju7YZ5kTFqzsrd9hSToAqVoEXgeT2nOmD/1jwc/xPVWLT0Lsd4PwN/fZUA1Qg4yWUKcqJPPMT5F01MkclE9Grl3xUUuggL3YhVSdbD1ia/T71w8lfctQ== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(13023025)(13024025)(13015025)(13018025)(13017025)(5005006)(8121501046)(10201501046)(3002001)(6055026); SRVR:DM2PR03MB383; BCL:0; PCL:0; RULEID:(400006); SRVR:DM2PR03MB383; X-Microsoft-Exchange-Diagnostics: 1; DM2PR03MB383; 4:wRgz/TdViwq63cFJFro7NzhZ5h0UkZMhZTuQBWOmgfnt1MmkLn6kgFjgiNkyOUFbLqx/wqJvTPjGIYWPBS5gSVuZ721vepkMgpmbwkxbLXDuKE+yr19XvCXSC1UWbaQZpDcxe+EzqegU4VbhYX1UIStN7TtE/ieXyNSC76SMMvgbrr9AibDqPRwadAQ+aRT1uX43PcVXfmTHH9s4BID7ktjty6vPjxbn8B8WUJCKiY7OLVr/RLFEUgg6jhMAWJjTiSsMNr3UOp053bfjMgaS0So95/9+XClA/fLyi0NDOBRHYBcxpD+zih4xdu/n2OK6mDpH9NWwfe/qDTvekKVyYrHBzO5KL7F5M6n/grYfWmvceZrps+xclxVAl3Llg7fG48XX6F0pGm8C8Zm7owRwvpxw5YvDVTS6fqPJxHGlzqik4a1AucgPNUIVhD2Y/aw9QmJMFii0C77OK0OpNqZy/LiOxHRUyl4to29r/pqu32s= X-Forefront-PRVS: 0962D394D2 X-Microsoft-Exchange-Diagnostics: 1; DM2PR03MB383; 23:acOvvIqQTRMdqVcngP7UAiWWf4MfrD2KwOW2tu7Znz727rPD8GPQjE3XND0Wgped1rCY626WSQMq1ObojWS0drTi0zHipJ0Z5lJvpZOeb/IRQDo+iz3/nCv2TiWjNI8fSyVbSYHUc28Mzr7hl0LZj4Zf/KpLicpz5Euhyx1iNsLVATLS9OLZS0qR3t/7wpXrxjXOUht5ykErbMVDCb9fibOGIk8/bZYbTAXSliZTUYn4T0wcfur+7dQ/pFChU6qbRXJRgx4Ou7fkJWDSIlJxy7AMzvPipOBVf+WUySIitPt/KjWPL/2+jk1o60SEYXkJpzNKutlEIc+e6NqQgj7Pp8rvBD6Bpez2euMqE9luP+Jdx7qd7qtZs1N3wV3qF5Kwmeh0U6BzCww+MPFDDlx6X2b4S0DIKsqw3mGd+aMo/KxGT9J1e8AjfdEHDkRJv/YjSOxXV3fBjhB58zjWjehr6IMlBqPlZHoWvqjCfe9xs+RgNUj/f6+thjg+sQcdbt4YWBQmaJOYcIsryKBm3u/tEXNgksJ50xUDEYR7Fo7jKcVIVkGR5iCOs3UCrFAkhbypdumgvDmb54VzsX5ulxcfKzGAd0kAdUQXF6oSy+0hgF9PHK3+jxYrvIowOq3gsaY6kLah5+s5IrtdUna1n18BaA/b0r3XR6DFhyc+fp0GlTd8TKj+tKTTvYj8vFL15YSfmZ0lRr7Gqi5XZqRnxN5PpPpQM1BtYkhyBHe8Ta4i0UVb/jxljRZsLh5gRksSg7Cshx/L4WfACutl7HmKn7lXeKWxsS5plqVGdL/jCHx1d4jirdFw4ecmgXP7SVZNRfdx4PRxePUZTxKc+ougQVz6WfOUDNtKhiYj1E3dgGpz3cVU38q/ADYJ6MbibbVdd/+gSLIJxZXHAmhpFQFLtyxa6w== X-Microsoft-Exchange-Diagnostics: 1; DM2PR03MB383; 5:lCSIN2p9xivsOUuE4bDHp+ux8ZnPJ37kz1sD9NcVEbbkTr053/SDOnizsRjBbpm4chTb7Y03nZHDFi21kSuZaDSQBC9RF6vWAHuhIZMCTs0JW7JEL/R/gfD5WpSKu+ZmdrfTT6tNGuPVvWiCJV/4ckEHgiRpuDqugwiXSAEZpMs=; 24:86crAi/gCtJkyqAtJin3hZySbkszUmiuZNBgCf8DbXd27gl9vtg/gMNwbUOqLNEi8QfN9bWt0sy4KJeNeJLJgRR705vJ/br9c8/KtWXRV6A=; 7:mwTphxng7ZML3/wYMmhhjItHqBNBVJvl5+ZcgZZA2InIW6ch5DrLeWTqHisuQF0knlb+BqbKiZnXvORHXrjaYLJpkbHfvF9oIv9UqSieSruhraZWjUTRjMfGwzlS/b2od329JsbUN3F3oPCP8PvUeRNlkRVy8HWxgdWcPZ5pMA+49qeL4NYN2gTKPcXr33rVHk7VhhRk3+aT5MZUwQ/m6VuPT2yb1zGrnKQL+wJJ3LM= SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Jun 2016 12:29:44.0201 (UTC) X-MS-Exchange-CrossTenant-Id: 5afe0b00-7697-4969-b663-5eab37d5f47e X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=5afe0b00-7697-4969-b663-5eab37d5f47e; Ip=[192.88.158.2]; Helo=[az84smr01.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM2PR03MB383 Subject: [lng-odp] [RFC OPNESSL-ODP 2/2] Adding Async ODP engine for AES cipher X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" --- engine/eng_odp.c | 375 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 375 insertions(+) create mode 100644 engine/eng_odp.c diff --git a/engine/eng_odp.c b/engine/eng_odp.c new file mode 100644 index 0000000..3340649 --- /dev/null +++ b/engine/eng_odp.c @@ -0,0 +1,375 @@ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DUMMY_CHAR 'X' +odp_pool_t pool; +int num_queue = 8; +odp_queue_t out_queue[8]; +OSSL_ASYNC_FD pipefds[2] = {0, 0}; + +/* Engine Id and Name */ +static const char *engine_odp_id = "libsslodp"; +static const char *engine_odp_name = "ODP based engine"; + +/* Engine Lifetime functions */ +static int ossl_odp_destroy(ENGINE *e); +static int ossl_odp_init(ENGINE *e); +static int ossl_odp_finish(ENGINE *e); + +/* Set up digests. Just SHA1 for now */ +static int ossl_odp_digests(ENGINE *e, const EVP_MD **digest, + const int **nids, int nid); + +/* + * Holds the EVP_MD object for sha1 in this engine. Set up once only during + * engine bind and can then be reused many times. + */ +static void destroy_digests(void) +{ + /*Nothing for now*/ +} + +static int ossl_odp_digest_nids(const int **nids) +{ + int digest_nids[2] = { 0, 0 }; + + *nids = digest_nids; + return 1; +} + +/* AES */ + +static int ossl_odp_aes128_init_key(EVP_CIPHER_CTX *ctx, + const unsigned char *key, const unsigned char *iv, int enc); +static int ossl_odp_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl); +static int ossl_odp_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx); + +struct ossl_odp_ctx { + odp_crypto_session_t session; +}; + +/* + * Holds the EVP_CIPHER object for aes_128_cbc in this engine. Set up once only + * during engine bind and can then be reused many times. + */ +static EVP_CIPHER *_hidden_aes_128_cbc; +static const EVP_CIPHER *ossl_odp_aes_128_cbc(void) +{ + return _hidden_aes_128_cbc; +} + +static void destroy_ciphers(void) +{ + EVP_CIPHER_meth_free(_hidden_aes_128_cbc); + _hidden_aes_128_cbc = NULL; +} + +static int ossl_odp_ciphers(ENGINE *e, const EVP_CIPHER **cipher, + const int **nids, int nid); + +static int ossl_odp_cipher_nids[] = { + NID_aes_128_cbc, + 0 +}; + +static int bind_odp(ENGINE *e) +{ + if (!ENGINE_set_id(e, engine_odp_id) + || !ENGINE_set_name(e, engine_odp_name) + || !ENGINE_set_ciphers(e, ossl_odp_ciphers) + || !ENGINE_set_destroy_function(e, ossl_odp_destroy) + || !ENGINE_set_init_function(e, ossl_odp_init) + || !ENGINE_set_finish_function(e, ossl_odp_finish)) { + return 0; + } + + _hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc, + 16 /* block size */, + 16 /* key len */); + if (_hidden_aes_128_cbc == NULL + || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc, 16) + || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc, + EVP_CIPH_FLAG_DEFAULT_ASN1 + | EVP_CIPH_CBC_MODE) + || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc, + ossl_odp_aes128_init_key) + || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc, + ossl_odp_aes128_cbc_cipher) + || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc, + ossl_odp_aes128_cbc_cleanup) + || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc, + sizeof(struct ossl_odp_ctx))) { + EVP_CIPHER_meth_free(_hidden_aes_128_cbc); + _hidden_aes_128_cbc = NULL; + } + return 1; +} + +static int bind_helper(ENGINE *e, const char *id) +{ + if (id && (strcmp(id, engine_odp_id) != 0)) + return 0; + if (!bind_odp(e)) + return 0; + return 1; +} + +IMPLEMENT_DYNAMIC_CHECK_FN() +IMPLEMENT_DYNAMIC_BIND_FN(bind_helper) + +static int ossl_odp_init(ENGINE *e) +{ + odp_queue_param_t qparam; + odp_pool_param_t params; + int i; + char buf = DUMMY_CHAR; + + if (0 != odp_init_global(NULL, NULL)) { + printf("error: odp_init_global() failed.\n"); + return -1; + } + if (0 != odp_init_local(ODP_THREAD_WORKER)) { + printf("error: odp_init_local() failed.\n"); + return -1; + } + + memset(¶ms, 0, sizeof(params)); + params.pkt.seg_len = 20480; + params.pkt.len = 20480; + params.pkt.num = 4096; + params.type = ODP_POOL_PACKET; + + pool = odp_pool_create("packet_pool", ¶ms); + + if (ODP_POOL_INVALID == pool) { + printf("Packet pool creation failed.\n"); + return -1; + } + odp_queue_param_init(&qparam); + qparam.sched.prio = ODP_SCHED_PRIO_HIGHEST; + qparam.sched.sync = ODP_SCHED_SYNC_ATOMIC; + qparam.sched.group = ODP_SCHED_GROUP_ALL; + for (i = 0; i < num_queue; i++) { + out_queue[i] = odp_queue_create("crypto-out", + ODP_QUEUE_TYPE_SCHED, &qparam); + if (ODP_QUEUE_INVALID == out_queue[i]) { + printf("Crypto outq creation failed.\n"); + return -1; + } + } + if (pipe(pipefds) != 0) + return; + + write(pipefds[1], &buf, 1); + return 1; +} + + +static int ossl_odp_finish(ENGINE *e) +{ + int i; + + for (i = 0; i < num_queue; i++) + odp_queue_destroy(out_queue[i]); + + odp_pool_destroy(pool); + odp_term_local(); + odp_term_global(); + return 1; +} + + +static int ossl_odp_destroy(ENGINE *e) +{ + destroy_ciphers(); + return 1; +} + +static int ossl_odp_ciphers(ENGINE *e, const EVP_CIPHER **cipher, + const int **nids, int nid) +{ + int ok = 1; + + if (cipher == NULL) { + /* We are returning a list of supported nids */ + *nids = ossl_odp_cipher_nids; + return (sizeof(ossl_odp_cipher_nids) - + 1) / sizeof(ossl_odp_cipher_nids[0]); + } + /* We are being asked for a specific cipher */ + switch (nid) { + case NID_aes_128_cbc: + *cipher = ossl_odp_aes_128_cbc(); + break; + default: + ok = 0; + *cipher = NULL; + break; + } + return ok; +} + +static void wait_cleanup(ASYNC_WAIT_CTX *ctx, const void *key, + OSSL_ASYNC_FD readfd, void *pvwritefd) +{ + /*Nothing todo for now*/ +} + +/* Cipher helper functions */ +static int ossl_odp_cipher_init_key_helper(EVP_CIPHER_CTX *ctx, + const unsigned char *key, + const unsigned char *iv, int enc, + const EVP_CIPHER *cipher) +{ + int ret; + enum odp_crypto_ses_create_err status; + odp_crypto_session_params_t ses_params; + odp_crypto_key_t cipher_key = { .data = NULL, .length = 0 }, + auth_key = { .data = NULL, .length = 0 }; + odp_crypto_iv_t ses_iv; + static int next; + + if (next == num_queue) + next = 0; + + + struct ossl_odp_ctx *odp_ctx = + (struct ossl_odp_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx); + + /* Create a crypto session */ + memset(&ses_params, 0, sizeof(ses_params)); + ses_params.op = (enc == 1) ? ODP_CRYPTO_OP_ENCODE:ODP_CRYPTO_OP_DECODE; + ses_params.auth_cipher_text = false; + ses_params.pref_mode = ODP_CRYPTO_ASYNC; + ses_params.cipher_alg = ODP_CIPHER_ALG_AES128_CBC; + ses_params.auth_alg = ODP_AUTH_ALG_NULL; + cipher_key.data = (unsigned char *)key; + cipher_key.length = 16; + ses_iv.data = (unsigned char *)iv; + ses_iv.length = 16; + ses_params.cipher_key = cipher_key; + ses_params.iv = ses_iv; + ses_params.auth_key = auth_key; + ses_params.compl_queue = out_queue[next]; + ses_params.output_pool = pool; + + ret = odp_crypto_session_create(&ses_params, + &(odp_ctx->session), &status); + + next++; + if (ODP_CRYPTO_SES_CREATE_ERR_NONE != status) + return 0; + return ret; +} + +static int ossl_odp_cipher_helper(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl, + const EVP_CIPHER *cipher) +{ + int ret = 1; + odp_crypto_op_params_t params; + odp_packet_t pkt; + odp_bool_t posted; + odp_event_t event; + odp_crypto_compl_t compl_event; + ASYNC_WAIT_CTX *waitctx; + OSSL_ASYNC_FD *fd; + char completed = 0; + odp_crypto_op_result_t result; + ASYNC_JOB *job = ASYNC_get_current_job(); + struct ossl_odp_ctx *odp_ctx = + (struct ossl_odp_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx); + + if (job == NULL) { + printf("\nSYNC operation not yet supported\n"); + return; + } + + waitctx = ASYNC_get_wait_ctx(job); + + if (!ASYNC_WAIT_CTX_get_fd(waitctx, engine_odp_id, &pipefds[0], + (void **)&fd)) { + ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_odp_id, pipefds[0], + NULL, wait_cleanup); + } + pkt = odp_packet_alloc(pool, inl); + memcpy(odp_packet_data(pkt), in, inl); + memset(¶ms, 0, sizeof(params)); + params.ctx = NULL; + params.session = odp_ctx->session; + params.pkt = pkt; + params.ctx = &completed; + params.out_pkt = pkt; + params.cipher_range.offset = 0; + params.cipher_range.length = inl; + params.override_iv_ptr = NULL; + if (odp_crypto_operation(¶ms, + &posted, + &result)) { + abort(); + } + if (posted) { + while (!completed) { + ASYNC_pause_job(); + /* Poll completion queue for results */ + event = odp_schedule(NULL, ODP_SCHED_NO_WAIT); + + while (event != ODP_EVENT_INVALID) { + compl_event = + odp_crypto_compl_from_event(event); + odp_crypto_compl_result(compl_event, &result); + odp_crypto_compl_free(compl_event); + *((char *)result.ctx) = 1; + + if (!result.ok) + return; + event = odp_schedule(NULL, ODP_SCHED_NO_WAIT); + } + } + } + memcpy(out, odp_packet_data(pkt), inl); + odp_packet_free(pkt); + return ret; +} + +/* + * AES128 CBC Implementation + */ + +static int ossl_odp_aes128_init_key(EVP_CIPHER_CTX *ctx, + const unsigned char *key, const unsigned char *iv, int enc) +{ + return ossl_odp_cipher_init_key_helper(ctx, key, iv, + enc, EVP_aes_128_cbc()); +} + +static int ossl_odp_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, + const unsigned char *in, size_t inl) +{ + return ossl_odp_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc()); +} + +static int ossl_odp_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx) +{ + struct ossl_odp_ctx *odp_ctx = + (struct ossl_odp_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx); + + odp_crypto_session_destroy(odp_ctx->session); + + return 1; +} +