pcpaesauthgcm.h 5.7 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(_CP_AESAUTH_GCM_H)
  32. #define _CP_AESAUTH_GCM_H
  33. #include "owndefs.h"
  34. #include "owncp.h"
  35. #include "pcpaesm.h"
  36. #define BLOCK_SIZE (MBS_RIJ128)
  37. /* GCM Hash prototype: GHash = GHash*HKey mod G() */
  38. typedef void (*MulGcm_)(Ipp8u* pGHash, const Ipp8u* pHKey, const void* pParam);
  39. /* GCM Authentication prototype: GHash = (GHash^src[])*HKey mod G() */
  40. typedef void (*Auth_)(Ipp8u* pHash, const Ipp8u* pSrc, int len, const Ipp8u* pHKey, const void* pParam);
  41. /* GCM Encrypt_Authentication prototype */
  42. typedef void (*Encrypt_)(Ipp8u* pDst, const Ipp8u* pSrc, int len, IppsAES_GCMState* pCtx);
  43. /* GCM Authentication_Decrypt prototype */
  44. typedef void (*Decrypt_)(Ipp8u* pDst, const Ipp8u* pSrc, int len, IppsAES_GCMState* pCtx);
  45. typedef enum {
  46. GcmInit,
  47. GcmIVprocessing,
  48. GcmAADprocessing,
  49. GcmTXTprocessing
  50. } GcmState;
  51. struct _cpAES_GCM {
  52. IppCtxId idCtx; /* AES-GCM id */
  53. GcmState state; /* GCM state: Init, IV|AAD|TXT proccessing */
  54. Ipp64u ivLen; /* IV length (bytes) */
  55. Ipp64u aadLen; /* header length (bytes) */
  56. Ipp64u txtLen; /* text length (bytes) */
  57. int bufLen; /* staff buffer length */
  58. __ALIGN16 /* aligned buffers */
  59. Ipp8u counter[BLOCK_SIZE]; /* counter */
  60. Ipp8u ecounter0[BLOCK_SIZE]; /* encrypted initial counter */
  61. Ipp8u ecounter[BLOCK_SIZE]; /* encrypted counter */
  62. Ipp8u ghash[BLOCK_SIZE]; /* ghash accumulator */
  63. MulGcm_ hashFun; /* AES-GCM mul function */
  64. Auth_ authFun; /* authentication function */
  65. Encrypt_ encFun; /* encryption & authentication */
  66. Decrypt_ decFun; /* authentication & decryption */
  67. __ALIGN16 /* aligned AES context */
  68. IppsAESSpec cipher;
  69. __ALIGN16 /* aligned pre-computed data: */
  70. Ipp8u multiplier[BLOCK_SIZE]; /* - (default) hKey */
  71. /* - (ase_ni) hKey*t, (hKey*t)^2, (hKey*t)^4 */
  72. /* - (safe) hKey*(t^i), i=0,...,127 */
  73. };
  74. #define CTR_POS 12
  75. /* alignment */
  76. #define AESGCM_ALIGNMENT (16)
  77. #define PRECOMP_DATA_SIZE_AES_NI_AESGCM (BLOCK_SIZE*4)
  78. #define PRECOMP_DATA_SIZE_FAST2K (BLOCK_SIZE*128)
  79. /*
  80. // Useful macros
  81. */
  82. #define AESGCM_ID(stt) ((stt)->idCtx)
  83. #define AESGCM_STATE(stt) ((stt)->state)
  84. #define AESGCM_IV_LEN(stt) ((stt)->ivLen)
  85. #define AESGCM_AAD_LEN(stt) ((stt)->aadLen)
  86. #define AESGCM_TXT_LEN(stt) ((stt)->txtLen)
  87. #define AESGCM_BUFLEN(stt) ((stt)->bufLen)
  88. #define AESGCM_COUNTER(stt) ((stt)->counter)
  89. #define AESGCM_ECOUNTER0(stt) ((stt)->ecounter0)
  90. #define AESGCM_ECOUNTER(stt) ((stt)->ecounter)
  91. #define AESGCM_GHASH(stt) ((stt)->ghash)
  92. #define AESGCM_HASH(stt) ((stt)->hashFun)
  93. #define AESGCM_AUTH(stt) ((stt)->authFun)
  94. #define AESGCM_ENC(stt) ((stt)->encFun)
  95. #define AESGCM_DEC(stt) ((stt)->decFun)
  96. #define AESGCM_CIPHER(stt) (IppsAESSpec*)(&((stt)->cipher))
  97. #define AESGCM_HKEY(stt) ((stt)->multiplier)
  98. #define AESGCM_CPWR(stt) ((stt)->multiplier)
  99. #define AES_GCM_MTBL(stt) ((stt)->multiplier)
  100. #define AESGCM_VALID_ID(stt) (AESGCM_ID((stt))==idCtxAESGCM)
  101. __INLINE void IncrementCounter32(Ipp8u* pCtr)
  102. {
  103. int i;
  104. for(i=BLOCK_SIZE-1; i>=CTR_POS && 0==(Ipp8u)(++pCtr[i]); i--) ;
  105. }
  106. void AesGcmPrecompute_table2K(Ipp8u* pPrecomputeData, const Ipp8u* pHKey);
  107. void AesGcmMulGcm_table2K(Ipp8u* pGhash, const Ipp8u* pHkey, const void* pParam);
  108. void AesGcmAuth_table2K(Ipp8u* pGhash, const Ipp8u* pSrc, int len, const Ipp8u* pHkey, const void* pParam);
  109. void wrpAesGcmEnc_table2K(Ipp8u* pDst, const Ipp8u* pSrc, int len, IppsAES_GCMState* pCtx);
  110. void wrpAesGcmDec_table2K(Ipp8u* pDst, const Ipp8u* pSrc, int len, IppsAES_GCMState* pCtx);
  111. extern const Ipp16u AesGcmConst_table[256]; /* precomputed reduction table */
  112. #endif /* _CP_AESAUTH_GCM_H*/