1*b077aed3SPierre Pronchery /* 2*b077aed3SPierre Pronchery * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. 3*b077aed3SPierre Pronchery * 4*b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use 5*b077aed3SPierre Pronchery * this file except in compliance with the License. You can obtain a copy 6*b077aed3SPierre Pronchery * in the file LICENSE in the source distribution or at 7*b077aed3SPierre Pronchery * https://www.openssl.org/source/license.html 8*b077aed3SPierre Pronchery */ 9*b077aed3SPierre Pronchery 10*b077aed3SPierre Pronchery #include "ciphercommon_aead.h" 11*b077aed3SPierre Pronchery 12*b077aed3SPierre Pronchery typedef struct prov_ccm_hw_st PROV_CCM_HW; 13*b077aed3SPierre Pronchery 14*b077aed3SPierre Pronchery #if defined(OPENSSL_CPUID_OBJ) && defined(__s390__) 15*b077aed3SPierre Pronchery /*- 16*b077aed3SPierre Pronchery * KMAC-AES parameter block - begin 17*b077aed3SPierre Pronchery * (see z/Architecture Principles of Operation >= SA22-7832-08) 18*b077aed3SPierre Pronchery */ 19*b077aed3SPierre Pronchery typedef struct S390X_kmac_params_st { 20*b077aed3SPierre Pronchery union { 21*b077aed3SPierre Pronchery unsigned long long g[2]; 22*b077aed3SPierre Pronchery unsigned char b[16]; 23*b077aed3SPierre Pronchery } icv; 24*b077aed3SPierre Pronchery unsigned char k[32]; 25*b077aed3SPierre Pronchery } S390X_KMAC_PARAMS; 26*b077aed3SPierre Pronchery /* KMAC-AES parameter block - end */ 27*b077aed3SPierre Pronchery #endif 28*b077aed3SPierre Pronchery 29*b077aed3SPierre Pronchery /* Base structure that is shared by AES & ARIA for CCM MODE */ 30*b077aed3SPierre Pronchery typedef struct prov_ccm_st { 31*b077aed3SPierre Pronchery unsigned int enc : 1; 32*b077aed3SPierre Pronchery unsigned int key_set : 1; /* Set if key initialised */ 33*b077aed3SPierre Pronchery unsigned int iv_set : 1; /* Set if an iv is set */ 34*b077aed3SPierre Pronchery unsigned int tag_set : 1; /* Set if tag is valid */ 35*b077aed3SPierre Pronchery unsigned int len_set : 1; /* Set if message length set */ 36*b077aed3SPierre Pronchery size_t l, m; /* L and M parameters from RFC3610 */ 37*b077aed3SPierre Pronchery size_t keylen; 38*b077aed3SPierre Pronchery size_t tls_aad_len; /* TLS AAD length */ 39*b077aed3SPierre Pronchery size_t tls_aad_pad_sz; 40*b077aed3SPierre Pronchery unsigned char iv[GENERIC_BLOCK_SIZE]; 41*b077aed3SPierre Pronchery unsigned char buf[GENERIC_BLOCK_SIZE]; 42*b077aed3SPierre Pronchery CCM128_CONTEXT ccm_ctx; 43*b077aed3SPierre Pronchery ccm128_f str; 44*b077aed3SPierre Pronchery const PROV_CCM_HW *hw; /* hardware specific methods */ 45*b077aed3SPierre Pronchery } PROV_CCM_CTX; 46*b077aed3SPierre Pronchery 47*b077aed3SPierre Pronchery PROV_CIPHER_FUNC(int, CCM_cipher, (PROV_CCM_CTX *ctx, unsigned char *out, \ 48*b077aed3SPierre Pronchery size_t *padlen, const unsigned char *in, \ 49*b077aed3SPierre Pronchery size_t len)); 50*b077aed3SPierre Pronchery PROV_CIPHER_FUNC(int, CCM_setkey, (PROV_CCM_CTX *ctx, \ 51*b077aed3SPierre Pronchery const unsigned char *key, size_t keylen)); 52*b077aed3SPierre Pronchery PROV_CIPHER_FUNC(int, CCM_setiv, (PROV_CCM_CTX *dat, \ 53*b077aed3SPierre Pronchery const unsigned char *iv, size_t ivlen, \ 54*b077aed3SPierre Pronchery size_t mlen)); 55*b077aed3SPierre Pronchery PROV_CIPHER_FUNC(int, CCM_setaad, (PROV_CCM_CTX *ctx, \ 56*b077aed3SPierre Pronchery const unsigned char *aad, size_t aadlen)); 57*b077aed3SPierre Pronchery PROV_CIPHER_FUNC(int, CCM_auth_encrypt, (PROV_CCM_CTX *ctx, \ 58*b077aed3SPierre Pronchery const unsigned char *in, \ 59*b077aed3SPierre Pronchery unsigned char *out, size_t len, \ 60*b077aed3SPierre Pronchery unsigned char *tag, size_t taglen)); 61*b077aed3SPierre Pronchery PROV_CIPHER_FUNC(int, CCM_auth_decrypt, (PROV_CCM_CTX *ctx, \ 62*b077aed3SPierre Pronchery const unsigned char *in, \ 63*b077aed3SPierre Pronchery unsigned char *out, size_t len, \ 64*b077aed3SPierre Pronchery unsigned char *tag, size_t taglen)); 65*b077aed3SPierre Pronchery PROV_CIPHER_FUNC(int, CCM_gettag, (PROV_CCM_CTX *ctx, \ 66*b077aed3SPierre Pronchery unsigned char *tag, size_t taglen)); 67*b077aed3SPierre Pronchery 68*b077aed3SPierre Pronchery /* 69*b077aed3SPierre Pronchery * CCM Mode internal method table used to handle hardware specific differences, 70*b077aed3SPierre Pronchery * (and different algorithms). 71*b077aed3SPierre Pronchery */ 72*b077aed3SPierre Pronchery struct prov_ccm_hw_st { 73*b077aed3SPierre Pronchery OSSL_CCM_setkey_fn setkey; 74*b077aed3SPierre Pronchery OSSL_CCM_setiv_fn setiv; 75*b077aed3SPierre Pronchery OSSL_CCM_setaad_fn setaad; 76*b077aed3SPierre Pronchery OSSL_CCM_auth_encrypt_fn auth_encrypt; 77*b077aed3SPierre Pronchery OSSL_CCM_auth_decrypt_fn auth_decrypt; 78*b077aed3SPierre Pronchery OSSL_CCM_gettag_fn gettag; 79*b077aed3SPierre Pronchery }; 80*b077aed3SPierre Pronchery 81*b077aed3SPierre Pronchery OSSL_FUNC_cipher_encrypt_init_fn ossl_ccm_einit; 82*b077aed3SPierre Pronchery OSSL_FUNC_cipher_decrypt_init_fn ossl_ccm_dinit; 83*b077aed3SPierre Pronchery OSSL_FUNC_cipher_get_ctx_params_fn ossl_ccm_get_ctx_params; 84*b077aed3SPierre Pronchery OSSL_FUNC_cipher_set_ctx_params_fn ossl_ccm_set_ctx_params; 85*b077aed3SPierre Pronchery OSSL_FUNC_cipher_update_fn ossl_ccm_stream_update; 86*b077aed3SPierre Pronchery OSSL_FUNC_cipher_final_fn ossl_ccm_stream_final; 87*b077aed3SPierre Pronchery OSSL_FUNC_cipher_cipher_fn ossl_ccm_cipher; 88*b077aed3SPierre Pronchery void ossl_ccm_initctx(PROV_CCM_CTX *ctx, size_t keybits, const PROV_CCM_HW *hw); 89*b077aed3SPierre Pronchery 90*b077aed3SPierre Pronchery int ossl_ccm_generic_setiv(PROV_CCM_CTX *ctx, const unsigned char *nonce, 91*b077aed3SPierre Pronchery size_t nlen, size_t mlen); 92*b077aed3SPierre Pronchery int ossl_ccm_generic_setaad(PROV_CCM_CTX *ctx, const unsigned char *aad, 93*b077aed3SPierre Pronchery size_t alen); 94*b077aed3SPierre Pronchery int ossl_ccm_generic_gettag(PROV_CCM_CTX *ctx, unsigned char *tag, size_t tlen); 95*b077aed3SPierre Pronchery int ossl_ccm_generic_auth_encrypt(PROV_CCM_CTX *ctx, const unsigned char *in, 96*b077aed3SPierre Pronchery unsigned char *out, size_t len, 97*b077aed3SPierre Pronchery unsigned char *tag, size_t taglen); 98*b077aed3SPierre Pronchery int ossl_ccm_generic_auth_decrypt(PROV_CCM_CTX *ctx, const unsigned char *in, 99*b077aed3SPierre Pronchery unsigned char *out, size_t len, 100*b077aed3SPierre Pronchery unsigned char *expected_tag, size_t taglen); 101