// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package aes import "testing" // See const.go for overview of math here. // Test that powx is initialized correctly. // (Can adapt this code to generate it too.) func TestPowx(t *testing.T) { p := 1 for i := 0; i < len(powx); i++ { if powx[i] != byte(p) { t.Errorf("powx[%d] = %#x, want %#x", i, powx[i], p) } p <<= 1 if p&0x100 != 0 { p ^= poly } } } // Multiply b and c as GF(2) polynomials modulo poly func mul(b, c uint32) uint32 { i := b j := c s := uint32(0) for k := uint32(1); k < 0x100 && j != 0; k <<= 1 { // Invariant: k == 1<>8 } } } // Test that decryption tables are correct. // (Can adapt this code to generate them too.) func TestTd(t *testing.T) { for i := 0; i < 256; i++ { s := uint32(sbox1[i]) s9 := mul(s, 0x9) sb := mul(s, 0xb) sd := mul(s, 0xd) se := mul(s, 0xe) w := se<<24 | s9<<16 | sd<<8 | sb td := [][256]uint32{td0, td1, td2, td3} for j := 0; j < 4; j++ { if x := td[j][i]; x != w { t.Fatalf("td[%d][%d] = %#x, want %#x", j, i, x, w) } w = w<<24 | w>>8 } } }