Galois Multiplication

Finite field multiplication for AES.

by Trevor Dixon

HTML

<h3>Multiply 2</h3>
<table border="1" cellpadding="4" cellspacing="1" id="mult2">
	<tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr>
</table>

<h3>Multiply 3</h3>
<table border="1" cellpadding="4" cellspacing="1" id="mult3">
	<tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr>
</table>

<h3>Multiply 9</h3>
<table border="1" cellpadding="4" cellspacing="1" id="mult9">
	<tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr>
</table>

<h3>Multiply 11</h3>
<table border="1" cellpadding="4" cellspacing="1" id="mult11">
	<tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr>
</table>

<h3>Multiply 13</h3>
<table border="1" cellpadding="4" cellspacing="1" id="mult13">
	<tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr>
</table>

<h3>Multiply 14</h3>
<table border="1" cellpadding="4" cellspacing="1" id="mult14">
	<tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr>
</table>

CSS

table {
    margin-bottom: 30px;
}

JavaScript

function mult(a, b) {
    var answer = 0
    
    for (; b > 0; b = b >> 1) {
        if ((b & 1) > 0)
            answer ^= a;
        a = xtime(a);
    }
    
    return answer;
}

function xtime(n) {
    var bit8 = 1 << 8;
    n = n << 1;
    if ((n & bit8) != 0) {
        n ^= 0x1b;
    }
    return n & 0xff;
}

// Generate tables

[2, 3, 9, 11, 13, 14].forEach(function(m) {
    for (var i = 0; i < 256; i++) {
        var row = ~~(i/16);
        var text = hex(mult(i, m));
        $('#mult' + m + ' tr').eq(row).append($('<td/>').text(text));
    }
});

// Helper

function hex(n) {
    n = n.toString(16);
    if (n.length === 1)
        n = '0' + n;
    return '0x' + n;
}