pcpaesminitca.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "pcpaesm.h"
  34. #include "pcptool.h"
  35. #include "pcprijtables.h"
  36. /*F*
  37. // Name: ippsAESGetSize
  38. //
  39. // Purpose: Returns size of AES context (in bytes).
  40. //
  41. // Returns: Reason:
  42. // ippStsNullPtrErr pSzie == NULL
  43. // ippStsNoErr no errors
  44. //
  45. // Parameters:
  46. // pSize pointer to AES size of context(in bytes)
  47. //
  48. *F*/
  49. IPPFUN(IppStatus, ippsAESGetSize,(int* pSize))
  50. {
  51. /* test size's pointer */
  52. IPP_BAD_PTR1_RET(pSize);
  53. *pSize = cpSizeofCtx_AES();
  54. return ippStsNoErr;
  55. }
  56. /* number of rounds (use [NK] for access) */
  57. static int rij128nRounds[3] = {NR128_128, NR128_192, NR128_256};
  58. /*
  59. // number of keys (estimation only!) (use [NK] for access)
  60. //
  61. // accurate number of keys necassary for encrypt/decrypt are:
  62. // nKeys = NB * (NR+1)
  63. // where NB - data block size (32-bit words)
  64. // NR - number of rounds (depend on NB and keyLen)
  65. //
  66. // but the estimation
  67. // estnKeys = (NK*n) >= nKeys
  68. // or
  69. // estnKeys = ( (NB*(NR+1) + (NK-1)) / NK) * NK
  70. // where NK - key length (words)
  71. // NB - data block size (word)
  72. // NR - number of rounds (depend on NB and keyLen)
  73. // nKeys - accurate numner of keys
  74. // is more convinient when calculates key extension
  75. */
  76. static int rij128nKeys[3] = {44, 54, 64 };
  77. /*
  78. // helper for nRounds[] and estnKeys[] access
  79. // note: x is length in 32-bits words
  80. */
  81. __INLINE int rij_index(int x)
  82. { return (x-NB(128))>>1; }
  83. /*F*
  84. // Name: ippsAESInit
  85. //
  86. // Purpose: Init AES context for future usage
  87. // and setup secret key.
  88. //
  89. // Returns: Reason:
  90. // ippStsNullPtrErr pCtx == NULL
  91. // ippStsMemAllocErr size of buffer is not match fro operation
  92. // ippStsLengthErr keyLen != 16
  93. // keyLen != 24
  94. // keyLen != 32
  95. //
  96. // Parameters:
  97. // pKey secret key
  98. // keyLen length of the secret key (in bytes)
  99. // pCtx pointer to buffer initialized as AES context
  100. // ctxSize available size (in bytes) of buffer above
  101. //
  102. // Note:
  103. // if pKey==NULL, then AES initialized by zero value key
  104. //
  105. *F*/
  106. IPPFUN(IppStatus, ippsAESInit,(const Ipp8u* pKey, int keyLen,
  107. IppsAESSpec* pCtxRaw, int rawCtxSize))
  108. {
  109. /* test context pointer */
  110. IPP_BAD_PTR1_RET(pCtxRaw);
  111. /* make sure in legal keyLen */
  112. IPP_BADARG_RET(keyLen!=16 && keyLen!=24 && keyLen!=32, ippStsLengthErr);
  113. {
  114. /* use aligned Rijndael context */
  115. IppsAESSpec* pCtx = (IppsAESSpec*)( IPP_ALIGNED_PTR(pCtxRaw, AES_ALIGNMENT) );
  116. /* test available size of context buffer */
  117. if(((Ipp8u*)pCtx+sizeof(IppsAESSpec)) > ((Ipp8u*)pCtxRaw+rawCtxSize))
  118. IPP_ERROR_RET(ippStsMemAllocErr);
  119. else {
  120. int keyWords = NK(keyLen*BITSIZE(Ipp8u));
  121. int nExpKeys = rij128nKeys [ rij_index(keyWords) ];
  122. int nRounds = rij128nRounds[ rij_index(keyWords) ];
  123. Ipp8u zeroKey[32] = {0};
  124. const Ipp8u* pActualKey = pKey? pKey : zeroKey;
  125. /* clear context */
  126. PaddBlock(0, pCtx, sizeof(IppsAESSpec));
  127. /* init spec */
  128. RIJ_ID(pCtx) = idCtxRijndael;
  129. RIJ_NB(pCtx) = NB(128);
  130. RIJ_NK(pCtx) = keyWords;
  131. RIJ_NR(pCtx) = nRounds;
  132. RIJ_SAFE_INIT(pCtx) = 1;
  133. /* set key expansion */
  134. ExpandRijndaelKey(pActualKey, keyWords, NB(128), nRounds, nExpKeys,
  135. RIJ_EKEYS(pCtx),
  136. RIJ_DKEYS(pCtx));
  137. {
  138. int nr;
  139. Ipp8u* pEnc_key = (Ipp8u*)(RIJ_EKEYS(pCtx));
  140. /* update key material: transpose inplace */
  141. for(nr=0; nr<(1+nRounds); nr++, pEnc_key+=16) {
  142. SWAP(pEnc_key[ 1], pEnc_key[ 4]);
  143. SWAP(pEnc_key[ 2], pEnc_key[ 8]);
  144. SWAP(pEnc_key[ 3], pEnc_key[12]);
  145. SWAP(pEnc_key[ 6], pEnc_key[ 9]);
  146. SWAP(pEnc_key[ 7], pEnc_key[13]);
  147. SWAP(pEnc_key[11], pEnc_key[14]);
  148. }
  149. }
  150. RIJ_ENCODER(pCtx) = Safe2Encrypt_RIJ128; /* safe encoder (compact Sbox)) */
  151. RIJ_DECODER(pCtx) = Safe2Decrypt_RIJ128; /* safe decoder (compact Sbox)) */
  152. return ippStsNoErr;
  153. }
  154. }
  155. }