pcpaesmctrca.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /*
  37. // AES-CRT processing.
  38. //
  39. // Returns: Reason:
  40. // ippStsNullPtrErr pCtx == NULL
  41. // pSrc == NULL
  42. // pDst == NULL
  43. // pCtrValue ==NULL
  44. // ippStsContextMatchErr !VALID_AES_ID()
  45. // ippStsLengthErr len <1
  46. // ippStsCTRSizeErr 128 < ctrNumBitSize < 1
  47. // ippStsNoErr no errors
  48. //
  49. // Parameters:
  50. // pSrc pointer to the source data buffer
  51. // pDst pointer to the target data buffer
  52. // dataLen input/output buffer length (in bytes)
  53. // pCtx pointer to rge AES context
  54. // pCtrValue pointer to the counter block
  55. // ctrNumBitSize counter block size (bits)
  56. //
  57. // Note:
  58. // counter will updated on return
  59. //
  60. */
  61. static
  62. IppStatus cpProcessAES_ctr(const Ipp8u* pSrc, Ipp8u* pDst, int dataLen,
  63. const IppsAESSpec* pCtx,
  64. Ipp8u* pCtrValue, int ctrNumBitSize)
  65. {
  66. /* test context */
  67. IPP_BAD_PTR1_RET(pCtx);
  68. /* use aligned AES context */
  69. pCtx = (IppsAESSpec*)( IPP_ALIGNED_PTR(pCtx, AES_ALIGNMENT) );
  70. /* test the context ID */
  71. IPP_BADARG_RET(!VALID_AES_ID(pCtx), ippStsContextMatchErr);
  72. /* test source, target and counter block pointers */
  73. IPP_BAD_PTR3_RET(pSrc, pDst, pCtrValue);
  74. /* test stream length */
  75. IPP_BADARG_RET((dataLen<1), ippStsLengthErr);
  76. /* test counter block size */
  77. IPP_BADARG_RET(((MBS_RIJ128*8)<ctrNumBitSize)||(ctrNumBitSize<1), ippStsCTRSizeErr);
  78. {
  79. Ipp32u counter[NB(128)];
  80. Ipp32u output[NB(128)];
  81. /* setup encoder method */
  82. RijnCipher encoder = RIJ_ENCODER(pCtx);
  83. /* copy counter */
  84. CopyBlock16(pCtrValue, counter);
  85. /*
  86. // encrypt block-by-block aligned streams
  87. */
  88. while(dataLen>= MBS_RIJ128) {
  89. /* encrypt counter block */
  90. encoder((Ipp8u*)counter, (Ipp8u*)output, RIJ_NR(pCtx), RIJ_EKEYS(pCtx), RijEncSbox);
  91. /* compute ciphertext block */
  92. if( !(IPP_UINT_PTR(pSrc) & 0x3) && !(IPP_UINT_PTR(pDst) & 0x3)) {
  93. ((Ipp32u*)pDst)[0] = output[0]^((Ipp32u*)pSrc)[0];
  94. ((Ipp32u*)pDst)[1] = output[1]^((Ipp32u*)pSrc)[1];
  95. ((Ipp32u*)pDst)[2] = output[2]^((Ipp32u*)pSrc)[2];
  96. ((Ipp32u*)pDst)[3] = output[3]^((Ipp32u*)pSrc)[3];
  97. }
  98. else
  99. XorBlock16(pSrc, output, pDst);
  100. /* encrement counter block */
  101. StdIncrement((Ipp8u*)counter,MBS_RIJ128*8, ctrNumBitSize);
  102. pSrc += MBS_RIJ128;
  103. pDst += MBS_RIJ128;
  104. dataLen -= MBS_RIJ128;
  105. }
  106. /*
  107. // encrypt last data block
  108. */
  109. if(dataLen) {
  110. /* encrypt counter block */
  111. encoder((Ipp8u*)counter, (Ipp8u*)output, RIJ_NR(pCtx), RIJ_EKEYS(pCtx), RijEncSbox);
  112. /* compute ciphertext block */
  113. XorBlock(pSrc, output, pDst,dataLen);
  114. /* encrement counter block */
  115. StdIncrement((Ipp8u*)counter,MBS_RIJ128*8, ctrNumBitSize);
  116. }
  117. /* update counter */
  118. CopyBlock16(counter, pCtrValue);
  119. return ippStsNoErr;
  120. }
  121. }
  122. IPPFUN(IppStatus, ippsAESEncryptCTR,(const Ipp8u* pSrc, Ipp8u* pDst, int dataLen,
  123. const IppsAESSpec* pCtx,
  124. Ipp8u* pCtrValue, int ctrNumBitSize))
  125. {
  126. return cpProcessAES_ctr(pSrc, pDst, dataLen, pCtx, pCtrValue, ctrNumBitSize);
  127. }
  128. IPPFUN(IppStatus, ippsAESDecryptCTR,(const Ipp8u* pSrc, Ipp8u* pDst, int dataLen,
  129. const IppsAESSpec* pCtx,
  130. Ipp8u* pCtrValue, int ctrNumBitSize))
  131. {
  132. return cpProcessAES_ctr(pSrc, pDst, dataLen, pCtx, pCtrValue, ctrNumBitSize);
  133. }