Source file src/crypto/subtle/xor_linux_test.go

     1  // Copyright 2024 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package subtle_test
     6  
     7  import (
     8  	"crypto/subtle"
     9  	"syscall"
    10  	"testing"
    11  )
    12  
    13  // dangerousSlice returns a slice which is immediately
    14  // preceded and followed by a faulting page.
    15  // Copied from the bytes package tests.
    16  func dangerousSlice(t *testing.T) []byte {
    17  	pagesize := syscall.Getpagesize()
    18  	b, err := syscall.Mmap(0, 0, 3*pagesize, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_ANONYMOUS|syscall.MAP_PRIVATE)
    19  	if err != nil {
    20  		t.Fatalf("mmap failed %s", err)
    21  	}
    22  	err = syscall.Mprotect(b[:pagesize], syscall.PROT_NONE)
    23  	if err != nil {
    24  		t.Fatalf("mprotect low failed %s\n", err)
    25  	}
    26  	err = syscall.Mprotect(b[2*pagesize:], syscall.PROT_NONE)
    27  	if err != nil {
    28  		t.Fatalf("mprotect high failed %s\n", err)
    29  	}
    30  	return b[pagesize : 2*pagesize]
    31  }
    32  
    33  func TestXORBytesBoundary(t *testing.T) {
    34  	safe := make([]byte, syscall.Getpagesize()*2)
    35  	spicy := dangerousSlice(t)
    36  	for i := 1; i <= syscall.Getpagesize(); i++ {
    37  		start := spicy[:i]
    38  		end := spicy[len(spicy)-i:]
    39  		subtle.XORBytes(end, safe, safe[:i])
    40  		subtle.XORBytes(start, safe, safe[:i])
    41  		subtle.XORBytes(safe, start, safe)
    42  		subtle.XORBytes(safe, end, safe)
    43  		subtle.XORBytes(safe, safe, start)
    44  		subtle.XORBytes(safe, safe, end)
    45  	}
    46  }
    47  

View as plain text