pcprijkeysca.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "pcprij.h"
  34. #include "pcprijtables.h"
  35. #include "pcptool.h"
  36. /*
  37. // RconTbl[] contains [x**(i),{00},{00},{00}], i=0,..,10 GF(256)
  38. //
  39. // Note:
  40. // Reference sec 4.2 of FIPS-197 for calculation
  41. */
  42. static const Ipp32u RconTbl[] = {
  43. BYTE0_TO_WORD(0x01), BYTE0_TO_WORD(0x02), BYTE0_TO_WORD(0x04), BYTE0_TO_WORD(0x08),
  44. BYTE0_TO_WORD(0x10), BYTE0_TO_WORD(0x20), BYTE0_TO_WORD(0x40), BYTE0_TO_WORD(0x80),
  45. BYTE0_TO_WORD(0x1B), BYTE0_TO_WORD(0x36), BYTE0_TO_WORD(0x6C), BYTE0_TO_WORD(0xD8),
  46. BYTE0_TO_WORD(0xAB), BYTE0_TO_WORD(0x4D), BYTE0_TO_WORD(0x9A), BYTE0_TO_WORD(0x2F),
  47. BYTE0_TO_WORD(0x5E), BYTE0_TO_WORD(0xBC), BYTE0_TO_WORD(0x63), BYTE0_TO_WORD(0xC6),
  48. BYTE0_TO_WORD(0x97), BYTE0_TO_WORD(0x35), BYTE0_TO_WORD(0x6A), BYTE0_TO_WORD(0xD4),
  49. BYTE0_TO_WORD(0xB3), BYTE0_TO_WORD(0x7D), BYTE0_TO_WORD(0xFA), BYTE0_TO_WORD(0xEF),
  50. BYTE0_TO_WORD(0xC5)
  51. };
  52. /* precomputed table for InvMixColumn() operation */
  53. static const Ipp32u InvMixCol_Tbl[4][256] = {
  54. { LINE(inv_t0) },
  55. { LINE(inv_t1) },
  56. { LINE(inv_t2) },
  57. { LINE(inv_t3) }
  58. };
  59. #define InvMixColumn(x, tbl) \
  60. ( (tbl)[0][ EBYTE((x),0) ] \
  61. ^(tbl)[1][ EBYTE((x),1) ] \
  62. ^(tbl)[2][ EBYTE((x),2) ] \
  63. ^(tbl)[3][ EBYTE((x),3) ] )
  64. /*
  65. // Expansion of key for Rijndael's Encryption
  66. */
  67. void ExpandRijndaelKey(const Ipp8u* pKey, int NK, int NB, int NR, int nKeys,
  68. Ipp8u* pEncKeys, Ipp8u* pDecKeys)
  69. {
  70. Ipp32u* enc_keys = (Ipp32u*)pEncKeys;
  71. Ipp32u* dec_keys = (Ipp32u*)pDecKeys;
  72. /* convert security key to WORD and save into the enc_key array */
  73. int n;
  74. for(n=0; n<NK; n++)
  75. enc_keys[n] = BYTES_TO_WORD(pKey[4*n+0], pKey[4*n+1], pKey[4*n+2], pKey[4*n+3]);
  76. /* 128-bits Key */
  77. if(NK128 == NK) {
  78. const Ipp32u* rtbl = RconTbl;
  79. Ipp32u k0 = enc_keys[0];
  80. Ipp32u k1 = enc_keys[1];
  81. Ipp32u k2 = enc_keys[2];
  82. Ipp32u k3 = enc_keys[3];
  83. for(n=NK128; n<nKeys; n+=NK128) {
  84. /* key expansion: extract bytes, substitute via Sbox and rotate */
  85. k0 ^= BYTES_TO_WORD( RijEncSbox[EBYTE(k3,1)],
  86. RijEncSbox[EBYTE(k3,2)],
  87. RijEncSbox[EBYTE(k3,3)],
  88. RijEncSbox[EBYTE(k3,0)] ) ^ *rtbl++;
  89. k1 ^= k0;
  90. k2 ^= k1;
  91. k3 ^= k2;
  92. /* add key expansion */
  93. enc_keys[n ] = k0;
  94. enc_keys[n+1] = k1;
  95. enc_keys[n+2] = k2;
  96. enc_keys[n+3] = k3;
  97. }
  98. }
  99. /* 192-bits Key */
  100. else if(NK192 == NK) {
  101. const Ipp32u* rtbl = RconTbl;
  102. Ipp32u k0 = enc_keys[0];
  103. Ipp32u k1 = enc_keys[1];
  104. Ipp32u k2 = enc_keys[2];
  105. Ipp32u k3 = enc_keys[3];
  106. Ipp32u k4 = enc_keys[4];
  107. Ipp32u k5 = enc_keys[5];
  108. for(n=NK192; n<nKeys; n+=NK192) {
  109. /* key expansion: extract bytes, substitute via Sbox and rorate */
  110. k0 ^= BYTES_TO_WORD( RijEncSbox[EBYTE(k5,1)],
  111. RijEncSbox[EBYTE(k5,2)],
  112. RijEncSbox[EBYTE(k5,3)],
  113. RijEncSbox[EBYTE(k5,0)] ) ^ *rtbl++;
  114. k1 ^= k0;
  115. k2 ^= k1;
  116. k3 ^= k2;
  117. k4 ^= k3;
  118. k5 ^= k4;
  119. /* add key expansion */
  120. enc_keys[n ] = k0;
  121. enc_keys[n+1] = k1;
  122. enc_keys[n+2] = k2;
  123. enc_keys[n+3] = k3;
  124. enc_keys[n+4] = k4;
  125. enc_keys[n+5] = k5;
  126. }
  127. }
  128. /* 256-bits Key */
  129. else {
  130. const Ipp32u* rtbl = RconTbl;
  131. Ipp32u k0 = enc_keys[0];
  132. Ipp32u k1 = enc_keys[1];
  133. Ipp32u k2 = enc_keys[2];
  134. Ipp32u k3 = enc_keys[3];
  135. Ipp32u k4 = enc_keys[4];
  136. Ipp32u k5 = enc_keys[5];
  137. Ipp32u k6 = enc_keys[6];
  138. Ipp32u k7 = enc_keys[7];
  139. for(n=NK256; n<nKeys; n+=NK256) {
  140. /* key expansion: extract bytes, substitute via Sbox and rorate */
  141. k0 ^= BYTES_TO_WORD( RijEncSbox[EBYTE(k7,1)],
  142. RijEncSbox[EBYTE(k7,2)],
  143. RijEncSbox[EBYTE(k7,3)],
  144. RijEncSbox[EBYTE(k7,0)] ) ^ *rtbl++;
  145. k1 ^= k0;
  146. k2 ^= k1;
  147. k3 ^= k2;
  148. k4 ^= BYTES_TO_WORD( RijEncSbox[EBYTE(k3,0)],
  149. RijEncSbox[EBYTE(k3,1)],
  150. RijEncSbox[EBYTE(k3,2)],
  151. RijEncSbox[EBYTE(k3,3)] );
  152. k5 ^= k4;
  153. k6 ^= k5;
  154. k7 ^= k6;
  155. /* add key expansion */
  156. enc_keys[n ] = k0;
  157. enc_keys[n+1] = k1;
  158. enc_keys[n+2] = k2;
  159. enc_keys[n+3] = k3;
  160. enc_keys[n+4] = k4;
  161. enc_keys[n+5] = k5;
  162. enc_keys[n+6] = k6;
  163. enc_keys[n+7] = k7;
  164. }
  165. }
  166. /*
  167. // Key Expansion for Decryption
  168. */
  169. /* copy keys */
  170. CopyBlock(enc_keys, dec_keys, sizeof(Ipp32u)*nKeys);
  171. /* update decryption keys */
  172. for(n=NB; n<NR*NB; n++)
  173. dec_keys[n] = InvMixColumn(dec_keys[n], InvMixCol_Tbl);
  174. }