This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from s7techlab/aes-encrypting
AES encrypting - remove bccsp deps
- Loading branch information
Showing
5 changed files
with
141 additions
and
24 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 |
---|---|---|
@@ -1,3 +1,11 @@ | ||
GOFLAGS ?= -mod=vendor | ||
|
||
test: | ||
@echo "go test -mod vendor ./..." | ||
@go test -mod vendor ./... | ||
@go test ./... | ||
|
||
refresh-deps: | ||
@echo "go mod tidy" | ||
@GOFLAGS='' GONOSUMDB=github.com/hyperledger/fabric go mod tidy | ||
@echo "go mod vendor" | ||
@GOFLAGS='' GONOSUMDB=github.com/hyperledger/fabric go mod vendor |
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,126 @@ | ||
package encryption | ||
|
||
import ( | ||
"bytes" | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"io" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// From bccsp/sw/aes | ||
func pkcs7Padding(src []byte) []byte { | ||
padding := aes.BlockSize - len(src)%aes.BlockSize | ||
padtext := bytes.Repeat([]byte{byte(padding)}, padding) | ||
return append(src, padtext...) | ||
} | ||
|
||
func pkcs7UnPadding(src []byte) ([]byte, error) { | ||
length := len(src) | ||
unpadding := int(src[length-1]) | ||
|
||
if unpadding > aes.BlockSize || unpadding == 0 { | ||
return nil, errors.New("Invalid pkcs7 padding (unpadding > aes.BlockSize || unpadding == 0)") | ||
} | ||
|
||
pad := src[len(src)-unpadding:] | ||
for i := 0; i < unpadding; i++ { | ||
if pad[i] != byte(unpadding) { | ||
return nil, errors.New("Invalid pkcs7 padding (pad[i] != unpadding)") | ||
} | ||
} | ||
|
||
return src[:(length - unpadding)], nil | ||
} | ||
|
||
func aesCBCEncrypt(key, s []byte) ([]byte, error) { | ||
return aesCBCEncryptWithRand(rand.Reader, key, s) | ||
} | ||
|
||
func aesCBCEncryptWithIV(IV []byte, key, s []byte) ([]byte, error) { | ||
if len(s)%aes.BlockSize != 0 { | ||
return nil, errors.New("Invalid plaintext. It must be a multiple of the block size") | ||
} | ||
|
||
if len(IV) != aes.BlockSize { | ||
return nil, errors.New("Invalid IV. It must have length the block size") | ||
} | ||
|
||
block, err := aes.NewCipher(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ciphertext := make([]byte, aes.BlockSize+len(s)) | ||
copy(ciphertext[:aes.BlockSize], IV) | ||
|
||
mode := cipher.NewCBCEncrypter(block, IV) | ||
mode.CryptBlocks(ciphertext[aes.BlockSize:], s) | ||
|
||
return ciphertext, nil | ||
} | ||
func aesCBCEncryptWithRand(prng io.Reader, key, s []byte) ([]byte, error) { | ||
if len(s)%aes.BlockSize != 0 { | ||
return nil, errors.New("Invalid plaintext. It must be a multiple of the block size") | ||
} | ||
|
||
block, err := aes.NewCipher(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ciphertext := make([]byte, aes.BlockSize+len(s)) | ||
iv := ciphertext[:aes.BlockSize] | ||
if _, err := io.ReadFull(prng, iv); err != nil { | ||
return nil, err | ||
} | ||
|
||
mode := cipher.NewCBCEncrypter(block, iv) | ||
mode.CryptBlocks(ciphertext[aes.BlockSize:], s) | ||
|
||
return ciphertext, nil | ||
} | ||
|
||
// AESCBCPKCS7Decrypt combines CBC decryption and PKCS7 unpadding | ||
func AESCBCPKCS7Decrypt(key, src []byte) ([]byte, error) { | ||
// First decrypt | ||
pt, err := aesCBCDecrypt(key, src) | ||
if err == nil { | ||
return pkcs7UnPadding(pt) | ||
} | ||
return nil, err | ||
} | ||
|
||
func aesCBCDecrypt(key, src []byte) ([]byte, error) { | ||
block, err := aes.NewCipher(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(src) < aes.BlockSize { | ||
return nil, errors.New("Invalid ciphertext. It must be a multiple of the block size") | ||
} | ||
iv := src[:aes.BlockSize] | ||
src = src[aes.BlockSize:] | ||
|
||
if len(src)%aes.BlockSize != 0 { | ||
return nil, errors.New("Invalid ciphertext. It must be a multiple of the block size") | ||
} | ||
|
||
mode := cipher.NewCBCDecrypter(block, iv) | ||
|
||
mode.CryptBlocks(src, src) | ||
|
||
return src, nil | ||
} | ||
|
||
func EncryptBytes(key, value []byte) ([]byte, error) { | ||
// IV temporary blank | ||
return aesCBCEncryptWithIV(make([]byte, 16), key, pkcs7Padding(value)) | ||
} | ||
|
||
func DecryptBytes(key, value []byte) ([]byte, error) { | ||
return AESCBCPKCS7Decrypt(key, value) | ||
} |
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