25 #ifdef __SGCTP_USE_OPENSSL__
28 #include "openssl/evp.h"
29 #include "openssl/rand.h"
31 #else // __SGCTP_USE_OPENSSL__
36 #endif // NOT __SGCTP_USE_OPENSSL__
42 using namespace SGCTP;
49 #ifdef __SGCTP_USE_OPENSSL__
51 #endif // __SGCTP_USE_OPENSSL__
53 #ifdef __SGCTP_USE_OPENSSL__
69 #else // __SGCTP_USE_OPENSSL__
89 if( gcry_control( GCRYCTL_INITIALIZATION_FINISHED_P ) )
91 if( !gcry_check_version(
"1.5.0" ) )
93 gcry_control( GCRYCTL_SUSPEND_SECMEM_WARN );
94 gcry_control( GCRYCTL_INIT_SECMEM, 16384, 0 );
95 gcry_control( GCRYCTL_RESUME_SECMEM_WARN );
96 gcry_control( GCRYCTL_INITIALIZATION_FINISHED, 0 );
100 #endif // NOT __SGCTP_USE_OPENSSL__
109 , pucBufferTmp( NULL )
135 const CData &_roData )
153 int __iPayloadSize_RAW = __iReturn;
159 #ifndef __SGCTP_USE_OPENSSL__
162 unsigned char __ucPadLength =
164 memset(
pucBufferTmp+__iPayloadSize_RAW, __ucPadLength, __ucPadLength );
165 __iPayloadSize_RAW += __ucPadLength;
167 #endif // NOT __SGCTP_USE_OPENSSL__
170 int __iPayloadSize = 0;
173 #ifdef __SGCTP_USE_OPENSSL__
182 EVP_CIPHER_CTX __evpCipherCtx;
183 EVP_CIPHER_CTX_init( &__evpCipherCtx );
184 EVP_EncryptInit_ex( &__evpCipherCtx,
187 EVP_EncryptUpdate( &__evpCipherCtx,
188 _pucBuffer+__iPayloadSize, &__iLength,
190 __iPayloadSize += __iLength;
191 EVP_EncryptFinal_ex( &__evpCipherCtx,
192 _pucBuffer+__iPayloadSize, &__iLength );
193 __iPayloadSize += __iLength;
194 EVP_CIPHER_CTX_cleanup( &__evpCipherCtx );
196 #else // __SGCTP_USE_OPENSSL__
204 gcry_cipher_hd_t __gcryCipherHd;
206 gcry_cipher_setkey( __gcryCipherHd,
208 gcry_cipher_setiv( __gcryCipherHd,
210 gcry_cipher_encrypt( __gcryCipherHd,
211 _pucBuffer+__iPayloadSize,
BUFFER_SIZE-__iPayloadSize,
213 __iPayloadSize += __iPayloadSize_RAW;
214 gcry_cipher_close( __gcryCipherHd );
216 #endif // NOT __SGCTP_USE_OPENSSL__
219 return __iPayloadSize;
223 const unsigned char *_pucBuffer,
224 uint16_t _ui16tBufferSize )
239 int __iPayloadSize = 0;
246 #ifdef __SGCTP_USE_OPENSSL__
250 EVP_CIPHER_CTX __evpCipherCtx;
251 EVP_CIPHER_CTX_init( &__evpCipherCtx );
252 EVP_DecryptInit_ex( &__evpCipherCtx,
255 EVP_DecryptUpdate( &__evpCipherCtx,
257 _pucBuffer+__iPayloadSize,
259 __iPayloadSize += __iLength;
260 EVP_DecryptFinal_ex( &__evpCipherCtx,
262 __iPayloadSize += __iLength;
263 EVP_CIPHER_CTX_cleanup( &__evpCipherCtx );
265 #else // __SGCTP_USE_OPENSSL__
268 gcry_cipher_hd_t __gcryCipherHd;
270 gcry_cipher_setkey( __gcryCipherHd,
272 gcry_cipher_setiv( __gcryCipherHd,
274 gcry_cipher_decrypt( __gcryCipherHd,
276 _pucBuffer+__iPayloadSize,
279 gcry_cipher_close( __gcryCipherHd );
281 #endif // NOT __SGCTP_USE_OPENSSL__
286 #ifndef __SGCTP_USE_OPENSSL__
289 unsigned char __ucPadLength =
pucBufferTmp[__iPayloadSize_RAW-1];
290 __iPayloadSize_RAW -= __ucPadLength;
292 #endif // __SGCTP_USE_OPENSSL__
305 __iPayloadSize_RAW );
310 return __iPayloadSize;
326 int _iPasswordLength,
327 const unsigned char *_pucNonce )
344 #ifdef __SGCTP_USE_OPENSSL__
346 PKCS5_PBKDF2_HMAC_SHA1( _pucPassword, _iPasswordLength,
350 __pucCryptoMaterial );
352 #else // __SGCTP_USE_OPENSSL__
354 gcry_kdf_derive( _pucPassword, _iPasswordLength,
355 GCRY_KDF_PBKDF2, GCRY_MD_SHA1,
359 __pucCryptoMaterial );
361 #endif // NOT __SGCTP_USE_OPENSSL__
383 #ifdef __SGCTP_USE_OPENSSL__
389 __pucCryptoMaterial );
391 #else // __SGCTP_USE_OPENSSL__
394 GCRY_KDF_PBKDF2, GCRY_MD_SHA1,
398 __pucCryptoMaterial );
400 #endif // NOT __SGCTP_USE_OPENSSL__
virtual int unserialize(CData *_poData, const unsigned char *_pucBuffer, uint16_t _ui16tBufferSize)
Unserialize the SGCTP data from the given payload buffer.
int makeCryptoKey(const unsigned char *_pucPassword, int _iPasswordLength, const unsigned char *_pucNonce)
Create cryptographic key (and seal)
int incrCryptoKey()
Increment cryptographic key (and seal)
static const uint16_t CRYPTO_KEY_ITER
virtual int alloc()
Allocate resources for payload (un-)serialization.
static const uint16_t BUFFER_SIZE
Buffer size required for payload (un-)serialization.
virtual ~CPayload_AES128()
static void freeBuffer(unsigned char *_pucBuffer)
Free a buffer allocated for payload (un-)serialization.
static const uint16_t CRYPTO_SEAL_SIZE
static const uint16_t CRYPTO_KEY_SIZE
static const uint16_t CRYPTO_NONCE_SIZE
unsigned char pucCryptoKey[CRYPTO_BLOCK_SIZE]
Cryptographic key (used for encryption/decryption)
virtual int serialize(unsigned char *_pucBuffer, const CData &_roData)
Serialize the given SGCTP data into the given payload buffer.
static const int CRYPTO_CIPHER
unsigned char pucCryptoSeal[CRYPTO_SEAL_SIZE]
Cryptographic seal (used to check valid decryption)
static int initCryptoEngine()
Initialize cryptographic engine.
static int makeCryptoNonce(unsigned char *_pucNonce)
Create cryptographic nonce.
virtual int unserialize(CData *_poData, const unsigned char *_pucBuffer, uint16_t _ui16tBufferSize)
Unserialize the SGCTP data from the given payload buffer.
virtual int serialize(unsigned char *_pucBuffer, const CData &_roData)
Serialize the given SGCTP data into the given payload buffer.
static const uint16_t CRYPTO_BLOCK_SIZE
unsigned char * pucBufferTmp
Payload temporary import/export buffer.
virtual void free()
Free resources for payload (un-)serialization.
static const int CRYPTO_MODE
unsigned char pucCryptoNonce[CRYPTO_NONCE_SIZE]
Cryptographic nonce (used for cryptographic hashing)
static unsigned char * allocBuffer()
Allocate a buffer for payload (un-)serialization.