packages feed

crypton-2.3.1 / cbits / crypton_aes.c

/* * Copyright (c) 2012 Vincent Hanquez <vincent@snarc.org> * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * 3. Neither the name of the author nor the names of his contributors *    may be used to endorse or promote products derived from this software *    without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#include <stddef.h>#include <string.h>#include <stdio.h>#include <crypton_cpu.h>#include <crypton_aes.h>#include <crypton_bitfn.h>#include <aes/generic.h>#include <aes/gf.h>#include <aes/x86ni.h>#ifdef WITH_PPC8_CRYPTO#include <aes/ppc8.h>#endif#ifdef WITH_GCM_FUSED#include <aes/gcm_fused_x86.h>#endifvoid crypton_aes_generic_encrypt_ecb(aes_block *output, aes_key *key, aes_block *input, uint32_t nb_blocks);void crypton_aes_generic_decrypt_ecb(aes_block *output, aes_key *key, aes_block *input, uint32_t nb_blocks);void crypton_aes_generic_encrypt_cbc(aes_block *output, aes_key *key, aes_block *iv, aes_block *input, uint32_t nb_blocks);void crypton_aes_generic_decrypt_cbc(aes_block *output, aes_key *key, aes_block *iv, aes_block *input, uint32_t nb_blocks);void crypton_aes_generic_encrypt_ctr(uint8_t *output, aes_key *key, aes_block *iv, uint8_t *input, uint32_t length);void crypton_aes_generic_encrypt_c32(uint8_t *output, aes_key *key, aes_block *iv, uint8_t *input, uint32_t length);void crypton_aes_generic_encrypt_xts(aes_block *output, aes_key *k1, aes_key *k2, aes_block *dataunit,                             uint32_t spoint, aes_block *input, uint32_t nb_blocks);void crypton_aes_generic_decrypt_xts(aes_block *output, aes_key *k1, aes_key *k2, aes_block *dataunit,                             uint32_t spoint, aes_block *input, uint32_t nb_blocks);void crypton_aes_generic_gcm_encrypt(uint8_t *output, aes_gcm *gcm, aes_key *key, uint8_t *input, uint32_t length);void crypton_aes_generic_gcm_decrypt(uint8_t *output, aes_gcm *gcm, aes_key *key, uint8_t *input, uint32_t length);void crypton_aes_bitsliced_gcm_encrypt(uint8_t *output, aes_gcm *gcm, aes_key *key, uint8_t *input, uint32_t length);void crypton_aes_bitsliced_gcm_decrypt(uint8_t *output, aes_gcm *gcm, aes_key *key, uint8_t *input, uint32_t length);void crypton_aes_generic_ocb_encrypt(uint8_t *output, aes_ocb *ocb, aes_key *key, uint8_t *input, uint32_t length);void crypton_aes_generic_ocb_decrypt(uint8_t *output, aes_ocb *ocb, aes_key *key, uint8_t *input, uint32_t length);void crypton_aes_generic_ccm_encrypt(uint8_t *output, aes_ccm *ccm, aes_key *key, uint8_t *input, uint32_t length);void crypton_aes_generic_ccm_decrypt(uint8_t *output, aes_ccm *ccm, aes_key *key, uint8_t *input, uint32_t length);#ifdef WITH_ARMV8_CRYPTOvoid crypton_aes_armv8_init(aes_key *key, uint8_t *origkey, uint8_t size);int crypton_aes_armv8_gcm_fused_dec(uint8_t *out, const block128 *ht,                                    aes_key *key, const uint8_t *nonce,                                    const uint8_t *aad, uint32_t aadlen,                                    const uint8_t *in, uint32_t inlen,                                    const uint8_t *tag, uint32_t taglen,                                    uint8_t *outtag);void crypton_aes_armv8_gcm_fused(uint8_t *out, const block128 *ht,                                 aes_key *key, const uint8_t *nonce,                                 const uint8_t *aad, uint32_t aadlen,                                 const uint8_t *in, uint32_t inlen,                                 uint32_t taglen, aes_key *hpkey,                                 uint32_t sampleoff, uint8_t *mask);#define ARMV8_DECLS(sz) \	void crypton_aes_armv8_encrypt_block##sz(aes_block *output, aes_key *key, aes_block *input); \	void crypton_aes_armv8_decrypt_block##sz(aes_block *output, aes_key *key, aes_block *input); \	void crypton_aes_armv8_encrypt_ecb##sz(aes_block *output, aes_key *key, aes_block *input, uint32_t nb_blocks); \	void crypton_aes_armv8_decrypt_ecb##sz(aes_block *output, aes_key *key, aes_block *input, uint32_t nb_blocks); \	void crypton_aes_armv8_encrypt_cbc##sz(aes_block *output, aes_key *key, aes_block *iv, aes_block *input, uint32_t nb_blocks); \	void crypton_aes_armv8_decrypt_cbc##sz(aes_block *output, aes_key *key, aes_block *iv, aes_block *input, uint32_t nb_blocks); \	void crypton_aes_armv8_encrypt_ctr##sz(uint8_t *output, aes_key *key, aes_block *iv, uint8_t *input, uint32_t len); \	void crypton_aes_armv8_gcm_encrypt##sz(uint8_t *output, aes_gcm *gcm, aes_key *key, uint8_t *input, uint32_t length); \	void crypton_aes_armv8_gcm_decrypt##sz(uint8_t *output, aes_gcm *gcm, aes_key *key, uint8_t *input, uint32_t length); \	void crypton_aes_armv8_encrypt_xts##sz(aes_block *output, aes_key *k1, aes_key *k2, aes_block *dataunit, uint32_t spoint, aes_block *input, uint32_t nb_blocks); \	void crypton_aes_armv8_decrypt_xts##sz(aes_block *output, aes_key *k1, aes_key *k2, aes_block *dataunit, uint32_t spoint, aes_block *input, uint32_t nb_blocks);ARMV8_DECLS(128)ARMV8_DECLS(192)ARMV8_DECLS(256)int crypton_aes_armv8_available(void);int crypton_aes_armv8_pmull_available(void);void crypton_aes_armv8_hinit_pmull(block128 *htable, const block128 *h);void crypton_aes_armv8_gf_mul_pmull(block128 *a, const block128 *htable);void crypton_aes_armv8_gf_mul4_pmull(block128 *a, const block128 *blocks, const block128 *htable);#endifenum {	/* init */	INIT_128, INIT_192, INIT_256,	/* single block */	ENCRYPT_BLOCK_128, ENCRYPT_BLOCK_192, ENCRYPT_BLOCK_256,	DECRYPT_BLOCK_128, DECRYPT_BLOCK_192, DECRYPT_BLOCK_256,	/* ecb */	ENCRYPT_ECB_128, ENCRYPT_ECB_192, ENCRYPT_ECB_256,	DECRYPT_ECB_128, DECRYPT_ECB_192, DECRYPT_ECB_256,	/* cbc */	ENCRYPT_CBC_128, ENCRYPT_CBC_192, ENCRYPT_CBC_256,	DECRYPT_CBC_128, DECRYPT_CBC_192, DECRYPT_CBC_256,	/* ctr */	ENCRYPT_CTR_128, ENCRYPT_CTR_192, ENCRYPT_CTR_256,	/* ctr with 32-bit wrapping */	ENCRYPT_C32_128, ENCRYPT_C32_192, ENCRYPT_C32_256,	/* xts */	ENCRYPT_XTS_128, ENCRYPT_XTS_192, ENCRYPT_XTS_256,	DECRYPT_XTS_128, DECRYPT_XTS_192, DECRYPT_XTS_256,	/* gcm */	ENCRYPT_GCM_128, ENCRYPT_GCM_192, ENCRYPT_GCM_256,	DECRYPT_GCM_128, DECRYPT_GCM_192, DECRYPT_GCM_256,	/* ocb */	ENCRYPT_OCB_128, ENCRYPT_OCB_192, ENCRYPT_OCB_256,	DECRYPT_OCB_128, DECRYPT_OCB_192, DECRYPT_OCB_256,	/* ccm */	ENCRYPT_CCM_128, ENCRYPT_CCM_192, ENCRYPT_CCM_256,	DECRYPT_CCM_128, DECRYPT_CCM_192, DECRYPT_CCM_256,	/* ghash */	GHASH_HINIT, GHASH_GF_MUL, GHASH_GF_MUL4,};void *crypton_aes_branch_table[] = {	/* INIT */	[INIT_128]          = crypton_aes_generic_init,	[INIT_192]          = crypton_aes_generic_init,	[INIT_256]          = crypton_aes_generic_init,	/* BLOCK */	[ENCRYPT_BLOCK_128] = crypton_aes_generic_encrypt_block,	[ENCRYPT_BLOCK_192] = crypton_aes_generic_encrypt_block,	[ENCRYPT_BLOCK_256] = crypton_aes_generic_encrypt_block,	[DECRYPT_BLOCK_128] = crypton_aes_generic_decrypt_block,	[DECRYPT_BLOCK_192] = crypton_aes_generic_decrypt_block,	[DECRYPT_BLOCK_256] = crypton_aes_generic_decrypt_block,	/* ECB */	[ENCRYPT_ECB_128]   = crypton_aes_generic_encrypt_ecb,	[ENCRYPT_ECB_192]   = crypton_aes_generic_encrypt_ecb,	[ENCRYPT_ECB_256]   = crypton_aes_generic_encrypt_ecb,	[DECRYPT_ECB_128]   = crypton_aes_generic_decrypt_ecb,	[DECRYPT_ECB_192]   = crypton_aes_generic_decrypt_ecb,	[DECRYPT_ECB_256]   = crypton_aes_generic_decrypt_ecb,	/* CBC */	[ENCRYPT_CBC_128]   = crypton_aes_generic_encrypt_cbc,	[ENCRYPT_CBC_192]   = crypton_aes_generic_encrypt_cbc,	[ENCRYPT_CBC_256]   = crypton_aes_generic_encrypt_cbc,	[DECRYPT_CBC_128]   = crypton_aes_generic_decrypt_cbc,	[DECRYPT_CBC_192]   = crypton_aes_generic_decrypt_cbc,	[DECRYPT_CBC_256]   = crypton_aes_generic_decrypt_cbc,	/* CTR */	[ENCRYPT_CTR_128]   = crypton_aes_generic_encrypt_ctr,	[ENCRYPT_CTR_192]   = crypton_aes_generic_encrypt_ctr,	[ENCRYPT_CTR_256]   = crypton_aes_generic_encrypt_ctr,	/* CTR with 32-bit wrapping */	[ENCRYPT_C32_128]   = crypton_aes_generic_encrypt_c32,	[ENCRYPT_C32_192]   = crypton_aes_generic_encrypt_c32,	[ENCRYPT_C32_256]   = crypton_aes_generic_encrypt_c32,	/* XTS */	[ENCRYPT_XTS_128]   = crypton_aes_generic_encrypt_xts,	[ENCRYPT_XTS_192]   = crypton_aes_generic_encrypt_xts,	[ENCRYPT_XTS_256]   = crypton_aes_generic_encrypt_xts,	[DECRYPT_XTS_128]   = crypton_aes_generic_decrypt_xts,	[DECRYPT_XTS_192]   = crypton_aes_generic_decrypt_xts,	[DECRYPT_XTS_256]   = crypton_aes_generic_decrypt_xts,	/* GCM */	[ENCRYPT_GCM_128]   = crypton_aes_generic_gcm_encrypt,	[ENCRYPT_GCM_192]   = crypton_aes_generic_gcm_encrypt,	[ENCRYPT_GCM_256]   = crypton_aes_generic_gcm_encrypt,	[DECRYPT_GCM_128]   = crypton_aes_generic_gcm_decrypt,	[DECRYPT_GCM_192]   = crypton_aes_generic_gcm_decrypt,	[DECRYPT_GCM_256]   = crypton_aes_generic_gcm_decrypt,	/* OCB */	[ENCRYPT_OCB_128]   = crypton_aes_generic_ocb_encrypt,	[ENCRYPT_OCB_192]   = crypton_aes_generic_ocb_encrypt,	[ENCRYPT_OCB_256]   = crypton_aes_generic_ocb_encrypt,	[DECRYPT_OCB_128]   = crypton_aes_generic_ocb_decrypt,	[DECRYPT_OCB_192]   = crypton_aes_generic_ocb_decrypt,	[DECRYPT_OCB_256]   = crypton_aes_generic_ocb_decrypt,	/* CCM */	[ENCRYPT_CCM_128]   = crypton_aes_generic_ccm_encrypt,	[ENCRYPT_CCM_192]   = crypton_aes_generic_ccm_encrypt,	[ENCRYPT_CCM_256]   = crypton_aes_generic_ccm_encrypt,	[DECRYPT_CCM_128]   = crypton_aes_generic_ccm_decrypt,	[DECRYPT_CCM_192]   = crypton_aes_generic_ccm_decrypt,	[DECRYPT_CCM_256]   = crypton_aes_generic_ccm_decrypt,	/* GHASH */	[GHASH_HINIT]       = crypton_aes_generic_hinit,	[GHASH_GF_MUL]      = crypton_aes_generic_gf_mul,	[GHASH_GF_MUL4]     = crypton_aes_generic_gf_mul4,};typedef void (*init_f)(aes_key *, uint8_t *, uint8_t);typedef void (*ecb_f)(aes_block *output, aes_key *key, aes_block *input, uint32_t nb_blocks);typedef void (*cbc_f)(aes_block *output, aes_key *key, aes_block *iv, aes_block *input, uint32_t nb_blocks);typedef void (*ctr_f)(uint8_t *output, aes_key *key, aes_block *iv, uint8_t *input, uint32_t length);typedef void (*xts_f)(aes_block *output, aes_key *k1, aes_key *k2, aes_block *dataunit, uint32_t spoint, aes_block *input, uint32_t nb_blocks);typedef void (*gcm_crypt_f)(uint8_t *output, aes_gcm *gcm, aes_key *key, uint8_t *input, uint32_t length);typedef void (*ocb_crypt_f)(uint8_t *output, aes_ocb *ocb, aes_key *key, uint8_t *input, uint32_t length);typedef void (*ccm_crypt_f)(uint8_t *output, aes_ccm *ccm, aes_key *key, uint8_t *input, uint32_t length);typedef void (*block_f)(aes_block *output, aes_key *key, aes_block *input);typedef void (*hinit_f)(table_4bit htable, const block128 *h);typedef void (*gf_mul_f)(block128 *a, const table_4bit htable);typedef void (*gf_mul4_f)(block128 *a, const block128 *blocks, const table_4bit htable);#if defined(WITH_AESNI) || defined(WITH_ARMV8_CRYPTO) || defined(WITH_PPC8_CRYPTO)#define GET_INIT(strength) \	((init_f) (crypton_aes_branch_table[INIT_128 + strength]))#define GET_ECB_ENCRYPT(strength) \	((ecb_f) (crypton_aes_branch_table[ENCRYPT_ECB_128 + strength]))#define GET_ECB_DECRYPT(strength) \	((ecb_f) (crypton_aes_branch_table[DECRYPT_ECB_128 + strength]))#define GET_CBC_ENCRYPT(strength) \	((cbc_f) (crypton_aes_branch_table[ENCRYPT_CBC_128 + strength]))#define GET_CBC_DECRYPT(strength) \	((cbc_f) (crypton_aes_branch_table[DECRYPT_CBC_128 + strength]))#define GET_CTR_ENCRYPT(strength) \	((ctr_f) (crypton_aes_branch_table[ENCRYPT_CTR_128 + strength]))#define GET_C32_ENCRYPT(strength) \	((ctr_f) (crypton_aes_branch_table[ENCRYPT_C32_128 + strength]))#define GET_XTS_ENCRYPT(strength) \	((xts_f) (crypton_aes_branch_table[ENCRYPT_XTS_128 + strength]))#define GET_XTS_DECRYPT(strength) \	((xts_f) (crypton_aes_branch_table[DECRYPT_XTS_128 + strength]))#define GET_GCM_ENCRYPT(strength) \	((gcm_crypt_f) (crypton_aes_branch_table[ENCRYPT_GCM_128 + strength]))#define GET_GCM_DECRYPT(strength) \	((gcm_crypt_f) (crypton_aes_branch_table[DECRYPT_GCM_128 + strength]))#define GET_OCB_ENCRYPT(strength) \	((ocb_crypt_f) (crypton_aes_branch_table[ENCRYPT_OCB_128 + strength]))#define GET_OCB_DECRYPT(strength) \	((ocb_crypt_f) (crypton_aes_branch_table[DECRYPT_OCB_128 + strength]))#define GET_CCM_ENCRYPT(strength) \	((ccm_crypt_f) (crypton_aes_branch_table[ENCRYPT_CCM_128 + strength]))#define GET_CCM_DECRYPT(strength) \	((ccm_crypt_f) (crypton_aes_branch_table[DECRYPT_CCM_128 + strength]))#define crypton_aes_encrypt_block(o,k,i) \	(((block_f) (crypton_aes_branch_table[ENCRYPT_BLOCK_128 + k->strength]))(o,k,i))#define crypton_aes_decrypt_block(o,k,i) \	(((block_f) (crypton_aes_branch_table[DECRYPT_BLOCK_128 + k->strength]))(o,k,i))#define crypton_hinit(t,h) \	(((hinit_f) (crypton_aes_branch_table[GHASH_HINIT]))(t,h))#define crypton_gf_mul(a,t) \	(((gf_mul_f) (crypton_aes_branch_table[GHASH_GF_MUL]))(a,t))#define crypton_gf_mul4(a,b,t) \	(((gf_mul4_f) (crypton_aes_branch_table[GHASH_GF_MUL4]))(a,b,t))#else#define GET_INIT(strength) crypton_aes_generic_init#define GET_ECB_ENCRYPT(strength) crypton_aes_generic_encrypt_ecb#define GET_ECB_DECRYPT(strength) crypton_aes_generic_decrypt_ecb#define GET_CBC_ENCRYPT(strength) crypton_aes_generic_encrypt_cbc#define GET_CBC_DECRYPT(strength) crypton_aes_generic_decrypt_cbc/* No accelerator is compiled in, so the portable schedule is the only one * a key can hold and the four-block CTR is named here rather than installed * at run time.  This is the build ppc64le, s390x, riscv64, 32-bit ARM and * -f-support_aesni all get, and nothing in it reads the branch table. */#define GET_CTR_ENCRYPT(strength) crypton_aes_bitsliced_encrypt_ctr#define GET_C32_ENCRYPT(strength) crypton_aes_bitsliced_encrypt_c32#define GET_XTS_ENCRYPT(strength) crypton_aes_generic_encrypt_xts#define GET_XTS_DECRYPT(strength) crypton_aes_generic_decrypt_xts#define GET_GCM_ENCRYPT(strength) crypton_aes_bitsliced_gcm_encrypt#define GET_GCM_DECRYPT(strength) crypton_aes_bitsliced_gcm_decrypt#define GET_OCB_ENCRYPT(strength) crypton_aes_generic_ocb_encrypt#define GET_OCB_DECRYPT(strength) crypton_aes_generic_ocb_decrypt#define GET_CCM_ENCRYPT(strength) crypton_aes_generic_ccm_encrypt#define GET_CCM_DECRYPT(strength) crypton_aes_generic_ccm_decrypt#define crypton_aes_encrypt_block(o,k,i) crypton_aes_generic_encrypt_block(o,k,i)#define crypton_aes_decrypt_block(o,k,i) crypton_aes_generic_decrypt_block(o,k,i)#define crypton_hinit(t,h) crypton_aes_generic_hinit(t,h)#define crypton_gf_mul(a,t) crypton_aes_generic_gf_mul(a,t)#define crypton_gf_mul4(a,b,t) crypton_aes_generic_gf_mul4(a,b,t)#endifstatic uint8_t crypton_aes_cpu_options[CPU_OPTION_COUNT] = {};#if defined(ARCH_X86) && defined(WITH_AESNI)static void initialize_table_ni(int aesni, int pclmul){	if (!aesni)		return;	crypton_aes_cpu_options[CPU_AESNI] = 1;	crypton_aes_branch_table[INIT_128] = crypton_aesni_init;	crypton_aes_branch_table[INIT_192] = crypton_aesni_init;	crypton_aes_branch_table[INIT_256] = crypton_aesni_init;	crypton_aes_branch_table[ENCRYPT_BLOCK_128] = crypton_aesni_encrypt_block128;	crypton_aes_branch_table[DECRYPT_BLOCK_128] = crypton_aesni_decrypt_block128;	crypton_aes_branch_table[ENCRYPT_BLOCK_192] = crypton_aesni_encrypt_block192;	crypton_aes_branch_table[ENCRYPT_BLOCK_256] = crypton_aesni_encrypt_block256;	crypton_aes_branch_table[DECRYPT_BLOCK_192] = crypton_aesni_decrypt_block192;	crypton_aes_branch_table[DECRYPT_BLOCK_256] = crypton_aesni_decrypt_block256;	/* ECB */	crypton_aes_branch_table[ENCRYPT_ECB_128] = crypton_aesni_encrypt_ecb128;	crypton_aes_branch_table[DECRYPT_ECB_128] = crypton_aesni_decrypt_ecb128;	crypton_aes_branch_table[ENCRYPT_ECB_192] = crypton_aesni_encrypt_ecb192;	crypton_aes_branch_table[ENCRYPT_ECB_256] = crypton_aesni_encrypt_ecb256;	crypton_aes_branch_table[DECRYPT_ECB_192] = crypton_aesni_decrypt_ecb192;	crypton_aes_branch_table[DECRYPT_ECB_256] = crypton_aesni_decrypt_ecb256;	/* CBC */	crypton_aes_branch_table[ENCRYPT_CBC_128] = crypton_aesni_encrypt_cbc128;	crypton_aes_branch_table[DECRYPT_CBC_128] = crypton_aesni_decrypt_cbc128;	crypton_aes_branch_table[ENCRYPT_CBC_192] = crypton_aesni_encrypt_cbc192;	crypton_aes_branch_table[ENCRYPT_CBC_256] = crypton_aesni_encrypt_cbc256;	crypton_aes_branch_table[DECRYPT_CBC_192] = crypton_aesni_decrypt_cbc192;	crypton_aes_branch_table[DECRYPT_CBC_256] = crypton_aesni_decrypt_cbc256;	/* CTR */	crypton_aes_branch_table[ENCRYPT_CTR_128] = crypton_aesni_encrypt_ctr128;	crypton_aes_branch_table[ENCRYPT_CTR_192] = crypton_aesni_encrypt_ctr192;	crypton_aes_branch_table[ENCRYPT_CTR_256] = crypton_aesni_encrypt_ctr256;	/* CTR with 32-bit wrapping */	crypton_aes_branch_table[ENCRYPT_C32_128] = crypton_aesni_encrypt_c32_128;	crypton_aes_branch_table[ENCRYPT_C32_192] = crypton_aesni_encrypt_c32_192;	crypton_aes_branch_table[ENCRYPT_C32_256] = crypton_aesni_encrypt_c32_256;	/* XTS */	crypton_aes_branch_table[ENCRYPT_XTS_128] = crypton_aesni_encrypt_xts128;	crypton_aes_branch_table[ENCRYPT_XTS_192] = crypton_aesni_encrypt_xts192;	crypton_aes_branch_table[ENCRYPT_XTS_256] = crypton_aesni_encrypt_xts256;	crypton_aes_branch_table[DECRYPT_XTS_128] = crypton_aesni_decrypt_xts128;	crypton_aes_branch_table[DECRYPT_XTS_192] = crypton_aesni_decrypt_xts192;	crypton_aes_branch_table[DECRYPT_XTS_256] = crypton_aesni_decrypt_xts256;	/* GCM */	/* GCM, where the build has the carry-less multiply, waits below until	 * the processor is known to have it too: the loop calls the multiply	 * rather than reaching it through the branch pointer, so that it can	 * be scheduled against the rounds, and is compiled with the	 * instruction.  The AArch64 table waits for PMULL for the same reason.	 */#ifndef WITH_PCLMUL	crypton_aes_branch_table[ENCRYPT_GCM_128] = crypton_aesni_gcm_encrypt128;	crypton_aes_branch_table[ENCRYPT_GCM_192] = crypton_aesni_gcm_encrypt192;	crypton_aes_branch_table[ENCRYPT_GCM_256] = crypton_aesni_gcm_encrypt256;	crypton_aes_branch_table[DECRYPT_GCM_128] = crypton_aesni_gcm_decrypt128;	crypton_aes_branch_table[DECRYPT_GCM_192] = crypton_aesni_gcm_decrypt192;	crypton_aes_branch_table[DECRYPT_GCM_256] = crypton_aesni_gcm_decrypt256;#endif	/* OCB drives the ECB paths above a group at a time, so it has no	 * entries of its own */#ifdef WITH_PCLMUL	if (!pclmul)		return;	crypton_aes_cpu_options[CPU_PCLMUL] = 1;	/* GHASH */	crypton_aes_branch_table[GHASH_HINIT]     = crypton_aesni_hinit_pclmul,	crypton_aes_branch_table[GHASH_GF_MUL]    = crypton_aesni_gf_mul_pclmul,	crypton_aes_branch_table[GHASH_GF_MUL4]   = crypton_aesni_gf_mul4_pclmul,	crypton_aesni_init_pclmul();	/* and GCM, which needs both halves */	crypton_aes_branch_table[ENCRYPT_GCM_128] = crypton_aesni_gcm_encrypt128;	crypton_aes_branch_table[ENCRYPT_GCM_192] = crypton_aesni_gcm_encrypt192;	crypton_aes_branch_table[ENCRYPT_GCM_256] = crypton_aesni_gcm_encrypt256;	crypton_aes_branch_table[DECRYPT_GCM_128] = crypton_aesni_gcm_decrypt128;	crypton_aes_branch_table[DECRYPT_GCM_192] = crypton_aesni_gcm_decrypt192;	crypton_aes_branch_table[DECRYPT_GCM_256] = crypton_aesni_gcm_decrypt256;#endif}#endif#ifdef WITH_ARMV8_CRYPTOstatic void initialize_table_armv8(void){	if (!crypton_aes_armv8_available())		return;	crypton_aes_cpu_options[CPU_AESNI] = 1;	crypton_aes_branch_table[INIT_128] = crypton_aes_armv8_init;	crypton_aes_branch_table[INIT_192] = crypton_aes_armv8_init;	crypton_aes_branch_table[INIT_256] = crypton_aes_armv8_init;	crypton_aes_branch_table[ENCRYPT_BLOCK_128] = crypton_aes_armv8_encrypt_block128;	crypton_aes_branch_table[DECRYPT_BLOCK_128] = crypton_aes_armv8_decrypt_block128;	crypton_aes_branch_table[ENCRYPT_BLOCK_192] = crypton_aes_armv8_encrypt_block192;	crypton_aes_branch_table[ENCRYPT_BLOCK_256] = crypton_aes_armv8_encrypt_block256;	crypton_aes_branch_table[DECRYPT_BLOCK_192] = crypton_aes_armv8_decrypt_block192;	crypton_aes_branch_table[DECRYPT_BLOCK_256] = crypton_aes_armv8_decrypt_block256;	/* ECB */	crypton_aes_branch_table[ENCRYPT_ECB_128] = crypton_aes_armv8_encrypt_ecb128;	crypton_aes_branch_table[DECRYPT_ECB_128] = crypton_aes_armv8_decrypt_ecb128;	crypton_aes_branch_table[ENCRYPT_ECB_192] = crypton_aes_armv8_encrypt_ecb192;	crypton_aes_branch_table[ENCRYPT_ECB_256] = crypton_aes_armv8_encrypt_ecb256;	crypton_aes_branch_table[DECRYPT_ECB_192] = crypton_aes_armv8_decrypt_ecb192;	crypton_aes_branch_table[DECRYPT_ECB_256] = crypton_aes_armv8_decrypt_ecb256;	/* CBC */	crypton_aes_branch_table[ENCRYPT_CBC_128] = crypton_aes_armv8_encrypt_cbc128;	crypton_aes_branch_table[DECRYPT_CBC_128] = crypton_aes_armv8_decrypt_cbc128;	crypton_aes_branch_table[ENCRYPT_CBC_192] = crypton_aes_armv8_encrypt_cbc192;	crypton_aes_branch_table[ENCRYPT_CBC_256] = crypton_aes_armv8_encrypt_cbc256;	crypton_aes_branch_table[DECRYPT_CBC_192] = crypton_aes_armv8_decrypt_cbc192;	crypton_aes_branch_table[DECRYPT_CBC_256] = crypton_aes_armv8_decrypt_cbc256;	/* CTR, which the generic loop would otherwise drive one block at a time */	crypton_aes_branch_table[ENCRYPT_CTR_128] = crypton_aes_armv8_encrypt_ctr128;	crypton_aes_branch_table[ENCRYPT_CTR_192] = crypton_aes_armv8_encrypt_ctr192;	crypton_aes_branch_table[ENCRYPT_CTR_256] = crypton_aes_armv8_encrypt_ctr256;	/* XTS, likewise, in both directions */	crypton_aes_branch_table[ENCRYPT_XTS_128] = crypton_aes_armv8_encrypt_xts128;	crypton_aes_branch_table[DECRYPT_XTS_128] = crypton_aes_armv8_decrypt_xts128;	crypton_aes_branch_table[ENCRYPT_XTS_192] = crypton_aes_armv8_encrypt_xts192;	crypton_aes_branch_table[ENCRYPT_XTS_256] = crypton_aes_armv8_encrypt_xts256;	crypton_aes_branch_table[DECRYPT_XTS_192] = crypton_aes_armv8_decrypt_xts192;	crypton_aes_branch_table[DECRYPT_XTS_256] = crypton_aes_armv8_decrypt_xts256;	/* GHASH, which GCM spends its time in once AES itself is fast */	if (!crypton_aes_armv8_pmull_available())		return;	crypton_aes_cpu_options[CPU_PCLMUL] = 1;	crypton_aes_branch_table[GHASH_HINIT]  = crypton_aes_armv8_hinit_pmull;	crypton_aes_branch_table[GHASH_GF_MUL]  = crypton_aes_armv8_gf_mul_pmull;	crypton_aes_branch_table[GHASH_GF_MUL4] = crypton_aes_armv8_gf_mul4_pmull;	/* GCM, which needs both halves and so waits until PMULL is known to	 * be there; the generic loop stands in otherwise */	crypton_aes_branch_table[ENCRYPT_GCM_128] = crypton_aes_armv8_gcm_encrypt128;	crypton_aes_branch_table[DECRYPT_GCM_128] = crypton_aes_armv8_gcm_decrypt128;	crypton_aes_branch_table[ENCRYPT_GCM_192] = crypton_aes_armv8_gcm_encrypt192;	crypton_aes_branch_table[ENCRYPT_GCM_256] = crypton_aes_armv8_gcm_encrypt256;	crypton_aes_branch_table[DECRYPT_GCM_192] = crypton_aes_armv8_gcm_decrypt192;	crypton_aes_branch_table[DECRYPT_GCM_256] = crypton_aes_armv8_gcm_decrypt256;}#endif/* Which implementation each entry of the branch table names is decided once, * before anything else runs. * * It used to be decided again on every crypton_aes_initkey, which meant two * threads taking a key at the same time were writing the whole table at the * same time -- a data race for as long as the program used AES, not just at * the start.  ThreadSanitizer reports forty-two of them for eight threads * doing nothing but taking keys.  The values written are the same ones every * time and the table starts out holding valid generic implementations, so * nothing has ever come of it; it is a race the standard gives no meaning to * all the same. * * A constructor runs while there is one thread, which is the cheapest way to * have no race at all: no flag to test, no lock to take, and one less thing * for crypton_aes_initkey to do per key. *//* * The two CTR entries, where the portable implementation is the one that * will run.  This is for the build that has an accelerator compiled in and * did not find it on the processor; a build with no accelerator at all * names them directly in the GET_ macros above and never reads this table. * * They cannot be the defaults.  crypton_aes_generic_encrypt_c32 is not * overridden on AArch64 -- the ARMv8 table has no C32 of its own -- and * neither CCM nor OCB is overridden anywhere, so those reach the block * function through the branch table and work whatever is installed.  A CTR * that read the portable schedule directly would be wrong on exactly those * machines.  So these go in only once nothing has claimed AES. */static void initialize_table_bitsliced(void){	crypton_aes_branch_table[ENCRYPT_CTR_128] = crypton_aes_bitsliced_encrypt_ctr;	crypton_aes_branch_table[ENCRYPT_CTR_192] = crypton_aes_bitsliced_encrypt_ctr;	crypton_aes_branch_table[ENCRYPT_CTR_256] = crypton_aes_bitsliced_encrypt_ctr;	crypton_aes_branch_table[ENCRYPT_C32_128] = crypton_aes_bitsliced_encrypt_c32;	crypton_aes_branch_table[ENCRYPT_C32_192] = crypton_aes_bitsliced_encrypt_c32;	crypton_aes_branch_table[ENCRYPT_C32_256] = crypton_aes_bitsliced_encrypt_c32;	crypton_aes_branch_table[ENCRYPT_GCM_128] = crypton_aes_bitsliced_gcm_encrypt;	crypton_aes_branch_table[ENCRYPT_GCM_192] = crypton_aes_bitsliced_gcm_encrypt;	crypton_aes_branch_table[ENCRYPT_GCM_256] = crypton_aes_bitsliced_gcm_encrypt;	crypton_aes_branch_table[DECRYPT_GCM_128] = crypton_aes_bitsliced_gcm_decrypt;	crypton_aes_branch_table[DECRYPT_GCM_192] = crypton_aes_bitsliced_gcm_decrypt;	crypton_aes_branch_table[DECRYPT_GCM_256] = crypton_aes_bitsliced_gcm_decrypt;}#ifdef WITH_PPC8_CRYPTO/* * POWER8.  One function per operation rather than three: the assembly reads * the round count out of the key, so the same entry serves every key size. * * GCM and the 32-bit counter are left where they are.  Neither has an entry * in the assembly, and the generic loops reach the block function and the * GHASH through this table, so both get the instructions anyway. */static void initialize_table_ppc8(void){	int sz;	if (!crypton_aes_ppc8_available())		return;	/* what stops initialize_table_bitsliced below from putting its own CTR	 * in, which would read this key as a bitsliced schedule */	crypton_aes_cpu_options[CPU_AESNI] = 1;	crypton_aes_cpu_options[CPU_PCLMUL] = 1;	for (sz = 0; sz < 3; sz++) {		crypton_aes_branch_table[INIT_128 + sz] = crypton_aes_ppc8_init;		crypton_aes_branch_table[ENCRYPT_BLOCK_128 + sz] = crypton_aes_ppc8_encrypt_block;		crypton_aes_branch_table[DECRYPT_BLOCK_128 + sz] = crypton_aes_ppc8_decrypt_block;		crypton_aes_branch_table[ENCRYPT_ECB_128 + sz] = crypton_aes_ppc8_encrypt_ecb;		crypton_aes_branch_table[DECRYPT_ECB_128 + sz] = crypton_aes_ppc8_decrypt_ecb;		crypton_aes_branch_table[ENCRYPT_CBC_128 + sz] = crypton_aes_ppc8_encrypt_cbc;		crypton_aes_branch_table[DECRYPT_CBC_128 + sz] = crypton_aes_ppc8_decrypt_cbc;		crypton_aes_branch_table[ENCRYPT_CTR_128 + sz] = crypton_aes_ppc8_encrypt_ctr;		crypton_aes_branch_table[ENCRYPT_XTS_128 + sz] = crypton_aes_ppc8_encrypt_xts;		crypton_aes_branch_table[DECRYPT_XTS_128 + sz] = crypton_aes_ppc8_decrypt_xts;	}	crypton_aes_branch_table[GHASH_HINIT]   = crypton_aes_ppc8_hinit;	crypton_aes_branch_table[GHASH_GF_MUL]  = crypton_aes_ppc8_gf_mul;	crypton_aes_branch_table[GHASH_GF_MUL4] = crypton_aes_ppc8_gf_mul4;}#endifstatic void crypton_aes_cpu_setup(void){#if defined(ARCH_X86) && defined(WITH_AESNI)	crypton_aesni_initialize_hw(initialize_table_ni);#endif#ifdef WITH_ARMV8_CRYPTO	initialize_table_armv8();#endif#ifdef WITH_PPC8_CRYPTO	initialize_table_ppc8();#endif	if (crypton_aes_cpu_options[CPU_AESNI] == 0)		initialize_table_bitsliced();}__attribute__((constructor))static void crypton_aes_cpu_ctor(void){	crypton_aes_cpu_setup();}uint8_t *crypton_aes_cpu_init(void){	return crypton_aes_cpu_options;}void crypton_aes_initkey(aes_key *key, uint8_t *origkey, uint8_t size){	switch (size) {	case 16: key->nbr = 10; key->strength = 0; break;	case 24: key->nbr = 12; key->strength = 1; break;	case 32: key->nbr = 14; key->strength = 2; break;	}	init_f _init = GET_INIT(key->strength);	_init(key, origkey, size);}void crypton_aes_encrypt_ecb(aes_block *output, aes_key *key, aes_block *input, uint32_t nb_blocks){	ecb_f e = GET_ECB_ENCRYPT(key->strength);	e(output, key, input, nb_blocks);}void crypton_aes_decrypt_ecb(aes_block *output, aes_key *key, aes_block *input, uint32_t nb_blocks){	ecb_f d = GET_ECB_DECRYPT(key->strength);	d(output, key, input, nb_blocks);}void crypton_aes_encrypt_cbc(aes_block *output, aes_key *key, aes_block *iv, aes_block *input, uint32_t nb_blocks){	cbc_f e = GET_CBC_ENCRYPT(key->strength);	e(output, key, iv, input, nb_blocks);}void crypton_aes_decrypt_cbc(aes_block *output, aes_key *key, aes_block *iv, aes_block *input, uint32_t nb_blocks){	cbc_f d = GET_CBC_DECRYPT(key->strength);	d(output, key, iv, input, nb_blocks);}void crypton_aes_encrypt_ctr(uint8_t *output, aes_key *key, aes_block *iv, uint8_t *input, uint32_t len){	ctr_f e = GET_CTR_ENCRYPT(key->strength);	e(output, key, iv, input, len);}void crypton_aes_encrypt_c32(uint8_t *output, aes_key *key, aes_block *iv, uint8_t *input, uint32_t len){	ctr_f e = GET_C32_ENCRYPT(key->strength);	e(output, key, iv, input, len);}void crypton_aes_encrypt_xts(aes_block *output, aes_key *k1, aes_key *k2, aes_block *dataunit,                     uint32_t spoint, aes_block *input, uint32_t nb_blocks){	xts_f e = GET_XTS_ENCRYPT(k1->strength);	e(output, k1, k2, dataunit, spoint, input, nb_blocks);}void crypton_aes_decrypt_xts(aes_block *output, aes_key *k1, aes_key *k2, aes_block *dataunit,                     uint32_t spoint, aes_block *input, uint32_t nb_blocks){	xts_f d = GET_XTS_DECRYPT(k1->strength);	d(output, k1, k2, dataunit, spoint, input, nb_blocks);}void crypton_aes_gcm_encrypt(uint8_t *output, aes_gcm *gcm, aes_key *key, uint8_t *input, uint32_t length){	gcm_crypt_f e = GET_GCM_ENCRYPT(key->strength);	e(output, gcm, key, input, length);}void crypton_aes_gcm_decrypt(uint8_t *output, aes_gcm *gcm, aes_key *key, uint8_t *input, uint32_t length){	gcm_crypt_f d = GET_GCM_DECRYPT(key->strength);	d(output, gcm, key, input, length);}/* * A CCM context takes the additional data at most once and then the whole * message in one call, of exactly the length given at init.  Anything else * fails the context: the output is zeroed and no tag is produced, since the * CBC-MAC would no longer cover what was processed. */#define CCM_PHASE_INIT   0#define CCM_PHASE_AAD    1#define CCM_PHASE_DATA   2#define CCM_PHASE_FAILED 3static int ccm_data_ok(uint8_t *output, aes_ccm *ccm, uint32_t length){	if (ccm->phase > CCM_PHASE_AAD || length != ccm->length_input) {		ccm->phase = CCM_PHASE_FAILED;		memset(output, 0, length);		return 0;	}	ccm->phase = CCM_PHASE_DATA;	return 1;}int crypton_aes_ccm_encrypt(uint8_t *output, aes_ccm *ccm, aes_key *key, uint8_t *input, uint32_t length){	ccm_crypt_f e = GET_CCM_ENCRYPT(key->strength);	if (!ccm_data_ok(output, ccm, length))		return 0;	e(output, ccm, key, input, length);	return 1;}int crypton_aes_ccm_decrypt(uint8_t *output, aes_ccm *ccm, aes_key *key, uint8_t *input, uint32_t length){	ccm_crypt_f d = GET_CCM_DECRYPT(key->strength);	if (!ccm_data_ok(output, ccm, length))		return 0;	d(output, ccm, key, input, length);	return 1;}void crypton_aes_ocb_encrypt(uint8_t *output, aes_ocb *ocb, aes_key *key, uint8_t *input, uint32_t length){	ocb_crypt_f e = GET_OCB_ENCRYPT(key->strength);	e(output, ocb, key, input, length);}void crypton_aes_ocb_decrypt(uint8_t *output, aes_ocb *ocb, aes_key *key, uint8_t *input, uint32_t length){	ocb_crypt_f d = GET_OCB_DECRYPT(key->strength);	d(output, ocb, key, input, length);}static void gcm_ghash_add(aes_gcm *gcm, block128 *b){	block128_xor(&gcm->tag, b);	crypton_gf_mul(&gcm->tag, gcm->htable);}/* Same, for four consecutive blocks.  Where the multiply is a carry-less * instruction this costs one reduction instead of four. */static void gcm_ghash_add4(aes_gcm *gcm, const block128 *b){	crypton_gf_mul4(&gcm->tag, b, gcm->htable);}/* The part of the state that depends on the key alone: H = encrypt_K(0^128) * and the table of its multiples.  It is 256 of the 320 bytes, and a caller * that keeps a key can compute it once instead of once per message. */void crypton_aes_gcm_key_init(aes_gcm_key *gk, aes_key *key){	block128 h;	block128_zero(&h);	crypton_aes_encrypt_block(&h, key, &h);	crypton_hinit(gk->gcm.htable, &h);#ifdef WITH_GCM_FUSED	if (crypton_aes_cpu_options[CPU_AESNI]	    && crypton_aes_cpu_options[CPU_PCLMUL])		crypton_gcm_fused_key_init(&gk->fused, key);#endif}/* Everything else: what the nonce and the message determine.  Leaves htable * alone, so it runs on a state whose key part is already there. */static void gcm_message_init(aes_gcm *gcm, uint8_t *iv, uint32_t len){	gcm->length_aad = 0;	gcm->length_input = 0;	block128_zero(&gcm->tag);	block128_zero(&gcm->iv);	if (len == 12) {		block128_copy_bytes(&gcm->iv, iv, 12);		gcm->iv.b[15] = 0x01;	} else {		uint32_t origlen = len << 3;		int i;		for (; len >= 16; len -= 16, iv += 16) {			block128_xor(&gcm->iv, (block128 *) iv);			crypton_gf_mul(&gcm->iv, gcm->htable);		}		if (len > 0) {			block128_xor_bytes(&gcm->iv, iv, len);			crypton_gf_mul(&gcm->iv, gcm->htable);		}		for (i = 15; origlen; --i, origlen >>= 8)			gcm->iv.b[i] ^= (uint8_t) origlen;		crypton_gf_mul(&gcm->iv, gcm->htable);	}	block128_copy_aligned(&gcm->civ, &gcm->iv);}void crypton_aes_gcm_init(aes_gcm *gcm, aes_key *key, uint8_t *iv, uint32_t len){	block128 h;	block128_zero(&h);	crypton_aes_encrypt_block(&h, key, &h);	crypton_hinit(gcm->htable, &h);	gcm_message_init(gcm, iv, len);}/* Crypto.Cipher.AES.Primitive reads length_input at this offset */typedef char crypton_gcm_length_input_offset[offsetof(aes_gcm, length_input) == 312 ? 1 : -1];/* Crypto.Cipher.AES.Primitive allocates sizeOCB bytes */typedef char crypton_ocb_size[sizeof(aes_ocb) <= 176 ? 1 : -1];void crypton_aes_gcm_aad(aes_gcm *gcm, uint8_t *input, uint32_t length){	gcm->length_aad += length;	for (; length >= 64; input += 64, length -= 64) {		gcm_ghash_add4(gcm, (const block128 *) input);	}	for (; length >= 16; input += 16, length -= 16) {		gcm_ghash_add(gcm, (block128 *) input);	}	if (length > 0) {		aes_block tmp;		block128_zero(&tmp);		block128_copy_bytes(&tmp, input, length);		gcm_ghash_add(gcm, &tmp);	}}void crypton_aes_gcm_finish(uint8_t *tag, aes_gcm *gcm, aes_key *key){	aes_block lblock;	int i;	/* tag = (tag-1 xor (lenbits(a) | lenbits(c)) ) . H */	lblock.q[0] = cpu_to_be64(gcm->length_aad << 3);	lblock.q[1] = cpu_to_be64(gcm->length_input << 3);	gcm_ghash_add(gcm, &lblock);	crypton_aes_encrypt_block(&lblock, key, &gcm->iv);	block128_xor_aligned(&gcm->tag, &lblock);	for (i = 0; i < 16; i++) {		tag[i] = gcm->tag.b[i];	}}/* One message, one call.  The key part of the state comes in already built, * the rest is set up on the stack, and the additional data, the encryption * and the tag all happen before returning, so nothing crosses a language * boundary between them and no intermediate state is copied out.  The output * buffer takes the ciphertext and then the tag, so it wants length + taglen * bytes. */void crypton_aes_gcm_full_encrypt(uint8_t *output, const aes_gcm_key *gcmkey, aes_key *key,                                  uint8_t *iv, uint32_t ivlen,                                  uint8_t *aad, uint32_t aadlen,                                  uint8_t *input, uint32_t length, uint32_t taglen){	aes_gcm gcm;	uint8_t tag[16];#ifdef WITH_GCM_FUSED	/* Short messages go the other way: the assembly below will not start	 * on anything under 288 bytes, and under about 1.5 KB the fused path	 * is ahead of it even where it does. */	if (ivlen == 12 && length <= CRYPTON_GCM_FUSED_MAX_MESSAGE	    && crypton_aes_cpu_options[CPU_AESNI]	    && crypton_aes_cpu_options[CPU_PCLMUL]) {		crypton_gcm_fused_encrypt(output, &gcmkey->fused, key, iv,		                          aad, aadlen, input, length, taglen,		                          NULL, 0, NULL);		return;	}#endif#ifdef WITH_ARMV8_CRYPTO	/* The same on AArch64, where the framing is what costs: composing the	 * additional data, the encryption and the tag reaches each through the	 * branch table, so the running state goes back to memory between them	 * and a one-block header pays a reduction of its own.  Measured on an	 * Apple M4, a 100-byte packet is 3.0x faster taken in one call.	 *	 * No length limit, unlike x86: there is no vendored assembly on this	 * side for a long message to be handed to instead, and measured	 * against the path this replaces it is never slower -- 1.25x at 1440	 * bytes, level from about 6 KB up. */	if (ivlen == 12	    && crypton_aes_cpu_options[CPU_AESNI]	    && crypton_aes_cpu_options[CPU_PCLMUL]) {		crypton_aes_armv8_gcm_fused(output, gcmkey->gcm.htable, key, iv,		                            aad, aadlen, input, length, taglen,		                            NULL, 0, NULL);		return;	}#endif	memcpy(gcm.htable, gcmkey->gcm.htable, sizeof(gcm.htable));	gcm_message_init(&gcm, iv, ivlen);	if (aadlen)		crypton_aes_gcm_aad(&gcm, aad, aadlen);	if (length)		crypton_aes_gcm_encrypt(output, &gcm, key, input, length);	crypton_aes_gcm_finish(tag, &gcm, key);	memcpy(output + length, tag, taglen);}/* The same, and then the header protection mask.  QUIC takes its sample from * the ciphertext, so the mask cannot be had before the encryption -- but it * can be had before returning, which saves a second crossing for one AES * block.  The block itself is about a nanosecond; what it saves is the call. * sampleoff is where the sixteen bytes of sample start in the output. */void crypton_aes_gcm_full_encrypt_mask(uint8_t *output, const aes_gcm_key *gcmkey, aes_key *key,                                       uint8_t *iv, uint32_t ivlen,                                       uint8_t *aad, uint32_t aadlen,                                       uint8_t *input, uint32_t length, uint32_t taglen,                                       aes_key *hpkey, uint32_t sampleoff, uint8_t *mask){	block128 sample, m;#ifdef WITH_GCM_FUSED	/* Here the mask rides in a lane of the AES pipeline that the message	 * length leaves idle, so it costs very nearly nothing on top of the	 * encryption rather than a block of its own. */	if (ivlen == 12 && length <= CRYPTON_GCM_FUSED_MAX_MESSAGE	    && crypton_aes_cpu_options[CPU_AESNI]	    && crypton_aes_cpu_options[CPU_PCLMUL]) {		crypton_gcm_fused_encrypt(output, &gcmkey->fused, key, iv,		                          aad, aadlen, input, length, taglen,		                          hpkey, sampleoff, mask);		return;	}#endif#ifdef WITH_ARMV8_CRYPTO	/* The same on AArch64, where the framing is what costs: composing the	 * additional data, the encryption and the tag reaches each through the	 * branch table, so the running state goes back to memory between them	 * and a one-block header pays a reduction of its own.  Measured on an	 * Apple M4, a 100-byte packet is 3.3x faster taken in one call. */	if (ivlen == 12	    && crypton_aes_cpu_options[CPU_AESNI]	    && crypton_aes_cpu_options[CPU_PCLMUL]) {		crypton_aes_armv8_gcm_fused(output, gcmkey->gcm.htable, key, iv,		                            aad, aadlen, input, length, taglen,		                            hpkey, sampleoff, mask);		return;	}#endif	crypton_aes_gcm_full_encrypt(output, gcmkey, key, iv, ivlen, aad, aadlen,	                             input, length, taglen);	/* copied rather than cast: the sample lands wherever the header put it	 * and a block128 is read as 64-bit words */	memcpy(&sample, output + sampleoff, 16);	crypton_aes_encrypt_block(&m, hpkey, &sample);	memcpy(mask, &m, 16);}/* The same the other way, with the tag checked here rather than by the * caller: returns 1 when it matches and 0 when it does not, comparing every * byte either way.  The plaintext is written whatever the answer, so a caller * that gets 0 must not use it. *//* Decrypt, and either compare the tag or hand it back. * * With outtag NULL this is the verifying form: the tag is compared here, a * byte at a time over its whole length whichever way the answer goes, and the * answer is the return value.  With outtag not NULL the computed tag is * written there instead and the return value is 1 -- for a caller that holds * the expected tag in a form of its own and will compare it itself. */static int gcm_full_decrypt(uint8_t *output, const aes_gcm_key *gcmkey, aes_key *key,                            uint8_t *iv, uint32_t ivlen,                            uint8_t *aad, uint32_t aadlen,                            uint8_t *input, uint32_t length,                            const uint8_t *tag, uint32_t taglen,                            uint8_t *outtag){	aes_gcm gcm;	uint8_t expected[16];	uint32_t i;	uint8_t diff = 0;#ifdef WITH_GCM_FUSED	/* The same as the encryption side, and simpler: what GHASH absorbs	 * here is the ciphertext, which is the input, so the multiplies need	 * not wait for anything.  Measured on an Intel Haswell, a 100-byte	 * packet was three times the cost of encrypting one before this. */	if (ivlen == 12 && length <= CRYPTON_GCM_FUSED_MAX_MESSAGE	    && crypton_aes_cpu_options[CPU_AESNI]	    && crypton_aes_cpu_options[CPU_PCLMUL])		return crypton_gcm_fused_decrypt(output, &gcmkey->fused, key,		                                 iv, aad, aadlen, input,		                                 length, tag, taglen, outtag);#endif#ifdef WITH_ARMV8_CRYPTO	if (ivlen == 12	    && crypton_aes_cpu_options[CPU_AESNI]	    && crypton_aes_cpu_options[CPU_PCLMUL])		return crypton_aes_armv8_gcm_fused_dec(output, gcmkey->gcm.htable,		                                       key, iv, aad, aadlen,		                                       input, length, tag, taglen,		                                       outtag);#endif	memcpy(gcm.htable, gcmkey->gcm.htable, sizeof(gcm.htable));	gcm_message_init(&gcm, iv, ivlen);	if (aadlen)		crypton_aes_gcm_aad(&gcm, aad, aadlen);	if (length)		crypton_aes_gcm_decrypt(output, &gcm, key, input, length);	crypton_aes_gcm_finish(expected, &gcm, key);	if (outtag) {		memcpy(outtag, expected, taglen);		return 1;	}	for (i = 0; i < taglen; i++)		diff |= (uint8_t) (expected[i] ^ tag[i]);	return diff == 0;}int crypton_aes_gcm_full_decrypt(uint8_t *output, const aes_gcm_key *gcmkey, aes_key *key,                                 uint8_t *iv, uint32_t ivlen,                                 uint8_t *aad, uint32_t aadlen,                                 uint8_t *input, uint32_t length,                                 const uint8_t *tag, uint32_t taglen){	return gcm_full_decrypt(output, gcmkey, key, iv, ivlen, aad, aadlen,	                        input, length, tag, taglen, NULL);}void crypton_aes_gcm_full_decrypt_tag(uint8_t *output, uint8_t *outtag,                                      const aes_gcm_key *gcmkey, aes_key *key,                                      uint8_t *iv, uint32_t ivlen,                                      uint8_t *aad, uint32_t aadlen,                                      uint8_t *input, uint32_t length,                                      uint32_t taglen){	(void) gcm_full_decrypt(output, gcmkey, key, iv, ivlen, aad, aadlen,	                        input, length, NULL, taglen, outtag);}static inline uint8_t ccm_b0_flags(uint32_t has_adata, uint32_t m, uint32_t l){	return 8*m + l + (has_adata? 64: 0);}/* depends on input size */static void ccm_encode_b0(block128* output, aes_ccm* ccm, uint32_t has_adata){	int last = 15;	uint32_t m = ccm->length_M;	uint32_t l = ccm->length_L;	uint32_t msg_len = ccm->length_input;	block128_zero(output);	block128_copy_aligned(output, &ccm->nonce);	output->b[0] = ccm_b0_flags(has_adata, (m-2)/2, l-1);	while (msg_len > 0) {		output->b[last--] = msg_len & 0xff;		msg_len >>= 8;	}}/* encode adata length */static int ccm_encode_la(block128* output, uint32_t la){	if (la < ( (1 << 16) - (1 << 8)) ) {		output->b[0] = (la >> 8) & 0xff;		output->b[1] = la        & 0xff;		return 2;	} else {		output->b[0] = 0xff;		output->b[1] = 0xfe;		output->b[2] = (la >> 24) & 0xff;		output->b[3] = (la >> 16) & 0xff;		output->b[4] = (la >>  8) & 0xff;		output->b[5] = la         & 0xff;		return 6;	}}static void ccm_encode_ctr(block128* out, aes_ccm* ccm, unsigned int cnt){	int last = 15;	block128_copy_aligned(out, &ccm->nonce);	out->b[0] = ccm->length_L - 1;	while (cnt > 0) {		out->b[last--] = cnt & 0xff;		cnt >>= 8;	}}static void ccm_cbcmac_add(aes_ccm* ccm, aes_key* key, block128* bi){	block128_xor_aligned(&ccm->xi, bi);	crypton_aes_encrypt_block(&ccm->xi, key, &ccm->xi);}/* even though it is possible to support message size as large as 2^64, we support up to 2^32 only */void crypton_aes_ccm_init(aes_ccm *ccm, aes_key *key, uint8_t *nonce, uint32_t nonce_len, uint32_t input_size, int m, int l){	memset(ccm, 0, sizeof(aes_ccm));	ccm->phase = CCM_PHASE_FAILED;	if (l < 2 || l > 4) return;	if (m != 4 && m != 6 && m != 8 && m != 10		   && m != 12 && m != 14 && m != 16) return;	/* the nonce is exactly 15 - L bytes, or the message length overlaps it */	if (nonce_len != (uint32_t) (15 - l)) return;	if (input_size >= (1ull << (8*l))) return;	ccm->phase = CCM_PHASE_INIT;	ccm->length_L = l;	ccm->length_M = m;	ccm->length_input = input_size;	memcpy(&ccm->nonce.b[1], nonce, nonce_len);	ccm_encode_b0(&ccm->b0, ccm, 1); /* assume aad is present */	crypton_aes_encrypt_block(&ccm->xi, key, &ccm->b0);}/* even though l(a) can be as large as 2^64, we only handle aad up to 2 ^ 32 for practical reasons.  Also we don't support incremental aad add, because the 1st encoded adata has length information */void crypton_aes_ccm_aad(aes_ccm *ccm, aes_key *key, uint8_t *input, uint32_t length){	block128 tmp;	if (ccm->phase != CCM_PHASE_INIT) {		ccm->phase = CCM_PHASE_FAILED;		return;	}	ccm->phase = CCM_PHASE_AAD;	ccm->length_aad = length;	int len_len;	block128_zero(&tmp);	len_len = ccm_encode_la(&tmp, length);	if (length < 16 - len_len) {		memcpy(&tmp.b[len_len], input, length);		length = 0;	} else {		memcpy(&tmp.b[len_len], input, 16 - len_len);		input += 16 - len_len;		length -= 16 - len_len;	}	ccm_cbcmac_add(ccm, key, &tmp);	for (; length >= 16; input += 16, length -= 16) {		block128_copy(&tmp, (block128*)input);		ccm_cbcmac_add(ccm, key, &tmp);	}	if (length > 0) {		block128_zero(&tmp);		block128_copy_bytes(&tmp, input, length);		ccm_cbcmac_add(ccm, key, &tmp);	}	block128_copy_aligned(&ccm->header_cbcmac, &ccm->xi);}int crypton_aes_ccm_finish(uint8_t *tag, aes_ccm *ccm, aes_key *key){	block128 iv, s0;	/* no data call is fine only for an empty message */	if (ccm->phase == CCM_PHASE_FAILED	    || (ccm->phase != CCM_PHASE_DATA && ccm->length_input != 0)) {		memset(tag, 0, 16);		return 0;	}	if (ccm->phase != CCM_PHASE_DATA && ccm->length_aad == 0) {		ccm_encode_b0(&ccm->b0, ccm, 0);		crypton_aes_encrypt_block(&ccm->xi, key, &ccm->b0);	}	block128_zero(&iv);	ccm_encode_ctr(&iv, ccm, 0);	crypton_aes_encrypt_block(&s0, key, &iv);	block128_vxor((block128*)tag, &ccm->xi, &s0);	return 1;}static inline void ocb_block_double(block128 *d, block128 *s){	unsigned int i;	uint8_t tmp = s->b[0];	for (i=0; i<15; i++)		d->b[i] = (s->b[i] << 1) | (s->b[i+1] >> 7);	d->b[15] = (s->b[15] << 1) ^ ((tmp >> 7) * 0x87);}static void ocb_get_L_i(block128 *l, block128 *lis, uint64_t n){#define L_CACHED 4	unsigned int i = (uint32_t) n ? bitfn_ntz((uint32_t) n)	                              : 32 + bitfn_ntz((uint32_t) (n >> 32));	if (i < L_CACHED) {		block128_copy(l, &lis[i]);	} else {		i -= (L_CACHED - 1);		block128_copy(l, &lis[L_CACHED - 1]);		while (i--) {			ocb_block_double(l, l);		}	}#undef L_CACHED}/* * OCB's offsets are a running exclusive-or, so they have to be worked out in * order, but the block cipher calls under them do not depend on each other: * every block is offset, encrypted, and offset again.  So the offsets are * computed a group at a time and the group goes through ECB together, which * is where the code written for the AES instructions interleaves eight blocks * and covers the latency of AESENC.  One block at a time left that idle and * cost four times what GCM costs on the same machine, for a mode that does * less work than GCM. * * Eight is what the ECB paths interleave; a group beyond that gains nothing * and only makes the buffers larger. */#define OCB_WAY 8void crypton_aes_ocb_init(aes_ocb *ocb, aes_key *key, uint8_t *iv, uint32_t len, uint32_t taglen){	block128 tmp, nonce, ktop;	unsigned char stretch[24];	unsigned bottom, byteshift, bitshift, i;	/* we don't accept more than 15 bytes, any bytes higher will be ignored. */	if (len > 15) {		len = 15;	}	if (taglen > 16) {		taglen = 16;	}	/* create L*, and L$,L0,L1,L2,L3 */	block128_zero(&tmp);	crypton_aes_encrypt_block(&ocb->lstar, key, &tmp);	ocb_block_double(&ocb->ldollar, &ocb->lstar);	ocb_block_double(&ocb->li[0], &ocb->ldollar);	ocb_block_double(&ocb->li[1], &ocb->li[0]);	ocb_block_double(&ocb->li[2], &ocb->li[1]);	ocb_block_double(&ocb->li[3], &ocb->li[2]);	/* create stretch from the nonce */	block128_zero(&nonce);	if (len > 0) {		memcpy(nonce.b + (16 - len), iv, len);		nonce.b[16 - len - 1] |= 0x01;	}	nonce.b[0] |= (unsigned char)(((taglen * 8) % 128) << 1);	bottom = nonce.b[15] & 0x3F;	nonce.b[15] &= 0xC0;	crypton_aes_encrypt_block(&ktop, key, &nonce);	memcpy(stretch, ktop.b, 16);	memcpy(tmp.b, ktop.b + 1, 8);	block128_xor_aligned(&tmp, &ktop);	memcpy(stretch + 16, tmp.b, 8);	/* initialize the encryption offset from stretch */	byteshift = bottom / 8;	bitshift = bottom % 8;	if (bitshift != 0)		for (i = 0; i < 16; i++)			ocb->offset_enc.b[i] = (stretch[i+byteshift] << bitshift)			                     | (stretch[i+byteshift+1] >> (8-bitshift));	else		for (i = 0; i < 16; i++)			ocb->offset_enc.b[i] = stretch[i+byteshift];	/* initialize checksum for aad and encryption, and the aad offset */	block128_zero(&ocb->sum_aad);	block128_zero(&ocb->sum_enc);	block128_zero(&ocb->offset_aad);	ocb->blocks_aad = 0;	ocb->blocks_enc = 0;}void crypton_aes_ocb_aad(aes_ocb *ocb, aes_key *key, uint8_t *input, uint32_t length){	block128 tmp;	block128 buf[OCB_WAY];	uint32_t blocks = length / 16;	uint64_t i = ocb->blocks_aad + 1;	unsigned int j;	ocb->blocks_aad += blocks;	for (; blocks >= OCB_WAY; blocks -= OCB_WAY, input += 16 * OCB_WAY) {		for (j = 0; j < OCB_WAY; j++, i++) {			ocb_get_L_i(&tmp, ocb->li, i);			block128_xor_aligned(&ocb->offset_aad, &tmp);			block128_vxor(&buf[j], &ocb->offset_aad,			              (block128 *) (input + 16 * j));		}		crypton_aes_encrypt_ecb(buf, key, buf, OCB_WAY);		for (j = 0; j < OCB_WAY; j++)			block128_xor_aligned(&ocb->sum_aad, &buf[j]);	}	for (; blocks > 0; blocks--, i++, input += 16) {		ocb_get_L_i(&tmp, ocb->li, i);		block128_xor_aligned(&ocb->offset_aad, &tmp);		block128_vxor(&tmp, &ocb->offset_aad, (block128 *) input);		crypton_aes_encrypt_block(&tmp, key, &tmp);		block128_xor_aligned(&ocb->sum_aad, &tmp);	}	length = length % 16; /* Bytes in final block */	if (length > 0) {		block128_xor_aligned(&ocb->offset_aad, &ocb->lstar);		block128_zero(&tmp);		block128_copy_bytes(&tmp, input, length);		tmp.b[length] = 0x80;		block128_xor_aligned(&tmp, &ocb->offset_aad);		crypton_aes_encrypt_block(&tmp, key, &tmp);		block128_xor_aligned(&ocb->sum_aad, &tmp);	}}void crypton_aes_ocb_finish(uint8_t *tag, aes_ocb *ocb, aes_key *key){	block128 tmp;	block128_vxor_aligned(&tmp, &ocb->sum_enc, &ocb->offset_enc);	block128_xor_aligned(&tmp, &ocb->ldollar);	crypton_aes_encrypt_block((block128 *) tag, key, &tmp);	block128_xor((block128 *) tag, &ocb->sum_aad);}/* * These three reach cbits/aes/generic.c directly rather than through the * branch table, so they run only where the portable implementation was the * one installed -- which is what lets them read its schedule and hand its * core four blocks at a time.  The CTR entries below cannot do the same: * they go through the branch table for the block, and so run on accelerated * machines too. */void crypton_aes_generic_encrypt_ecb(aes_block *output, aes_key *key, aes_block *input, uint32_t nb_blocks){	aes_sched sched;	crypton_aes_generic_schedule(&sched, key);	crypton_aes_generic_blocks((uint8_t *) output, (const uint8_t *) input,	                           nb_blocks, &sched, 0);}void crypton_aes_generic_decrypt_ecb(aes_block *output, aes_key *key, aes_block *input, uint32_t nb_blocks){	aes_sched sched;	crypton_aes_generic_schedule(&sched, key);	crypton_aes_generic_blocks((uint8_t *) output, (const uint8_t *) input,	                           nb_blocks, &sched, 1);}void crypton_aes_generic_encrypt_cbc(aes_block *output, aes_key *key, aes_block *iv, aes_block *input, uint32_t nb_blocks){	aes_block block;	/* preload IV in block */	block128_copy(&block, iv);	for ( ; nb_blocks-- > 0; input++, output++) {		block128_xor(&block, (block128 *) input);		crypton_aes_generic_encrypt_block(&block, key, &block);		block128_copy((block128 *) output, &block);	}}/* * Decryption, unlike encryption, does not wait for the block before it: * every ciphertext block is ready at once and the chaining is a XOR * afterwards.  So four at a pass, with the ciphertext copied aside first, * since the caller is allowed to decrypt in place and the next group's IV * is the last ciphertext block of this one. */void crypton_aes_generic_decrypt_cbc(aes_block *output, aes_key *key, aes_block *ivini, aes_block *input, uint32_t nb_blocks){	aes_sched sched;	aes_block iv;	uint8_t ct[64], plain[64];	crypton_aes_generic_schedule(&sched, key);	block128_copy(&iv, ivini);	while (nb_blocks > 0) {		uint32_t n = nb_blocks < 4 ? nb_blocks : 4;		uint32_t i;		memcpy(ct, input, n * 16);		crypton_aes_generic_blocks(plain, ct, n, &sched, 1);		for (i = 0; i < n; i++) {			block128_vxor((block128 *) (output + i),			              (block128 *) (plain + 16 * i), &iv);			block128_copy(&iv, (block128 *) (ct + 16 * i));		}		input += n;		output += n;		nb_blocks -= n;	}}void crypton_aes_generic_encrypt_ctr(uint8_t *output, aes_key *key, aes_block *iv, uint8_t *input, uint32_t len){	aes_block block, o;	uint32_t nb_blocks = len / 16;	int i;	/* preload IV in block */	block128_copy(&block, iv);	for ( ; nb_blocks-- > 0; block128_inc_be(&block), output += 16, input += 16) {		crypton_aes_encrypt_block(&o, key, &block);		block128_vxor((block128 *) output, &o, (block128 *) input);	}	if ((len % 16) != 0) {		crypton_aes_encrypt_block(&o, key, &block);		for (i = 0; i < (len % 16); i++) {			*output = ((uint8_t *) &o)[i] ^ *input;			output++;			input++;		}	}}void crypton_aes_generic_encrypt_c32(uint8_t *output, aes_key *key, aes_block *iv, uint8_t *input, uint32_t len){	aes_block block, o;	uint32_t nb_blocks = len / 16;	int i;	/* preload IV in block */	block128_copy(&block, iv);	for ( ; nb_blocks-- > 0; block128_inc32_le(&block), output += 16, input += 16) {		crypton_aes_encrypt_block(&o, key, &block);		block128_vxor((block128 *) output, &o, (block128 *) input);	}	if ((len % 16) != 0) {		crypton_aes_encrypt_block(&o, key, &block);		for (i = 0; i < (len % 16); i++) {			*output = ((uint8_t *) &o)[i] ^ *input;			output++;			input++;		}	}}void crypton_aes_generic_encrypt_xts(aes_block *output, aes_key *k1, aes_key *k2, aes_block *dataunit,                             uint32_t spoint, aes_block *input, uint32_t nb_blocks){	aes_sched sched;	aes_block tweak;	uint8_t buf[64], tw[64];	/* load IV and encrypt it using k2 as the tweak */	block128_copy(&tweak, dataunit);	crypton_aes_encrypt_block(&tweak, k2, &tweak);	/* TO OPTIMISE: this is really inefficient way to do that */	while (spoint-- > 0)		crypton_aes_generic_gf_mulx(&tweak);	crypton_aes_generic_schedule(&sched, k1);	while (nb_blocks > 0) {		uint32_t n = nb_blocks < 4 ? nb_blocks : 4;		uint32_t i;		/* the tweaks for a group depend on nothing but each other, so		 * all four are known before any block is enciphered */		for (i = 0; i < n; i++) {			block128_copy((block128 *) (tw + 16 * i), &tweak);			block128_vxor((block128 *) (buf + 16 * i), input + i, &tweak);			crypton_aes_generic_gf_mulx(&tweak);		}		crypton_aes_generic_blocks(buf, buf, n, &sched, 0);		for (i = 0; i < n; i++)			block128_vxor(output + i, (block128 *) (buf + 16 * i),			              (block128 *) (tw + 16 * i));		input += n;		output += n;		nb_blocks -= n;	}}void crypton_aes_generic_decrypt_xts(aes_block *output, aes_key *k1, aes_key *k2, aes_block *dataunit,                             uint32_t spoint, aes_block *input, uint32_t nb_blocks){	aes_sched sched;	aes_block tweak;	uint8_t buf[64], tw[64];	/* load IV and encrypt it using k2 as the tweak */	block128_copy(&tweak, dataunit);	crypton_aes_encrypt_block(&tweak, k2, &tweak);	/* TO OPTIMISE: this is really inefficient way to do that */	while (spoint-- > 0)		crypton_aes_generic_gf_mulx(&tweak);	crypton_aes_generic_schedule(&sched, k1);	while (nb_blocks > 0) {		uint32_t n = nb_blocks < 4 ? nb_blocks : 4;		uint32_t i;		/* the tweaks for a group depend on nothing but each other, so		 * all four are known before any block is enciphered */		for (i = 0; i < n; i++) {			block128_copy((block128 *) (tw + 16 * i), &tweak);			block128_vxor((block128 *) (buf + 16 * i), input + i, &tweak);			crypton_aes_generic_gf_mulx(&tweak);		}		crypton_aes_generic_blocks(buf, buf, n, &sched, 1);		for (i = 0; i < n; i++)			block128_vxor(output + i, (block128 *) (buf + 16 * i),			              (block128 *) (tw + 16 * i));		input += n;		output += n;		nb_blocks -= n;	}}void crypton_aes_generic_gcm_encrypt(uint8_t *output, aes_gcm *gcm, aes_key *key, uint8_t *input, uint32_t length){	aes_block out;	gcm->length_input += length;	/* four blocks at a time, so GHASH can fold them into one reduction */	for (; length >= 64; input += 64, output += 64, length -= 64) {		aes_block buf[4];		int i;		for (i = 0; i < 4; i++) {			block128_inc32_be(&gcm->civ);			crypton_aes_encrypt_block(&buf[i], key, &gcm->civ);			block128_xor(&buf[i], (block128 *) (input + 16 * i));		}		gcm_ghash_add4(gcm, buf);		for (i = 0; i < 4; i++)			block128_copy((block128 *) (output + 16 * i), &buf[i]);	}	for (; length >= 16; input += 16, output += 16, length -= 16) {		block128_inc32_be(&gcm->civ);		crypton_aes_encrypt_block(&out, key, &gcm->civ);		block128_xor(&out, (block128 *) input);		gcm_ghash_add(gcm, &out);		block128_copy((block128 *) output, &out);	}	if (length > 0) {		aes_block tmp;		int i;		block128_inc32_be(&gcm->civ);		/* create e(civ) in out */		crypton_aes_encrypt_block(&out, key, &gcm->civ);		/* initialize a tmp as input and xor it to e(civ) */		block128_zero(&tmp);		block128_copy_bytes(&tmp, input, length);		block128_xor_bytes(&tmp, out.b, length);		gcm_ghash_add(gcm, &tmp);		for (i = 0; i < length; i++) {			output[i] = tmp.b[i];		}	}}void crypton_aes_generic_gcm_decrypt(uint8_t *output, aes_gcm *gcm, aes_key *key, uint8_t *input, uint32_t length){	aes_block out;	gcm->length_input += length;	/* GHASH all four ciphertext blocks before writing any plaintext, since	 * output may be input */	for (; length >= 64; input += 64, output += 64, length -= 64) {		int i;		gcm_ghash_add4(gcm, (const block128 *) input);		for (i = 0; i < 4; i++) {			block128_inc32_be(&gcm->civ);			crypton_aes_encrypt_block(&out, key, &gcm->civ);			block128_xor(&out, (block128 *) (input + 16 * i));			block128_copy((block128 *) (output + 16 * i), &out);		}	}	for (; length >= 16; input += 16, output += 16, length -= 16) {		block128_inc32_be(&gcm->civ);		crypton_aes_encrypt_block(&out, key, &gcm->civ);		gcm_ghash_add(gcm, (block128 *) input);		block128_xor(&out, (block128 *) input);		block128_copy((block128 *) output, &out);	}	if (length > 0) {		aes_block tmp;		int i;		block128_inc32_be(&gcm->civ);		block128_zero(&tmp);		block128_copy_bytes(&tmp, input, length);		gcm_ghash_add(gcm, &tmp);		crypton_aes_encrypt_block(&out, key, &gcm->civ);		block128_xor_bytes(&tmp, out.b, length);		for (i = 0; i < length; i++) {			output[i] = tmp.b[i];		}	}}static void ocb_generic_crypt(uint8_t *output, aes_ocb *ocb, aes_key *key,                              uint8_t *input, uint32_t length, int encrypt){	block128 tmp, pad;	block128 offsets[OCB_WAY], buf[OCB_WAY];	uint32_t blocks = length / 16;	uint64_t i = ocb->blocks_enc + 1;	unsigned int j;	ocb->blocks_enc += blocks;	for (; blocks >= OCB_WAY;	     blocks -= OCB_WAY, input += 16 * OCB_WAY, output += 16 * OCB_WAY) {		for (j = 0; j < OCB_WAY; j++, i++) {			/* Offset_i = Offset_{i-1} xor L_{ntz(i)} */			ocb_get_L_i(&tmp, ocb->li, i);			block128_xor_aligned(&ocb->offset_enc, &tmp);			block128_copy_aligned(&offsets[j], &ocb->offset_enc);			block128_vxor(&buf[j], &ocb->offset_enc,			              (block128 *) (input + 16 * j));		}		if (encrypt)			crypton_aes_encrypt_ecb(buf, key, buf, OCB_WAY);		else			crypton_aes_decrypt_ecb(buf, key, buf, OCB_WAY);		for (j = 0; j < OCB_WAY; j++) {			block128_vxor((block128 *) (output + 16 * j),			              &offsets[j], &buf[j]);			block128_xor(&ocb->sum_enc,			             (block128 *) ((encrypt ? input : output)			                           + 16 * j));		}	}	/* and what is left of the message, a block at a time */	for (; blocks > 0; blocks--, i++, input += 16, output += 16) {		ocb_get_L_i(&tmp, ocb->li, i);		block128_xor_aligned(&ocb->offset_enc, &tmp);		block128_vxor(&tmp, &ocb->offset_enc, (block128 *) input);		if (encrypt) {			crypton_aes_encrypt_block(&tmp, key, &tmp);			block128_vxor((block128 *) output, &ocb->offset_enc, &tmp);			block128_xor(&ocb->sum_enc, (block128 *) input);		} else {			crypton_aes_decrypt_block(&tmp, key, &tmp);			block128_vxor((block128 *) output, &ocb->offset_enc, &tmp);			block128_xor(&ocb->sum_enc, (block128 *) output);		}	}	/* process the last partial block if any */	length = length % 16;	if (length > 0) {		block128_xor_aligned(&ocb->offset_enc, &ocb->lstar);		crypton_aes_encrypt_block(&pad, key, &ocb->offset_enc);		if (encrypt) {			block128_zero(&tmp);			block128_copy_bytes(&tmp, input, length);			tmp.b[length] = 0x80;			block128_xor_aligned(&ocb->sum_enc, &tmp);			block128_xor_aligned(&pad, &tmp);			memcpy(output, pad.b, length);			output += length;		} else {			block128_copy_aligned(&tmp, &pad);			block128_copy_bytes(&tmp, input, length);			block128_xor_aligned(&tmp, &pad);			tmp.b[length] = 0x80;			memcpy(output, tmp.b, length);			block128_xor_aligned(&ocb->sum_enc, &tmp);			input += length;		}	}}void crypton_aes_generic_ccm_encrypt(uint8_t *output, aes_ccm *ccm, aes_key *key, uint8_t *input, uint32_t length){	block128 tmp, ctr;	/* when aad is absent, reset b0 block */	if (ccm->length_aad == 0) {		ccm_encode_b0(&ccm->b0, ccm, 0); /* assume aad is present */		crypton_aes_encrypt_block(&ccm->xi, key, &ccm->b0);		block128_copy_aligned(&ccm->header_cbcmac, &ccm->xi);	}	if (length != ccm->length_input) {		return;	}	ccm_encode_ctr(&ctr, ccm, 1);	crypton_aes_encrypt_ctr(output, key, &ctr, input, length);	for (;length >= 16; input += 16, length -= 16) {		block128_copy(&tmp, (block128*)input);		ccm_cbcmac_add(ccm, key, &tmp);	}	if (length > 0) {		block128_zero(&tmp);		block128_copy_bytes(&tmp, input, length);		ccm_cbcmac_add(ccm, key, &tmp);	}}void crypton_aes_generic_ccm_decrypt(uint8_t *output, aes_ccm *ccm, aes_key *key, uint8_t *input, uint32_t length){	block128 tmp, ctr;	if (length != ccm->length_input) {		return;	}	/* when aad is absent, reset b0 block */	if (ccm->length_aad == 0) {		ccm_encode_b0(&ccm->b0, ccm, 0); /* assume aad is present */		crypton_aes_encrypt_block(&ccm->xi, key, &ccm->b0);		block128_copy_aligned(&ccm->header_cbcmac, &ccm->xi);	}	ccm_encode_ctr(&ctr, ccm, 1);	crypton_aes_encrypt_ctr(output, key, &ctr, input, length);	block128_copy_aligned(&ccm->xi, &ccm->header_cbcmac);	input = output;	for (;length >= 16; input += 16, length -= 16) {		block128_copy(&tmp, (block128*)input);		ccm_cbcmac_add(ccm, key, &tmp);	}	if (length > 0) {		block128_zero(&tmp);		block128_copy_bytes(&tmp, input, length);		ccm_cbcmac_add(ccm, key, &tmp);	}}/* * GCM where the portable implementation is the one that will run. * * The same shape as the two above, with two differences: the four counter * blocks of a group are encrypted together, which is what the bitsliced * core is for, and the schedule is expanded once for the message rather * than once per block. * * These cannot replace the generic pair.  That pair also runs on an x86 * machine that has AES-NI and no carry-less multiply, where crypton_aes.c * installs the AES-NI block function and leaves GCM alone -- and there the * key holds the AES-NI schedule, not this one.  So the choice is made where * the rest of the portable entries are chosen. */void crypton_aes_bitsliced_gcm_encrypt(uint8_t *output, aes_gcm *gcm, aes_key *key, uint8_t *input, uint32_t length){	aes_sched sched;	aes_block out;	crypton_aes_generic_schedule(&sched, key);	gcm->length_input += length;	for (; length >= 64; input += 64, output += 64, length -= 64) {		aes_block buf[4];		int i;		for (i = 0; i < 4; i++) {			block128_inc32_be(&gcm->civ);			block128_copy(&buf[i], &gcm->civ);		}		crypton_aes_generic_blocks((uint8_t *) buf, (const uint8_t *) buf,		                           4, &sched, 0);		for (i = 0; i < 4; i++)			block128_xor(&buf[i], (block128 *) (input + 16 * i));		gcm_ghash_add4(gcm, buf);		for (i = 0; i < 4; i++)			block128_copy((block128 *) (output + 16 * i), &buf[i]);	}	for (; length >= 16; input += 16, output += 16, length -= 16) {		block128_inc32_be(&gcm->civ);		crypton_aes_generic_blocks((uint8_t *) &out,		                           (const uint8_t *) &gcm->civ, 1, &sched, 0);		block128_xor(&out, (block128 *) input);		gcm_ghash_add(gcm, &out);		block128_copy((block128 *) output, &out);	}	if (length > 0) {		aes_block tmp;		uint32_t i;		block128_inc32_be(&gcm->civ);		crypton_aes_generic_blocks((uint8_t *) &out,		                           (const uint8_t *) &gcm->civ, 1, &sched, 0);		block128_zero(&tmp);		block128_copy_bytes(&tmp, input, length);		block128_xor_bytes(&tmp, out.b, length);		gcm_ghash_add(gcm, &tmp);		for (i = 0; i < length; i++)			output[i] = tmp.b[i];	}}void crypton_aes_bitsliced_gcm_decrypt(uint8_t *output, aes_gcm *gcm, aes_key *key, uint8_t *input, uint32_t length){	aes_sched sched;	aes_block out;	crypton_aes_generic_schedule(&sched, key);	gcm->length_input += length;	/* GHASH all four ciphertext blocks before writing any plaintext, since	 * output may be input */	for (; length >= 64; input += 64, output += 64, length -= 64) {		aes_block buf[4];		int i;		gcm_ghash_add4(gcm, (const block128 *) input);		for (i = 0; i < 4; i++) {			block128_inc32_be(&gcm->civ);			block128_copy(&buf[i], &gcm->civ);		}		crypton_aes_generic_blocks((uint8_t *) buf, (const uint8_t *) buf,		                           4, &sched, 0);		for (i = 0; i < 4; i++) {			block128_xor(&buf[i], (block128 *) (input + 16 * i));			block128_copy((block128 *) (output + 16 * i), &buf[i]);		}	}	for (; length >= 16; input += 16, output += 16, length -= 16) {		block128_inc32_be(&gcm->civ);		crypton_aes_generic_blocks((uint8_t *) &out,		                           (const uint8_t *) &gcm->civ, 1, &sched, 0);		gcm_ghash_add(gcm, (block128 *) input);		block128_xor(&out, (block128 *) input);		block128_copy((block128 *) output, &out);	}	if (length > 0) {		aes_block tmp;		uint32_t i;		block128_inc32_be(&gcm->civ);		crypton_aes_generic_blocks((uint8_t *) &out,		                           (const uint8_t *) &gcm->civ, 1, &sched, 0);		block128_zero(&tmp);		block128_copy_bytes(&tmp, input, length);		gcm_ghash_add(gcm, &tmp);		block128_xor_bytes(&tmp, out.b, length);		for (i = 0; i < length; i++)			output[i] = tmp.b[i];	}}void crypton_aes_generic_ocb_encrypt(uint8_t *output, aes_ocb *ocb, aes_key *key, uint8_t *input, uint32_t length){	ocb_generic_crypt(output, ocb, key, input, length, 1);}void crypton_aes_generic_ocb_decrypt(uint8_t *output, aes_ocb *ocb, aes_key *key, uint8_t *input, uint32_t length){	ocb_generic_crypt(output, ocb, key, input, length, 0);}static inline void gf_mulx_rev(block128 *a, const block128 *h){	uint64_t v1 = cpu_to_le64(h->q[0]);	uint64_t v0 = cpu_to_le64(h->q[1]);	a->q[1] = cpu_to_be64(v1 >> 1 | v0 << 63);	a->q[0] = cpu_to_be64(v0 >> 1 ^ ((0-(v1 & 1)) & 0xe100000000000000ULL));}void crypton_aes_polyval_init(aes_polyval *ctx, const aes_block *h){	aes_block r;	/* ByteReverse(S_0) = 0 */	block128_zero(&ctx->s);	/* ByteReverse(H) * x */	gf_mulx_rev(&r, h);	crypton_hinit(ctx->htable, &r);}void crypton_aes_polyval_update(aes_polyval *ctx, const uint8_t *input, uint32_t length){	aes_block r;	const uint8_t *p;	uint32_t sz;	/* This automatically pads with zeros if input is not a multiple of the	   block size. */	for (p = input; length > 0; p += 16, length -= sz)	{		sz = length < 16 ? length : 16;		/* ByteReverse(X_j) */		block128_zero(&r);		memcpy(&r, p, sz);		block128_byte_reverse(&r);		/* ByteReverse(S_{j-1}) + ByteReverse(X_j) */		block128_xor_aligned(&ctx->s, &r);		/* ByteReverse(S_j) */		crypton_gf_mul(&ctx->s, ctx->htable);	}}void crypton_aes_polyval_finalize(aes_polyval *ctx, aes_block *dst){	/* S_s */	block128_copy_aligned(dst, &ctx->s);	block128_byte_reverse(dst);}