github.com/emmansun/gmsm@v0.29.1/cipher/xts_sm4_test.go (about)

     1  package cipher_test
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"testing"
     7  
     8  	"github.com/emmansun/gmsm/cipher"
     9  	"github.com/emmansun/gmsm/sm4"
    10  )
    11  
    12  var xtsTestVectors = []struct {
    13  	key        string
    14  	sector     uint64
    15  	plaintext  string
    16  	ciphertext string
    17  }{
    18  	{ // XTS-SM4-128 applied for a data unit of 32 bytes
    19  		"0000000000000000000000000000000000000000000000000000000000000000",
    20  		0,
    21  		"0000000000000000000000000000000000000000000000000000000000000000",
    22  		"d9b421f731c894fdc35b77291fe4e3b02a1fb76698d59f0e51376c4ada5bc75d",
    23  	}, {
    24  		"1111111111111111111111111111111122222222222222222222222222222222",
    25  		0x3333333333,
    26  		"4444444444444444444444444444444444444444444444444444444444444444",
    27  		"a74d726c11196a32be04e001ff29d0c7932f9f3ec29bfcb64dd17f63cbd3ea31",
    28  	}, {
    29  		"fffefdfcfbfaf9f8f7f6f5f4f3f2f1f022222222222222222222222222222222",
    30  		0x3333333333,
    31  		"4444444444444444444444444444444444444444444444444444444444444444",
    32  		"7f76088effadf70c02ea9f95da0628d351bfcb9eac0563bcf17b710dab0a9826",
    33  	}, { // XTS-SM4-128 applied for a data unit of 512 bytes
    34  		"2718281828459045235360287471352631415926535897932384626433832795",
    35  		0,
    36  		"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
    37  		"54dd65b6326faea8fad1a83c63614af39f721d8dfe177a30b66abf6a449980e1cdbe06afb73336f37a4d39de964a30d7d04a3799169c60258f6b748a61861aa5ec92a2c15b2b7c615a42aba499bbd6b71db9c789b2182089a25dd3df800ed1864d19f7ed45fd17a9480b0fb82d9b7fc3ed57e9a1140eaa778dd2dd679e3edc3dc4d55c950ebc531d9592f7c4638256d56518292a20af98fdd3a63600350a70ab5a40f4c285037ca01f251f19ecae0329ff77ad88cd5a4cdea2aeabc22148ffbd239bd10515bde1131dec8404e443dc763140d5f22bf33e0c6872d6b81d630f6f00cdd058fe80f9cbfb77707f93cee2ca92b915b8304027c190a84e2d65e018cc6a387d3766acdb28253284e8db9acf8f52280ddc6d0033d2ccaaa4f9aeff123669bc024fd6768edf8bc1f8d622c19c609ef97f609190cd110241e7fb084ed8942da1f9b9cf1b514b61a388b30ea61a4a745b381ee7ad6c4db1275453b8413f98df6e4a40986ee4b59af5dfaecd301265179067a00d7ca35ab95abd617adea28ec1c26a97de28b8bfe30120d6aefbd258c59e42d161e8065a78106bdca5cd90fb3aac4e93866c8a7f9676860a79145bd92e02e819a90be0b97cc522b32106856fdf0e54d88e4624155a2f1c14eaeaa163f858e99a806e791acd82f1b0e29f0028a4c38e976f571a93f4fd57d787c24db0e01ca304e5a5c4dd50cf8bdbf491e57c",
    38  	}, { // Vector 5
    39  		"2718281828459045235360287471352631415926535897932384626433832795",
    40  		1,
    41  		"27a7479befa1d476489f308cd4cfa6e2a96e4bbe3208ff25287dd3819616e89cc78cf7f5e543445f8333d8fa7f56000005279fa5d8b5e4ad40e736ddb4d35412328063fd2aab53e5ea1e0a9f332500a5df9487d07a5c92cc512c8866c7e860ce93fdf166a24912b422976146ae20ce846bb7dc9ba94a767aaef20c0d61ad02655ea92dc4c4e41a8952c651d33174be51a10c421110e6d81588ede82103a252d8a750e8768defffed9122810aaeb99f9172af82b604dc4b8e51bcb08235a6f4341332e4ca60482a4ba1a03b3e65008fc5da76b70bf1690db4eae29c5f1badd03c5ccf2a55d705ddcd86d449511ceb7ec30bf12b1fa35b913f9f747a8afd1b130e94bff94effd01a91735ca1726acd0b197c4e5b03393697e126826fb6bbde8ecc1e08298516e2c9ed03ff3c1b7860f6de76d4cecd94c8119855ef5297ca67e9f3e7ff72b1e99785ca0a7e7720c5b36dc6d72cac9574c8cbbc2f801e23e56fd344b07f22154beba0f08ce8891e643ed995c94d9a69c9f1b5f499027a78572aeebd74d20cc39881c213ee770b1010e4bea718846977ae119f7a023ab58cca0ad752afe656bb3c17256a9f6e9bf19fdd5a38fc82bbe872c5539edb609ef4f79c203ebb140f2e583cb2ad15b4aa5b655016a8449277dbd477ef2c8d6c017db738b18deb4a427d1923ce3ff262735779a418f20a282df920147beabe421ee5319d0568",
    42  		"0e36ba273dd121afc77e0d8c00aa4a662b21f363470d333f2fe2ddcbcc51ecd523022f5fa7970062800cd3859cacead369263681543db431f3844a3638e837cf025cecc3b778e14ac1fd02bb684d0e3cc3d05758cf4b3827bae92f9f09a45487e0a830154a4206a14c4077bcc928e6039b78cdf8f915236c5a4efc21a0ba7173232cef6f18f8b53be5e1eb37282bed31a24f322cf1bba02dfd2583ce216a73726915116fd8ce46d58aa562b5a5d88076792d6e35cba40552db6a19776eaf255c3fc927adc41cb83a83884f98176267f37e543ce34fa32960d1d05aa05ff04103037a730175f1d59a32b64f308925fc9fa9c60421b4ab438e14504227cba20c8c06b508554fb02e52b92a1cd0a8e386511bc4c2fb62998d0ac5d9e7614080a10039b8cddf24a644b3e0aa02bb5d6c0897a84bfe0d12690cbd9fb92fd39b5b9504deeeaab0c5b9839b6283b87abe6439d28f0afb0508104fd4db9fd6e0301c6a488e76fd2a4801d2b7df57e0179506e9a8dbd7312be3922ea4e7339227061485452296dabc3b0f178a2e4ba012bbb6e836dec5d25abaa0f399ca622c5f075dfae7b2ffef4e396cd74b9bc3aeb7c212a5fd5c42b73fcf92e1f4ca458bb50e7257c4ffea253f30f7eaf9a6762ce15177f55ba250a4293d6ecdbd2e9a80c942b38dbdbd74773245a7a7db6b91d1f6c74bd32b7a7a193a2d260d266b64dd19b959ae42",
    43  	}, { // XTS-SM4-128 applied for a data unit that is not a multiple of 16 bytes, but should be a complte byte
    44  		"c46acc2e7e013cb71cdbf750cf76b000249fbf4fb6cd17607773c23ffa2c4330",
    45  		94,
    46  		"7e9c2289cba460e470222953439cdaa892a5433d4dab2a3f67",
    47  		"c3cf5445c64aa518f4abce2848faddfb4605d9fb66f1f12c0c",
    48  	}, {
    49  		"56ffcc9bbbdf413f0fc0f888f44b7493bb1925a39b8adf02d9009bb16db0a887",
    50  		144,
    51  		"9a839cc14363bafcfc0cc93b14f8e769d35b94cc98267438e3",
    52  		"af027012c829206c32a31706999d046f10a83bcacbc5c96353",
    53  	},
    54  	{
    55  		"7454a43b87b1cf0dec95032c22873be3cace3bb795568854c1a008c07c5813f3",
    56  		108,
    57  		"41088fa15195b2733fe824d2c1fdc8306080863945fb2a73cf",
    58  		"614ee9311a53791889338eb2f66fedff7dc15126349bed1465",
    59  	},
    60  }
    61  
    62  func fromHex(s string) []byte {
    63  	ret, err := hex.DecodeString(s)
    64  	if err != nil {
    65  		panic("xts: invalid hex in test")
    66  	}
    67  	return ret
    68  }
    69  
    70  func TestXTS(t *testing.T) {
    71  	for i, test := range xtsTestVectors {
    72  		key := fromHex(test.key)
    73  
    74  		encrypter, err := cipher.NewXTSEncrypterWithSector(sm4.NewCipher, key[:len(key)/2], key[len(key)/2:], test.sector)
    75  		if err != nil {
    76  			t.Errorf("#%d: failed to create encrypter: %s", i, err)
    77  			continue
    78  		}
    79  		decrypter, err := cipher.NewXTSDecrypterWithSector(sm4.NewCipher, key[:len(key)/2], key[len(key)/2:], test.sector)
    80  		if err != nil {
    81  			t.Errorf("#%d: failed to create decrypter: %s", i, err)
    82  			continue
    83  		}
    84  		plaintext := fromHex(test.plaintext)
    85  		ciphertext := make([]byte, len(plaintext))
    86  
    87  		encrypter.CryptBlocks(ciphertext, plaintext)
    88  		expectedCiphertext := fromHex(test.ciphertext)
    89  		if !bytes.Equal(ciphertext, expectedCiphertext) {
    90  			t.Errorf("#%d: encrypted failed, got: %x, want: %x", i, ciphertext, expectedCiphertext)
    91  			continue
    92  		}
    93  
    94  		decrypted := make([]byte, len(ciphertext))
    95  		decrypter.CryptBlocks(decrypted, ciphertext)
    96  		if !bytes.Equal(decrypted, plaintext) {
    97  			t.Errorf("#%d: decryption failed, got: %x, want: %x", i, decrypted, plaintext)
    98  		}
    99  	}
   100  }
   101  
   102  // Test data is from GB/T 17964-2021 B.7
   103  var xtsGBTestVectors = []struct {
   104  	key        string
   105  	tweak      string
   106  	plaintext  string
   107  	ciphertext string
   108  }{
   109  	{
   110  		"2B7E151628AED2A6ABF7158809CF4F3C000102030405060708090A0B0C0D0E0F",
   111  		"F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF",
   112  		"6BC1BEE22E409F96E93D7E117393172AAE2D8A571E03AC9C9EB76FAC45AF8E5130C81C46A35CE411E5FBC1191A0A52EFF69F2445DF4F9B17",
   113  		"E9538251C71D7B80BBE4483FEF497BD12C5C581BD6242FC51E08964FB4F60FDB0BA42F63499279213D318D2C11F6886E903BE7F93A1B3479",
   114  	},
   115  }
   116  
   117  func TestGBXTSSample(t *testing.T) {
   118  	for i, test := range xtsGBTestVectors {
   119  		key := fromHex(test.key)
   120  		tweak := fromHex(test.tweak)
   121  		encrypter, err := cipher.NewGBXTSEncrypter(sm4.NewCipher, key[:len(key)/2], key[len(key)/2:], tweak)
   122  		if err != nil {
   123  			t.Errorf("#%d: failed to create encrypter: %s", i, err)
   124  			continue
   125  		}
   126  		decrypter, err := cipher.NewGBXTSDecrypter(sm4.NewCipher, key[:len(key)/2], key[len(key)/2:], tweak)
   127  		if err != nil {
   128  			t.Errorf("#%d: failed to create decrypter: %s", i, err)
   129  			continue
   130  		}
   131  		plaintext := fromHex(test.plaintext)
   132  		ciphertext := make([]byte, len(plaintext))
   133  
   134  		encrypter.CryptBlocks(ciphertext, plaintext)
   135  		expectedCiphertext := fromHex(test.ciphertext)
   136  		if !bytes.Equal(ciphertext, expectedCiphertext) {
   137  			t.Errorf("#%d: encrypted failed, got: %x, want: %x", i, ciphertext, expectedCiphertext)
   138  			continue
   139  		}
   140  
   141  		decrypted := make([]byte, len(ciphertext))
   142  		decrypter.CryptBlocks(decrypted, ciphertext)
   143  		if !bytes.Equal(decrypted, plaintext) {
   144  			t.Errorf("#%d: decryption failed, got: %x, want: %x", i, decrypted, plaintext)
   145  		}
   146  	}
   147  }
   148  
   149  var gbXtsTestVectors = []struct {
   150  	key        string
   151  	sector     uint64
   152  	plaintext  string
   153  	ciphertext string
   154  }{
   155  	{ // XTS-SM4-128 applied for a data unit of 32 bytes
   156  		"0000000000000000000000000000000000000000000000000000000000000000",
   157  		0,
   158  		"0000000000000000000000000000000000000000000000000000000000000000",
   159  		"d9b421f731c894fdc35b77291fe4e3b0e58e55e613a862b4d2b0f1073b4b4fd0",
   160  	}, {
   161  		"1111111111111111111111111111111122222222222222222222222222222222",
   162  		0x3333333333,
   163  		"4444444444444444444444444444444444444444444444444444444444444444",
   164  		"a74d726c11196a32be04e001ff29d0c7724feef81d666ae5afdfe4649544fcf5",
   165  	}, {
   166  		"fffefdfcfbfaf9f8f7f6f5f4f3f2f1f022222222222222222222222222222222",
   167  		0x3333333333,
   168  		"4444444444444444444444444444444444444444444444444444444444444444",
   169  		"7f76088effadf70c02ea9f95da0628d3ef2d6a77004beaa9016001d6789dd5a0",
   170  	}, { // XTS-SM4-128 applied for a data unit of 512 bytes
   171  		"2718281828459045235360287471352631415926535897932384626433832795",
   172  		0,
   173  		"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
   174  		"54dd65b6326faea8fad1a83c63614af398bddb6824735dab93ec4e75734215d463f67daf53742fb2a2847d5fde3984f8882cfd5fa9d6642e1e871c155202440044251465211628ba86f8d2998387a685edde23c07610b7388aab17f205aa5dada33c0a8a4225bc114254c796f800c638e016d199cd21dc28e92dc2b8587545509a8e1d659c596d3f6c8c225a27bdb2b02fe5a0c0183a592b396d32765fe733afb438a6ffb305ae1377c56d872badcebbd37812ff79f0571b3f977537570a1f76b9a50c49ab8d867fa024ea4483a25f7947b07885bb839e777abe76af11adf3108d1195933f96b7949b0664bdb89beb3bc48fb5f5d2109d32332f17c9a6ddea55441d1bbf43280ec7e75791e234d651a0716209eb21ae06061e33a72b0c530cb15fe0b55016b188dad75c4c50232dce1f5df61911c79bee60397b64bb914c0f26efcfb6ffab2bb33bdfd8db98c44debbd4ca865d41cbe1d0801b01aba2603cbea599b32c836789deeb9a3c18f3cae977b42ec81f1dfef6e098dd9e9dd6c1822bb938b08641bb72461f8d38c1724a43ae1254b9223e2270cf9f7d71a6bf093df2079fd2cc2fe87e846d799de30483f80164c31e65d8aae5f72d6dc71118932a008df547c712bee45ddebcdce098d673ef5ede91edfd45d17cb90963d3e2e2e2508a376a7b1af4d69e756ea5df52ac440791d57d56b5e057ad00e077d2df5009416",
   175  	}, { // Vector 5
   176  		"2718281828459045235360287471352631415926535897932384626433832795",
   177  		1,
   178  		"27a7479befa1d476489f308cd4cfa6e2a96e4bbe3208ff25287dd3819616e89cc78cf7f5e543445f8333d8fa7f56000005279fa5d8b5e4ad40e736ddb4d35412328063fd2aab53e5ea1e0a9f332500a5df9487d07a5c92cc512c8866c7e860ce93fdf166a24912b422976146ae20ce846bb7dc9ba94a767aaef20c0d61ad02655ea92dc4c4e41a8952c651d33174be51a10c421110e6d81588ede82103a252d8a750e8768defffed9122810aaeb99f9172af82b604dc4b8e51bcb08235a6f4341332e4ca60482a4ba1a03b3e65008fc5da76b70bf1690db4eae29c5f1badd03c5ccf2a55d705ddcd86d449511ceb7ec30bf12b1fa35b913f9f747a8afd1b130e94bff94effd01a91735ca1726acd0b197c4e5b03393697e126826fb6bbde8ecc1e08298516e2c9ed03ff3c1b7860f6de76d4cecd94c8119855ef5297ca67e9f3e7ff72b1e99785ca0a7e7720c5b36dc6d72cac9574c8cbbc2f801e23e56fd344b07f22154beba0f08ce8891e643ed995c94d9a69c9f1b5f499027a78572aeebd74d20cc39881c213ee770b1010e4bea718846977ae119f7a023ab58cca0ad752afe656bb3c17256a9f6e9bf19fdd5a38fc82bbe872c5539edb609ef4f79c203ebb140f2e583cb2ad15b4aa5b655016a8449277dbd477ef2c8d6c017db738b18deb4a427d1923ce3ff262735779a418f20a282df920147beabe421ee5319d0568",
   179  		"0e36ba273dd121afc77e0d8c00aa4a665f801f3607af61b61058b2f5d007310822200eaaeef759d515ebd032dad4235f5cd2dc735b57b56e003bce3f56890618877db69aa4519edcf681c6fc19c9c4a5655372d1549148c759efba00140275b46b6a5f6522de1702c48ff209a1dd7d1f56e775252796a09c20f903bfb3935bc79c0cdcaa9d2f30e616160e0662fb35311676e86e18d7d90d4203bc6862a9187b8657162143ce914750a86f984cf660311917e00fcf450ee188f088b4222522276bf3391e94de4fdad4134dfc7d08113c65e1b103bd3ad75fb13bba7f842451f9023ed21f1d23bc1c57d593932e021548bbff61ea9a24f359b4f7a8f2a998587495b726411f84734b189f65c4e79f09c7875f9c924b32e5bf2785a9935854e08ce86f5a4a399af6731099a13e10db0b32b888865b4416d69014a8cdb28b3912ec0b832835df7b59637d0687747815ba7cf9efae862dd6e80763acb50898fe1b3ba13a39d81b20d6d50613fbb5fbdcae2a7a87b9377eec455a8bae5102d5e6a7bea9b6b77d3f9895b277a55a524721cd0e59ce35e915de622480c5e0d31d153282dd832278fd2b795933f5dc591c17bd6d7f38fcd6afce551e8485109673881519d2845395ce9ceaea6306e38a73f9bb990931323a3136d18ee76c3e727cfb07cf386519313e1c44adcc50ae79bfac6952e3b98948206fb3dc3ebaed556bf27f16",
   180  	}, { // XTS-SM4-128 applied for a data unit that is not a multiple of 16 bytes, but should be a complte byte
   181  		"c46acc2e7e013cb71cdbf750cf76b000249fbf4fb6cd17607773c23ffa2c4330",
   182  		94,
   183  		"7e9c2289cba460e470222953439cdaa892a5433d4dab2a3f67",
   184  		"4d5501ea41cf6b6532b4b7129c6f6ee74605d9fb66f1f12c0c",
   185  	}, {
   186  		"56ffcc9bbbdf413f0fc0f888f44b7493bb1925a39b8adf02d9009bb16db0a887",
   187  		144,
   188  		"9a839cc14363bafcfc0cc93b14f8e769d35b94cc98267438e3",
   189  		"f04f3f16b354cccdc39fc664ec7f8db010a83bcacbc5c96353",
   190  	},
   191  	{
   192  		"7454a43b87b1cf0dec95032c22873be3cace3bb795568854c1a008c07c5813f3",
   193  		108,
   194  		"41088fa15195b2733fe824d2c1fdc8306080863945fb2a73cf",
   195  		"791a9469ed5a22d8195ac37c43c1b0377dc15126349bed1465",
   196  	},
   197  }
   198  
   199  func TestGBXTS(t *testing.T) {
   200  	for i, test := range gbXtsTestVectors {
   201  		key := fromHex(test.key)
   202  
   203  		encrypter, err := cipher.NewGBXTSEncrypterWithSector(sm4.NewCipher, key[:len(key)/2], key[len(key)/2:], test.sector)
   204  		if err != nil {
   205  			t.Errorf("#%d: failed to create encrypter: %s", i, err)
   206  			continue
   207  		}
   208  		decrypter, err := cipher.NewGBXTSDecrypterWithSector(sm4.NewCipher, key[:len(key)/2], key[len(key)/2:], test.sector)
   209  		if err != nil {
   210  			t.Errorf("#%d: failed to create decrypter: %s", i, err)
   211  			continue
   212  		}
   213  		plaintext := fromHex(test.plaintext)
   214  		ciphertext := make([]byte, len(plaintext))
   215  
   216  		encrypter.CryptBlocks(ciphertext, plaintext)
   217  		expectedCiphertext := fromHex(test.ciphertext)
   218  		if !bytes.Equal(ciphertext, expectedCiphertext) {
   219  			t.Errorf("#%d: encrypted failed, got: %x, want: %x", i, ciphertext, expectedCiphertext)
   220  			continue
   221  		}
   222  
   223  		decrypted := make([]byte, len(ciphertext))
   224  		decrypter.CryptBlocks(decrypted, ciphertext)
   225  		if !bytes.Equal(decrypted, plaintext) {
   226  			t.Errorf("#%d: decryption failed, got: %x, want: %x", i, decrypted, plaintext)
   227  		}
   228  	}
   229  }