1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 #ifndef _AES_CBC_CRYPT_H
  27 #define _AES_CBC_CRYPT_H
  28 
  29 #pragma ident   "@(#)aes_cbc_crypt.h    1.7     08/05/21 SMI"
  30 
  31 #ifdef  __cplusplus
  32 extern "C" {
  33 #endif
  34 
  35 #include <sys/crypto/common.h>
  36 #include "aes_impl.h"
  37 
  38 /*
  39  * ac_keysched:         Pointer to key schedule.
  40  *
  41  * ac_keysched_len:     Length of the key schedule.
  42  *
  43  * ac_remainder:        This is for residual data, i.e. data that can't
  44  *                      be processed because there are too few bytes.
  45  *                      Must wait until more data arrives.
  46  *
  47  * ac_remainder_len:    Number of bytes in ac_remainder.
  48  *
  49  * ac_iv:               Scratch buffer that sometimes contains the IV.
  50  *
  51  * ac_lastblock:        Scratch buffer.
  52  *
  53  * ac_lastp:            Pointer to previous block of ciphertext.
  54  *
  55  * ac_copy_to:          Pointer to where encrypted residual data needs
  56  *                      to be copied.
  57  *
  58  * ac_flags:            AES_PROVIDER_OWNS_KEY_SCHEDULE
  59  *                      When a context is freed, it is necessary
  60  *                      to know whether the key schedule was allocated
  61  *                      by the caller, or by aes_encrypt_init() or
  62  *                      aes_decrypt_init().  If allocated by the latter,
  63  *                      then it needs to be freed.
  64  *
  65  *                      AES_ECB_MODE, AES_CBC_MODE, or AES_CTR_MODE
  66  *                      AES_CCM_MODE
  67  *
  68  * ac_ccm_mac_len:      Stores length of the MAC in CCM mode.
  69  * ac_ccm_mac_buf:      Stores the intermediate value for MAC in CCM encrypt.
  70  *                      In CCM decrypt, stores the input MAC value.
  71  * ac_ccm_data_len:     Length of the plaintext for CCM mode encrypt, or
  72  *                      length of the ciphertext for CCM mode decrypt.
  73  * ac_ccm_processed_data_len:
  74  *                      Length of processed plaintext in CCM mode encrypt,
  75  *                      or length of processed ciphertext for CCM mode decrypt.
  76  * ac_ccm_processed_mac_len:
  77  *                      Length of MAC data accumulated in CCM mode decrypt.
  78  *
  79  * ac_ccm_pt_buf:       Only used in CCM mode decrypt.  It stores the
  80  *                      decrypted plaintext to be returned when
  81  *                      MAC verification succeeds in decrypt_final.
  82  *                      Memory for this should be allocated in the AES module.
  83  *
  84  */
  85 typedef struct aes_ctx {
  86         void *ac_keysched;
  87         size_t ac_keysched_len;
  88         uint64_t ac_iv[2];
  89         uint64_t ac_lastblock[2];
  90         uint64_t ac_remainder[2];
  91         size_t ac_remainder_len;
  92         uint8_t *ac_lastp;
  93         uint8_t *ac_copy_to;
  94         uint32_t ac_flags;
  95         size_t ac_ccm_mac_len;
  96         uint64_t ac_ccm_mac_buf[2];
  97         size_t ac_ccm_data_len;
  98         size_t ac_ccm_processed_data_len;
  99         size_t ac_ccm_processed_mac_len;
 100         uint8_t *ac_ccm_pt_buf;
 101         uint64_t ac_ccm_mac_input_buf[2];
 102 } aes_ctx_t;
 103 
 104 /*
 105  * ac_cb                Counter block.
 106  *
 107  * ac_counter_mask      Mask of counter bits in the last 8 bytes of the
 108  *                      counter block.
 109  */
 110 #define ac_cb           ac_iv
 111 #define ac_counter_mask ac_lastblock[0]
 112 
 113 #define AES_PROVIDER_OWNS_KEY_SCHEDULE  0x00000001
 114 #define AES_ECB_MODE                    0x00000002
 115 #define AES_CBC_MODE                    0x00000004
 116 #define AES_CTR_MODE                    0x00000008
 117 #define AES_CCM_MODE                    0x00000010
 118 
 119 extern int aes_encrypt_contiguous_blocks(aes_ctx_t *, char *, size_t,
 120     crypto_data_t *);
 121 extern int aes_decrypt_contiguous_blocks(aes_ctx_t *, char *, size_t,
 122     crypto_data_t *);
 123 extern int aes_counter_final(aes_ctx_t *, crypto_data_t *);
 124 extern int aes_ccm_init(aes_ctx_t *, unsigned char *, size_t,
 125     unsigned char *, size_t);
 126 extern int aes_ccm_validate_args(CK_AES_CCM_PARAMS *, boolean_t);
 127 extern int aes_ccm_encrypt_final(aes_ctx_t *, crypto_data_t *);
 128 extern int aes_ccm_decrypt_final(aes_ctx_t *, crypto_data_t *);
 129 
 130 #ifdef  __cplusplus
 131 }
 132 #endif
 133 
 134 #endif  /* _AES_CBC_CRYPT_H */