code.pfad.fr/gohmekit@v0.2.1/pairing/crypto/session_test.go (about)

     1  package crypto_test
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"testing"
     7  
     8  	"code.pfad.fr/gohmekit/pairing/crypto"
     9  	hcrypto "github.com/brutella/hc/crypto"
    10  	"gotest.tools/v3/assert"
    11  )
    12  
    13  func TestShortSeal(t *testing.T) {
    14  	data := []byte{0x01, 0x02, 0x03}
    15  	key := [32]byte{
    16  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
    17  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
    18  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
    19  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
    20  
    21  	accSession, err := crypto.NewSession(key, true)
    22  	assert.NilError(t, err)
    23  
    24  	client, err := hcrypto.NewSecureClientSessionFromSharedKey(key)
    25  	assert.NilError(t, err)
    26  
    27  	// Set count to min 2 bytes to test byte order handling
    28  	encrypted := accSession.Seal(data)
    29  
    30  	decrypted, err := client.Decrypt(bytes.NewReader(encrypted))
    31  	assert.NilError(t, err)
    32  
    33  	orig, err := io.ReadAll(decrypted)
    34  	assert.NilError(t, err)
    35  	assert.DeepEqual(t, orig, data)
    36  }
    37  
    38  func TestLongSeal(t *testing.T) {
    39  	data := bytes.Repeat([]byte("1234567890"), 300)
    40  	assert.Equal(t, 3000, len(data))
    41  
    42  	key := [32]byte{
    43  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
    44  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
    45  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
    46  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
    47  
    48  	accSession, err := crypto.NewSession(key, true)
    49  	assert.NilError(t, err)
    50  
    51  	client, err := hcrypto.NewSecureClientSessionFromSharedKey(key)
    52  	assert.NilError(t, err)
    53  
    54  	// Set count to min 2 bytes to test byte order handling
    55  	encrypted := accSession.Seal(data)
    56  
    57  	decrypted, err := client.Decrypt(bytes.NewReader(encrypted))
    58  	assert.NilError(t, err)
    59  
    60  	orig, err := io.ReadAll(decrypted)
    61  	assert.NilError(t, err)
    62  	assert.DeepEqual(t, orig, data)
    63  }
    64  
    65  func TestShortOpen(t *testing.T) {
    66  	data := []byte{0x01, 0x02, 0x03}
    67  	key := [32]byte{
    68  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
    69  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
    70  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
    71  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
    72  
    73  	client, err := hcrypto.NewSecureClientSessionFromSharedKey(key)
    74  	assert.NilError(t, err)
    75  
    76  	// Set count to min 2 bytes to test byte order handling
    77  	clientEncrypted, err := client.Encrypt(bytes.NewReader(data))
    78  	assert.NilError(t, err)
    79  
    80  	b, err := io.ReadAll(clientEncrypted)
    81  	assert.NilError(t, err)
    82  
    83  	accSession, err := crypto.NewSession(key, true)
    84  	assert.NilError(t, err)
    85  
    86  	out, err := accSession.Open(b)
    87  	assert.NilError(t, err)
    88  
    89  	assert.DeepEqual(t, out, data)
    90  }
    91  
    92  func TestOpenByChunks(t *testing.T) {
    93  	data := []byte{0x01, 0x02, 0x03}
    94  	key := [32]byte{
    95  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
    96  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
    97  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
    98  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
    99  
   100  	client, err := hcrypto.NewSecureClientSessionFromSharedKey(key)
   101  	assert.NilError(t, err)
   102  
   103  	// Set count to min 2 bytes to test byte order handling
   104  	clientEncrypted, err := client.Encrypt(bytes.NewReader(data))
   105  	assert.NilError(t, err)
   106  
   107  	b, err := io.ReadAll(clientEncrypted)
   108  	assert.NilError(t, err)
   109  
   110  	accSession, err := crypto.NewSession(key, true)
   111  	assert.NilError(t, err)
   112  
   113  	out, err := accSession.Open(b[0:2])
   114  	assert.Check(t, nil == out)
   115  	assert.NilError(t, err)
   116  
   117  	out, err = accSession.Open(b[2:20])
   118  	assert.Check(t, nil == out)
   119  	assert.NilError(t, err)
   120  
   121  	out, err = accSession.Open(b[20:])
   122  	assert.NilError(t, err)
   123  
   124  	assert.DeepEqual(t, out, data)
   125  }
   126  
   127  func TestLongOpen(t *testing.T) {
   128  	data := bytes.Repeat([]byte("1234567890"), 300)
   129  	assert.Equal(t, 3000, len(data))
   130  
   131  	key := [32]byte{
   132  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   133  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   134  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   135  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
   136  
   137  	client, err := hcrypto.NewSecureClientSessionFromSharedKey(key)
   138  	assert.NilError(t, err)
   139  
   140  	// Set count to min 2 bytes to test byte order handling
   141  	clientEncrypted, err := client.Encrypt(bytes.NewReader(data))
   142  	assert.NilError(t, err)
   143  
   144  	b, err := io.ReadAll(clientEncrypted)
   145  	assert.NilError(t, err)
   146  
   147  	accSession, err := crypto.NewSession(key, true)
   148  	assert.NilError(t, err)
   149  
   150  	out, err := accSession.Open(b)
   151  	assert.NilError(t, err)
   152  
   153  	assert.DeepEqual(t, out, data)
   154  }