Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
client
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Automate
Agent sessions
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
client
Commits
d639eea8
Commit
d639eea8
authored
Oct 21, 2022
by
Josh Brooks
Browse files
Options
Downloads
Patches
Plain Diff
Add better documentation for channels DB cipher in bindings
parent
4d25a8f6
No related branches found
No related tags found
3 merge requests
!510
Release
,
!422
Add cipher bindings
,
!340
Project/channels
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
bindings/channels.go
+46
-34
46 additions, 34 deletions
bindings/channels.go
with
46 additions
and
34 deletions
bindings/channels.go
+
46
−
34
View file @
d639eea8
...
...
@@ -1390,42 +1390,42 @@ func (tem *toEventModel) UpdateSentStatus(uuid uint64,
}
////////////////////////////////////////////////////////////////////////////////
// Channel Cipher //
// Channel
ChannelDb
Cipher //
////////////////////////////////////////////////////////////////////////////////
// Cipher is the bindings layer representation of the [channel.Cipher].
type
Cipher
struct
{
//
ChannelDb
Cipher is the bindings layer representation of the [channel.Cipher].
type
ChannelDb
Cipher
struct
{
api
cryptoChannel
.
Cipher
salt
[]
byte
id
int
}
// cipherTrackerSingleton is used to track Cipher objects
// c
hannelDbC
ipherTrackerSingleton is used to track
ChannelDb
Cipher objects
// so that they can be referenced by ID back over the bindings.
var
cipherTrackerSingleton
=
&
cipherTracker
{
tracked
:
make
(
map
[
int
]
*
Cipher
),
var
c
hannelDbC
ipherTrackerSingleton
=
&
c
hannelDbC
ipherTracker
{
tracked
:
make
(
map
[
int
]
*
ChannelDb
Cipher
),
count
:
0
,
}
// cipherTracker is a singleton used to keep track of extant
// Cipher objects, preventing race conditions created by passing it
// c
hannelDbC
ipherTracker is a singleton used to keep track of extant
//
ChannelDb
Cipher objects, preventing race conditions created by passing it
// over the bindings.
type
cipherTracker
struct
{
tracked
map
[
int
]
*
Cipher
type
c
hannelDbC
ipherTracker
struct
{
tracked
map
[
int
]
*
ChannelDb
Cipher
count
int
mux
sync
.
RWMutex
}
// make create a Cipher from a [channel.Cipher], assigns it a unique
// ID, and adds it to the cipherTracker.
func
(
ct
*
cipherTracker
)
make
(
c
cryptoChannel
.
Cipher
)
*
Cipher
{
// make create a
ChannelDb
Cipher from a [channel.Cipher], assigns it a unique
// ID, and adds it to the c
hannelDbC
ipherTracker.
func
(
ct
*
c
hannelDbC
ipherTracker
)
make
(
c
cryptoChannel
.
Cipher
)
*
ChannelDb
Cipher
{
ct
.
mux
.
Lock
()
defer
ct
.
mux
.
Unlock
()
chID
:=
ct
.
count
ct
.
count
++
ct
.
tracked
[
chID
]
=
&
Cipher
{
ct
.
tracked
[
chID
]
=
&
ChannelDb
Cipher
{
api
:
c
,
id
:
chID
,
}
...
...
@@ -1433,31 +1433,39 @@ func (ct *cipherTracker) make(c cryptoChannel.Cipher) *Cipher {
return
ct
.
tracked
[
chID
]
}
// get an Cipher from the cipherTracker given its ID.
func
(
ct
*
cipherTracker
)
get
(
id
int
)
(
*
Cipher
,
error
)
{
// get an
ChannelDb
Cipher from the c
hannelDbC
ipherTracker given its ID.
func
(
ct
*
c
hannelDbC
ipherTracker
)
get
(
id
int
)
(
*
ChannelDb
Cipher
,
error
)
{
ct
.
mux
.
RLock
()
defer
ct
.
mux
.
RUnlock
()
c
,
exist
:=
ct
.
tracked
[
id
]
if
!
exist
{
return
nil
,
errors
.
Errorf
(
"Cannot get Cipher for ID %d, does not exist"
,
id
)
"Cannot get
ChannelDb
Cipher for ID %d, does not exist"
,
id
)
}
return
c
,
nil
}
// delete removes a Cipher from the cipherTracker.
func
(
ct
*
cipherTracker
)
delete
(
id
int
)
{
// delete removes a
ChannelDb
Cipher from the c
hannelDbC
ipherTracker.
func
(
ct
*
c
hannelDbC
ipherTracker
)
delete
(
id
int
)
{
ct
.
mux
.
Lock
()
defer
ct
.
mux
.
Unlock
()
delete
(
ct
.
tracked
,
id
)
}
// NewCipher constructs a Cipher object.
func
NewCipher
(
cmixID
int
,
password
string
,
plaintTextBlockSize
int
)
(
*
Cipher
,
error
)
{
// NewChannelsDatabaseCipher constructs a ChannelDbCipher object.
//
// Parameters:
// - cmixID - The tracked [Cmix] object ID.
// - password - The password for storage. This should be the same password
// passed into [NewCmix].
// - plaintTextBlockSize - The maximum size of a payload to be encrypted.
// A payload passed into [ChannelDbCipher.Encrypt] that is larger than
// plaintTextBlockSize will result in an error.
func
NewChannelsDatabaseCipher
(
cmixID
int
,
password
[]
byte
,
plaintTextBlockSize
int
)
(
*
ChannelDbCipher
,
error
)
{
// Get user from singleton
user
,
err
:=
cmixTrackerSingleton
.
get
(
cmixID
)
if
err
!=
nil
{
...
...
@@ -1475,29 +1483,33 @@ func NewCipher(cmixID int, password string,
}
// Construct a cipher
c
,
err
:=
cryptoChannel
.
NewCipher
(
[]
byte
(
password
)
,
salt
,
c
,
err
:=
cryptoChannel
.
NewCipher
(
password
,
salt
,
plaintTextBlockSize
,
stream
)
if
err
!=
nil
{
return
nil
,
err
}
// Return a cipher
return
cipherTrackerSingleton
.
make
(
c
),
nil
return
c
hannelDbC
ipherTrackerSingleton
.
make
(
c
),
nil
}
// Encrypt will encrypt the raw data. The returned ciphertext includes the
// nonce (24 bytes) and the encrypted plaintext (with possible padding, if
// needed). Prior to encryption the plaintext has been appended with
// padding if the byte data is shorted than the pre-defined block size
// passed into NewCipher. If plaintext longer than this pre-defined block
// size is passed in, Encrypt will return an error.
func
(
c
*
Cipher
)
Encrypt
(
plaintext
[]
byte
)
([]
byte
,
error
)
{
// Encrypt will encrypt the raw data. It will return a ciphertext. Padding is
// done on the plaintext so all encrypted data looks uniform at rest.
//
// Parameters:
// - plaintext - The data to be encrypted. This must be smaller than the block
// size passed into [NewChannelsDatabaseCipher]. If it is larger, this will
// return an error.
func
(
c
*
ChannelDbCipher
)
Encrypt
(
plaintext
[]
byte
)
([]
byte
,
error
)
{
return
c
.
api
.
Encrypt
(
plaintext
)
}
// Decrypt will decrypt the passed in encrypted value. The plaintext will
// be returned by this function. If the plaintext was padded, those
// modifications will be discarded prior to returning.
func
(
c
*
Cipher
)
Decrypt
(
ciphertext
[]
byte
)
([]
byte
,
error
)
{
// be returned by this function. Any padding will be discarded within
// this function.
//
// Parameters:
// - ciphertext - the encrypted data returned by [ChannelDbCipher.Encrypt].
func
(
c
*
ChannelDbCipher
)
Decrypt
(
ciphertext
[]
byte
)
([]
byte
,
error
)
{
return
c
.
api
.
Decrypt
(
ciphertext
)
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment