Skip to content

Commit 56f8831

Browse files
committed
defaultCryptoBackend should be a pointer in receivedMessage.
Doc added for Crypto
1 parent 592fe59 commit 56f8831

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

pss/crypto/crypto.go

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,28 @@ type WrapParams struct {
5959
KeySym []byte
6060
}
6161

62+
// Contains a successfully decrypted message prior to parsing and validating
6263
type ReceivedMessage interface {
6364
ValidateAndParse() bool
6465
GetPayload() []byte
6566
GetSrc() *ecdsa.PublicKey
6667
}
6768

69+
// Crypto contains methods from MessageCrypto, CryptoBackend and CryptoUtils
6870
type Crypto interface {
6971
MessageCrypto
7072
CryptoBackend
7173
CryptoUtils
7274
}
7375

76+
// MessageCrypto contains methods for wrapping(encrypting) and unwrapping(decrypting) messages
7477
type MessageCrypto interface {
7578
WrapMessage(msgData []byte, params *WrapParams) (data []byte, err error)
7679
UnWrapSymmetric(encryptedData, key []byte) (ReceivedMessage, error)
7780
UnWrapAsymmetric(encryptedData []byte, key *ecdsa.PrivateKey) (ReceivedMessage, error)
7881
}
7982

83+
// CryptoBackend contains key manipulation methods
8084
type CryptoBackend interface {
8185

8286
// Key store functions
@@ -92,7 +96,7 @@ type CryptoBackend interface {
9296
CompressPubkey(pubkey *ecdsa.PublicKey) []byte
9397
}
9498

95-
//Used only in tests
99+
// CryptoUtils contains utility methods for tests
96100
type CryptoUtils interface {
97101
GenerateKey() (*ecdsa.PrivateKey, error)
98102
NewKeyPair(ctx context.Context) (string, error)
@@ -125,13 +129,14 @@ type receivedMessage struct {
125129
Src *ecdsa.PublicKey // Source public key used for signing the message
126130
Dst *ecdsa.PublicKey // Destination public key used for encrypting the msessage asymmetrically
127131

128-
crypto defaultCryptoBackend
132+
crypto *defaultCryptoBackend
129133
}
130134

131135
func (msg receivedMessage) GetSrc() *ecdsa.PublicKey {
132136
return msg.Src
133137
}
134138

139+
// ValidateAndParse checks that the format and the signnature are correct. It also set Payload as the parsed message
135140
func (msg *receivedMessage) ValidateAndParse() bool {
136141
end := len(msg.Raw)
137142
if end < 1 {
@@ -166,12 +171,13 @@ func (msg *receivedMessage) ValidateAndParse() bool {
166171
return true
167172
}
168173

174+
// GetPayload obtains the parsed payload of the message. Should be called after ValidateAndParse
169175
func (msg receivedMessage) GetPayload() []byte {
170176
return msg.Payload
171177
}
172178

173179
// sigToPubKey returns the public key associated to the message's signature.
174-
// should only be called id message has been signed
180+
// should only be called if message has been signed
175181
func (msg *receivedMessage) sigToPubKey() *ecdsa.PublicKey {
176182
defer func() { recover() }() // in case of invalid signature
177183

@@ -188,6 +194,14 @@ func (msg *receivedMessage) sigToPubKey() *ecdsa.PublicKey {
188194
return pub
189195
}
190196

197+
func newReceivedMessage(decrypted []byte, salt []byte, crypto *defaultCryptoBackend) *receivedMessage {
198+
return &receivedMessage{
199+
Raw: decrypted,
200+
Salt: salt,
201+
crypto: crypto,
202+
}
203+
}
204+
191205
func New() Crypto {
192206
cryptoBackend = defaultCryptoBackend{
193207
symKeys: make(map[string][]byte),
@@ -205,7 +219,7 @@ func NewCryptoUtils() CryptoUtils {
205219

206220
// == Message Crypto ==
207221

208-
func (crypto defaultCryptoBackend) WrapMessage(payload []byte, params *WrapParams) (data []byte, err error) {
222+
func (crypto *defaultCryptoBackend) WrapMessage(payload []byte, params *WrapParams) (data []byte, err error) {
209223
var padding []byte
210224
if createPadding {
211225
padding, err = crypto.generateSecureRandomData(defaultPaddingByteSize)
@@ -248,20 +262,20 @@ func (crypto defaultCryptoBackend) WrapMessage(payload []byte, params *WrapParam
248262
return
249263
}
250264

251-
func (crypto defaultCryptoBackend) UnWrapSymmetric(encryptedData, key []byte) (ReceivedMessage, error) {
265+
func (crypto *defaultCryptoBackend) UnWrapSymmetric(encryptedData, key []byte) (ReceivedMessage, error) {
252266
decrypted, salt, err := crypto.decryptSymmetric(encryptedData, key)
253267
if err != nil {
254268
return nil, err
255269
}
256-
msg := newDecryptedMessage(decrypted, salt)
270+
msg := newReceivedMessage(decrypted, salt, crypto)
257271
return msg, err
258272
}
259273

260-
func (crypto defaultCryptoBackend) UnWrapAsymmetric(encryptedData []byte, key *ecdsa.PrivateKey) (ReceivedMessage, error) {
274+
func (crypto *defaultCryptoBackend) UnWrapAsymmetric(encryptedData []byte, key *ecdsa.PrivateKey) (ReceivedMessage, error) {
261275
decrypted, err := crypto.decryptAsymmetric(encryptedData, key)
262276
switch err {
263277
case nil:
264-
message := newDecryptedMessage(decrypted, nil)
278+
message := newReceivedMessage(decrypted, nil, crypto)
265279
return message, nil
266280
case ecies.ErrInvalidPublicKey: // addressed to somebody else
267281
return nil, err
@@ -270,14 +284,7 @@ func (crypto defaultCryptoBackend) UnWrapAsymmetric(encryptedData []byte, key *e
270284
}
271285
}
272286

273-
func newDecryptedMessage(decrypted []byte, salt []byte) *receivedMessage {
274-
return &receivedMessage{
275-
Raw: decrypted,
276-
Salt: salt,
277-
}
278-
}
279-
280-
func (crypto defaultCryptoBackend) addPayloadSizeField(rawBytes rawMessage, payload []byte) rawMessage {
287+
func (crypto *defaultCryptoBackend) addPayloadSizeField(rawBytes rawMessage, payload []byte) rawMessage {
281288
fieldSize := getSizeOfPayloadSizeField(payload)
282289
field := make([]byte, 4)
283290
binary.LittleEndian.PutUint32(field, uint32(len(payload)))
@@ -289,7 +296,7 @@ func (crypto defaultCryptoBackend) addPayloadSizeField(rawBytes rawMessage, payl
289296

290297
// appendPadding appends the padding specified in params.
291298
// If no padding is provided in params, then random padding is generated.
292-
func (crypto defaultCryptoBackend) appendPadding(rawBytes, payload []byte, src *ecdsa.PrivateKey) (rawMessage, error) {
299+
func (crypto *defaultCryptoBackend) appendPadding(rawBytes, payload []byte, src *ecdsa.PrivateKey) (rawMessage, error) {
293300
rawSize := flagsLength + getSizeOfPayloadSizeField(payload) + len(payload)
294301
if src != nil {
295302
rawSize += signatureLength
@@ -311,7 +318,7 @@ func (crypto defaultCryptoBackend) appendPadding(rawBytes, payload []byte, src *
311318

312319
// sign calculates and sets the cryptographic signature for the message,
313320
// also setting the sign flag.
314-
func (crypto defaultCryptoBackend) sign(rawBytes rawMessage, key *ecdsa.PrivateKey) (rawMessage, error) {
321+
func (crypto *defaultCryptoBackend) sign(rawBytes rawMessage, key *ecdsa.PrivateKey) (rawMessage, error) {
315322
if isMessageSigned(rawBytes[0]) {
316323
// this should not happen, but no reason to panic
317324
log.Error("failed to sign the message: already signed")

pss/keystore.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (ks *KeyStore) getPeerAddress(keyid string, topic Topic) (PssAddress, error
144144

145145
// Attempt to decrypt, validate and unpack a symmetrically encrypted message.
146146
// If successful, returns the unpacked receivedMessage struct
147-
// encapsulating the decrypted message, and the utils backend id
147+
// encapsulating the decrypted message, and the id
148148
// of the symmetric key used to decrypt the message.
149149
// It fails if decryption of the message fails or if the message is corrupted.
150150
func (ks *KeyStore) processSym(pssMsg *PssMsg) (crypto.ReceivedMessage, string, PssAddress, error) {
@@ -245,8 +245,8 @@ func (ks *KeyStore) GenerateSymmetricKey(topic Topic, address PssAddress, addToC
245245
return keyid, err
246246
}
247247

248-
// Returns a symmetric key byte sequence stored in the utils backend by its unique id.
249-
// Passes on the error value from the utils backend.
248+
// Returns a symmetric key byte sequence stored in the crypto backend by its unique id.
249+
// Passes on the error value from the crypto backend.
250250
func (ks *KeyStore) GetSymmetricKey(symkeyid string) ([]byte, error) {
251251
return ks.Crypto.GetSymKey(symkeyid)
252252
}
@@ -255,13 +255,13 @@ func (ks *KeyStore) GetSymmetricKey(symkeyid string) ([]byte, error) {
255255
//
256256
// This is required for symmetrically encrypted message exchange on the given topic.
257257
//
258-
// The key is stored in the utils backend.
258+
// The key is stored in the crypto backend.
259259
//
260260
// If addtocache is set to true, the key will be added to the cache of keys
261261
// used to attempt symmetric decryption of incoming messages.
262262
//
263263
// Returns a string id that can be used to retrieve the key bytes
264-
// from the utils backend (see pss.GetSymmetricKey())
264+
// from the crypto backend (see pss.GetSymmetricKey())
265265
func (ks *KeyStore) SetSymmetricKey(key []byte, topic Topic, address PssAddress, addtocache bool) (string, error) {
266266
if err := validateAddress(address); err != nil {
267267
return "", err

pss/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func BytesToTopic(b []byte) Topic {
229229
}
230230

231231
// toTopic converts from the byte array representation of a topic
232-
// into the TopicType type.
232+
// into the Topic type.
233233
func toTopic(b []byte) (t Topic) {
234234
sz := TopicLength
235235
if x := len(b); x < TopicLength {

0 commit comments

Comments
 (0)