This repository has been archived by the owner on Dec 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Add encryption mode support #18
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package aes | ||
|
||
import ( | ||
"bytes" | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"errors" | ||
"io" | ||
|
||
"github.com/enj/kms/pkg/encryption" | ||
"github.com/enj/kms/pkg/kek" | ||
) | ||
|
||
func NewAESCBCService(kek kek.KeyEncryptionKeyService) (encryption.EncryptionService, error) { | ||
c := &cbc{kek: kek} | ||
if _, err := c.getBlock(); err != nil { | ||
return nil, err | ||
} | ||
return c, nil | ||
} | ||
|
||
var ( | ||
errInvalidDataLength = errors.New("the stored data was shorter than the required size") | ||
errInvalidBlockSize = errors.New("the stored data is not a multiple of the block size") | ||
errInvalidPKCS7Data = errors.New("invalid PKCS7 data (empty or not padded)") | ||
errInvalidPKCS7Padding = errors.New("invalid padding on input") | ||
) | ||
|
||
type cbc struct { | ||
kek kek.KeyEncryptionKeyService | ||
} | ||
|
||
func (c *cbc) Decrypt(ciphertext []byte) ([]byte, error) { | ||
block, err := c.getBlock() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
blockSize := aes.BlockSize | ||
if len(ciphertext) < blockSize { | ||
return nil, errInvalidDataLength | ||
} | ||
iv := ciphertext[:blockSize] | ||
ciphertext = ciphertext[blockSize:] | ||
|
||
if len(ciphertext)%blockSize != 0 { | ||
return nil, errInvalidBlockSize | ||
} | ||
|
||
result := make([]byte, len(ciphertext)) | ||
copy(result, ciphertext) | ||
mode := cipher.NewCBCDecrypter(block, iv) | ||
mode.CryptBlocks(result, result) | ||
|
||
// remove and verify PKCS#7 padding for CBC | ||
lastPadding := result[len(result)-1] | ||
paddingSize := int(lastPadding) | ||
size := len(result) - paddingSize | ||
if paddingSize == 0 || paddingSize > len(result) { | ||
return nil, errInvalidPKCS7Data | ||
} | ||
for i := 0; i < paddingSize; i++ { | ||
if result[size+i] != lastPadding { | ||
return nil, errInvalidPKCS7Padding | ||
} | ||
} | ||
|
||
return result[:size], nil | ||
} | ||
|
||
func (c *cbc) Encrypt(plaintext []byte) ([]byte, error) { | ||
block, err := c.getBlock() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
blockSize := aes.BlockSize | ||
paddingSize := blockSize - (len(plaintext) % blockSize) | ||
result := make([]byte, blockSize+len(plaintext)+paddingSize) | ||
iv := result[:blockSize] | ||
if _, err := io.ReadFull(rand.Reader, iv); err != nil { | ||
return nil, err | ||
} | ||
copy(result[blockSize:], plaintext) | ||
|
||
// add PKCS#7 padding for CBC | ||
copy(result[blockSize+len(plaintext):], bytes.Repeat([]byte{byte(paddingSize)}, paddingSize)) | ||
|
||
mode := cipher.NewCBCEncrypter(block, iv) | ||
mode.CryptBlocks(result[blockSize:], result[blockSize:]) | ||
return result, nil | ||
} | ||
|
||
func (c *cbc) getBlock() (cipher.Block, error) { | ||
key, err := c.kek.Get() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
block, err := aes.NewCipher(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return block, nil | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package prefix | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"strings" | ||
|
||
"github.com/enj/kms/pkg/encryption" | ||
) | ||
|
||
const ( | ||
kmsName = "ck" | ||
sep = ":" | ||
) | ||
|
||
func NewPrefixEncryption(mode encryption.EncryptionMode, delegate encryption.EncryptionService) encryption.EncryptionService { | ||
modeStr := strings.Join([]string{kmsName, mode.Name, mode.Version}, sep) | ||
return &prefixEncryption{ | ||
prefix: []byte(sep + modeStr + sep), | ||
delegate: delegate, | ||
} | ||
} | ||
|
||
var errInvalidPrefix = errors.New("invalid encryption mode prefix") | ||
|
||
type prefixEncryption struct { | ||
prefix []byte | ||
delegate encryption.EncryptionService | ||
} | ||
|
||
func (p *prefixEncryption) Decrypt(ciphertext []byte) ([]byte, error) { | ||
if !bytes.HasPrefix(ciphertext, p.prefix) { | ||
return nil, errInvalidPrefix | ||
} | ||
return p.delegate.Decrypt(ciphertext[len(p.prefix):]) | ||
} | ||
|
||
func (p *prefixEncryption) Encrypt(plaintext []byte) ([]byte, error) { | ||
result, err := p.delegate.Encrypt(plaintext) | ||
if err != nil { | ||
return nil, err | ||
} | ||
prefixedData := make([]byte, len(p.prefix), len(p.prefix)+len(result)) | ||
copy(prefixedData, p.prefix) | ||
return append(prefixedData, result...), nil | ||
} |
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.
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.
Can you clarify the difference between single and double dash in this case?
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.
There is none (both do the same exact thing). I just really prefer
--
(and it matches howkubectl
,oc
, etc print their flags).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.
I can see if there is a simple way to override how the flags print so they have double dash.
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.
#20 to track