Path: blob/main/crypto/openssl/providers/implementations/include/prov/ciphercommon_gcm.h
48534 views
1/*2* Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.3*4* Licensed under the Apache License 2.0 (the "License"). You may not use5* this file except in compliance with the License. You can obtain a copy6* in the file LICENSE in the source distribution or at7* https://www.openssl.org/source/license.html8*/910#ifndef OSSL_PROV_CIPHERCOMMON_GCM_H11# define OSSL_PROV_CIPHERCOMMON_GCM_H12# pragma once1314# include <openssl/aes.h>15# include "ciphercommon_aead.h"1617typedef struct prov_gcm_hw_st PROV_GCM_HW;1819# define GCM_IV_DEFAULT_SIZE 12 /* IV's for AES_GCM should normally be 12 bytes */20# define GCM_IV_MAX_SIZE (1024 / 8)21# define GCM_TAG_MAX_SIZE 162223# if defined(OPENSSL_CPUID_OBJ) && defined(__s390__)24/*-25* KMA-GCM-AES parameter block - begin26* (see z/Architecture Principles of Operation >= SA22-7832-11)27*/28typedef struct S390X_kma_params_st {29unsigned char reserved[12];30union {31unsigned int w;32unsigned char b[4];33} cv; /* 32 bit counter value */34union {35unsigned long long g[2];36unsigned char b[16];37} t; /* tag */38unsigned char h[16]; /* hash subkey */39unsigned long long taadl; /* total AAD length */40unsigned long long tpcl; /* total plaintxt/ciphertxt len */41union {42unsigned long long g[2];43unsigned int w[4];44} j0; /* initial counter value */45unsigned char k[32]; /* key */46} S390X_KMA_PARAMS;4748# endif4950typedef struct prov_gcm_ctx_st {51unsigned int mode; /* The mode that we are using */52size_t keylen;53size_t ivlen;54size_t taglen;55size_t tls_aad_pad_sz;56size_t tls_aad_len; /* TLS AAD length */57uint64_t tls_enc_records; /* Number of TLS records encrypted */5859/*60* num contains the number of bytes of |iv| which are valid for modes that61* manage partial blocks themselves.62*/63size_t num;64size_t bufsz; /* Number of bytes in buf */65uint64_t flags;6667unsigned int iv_state; /* set to one of IV_STATE_XXX */68unsigned int enc:1; /* Set to 1 if we are encrypting or 0 otherwise */69unsigned int pad:1; /* Whether padding should be used or not */70unsigned int key_set:1; /* Set if key initialised */71unsigned int iv_gen_rand:1; /* No IV was specified, so generate a rand IV */72unsigned int iv_gen:1; /* It is OK to generate IVs */7374unsigned char iv[GCM_IV_MAX_SIZE]; /* Buffer to use for IV's */75unsigned char buf[AES_BLOCK_SIZE]; /* Buffer of partial blocks processed via update calls */7677OSSL_LIB_CTX *libctx; /* needed for rand calls */78const PROV_GCM_HW *hw; /* hardware specific methods */79GCM128_CONTEXT gcm;80ctr128_f ctr;81} PROV_GCM_CTX;8283PROV_CIPHER_FUNC(int, GCM_setkey, (PROV_GCM_CTX *ctx, const unsigned char *key,84size_t keylen));85PROV_CIPHER_FUNC(int, GCM_setiv, (PROV_GCM_CTX *dat, const unsigned char *iv,86size_t ivlen));87PROV_CIPHER_FUNC(int, GCM_aadupdate, (PROV_GCM_CTX *ctx,88const unsigned char *aad, size_t aadlen));89PROV_CIPHER_FUNC(int, GCM_cipherupdate, (PROV_GCM_CTX *ctx,90const unsigned char *in, size_t len,91unsigned char *out));92PROV_CIPHER_FUNC(int, GCM_cipherfinal, (PROV_GCM_CTX *ctx, unsigned char *tag));93PROV_CIPHER_FUNC(int, GCM_oneshot, (PROV_GCM_CTX *ctx, unsigned char *aad,94size_t aad_len, const unsigned char *in,95size_t in_len, unsigned char *out,96unsigned char *tag, size_t taglen));97struct prov_gcm_hw_st {98OSSL_GCM_setkey_fn setkey;99OSSL_GCM_setiv_fn setiv;100OSSL_GCM_aadupdate_fn aadupdate;101OSSL_GCM_cipherupdate_fn cipherupdate;102OSSL_GCM_cipherfinal_fn cipherfinal;103OSSL_GCM_oneshot_fn oneshot;104};105106OSSL_FUNC_cipher_encrypt_init_fn ossl_gcm_einit;107OSSL_FUNC_cipher_decrypt_init_fn ossl_gcm_dinit;108OSSL_FUNC_cipher_get_ctx_params_fn ossl_gcm_get_ctx_params;109OSSL_FUNC_cipher_set_ctx_params_fn ossl_gcm_set_ctx_params;110OSSL_FUNC_cipher_cipher_fn ossl_gcm_cipher;111OSSL_FUNC_cipher_update_fn ossl_gcm_stream_update;112OSSL_FUNC_cipher_final_fn ossl_gcm_stream_final;113void ossl_gcm_initctx(void *provctx, PROV_GCM_CTX *ctx, size_t keybits,114const PROV_GCM_HW *hw);115116int ossl_gcm_setiv(PROV_GCM_CTX *ctx, const unsigned char *iv, size_t ivlen);117int ossl_gcm_aad_update(PROV_GCM_CTX *ctx, const unsigned char *aad,118size_t aad_len);119int ossl_gcm_cipher_final(PROV_GCM_CTX *ctx, unsigned char *tag);120int ossl_gcm_one_shot(PROV_GCM_CTX *ctx, unsigned char *aad, size_t aad_len,121const unsigned char *in, size_t in_len,122unsigned char *out, unsigned char *tag, size_t tag_len);123int ossl_gcm_cipher_update(PROV_GCM_CTX *ctx, const unsigned char *in,124size_t len, unsigned char *out);125126# define GCM_HW_SET_KEY_CTR_FN(ks, fn_set_enc_key, fn_block, fn_ctr) \127fn_set_enc_key(key, keylen * 8, ks); \128CRYPTO_gcm128_init(&ctx->gcm, ks, (block128_f)fn_block); \129ctx->ctr = (ctr128_f)fn_ctr; \130ctx->key_set = 1;131132#endif133134135