Browse Source

Fixed aes_gcm_128 function's refs to outdated openssl functions.
Commented out generate_rsa_keypair_hash function (not used yet) - RSA_get0_key was outdated.

dettanym 4 years ago
parent
commit
c50ea93e08
1 changed files with 68 additions and 78 deletions
  1. 68 78
      crypto.cpp

+ 68 - 78
crypto.cpp

@@ -96,6 +96,7 @@ uint32_t generate_rsa_keypair(FILE* fp, std::string& priv_key_str, std::string&
 
 uint32_t generate_rsa_keypair_hash(uint8_t* hash)
 {
+    /*
      uint32_t return_internal;
     const BIGNUM* n_internal_bigendian_struct;
     RSA_get0_key(rsa, &n_internal_bigendian_struct, NULL, NULL);
@@ -120,7 +121,8 @@ uint32_t generate_rsa_keypair_hash(uint8_t* hash)
 	printf("\n");
 	fflush(stdout); 
 	return return_internal;
-//   return 0; //length_bignum_le; 
+     */
+    return 0; //length_bignum_le;
 }
 
 void crypto_cleanup()
@@ -133,85 +135,73 @@ void crypto_cleanup()
 // Code adapted from here: https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption
 int aes_gcm_128(int enc, unsigned char *key, unsigned char *iv, unsigned char* plaintext, int plaintext_len, unsigned char *ciphertext,  uint32_t* op_ciphertext_len, unsigned char* tag)
 {
-	int len;
-	int ciphertext_len;
-	int reset_return;
-	if(ctx == NULL)
-	{
-		/* Create and initialise the context */
-		if(!(ctx = EVP_CIPHER_CTX_new())) { ERR_print_errors_fp(stderr); fflush(stderr);return 0x1; }
-	}
-
-	/* Initialise the encryption operation. */
-	if(1 != EVP_CipherInit_ex(ctx, EVP_aes_128_gcm(), NULL, key, iv, enc))
-	{
-		reset_return = EVP_CIPHER_CTX_reset(ctx);
-		ERR_print_errors_fp(stderr);
-		if(reset_return != 1)
-			return 0xf2;
-		return 0x2;
-	}
-	/* Provide the message to be encrypted, and obtain the encrypted output.
-	 * EVP_EncryptUpdate can be called multiple times if necessary
-	 */
-	if(1 != EVP_CipherUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
-	{
-                reset_return = EVP_CIPHER_CTX_reset(ctx);
-                ERR_print_errors_fp(stderr);
-	        if(1 != reset_return)
-			return 0xF3;
-		return 0x3;
-	}
-	ciphertext_len = len;
-
-	if(enc == 0)
+    int len;
+    int ciphertext_len;
+    if(ctx == NULL)
+    {
+        /* Create and initialise the context */
+        if(!(ctx = EVP_CIPHER_CTX_new())) {
+            ERR_print_errors_fp(stderr);
+            fflush(stderr);
+            return 0x1;
+        }
+    }
+
+    /* Initialise the encryption operation. */
+    if(1 != EVP_CipherInit_ex(ctx, EVP_aes_128_gcm(), NULL, key, iv, enc))
+    {
+        EVP_CIPHER_CTX_init(ctx);
+        ERR_print_errors_fp(stderr);
+        return 0x2;
+    }
+    /* Provide the message to be encrypted, and obtain the encrypted output.
+     * EVP_EncryptUpdate can be called multiple times if necessary
+     */
+    if(1 != EVP_CipherUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
+    {
+        EVP_CIPHER_CTX_init(ctx);
+        ERR_print_errors_fp(stderr);
+        return 0x3;
+    }
+    ciphertext_len = len;
+
+    if(enc == 0)
+    {
+        if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag))
         {
-                if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag))
-                {
-                       reset_return = EVP_CIPHER_CTX_reset(ctx);
-                       ERR_print_errors_fp(stderr); fflush(stderr);
-                       if(1 != reset_return)
-                                return 0xF5;
-                        return 0x5;
-                }
+            EVP_CIPHER_CTX_init(ctx);
+            ERR_print_errors_fp(stderr); fflush(stderr);
+            return 0x5;
         }
+    }
+
+
+    /* Finalise the encryption. Normally ciphertext bytes may be written at
+     * this stage, but this does not occur in GCM mode
+     */
+    // TODO: ^^^ Why the heck does it not occur in GCM mode ?
+    if(1 != EVP_CipherFinal_ex(ctx, ciphertext + len, &len))
+    {
+        EVP_CIPHER_CTX_init(ctx);
+        ERR_print_errors_fp(stderr); fflush(stderr);
+        return 0x4;
+    }
+    ciphertext_len += len;
+
+    /* Get the tag */
+    if(enc == 1)
+    {
+        if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag))
+        {
+            EVP_CIPHER_CTX_init(ctx);
+            ERR_print_errors_fp(stderr); fflush(stderr);
+            return 0x5;
+        }
+    }
 
+    /* Clean up */
+    EVP_CIPHER_CTX_init(ctx);
 
-	/* Finalise the encryption. Normally ciphertext bytes may be written at
-	 * this stage, but this does not occur in GCM mode
-	 */
-	// TODO: ^^^ Why the heck does it not occur in GCM mode ?
-	if(1 != EVP_CipherFinal_ex(ctx, ciphertext + len, &len))
-	{
-		reset_return = EVP_CIPHER_CTX_reset(ctx);
-		ERR_print_errors_fp(stderr); fflush(stderr);
-                if(1 != reset_return)
-                        return 0xF4;
-                return 0x4;
-	}
-	ciphertext_len += len;
-
-	/* Get the tag */
-	if(enc == 1)
-	{
-		if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag))
-		{
-                	reset_return = EVP_CIPHER_CTX_reset(ctx);
-                	ERR_print_errors_fp(stderr); fflush(stderr);
-                	if(1 != reset_return)
-                        	return 0xF5;
-	                return 0x5;
-		}
-	}
-
-	/* Clean up */
-	if(1 != EVP_CIPHER_CTX_reset(ctx))
-	{
-		ERR_print_errors_fp(stderr); fflush(stderr);
-		return 0xF0;
-	}
-
-	*op_ciphertext_len=ciphertext_len;
-	return 0;
+    *op_ciphertext_len=ciphertext_len;
+    return 0;
 }
-