New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add encrypted stream #133
Merged
Merged
Add encrypted stream #133
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WIP: API+functionality review. Tests are missing.
# Conflicts: # go.mod
klauspost
force-pushed
the
add-encrypted-stream
branch
from
October 6, 2022 12:07
db33245
to
7d555ca
Compare
klauspost
force-pushed
the
add-encrypted-stream
branch
from
October 6, 2022 13:33
e2df0ea
to
eb54b8f
Compare
klauspost
force-pushed
the
add-encrypted-stream
branch
3 times, most recently
from
October 7, 2022 11:03
0c058c7
to
aecd72b
Compare
klauspost
force-pushed
the
add-encrypted-stream
branch
from
October 7, 2022 11:09
aecd72b
to
b63f9c2
Compare
# Conflicts: # go.mod # go.sum
vadmeste
approved these changes
Oct 21, 2022
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
klauspost
force-pushed
the
add-encrypted-stream
branch
from
October 25, 2022 11:05
4ee542b
to
98d0a81
Compare
harshavardhana
approved these changes
Oct 25, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Encrypted Streams
This package provides a flexible way to merge multiple streams with controlled encryption.
Features
Usage
Create a writer that will write the stream.
You must provide an
io.Writer
to which the output is written.Once all streams have been written it should be closed to indicate end of payload.
It is possible to signal an error to the receiver using
w.AddError(msg string)
.This will return the error to the receiver.
Adding keys
Keys for streams must be added. The keys themselves are 32 bytes of random data,
but it must be specified how they are stored.
They can be added as plain text, which isn't secure,
but allows later encryption using a public key.
To add a key without encryption use
w.AddKeyPlain()
which will add the keys to the stream.
To add an encrypted key provide a 2048 bit public RSA key.
Use
w.AddKeyEncrypted(publicKey)
to add a key to the stream.Once a key has been sent on the stream it will be used for all subsequent encrypted streams.
This means that different keys with different private/public keys can be sent for different streams.
Sending streams
Streams are added using either
w.AddEncryptedStream
orw.AddUnencryptedStream
.A string identifier can be used to identify each stream when reading.
An optional byte block can also be sent.
Note that neither the name nor the byte block is encrypted,
so they should not contain sensitive data.
The functions above return an
io.WriteCloser
.Data for this stream should be written to this interface
and
Close()
should be called before another stream can be added.Reading Streams
To read back data
r, err := estream.NewReader(input)
can be used for create a Reader.To set a private key, use
r.SetPrivateKey(key)
to set a single private key.For multiple keys a key provider can be made to return the appropriate key:
It is possible to skip streams that cannot be decrypted using
r.SkipEncrypted(true)
.A simple for loop can be used to get all streams:
Replacing keys
It is possible to replace public keys needed for decryption using
estream.ReplaceKeys()
.For encrypted keys the private key must be provided and optionally unencrypted keys can also be
encrypted using a public key.
Format
Header
Format starts with 2 version bytes.
Unknown major versions should be rejected by the decoder,
however minor versions are assumed to be compatible,
but may contain data that will be ignored by older versions.
Blocks
Each block is preceded by a messagepack encoded int8 indicating the block type.
Positive types must be parsed by the decoder. Negative types are skippable blocks.
Blocks have their length encoded as a messagepack unsigned integer following the block ID.
This indicates the number of bytes to skip after the length to reach the next block ID.
Maximum block size is 2^32-1 (4294967295) bytes.
All block content is messagepack encoded.
id 1: Plain Key
This block contains an unencrypted key that is used for all following streams.
Multiple keys can be sent, but only the latest key should be used to decrypt a stream.
id 2: RSA Encrypted Key
This block contains an RSA encrypted key that is used for all following streams.
Multiple keys can be sent, but only the latest key should be used to decrypt a stream.
The cipher key is encrypted with RSA-OAEP using SHA-512.
id 3: SIO Encrypted Stream
Start of stream encrypted using sio-go.
Stream will be encrypted using
AES_256_GCM
using the last key provided on stream.The stream consists of all data blocks following until "End Of Stream" block is sent.
Checksum is of encrypted data.
There is no checksum for decrypted data.
id 4: Plain Stream
Start of unencrypted stream.
The stream consists of all data blocks following until "End Of Stream" block is sent.
id 5: Data Block
Data contains a data block.
If block is part of an encrypted stream it should be sent to the stream decrypter as is.
id 6: End Of Stream
Indicates successful end of individual stream.
No more data blocks should be expected before new stream information is sent.
id 7: EOF
Indicates successful end of all streams.
id 8: Error
An error block can be sent to indicate an error occurred while generating the stream.
It is expected that the parser returns the message and stops processing.
Checksum types
Version History