Skip to content

Commit a042249

Browse files
committed
More Hmac cases
1 parent cb11d25 commit a042249

20 files changed

+151
-151
lines changed

auth/peer_test.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ func CreatePeerPair(t *testing.T) (*Peer, *Peer, *wallet.MockWallet, *wallet.Moc
128128
hmacBytes[i] = byte(i)
129129
}
130130

131-
aliceWallet.MockCreateHmac = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
132-
return &wallet.CreateHMACResult{Hmac: hmacBytes}, nil
131+
aliceWallet.MockCreateHMAC = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
132+
return &wallet.CreateHMACResult{HMAC: hmacBytes}, nil
133133
}
134-
bobWallet.MockCreateHmac = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
135-
return &wallet.CreateHMACResult{Hmac: hmacBytes}, nil
134+
bobWallet.MockCreateHMAC = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
135+
return &wallet.CreateHMACResult{HMAC: hmacBytes}, nil
136136
}
137137

138138
aliceWallet.MockDecrypt = func(ctx context.Context, args wallet.DecryptArgs, originator string) (*wallet.DecryptResult, error) {
@@ -553,11 +553,11 @@ func TestPeerCertificateExchange(t *testing.T) {
553553
hmacBytes[i] = byte(i)
554554
}
555555

556-
aliceWallet.MockCreateHmac = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
557-
return &wallet.CreateHMACResult{Hmac: hmacBytes}, nil
556+
aliceWallet.MockCreateHMAC = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
557+
return &wallet.CreateHMACResult{HMAC: hmacBytes}, nil
558558
}
559-
bobWallet.MockCreateHmac = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
560-
return &wallet.CreateHMACResult{Hmac: hmacBytes}, nil
559+
bobWallet.MockCreateHMAC = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
560+
return &wallet.CreateHMACResult{HMAC: hmacBytes}, nil
561561
}
562562

563563
// Set up transport with logging
@@ -765,11 +765,11 @@ func TestPeerMultiDeviceAuthentication(t *testing.T) {
765765
hmacBytes1[i] = byte(i)
766766
}
767767

768-
aliceWallet1.MockCreateHmac = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
769-
return &wallet.CreateHMACResult{Hmac: hmacBytes1}, nil
768+
aliceWallet1.MockCreateHMAC = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
769+
return &wallet.CreateHMACResult{HMAC: hmacBytes1}, nil
770770
}
771-
aliceWallet2.MockCreateHmac = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
772-
return &wallet.CreateHMACResult{Hmac: hmacBytes1}, nil
771+
aliceWallet2.MockCreateHMAC = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
772+
return &wallet.CreateHMACResult{HMAC: hmacBytes1}, nil
773773
}
774774

775775
aliceWallet1.MockDecrypt = func(ctx context.Context, args wallet.DecryptArgs, originator string) (*wallet.DecryptResult, error) {
@@ -802,8 +802,8 @@ func TestPeerMultiDeviceAuthentication(t *testing.T) {
802802
hmacBytes2[i] = byte(i)
803803
}
804804

805-
bobWallet.MockCreateHmac = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
806-
return &wallet.CreateHMACResult{Hmac: hmacBytes2}, nil
805+
bobWallet.MockCreateHMAC = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
806+
return &wallet.CreateHMACResult{HMAC: hmacBytes2}, nil
807807
}
808808
bobWallet.MockDecrypt = func(ctx context.Context, args wallet.DecryptArgs, originator string) (*wallet.DecryptResult, error) {
809809
return &wallet.DecryptResult{Plaintext: []byte("decrypted")}, nil
@@ -1039,11 +1039,11 @@ func TestPartialCertificateAcceptance(t *testing.T) {
10391039
hmacBytes[i] = byte(i)
10401040
}
10411041

1042-
aliceWallet.MockCreateHmac = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
1043-
return &wallet.CreateHMACResult{Hmac: hmacBytes}, nil
1042+
aliceWallet.MockCreateHMAC = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
1043+
return &wallet.CreateHMACResult{HMAC: hmacBytes}, nil
10441044
}
1045-
bobWallet.MockCreateHmac = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
1046-
return &wallet.CreateHMACResult{Hmac: hmacBytes}, nil
1045+
bobWallet.MockCreateHMAC = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
1046+
return &wallet.CreateHMACResult{HMAC: hmacBytes}, nil
10471047
}
10481048

10491049
// Create mocked transports
@@ -1317,11 +1317,11 @@ func TestLibraryCardVerification(t *testing.T) {
13171317
hmacBytes[i] = byte(i)
13181318
}
13191319

1320-
aliceWallet.MockCreateHmac = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
1321-
return &wallet.CreateHMACResult{Hmac: hmacBytes}, nil
1320+
aliceWallet.MockCreateHMAC = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
1321+
return &wallet.CreateHMACResult{HMAC: hmacBytes}, nil
13221322
}
1323-
bobWallet.MockCreateHmac = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
1324-
return &wallet.CreateHMACResult{Hmac: hmacBytes}, nil
1323+
bobWallet.MockCreateHMAC = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
1324+
return &wallet.CreateHMACResult{HMAC: hmacBytes}, nil
13251325
}
13261326

13271327
// Create mocked transports
@@ -1639,11 +1639,11 @@ func TestNonmatchingCertificateRejection(t *testing.T) {
16391639
for i := range hmacBytes {
16401640
hmacBytes[i] = byte(i)
16411641
}
1642-
aliceWallet.MockCreateHmac = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
1643-
return &wallet.CreateHMACResult{Hmac: hmacBytes}, nil
1642+
aliceWallet.MockCreateHMAC = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
1643+
return &wallet.CreateHMACResult{HMAC: hmacBytes}, nil
16441644
}
1645-
bobWallet.MockCreateHmac = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
1646-
return &wallet.CreateHMACResult{Hmac: hmacBytes}, nil
1645+
bobWallet.MockCreateHMAC = func(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
1646+
return &wallet.CreateHMACResult{HMAC: hmacBytes}, nil
16471647
}
16481648

16491649
// Setup transports

auth/utils/completed_proto_wallet.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,12 @@ func (c *CompletedProtoWallet) GetHeaderForHeight(ctx context.Context, args wall
130130
return nil, nil
131131
}
132132

133-
// CreateHmac delegates to the embedded ProtoWallet
133+
// CreateHMAC delegates to the embedded ProtoWallet
134134
func (c *CompletedProtoWallet) CreateHMAC(ctx context.Context, args wallet.CreateHMACArgs, originator string) (*wallet.CreateHMACResult, error) {
135135
return c.ProtoWallet.CreateHMAC(ctx, args, originator)
136136
}
137137

138-
// VerifyHmac delegates to the embedded ProtoWallet
138+
// VerifyHMAC delegates to the embedded ProtoWallet
139139
func (c *CompletedProtoWallet) VerifyHMAC(ctx context.Context, args wallet.VerifyHMACArgs, originator string) (*wallet.VerifyHMACResult, error) {
140140
return c.ProtoWallet.VerifyHMAC(ctx, args, originator)
141141
}

auth/utils/create_nonce.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func CreateNonce(ctx context.Context, w wallet.KeyOperations, counterparty walle
3939
}
4040

4141
// Combine the random data and the HMAC
42-
combined := append(randomBytes, hmac.Hmac...)
42+
combined := append(randomBytes, hmac.HMAC...)
4343

4444
// Encode as base64
4545
nonce := base64.StdEncoding.EncodeToString(combined)

auth/utils/verify_nonce.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func VerifyNonce(
4343
Counterparty: counterparty,
4444
},
4545
Data: data,
46-
Hmac: hmac,
46+
HMAC: hmac,
4747
}
4848

4949
// Verify the hmac

wallet/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ type KeyOperations interface {
114114
GetPublicKey(ctx context.Context, args GetPublicKeyArgs, originator string) (*GetPublicKeyResult, error)
115115
Encrypt(ctx context.Context, args EncryptArgs, originator string) (*EncryptResult, error)
116116
Decrypt(ctx context.Context, args DecryptArgs, originator string) (*DecryptResult, error)
117-
CreateHmac(ctx context.Context, args CreateHmacArgs, originator string) (*CreateHmacResult, error)
118-
VerifyHmac(ctx context.Context, args VerifyHmacArgs, originator string) (*VerifyHmacResult, error)
117+
CreateHMAC(ctx context.Context, args CreateHMACArgs, originator string) (*CreateHMACResult, error)
118+
VerifyHMAC(ctx context.Context, args VerifyHMACArgs, originator string) (*VerifyHMACResult, error)
119119
CreateSignature(ctx context.Context, args CreateSignatureArgs, originator string) (*CreateSignatureResult, error)
120120
VerifySignature(ctx context.Context, args VerifySignatureArgs, originator string) (*VerifySignatureResult, error)
121121
}

wallet/mock.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ type MockWallet struct {
3333
ProveCertificateError error
3434
GetPublicKeyResult *GetPublicKeyResult
3535
GetPublicKeyError error
36-
CreateHmacResult *CreateHMACResult
37-
CreateHmacError error
36+
CreateHMACResult *CreateHMACResult
37+
CreateHMACError error
3838
CreateSignatureResult *CreateSignatureResult
3939
CreateSignatureError error
4040
VerifySignatureResult *VerifySignatureResult
@@ -49,7 +49,7 @@ type MockWallet struct {
4949
MockDiscoverByAttributes func(ctx context.Context, args DiscoverByAttributesArgs, originator string) (*DiscoverCertificatesResult, error)
5050
MockGetPublicKey func(ctx context.Context, args GetPublicKeyArgs, originator string) (*GetPublicKeyResult, error)
5151
MockCreateSignature func(ctx context.Context, args CreateSignatureArgs, originator string) (*CreateSignatureResult, error)
52-
MockCreateHmac func(ctx context.Context, args CreateHMACArgs, originator string) (*CreateHMACResult, error)
52+
MockCreateHMAC func(ctx context.Context, args CreateHMACArgs, originator string) (*CreateHMACResult, error)
5353
MockDecrypt func(ctx context.Context, args DecryptArgs, originator string) (*DecryptResult, error)
5454
MockVerifySignature func(ctx context.Context, args VerifySignatureArgs, originator string) (*VerifySignatureResult, error)
5555
MockListCertificates func(ctx context.Context, args ListCertificatesArgs, originator string) (*ListCertificatesResult, error)
@@ -192,17 +192,17 @@ func (m *MockWallet) RevealSpecificKeyLinkage(ctx context.Context, args RevealSp
192192
}
193193

194194
func (m *MockWallet) CreateHMAC(ctx context.Context, args CreateHMACArgs, originator string) (*CreateHMACResult, error) {
195-
if m.MockCreateHmac != nil {
196-
return m.MockCreateHmac(ctx, args, originator)
195+
if m.MockCreateHMAC != nil {
196+
return m.MockCreateHMAC(ctx, args, originator)
197197
}
198-
if m.CreateHmacResult == nil {
198+
if m.CreateHMACResult == nil {
199199
require.Fail(m.T, "CreateHMAC mock called but CreateHMACResult not set")
200200
return nil, errors.New("CreateHMAC mock result not configured")
201201
}
202-
if m.CreateHmacError != nil {
203-
return nil, m.CreateHmacError
202+
if m.CreateHMACError != nil {
203+
return nil, m.CreateHMACError
204204
}
205-
return m.CreateHmacResult, nil
205+
return m.CreateHMACResult, nil
206206
}
207207

208208
func (m *MockWallet) VerifyHMAC(ctx context.Context, args VerifyHMACArgs, originator string) (*VerifyHMACResult, error) {

wallet/proto_wallet.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func (p *ProtoWallet) VerifySignature(
273273
}, nil
274274
}
275275

276-
// CreateHmac generates an HMAC (Hash-based Message Authentication Code) for the provided data
276+
// CreateHMAC generates an HMAC (Hash-based Message Authentication Code) for the provided data
277277
// using a symmetric key derived from the protocol, key ID, and counterparty.
278278
func (p *ProtoWallet) CreateHMAC(
279279
ctx context.Context,
@@ -307,10 +307,10 @@ func (p *ProtoWallet) CreateHMAC(
307307
mac.Write(args.Data)
308308
hmacValue := mac.Sum(nil)
309309

310-
return &CreateHMACResult{Hmac: hmacValue}, nil
310+
return &CreateHMACResult{HMAC: hmacValue}, nil
311311
}
312312

313-
// VerifyHmac verifies that the provided HMAC matches the expected value for the given data.
313+
// VerifyHMAC verifies that the provided HMAC matches the expected value for the given data.
314314
// The verification uses the same protocol, key ID, and counterparty that were used to create the HMAC.
315315
func (p *ProtoWallet) VerifyHMAC(
316316
ctx context.Context,
@@ -342,10 +342,10 @@ func (p *ProtoWallet) VerifyHMAC(
342342
// Create expected HMAC
343343
mac := hmac.New(sha256.New, key.ToBytes())
344344
mac.Write(args.Data)
345-
expectedHmac := mac.Sum(nil)
345+
expectedHMAC := mac.Sum(nil)
346346

347347
// Verify HMAC
348-
if !hmac.Equal(expectedHmac, args.Hmac) {
348+
if !hmac.Equal(expectedHMAC, args.HMAC) {
349349
return &VerifyHMACResult{Valid: false}, nil
350350
}
351351

wallet/serializer/create_hmac.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/bsv-blockchain/go-sdk/wallet"
88
)
99

10-
func SerializeCreateHmacArgs(args *wallet.CreateHMACArgs) ([]byte, error) {
10+
func SerializeCreateHMACArgs(args *wallet.CreateHMACArgs) ([]byte, error) {
1111
w := util.NewWriter()
1212

1313
// Encode key related params (protocol, key, counterparty, privileged)
@@ -34,7 +34,7 @@ func SerializeCreateHmacArgs(args *wallet.CreateHMACArgs) ([]byte, error) {
3434
return w.Buf, nil
3535
}
3636

37-
func DeserializeCreateHmacArgs(data []byte) (*wallet.CreateHMACArgs, error) {
37+
func DeserializeCreateHMACArgs(data []byte) (*wallet.CreateHMACArgs, error) {
3838
r := util.NewReaderHoldError(data)
3939
args := &wallet.CreateHMACArgs{}
4040

@@ -64,25 +64,25 @@ func DeserializeCreateHmacArgs(data []byte) (*wallet.CreateHMACArgs, error) {
6464
return args, nil
6565
}
6666

67-
func SerializeCreateHmacResult(result *wallet.CreateHMACResult) ([]byte, error) {
67+
func SerializeCreateHMACResult(result *wallet.CreateHMACResult) ([]byte, error) {
6868
w := util.NewWriter()
6969
w.WriteByte(0) // errorByte = 0 (success)
70-
w.WriteBytes(result.Hmac)
70+
w.WriteBytes(result.HMAC)
7171
return w.Buf, nil
7272
}
7373

74-
func DeserializeCreateHmacResult(data []byte) (*wallet.CreateHMACResult, error) {
74+
func DeserializeCreateHMACResult(data []byte) (*wallet.CreateHMACResult, error) {
7575
r := util.NewReaderHoldError(data)
7676
result := &wallet.CreateHMACResult{}
7777

7878
// Read error byte (0 = success)
7979
errorByte := r.ReadByte()
8080
if errorByte != 0 {
81-
return nil, fmt.Errorf("createHmac failed with error byte %d", errorByte)
81+
return nil, fmt.Errorf("createHMAC failed with error byte %d", errorByte)
8282
}
8383

8484
// Read hmac (remaining bytes)
85-
result.Hmac = r.ReadRemaining()
85+
result.HMAC = r.ReadRemaining()
8686

8787
if r.Err != nil {
8888
return nil, fmt.Errorf("error deserializing CreateHMAC result: %w", r.Err)

wallet/serializer/create_hmac_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"testing"
77
)
88

9-
func TestCreateHmacArgs(t *testing.T) {
9+
func TestCreateHMACArgs(t *testing.T) {
1010
tests := []struct {
1111
name string
1212
args *wallet.CreateHMACArgs
@@ -55,11 +55,11 @@ func TestCreateHmacArgs(t *testing.T) {
5555
for _, tt := range tests {
5656
t.Run(tt.name, func(t *testing.T) {
5757
// Test serialization
58-
data, err := SerializeCreateHmacArgs(tt.args)
58+
data, err := SerializeCreateHMACArgs(tt.args)
5959
require.NoError(t, err)
6060

6161
// Test deserialization
62-
got, err := DeserializeCreateHmacArgs(data)
62+
got, err := DeserializeCreateHMACArgs(data)
6363
require.NoError(t, err)
6464

6565
// Compare results
@@ -68,13 +68,13 @@ func TestCreateHmacArgs(t *testing.T) {
6868
}
6969
}
7070

71-
func TestCreateHmacResult(t *testing.T) {
71+
func TestCreateHMACResult(t *testing.T) {
7272
t.Run("serialize/deserialize", func(t *testing.T) {
73-
result := &wallet.CreateHMACResult{Hmac: []byte{1, 2, 3, 4}}
74-
data, err := SerializeCreateHmacResult(result)
73+
result := &wallet.CreateHMACResult{HMAC: []byte{1, 2, 3, 4}}
74+
data, err := SerializeCreateHMACResult(result)
7575
require.NoError(t, err)
7676

77-
got, err := DeserializeCreateHmacResult(data)
77+
got, err := DeserializeCreateHMACResult(data)
7878
require.NoError(t, err)
7979
require.Equal(t, result, got)
8080
})

wallet/serializer/verify_hmac.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/bsv-blockchain/go-sdk/wallet"
88
)
99

10-
func SerializeVerifyHmacArgs(args *wallet.VerifyHMACArgs) ([]byte, error) {
10+
func SerializeVerifyHMACArgs(args *wallet.VerifyHMACArgs) ([]byte, error) {
1111
w := util.NewWriter()
1212

1313
// Encode key related params (protocol, key, counterparty, privileged)
@@ -25,7 +25,7 @@ func SerializeVerifyHmacArgs(args *wallet.VerifyHMACArgs) ([]byte, error) {
2525
w.WriteBytes(keyParams)
2626

2727
// Write HMAC bytes (fixed 32 bytes)
28-
w.WriteBytes(args.Hmac)
28+
w.WriteBytes(args.HMAC)
2929

3030
// Write data length + bytes
3131
w.WriteVarInt(uint64(len(args.Data)))
@@ -37,7 +37,7 @@ func SerializeVerifyHmacArgs(args *wallet.VerifyHMACArgs) ([]byte, error) {
3737
return w.Buf, nil
3838
}
3939

40-
func DeserializeVerifyHmacArgs(data []byte) (*wallet.VerifyHMACArgs, error) {
40+
func DeserializeVerifyHMACArgs(data []byte) (*wallet.VerifyHMACArgs, error) {
4141
r := util.NewReaderHoldError(data)
4242
args := &wallet.VerifyHMACArgs{}
4343

@@ -53,7 +53,7 @@ func DeserializeVerifyHmacArgs(data []byte) (*wallet.VerifyHMACArgs, error) {
5353
args.PrivilegedReason = params.PrivilegedReason
5454

5555
// Read HMAC (fixed 32 bytes)
56-
args.Hmac = r.ReadBytes(32)
56+
args.HMAC = r.ReadBytes(32)
5757

5858
// Read data
5959
dataLen := r.ReadVarInt()
@@ -70,19 +70,19 @@ func DeserializeVerifyHmacArgs(data []byte) (*wallet.VerifyHMACArgs, error) {
7070
return args, nil
7171
}
7272

73-
func SerializeVerifyHmacResult(result *wallet.VerifyHMACResult) ([]byte, error) {
73+
func SerializeVerifyHMACResult(result *wallet.VerifyHMACResult) ([]byte, error) {
7474
w := util.NewWriter()
7575
w.WriteByte(0) // errorByte = 0 (success)
7676
return w.Buf, nil
7777
}
7878

79-
func DeserializeVerifyHmacResult(data []byte) (*wallet.VerifyHMACResult, error) {
79+
func DeserializeVerifyHMACResult(data []byte) (*wallet.VerifyHMACResult, error) {
8080
r := util.NewReaderHoldError(data)
8181

8282
// Read error byte (0 = success)
8383
errorByte := r.ReadByte()
8484
if errorByte != 0 {
85-
return nil, fmt.Errorf("verifyHmac failed with error byte %d", errorByte)
85+
return nil, fmt.Errorf("verifyHMAC failed with error byte %d", errorByte)
8686
}
8787

8888
r.CheckComplete()

0 commit comments

Comments
 (0)