Пример #1
0
func TestRandomBytes(t *testing.T) {
	Init()

	sizes := []int{1, 7, 8, 9, 15, 16, 17, 31, 32, 33, 127, 128, 129, 1023, 1024, 1025}

	for _, size := range sizes {
		buf1 := make([]byte, size)
		buf2 := make([]byte, size)
		if !bytes.Equal(buf1, buf2) {
			t.Fatal("buf1 and buf2 aren't zero'd.")
		}
		randombytes.RandomBytes(buf1)
		if bytes.Equal(buf1, buf2) {
			t.Fatal("buf1 and buf2 are still the same.")
		}
		copy(buf2, buf1)
		if !bytes.Equal(buf1, buf2) {
			t.Fatal("Copy failed, arrays aren't the same.")
		}
		randombytes.RandomBytes(buf2)
		if bytes.Equal(buf1, buf2) {
			t.Fatal("Second call to RandomBytes failed.")
		}
	}
}
Пример #2
0
func TestSignAndVerify(t *testing.T) {

	t.Logf("SignBytes is %v", SignBytes())
	t.Logf("SignSeedBytes is %v", SignSeedBytes())
	t.Logf("SignPublicKeyBytes is %v", SignPublicKeyBytes())
	t.Logf("SignSecretKeyBytes is %v", SignSecretKeyBytes())

	Pk := make([]byte, SignPublicKeyBytes())
	Sk := make([]byte, SignSecretKeyBytes())
	ret := SignKeyPair(Pk, Sk)
	if ret != 0 {
		t.Fatalf("bad result making alice sign pair: %v", ret)
	}

	messageSizes := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
		17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 63,
		64, 65, 95, 96, 97, 127, 128, 129, 1023, 1024, 1025, 2047, 3072, 4096,
		5000, 18000, 32768, 128000}

	for i := 0; i < len(messageSizes); i++ {
		// new message
		msg := make([]byte, messageSizes[i])
		randombytes.RandomBytes(msg)

		// sign message
		signed := make([]byte, len(msg)+SignBytes())
		ret = Sign(signed, msg, Sk)
		if ret != 0 {
			t.Fatalf("bad result signing: %v", ret)
		}

		// verify message
		msg2 := make([]byte, len(msg))
		ret = SignOpen(msg2, signed, Pk)
		if ret != 0 {
			t.Fatalf("bad result verifying: %v", ret)
		}
		if !bytes.Equal(msg, msg2) {
			t.Fatalf("different unsealed message")
		}

		// try corrupting the message at various points
		corruptedCopy := make([]byte, len(signed))
		for c := 0; c < len(signed); c += 100 {
			copy(corruptedCopy, signed)

			corruptedCopy[c] += 1 // wrap around

			ret = SignOpen(msg2, corruptedCopy, Pk)
			if ret == 0 {
				t.Fatalf("good result verifying corrupted copy: %v", ret)
			}
		}
	}

}
Пример #3
0
func NewBoxNonce() Nonce {
	nonce := make([]byte, cryptobox.BoxNonceBytes())
	randombytes.RandomBytes(nonce)

	return nonce
}
Пример #4
0
func TestCryptoBox(t *testing.T) {
	messageSizes := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
		17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 63, 64,
		65, 95, 96, 97, 127, 128, 129, 1023, 1024, 1025}
	const maxMessageSize = 1025 + 32 // 32 == Box_ZeroBytes constant

	msgBuf := make([]byte, maxMessageSize)
	cypherBuf := make([]byte, maxMessageSize)
	resultBuf := make([]byte, maxMessageSize)
	pk1 := make([]byte, BoxPublicKeyBytes())
	sk1 := make([]byte, BoxSecretKeyBytes())
	pk2 := make([]byte, BoxPublicKeyBytes())
	sk2 := make([]byte, BoxSecretKeyBytes())
	nonce := make([]byte, BoxNonceBytes())
	key := make([]byte, BoxBeforeNmBytes())

	BoxKeyPair(pk1, sk1)
	BoxKeyPair(pk2, sk2)
	for _, size := range messageSizes {
		t.Log("Running message size ", size)
		// These functions require the first MacBytes of the message buffer to be zeros.
		allocSize := size + BoxZeroBytes()
		msg := msgBuf[:allocSize]
		ct := cypherBuf[:allocSize]
		result := resultBuf[:allocSize]
		sodium.MemZero(msg[:BoxZeroBytes()])
		randombytes.RandomBytes(msg[BoxZeroBytes():]) // Leave first MacBytes() as zeros.
		randombytes.RandomBytes(nonce)

		// Basic coverage of standard box/open functions.
		r1 := Box(ct, msg, nonce, pk1, sk2)
		if r1 != 0 {
			t.Fatal("Crypto box encrypt failed, got ", r1, " expected 0")
		}
		r2 := BoxOpen(result, ct, nonce, pk2, sk1)
		if r2 != 0 {
			t.Fatal("Crypto box open failed, got ", r2, " expected 0")
		}
		if !bytes.Equal(msg, result) {
			t.Fatalf("Byte arrays are not the same, starting bytes are %x, %x, %x, %x vs %x, %x, %x, %x",
				result[0], result[1], result[2], result[3], msg[0], msg[1], msg[2], msg[3])
		}

		// Re-try using Before/After functions for pre-computing the shared key.
		r1 = BoxBeforeNm(key, pk1, sk2)
		if r1 != 0 {
			t.Fatal("Crypto box before nm failed, got ", r1, " expected 0")
		}
		r1 = BoxAfterNm(ct, msg, nonce, key)
		if r1 != 0 {
			t.Fatal("Crypto box after nm failed, got ", r1, " expected 0")
		}
		r2 = BoxBeforeNm(key, pk2, sk1)
		if r2 != 0 {
			t.Fatal("Crypto box before nm (call 2) failed, got ", r2, " expected 0")
		}
		r2 = BoxOpenAfterNm(result, ct, nonce, key)
		if r2 != 0 {
			t.Fatal("Crypto box open after nm failed, got ", r2, " expected 0")
		}
		if !bytes.Equal(msg, result) {
			t.Fatalf("Byte arrays using before/after are not the same, starting bytes are %x, %x, %x, %x vs %x, %x, %x, %x",
				result[0], result[1], result[2], result[3], msg[0], msg[1], msg[2], msg[3])
		}
	}
	t.Log("TestBox passed")
}