rjw | 6c1fd8f | 2022-11-30 14:33:01 +0800 | [diff] [blame] | 1 | /* Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi> and contributors |
| 2 | * All Rights Reserved. |
| 3 | * |
| 4 | * This program is dual-licensed under both the GPL version 2 and BSD |
| 5 | * license. Either license may be used at your option. |
| 6 | * |
| 7 | * |
| 8 | * |
| 9 | * License |
| 10 | * ------- |
| 11 | * |
| 12 | * GPL v2: |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License version 2 as |
| 16 | * published by the Free Software Foundation. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 26 | * |
| 27 | * (this copy of the license is in COPYING file) |
| 28 | * |
| 29 | * |
| 30 | * Alternatively, this software may be distributed, used, and modified |
| 31 | * under the terms of BSD license: |
| 32 | * |
| 33 | * Redistribution and use in source and binary forms, with or without |
| 34 | * modification, are permitted provided that the following conditions are |
| 35 | * met: |
| 36 | * |
| 37 | * 1. Redistributions of source code must retain the above copyright |
| 38 | * notice, this list of conditions and the following disclaimer. |
| 39 | * |
| 40 | * 2. Redistributions in binary form must reproduce the above copyright |
| 41 | * notice, this list of conditions and the following disclaimer in the |
| 42 | * documentation and/or other materials provided with the distribution. |
| 43 | * |
| 44 | * 3. Neither the name(s) of the above-listed copyright holder(s) nor the |
| 45 | * names of its contributors may be used to endorse or promote products |
| 46 | * derived from this software without specific prior written permission. |
| 47 | * |
| 48 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 49 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 50 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 51 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 52 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 53 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 54 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 55 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 56 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 57 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 58 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 59 | */ |
| 60 | |
| 61 | /***************************************************************************** |
| 62 | * |
| 63 | * Filename: |
| 64 | * --------- |
| 65 | * aes.h |
| 66 | * |
| 67 | * Project: |
| 68 | * -------- |
| 69 | * Maui_Software |
| 70 | * |
| 71 | * Description: |
| 72 | * ------------ |
| 73 | * aes software function. |
| 74 | * |
| 75 | * Author: |
| 76 | * ------- |
| 77 | * ------- |
| 78 | * |
| 79 | *============================================================================ |
| 80 | * HISTORY |
| 81 | * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!! |
| 82 | *------------------------------------------------------------------------------ |
| 83 | * removed! |
| 84 | * |
| 85 | * removed! |
| 86 | * removed! |
| 87 | * removed! |
| 88 | * |
| 89 | * removed! |
| 90 | * removed! |
| 91 | * removed! |
| 92 | * |
| 93 | * removed! |
| 94 | * removed! |
| 95 | * removed! |
| 96 | * |
| 97 | * removed! |
| 98 | * removed! |
| 99 | * removed! |
| 100 | * |
| 101 | * removed! |
| 102 | * removed! |
| 103 | * removed! |
| 104 | * |
| 105 | * removed! |
| 106 | * removed! |
| 107 | * removed! |
| 108 | * |
| 109 | * removed! |
| 110 | * removed! |
| 111 | * removed! |
| 112 | * |
| 113 | * removed! |
| 114 | * removed! |
| 115 | * removed! |
| 116 | * |
| 117 | * removed! |
| 118 | * removed! |
| 119 | * removed! |
| 120 | * |
| 121 | * removed! |
| 122 | * removed! |
| 123 | * removed! |
| 124 | * |
| 125 | * removed! |
| 126 | * removed! |
| 127 | * removed! |
| 128 | * |
| 129 | *------------------------------------------------------------------------------ |
| 130 | * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!! |
| 131 | *============================================================================ |
| 132 | ****************************************************************************/ |
| 133 | |
| 134 | #ifndef __AES_H__ |
| 135 | #define __AES_H__ |
| 136 | |
| 137 | #include "kal_general_types.h" |
| 138 | |
| 139 | #if defined(_MSC_VER) |
| 140 | #define SWAP(x) (_lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00) |
| 141 | #define GETU32(p) SWAP(*((u32 *)(p))) |
| 142 | #define PUTU32(ct, st) { *((u32 *)(ct)) = SWAP((st)); } |
| 143 | #else |
| 144 | #define GETU32(pt) (((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ ((u32)(pt)[2] << 8) ^ ((u32)(pt)[3])) |
| 145 | #define PUTU32(ct, st) { (ct)[0] = (u8)((st) >> 24); (ct)[1] = (u8)((st) >> 16); (ct)[2] = (u8)((st) >> 8); (ct)[3] = (u8)(st); } |
| 146 | #endif |
| 147 | |
| 148 | typedef unsigned long u32; |
| 149 | typedef unsigned short u16; |
| 150 | typedef unsigned char u8; |
| 151 | |
| 152 | #define MAXKC (256/32) |
| 153 | #define MAXKB (256/8) |
| 154 | #define MAXNR 14 |
| 155 | |
| 156 | |
| 157 | #undef FULL_UNROLL |
| 158 | |
| 159 | |
| 160 | |
| 161 | #define AES_ENCRYPT 1 |
| 162 | #define AES_DECRYPT 0 |
| 163 | |
| 164 | |
| 165 | |
| 166 | #define AES_MAXNR 14 |
| 167 | #define AES_BLOCK_SIZE 16 |
| 168 | |
| 169 | struct aes_key_st { |
| 170 | unsigned long rd_key[4 *(AES_MAXNR + 1)]; |
| 171 | int rounds; |
| 172 | }; |
| 173 | typedef struct aes_key_st AES_KEY; |
| 174 | |
| 175 | kal_int32 CHE_AES_block_encrypt(kal_uint8 *ivec, AES_KEY *cipher, kal_uint8 *input, kal_uint32 uInputLen, kal_uint8 *outBuffer, kal_bool first_flag, kal_uint8 *orgIV); |
| 176 | |
| 177 | kal_int32 CHE_AES_block_decrypt(kal_uint8 *ivec, AES_KEY *cipher, kal_uint8 *input, kal_uint32 uInputLen, kal_uint8 *outBuffer, kal_bool first_flag, kal_uint8 *orgIV); |
| 178 | |
| 179 | |
| 180 | void CHE_AES_unit_encrypt(const kal_uint32 rk[], kal_int32 Nr, const kal_uint8 pt[16], kal_uint8 ct[16]); |
| 181 | |
| 182 | void CHE_AES_unit_decrypt(const kal_uint32 rk[], int Nr, const kal_uint8 ct[16], kal_uint8 pt[16]); |
| 183 | |
| 184 | |
| 185 | void CHE_AES_unit_encrypt(const kal_uint32 rk[], kal_int32 Nr, const kal_uint8 pt[16], kal_uint8 ct[16]); |
| 186 | |
| 187 | void CHE_AES_unit_decrypt(const kal_uint32 rk[], int Nr, const kal_uint8 ct[16], kal_uint8 pt[16]); |
| 188 | |
| 189 | |
| 190 | kal_int32 CHE_AES_ctr_encrypt( unsigned char *nonce, AES_KEY *key, unsigned char *data, unsigned long data_len, unsigned char *output, kal_bool first_flag, kal_uint8 *orgIV, void *aes_param); |
| 191 | |
| 192 | kal_int32 CHE_AES_ctr_decrypt( unsigned char *nonce, AES_KEY *key, unsigned char *data, unsigned long data_len, unsigned char *output, kal_bool first_flag, kal_uint8 *orgIV, void *aes_param); |
| 193 | |
| 194 | kal_int32 CHE_AES_f8_encrypt( unsigned char *IV, AES_KEY *key, unsigned char *data, unsigned long data_len, unsigned char *output, kal_bool first_flag); |
| 195 | |
| 196 | kal_int32 CHE_AES_f8_decrypt( unsigned char *IV, AES_KEY *key, unsigned char *data, unsigned long data_len, unsigned char *output, kal_bool first_flag); |
| 197 | |
| 198 | kal_int32 CHE_AES_setup_enc_key(kal_uint32 rk[], const kal_uint8 *cipherKey, kal_int32 keyBits); |
| 199 | //kal_int32 CHE_AES_ctr_decrypt( unsigned char *nonce, AES_KEY *key, unsigned char *data, unsigned long data_len, unsigned char *output, kal_bool first_flag, kal_uint8 *orgIV); |
| 200 | kal_int32 CHE_AES_setup_dec_key(kal_uint32 rk[], const kal_uint8 cipherKey[], kal_int32 keyBits); |
| 201 | int CHE_AES_WRAP(AES_KEY *key, kal_int32 n, const kal_uint8 *plain, kal_uint8 *cipher); |
| 202 | int CHE_AES_UNWRAP(AES_KEY *key, kal_int32 n, const kal_uint8 *cipher, kal_uint8 *plain); |
| 203 | #endif /* __AES_H__ */ |