23c57df72989c916b3e98084eb88d48777999691mcpowers/*
23c57df72989c916b3e98084eb88d48777999691mcpowers * CDDL HEADER START
23c57df72989c916b3e98084eb88d48777999691mcpowers *
23c57df72989c916b3e98084eb88d48777999691mcpowers * The contents of this file are subject to the terms of the
23c57df72989c916b3e98084eb88d48777999691mcpowers * Common Development and Distribution License (the "License").
23c57df72989c916b3e98084eb88d48777999691mcpowers * You may not use this file except in compliance with the License.
23c57df72989c916b3e98084eb88d48777999691mcpowers *
23c57df72989c916b3e98084eb88d48777999691mcpowers * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
23c57df72989c916b3e98084eb88d48777999691mcpowers * or http://www.opensolaris.org/os/licensing.
23c57df72989c916b3e98084eb88d48777999691mcpowers * See the License for the specific language governing permissions
23c57df72989c916b3e98084eb88d48777999691mcpowers * and limitations under the License.
23c57df72989c916b3e98084eb88d48777999691mcpowers *
23c57df72989c916b3e98084eb88d48777999691mcpowers * When distributing Covered Code, include this CDDL HEADER in each
23c57df72989c916b3e98084eb88d48777999691mcpowers * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
23c57df72989c916b3e98084eb88d48777999691mcpowers * If applicable, add the following below this CDDL HEADER, with the
23c57df72989c916b3e98084eb88d48777999691mcpowers * fields enclosed by brackets "[]" replaced with your own identifying
23c57df72989c916b3e98084eb88d48777999691mcpowers * information: Portions Copyright [yyyy] [name of copyright owner]
23c57df72989c916b3e98084eb88d48777999691mcpowers *
23c57df72989c916b3e98084eb88d48777999691mcpowers * CDDL HEADER END
23c57df72989c916b3e98084eb88d48777999691mcpowers */
23c57df72989c916b3e98084eb88d48777999691mcpowers/*
e8c016ef6bb7e825057460cea4d904e2b90eb424Mark Powers * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23c57df72989c916b3e98084eb88d48777999691mcpowers * Use is subject to license terms.
23c57df72989c916b3e98084eb88d48777999691mcpowers */
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowers#ifndef _COMMON_CRYPTO_MODES_H
23c57df72989c916b3e98084eb88d48777999691mcpowers#define _COMMON_CRYPTO_MODES_H
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowers#ifdef __cplusplus
23c57df72989c916b3e98084eb88d48777999691mcpowersextern "C" {
23c57df72989c916b3e98084eb88d48777999691mcpowers#endif
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowers#include <sys/strsun.h>
23c57df72989c916b3e98084eb88d48777999691mcpowers#include <sys/systm.h>
23c57df72989c916b3e98084eb88d48777999691mcpowers#include <sys/sysmacros.h>
23c57df72989c916b3e98084eb88d48777999691mcpowers#include <sys/types.h>
23c57df72989c916b3e98084eb88d48777999691mcpowers#include <sys/errno.h>
23c57df72989c916b3e98084eb88d48777999691mcpowers#include <sys/rwlock.h>
23c57df72989c916b3e98084eb88d48777999691mcpowers#include <sys/kmem.h>
23c57df72989c916b3e98084eb88d48777999691mcpowers#include <sys/crypto/common.h>
23c57df72989c916b3e98084eb88d48777999691mcpowers#include <sys/crypto/impl.h>
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ECB_MODE 0x00000002
23c57df72989c916b3e98084eb88d48777999691mcpowers#define CBC_MODE 0x00000004
23c57df72989c916b3e98084eb88d48777999691mcpowers#define CTR_MODE 0x00000008
23c57df72989c916b3e98084eb88d48777999691mcpowers#define CCM_MODE 0x00000010
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers#define GCM_MODE 0x00000020
983a10335731bc55a0b7a37f195575fa109e30d4Mark Powers#define GMAC_MODE 0x00000040
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowers/*
23c57df72989c916b3e98084eb88d48777999691mcpowers * cc_keysched: Pointer to key schedule.
23c57df72989c916b3e98084eb88d48777999691mcpowers *
23c57df72989c916b3e98084eb88d48777999691mcpowers * cc_keysched_len: Length of the key schedule.
23c57df72989c916b3e98084eb88d48777999691mcpowers *
23c57df72989c916b3e98084eb88d48777999691mcpowers * cc_remainder: This is for residual data, i.e. data that can't
23c57df72989c916b3e98084eb88d48777999691mcpowers * be processed because there are too few bytes.
23c57df72989c916b3e98084eb88d48777999691mcpowers * Must wait until more data arrives.
23c57df72989c916b3e98084eb88d48777999691mcpowers *
23c57df72989c916b3e98084eb88d48777999691mcpowers * cc_remainder_len: Number of bytes in cc_remainder.
23c57df72989c916b3e98084eb88d48777999691mcpowers *
23c57df72989c916b3e98084eb88d48777999691mcpowers * cc_iv: Scratch buffer that sometimes contains the IV.
23c57df72989c916b3e98084eb88d48777999691mcpowers *
23c57df72989c916b3e98084eb88d48777999691mcpowers * cc_lastp: Pointer to previous block of ciphertext.
23c57df72989c916b3e98084eb88d48777999691mcpowers *
23c57df72989c916b3e98084eb88d48777999691mcpowers * cc_copy_to: Pointer to where encrypted residual data needs
23c57df72989c916b3e98084eb88d48777999691mcpowers * to be copied.
23c57df72989c916b3e98084eb88d48777999691mcpowers *
23c57df72989c916b3e98084eb88d48777999691mcpowers * cc_flags: PROVIDER_OWNS_KEY_SCHEDULE
23c57df72989c916b3e98084eb88d48777999691mcpowers * When a context is freed, it is necessary
23c57df72989c916b3e98084eb88d48777999691mcpowers * to know whether the key schedule was allocated
23c57df72989c916b3e98084eb88d48777999691mcpowers * by the caller, or internally, e.g. an init routine.
23c57df72989c916b3e98084eb88d48777999691mcpowers * If allocated by the latter, then it needs to be freed.
23c57df72989c916b3e98084eb88d48777999691mcpowers *
23c57df72989c916b3e98084eb88d48777999691mcpowers * ECB_MODE, CBC_MODE, CTR_MODE, or CCM_MODE
23c57df72989c916b3e98084eb88d48777999691mcpowers */
23c57df72989c916b3e98084eb88d48777999691mcpowersstruct common_ctx {
23c57df72989c916b3e98084eb88d48777999691mcpowers void *cc_keysched;
23c57df72989c916b3e98084eb88d48777999691mcpowers size_t cc_keysched_len;
23c57df72989c916b3e98084eb88d48777999691mcpowers uint64_t cc_iv[2];
23c57df72989c916b3e98084eb88d48777999691mcpowers uint64_t cc_remainder[2];
23c57df72989c916b3e98084eb88d48777999691mcpowers size_t cc_remainder_len;
23c57df72989c916b3e98084eb88d48777999691mcpowers uint8_t *cc_lastp;
23c57df72989c916b3e98084eb88d48777999691mcpowers uint8_t *cc_copy_to;
23c57df72989c916b3e98084eb88d48777999691mcpowers uint32_t cc_flags;
23c57df72989c916b3e98084eb88d48777999691mcpowers};
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowerstypedef struct common_ctx common_ctx_t;
23c57df72989c916b3e98084eb88d48777999691mcpowers
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powerstypedef struct ecb_ctx {
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers struct common_ctx ecb_common;
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers uint64_t ecb_lastblock[2];
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers} ecb_ctx_t;
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define ecb_keysched ecb_common.cc_keysched
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define ecb_keysched_len ecb_common.cc_keysched_len
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define ecb_iv ecb_common.cc_iv
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define ecb_remainder ecb_common.cc_remainder
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define ecb_remainder_len ecb_common.cc_remainder_len
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define ecb_lastp ecb_common.cc_lastp
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define ecb_copy_to ecb_common.cc_copy_to
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define ecb_flags ecb_common.cc_flags
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powerstypedef struct cbc_ctx {
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers struct common_ctx cbc_common;
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers uint64_t cbc_lastblock[2];
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers} cbc_ctx_t;
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define cbc_keysched cbc_common.cc_keysched
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define cbc_keysched_len cbc_common.cc_keysched_len
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define cbc_iv cbc_common.cc_iv
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define cbc_remainder cbc_common.cc_remainder
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define cbc_remainder_len cbc_common.cc_remainder_len
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define cbc_lastp cbc_common.cc_lastp
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define cbc_copy_to cbc_common.cc_copy_to
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define cbc_flags cbc_common.cc_flags
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers/*
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers * ctr_lower_mask Bit-mask for lower 8 bytes of counter block.
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers * ctr_upper_mask Bit-mask for upper 8 bytes of counter block.
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers */
23c57df72989c916b3e98084eb88d48777999691mcpowerstypedef struct ctr_ctx {
23c57df72989c916b3e98084eb88d48777999691mcpowers struct common_ctx ctr_common;
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers uint64_t ctr_lower_mask;
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers uint64_t ctr_upper_mask;
23c57df72989c916b3e98084eb88d48777999691mcpowers uint32_t ctr_tmp[4];
23c57df72989c916b3e98084eb88d48777999691mcpowers} ctr_ctx_t;
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowers/*
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers * ctr_cb Counter block.
23c57df72989c916b3e98084eb88d48777999691mcpowers */
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ctr_keysched ctr_common.cc_keysched
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ctr_keysched_len ctr_common.cc_keysched_len
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ctr_cb ctr_common.cc_iv
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ctr_remainder ctr_common.cc_remainder
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ctr_remainder_len ctr_common.cc_remainder_len
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ctr_lastp ctr_common.cc_lastp
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ctr_copy_to ctr_common.cc_copy_to
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ctr_flags ctr_common.cc_flags
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowers/*
23c57df72989c916b3e98084eb88d48777999691mcpowers *
23c57df72989c916b3e98084eb88d48777999691mcpowers * ccm_mac_len: Stores length of the MAC in CCM mode.
23c57df72989c916b3e98084eb88d48777999691mcpowers * ccm_mac_buf: Stores the intermediate value for MAC in CCM encrypt.
23c57df72989c916b3e98084eb88d48777999691mcpowers * In CCM decrypt, stores the input MAC value.
23c57df72989c916b3e98084eb88d48777999691mcpowers * ccm_data_len: Length of the plaintext for CCM mode encrypt, or
23c57df72989c916b3e98084eb88d48777999691mcpowers * length of the ciphertext for CCM mode decrypt.
23c57df72989c916b3e98084eb88d48777999691mcpowers * ccm_processed_data_len:
23c57df72989c916b3e98084eb88d48777999691mcpowers * Length of processed plaintext in CCM mode encrypt,
23c57df72989c916b3e98084eb88d48777999691mcpowers * or length of processed ciphertext for CCM mode decrypt.
23c57df72989c916b3e98084eb88d48777999691mcpowers * ccm_processed_mac_len:
23c57df72989c916b3e98084eb88d48777999691mcpowers * Length of MAC data accumulated in CCM mode decrypt.
23c57df72989c916b3e98084eb88d48777999691mcpowers *
23c57df72989c916b3e98084eb88d48777999691mcpowers * ccm_pt_buf: Only used in CCM mode decrypt. It stores the
23c57df72989c916b3e98084eb88d48777999691mcpowers * decrypted plaintext to be returned when
23c57df72989c916b3e98084eb88d48777999691mcpowers * MAC verification succeeds in decrypt_final.
23c57df72989c916b3e98084eb88d48777999691mcpowers * Memory for this should be allocated in the AES module.
23c57df72989c916b3e98084eb88d48777999691mcpowers *
23c57df72989c916b3e98084eb88d48777999691mcpowers */
23c57df72989c916b3e98084eb88d48777999691mcpowerstypedef struct ccm_ctx {
23c57df72989c916b3e98084eb88d48777999691mcpowers struct common_ctx ccm_common;
23c57df72989c916b3e98084eb88d48777999691mcpowers uint32_t ccm_tmp[4];
23c57df72989c916b3e98084eb88d48777999691mcpowers size_t ccm_mac_len;
23c57df72989c916b3e98084eb88d48777999691mcpowers uint64_t ccm_mac_buf[2];
23c57df72989c916b3e98084eb88d48777999691mcpowers size_t ccm_data_len;
23c57df72989c916b3e98084eb88d48777999691mcpowers size_t ccm_processed_data_len;
23c57df72989c916b3e98084eb88d48777999691mcpowers size_t ccm_processed_mac_len;
23c57df72989c916b3e98084eb88d48777999691mcpowers uint8_t *ccm_pt_buf;
23c57df72989c916b3e98084eb88d48777999691mcpowers uint64_t ccm_mac_input_buf[2];
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers uint64_t ccm_counter_mask;
23c57df72989c916b3e98084eb88d48777999691mcpowers} ccm_ctx_t;
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ccm_keysched ccm_common.cc_keysched
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ccm_keysched_len ccm_common.cc_keysched_len
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ccm_cb ccm_common.cc_iv
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ccm_remainder ccm_common.cc_remainder
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ccm_remainder_len ccm_common.cc_remainder_len
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ccm_lastp ccm_common.cc_lastp
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ccm_copy_to ccm_common.cc_copy_to
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ccm_flags ccm_common.cc_flags
23c57df72989c916b3e98084eb88d48777999691mcpowers
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers/*
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers * gcm_tag_len: Length of authentication tag.
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers *
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers * gcm_ghash: Stores output from the GHASH function.
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers *
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers * gcm_processed_data_len:
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers * Length of processed plaintext (encrypt) or
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers * length of processed ciphertext (decrypt).
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers *
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers * gcm_pt_buf: Stores the decrypted plaintext returned by
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers * decrypt_final when the computed authentication
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers * tag matches the user supplied tag.
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers *
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers * gcm_pt_buf_len: Length of the plaintext buffer.
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers *
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers * gcm_H: Subkey.
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers *
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers * gcm_J0: Pre-counter block generated from the IV.
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers *
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers * gcm_len_a_len_c: 64-bit representations of the bit lengths of
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers * AAD and ciphertext.
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers *
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers * gcm_kmflag: Current value of kmflag. Used only for allocating
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers * the plaintext buffer during decryption.
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers */
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powerstypedef struct gcm_ctx {
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers struct common_ctx gcm_common;
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers size_t gcm_tag_len;
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers size_t gcm_processed_data_len;
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers size_t gcm_pt_buf_len;
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers uint32_t gcm_tmp[4];
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers uint64_t gcm_ghash[2];
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers uint64_t gcm_H[2];
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers uint64_t gcm_J0[2];
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers uint64_t gcm_len_a_len_c[2];
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers uint8_t *gcm_pt_buf;
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers int gcm_kmflag;
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers} gcm_ctx_t;
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers#define gcm_keysched gcm_common.cc_keysched
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers#define gcm_keysched_len gcm_common.cc_keysched_len
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers#define gcm_cb gcm_common.cc_iv
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers#define gcm_remainder gcm_common.cc_remainder
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers#define gcm_remainder_len gcm_common.cc_remainder_len
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers#define gcm_lastp gcm_common.cc_lastp
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers#define gcm_copy_to gcm_common.cc_copy_to
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers#define gcm_flags gcm_common.cc_flags
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers
983a10335731bc55a0b7a37f195575fa109e30d4Mark Powers#define AES_GMAC_IV_LEN 12
983a10335731bc55a0b7a37f195575fa109e30d4Mark Powers#define AES_GMAC_TAG_BITS 128
983a10335731bc55a0b7a37f195575fa109e30d4Mark Powers
23c57df72989c916b3e98084eb88d48777999691mcpowerstypedef struct aes_ctx {
23c57df72989c916b3e98084eb88d48777999691mcpowers union {
23c57df72989c916b3e98084eb88d48777999691mcpowers ecb_ctx_t acu_ecb;
23c57df72989c916b3e98084eb88d48777999691mcpowers cbc_ctx_t acu_cbc;
23c57df72989c916b3e98084eb88d48777999691mcpowers ctr_ctx_t acu_ctr;
23c57df72989c916b3e98084eb88d48777999691mcpowers#ifdef _KERNEL
23c57df72989c916b3e98084eb88d48777999691mcpowers ccm_ctx_t acu_ccm;
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers gcm_ctx_t acu_gcm;
23c57df72989c916b3e98084eb88d48777999691mcpowers#endif
23c57df72989c916b3e98084eb88d48777999691mcpowers } acu;
23c57df72989c916b3e98084eb88d48777999691mcpowers} aes_ctx_t;
23c57df72989c916b3e98084eb88d48777999691mcpowers
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define ac_flags acu.acu_ecb.ecb_common.cc_flags
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define ac_remainder_len acu.acu_ecb.ecb_common.cc_remainder_len
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define ac_keysched acu.acu_ecb.ecb_common.cc_keysched
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define ac_keysched_len acu.acu_ecb.ecb_common.cc_keysched_len
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define ac_iv acu.acu_ecb.ecb_common.cc_iv
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define ac_lastp acu.acu_ecb.ecb_common.cc_lastp
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ac_pt_buf acu.acu_ccm.ccm_pt_buf
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ac_mac_len acu.acu_ccm.ccm_mac_len
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ac_data_len acu.acu_ccm.ccm_data_len
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ac_processed_mac_len acu.acu_ccm.ccm_processed_mac_len
23c57df72989c916b3e98084eb88d48777999691mcpowers#define ac_processed_data_len acu.acu_ccm.ccm_processed_data_len
1dcbfafd968b0b71d294f13dbacb0e4b44b044caMark Powers#define ac_tag_len acu.acu_gcm.gcm_tag_len
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowerstypedef struct blowfish_ctx {
23c57df72989c916b3e98084eb88d48777999691mcpowers union {
23c57df72989c916b3e98084eb88d48777999691mcpowers ecb_ctx_t bcu_ecb;
23c57df72989c916b3e98084eb88d48777999691mcpowers cbc_ctx_t bcu_cbc;
23c57df72989c916b3e98084eb88d48777999691mcpowers } bcu;
23c57df72989c916b3e98084eb88d48777999691mcpowers} blowfish_ctx_t;
23c57df72989c916b3e98084eb88d48777999691mcpowers
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define bc_flags bcu.bcu_ecb.ecb_common.cc_flags
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define bc_remainder_len bcu.bcu_ecb.ecb_common.cc_remainder_len
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define bc_keysched bcu.bcu_ecb.ecb_common.cc_keysched
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define bc_keysched_len bcu.bcu_ecb.ecb_common.cc_keysched_len
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define bc_iv bcu.bcu_ecb.ecb_common.cc_iv
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define bc_lastp bcu.bcu_ecb.ecb_common.cc_lastp
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowerstypedef struct des_ctx {
23c57df72989c916b3e98084eb88d48777999691mcpowers union {
23c57df72989c916b3e98084eb88d48777999691mcpowers ecb_ctx_t dcu_ecb;
23c57df72989c916b3e98084eb88d48777999691mcpowers cbc_ctx_t dcu_cbc;
23c57df72989c916b3e98084eb88d48777999691mcpowers } dcu;
23c57df72989c916b3e98084eb88d48777999691mcpowers} des_ctx_t;
23c57df72989c916b3e98084eb88d48777999691mcpowers
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define dc_flags dcu.dcu_ecb.ecb_common.cc_flags
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define dc_remainder_len dcu.dcu_ecb.ecb_common.cc_remainder_len
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define dc_keysched dcu.dcu_ecb.ecb_common.cc_keysched
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define dc_keysched_len dcu.dcu_ecb.ecb_common.cc_keysched_len
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define dc_iv dcu.dcu_ecb.ecb_common.cc_iv
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powers#define dc_lastp dcu.dcu_ecb.ecb_common.cc_lastp
23c57df72989c916b3e98084eb88d48777999691mcpowers
16239bc82c111618343e0a5b1a70e0fc702d00e0Mark Powersextern int ecb_cipher_contiguous_blocks(ecb_ctx_t *, char *, size_t,
23c57df72989c916b3e98084eb88d48777999691mcpowers crypto_data_t *, size_t, int (*cipher)(const void *, const uint8_t *,
23c57df72989c916b3e98084eb88d48777999691mcpowers uint8_t *));
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowersextern int cbc_encrypt_contiguous_blocks(cbc_ctx_t *, char *, size_t,
23c57df72989c916b3e98084eb88d48777999691mcpowers crypto_data_t *, size_t,
23c57df72989c916b3e98084eb88d48777999691mcpowers int (*encrypt)(const void *, const uint8_t *, uint8_t *),
23c57df72989c916b3e98084eb88d48777999691mcpowers void (*copy_block)(uint8_t *, uint8_t *),
23c57df72989c916b3e98084eb88d48777999691mcpowers void (*xor_block)(uint8_t *, uint8_t *));
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowersextern int cbc_decrypt_contiguous_blocks(cbc_ctx_t *, char *, size_t,
23c57df72989c916b3e98084eb88d48777999691mcpowers crypto_data_t *, size_t,
23c57df72989c916b3e98084eb88d48777999691mcpowers int (*decrypt)(const void *, const uint8_t *, uint8_t *),
23c57df72989c916b3e98084eb88d48777999691mcpowers void (*copy_block)(uint8_t *, uint8_t *),
23c57df72989c916b3e98084eb88d48777999691mcpowers void (*xor_block)(uint8_t *, uint8_t *));
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowersextern int ctr_mode_contiguous_blocks(ctr_ctx_t *, char *, size_t,
23c57df72989c916b3e98084eb88d48777999691mcpowers crypto_data_t *, size_t,
23c57df72989c916b3e98084eb88d48777999691mcpowers int (*cipher)(const void *, const uint8_t *, uint8_t *),
23c57df72989c916b3e98084eb88d48777999691mcpowers void (*xor_block)(uint8_t *, uint8_t *));
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowersextern int ccm_mode_encrypt_contiguous_blocks(ccm_ctx_t *, char *, size_t,
23c57df72989c916b3e98084eb88d48777999691mcpowers crypto_data_t *, size_t,
23c57df72989c916b3e98084eb88d48777999691mcpowers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
23c57df72989c916b3e98084eb88d48777999691mcpowers void (*copy_block)(uint8_t *, uint8_t *),
23c57df72989c916b3e98084eb88d48777999691mcpowers void (*xor_block)(uint8_t *, uint8_t *));
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowersextern int ccm_mode_decrypt_contiguous_blocks(ccm_ctx_t *, char *, size_t,
23c57df72989c916b3e98084eb88d48777999691mcpowers crypto_data_t *, size_t,
23c57df72989c916b3e98084eb88d48777999691mcpowers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
23c57df72989c916b3e98084eb88d48777999691mcpowers void (*copy_block)(uint8_t *, uint8_t *),
23c57df72989c916b3e98084eb88d48777999691mcpowers void (*xor_block)(uint8_t *, uint8_t *));
23c57df72989c916b3e98084eb88d48777999691mcpowers
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powersextern int gcm_mode_encrypt_contiguous_blocks(gcm_ctx_t *, char *, size_t,
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers crypto_data_t *, size_t,
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers void (*copy_block)(uint8_t *, uint8_t *),
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers void (*xor_block)(uint8_t *, uint8_t *));
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powersextern int gcm_mode_decrypt_contiguous_blocks(gcm_ctx_t *, char *, size_t,
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers crypto_data_t *, size_t,
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers void (*copy_block)(uint8_t *, uint8_t *),
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers void (*xor_block)(uint8_t *, uint8_t *));
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers
23c57df72989c916b3e98084eb88d48777999691mcpowersint ccm_encrypt_final(ccm_ctx_t *, crypto_data_t *, size_t,
23c57df72989c916b3e98084eb88d48777999691mcpowers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
23c57df72989c916b3e98084eb88d48777999691mcpowers void (*xor_block)(uint8_t *, uint8_t *));
23c57df72989c916b3e98084eb88d48777999691mcpowers
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powersint gcm_encrypt_final(gcm_ctx_t *, crypto_data_t *, size_t,
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers void (*copy_block)(uint8_t *, uint8_t *),
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers void (*xor_block)(uint8_t *, uint8_t *));
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers
23c57df72989c916b3e98084eb88d48777999691mcpowersextern int ccm_decrypt_final(ccm_ctx_t *, crypto_data_t *, size_t,
23c57df72989c916b3e98084eb88d48777999691mcpowers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
23c57df72989c916b3e98084eb88d48777999691mcpowers void (*copy_block)(uint8_t *, uint8_t *),
23c57df72989c916b3e98084eb88d48777999691mcpowers void (*xor_block)(uint8_t *, uint8_t *));
23c57df72989c916b3e98084eb88d48777999691mcpowers
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powersextern int gcm_decrypt_final(gcm_ctx_t *, crypto_data_t *, size_t,
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers void (*xor_block)(uint8_t *, uint8_t *));
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers
23c57df72989c916b3e98084eb88d48777999691mcpowersextern int ctr_mode_final(ctr_ctx_t *, crypto_data_t *,
23c57df72989c916b3e98084eb88d48777999691mcpowers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *));
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowersextern int cbc_init_ctx(cbc_ctx_t *, char *, size_t, size_t,
23c57df72989c916b3e98084eb88d48777999691mcpowers void (*copy_block)(uint8_t *, uint64_t *));
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowersextern int ctr_init_ctx(ctr_ctx_t *, ulong_t, uint8_t *,
23c57df72989c916b3e98084eb88d48777999691mcpowers void (*copy_block)(uint8_t *, uint8_t *));
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowersextern int ccm_init_ctx(ccm_ctx_t *, char *, int, boolean_t, size_t,
23c57df72989c916b3e98084eb88d48777999691mcpowers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
23c57df72989c916b3e98084eb88d48777999691mcpowers void (*xor_block)(uint8_t *, uint8_t *));
23c57df72989c916b3e98084eb88d48777999691mcpowers
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powersextern int gcm_init_ctx(gcm_ctx_t *, char *, size_t,
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers void (*copy_block)(uint8_t *, uint8_t *),
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers void (*xor_block)(uint8_t *, uint8_t *));
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powers
983a10335731bc55a0b7a37f195575fa109e30d4Mark Powersextern int gmac_init_ctx(gcm_ctx_t *, char *, size_t,
983a10335731bc55a0b7a37f195575fa109e30d4Mark Powers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
983a10335731bc55a0b7a37f195575fa109e30d4Mark Powers void (*copy_block)(uint8_t *, uint8_t *),
983a10335731bc55a0b7a37f195575fa109e30d4Mark Powers void (*xor_block)(uint8_t *, uint8_t *));
983a10335731bc55a0b7a37f195575fa109e30d4Mark Powers
23c57df72989c916b3e98084eb88d48777999691mcpowersextern void calculate_ccm_mac(ccm_ctx_t *, uint8_t *,
23c57df72989c916b3e98084eb88d48777999691mcpowers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *));
23c57df72989c916b3e98084eb88d48777999691mcpowers
e8c016ef6bb7e825057460cea4d904e2b90eb424Mark Powersextern void gcm_mul(uint64_t *, uint64_t *, uint64_t *);
e8c016ef6bb7e825057460cea4d904e2b90eb424Mark Powers
23c57df72989c916b3e98084eb88d48777999691mcpowersextern void crypto_init_ptrs(crypto_data_t *, void **, offset_t *);
23c57df72989c916b3e98084eb88d48777999691mcpowersextern void crypto_get_ptrs(crypto_data_t *, void **, offset_t *,
23c57df72989c916b3e98084eb88d48777999691mcpowers uint8_t **, size_t *, uint8_t **, size_t);
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowersextern void *ecb_alloc_ctx(int);
23c57df72989c916b3e98084eb88d48777999691mcpowersextern void *cbc_alloc_ctx(int);
23c57df72989c916b3e98084eb88d48777999691mcpowersextern void *ctr_alloc_ctx(int);
23c57df72989c916b3e98084eb88d48777999691mcpowersextern void *ccm_alloc_ctx(int);
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powersextern void *gcm_alloc_ctx(int);
983a10335731bc55a0b7a37f195575fa109e30d4Mark Powersextern void *gmac_alloc_ctx(int);
23c57df72989c916b3e98084eb88d48777999691mcpowersextern void crypto_free_mode_ctx(void *);
4d703b5c9cb1bc29ace6c53cb50b2fe766e6370fMark Powersextern void gcm_set_kmflag(gcm_ctx_t *, int);
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowers#ifdef __cplusplus
23c57df72989c916b3e98084eb88d48777999691mcpowers}
23c57df72989c916b3e98084eb88d48777999691mcpowers#endif
23c57df72989c916b3e98084eb88d48777999691mcpowers
23c57df72989c916b3e98084eb88d48777999691mcpowers#endif /* _COMMON_CRYPTO_MODES_H */