// 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 implements AES encryption (formerly Rijndael), as defined in // U.S. Federal Information Processing Standards Publication 197. // // The AES operations in this package are not implemented using constant-time algorithms. // An exception is when running on systems with enabled hardware support for AES // that makes these operations constant-time. Examples include amd64 systems using AES-NI // extensions and s390x systems using Message-Security-Assist extensions. // On such systems, when the result of NewCipher is passed to cipher.NewGCM, // the GHASH operation used by GCM is also constant-time. package aes import ( "bytes" "math/bits" ) // This file generates AES constants - 8720 bytes of initialized data. // https://csrc.nist.gov/publications/fips/fips197/fips-197.pdf // AES is based on the mathematical behavior of binary polynomials // (polynomials over GF(2)) modulo the irreducible polynomial x⁸ + x⁴ + x³ + x + 1. // Addition of these binary polynomials corresponds to binary xor. // Reducing mod poly corresponds to binary xor with poly every // time a 0x100 bit appears. const poly = 1<<8 | 1<<4 | 1<<3 | 1<<1 | 1<<0 // x⁸ + x⁴ + x³ + x + 1 // 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 } } return te }() // decLut Lookup tables for decryption. var decLut = func() (td [4][256]uint32) { 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 for j := 0; j < 4; j++ { td[j][i] = bits.ReverseBytes32(w) w = w<<24 | w>>8 } } return td }()