Node.js Encrypted Lantronix XPort Comms

The Lantronix XPort is a popular embedded "device server" which is effectively an ethernet card for microcontrollers. It provides transparent ethernet connectivity to a serial device sonnected via a simple TTL USART.

There is a version of this device that supports 256-bit AES-CFB encryption and the protocol is quite simple, but the documentation is not easy to find and is marked confidential. It seems they'd prefer you use their binary libraries to mitigate support load, though they do have a FIPS certified implementation.

This code example shows how to make a connection to an XPort that has been configured for encryption with the same key. The XPort simply expects the first 16 bytes it receives on a fresh connection to be the IV it needs to setup decryption, then simply send CFB coded ciphertext. Done.

At the bottom is just a two way pipe to your console. It is a serial device after all.

'use strict';  
var crypto = require('crypto'),  
    duplexer = require('duplexer2'),
    net = require('net');

var key = new Buffer(  
  'A939980FAA5DD3B7D618737DEA100F5FAE23DEDE0D0D204C57BBAF9D42A48541',
  'hex'
);
var iv = crypto.randomBytes(16);

var cryptstream = crypto.createCipheriv('AES-256-CFB', key, iv);  
var decryptstream = crypto.createDecipheriv('AES-256-CFB', key, iv);

var socket = new net.Socket();

socket.connect({host: '192.168.11.107', port: 10001});

socket.on('connect', function() {  
  console.log('connected, sending iv');
  // Send IV
  socket.write(iv);

  // Pipe streams
  let duplex = duplexer(cryptstream, decryptstream);
  cryptstream.pipe(socket).pipe(decryptstream);

  process.stdin.pipe(duplex).pipe(process.stdout);
});