Path: blob/main/crypto/openssl/providers/implementations/ciphers/cipher_aes_gcm_siv.h
48383 views
/*1* Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include <openssl/aes.h>10#include "prov/ciphercommon.h"11#include "crypto/aes_platform.h"1213#define BLOCK_SIZE 1614#define NONCE_SIZE 1215#define TAG_SIZE 161617/* AAD manipulation macros */18#define UP16(x) (((x) + 15) & ~0x0F)19#define DOWN16(x) ((x) & ~0x0F)20#define REMAINDER16(x) ((x) & 0x0F)21#define IS16(x) (((x) & 0x0F) == 0)2223typedef struct prov_cipher_hw_aes_gcm_siv_st {24int (*initkey)(void *vctx);25int (*cipher)(void *vctx, unsigned char *out, const unsigned char *in,26size_t len);27int (*dup_ctx)(void *vdst, void *vsrc);28void (*clean_ctx)(void *vctx);29} PROV_CIPHER_HW_AES_GCM_SIV;3031/* Arranged for alignment purposes */32typedef struct prov_aes_gcm_siv_ctx_st {33EVP_CIPHER_CTX *ecb_ctx;34const PROV_CIPHER_HW_AES_GCM_SIV *hw; /* maybe not used, yet? */35uint8_t *aad; /* Allocated, rounded up to 16 bytes, from user */36OSSL_LIB_CTX *libctx;37OSSL_PROVIDER *provctx;38size_t aad_len; /* actual AAD length */39size_t key_len;40uint8_t key_gen_key[32]; /* from user */41uint8_t msg_enc_key[32]; /* depends on key size */42uint8_t msg_auth_key[BLOCK_SIZE];43uint8_t tag[TAG_SIZE]; /* generated tag, given to user or compared to user */44uint8_t user_tag[TAG_SIZE]; /* from user */45uint8_t nonce[NONCE_SIZE]; /* from user */46u128 Htable[16]; /* Polyval calculations via ghash */47unsigned int enc : 1; /* Set to 1 if we are encrypting or 0 otherwise */48unsigned int have_user_tag : 1;49unsigned int generated_tag : 1;50unsigned int used_enc : 1;51unsigned int used_dec : 1;52unsigned int speed : 1;53} PROV_AES_GCM_SIV_CTX;5455const PROV_CIPHER_HW_AES_GCM_SIV *ossl_prov_cipher_hw_aes_gcm_siv(size_t keybits);5657void ossl_polyval_ghash_init(u128 Htable[16], const uint64_t H[2]);58void ossl_polyval_ghash_hash(const u128 Htable[16], uint8_t *tag, const uint8_t *inp, size_t len);5960/* Define GSWAP8/GSWAP4 - used for BOTH little and big endian architectures */61static ossl_inline uint32_t GSWAP4(uint32_t n)62{63return (((n & 0x000000FF) << 24)64| ((n & 0x0000FF00) << 8)65| ((n & 0x00FF0000) >> 8)66| ((n & 0xFF000000) >> 24));67}68static ossl_inline uint64_t GSWAP8(uint64_t n)69{70uint64_t result;7172result = GSWAP4(n & 0x0FFFFFFFF);73result <<= 32;74return result | GSWAP4(n >> 32);75}767778