pcpaesgcmtbl2kca.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (C) 2016 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "owndefs.h"
  32. #include "owncp.h"
  33. #include "pcpaesauthgcm.h"
  34. #include "pcptool.h"
  35. #include "pcprijtables.h"
  36. /*
  37. // AES-GCM precomputations.
  38. */
  39. static void RightShiftBlock16(Ipp8u* pBlock)
  40. {
  41. Ipp8u v0 = 0;
  42. int i;
  43. for(i=0; i<16; i++) {
  44. Ipp8u v1 = pBlock[i];
  45. Ipp8u tmp = (Ipp8u)( (v1>>1) | (v0<<7) );
  46. pBlock[i] = tmp;
  47. v0 = v1;
  48. }
  49. }
  50. void AesGcmPrecompute_table2K(Ipp8u* pPrecomputeData, const Ipp8u* pHKey)
  51. {
  52. Ipp8u t[BLOCK_SIZE];
  53. int n;
  54. CopyBlock16(pHKey, t);
  55. for(n=0; n<128-24; n++) {
  56. /* get msb */
  57. int hBit = t[15]&1;
  58. int k = n%32;
  59. if(k<4) {
  60. CopyBlock16(t, pPrecomputeData +1024 +(n/32)*256 +(Ipp32u)(1<<(7-k)));
  61. }
  62. else if(k<8) {
  63. CopyBlock16(t, pPrecomputeData +(n/32)*256 +(Ipp32u)(1<<(11-k)));
  64. }
  65. /* shift */
  66. RightShiftBlock16(t);
  67. /* xor if msb=1 */
  68. if(hBit)
  69. t[0] ^= 0xe1;
  70. }
  71. for(n=0; n<4; n++) {
  72. int m, k;
  73. XorBlock16(pPrecomputeData +n*256, pPrecomputeData +n*256, pPrecomputeData +n*256);
  74. XorBlock16(pPrecomputeData +1024 +n*256, pPrecomputeData +1024 +n*256, pPrecomputeData +1024 +n*256);
  75. for(m=2; m<=8; m*=2)
  76. for(k=1; k<m; k++) {
  77. XorBlock16(pPrecomputeData +n*256+m*16, pPrecomputeData +n*256+k*16, pPrecomputeData +n*256 +(m+k)*16);
  78. XorBlock16(pPrecomputeData +1024 +n*256+m*16, pPrecomputeData +1024 +n*256+k*16, pPrecomputeData +1024 +n*256 +(m+k)*16);
  79. }
  80. }
  81. }
  82. /*
  83. // AesGcmMulGcm_def|safe(Ipp8u* pGhash, const Ipp8u* pHKey)
  84. //
  85. // Ghash = Ghash * HKey mod G()
  86. */
  87. void AesGcmMulGcm_table2K(Ipp8u* pGhash, const Ipp8u* pPrecomputeData, const void* pParam)
  88. {
  89. __ALIGN16 Ipp8u t5[BLOCK_SIZE];
  90. __ALIGN16 Ipp8u t4[BLOCK_SIZE];
  91. __ALIGN16 Ipp8u t3[BLOCK_SIZE];
  92. __ALIGN16 Ipp8u t2[BLOCK_SIZE];
  93. int nw;
  94. Ipp32u a;
  95. UNREFERENCED_PARAMETER(pParam);
  96. XorBlock16(t5, t5, t5);
  97. XorBlock16(t4, t4, t4);
  98. XorBlock16(t3, t3, t3);
  99. XorBlock16(t2, t2, t2);
  100. for(nw=0; nw<4; nw++) {
  101. Ipp32u hashdw = ((Ipp32u*)pGhash)[nw];
  102. a = hashdw & 0xf0f0f0f0;
  103. XorBlock16(t5, pPrecomputeData+1024+EBYTE(a,1)+256*nw, t5);
  104. XorBlock16(t4, pPrecomputeData+1024+EBYTE(a,0)+256*nw, t4);
  105. XorBlock16(t3, pPrecomputeData+1024+EBYTE(a,3)+256*nw, t3);
  106. XorBlock16(t2, pPrecomputeData+1024+EBYTE(a,2)+256*nw, t2);
  107. a = (hashdw<<4) & 0xf0f0f0f0;
  108. XorBlock16(t5, pPrecomputeData+EBYTE(a,1)+256*nw, t5);
  109. XorBlock16(t4, pPrecomputeData+EBYTE(a,0)+256*nw, t4);
  110. XorBlock16(t3, pPrecomputeData+EBYTE(a,3)+256*nw, t3);
  111. XorBlock16(t2, pPrecomputeData+EBYTE(a,2)+256*nw, t2);
  112. }
  113. XorBlock(t2+1, t3, t2+1, BLOCK_SIZE-1);
  114. XorBlock(t5+1, t2, t5+1, BLOCK_SIZE-1);
  115. XorBlock(t4+1, t5, t4+1, BLOCK_SIZE-1);
  116. nw = t3[BLOCK_SIZE-1];
  117. a = (Ipp32u)AesGcmConst_table[nw];
  118. a <<= 8;
  119. nw = t2[BLOCK_SIZE-1];
  120. a ^= (Ipp32u)AesGcmConst_table[nw];
  121. a <<= 8;
  122. nw = t5[BLOCK_SIZE-1];
  123. a ^= (Ipp32u)AesGcmConst_table[nw];
  124. XorBlock(t4, &a, t4, sizeof(Ipp32u));
  125. CopyBlock16(t4, pGhash);
  126. }
  127. /*
  128. // authenticates n*BLOCK_SIZE bytes
  129. */
  130. void AesGcmAuth_table2K(Ipp8u* pHash, const Ipp8u* pSrc, int len, const Ipp8u* pHKey, const void* pParam)
  131. {
  132. UNREFERENCED_PARAMETER(pParam);
  133. while(len>=BLOCK_SIZE) {
  134. /* add src */
  135. XorBlock16(pSrc, pHash, pHash);
  136. /* hash it */
  137. AesGcmMulGcm_table2K(pHash, pHKey, AesGcmConst_table);
  138. pSrc += BLOCK_SIZE;
  139. len -= BLOCK_SIZE;
  140. }
  141. }
  142. /*
  143. // encrypts and authenticates n*BLOCK_SIZE bytes
  144. */
  145. void wrpAesGcmEnc_table2K(Ipp8u* pDst, const Ipp8u* pSrc, int len, IppsAES_GCMState* pState)
  146. {
  147. Ipp8u* pHashedData = pDst;
  148. int hashedDataLen = len;
  149. Ipp8u* pCounter = AESGCM_COUNTER(pState);
  150. Ipp8u* pECounter = AESGCM_ECOUNTER(pState);
  151. IppsAESSpec* pAES = AESGCM_CIPHER(pState);
  152. RijnCipher encoder = RIJ_ENCODER(pAES);
  153. while(len>=BLOCK_SIZE) {
  154. /* encrypt whole AES block */
  155. XorBlock16(pSrc, pECounter, pDst);
  156. pSrc += BLOCK_SIZE;
  157. pDst += BLOCK_SIZE;
  158. len -= BLOCK_SIZE;
  159. /* increment counter block */
  160. IncrementCounter32(pCounter);
  161. /* and encrypt counter */
  162. encoder(pCounter, pECounter, RIJ_NR(pAES), RIJ_EKEYS(pAES), RijEncSbox);
  163. }
  164. AesGcmAuth_table2K(AESGCM_GHASH(pState), pHashedData, hashedDataLen, AESGCM_HKEY(pState), AesGcmConst_table);
  165. }
  166. /*
  167. // authenticates and decrypts n*BLOCK_SIZE bytes
  168. */
  169. void wrpAesGcmDec_table2K(Ipp8u* pDst, const Ipp8u* pSrc, int len, IppsAES_GCMState* pState)
  170. {
  171. AesGcmAuth_table2K(AESGCM_GHASH(pState), pSrc, len, AESGCM_HKEY(pState), AesGcmConst_table);
  172. {
  173. Ipp8u* pCounter = AESGCM_COUNTER(pState);
  174. Ipp8u* pECounter = AESGCM_ECOUNTER(pState);
  175. IppsAESSpec* pAES = AESGCM_CIPHER(pState);
  176. RijnCipher encoder = RIJ_ENCODER(pAES);
  177. while(len>=BLOCK_SIZE) {
  178. /* encrypt whole AES block */
  179. XorBlock16(pSrc, pECounter, pDst);
  180. pSrc += BLOCK_SIZE;
  181. pDst += BLOCK_SIZE;
  182. len -= BLOCK_SIZE;
  183. /* increment counter block */
  184. IncrementCounter32(pCounter);
  185. /* and encrypt counter */
  186. encoder(pCounter, pECounter, RIJ_NR(pAES), RIJ_EKEYS(pAES), RijEncSbox);
  187. }
  188. }
  189. }