...

Source file src/golang.org/x/crypto/nacl/box/box_test.go

Documentation: golang.org/x/crypto/nacl/box

     1  // Copyright 2012 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 box
     6  
     7  import (
     8  	"bytes"
     9  	"crypto/rand"
    10  	"encoding/hex"
    11  	"testing"
    12  
    13  	"golang.org/x/crypto/curve25519"
    14  )
    15  
    16  func TestSealOpen(t *testing.T) {
    17  	publicKey1, privateKey1, _ := GenerateKey(rand.Reader)
    18  	publicKey2, privateKey2, _ := GenerateKey(rand.Reader)
    19  
    20  	if *privateKey1 == *privateKey2 {
    21  		t.Fatalf("private keys are equal!")
    22  	}
    23  	if *publicKey1 == *publicKey2 {
    24  		t.Fatalf("public keys are equal!")
    25  	}
    26  	message := []byte("test message")
    27  	var nonce [24]byte
    28  
    29  	box := Seal(nil, message, &nonce, publicKey1, privateKey2)
    30  	opened, ok := Open(nil, box, &nonce, publicKey2, privateKey1)
    31  	if !ok {
    32  		t.Fatalf("failed to open box")
    33  	}
    34  
    35  	if !bytes.Equal(opened, message) {
    36  		t.Fatalf("got %x, want %x", opened, message)
    37  	}
    38  
    39  	for i := range box {
    40  		box[i] ^= 0x40
    41  		_, ok := Open(nil, box, &nonce, publicKey2, privateKey1)
    42  		if ok {
    43  			t.Fatalf("opened box with byte %d corrupted", i)
    44  		}
    45  		box[i] ^= 0x40
    46  	}
    47  }
    48  
    49  func TestBox(t *testing.T) {
    50  	var privateKey1, privateKey2 [32]byte
    51  	for i := range privateKey1[:] {
    52  		privateKey1[i] = 1
    53  	}
    54  	for i := range privateKey2[:] {
    55  		privateKey2[i] = 2
    56  	}
    57  
    58  	var publicKey1 [32]byte
    59  	curve25519.ScalarBaseMult(&publicKey1, &privateKey1)
    60  	var message [64]byte
    61  	for i := range message[:] {
    62  		message[i] = 3
    63  	}
    64  
    65  	var nonce [24]byte
    66  	for i := range nonce[:] {
    67  		nonce[i] = 4
    68  	}
    69  
    70  	box := Seal(nil, message[:], &nonce, &publicKey1, &privateKey2)
    71  
    72  	// expected was generated using the C implementation of NaCl.
    73  	expected, _ := hex.DecodeString("78ea30b19d2341ebbdba54180f821eec265cf86312549bea8a37652a8bb94f07b78a73ed1708085e6ddd0e943bbdeb8755079a37eb31d86163ce241164a47629c0539f330b4914cd135b3855bc2a2dfc")
    74  
    75  	if !bytes.Equal(box, expected) {
    76  		t.Fatalf("box didn't match, got\n%x\n, expected\n%x", box, expected)
    77  	}
    78  }
    79  
    80  func TestSealOpenAnonymous(t *testing.T) {
    81  	publicKey, privateKey, _ := GenerateKey(rand.Reader)
    82  	message := []byte("test message")
    83  
    84  	box, err := SealAnonymous(nil, message, publicKey, nil)
    85  	if err != nil {
    86  		t.Fatalf("Unexpected error sealing %v", err)
    87  	}
    88  	opened, ok := OpenAnonymous(nil, box, publicKey, privateKey)
    89  	if !ok {
    90  		t.Fatalf("failed to open box")
    91  	}
    92  
    93  	if !bytes.Equal(opened, message) {
    94  		t.Fatalf("got %x, want %x", opened, message)
    95  	}
    96  
    97  	for i := range box {
    98  		box[i] ^= 0x40
    99  		_, ok := OpenAnonymous(nil, box, publicKey, privateKey)
   100  		if ok {
   101  			t.Fatalf("opened box with byte %d corrupted", i)
   102  		}
   103  		box[i] ^= 0x40
   104  	}
   105  
   106  	// allocates new slice if out isn't long enough
   107  	out := []byte("hello")
   108  	orig := append([]byte(nil), out...)
   109  	box, err = SealAnonymous(out, message, publicKey, nil)
   110  	if err != nil {
   111  		t.Fatalf("Unexpected error sealing %v", err)
   112  	}
   113  	if !bytes.Equal(out, orig) {
   114  		t.Fatal("expected out to be unchanged")
   115  	}
   116  	if !bytes.HasPrefix(box, orig) {
   117  		t.Fatal("expected out to be coppied to returned slice")
   118  	}
   119  	_, ok = OpenAnonymous(nil, box[len(out):], publicKey, privateKey)
   120  	if !ok {
   121  		t.Fatalf("failed to open box")
   122  	}
   123  
   124  	// uses provided slice if it's long enough
   125  	out = append(make([]byte, 0, 1000), []byte("hello")...)
   126  	orig = append([]byte(nil), out...)
   127  	box, err = SealAnonymous(out, message, publicKey, nil)
   128  	if err != nil {
   129  		t.Fatalf("Unexpected error sealing %v", err)
   130  	}
   131  	if !bytes.Equal(out, orig) {
   132  		t.Fatal("expected out to be unchanged")
   133  	}
   134  	if &out[0] != &box[0] {
   135  		t.Fatal("expected box to point to out")
   136  	}
   137  	_, ok = OpenAnonymous(nil, box[len(out):], publicKey, privateKey)
   138  	if !ok {
   139  		t.Fatalf("failed to open box")
   140  	}
   141  }
   142  
   143  func TestSealedBox(t *testing.T) {
   144  	var privateKey [32]byte
   145  	for i := range privateKey[:] {
   146  		privateKey[i] = 1
   147  	}
   148  
   149  	var publicKey [32]byte
   150  	curve25519.ScalarBaseMult(&publicKey, &privateKey)
   151  	var message [64]byte
   152  	for i := range message[:] {
   153  		message[i] = 3
   154  	}
   155  
   156  	fakeRand := bytes.NewReader([]byte{5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5})
   157  	box, err := SealAnonymous(nil, message[:], &publicKey, fakeRand)
   158  	if err != nil {
   159  		t.Fatalf("Unexpected error sealing %v", err)
   160  	}
   161  
   162  	// expected was generated using the C implementation of libsodium with a
   163  	// random implementation that always returns 5.
   164  	// https://gist.github.com/mastahyeti/942ec3f175448d68fed25018adbce5a7
   165  	expected, _ := hex.DecodeString("50a61409b1ddd0325e9b16b700e719e9772c07000b1bd7786e907c653d20495d2af1697137a53b1b1dfc9befc49b6eeb38f86be720e155eb2be61976d2efb34d67ecd44a6ad634625eb9c288bfc883431a84ab0f5557dfe673aa6f74c19f033e648a947358cfcc606397fa1747d5219a")
   166  
   167  	if !bytes.Equal(box, expected) {
   168  		t.Fatalf("box didn't match, got\n%x\n, expected\n%x", box, expected)
   169  	}
   170  
   171  	// box was generated using the C implementation of libsodium.
   172  	// https://gist.github.com/mastahyeti/942ec3f175448d68fed25018adbce5a7
   173  	box, _ = hex.DecodeString("3462e0640728247a6f581e3812850d6edc3dcad1ea5d8184c072f62fb65cb357e27ffa8b76f41656bc66a0882c4d359568410665746d27462a700f01e314f382edd7aae9064879b0f8ba7b88866f88f5e4fbd7649c850541877f9f33ebd25d46d9cbcce09b69a9ba07f0eb1d105d4264")
   174  	result, ok := OpenAnonymous(nil, box, &publicKey, &privateKey)
   175  	if !ok {
   176  		t.Fatalf("failed to open box")
   177  	}
   178  	if !bytes.Equal(result, message[:]) {
   179  		t.Fatalf("message didn't match, got\n%x\n, expected\n%x", result, message[:])
   180  	}
   181  }
   182  

View as plain text