AES CBC & Gpios usage

Hi all. I’ve been trying to implement the code below using AESLib in cbc mode on an Arduino Mega. However, whenever I am using GPIOs the function aes128_cbc_enc does not complete; aes128_enc_single works fine with GPIOs enabled. Any insight is appreciated. Thanks in advance.

#include <AESLib.h>
#define RANDPIN A3


unsigned long sendcount = 0;

uint8_t key[] = {
  0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  
uint8_t iv[] = {
  0,1,2,3,4, 5,6,7,8, 9,10,11,12, 13,14,15};

void setup(){
  Serial.begin(115200);
  //pinMode(2, OUTPUT);
  //pinMode(3, OUTPUT);
  //pinMode(4, OUTPUT);
  delay(1000);
}

void loop(){
  char data[] = "Testing AES Comm";

  //digitalWrite(2, HIGH);
  //digitalWrite(4, LOW);
  
/*
  unsigned long t = micros();
  int a = analogRead(RANDPIN);
  long r = random(0x8fff);
  sendcount++;
  memcpy(iv, &sendcount, 4);
  memcpy(iv+6, &t, 4);
  memcpy(iv+10, &a, 2);
  memcpy(iv+12, &r, 4);
*/

  aes128_cbc_enc(key, iv, data, 16);

  Serial.write(iv,16);
  Serial.flush();
  Serial.write(data,16);
  Serial.flush();

  delay(3000);

}

I eventually got this working by directly accessing the registers.

  DDRE = (1<<DDE4)|(1<<DDE5);
  PORTE = (1<<DDE4);
  DDRG = (1<<DDE5);
  PORTG = (0<<DDE5);

However I would still like to know why pinmode didn’t work…