1 /*******************************************************************************
2 * Copyright 2013-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 //     AES encryption/decryption (CBC mode)
46 //     AES encryption/decryption (CBC-CS mode)
47 //
48 //  Contents:
49 //     ippsAESDecryptCBC()
50 //
51 */
52 
53 #include "owndefs.h"
54 #include "owncp.h"
55 #include "pcpaesm.h"
56 #include "pcptool.h"
57 
58 #if !defined(_PCP_AES_CBC_H_)
59 #define _PCP_AES_CBC_H_
60 
61 #if (_ALG_AES_SAFE_==_ALG_AES_SAFE_COMPOSITE_GF_)
62 #  pragma message("_ALG_AES_SAFE_COMPOSITE_GF_ enabled")
63 #elif (_ALG_AES_SAFE_==_ALG_AES_SAFE_COMPACT_SBOX_)
64 #  pragma message("_ALG_AES_SAFE_COMPACT_SBOX_ enabled")
65 #  include "pcprijtables.h"
66 #else
67 #  pragma message("_ALG_AES_SAFE_ disabled")
68 #endif
69 
70 /*
71 // AES-CBC decryption
72 //
73 // Parameters:
74 //    pIV         pointer to the initialization vector
75 //    pSrc        pointer to the source data buffer
76 //    pDst        pointer to the target data buffer
77 //    nBlocks     number of decrypted data blocks
78 //    pCtx        pointer to the AES context
79 */
80 static
cpDecryptAES_cbc(const Ipp8u * pIV,const Ipp8u * pSrc,Ipp8u * pDst,int nBlocks,const IppsAESSpec * pCtx)81 void cpDecryptAES_cbc(const Ipp8u* pIV,
82                       const Ipp8u* pSrc, Ipp8u* pDst, int nBlocks,
83                       const IppsAESSpec* pCtx)
84 {
85 #if (_IPP>=_IPP_P8) || (_IPP32E>=_IPP32E_Y8)
86    /* use pipelined version is possible */
87    if(AES_NI_ENABLED==RIJ_AESNI(pCtx)) {
88       DecryptCBC_RIJ128pipe_AES_NI(pSrc, pDst, RIJ_NR(pCtx), RIJ_DKEYS(pCtx), nBlocks*MBS_RIJ128, pIV);
89    }
90    else
91 #endif
92    {
93       /* setup decoder method */
94       RijnCipher decoder = RIJ_DECODER(pCtx);
95 
96       /* copy IV */
97       Ipp32u iv[NB(128)];
98       CopyBlock16(pIV, iv);
99 
100       /* not inplace block-by-block decryption */
101       if(pSrc != pDst) {
102          while(nBlocks) {
103             //decoder((const Ipp32u*)pSrc, (Ipp32u*)pDst, RIJ_NR(pCtx), RIJ_DKEYS(pCtx), (const Ipp32u (*)[256])RIJ_DEC_SBOX(pCtx));
104             #if (_ALG_AES_SAFE_==_ALG_AES_SAFE_COMPACT_SBOX_)
105             decoder(pSrc, pDst, RIJ_NR(pCtx), RIJ_EKEYS(pCtx), RijDecSbox/*NULL*/);
106             #else
107             decoder(pSrc, pDst, RIJ_NR(pCtx), RIJ_DKEYS(pCtx), NULL);
108             #endif
109 
110             ((Ipp32u*)pDst)[0] ^= iv[0];
111             ((Ipp32u*)pDst)[1] ^= iv[1];
112             ((Ipp32u*)pDst)[2] ^= iv[2];
113             ((Ipp32u*)pDst)[3] ^= iv[3];
114 
115             iv[0] = ((Ipp32u*)pSrc)[0];
116             iv[1] = ((Ipp32u*)pSrc)[1];
117             iv[2] = ((Ipp32u*)pSrc)[2];
118             iv[3] = ((Ipp32u*)pSrc)[3];
119 
120             pSrc += MBS_RIJ128;
121             pDst += MBS_RIJ128;
122             nBlocks--;
123          }
124       }
125 
126       /* inplace block-by-block decryption */
127       else {
128          Ipp32u tmpInp[NB(128)];
129          Ipp32u tmpOut[NB(128)];
130 
131          while(nBlocks) {
132             CopyBlock16(pSrc, tmpInp);
133             //decoder(tmpInp, tmpOut, RIJ_NR(pCtx), RIJ_DKEYS(pCtx), (const Ipp32u (*)[256])RIJ_DEC_SBOX(pCtx));
134             #if (_ALG_AES_SAFE_==_ALG_AES_SAFE_COMPACT_SBOX_)
135             decoder((Ipp8u*)tmpInp, (Ipp8u*)tmpOut, RIJ_NR(pCtx), RIJ_EKEYS(pCtx), RijDecSbox/*NULL*/);
136             #else
137             decoder((Ipp8u*)tmpInp, (Ipp8u*)tmpOut, RIJ_NR(pCtx), RIJ_DKEYS(pCtx), NULL);
138             #endif
139 
140             tmpOut[0] ^= iv[0];
141             tmpOut[1] ^= iv[1];
142             tmpOut[2] ^= iv[2];
143             tmpOut[3] ^= iv[3];
144 
145             CopyBlock16(tmpOut, pDst);
146 
147             iv[0] = tmpInp[0];
148             iv[1] = tmpInp[1];
149             iv[2] = tmpInp[2];
150             iv[3] = tmpInp[3];
151 
152             pSrc += MBS_RIJ128;
153             pDst += MBS_RIJ128;
154             nBlocks--;
155          }
156       }
157    }
158 }
159 
160 
161 /*
162 // AES-CBC ecnryption
163 //
164 // Parameters:
165 //    pIV         pointer to the initialization vector
166 //    pSrc        pointer to the source data buffer
167 //    pDst        pointer to the target data buffer
168 //    nBlocks     number of ecnrypted data blocks
169 //    pCtx        pointer to the AES context
170 */
171 static
cpEncryptAES_cbc(const Ipp8u * pIV,const Ipp8u * pSrc,Ipp8u * pDst,int nBlocks,const IppsAESSpec * pCtx)172 void cpEncryptAES_cbc(const Ipp8u* pIV,
173                       const Ipp8u* pSrc, Ipp8u* pDst, int nBlocks, const IppsAESSpec* pCtx)
174 {
175 #if (_IPP>=_IPP_P8) || (_IPP32E>=_IPP32E_Y8)
176    if(AES_NI_ENABLED==RIJ_AESNI(pCtx)) {
177       EncryptCBC_RIJ128_AES_NI(pSrc, pDst, RIJ_NR(pCtx), RIJ_EKEYS(pCtx), nBlocks*MBS_RIJ128, pIV);
178    }
179    else
180 #endif
181    {
182       /* setup encoder method */
183       RijnCipher encoder = RIJ_ENCODER(pCtx);
184 
185       /* read IV */
186       Ipp32u iv[NB(128)];
187       CopyBlock16(pIV, iv);
188 
189       /* block-by-block encryption */
190       while(nBlocks) {
191          iv[0] ^= ((Ipp32u*)pSrc)[0];
192          iv[1] ^= ((Ipp32u*)pSrc)[1];
193          iv[2] ^= ((Ipp32u*)pSrc)[2];
194          iv[3] ^= ((Ipp32u*)pSrc)[3];
195 
196          //encoder(iv, (Ipp32u*)pDst, RIJ_NR(pCtx), RIJ_EKEYS(pCtx), (const Ipp32u (*)[256])RIJ_ENC_SBOX(pCtx));
197          #if (_ALG_AES_SAFE_==_ALG_AES_SAFE_COMPACT_SBOX_)
198          encoder((Ipp8u*)iv, pDst, RIJ_NR(pCtx), RIJ_EKEYS(pCtx), RijEncSbox/*NULL*/);
199          #else
200          encoder((Ipp8u*)iv, pDst, RIJ_NR(pCtx), RIJ_EKEYS(pCtx), NULL);
201          #endif
202 
203          iv[0] = ((Ipp32u*)pDst)[0];
204          iv[1] = ((Ipp32u*)pDst)[1];
205          iv[2] = ((Ipp32u*)pDst)[2];
206          iv[3] = ((Ipp32u*)pDst)[3];
207 
208          pSrc += MBS_RIJ128;
209          pDst += MBS_RIJ128;
210          nBlocks--;
211       }
212    }
213 }
214 
215 #endif /* #if !defined(_PCP_AES_CBC_H_) */
216