Skip to content
Snippets Groups Projects
Commit 6d2304df authored by Jono Wenger's avatar Jono Wenger
Browse files

Fix single use formatting and error messages

parent d25222ea
No related branches found
No related tags found
2 merge requests!65Updates for Channel Support,!37Restructure
......@@ -9,13 +9,13 @@ package singleUse
import (
"bytes"
"gitlab.com/elixxir/crypto/hash"
"crypto/hmac"
"gitlab.com/elixxir/crypto/hash"
)
const macSalt = "singleUseMacSalt"
// MakeMAC generates the MAC used in both the transmission and response CMIX
// MakeMAC generates the MAC used in both the transmission and response cMix
// messages.
func MakeMAC(key []byte, encryptedPayload []byte) []byte {
h := hmac.New(hash.DefaultHash, key)
......
......@@ -40,7 +40,7 @@ func TestMAC_Consistency(t *testing.T) {
testMacBase64 := base64.StdEncoding.EncodeToString(testMAC)
if expectedMac != testMacBase64 {
t.Errorf("MakeMAC() did not return the expected MAC (%d)."+
t.Errorf("MakeMAC did not return the expected MAC (%d)."+
"\nexpected: %s\nreceived: %s", i, expectedMac, testMacBase64)
}
}
......@@ -67,7 +67,8 @@ func TestMAC_Unique(t *testing.T) {
testMACBase64 := base64.StdEncoding.EncodeToString(testMAC)
if _, exists := MACs[testMACBase64]; exists {
t.Errorf("Generated MAC collides with previously generated MAC (%d, %d)."+
t.Errorf("Generated MAC collides with previously generated "+
"MAC (%d, %d)."+
"\ncurrent MAC: key: %+v encryptedPayload: %+v"+
"\npreviouse MAC: key: %+v encryptedPayload: %+v"+
"\nMAC: %s", i, j,
......@@ -94,7 +95,8 @@ func TestMAC_Unique(t *testing.T) {
testMACBase64 := base64.StdEncoding.EncodeToString(testMAC)
if _, exists := MACs[testMACBase64]; exists {
t.Errorf("Generated MAC collides with previously generated MAC (%d, %d)."+
t.Errorf("Generated MAC collides with previously generated "+
"MAC (%d, %d)."+
"\ncurrent MAC: key: %+v encryptedPayload: %+v"+
"\npreviouse MAC: key: %+v encryptedPayload: %+v"+
"\nMAC: %s", i, j,
......@@ -137,7 +139,7 @@ func TestVerifyMAC(t *testing.T) {
receivedMac, _ := base64.StdEncoding.DecodeString(expected)
if !VerifyMAC(key, encryptedPayload, receivedMac) {
t.Errorf("VerifyMAC() failed for a correct MAC (%d)."+
t.Errorf("VerifyMAC failed for a correct MAC (%d)."+
"\nkey: %+v\nexpected: %s\nreceived: %s",
i, key, expected, testMACBase64)
}
......@@ -161,7 +163,7 @@ func TestVerifyMAC_InvalidMacError(t *testing.T) {
expectedMACBase64 := base64.StdEncoding.EncodeToString(expectedMac)
if VerifyMAC(key, encryptedPayload, expectedMac) {
t.Errorf("VerifyMAC() verified invalid MAC (%d)."+
t.Errorf("VerifyMAC verified invalid MAC (%d)."+
"\nkey: %+v\nexpected: %s\nreceived: %s",
i, key, expectedMACBase64, testMACBase64)
}
......
......@@ -22,8 +22,8 @@ func NewRecipientID(pubKey *cyclic.Int, unencryptedPayload []byte) *id.ID {
// Create new hash
h, err := hash.NewCMixHash()
if err != nil {
jww.ERROR.Panicf("Failed to create new hash for single-use "+
"communication recipient ID: %v", err)
jww.FATAL.Panicf("[SU] Failed to create new hash for single-use "+
"communication recipient ID: %+v", err)
}
// Hash the public key and unencrypted payload
......
......@@ -25,7 +25,8 @@ func TestNewRecipientID_Consistency(t *testing.T) {
prng := rand.New(rand.NewSource(42))
for i, expectedRID := range expectedRIDs {
privKey := diffieHellman.GeneratePrivateKey(diffieHellman.DefaultPrivateKeyLength, getGrp(), prng)
privKey := diffieHellman.GeneratePrivateKey(
diffieHellman.DefaultPrivateKeyLength, getGrp(), prng)
pubKey := diffieHellman.GeneratePublicKey(privKey, getGrp())
unencryptedPayload := make([]byte, prng.Intn(500))
prng.Read(unencryptedPayload)
......@@ -33,12 +34,12 @@ func TestNewRecipientID_Consistency(t *testing.T) {
testRID := NewRecipientID(pubKey, unencryptedPayload)
if expectedRID != testRID.String() {
t.Errorf("NewRecipientID() did not return the expected ID (%d)."+
t.Errorf("NewRecipientID did not return the expected ID (%d)."+
"\nexpected: %s\nreceived: %s", i, expectedRID, testRID)
}
if testRID.GetType() != id.User {
t.Errorf("NewRecipientID() did not return expected ID type (%d)."+
t.Errorf("NewRecipientID did not return expected ID type (%d)."+
"\nexpected: %s\nreceived: %s", i, id.User, testRID.GetType())
}
}
......@@ -55,7 +56,8 @@ func TestNewRecipientID_Unique(t *testing.T) {
// Test with same public key but differing payloads
for i := 0; i < testRuns; i++ {
privKey := diffieHellman.GeneratePrivateKey(diffieHellman.DefaultPrivateKeyLength+i, getGrp(), prng)
privKey := diffieHellman.GeneratePrivateKey(
diffieHellman.DefaultPrivateKeyLength+i, getGrp(), prng)
pubKey := diffieHellman.GeneratePublicKey(privKey, getGrp())
for j := 0; j < testRuns; j++ {
unencryptedPayload := make([]byte, prng.Intn(500)+j)
......@@ -64,12 +66,16 @@ func TestNewRecipientID_Unique(t *testing.T) {
testID := NewRecipientID(pubKey, unencryptedPayload)
if _, exists := IDs[testID]; exists {
t.Errorf("Generated ID collides with previously generated ID (%d, %d)."+
t.Errorf("Generated ID collides with previously generated "+
"ID (%d, %d)."+
"\ncurrent ID: key: %s unencryptedPayload: %+v"+
"\npreviouse ID: key: %s unencryptedPayload: %+v"+
"\nID: %s", i, j,
pubKey.Text(10), unencryptedPayload, IDs[testID].pubKey.Text(10),
IDs[testID].encryptedPayload, testID)
pubKey.Text(10),
unencryptedPayload,
IDs[testID].pubKey.Text(10),
IDs[testID].encryptedPayload,
testID)
} else {
IDs[testID] = struct {
pubKey *cyclic.Int
......@@ -84,18 +90,23 @@ func TestNewRecipientID_Unique(t *testing.T) {
unencryptedPayload := make([]byte, prng.Intn(500)+i)
prng.Read(unencryptedPayload)
for j := 0; j < testRuns; j++ {
privKey := diffieHellman.GeneratePrivateKey(diffieHellman.DefaultPrivateKeyLength+j, getGrp(), prng)
privKey := diffieHellman.GeneratePrivateKey(
diffieHellman.DefaultPrivateKeyLength+j, getGrp(), prng)
pubKey := diffieHellman.GeneratePublicKey(privKey, getGrp())
testID := NewRecipientID(pubKey, unencryptedPayload)
if _, exists := IDs[testID]; exists {
t.Errorf("Generated ID collides with previously generated ID (%d, %d)."+
t.Errorf("Generated ID collides with previously generated "+
"ID (%d, %d)."+
"\ncurrent ID: key: %s unencryptedPayload: %+v"+
"\npreviouse ID: key: %s unencryptedPayload: %+v"+
"\nID: %s", i, j,
pubKey.Text(10), unencryptedPayload, IDs[testID].pubKey.Text(10),
IDs[testID].encryptedPayload, testID)
pubKey.Text(10),
unencryptedPayload,
IDs[testID].pubKey.Text(10),
IDs[testID].encryptedPayload,
testID)
} else {
IDs[testID] = struct {
pubKey *cyclic.Int
......
......@@ -23,8 +23,8 @@ func NewResponseFingerprint(dhKey *cyclic.Int, keyNum uint64) format.Fingerprint
// Create new hash
h, err := hash.NewCMixHash()
if err != nil {
jww.ERROR.Panicf("Failed to create new hash for single-use response "+
"fingerprint: %v", err)
jww.FATAL.Panicf("[SU] Failed to create new hash for single-use "+
"response fingerprint: %+v", err)
}
keyNumBytes := make([]byte, binary.MaxVarintLen64)
......
......@@ -33,7 +33,8 @@ func TestNewResponseFingerprint_Consistency(t *testing.T) {
prng := rand.New(rand.NewSource(42))
for i, expectedFP := range expectedFPs {
privKey := diffieHellman.GeneratePrivateKey(diffieHellman.DefaultPrivateKeyLength, getGrp(), prng)
privKey := diffieHellman.GeneratePrivateKey(
diffieHellman.DefaultPrivateKeyLength, getGrp(), prng)
pubKey := diffieHellman.GeneratePublicKey(privKey, getGrp())
dhKey := diffieHellman.GenerateSessionKey(privKey, pubKey, getGrp())
......@@ -41,7 +42,7 @@ func TestNewResponseFingerprint_Consistency(t *testing.T) {
testFpBase64 := base64.StdEncoding.EncodeToString(testFP[:])
if expectedFP != testFpBase64 {
t.Errorf("NewResponseFingerprint() did not return the expected "+
t.Errorf("NewResponseFingerprint did not return the expected "+
"fingerprint (%d).\nexpected: %s\nreceived: %s",
i, expectedFP, testFpBase64)
}
......@@ -59,7 +60,8 @@ func TestNewResponseFingerprint_Unique(t *testing.T) {
// Test with same DH key but differing key numbers
for i := 0; i < testRuns; i++ {
privKey := diffieHellman.GeneratePrivateKey(diffieHellman.DefaultPrivateKeyLength+i, getGrp(), prng)
privKey := diffieHellman.GeneratePrivateKey(
diffieHellman.DefaultPrivateKeyLength+i, getGrp(), prng)
pubKey := diffieHellman.GeneratePublicKey(privKey, getGrp())
dhKey := diffieHellman.GenerateSessionKey(privKey, pubKey, getGrp())
for j := 0; j < testRuns; j++ {
......@@ -85,7 +87,8 @@ func TestNewResponseFingerprint_Unique(t *testing.T) {
// Test with same key numbers but differing DH keys
for i := 0; i < testRuns; i++ {
for j := 0; j < testRuns; j++ {
privKey := diffieHellman.GeneratePrivateKey(diffieHellman.DefaultPrivateKeyLength+j, getGrp(), prng)
privKey := diffieHellman.GeneratePrivateKey(
diffieHellman.DefaultPrivateKeyLength+j, getGrp(), prng)
pubKey := diffieHellman.GeneratePublicKey(privKey, getGrp())
dhKey := diffieHellman.GenerateSessionKey(privKey, pubKey, getGrp())
testFP := NewResponseFingerprint(dhKey, uint64(i))
......
......@@ -22,8 +22,8 @@ func NewResponseKey(dhKey *cyclic.Int, keyNum uint64) []byte {
// Create new hash
h, err := hash.NewCMixHash()
if err != nil {
jww.ERROR.Panicf("Failed to create new hash for single-use response "+
"key: %v", err)
jww.FATAL.Panicf(
"[SU] Failed to create new hash for single-use response key: %+v", err)
}
keyNumBytes := make([]byte, binary.MaxVarintLen64)
......
......@@ -32,7 +32,8 @@ func TestNewResponseKey_Consistency(t *testing.T) {
prng := rand.New(rand.NewSource(42))
for i, expectedKey := range expectedKeys {
privKey := diffieHellman.GeneratePrivateKey(diffieHellman.DefaultPrivateKeyLength, getGrp(), prng)
privKey := diffieHellman.GeneratePrivateKey(
diffieHellman.DefaultPrivateKeyLength, getGrp(), prng)
pubKey := diffieHellman.GeneratePublicKey(privKey, getGrp())
dhKey := diffieHellman.GenerateSessionKey(privKey, pubKey, getGrp())
......@@ -40,7 +41,7 @@ func TestNewResponseKey_Consistency(t *testing.T) {
testKeyBase64 := base64.StdEncoding.EncodeToString(testKey)
if expectedKey != testKeyBase64 {
t.Errorf("NewResponseKey() did not return the expected key (%d)."+
t.Errorf("NewResponseKey did not return the expected key (%d)."+
"\nexpected: %s\nreceived: %s", i, expectedKey, testKeyBase64)
}
}
......@@ -57,7 +58,8 @@ func TestNewResponseKey_Unique(t *testing.T) {
// Test with same DH key but differing key numbers
for i := 0; i < testRuns; i++ {
privKey := diffieHellman.GeneratePrivateKey(diffieHellman.DefaultPrivateKeyLength+i, getGrp(), prng)
privKey := diffieHellman.GeneratePrivateKey(
diffieHellman.DefaultPrivateKeyLength+i, getGrp(), prng)
pubKey := diffieHellman.GeneratePublicKey(privKey, getGrp())
dhKey := diffieHellman.GenerateSessionKey(privKey, pubKey, getGrp())
for j := 0; j < testRuns; j++ {
......@@ -84,7 +86,8 @@ func TestNewResponseKey_Unique(t *testing.T) {
// Test with same key number but differing DH keys
for i := 0; i < testRuns; i++ {
for j := 0; j < testRuns; j++ {
privKey := diffieHellman.GeneratePrivateKey(diffieHellman.DefaultPrivateKeyLength+j, getGrp(), prng)
privKey := diffieHellman.GeneratePrivateKey(
diffieHellman.DefaultPrivateKeyLength+j, getGrp(), prng)
pubKey := diffieHellman.GeneratePublicKey(privKey, getGrp())
dhKey := diffieHellman.GenerateSessionKey(privKey, pubKey, getGrp())
testKey := NewResponseKey(dhKey, uint64(i))
......
......@@ -25,8 +25,8 @@ func NewTagFP(tag string) TagFP {
// Create new hash
h, err := hash.NewCMixHash()
if err != nil {
jww.ERROR.Panicf("Failed to create new hash for single-use "+
"communication tag fingerprint: %v", err)
jww.FATAL.Panicf("[SU] Failed to create new hash for single-use "+
"communication tag fingerprint: %+v", err)
}
// Hash tag and salt
......
......@@ -37,7 +37,7 @@ func TestNewTagFP_Consistency(t *testing.T) {
testFP := NewTagFP(string(tag))
if expectedFP != testFP.String() {
t.Errorf("NewTagFP() did not return the expected fingerprint (%d)."+
t.Errorf("NewTagFP did not return the expected fingerprint (%d)."+
"\nexpected: %s\nreceived: %s", i, expectedFP, testFP)
}
}
......@@ -74,14 +74,14 @@ func TestUnmarshalTagFP(t *testing.T) {
fp := UnmarshalTagFP(fpBytes)
if !bytes.Equal(fpBytes, fp[:]) {
t.Errorf("UnmarshalTagFP() failed to copy the correct bytes into the tag "+
"fingerprint.\nexpected: %+v\nreceived: %+v", fpBytes, fp)
t.Errorf("UnmarshalTagFP failed to copy the correct bytes into the "+
"tag fingerprint.\nexpected: %+v\nreceived: %+v", fpBytes, fp)
}
// Ensure that the data is copied
fpBytes[2]++
if fp[2] == fpBytes[2] {
t.Errorf("UnmarshalTagFP() failed to create a copy of the data.")
t.Errorf("UnmarshalTagFP failed to create a copy of the data.")
}
}
......@@ -94,14 +94,14 @@ func TestTagFP_Bytes(t *testing.T) {
fp := UnmarshalTagFP(fpBytes)
testFpBytes := fp.Bytes()
if !bytes.Equal(fpBytes, testFpBytes) {
t.Errorf("Bytes() failed to return the expected bytes."+
t.Errorf("Bytes failed to return the expected bytes."+
"\nexpected: %+v\nreceived: %+v", fpBytes, testFpBytes)
}
// Ensure that the data is copied
testFpBytes[2]++
if fp[2] == testFpBytes[2] {
t.Errorf("Bytes() failed to create a copy of the data.")
t.Errorf("Bytes failed to create a copy of the data.")
}
}
......@@ -114,7 +114,7 @@ func TestTagFP_String(t *testing.T) {
expectedString := base64.StdEncoding.EncodeToString(fpBytes)
if expectedString != fp.String() {
t.Errorf("String() failed to return the expected string."+
t.Errorf("String failed to return the expected string."+
"\nexpected: %s\nreceived: %s", expectedString, fp.String())
}
}
......@@ -22,8 +22,8 @@ func NewTransmitFingerprint(pubKey *cyclic.Int) format.Fingerprint {
// Create new hash
h, err := hash.NewCMixHash()
if err != nil {
jww.ERROR.Panicf("Failed to create new hash for single-use "+
"transmission fingerprint: %v", err)
jww.FATAL.Panicf("[SU] Failed to create new hash for single-use "+
"transmission fingerprint: %+v", err)
}
// Hash the public key and salt
......
......@@ -33,14 +33,15 @@ func TestNewTransmitFingerprint_Consistency(t *testing.T) {
prng := rand.New(rand.NewSource(42))
for i, expectedFP := range expectedFPs {
privKey := diffieHellman.GeneratePrivateKey(diffieHellman.DefaultPrivateKeyLength, getGrp(), prng)
privKey := diffieHellman.GeneratePrivateKey(
diffieHellman.DefaultPrivateKeyLength, getGrp(), prng)
pubKey := diffieHellman.GeneratePublicKey(privKey, getGrp())
testFP := NewTransmitFingerprint(pubKey)
testFpBase64 := base64.StdEncoding.EncodeToString(testFP[:])
if expectedFP != testFpBase64 {
t.Errorf("NewTransmitFingerprint() did not return the expected "+
t.Errorf("NewTransmitFingerprint did not return the expected "+
"fingerprint (%d).\nexpected: %s\nreceived: %s",
i, expectedFP, testFpBase64)
}
......@@ -54,12 +55,13 @@ func TestNewTransmitFingerprint_Unique(t *testing.T) {
FPs := make(map[format.Fingerprint]*cyclic.Int)
for i := 0; i < testRuns; i++ {
privKey := diffieHellman.GeneratePrivateKey(diffieHellman.DefaultPrivateKeyLength, getGrp(), prng)
privKey := diffieHellman.GeneratePrivateKey(
diffieHellman.DefaultPrivateKeyLength, getGrp(), prng)
pubKey := diffieHellman.GeneratePublicKey(privKey, getGrp())
testFP := NewTransmitFingerprint(pubKey)
if FPs[testFP] != nil {
t.Errorf("Generated fingerprint from key %s collides with "+
t.Errorf("generated fingerprint from key %s collides with "+
"previously generated fingerprint from key %s."+
"\nfingerprint: %s", pubKey.Text(10), FPs[testFP].Text(10),
testFP)
......
......@@ -20,8 +20,8 @@ func NewTransmitKey(dhKey *cyclic.Int) []byte {
// Create new hash
h, err := hash.NewCMixHash()
if err != nil {
jww.ERROR.Panicf("Failed to create new hash for single-use "+
"communication transmission key: %v", err)
jww.FATAL.Panicf("[SU] Failed to create new hash for single-use "+
"communication transmission key: %+v", err)
}
// Hash the DH key and salt
......
......@@ -32,7 +32,8 @@ func TestNewTransmitKey_Consistency(t *testing.T) {
prng := rand.New(rand.NewSource(42))
for i, expectedKey := range expectedKeys {
privKey := diffieHellman.GeneratePrivateKey(diffieHellman.DefaultPrivateKeyLength, getGrp(), prng)
privKey := diffieHellman.GeneratePrivateKey(
diffieHellman.DefaultPrivateKeyLength, getGrp(), prng)
pubKey := diffieHellman.GeneratePublicKey(privKey, getGrp())
dhKey := diffieHellman.GenerateSessionKey(privKey, pubKey, getGrp())
......@@ -40,7 +41,7 @@ func TestNewTransmitKey_Consistency(t *testing.T) {
testKeyBase64 := base64.StdEncoding.EncodeToString(testKey)
if expectedKey != testKeyBase64 {
t.Errorf("NewTransmitKey() did not return the expected key (%d)."+
t.Errorf("NewTransmitKey did not return the expected key (%d)."+
"\nexpected: %s\nreceived: %s", i, expectedKey, testKeyBase64)
}
}
......@@ -53,7 +54,8 @@ func TestNewTransmitKey_Unique(t *testing.T) {
keys := make(map[string]*cyclic.Int, 100)
for i := 0; i < testRuns; i++ {
privKey := diffieHellman.GeneratePrivateKey(diffieHellman.DefaultPrivateKeyLength, getGrp(), prng)
privKey := diffieHellman.GeneratePrivateKey(
diffieHellman.DefaultPrivateKeyLength, getGrp(), prng)
pubKey := diffieHellman.GeneratePublicKey(privKey, getGrp())
dhKey := diffieHellman.GenerateSessionKey(privKey, pubKey, getGrp())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment