Memory access fixes

Two fixes in one function to prevent read after bounds of a variable, and fixing wrong byte order in an IV for big endian architectures