Implementing XOR-MAPPED-ADDRESS attribute on STUN server

1.4k views Asked by At

From RFC 5389 Section 15.2:

If the IP address family is IPv4, X-Address is computed by taking the mapped IP address in host byte order, XOR'ing it with the magic cookie, and converting the result to network byte order. If the IP address family is IPv6, X-Address is computed by taking the mapped IP address in host byte order, XOR'ing it with the concatenation of the magic cookie and the 96-bit transaction ID, and converting the result to network byte order.

I'm writing a STUN server in Node.JS and I'm trying to understand how one would go about XOR-ing a 128-bit value. I feel as though it would involve using one of these functions from the Buffer module, though it says it only supports up to 48 bits. Any advice on how to implement a 128-bit XOR operator for an IPv6 address?

1

There are 1 answers

2
SomeKittens On BEST ANSWER

Here's my XOR operator from my CryptoPals code:

var xor = function (b0, b1) {
  if (Buffer.isBuffer(b0)) {
    b0 = new Buffer(b0);
  }
  if (Buffer.isBuffer(b1)) {
    b1 = new Buffer(b1);
  }

  if (b0.length !== b1.length) {
    console.log(b0.length, b1.length);
    throw new Error('Tried to xor two buffers of differing length');
  }

  var arr = [];

  for (var i = 0; i < b0.length; i++) {
    arr.push(b0[i] ^ b1[i]);
  }

  return new Buffer(arr);
};