Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Best way to XOR two 128 bit values in MSVC?

I’m trying to XOR the 128 bit Initialization Vector with the Plaintext as seen here

aes-cbc

In linux x86-64 gcc 12.2, there’s a one liner

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

*(unsigned __int128 *)( plaintext ) ^= *(unsigned __int128 *)( ivvectext );

For example, https://godbolt.org/z/sc8e66qeo

#include <stdio.h>
#include <stdint.h>

int main()
{
    uint8_t plaintext[16] = {'t','h','e','q','u','i','c','k','b','r','o','w','n','f','o','x'};
    uint8_t ivvectext[16] = {'w','a','1','2','o','b','s','q','m','v','c','s','s','q','u','w'};

    *(unsigned __int128 *)( plaintext ) ^= *(unsigned __int128 *)( ivvectext );

    for (int i = 0; i < sizeof(plaintext); i++) { printf("%02X ", (unsigned char)plaintext[i]); }

    return 0;
}

Question

In MSVC, what’s the preferred method to XOR these 128 bit values?

>Solution :

As mentioned in my comment I would use intrincis: Here is how to do it in MSVC:

#include <immintrin.h>
int main() {

    uint8_t plaintext[16] = { 't','h','e','q','u','i','c','k','b','r','o','w','n','f','o','x' };
    uint8_t ivvectext[16] = { 'w','a','1','2','o','b','s','q','m','v','c','s','s','q','u','w' };

    __m128i plain = _mm_loadu_si128((__m128i*)plaintext);
    __m128i ivvec = _mm_loadu_si128((__m128i*)ivvectext);

    __m128i xored = _mm_xor_si128(plain, ivvec);
    
    return 0;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading