gcm.h (4493B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #ifndef GCM_H 6 #define GCM_H 1 7 8 #include "blapii.h" 9 #include "pkcs11t.h" 10 #include <stdint.h> 11 12 #ifdef NSS_X86_OR_X64 13 /* GCC <= 4.8 doesn't support including emmintrin.h without enabling SSE2 */ 14 #if !defined(__clang__) && defined(__GNUC__) && defined(__GNUC_MINOR__) && \ 15 (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ <= 8)) 16 #pragma GCC push_options 17 #pragma GCC target("sse2") 18 #undef NSS_DISABLE_SSE2 19 #define NSS_DISABLE_SSE2 1 20 #endif /* GCC <= 4.8 */ 21 22 #include <emmintrin.h> /* __m128i */ 23 24 #ifdef NSS_DISABLE_SSE2 25 #undef NSS_DISABLE_SSE2 26 #pragma GCC pop_options 27 #endif /* NSS_DISABLE_SSE2 */ 28 #endif 29 30 #ifdef __aarch64__ 31 #include <arm_neon.h> 32 #endif 33 34 #if defined(__powerpc64__) 35 #include "ppc-crypto.h" 36 #endif 37 38 SEC_BEGIN_PROTOS 39 40 #ifdef HAVE_INT128_SUPPORT 41 typedef unsigned __int128 uint128_t; 42 #endif 43 44 typedef struct GCMContextStr GCMContext; 45 46 /* 47 * The context argument is the inner cipher context to use with cipher. The 48 * GCMContext does not own context. context needs to remain valid for as long 49 * as the GCMContext is valid. 50 * 51 * The cipher argument is a block cipher in the ECB encrypt mode. 52 */ 53 GCMContext *GCM_CreateContext(void *context, freeblCipherFunc cipher, 54 const unsigned char *params); 55 void GCM_DestroyContext(GCMContext *gcm, PRBool freeit); 56 SECStatus GCM_EncryptUpdate(GCMContext *gcm, unsigned char *outbuf, 57 unsigned int *outlen, unsigned int maxout, 58 const unsigned char *inbuf, unsigned int inlen, 59 unsigned int blocksize); 60 SECStatus GCM_DecryptUpdate(GCMContext *gcm, unsigned char *outbuf, 61 unsigned int *outlen, unsigned int maxout, 62 const unsigned char *inbuf, unsigned int inlen, 63 unsigned int blocksize); 64 SECStatus GCM_EncryptAEAD(GCMContext *gcm, unsigned char *outbuf, 65 unsigned int *outlen, unsigned int maxout, 66 const unsigned char *inbuf, unsigned int inlen, 67 void *params, unsigned int paramLen, 68 const unsigned char *aad, unsigned int aadLen, 69 unsigned int blocksize); 70 SECStatus GCM_DecryptAEAD(GCMContext *gcm, unsigned char *outbuf, 71 unsigned int *outlen, unsigned int maxout, 72 const unsigned char *inbuf, unsigned int inlen, 73 void *params, unsigned int paramLen, 74 const unsigned char *aad, unsigned int aadLen, 75 unsigned int blocksize); 76 77 /* These functions are here only so we can test them */ 78 #define GCM_HASH_LEN_LEN 8 /* gcm hash defines lengths to be 64 bits */ 79 typedef struct gcmHashContextStr gcmHashContext; 80 typedef SECStatus (*ghash_t)(gcmHashContext *, const unsigned char *, 81 unsigned int); 82 pre_align struct gcmHashContextStr { 83 #ifdef NSS_X86_OR_X64 84 __m128i x, h; 85 #elif defined(__aarch64__) 86 uint64x2_t x, h; 87 #elif defined(USE_PPC_CRYPTO) 88 vec_u64 x, h; 89 #endif 90 uint64_t x_low, x_high, h_high, h_low; 91 unsigned char buffer[MAX_BLOCK_SIZE]; 92 unsigned int bufLen; 93 uint8_t counterBuf[16]; 94 uint64_t cLen; 95 ghash_t ghash_mul; 96 PRBool hw; 97 gcmHashContext *mem; 98 } post_align; 99 100 typedef struct gcmIVContextStr gcmIVContext; 101 struct gcmIVContextStr { 102 PRUint64 counter; 103 PRUint64 max_count; 104 CK_GENERATOR_FUNCTION ivGen; 105 unsigned int fixedBits; 106 unsigned int ivLen; 107 }; 108 109 SECStatus gcmHash_Update(gcmHashContext *ghash, const unsigned char *buf, 110 unsigned int len); 111 SECStatus gcmHash_InitContext(gcmHashContext *ghash, const unsigned char *H, 112 PRBool sw); 113 SECStatus gcmHash_Reset(gcmHashContext *ghash, const unsigned char *AAD, 114 unsigned int AADLen); 115 SECStatus gcmHash_Final(gcmHashContext *ghash, unsigned char *outbuf, 116 unsigned int *outlen, unsigned int maxout); 117 118 void gcm_InitIVContext(gcmIVContext *gcmiv); 119 SECStatus gcm_GenerateIV(gcmIVContext *gcmIv, unsigned char *iv, 120 unsigned int ivLen, unsigned int fixedBits, 121 CK_GENERATOR_FUNCTION ivGen); 122 123 SEC_END_PROTOS 124 125 #endif