pcprij.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #if !defined(_PCP_RIJ_H)
  32. #define _PCP_RIJ_H
  33. /*
  34. // The GF(256) modular polynomial and elements
  35. */
  36. #define WPOLY 0x011B
  37. #define BPOLY 0x1B
  38. /*
  39. // Make WORD using 4 arbitrary bytes
  40. */
  41. #define BYTES_TO_WORD(b0,b1,b2,b3) ( ( ((Ipp32u)((Ipp8u)(b3))) <<24 ) \
  42. |( ((Ipp32u)((Ipp8u)(b2))) <<16 ) \
  43. |( ((Ipp32u)((Ipp8u)(b1))) << 8 ) \
  44. |( ((Ipp32u)((Ipp8u)(b0))) ) )
  45. /*
  46. // Make WORD setting byte in specified position
  47. */
  48. #define BYTE0_TO_WORD(b) BYTES_TO_WORD((b), 0, 0, 0)
  49. #define BYTE1_TO_WORD(b) BYTES_TO_WORD( 0, (b), 0, 0)
  50. #define BYTE2_TO_WORD(b) BYTES_TO_WORD( 0, 0, (b), 0)
  51. #define BYTE3_TO_WORD(b) BYTES_TO_WORD( 0, 0, 0, (b))
  52. /*
  53. // Extract byte from specified position n.
  54. // Sure, n=0,1,2 or 3 only
  55. */
  56. #define EBYTE(w,n) ((Ipp8u)((w) >> (8 * (n))))
  57. /*
  58. // Rijndael's spec
  59. */
  60. typedef void (*RijnCipher)(const Ipp8u* pInpBlk, Ipp8u* pOutBlk, int nr, const Ipp8u* pKeys, const void* pTbl);
  61. struct _cpRijndael128 {
  62. IppCtxId idCtx; /* Rijndael spec identifier */
  63. int nk; /* security key length (words) */
  64. int nb; /* data block size (words) */
  65. int nr; /* number of rounds */
  66. RijnCipher encoder; /* encoder/decoder */
  67. RijnCipher decoder; /* entry point */
  68. Ipp32u* pEncTbl; /* expanded S-boxes for */
  69. Ipp32u* pDecTbl; /* encryption and decryption */
  70. Ipp32u enc_keys[64]; /* array of keys for encryprion */
  71. Ipp32u dec_keys[64]; /* array of keys for decryprion */
  72. Ipp32u aesNI; /* AES instruction available */
  73. Ipp32u safeInit; /* SafeInit performed */
  74. };
  75. /* alignment */
  76. #define RIJ_ALIGNMENT (16)
  77. #define MBS_RIJ128 (128/8) /* message block size (bytes) */
  78. #define MBS_RIJ192 (192/8)
  79. #define MBS_RIJ256 (256/8)
  80. #define SR (4) /* number of rows in STATE data */
  81. #define NB(msgBlks) ((msgBlks)/32) /* message block size (words) */
  82. /* 4-word for 128-bits data block */
  83. /* 6-word for 192-bits data block */
  84. /* 8-word for 256-bits data block */
  85. #define NK(keybits) ((keybits)/32) /* key length (words): */
  86. #define NK128 NK(ippRijndaelKey128)/* 4-word for 128-bits security key */
  87. #define NK192 NK(ippRijndaelKey192)/* 6-word for 192-bits security key */
  88. #define NK256 NK(ippRijndaelKey256)/* 8-word for 256-bits security key */
  89. #define NR128_128 (10) /* number of rounds data: 128 bits key: 128 bits are used */
  90. #define NR128_192 (12) /* number of rounds data: 128 bits key: 192 bits are used */
  91. #define NR128_256 (14) /* number of rounds data: 128 bits key: 256 bits are used */
  92. #define NR192_128 (12) /* number of rounds data: 192 bits key: 128 bits are used */
  93. #define NR192_192 (12) /* number of rounds data: 192 bits key: 192 bits are used */
  94. #define NR192_256 (14) /* number of rounds data: 192 bits key: 256 bits are used */
  95. #define NR256_128 (14) /* number of rounds data: 256 bits key: 128 bits are used */
  96. #define NR256_192 (14) /* number of rounds data: 256 bits key: 192 bits are used */
  97. #define NR256_256 (14) /* number of rounds data: 256 bits key: 256 bits are used */
  98. /*
  99. // Useful macros
  100. */
  101. #define RIJ_ID(ctx) ((ctx)->idCtx)
  102. #define RIJ_NB(ctx) ((ctx)->nb)
  103. #define RIJ_NK(ctx) ((ctx)->nk)
  104. #define RIJ_NR(ctx) ((ctx)->nr)
  105. #define RIJ_ENCODER(ctx) ((ctx)->encoder)
  106. #define RIJ_DECODER(ctx) ((ctx)->decoder)
  107. #define RIJ_ENC_SBOX(ctx) ((ctx)->pEncTbl)
  108. #define RIJ_DEC_SBOX(ctx) ((ctx)->pDecTbl)
  109. #define RIJ_EKEYS(ctx) (Ipp8u*)((ctx)->enc_keys)
  110. #define RIJ_DKEYS(ctx) (Ipp8u*)((ctx)->dec_keys)
  111. #define RIJ_AESNI(ctx) ((ctx)->aesNI)
  112. #define RIJ_SAFE_INIT(ctx) ((ctx)->safeInit)
  113. #define RIJ_ID_TEST(ctx) (RIJ_ID((ctx))==idCtxRijndael)
  114. /*
  115. // Internal functions
  116. */
  117. void Safe2Encrypt_RIJ128(const Ipp8u* pInpBlk, Ipp8u* pOutBlk, int nr, const Ipp8u* pKeys, const void* pTbl);
  118. void Safe2Decrypt_RIJ128(const Ipp8u* pInpBlk, Ipp8u* pOutBlk, int nr, const Ipp8u* pKeys, const void* pTbl);
  119. void ExpandRijndaelKey(const Ipp8u* pKey, int NK, int NB, int NR, int nKeys,
  120. Ipp8u* pEncKeys, Ipp8u* pDecKeys);
  121. #endif /* _PCP_RIJ_H */