forked from meshbird/meshbird
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed the previous encryption with AES/GCM. Altough is not perfect,…
… the implementation from Google is pretty good. Besides, to use the same string for IV and for Key, using CBC, was not a good idea: the IV is usually attached to the encrypted payload. So you attach the key to each packet. Please notice, the tests (crypt_test.go) will fail because the signature of function has changed.(EncryptIV , DecryptIV) Unfortunately I wasn't able to understand this piece of code and what it was aiming to, so I didn't changed.
- Loading branch information
LowEel
committed
Jul 2, 2017
1 parent
4474ad3
commit f4e50d5
Showing
4 changed files
with
58 additions
and
40 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
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 |
---|---|---|
@@ -1,43 +1,59 @@ | ||
package secure | ||
|
||
import ( | ||
"bytes" | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"io" | ||
"log" | ||
) | ||
|
||
func EncryptIV(decrypted []byte, key []byte, iv []byte) ([]byte, error) { | ||
ac, err := aes.NewCipher(key) | ||
func EncryptIV(decrypted []byte, key []byte) ([]byte, error) { | ||
|
||
c, err := aes.NewCipher(key) | ||
if err != nil { | ||
log.Println("[CRYPT][AES][ENC] Problem %s", err.Error()) | ||
return nil, err | ||
} | ||
c := cipher.NewCBCEncrypter(ac, iv) | ||
decrypted = PKCS5Padding(decrypted, ac.BlockSize()) | ||
encrypted := make([]byte, len(decrypted)) | ||
c.CryptBlocks(encrypted, decrypted) | ||
return encrypted, nil | ||
} | ||
|
||
func DecryptIV(encrypted []byte, key []byte, iv []byte) ([]byte, error) { | ||
ac, err := aes.NewCipher(key) | ||
gcm, err := cipher.NewGCM(c) | ||
if err != nil { | ||
log.Println("[CRYPT][AES][ENC] Problem %s", err.Error()) | ||
return nil, err | ||
} | ||
|
||
nonce := make([]byte, gcm.NonceSize()) | ||
if _, err = io.ReadFull(rand.Reader, nonce); err != nil { | ||
log.Println("[CRYPT][AES][NONCE] Problem %s", err.Error()) | ||
return nil, err | ||
} | ||
c := cipher.NewCBCDecrypter(ac, iv) | ||
decrypted := make([]byte, len(encrypted)) | ||
c.CryptBlocks(decrypted, encrypted) | ||
decrypted = PKCS5UnPadding(decrypted) | ||
return decrypted, nil | ||
} | ||
|
||
func PKCS5Padding(src []byte, blockSize int) []byte { | ||
padding := blockSize - len(src)%blockSize | ||
padtext := bytes.Repeat([]byte{byte(padding)}, padding) | ||
return append(src, padtext...) | ||
return gcm.Seal(nonce, nonce, decrypted, nil), nil | ||
|
||
} | ||
|
||
func PKCS5UnPadding(src []byte) []byte { | ||
length := len(src) | ||
unpadding := int(src[length-1]) | ||
return src[:(length - unpadding)] | ||
func DecryptIV(ciphertext []byte, key []byte) ([]byte, error) { | ||
|
||
c, err := aes.NewCipher(key) | ||
if err != nil { | ||
log.Println("[DECRYPT][AES] Problem %s", err.Error()) | ||
return nil, err | ||
} | ||
|
||
gcm, err := cipher.NewGCM(c) | ||
if err != nil { | ||
log.Println("[DECRYPT][AES] Problem %s", err.Error()) | ||
return nil, err | ||
} | ||
|
||
nonceSize := gcm.NonceSize() | ||
if len(ciphertext) < nonceSize { | ||
log.Println("[DECRYPT][AES] Problem %s", "Cyphertext too short") | ||
return nil, err | ||
} | ||
|
||
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:] | ||
|
||
return gcm.Open(nil, nonce, ciphertext, 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