-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change vault to use GCM with counter (#178)
* Starting converting encryption to GCM. * Using base64 encodetostring * Almost working.. * Encrypt - decrypt is now working. * Encryption and password protection is working. * Padding is no longer needed. * Increase coverage and made sure the counter and decoding is working. * Working on legacy decrypt * Working on new password and testing the old vault decrypt. * All works with the fall back. * Added back the removal of GAIA check. * Using keysize and extracted legacy code into its own file.
- Loading branch information
Showing
5 changed files
with
283 additions
and
62 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package security | ||
|
||
import ( | ||
"bytes" | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"encoding/base64" | ||
"errors" | ||
|
||
"github.com/gaia-pipeline/gaia" | ||
) | ||
|
||
const ( | ||
secretCheckKey = "GAIA_CHECK_SECRET" | ||
secretCheckValue = "!CHECK_ME!" | ||
) | ||
|
||
func (v *Vault) legacyDecrypt(data []byte) ([]byte, error) { | ||
if len(data) < 1 { | ||
gaia.Cfg.Logger.Info("the vault is empty") | ||
return []byte{}, nil | ||
} | ||
paddedPassword := v.pad(v.cert) | ||
ci := base64.URLEncoding.EncodeToString(paddedPassword) | ||
block, err := aes.NewCipher([]byte(ci[:aes.BlockSize])) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
|
||
decodedMsg, err := base64.URLEncoding.DecodeString(string(data)) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
|
||
if (len(decodedMsg) % aes.BlockSize) != 0 { | ||
return []byte{}, errors.New("blocksize must be multiple of decoded message length") | ||
} | ||
|
||
iv := decodedMsg[:aes.BlockSize] | ||
msg := decodedMsg[aes.BlockSize:] | ||
|
||
cfb := cipher.NewCFBDecrypter(block, iv) | ||
cfb.XORKeyStream(msg, msg) | ||
|
||
unpadMsg, err := v.unpad(msg) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
|
||
if !bytes.Contains(unpadMsg, []byte(secretCheckValue)) { | ||
return []byte{}, errors.New("possible mistyped password") | ||
} | ||
return unpadMsg, nil | ||
} | ||
|
||
// Pad pads the src with 0x04 until block length. | ||
func (v *Vault) pad(src []byte) []byte { | ||
padding := aes.BlockSize - len(src)%aes.BlockSize | ||
padtext := bytes.Repeat([]byte{byte(padding)}, padding) | ||
return append(src, padtext...) | ||
} | ||
|
||
// Unpad removes the padding from pad. | ||
func (v *Vault) unpad(src []byte) ([]byte, error) { | ||
length := len(src) | ||
unpadding := int(src[length-1]) | ||
|
||
if unpadding > length { | ||
return nil, errors.New("possible mistyped password") | ||
} | ||
|
||
return src[:(length - unpadding)], 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-----BEGIN PRIVATE KEY----- | ||
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDVCwxvazrjIbap | ||
k4fP0gFThXS1xaWNucwp47w0xMCH0mOi5B1JGfHlRAJmfQOn4jqzEm+2nTzwQmR/ | ||
EN0wV5MPWg1cLTiSpDrFN9nmkJZiZiN5NROJpUPw4sozIkSBli055TSaNErf2KHF | ||
1lylR4ZyMV313zS+hb5wPfzbSPrmey6f8eY6b1FoS/5caTGweoxk764qRm6UU25Z | ||
cfGaTfpI3KsgN+Vot9qjSfdI7dDs9jRCf7BYCPhm0DfH9e3JBC/HyRQrbxghpjqQ | ||
8Van3a8OIWeV/AV1BI2gTrzeCWIAhatNAfjRaEVIpTU+1KJc+bGlxUKldqviGICC | ||
YKFnxLSTAgMBAAECggEAHS9gSrsz2/24Wk69ojiwudJkhKpI3buAPpTWKZxyi6jE | ||
wYHiiSsmujOw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
Z0dOjF6sSaiAnx7W2+0aKMRZOhtChlCq0SFvqTvKB7X9UvEVFSyfGEBOawiNXWDu | ||
PHeDB1DWcqToDTKqS5V4dqS+3ZaMHjvX39YhnjLCS0c3nofVWW+Pay11cC9i8tV1 | ||
OILObT2tga2txO4JxUrs+NU= | ||
-----END PRIVATE KEY----- |
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 @@ | ||
0xn40kNax9KkbKhUZZGYmIUZJaFDQ1ajEkyB1OgnRf6MSUhe2RK8xqlVj9mBcTMVin3_UPBlXMCrrDt5fbw0kw== |
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
Oops, something went wrong.