1 /******************************************************************************* 2 * Copyright 2002-2018 Intel Corporation 3 * All Rights Reserved. 4 * 5 * If this software was obtained under the Intel Simplified Software License, 6 * the following terms apply: 7 * 8 * The source code, information and material ("Material") contained herein is 9 * owned by Intel Corporation or its suppliers or licensors, and title to such 10 * Material remains with Intel Corporation or its suppliers or licensors. The 11 * Material contains proprietary information of Intel or its suppliers and 12 * licensors. The Material is protected by worldwide copyright laws and treaty 13 * provisions. No part of the Material may be used, copied, reproduced, 14 * modified, published, uploaded, posted, transmitted, distributed or disclosed 15 * in any way without Intel's prior express written permission. No license under 16 * any patent, copyright or other intellectual property rights in the Material 17 * is granted to or conferred upon you, either expressly, by implication, 18 * inducement, estoppel or otherwise. Any license under such intellectual 19 * property rights must be express and approved by Intel in writing. 20 * 21 * Unless otherwise agreed by Intel in writing, you may not remove or alter this 22 * notice or any other notice embedded in Materials by Intel or Intel's 23 * suppliers or licensors in any way. 24 * 25 * 26 * If this software was obtained under the Apache License, Version 2.0 (the 27 * "License"), the following terms apply: 28 * 29 * You may not use this file except in compliance with the License. You may 30 * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 31 * 32 * 33 * Unless required by applicable law or agreed to in writing, software 34 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 35 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 36 * 37 * See the License for the specific language governing permissions and 38 * limitations under the License. 39 *******************************************************************************/ 40 41 /* 42 // 43 // Purpose: 44 // Cryptography Primitive. 45 // Internal Definitions and 46 // Internal Rijndael based Encrypt/Decrypt Function Prototypes 47 // 48 // 49 */ 50 51 #if !defined(_PCP_RIJ_H) 52 #define _PCP_RIJ_H 53 54 55 /* 56 // The GF(256) modular polynomial and elements 57 */ 58 #define WPOLY 0x011B 59 #define BPOLY 0x1B 60 61 /* 62 // Make WORD using 4 arbitrary bytes 63 */ 64 #define BYTES_TO_WORD(b0,b1,b2,b3) ( ( ((Ipp32u)((Ipp8u)(b3))) <<24 ) \ 65 |( ((Ipp32u)((Ipp8u)(b2))) <<16 ) \ 66 |( ((Ipp32u)((Ipp8u)(b1))) << 8 ) \ 67 |( ((Ipp32u)((Ipp8u)(b0))) ) ) 68 /* 69 // Make WORD setting byte in specified position 70 */ 71 #define BYTE0_TO_WORD(b) BYTES_TO_WORD((b), 0, 0, 0) 72 #define BYTE1_TO_WORD(b) BYTES_TO_WORD( 0, (b), 0, 0) 73 #define BYTE2_TO_WORD(b) BYTES_TO_WORD( 0, 0, (b), 0) 74 #define BYTE3_TO_WORD(b) BYTES_TO_WORD( 0, 0, 0, (b)) 75 76 /* 77 // Extract byte from specified position n. 78 // Sure, n=0,1,2 or 3 only 79 */ 80 #define EBYTE(w,n) ((Ipp8u)((w) >> (8 * (n)))) 81 82 83 /* 84 // Rijndael's spec 85 // 86 // Rijndael128, Rijndael192 and Rijndael256 87 // reserve space for maximum number of expanded keys 88 */ 89 typedef void (*RijnCipher)(const Ipp8u* pInpBlk, Ipp8u* pOutBlk, int nr, const Ipp8u* pKeys, const void* pTbl); 90 91 struct _cpRijndael128 { 92 IppCtxId idCtx; /* Rijndael spec identifier */ 93 int nk; /* security key length (words) */ 94 int nb; /* data block size (words) */ 95 int nr; /* number of rounds */ 96 RijnCipher encoder; /* encoder/decoder */ 97 RijnCipher decoder; /* entry point */ 98 Ipp32u* pEncTbl; /* expanded S-boxes for */ 99 Ipp32u* pDecTbl; /* encryption and decryption */ 100 Ipp32u enc_keys[64]; /* array of keys for encryprion */ 101 Ipp32u dec_keys[64]; /* array of keys for decryprion */ 102 Ipp32u aesNI; /* AES instruction available */ 103 Ipp32u safeInit; /* SafeInit performed */ 104 }; 105 106 struct _cpRijndael192 { 107 IppCtxId idCtx; /* Rijndael spec identifier */ 108 int nk; /* security key length (words) */ 109 int nb; /* data block size (words) */ 110 int nr; /* number of rounds */ 111 RijnCipher encoder; /* encoder/decoder */ 112 RijnCipher decoder; /* entry point */ 113 Ipp32u* pEncTbl; /* expanded S-boxes for */ 114 Ipp32u* pDecTbl; /* encryption and decryption */ 115 Ipp32u enc_keys[96]; /* array of keys for encryprion */ 116 Ipp32u dec_keys[96]; /* array of keys for decryprion */ 117 Ipp32u aesNI; /* AES instruction available */ 118 Ipp32u safeInit; /* SafeInit performed */ 119 }; 120 121 struct _cpRijndael256 { 122 IppCtxId idCtx; /* Rijndael spec identifier */ 123 int nk; /* security key length (words) */ 124 int nb; /* data block size (words) */ 125 int nr; /* number of rounds */ 126 RijnCipher encoder; /* encoder/decoder */ 127 RijnCipher decoder; /* entry point */ 128 Ipp32u* pEncTbl; /* expanded S-boxes for */ 129 Ipp32u* pDecTbl; /* encryption and decryption */ 130 Ipp32u enc_keys[120]; /* array of keys for encryprion */ 131 Ipp32u dec_keys[120]; /* array of keys for decryprion */ 132 Ipp32u aesNI; /* AES instruction available */ 133 Ipp32u safeInit; /* SafeInit performed */ 134 }; 135 136 137 /* alignment */ 138 #define RIJ_ALIGNMENT (16) 139 140 #define MBS_RIJ128 (128/8) /* message block size (bytes) */ 141 #define MBS_RIJ192 (192/8) 142 #define MBS_RIJ256 (256/8) 143 144 #define SR (4) /* number of rows in STATE data */ 145 146 #define NB(msgBlks) ((msgBlks)/32) /* message block size (words) */ 147 /* 4-word for 128-bits data block */ 148 /* 6-word for 192-bits data block */ 149 /* 8-word for 256-bits data block */ 150 151 #define NK(keybits) ((keybits)/32) /* key length (words): */ 152 #define NK128 NK(ippRijndaelKey128)/* 4-word for 128-bits security key */ 153 #define NK192 NK(ippRijndaelKey192)/* 6-word for 192-bits security key */ 154 #define NK256 NK(ippRijndaelKey256)/* 8-word for 256-bits security key */ 155 156 #define NR128_128 (10) /* number of rounds data: 128 bits key: 128 bits are used */ 157 #define NR128_192 (12) /* number of rounds data: 128 bits key: 192 bits are used */ 158 #define NR128_256 (14) /* number of rounds data: 128 bits key: 256 bits are used */ 159 #define NR192_128 (12) /* number of rounds data: 192 bits key: 128 bits are used */ 160 #define NR192_192 (12) /* number of rounds data: 192 bits key: 192 bits are used */ 161 #define NR192_256 (14) /* number of rounds data: 192 bits key: 256 bits are used */ 162 #define NR256_128 (14) /* number of rounds data: 256 bits key: 128 bits are used */ 163 #define NR256_192 (14) /* number of rounds data: 256 bits key: 192 bits are used */ 164 #define NR256_256 (14) /* number of rounds data: 256 bits key: 256 bits are used */ 165 166 /* 167 // Useful macros 168 */ 169 #define RIJ_ID(ctx) ((ctx)->idCtx) 170 #define RIJ_NB(ctx) ((ctx)->nb) 171 #define RIJ_NK(ctx) ((ctx)->nk) 172 #define RIJ_NR(ctx) ((ctx)->nr) 173 #define RIJ_ENCODER(ctx) ((ctx)->encoder) 174 #define RIJ_DECODER(ctx) ((ctx)->decoder) 175 #define RIJ_ENC_SBOX(ctx) ((ctx)->pEncTbl) 176 #define RIJ_DEC_SBOX(ctx) ((ctx)->pDecTbl) 177 #define RIJ_EKEYS(ctx) (Ipp8u*)((ctx)->enc_keys) 178 #define RIJ_DKEYS(ctx) (Ipp8u*)((ctx)->dec_keys) 179 #define RIJ_AESNI(ctx) ((ctx)->aesNI) 180 #define RIJ_SAFE_INIT(ctx) ((ctx)->safeInit) 181 182 #define RIJ_ID_TEST(ctx) (RIJ_ID((ctx))==idCtxRijndael) 183 184 /* 185 // Internal functions 186 */ 187 #if (_ALG_AES_SAFE_==_ALG_AES_SAFE_COMPOSITE_GF_) 188 #define SafeEncrypt_RIJ128 OWNAPI(SafeEncrypt_RIJ128) 189 void SafeEncrypt_RIJ128(const Ipp8u* pInpBlk, Ipp8u* pOutBlk, int nr, const Ipp8u* pKeys, const void* pTbl); 190 #define SafeDecrypt_RIJ128 OWNAPI(SafeDecrypt_RIJ128) 191 void SafeDecrypt_RIJ128(const Ipp8u* pInpBlk, Ipp8u* pOutBlk, int nr, const Ipp8u* pKeys, const void* pTbl); 192 #endif 193 194 #if (_ALG_AES_SAFE_==_ALG_AES_SAFE_COMPACT_SBOX_) 195 #define Safe2Encrypt_RIJ128 OWNAPI(Safe2Encrypt_RIJ128) 196 void Safe2Encrypt_RIJ128(const Ipp8u* pInpBlk, Ipp8u* pOutBlk, int nr, const Ipp8u* pKeys, const void* pTbl); 197 #define Safe2Decrypt_RIJ128 OWNAPI(Safe2Decrypt_RIJ128) 198 void Safe2Decrypt_RIJ128(const Ipp8u* pInpBlk, Ipp8u* pOutBlk, int nr, const Ipp8u* pKeys, const void* pTbl); 199 #endif 200 201 #if (_IPP>=_IPP_P8) || (_IPP32E>=_IPP32E_Y8) 202 #define Encrypt_RIJ128_AES_NI OWNAPI(Encrypt_RIJ128_AES_NI) 203 void Encrypt_RIJ128_AES_NI(const Ipp8u* pInpBlk, Ipp8u* pOutBlk, int nr, const Ipp8u* pKeys, const void* pTbl); 204 #define EncryptECB_RIJ128pipe_AES_NI OWNAPI(EncryptECB_RIJ128pipe_AES_NI) 205 void EncryptECB_RIJ128pipe_AES_NI(const Ipp8u* pSrc, Ipp8u* pDst, int nr, const Ipp8u* pKeys, int len); 206 #define EncryptCBC_RIJ128_AES_NI OWNAPI(EncryptCBC_RIJ128_AES_NI) 207 void EncryptCBC_RIJ128_AES_NI(const Ipp8u* pSrc, Ipp8u* pDst, int nr, const Ipp8u* pKeys, int len, const Ipp8u* pIV); 208 #define EncryptCTR_RIJ128pipe_AES_NI OWNAPI(EncryptCTR_RIJ128pipe_AES_NI) 209 void EncryptCTR_RIJ128pipe_AES_NI(const Ipp8u* pSrc, Ipp8u* pDst, int nr, const Ipp8u* pKeys, int len, Ipp8u* pCtrValue, const Ipp8u* pCtrBitMask); 210 #define EncryptStreamCTR32_AES_NI OWNAPI(EncryptStreamCTR32_AES_NI) 211 void EncryptStreamCTR32_AES_NI(const Ipp8u* pSrc, Ipp8u* pDst, int nr, const Ipp8u* pKeys, int len, Ipp8u* pCtrValue); 212 213 #define EncryptCFB_RIJ128_AES_NI OWNAPI(EncryptCFB_RIJ128_AES_NI) 214 void EncryptCFB_RIJ128_AES_NI(const Ipp8u* pSrc, Ipp8u* pDst, int nr, const Ipp8u* pKeys, int len, int cfbBlkSize, const Ipp8u* pIV); 215 #define EncryptCFB32_RIJ128_AES_NI OWNAPI(EncryptCFB32_RIJ128_AES_NI) 216 void EncryptCFB32_RIJ128_AES_NI(const Ipp8u* pSrc, Ipp8u* pDst, int nr, const Ipp8u* pKeys, int len, int cfbBlkSize, const Ipp8u* pIV); 217 #define EncryptCFB128_RIJ128_AES_NI OWNAPI(EncryptCFB128_RIJ128_AES_NI) 218 void EncryptCFB128_RIJ128_AES_NI(const Ipp8u* pSrc, Ipp8u* pDst, int nr, const Ipp8u* pKeys, int len, const Ipp8u* pIV); 219 #define EncryptOFB_RIJ128_AES_NI OWNAPI(EncryptOFB_RIJ128_AES_NI) 220 void EncryptOFB_RIJ128_AES_NI(const Ipp8u* pSrc, Ipp8u* pDst, int nr, const Ipp8u* pKeys, int len, int ofbBlkSize, Ipp8u* pIV); 221 #define EncryptOFB128_RIJ128_AES_NI OWNAPI(EncryptOFB128_RIJ128_AES_NI) 222 void EncryptOFB128_RIJ128_AES_NI(const Ipp8u* pSrc, Ipp8u* pDst, int nr, const Ipp8u* pKeys, int len, Ipp8u* pIV); 223 224 #define Decrypt_RIJ128_AES_NI OWNAPI(Decrypt_RIJ128_AES_NI) 225 void Decrypt_RIJ128_AES_NI(const Ipp8u* pInpBlk, Ipp8u* pOutBlk, int nr, const Ipp8u* pKeys, const void* pTbl); 226 #define DecryptECB_RIJ128pipe_AES_NI OWNAPI(DecryptECB_RIJ128pipe_AES_NI) 227 void DecryptECB_RIJ128pipe_AES_NI(const Ipp8u* pSrc, Ipp8u* pDst, int nr, const Ipp8u* pKeys, int len); 228 #define DecryptCBC_RIJ128pipe_AES_NI OWNAPI(DecryptCBC_RIJ128pipe_AES_NI) 229 void DecryptCBC_RIJ128pipe_AES_NI(const Ipp8u* pSrc, Ipp8u* pDst, int nr, const Ipp8u* pKeys, int len, const Ipp8u* pIV); 230 #define DecryptCFB_RIJ128pipe_AES_NI OWNAPI(DecryptCFB_RIJ128pipe_AES_NI) 231 void DecryptCFB_RIJ128pipe_AES_NI(const Ipp8u* pSrc, Ipp8u* pDst, int nr, const Ipp8u* pKeys, int cfbBlocks, int cfbBlkSize, const Ipp8u* pIV); 232 #define DecryptCFB32_RIJ128pipe_AES_NI OWNAPI(DecryptCFB32_RIJ128pipe_AES_NI) 233 void DecryptCFB32_RIJ128pipe_AES_NI(const Ipp8u* pSrc, Ipp8u* pDst, int nr, const Ipp8u* pKeys, int cfbBlocks, int cfbBlkSize, const Ipp8u* pIV); 234 #define DecryptCFB128_RIJ128pipe_AES_NI OWNAPI(DecryptCFB128_RIJ128pipe_AES_NI) 235 void DecryptCFB128_RIJ128pipe_AES_NI(const Ipp8u* pSrc, Ipp8u* pDst, int nr, const Ipp8u* pKeys, int len, const Ipp8u* pIV); 236 237 #define cpExpandAesKey_NI OWNAPI(cpExpandAesKey_NI) 238 void cpExpandAesKey_NI(const Ipp8u* pSecret, IppsAESSpec* pCtx); 239 240 #define cpAESEncryptXTS_AES_NI OWNAPI(cpAESEncryptXTS_AES_NI) 241 void cpAESEncryptXTS_AES_NI(Ipp8u* outBlk, const Ipp8u* inpBlk, int nBlks, const Ipp8u* pRKey, int nr, Ipp8u* pTweak); 242 #define cpAESDecryptXTS_AES_NI OWNAPI(cpAESDecryptXTS_AES_NI) 243 void cpAESDecryptXTS_AES_NI(Ipp8u* outBlk, const Ipp8u* inpBlk, int nBlks, const Ipp8u* pRKey, int nr, Ipp8u* pTweak); 244 #endif 245 246 #define ExpandRijndaelKey OWNAPI(ExpandRijndaelKey) 247 void ExpandRijndaelKey(const Ipp8u* pKey, int NK, int NB, int NR, int nKeys, 248 Ipp8u* pEncKeys, Ipp8u* pDecKeys); 249 250 #if(_IPP>_IPP_PX || _IPP32E>_IPP32E_PX) 251 #define Touch_SubsDword_8uT OWNAPI(Touch_SubsDword_8uT) 252 Ipp32u Touch_SubsDword_8uT(Ipp32u inp, const Ipp8u* pTbl, int tblLen); 253 #endif 254 255 #endif /* _PCP_RIJ_H */ 256