diff --git a/mongoose.c b/mongoose.c index 7455f440..74f1a725 100644 --- a/mongoose.c +++ b/mongoose.c @@ -8512,9 +8512,9 @@ int aes_setkey(aes_context *ctx, // AES context provided by our caller } #if AES_DECRYPTION - if (mode == DECRYPT) // expand our key for encryption or decryption + if (mode == MG_DECRYPT) // expand our key for encryption or decryption return (aes_set_decryption_key(ctx, key, keysize)); - else /* ENCRYPT */ + else /* MG_ENCRYPT */ #endif /* AES_DECRYPTION */ return (aes_set_encryption_key(ctx, key, keysize)); } @@ -8545,7 +8545,7 @@ int aes_cipher(aes_context *ctx, const uchar input[16], uchar output[16]) { #if AES_DECRYPTION // whether AES decryption is supported - if (ctx->mode == DECRYPT) { + if (ctx->mode == MG_DECRYPT) { for (i = (ctx->rounds >> 1) - 1; i > 0; i--) { AES_RROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3); AES_RROUND(X0, X1, X2, X3, Y0, Y1, Y2, Y3); @@ -8572,7 +8572,7 @@ int aes_cipher(aes_context *ctx, const uchar input[16], uchar output[16]) { ((uint32_t) RSb[(Y2 >> 8) & 0xFF] << 8) ^ ((uint32_t) RSb[(Y1 >> 16) & 0xFF] << 16) ^ ((uint32_t) RSb[(Y0 >> 24) & 0xFF] << 24); - } else /* ENCRYPT */ + } else /* MG_ENCRYPT */ { #endif /* AES_DECRYPTION */ @@ -8799,7 +8799,7 @@ int gcm_setkey(gcm_context *ctx, // pointer to caller-provided gcm context // encrypt the null 128-bit block to generate a key-based value // which is then used to initialize our GHASH lookup tables - if ((ret = aes_setkey(&ctx->aes_ctx, ENCRYPT, key, keysize)) != 0) + if ((ret = aes_setkey(&ctx->aes_ctx, MG_ENCRYPT, key, keysize)) != 0) return (ret); if ((ret = aes_cipher(&ctx->aes_ctx, h, h)) != 0) return (ret); @@ -8877,7 +8877,7 @@ int gcm_start(gcm_context *ctx, // pointer to user-provided GCM context ctx->add_len = 0; ctx->mode = mode; // set the GCM encryption/decryption mode - ctx->aes_ctx.mode = ENCRYPT; // GCM *always* runs AES in ENCRYPTION mode + ctx->aes_ctx.mode = MG_ENCRYPT; // GCM *always* runs AES in ENCRYPTION mode if (iv_len == 12) { // GCM natively uses a 12-byte, 96-bit IV memcpy(ctx->y, iv, iv_len); // copy the IV to the top of the 'y' buff @@ -8948,7 +8948,7 @@ int gcm_update(gcm_context *ctx, // pointer to user-provided GCM context if ((ret = aes_cipher(&ctx->aes_ctx, ctx->y, ectr)) != 0) return (ret); // encrypt or decrypt the input to the output - if (ctx->mode == ENCRYPT) { + if (ctx->mode == MG_ENCRYPT) { for (i = 0; i < use_len; i++) { // XOR the cipher's ouptut vector (ectr) with our input output[i] = (uchar) (ectr[i] ^ input[i]); @@ -9086,7 +9086,7 @@ int gcm_auth_decrypt( (which is an identical XORing to reverse the previous one) and also to re-generate the matching authentication tag */ - gcm_crypt_and_tag(ctx, DECRYPT, iv, iv_len, add, add_len, input, output, + gcm_crypt_and_tag(ctx, MG_DECRYPT, iv, iv_len, add, add_len, input, output, length, check_tag, tag_len); // now we verify the authentication tag in 'constant time' @@ -9131,7 +9131,7 @@ int aes_gcm_encrypt(unsigned char *output, // gcm_setkey(&ctx, key, (const uint) key_len); - ret = gcm_crypt_and_tag(&ctx, ENCRYPT, iv, iv_len, aead, aead_len, input, output, + ret = gcm_crypt_and_tag(&ctx, MG_ENCRYPT, iv, iv_len, aead, aead_len, input, output, input_length, tag, tag_len); gcm_zero_ctx(&ctx); @@ -9151,7 +9151,7 @@ int aes_gcm_decrypt(unsigned char *output, const unsigned char *input, gcm_setkey(&ctx, key, (const uint) key_len); - ret = gcm_crypt_and_tag(&ctx, DECRYPT, iv, iv_len, NULL, 0, input, output, + ret = gcm_crypt_and_tag(&ctx, MG_DECRYPT, iv, iv_len, NULL, 0, input, output, input_length, tag_buf, tag_len); gcm_zero_ctx(&ctx); diff --git a/mongoose.h b/mongoose.h index 62d3f464..05198659 100644 --- a/mongoose.h +++ b/mongoose.h @@ -1220,8 +1220,8 @@ void mg_hmac_sha256(uint8_t dst[32], uint8_t *key, size_t keysz, uint8_t *data, #define AES_DECRYPTION 1 // whether AES decryption is supported /******************************************************************************/ -#define ENCRYPT 1 // specify whether we're encrypting -#define DECRYPT 0 // or decrypting +#define MG_ENCRYPT 1 // specify whether we're encrypting +#define MG_DECRYPT 0 // or decrypting @@ -1339,7 +1339,7 @@ int gcm_setkey(gcm_context *ctx, // caller-provided context ptr ******************************************************************************/ int gcm_crypt_and_tag( gcm_context *ctx, // gcm context with key already setup - int mode, // cipher direction: ENCRYPT (1) or DECRYPT (0) + int mode, // cipher direction: MG_ENCRYPT (1) or MG_DECRYPT (0) const uchar *iv, // pointer to the 12-byte initialization vector size_t iv_len, // byte length if the IV. should always be 12 const uchar *add, // pointer to the non-ciphered additional data @@ -1384,7 +1384,7 @@ int gcm_auth_decrypt( ******************************************************************************/ int gcm_start( gcm_context *ctx, // pointer to user-provided GCM context - int mode, // ENCRYPT (1) or DECRYPT (0) + int mode, // MG_ENCRYPT (1) or MG_DECRYPT (0) const uchar *iv, // pointer to initialization vector size_t iv_len, // IV length in bytes (should == 12) const uchar *add, // pointer to additional AEAD data (NULL if none) diff --git a/src/tls_aes128.c b/src/tls_aes128.c index 566e3aab..be3bf7ef 100644 --- a/src/tls_aes128.c +++ b/src/tls_aes128.c @@ -353,9 +353,9 @@ int aes_setkey(aes_context *ctx, // AES context provided by our caller } #if AES_DECRYPTION - if (mode == DECRYPT) // expand our key for encryption or decryption + if (mode == MG_DECRYPT) // expand our key for encryption or decryption return (aes_set_decryption_key(ctx, key, keysize)); - else /* ENCRYPT */ + else /* MG_ENCRYPT */ #endif /* AES_DECRYPTION */ return (aes_set_encryption_key(ctx, key, keysize)); } @@ -386,7 +386,7 @@ int aes_cipher(aes_context *ctx, const uchar input[16], uchar output[16]) { #if AES_DECRYPTION // whether AES decryption is supported - if (ctx->mode == DECRYPT) { + if (ctx->mode == MG_DECRYPT) { for (i = (ctx->rounds >> 1) - 1; i > 0; i--) { AES_RROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3); AES_RROUND(X0, X1, X2, X3, Y0, Y1, Y2, Y3); @@ -413,7 +413,7 @@ int aes_cipher(aes_context *ctx, const uchar input[16], uchar output[16]) { ((uint32_t) RSb[(Y2 >> 8) & 0xFF] << 8) ^ ((uint32_t) RSb[(Y1 >> 16) & 0xFF] << 16) ^ ((uint32_t) RSb[(Y0 >> 24) & 0xFF] << 24); - } else /* ENCRYPT */ + } else /* MG_ENCRYPT */ { #endif /* AES_DECRYPTION */ @@ -640,7 +640,7 @@ int gcm_setkey(gcm_context *ctx, // pointer to caller-provided gcm context // encrypt the null 128-bit block to generate a key-based value // which is then used to initialize our GHASH lookup tables - if ((ret = aes_setkey(&ctx->aes_ctx, ENCRYPT, key, keysize)) != 0) + if ((ret = aes_setkey(&ctx->aes_ctx, MG_ENCRYPT, key, keysize)) != 0) return (ret); if ((ret = aes_cipher(&ctx->aes_ctx, h, h)) != 0) return (ret); @@ -718,7 +718,7 @@ int gcm_start(gcm_context *ctx, // pointer to user-provided GCM context ctx->add_len = 0; ctx->mode = mode; // set the GCM encryption/decryption mode - ctx->aes_ctx.mode = ENCRYPT; // GCM *always* runs AES in ENCRYPTION mode + ctx->aes_ctx.mode = MG_ENCRYPT; // GCM *always* runs AES in ENCRYPTION mode if (iv_len == 12) { // GCM natively uses a 12-byte, 96-bit IV memcpy(ctx->y, iv, iv_len); // copy the IV to the top of the 'y' buff @@ -789,7 +789,7 @@ int gcm_update(gcm_context *ctx, // pointer to user-provided GCM context if ((ret = aes_cipher(&ctx->aes_ctx, ctx->y, ectr)) != 0) return (ret); // encrypt or decrypt the input to the output - if (ctx->mode == ENCRYPT) { + if (ctx->mode == MG_ENCRYPT) { for (i = 0; i < use_len; i++) { // XOR the cipher's ouptut vector (ectr) with our input output[i] = (uchar) (ectr[i] ^ input[i]); @@ -927,7 +927,7 @@ int gcm_auth_decrypt( (which is an identical XORing to reverse the previous one) and also to re-generate the matching authentication tag */ - gcm_crypt_and_tag(ctx, DECRYPT, iv, iv_len, add, add_len, input, output, + gcm_crypt_and_tag(ctx, MG_DECRYPT, iv, iv_len, add, add_len, input, output, length, check_tag, tag_len); // now we verify the authentication tag in 'constant time' @@ -972,7 +972,7 @@ int aes_gcm_encrypt(unsigned char *output, // gcm_setkey(&ctx, key, (const uint) key_len); - ret = gcm_crypt_and_tag(&ctx, ENCRYPT, iv, iv_len, aead, aead_len, input, output, + ret = gcm_crypt_and_tag(&ctx, MG_ENCRYPT, iv, iv_len, aead, aead_len, input, output, input_length, tag, tag_len); gcm_zero_ctx(&ctx); @@ -992,7 +992,7 @@ int aes_gcm_decrypt(unsigned char *output, const unsigned char *input, gcm_setkey(&ctx, key, (const uint) key_len); - ret = gcm_crypt_and_tag(&ctx, DECRYPT, iv, iv_len, NULL, 0, input, output, + ret = gcm_crypt_and_tag(&ctx, MG_DECRYPT, iv, iv_len, NULL, 0, input, output, input_length, tag_buf, tag_len); gcm_zero_ctx(&ctx); diff --git a/src/tls_aes128.h b/src/tls_aes128.h index d44494a5..4f12fbed 100644 --- a/src/tls_aes128.h +++ b/src/tls_aes128.h @@ -27,8 +27,8 @@ #define AES_DECRYPTION 1 // whether AES decryption is supported /******************************************************************************/ -#define ENCRYPT 1 // specify whether we're encrypting -#define DECRYPT 0 // or decrypting +#define MG_ENCRYPT 1 // specify whether we're encrypting +#define MG_DECRYPT 0 // or decrypting #include "arch.h" @@ -146,7 +146,7 @@ int gcm_setkey(gcm_context *ctx, // caller-provided context ptr ******************************************************************************/ int gcm_crypt_and_tag( gcm_context *ctx, // gcm context with key already setup - int mode, // cipher direction: ENCRYPT (1) or DECRYPT (0) + int mode, // cipher direction: MG_ENCRYPT (1) or MG_DECRYPT (0) const uchar *iv, // pointer to the 12-byte initialization vector size_t iv_len, // byte length if the IV. should always be 12 const uchar *add, // pointer to the non-ciphered additional data @@ -191,7 +191,7 @@ int gcm_auth_decrypt( ******************************************************************************/ int gcm_start( gcm_context *ctx, // pointer to user-provided GCM context - int mode, // ENCRYPT (1) or DECRYPT (0) + int mode, // MG_ENCRYPT (1) or MG_DECRYPT (0) const uchar *iv, // pointer to initialization vector size_t iv_len, // IV length in bytes (should == 12) const uchar *add, // pointer to additional AEAD data (NULL if none)