diff options
author | Nicolas Morey-Chaisemartin <nmorey@kalray.eu> | 2015-11-10 16:31:45 +0100 |
---|---|---|
committer | Maxim Uvarov <maxim.uvarov@linaro.org> | 2015-11-25 17:23:54 +0300 |
commit | 6379f5de64e7a6966058b7568e227d0db883ad0e (patch) | |
tree | 8065c1dd6202777696e51bf24851adc3137f0c77 | |
parent | b0e764fa6d3c80652e5af027256e2db323d1b1ee (diff) |
api: crypto: add HMAC-SHA-256-128 support
Signed-off-by: Nicolas Morey-Chaisemartin <nmorey@kalray.eu>
Reviewed-by: Petri Savolainen <petri.savolainen@nokia.com>
Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
-rw-r--r-- | include/odp/api/crypto.h | 2 | ||||
-rw-r--r-- | platform/linux-generic/include/odp_crypto_internal.h | 4 | ||||
-rw-r--r-- | platform/linux-generic/odp_crypto.c | 89 |
3 files changed, 95 insertions, 0 deletions
diff --git a/include/odp/api/crypto.h b/include/odp/api/crypto.h index 28d345be9..47c3fd6ac 100644 --- a/include/odp/api/crypto.h +++ b/include/odp/api/crypto.h @@ -78,6 +78,8 @@ typedef enum { ODP_AUTH_ALG_NULL, /** HMAC-MD5 with 96 bit key */ ODP_AUTH_ALG_MD5_96, + /** SHA256 with 128 bit key */ + ODP_AUTH_ALG_SHA256_128, } odp_auth_alg_t; /** diff --git a/platform/linux-generic/include/odp_crypto_internal.h b/platform/linux-generic/include/odp_crypto_internal.h index 23fec04d6..10bcfd45c 100644 --- a/platform/linux-generic/include/odp_crypto_internal.h +++ b/platform/linux-generic/include/odp_crypto_internal.h @@ -56,6 +56,10 @@ struct odp_crypto_generic_session { uint8_t key[16]; uint32_t bytes; } md5; + struct { + uint8_t key[32]; + uint32_t bytes; + } sha256; } data; crypto_func_t func; } auth; diff --git a/platform/linux-generic/odp_crypto.c b/platform/linux-generic/odp_crypto.c index e3bc557d9..ed3d14c7e 100644 --- a/platform/linux-generic/odp_crypto.c +++ b/platform/linux-generic/odp_crypto.c @@ -140,6 +140,72 @@ odp_crypto_alg_err_t md5_check(odp_crypto_op_params_t *params, } static +odp_crypto_alg_err_t sha256_gen(odp_crypto_op_params_t *params, + odp_crypto_generic_session_t *session) +{ + uint8_t *data = odp_packet_data(params->out_pkt); + uint8_t *icv = data; + uint32_t len = params->auth_range.length; + uint8_t hash[EVP_MAX_MD_SIZE]; + + /* Adjust pointer for beginning of area to auth */ + data += params->auth_range.offset; + icv += params->hash_result_offset; + + /* Hash it */ + HMAC(EVP_sha256(), + session->auth.data.sha256.key, + 32, + data, + len, + hash, + NULL); + + /* Copy to the output location */ + memcpy(icv, hash, session->auth.data.sha256.bytes); + + return ODP_CRYPTO_ALG_ERR_NONE; +} + +static +odp_crypto_alg_err_t sha256_check(odp_crypto_op_params_t *params, + odp_crypto_generic_session_t *session) +{ + uint8_t *data = odp_packet_data(params->out_pkt); + uint8_t *icv = data; + uint32_t len = params->auth_range.length; + uint32_t bytes = session->auth.data.sha256.bytes; + uint8_t hash_in[EVP_MAX_MD_SIZE]; + uint8_t hash_out[EVP_MAX_MD_SIZE]; + + /* Adjust pointer for beginning of area to auth */ + data += params->auth_range.offset; + icv += params->hash_result_offset; + + /* Copy current value out and clear it before authentication */ + memset(hash_in, 0, sizeof(hash_in)); + memcpy(hash_in, icv, bytes); + memset(icv, 0, bytes); + memset(hash_out, 0, sizeof(hash_out)); + + /* Hash it */ + HMAC(EVP_sha256(), + session->auth.data.sha256.key, + 32, + data, + len, + hash_out, + NULL); + + /* Verify match */ + if (0 != memcmp(hash_in, hash_out, bytes)) + return ODP_CRYPTO_ALG_ERR_ICV_CHECK; + + /* Matched */ + return ODP_CRYPTO_ALG_ERR_NONE; +} + +static odp_crypto_alg_err_t des_encrypt(odp_crypto_op_params_t *params, odp_crypto_generic_session_t *session) { @@ -261,6 +327,26 @@ int process_md5_params(odp_crypto_generic_session_t *session, return 0; } +static +int process_sha256_params(odp_crypto_generic_session_t *session, + odp_crypto_session_params_t *params, + uint32_t bits) +{ + /* Set function */ + if (ODP_CRYPTO_OP_ENCODE == params->op) + session->auth.func = sha256_gen; + else + session->auth.func = sha256_check; + + /* Number of valid bytes */ + session->auth.data.sha256.bytes = bits / 8; + + /* Convert keys */ + memcpy(session->auth.data.sha256.key, params->auth_key.data, 32); + + return 0; +} + int odp_crypto_session_create(odp_crypto_session_params_t *params, odp_crypto_session_t *session_out, @@ -323,6 +409,9 @@ odp_crypto_session_create(odp_crypto_session_params_t *params, case ODP_AUTH_ALG_MD5_96: rc = process_md5_params(session, params, 96); break; + case ODP_AUTH_ALG_SHA256_128: + rc = process_sha256_params(session, params, 128); + break; default: rc = -1; } |