pcprij128safedec2pxca.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "pcprij128safe2.h"
  34. #include "pcprijtables.h"
  35. __INLINE Ipp8u getInvSboxValue(Ipp32u x)
  36. {
  37. Ipp32u t[sizeof(RijDecSbox)/CACHE_LINE_SIZE];
  38. const Ipp8u* SboxEntry = RijDecSbox +x%CACHE_LINE_SIZE;
  39. Ipp32u i;
  40. for(i=0; i<sizeof(RijDecSbox)/CACHE_LINE_SIZE; i+=4, SboxEntry += 4*CACHE_LINE_SIZE) {
  41. t[i] = SboxEntry[CACHE_LINE_SIZE*0];
  42. t[i+1] = SboxEntry[CACHE_LINE_SIZE*1];
  43. t[i+2] = SboxEntry[CACHE_LINE_SIZE*2];
  44. t[i+3] = SboxEntry[CACHE_LINE_SIZE*3];
  45. }
  46. return (Ipp8u)t[x/CACHE_LINE_SIZE];
  47. }
  48. __INLINE void invSubBytes(Ipp8u state[])
  49. {
  50. int i;
  51. for(i=0;i<16;i++)
  52. state[i] = getInvSboxValue((Ipp32u)state[i]);
  53. }
  54. __INLINE void invShiftRows(Ipp32u* state)
  55. {
  56. state[1] = ROR32(state[1], 24);
  57. state[2] = ROR32(state[2], 16);
  58. state[3] = ROR32(state[3], 8);
  59. }
  60. __INLINE void invMixColumns(Ipp32u* state)
  61. {
  62. Ipp32u y0 = state[1] ^ state[2] ^ state[3];
  63. Ipp32u y1 = state[0] ^ state[2] ^ state[3];
  64. Ipp32u y2 = state[0] ^ state[1] ^ state[3];
  65. Ipp32u y3 = state[0] ^ state[1] ^ state[2];
  66. Ipp32u t02, t13, t0123;
  67. state[0] = xtime4(state[0]);
  68. state[1] = xtime4(state[1]);
  69. state[2] = xtime4(state[2]);
  70. state[3] = xtime4(state[3]);
  71. y0 ^= state[0] ^ state[1];
  72. y1 ^= state[1] ^ state[2];
  73. y2 ^= state[2] ^ state[3];
  74. y3 ^= state[3] ^ state[0];
  75. t02 = state[0] ^ state[2];
  76. t13 = state[1] ^ state[3];
  77. t02 = xtime4(t02);
  78. t13 = xtime4(t13);
  79. t0123 = t02^t13;
  80. t0123 = xtime4(t0123);
  81. state[0] = y0 ^t02 ^t0123;
  82. state[1] = y1 ^t13 ^t0123;
  83. state[2] = y2 ^t02 ^t0123;
  84. state[3] = y3 ^t13 ^t0123;
  85. }
  86. void Safe2Decrypt_RIJ128(const Ipp8u* in,
  87. Ipp8u* out,
  88. int Nr,
  89. const Ipp8u* RoundKey,
  90. const void* sbox)
  91. {
  92. Ipp32u state[4];
  93. int round=0;
  94. UNREFERENCED_PARAMETER(sbox);
  95. // copy input to the state array
  96. TRANSPOSE((Ipp8u*)state, in);
  97. // add the round key to the state before starting the rounds.
  98. XorRoundKey((Ipp32u*)state, (Ipp32u*)(RoundKey+Nr*16));
  99. // there will be Nr rounds
  100. for(round=Nr-1;round>0;round--) {
  101. invShiftRows(state);
  102. invSubBytes((Ipp8u*)state);
  103. XorRoundKey(state,(Ipp32u*)(RoundKey+round*16));
  104. invMixColumns(state);
  105. }
  106. // last round
  107. invShiftRows(state);
  108. invSubBytes((Ipp8u*)state);
  109. XorRoundKey(state,(Ipp32u*)(RoundKey+0*16));
  110. // copy from the state to output
  111. TRANSPOSE(out, (Ipp8u*)state);
  112. }