package gcltests

import (
	"bytes"

	"xdx.jelly/xgcl/sm/sm4"
)

func SM4MacTest() bool {
	key := hexDecode("EEE8A0136E9DBC99F09E29EB5C79DAC1")
	data := hexDecode("27B16EAF7D0341A1449A14A3D1BD69F9")
	iv := hexDecode("6071A1EB4C784230A037909D33290854")
	stdMac := hexDecode("F5CAF5A089C06942F83D1B9BB212F8AF")

	mac, err := sm4.Mac(key, iv, data)
	if err != nil {
		return false
	}
	return bytes.Equal(mac, stdMac)
}

func SM4EncECB() bool {
	key := hexDecode("C35242CC90CB75935A536F32149F5C35")
	data := hexDecode("ECB57EE6D15BD06632CFE9FD09B822AF")
	stdCipher := hexDecode("03D8C466A7C245971925E35540E9209F")
	cipher, err := sm4.EncryptECB(nil, key, data)
	if err != nil {
		return false
	}
	return bytes.Equal(cipher, stdCipher)
}

func SM4DecECB() bool {
	key := hexDecode("C35242CC90CB75935A536F32149F5C35")
	stdData := hexDecode("ECB57EE6D15BD06632CFE9FD09B822AF")
	cipher := hexDecode("03D8C466A7C245971925E35540E9209F")

	data, err := sm4.DecryptECB(nil, key, cipher)
	if err != nil {
		return false
	}
	return bytes.Equal(data, stdData)
}

func SM4EncCBC() bool {
	key := hexDecode("99CF573EFE8BE7F3277E800FF793F217")
	iv := hexDecode("57C1AD66EFD690F45785A0176C42211B")

	data := hexDecode("5B3ADAFFDC04A435D8582B5E04D22CB2920B85FBE0655E2C62DBF15EE6D9D2546D05CA85C78BF757AF9B10C3699879160E52A6060A9F910AA05CFA3F4A58B64D")
	stdCipher := hexDecode("6A4320D8EC4A544116F5AD9C50EB33AC61A472F451AF2496E7C1761734907AC3A7D474CE21CFA8E64D5CB2D92659FCB4A7F8F5D5DAAFAEA0E3C6078176EB6DD3")
	cipher, err := sm4.EncryptCBC(nil, iv, key, data)
	if err != nil {
		return false
	}
	return bytes.Equal(cipher, stdCipher)
}

func SM4DecCBC() bool {
	key := hexDecode("99CF573EFE8BE7F3277E800FF793F217")
	iv := hexDecode("57C1AD66EFD690F45785A0176C42211B")

	stdData := hexDecode("5B3ADAFFDC04A435D8582B5E04D22CB2920B85FBE0655E2C62DBF15EE6D9D2546D05CA85C78BF757AF9B10C3699879160E52A6060A9F910AA05CFA3F4A58B64D")
	cipher := hexDecode("6A4320D8EC4A544116F5AD9C50EB33AC61A472F451AF2496E7C1761734907AC3A7D474CE21CFA8E64D5CB2D92659FCB4A7F8F5D5DAAFAEA0E3C6078176EB6DD3")

	data, err := sm4.DecryptCBC(nil, iv, key, cipher)
	if err != nil {
		return false
	}
	return bytes.Equal(data, stdData)
}
