示例#1
0
func testConstruction() (*chow.Construction, []byte) {
	key := test_vectors.AESVectors[50].Key
	seed := test_vectors.AESVectors[51].Key
	constr, _, _ := chow.GenerateEncryptionKeys(key, seed, common.SameMasks(common.IdentityMask))

	return &constr, key
}
示例#2
0
func BenchmarkShiftRows(b *testing.B) {
	constr, _, _ := GenerateEncryptionKeys(key, seed, common.SameMasks(common.IdentityMask))

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		constr.ShiftRows(input)
	}
}
示例#3
0
func TestShiftRows(t *testing.T) {
	in := []byte{99, 202, 183, 4, 9, 83, 208, 81, 205, 96, 224, 231, 186, 112, 225, 140}
	out := []byte{99, 83, 224, 140, 9, 96, 225, 4, 205, 112, 183, 81, 186, 202, 208, 231}

	constr, _, _ := GenerateEncryptionKeys(key, key, common.SameMasks(common.IdentityMask))
	constr.shiftRows(in)

	if !bytes.Equal(out, in) {
		t.Fatalf("Real disagrees with result! %x != %x", out, in)
	}
}
示例#4
0
func TestUnmaskedEncrypt(t *testing.T) {
	cand, real := make([]byte, 16), make([]byte, 16)

	// Calculate the candidate output.
	constr, _, _ := GenerateEncryptionKeys(key, seed, common.SameMasks(common.IdentityMask))
	constr.Encrypt(cand, input)

	// Calculate the real output.
	c, _ := aes.NewCipher(key)
	c.Encrypt(real, input)

	if !bytes.Equal(real, cand) {
		t.Fatalf("Real disagrees with result! %x != %x", real, cand)
	}
}
示例#5
0
func BenchmarkExpandWord(b *testing.B) {
	constr1, _, _ := GenerateEncryptionKeys(key, seed, common.SameMasks(common.IdentityMask))

	serialized := constr1.Serialize()
	constr2, _ := Parse(serialized)

	dst := make([]byte, 16)
	copy(dst, input)

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		constr2.ExpandWord(constr2.TBoxTyiTable[0][0:4], dst[0:4])
	}
}
示例#6
0
func main() {
	flag.Parse()

	// Generate AES key and seed for the white-box generator.
	key, seed := make([]byte, 16), make([]byte, 16)
	rand.Read(key)
	rand.Read(seed)

	fmt.Printf("Key: %x\n", key)

	// Create a white-box version of the above key.
	constr, _, _ := chow.GenerateEncryptionKeys(key, seed, common.SameMasks(common.IdentityMask))
	keyData := constr.Serialize()

	ioutil.WriteFile(*out, keyData, os.ModePerm)
}
示例#7
0
func ExtractKey(cand ast.Expr) ([]byte, bool) {
	// Verify expression contains a byte slice.
	compositeLit, ok := cand.(*ast.CompositeLit)
	if !ok {
		return nil, false
	}

	arrayType, ok := compositeLit.Type.(*ast.ArrayType)
	if !ok {
		return nil, false
	}

	elt, ok := arrayType.Elt.(*ast.Ident)
	if !ok || elt.Name != "byte" || elt.Obj != nil {
		return nil, false
	}

	// Extract raw key.
	key := []byte{}

	for _, e := range compositeLit.Elts {
		basicLit, ok := e.(*ast.BasicLit)
		if !ok || basicLit.Kind != token.INT {
			return nil, false
		}

		num, err := strconv.ParseUint(basicLit.Value, 0, 8)
		if err != nil {
			return nil, false
		}

		key = append(key, byte(num))
	}

	if len(key) != 16 {
		return nil, false
	}

	// Mask key.
	seed := make([]byte, 16)
	rand.Read(seed)

	constr, _, _ := chow.GenerateEncryptionKeys(key, seed, common.SameMasks(common.IdentityMask))

	return constr.Serialize(), true
}