|
7 | 7 | package cipher |
8 | 8 |
|
9 | 9 | import ( |
10 | | - "fmt" |
| 10 | + "bytes" |
| 11 | + "encoding/hex" |
| 12 | + "runtime" |
11 | 13 | "testing" |
12 | 14 | ) |
13 | 15 |
|
14 | 16 | func TestPolyvalTableInitAsm(t *testing.T) { |
15 | 17 | if !supportPolyvalAsm { |
16 | 18 | t.Skip("skipping test on unsupported CPU") |
17 | 19 | } |
| 20 | + amd64Expected, _ := hex.DecodeString("87f00e25c6685b1a4e7a1d632f79d284c98a1346e911899ec98a1346e911899e298c3094abe125e631960b1bc5d961ec181a3b8f6e38440a181a3b8f6e38440a92ba923b49631f0c7e4b4bf6c8450857ecf1d9cd8126175becf1d9cd8126175bfc0e6f7ae0c1510c5cece7069f5f571ea0e2887c7f9e0612a0e2887c7f9e0612ee237ddddd694634da80497ef9cab6fe34a334a324a3f0ca34a334a324a3f0ca34ee209446f97afb6acda9f97b6c7f3e5e23896d3d9505c55e23896d3d9505c5559f5eb479b37218e52fee04c903c6f8b0b0b0b0b0b0b4e0b0b0b0b0b0b0b4e00123456789abcdeffedcba9876543210ffffffffffffffffffffffffffffffff") |
| 21 | + arm64Expected, _ := hex.DecodeString("4e7a1d632f79d28487f00e25c6685b1ac98a1346e911899ec98a1346e911899e31960b1bc5d961ec298c3094abe125e6181a3b8f6e38440a181a3b8f6e38440a7e4b4bf6c845085792ba923b49631f0cecf1d9cd8126175becf1d9cd8126175b5cece7069f5f571efc0e6f7ae0c1510ca0e2887c7f9e0612a0e2887c7f9e0612da80497ef9cab6feee237ddddd69463434a334a324a3f0ca34a334a324a3f0ca6acda9f97b6c7f3e34ee209446f97afb5e23896d3d9505c55e23896d3d9505c5e52fee04c903c6f8559f5eb479b37218b0b0b0b0b0b0b4e0b0b0b0b0b0b0b4e0fedcba98765432100123456789abcdefffffffffffffffffffffffffffffffff") |
| 22 | + |
18 | 23 | var authKey = [16]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10} |
19 | 24 | var table polyvalAsmTable |
20 | 25 | polyvalTableInitAsm(&authKey, &table) |
21 | | - for i := range 16 { |
22 | | - fmt.Printf("%x", table[i*16:(i+1)*16]) |
23 | | - fmt.Println() |
| 26 | + switch runtime.GOARCH { |
| 27 | + case "arm64": |
| 28 | + if table != (polyvalAsmTable(arm64Expected)) { |
| 29 | + t.Errorf("unexpected table value: got %x, want %x", table, arm64Expected) |
| 30 | + } |
| 31 | + case "amd64": |
| 32 | + if table != (polyvalAsmTable(amd64Expected)) { |
| 33 | + t.Errorf("unexpected table value: got %x, want %x", table, amd64Expected) |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func TestPolyvalBlocksUpdateAsm(t *testing.T) { |
| 39 | + if !supportPolyvalAsm { |
| 40 | + t.Skip("skipping test on unsupported CPU") |
| 41 | + } |
| 42 | + var authKey = [16]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10} |
| 43 | + var table polyvalAsmTable |
| 44 | + polyvalTableInitAsm(&authKey, &table) |
| 45 | + |
| 46 | + expected, _ := hex.DecodeString("b1aba232dd6c847586593756cb2644eb") |
| 47 | + |
| 48 | + var y [16]byte |
| 49 | + var blocks = []byte{ |
| 50 | + 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, |
| 51 | + 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, |
| 52 | + 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, |
| 53 | + 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00, |
| 54 | + } |
| 55 | + polyvalBlocksUpdateAsm(&table, &y, blocks) |
| 56 | + if !bytes.Equal(y[:], expected) { |
| 57 | + t.Errorf("unexpected result: got %x, want %x", y, expected) |
24 | 58 | } |
25 | 59 | } |
0 commit comments