-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
530 additions
and
8 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,68 @@ | ||
package hdfs | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"encoding/binary" | ||
"fmt" | ||
) | ||
|
||
const ( | ||
// in FileWriter we use chunks upto aesChunkSize bytes to encrypt data | ||
aesChunkSize = 1024 * 1024 | ||
) | ||
|
||
// calculateIV `shifts` IV to given offset | ||
// based on calculateIV from AesCtrCryptoCodec.java | ||
func calculateIV(offset int64, initIV []byte) ([]byte, error) { | ||
if len(initIV) != aes.BlockSize { | ||
return nil, fmt.Errorf("calculateIV: invalid iv size: %v", len(initIV)) | ||
} | ||
|
||
counter := offset / aes.BlockSize | ||
iv := make([]byte, aes.BlockSize) | ||
|
||
high := binary.BigEndian.Uint64(initIV[:8]) | ||
low := binary.BigEndian.Uint64(initIV[8:]) | ||
origLow := low | ||
|
||
low += uint64(counter) | ||
if low < origLow { // wrap | ||
high += 1 | ||
} | ||
|
||
binary.BigEndian.PutUint64(iv, high) | ||
binary.BigEndian.PutUint64(iv[8:], low) | ||
|
||
return iv, nil | ||
} | ||
|
||
// aesCtrStep perform AES-CTR XOR operation on given byte string. | ||
// Once encryption and decryption are exactly the same operation for CTR mode, | ||
// this function can be used to perform both. | ||
func aesCtrStep(offset int64, enc *TransparentEncryptionInfo, b []byte) ([]byte, error) { | ||
iv, err := calculateIV(offset, enc.iv) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if enc.cipher == nil { | ||
cipher, err := aes.NewCipher(enc.key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
enc.cipher = cipher | ||
} | ||
|
||
stream := cipher.NewCTR(enc.cipher, iv) | ||
|
||
padding := offset % aes.BlockSize | ||
if padding > 0 { | ||
tmp := make([]byte, padding) | ||
stream.XORKeyStream(tmp, tmp) | ||
} | ||
|
||
text := make([]byte, len(b)) | ||
stream.XORKeyStream(text, b) | ||
return text, 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,50 @@ | ||
package hdfs | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAesChunks(t *testing.T) { | ||
originalText := []byte("some random plain text, nice to have it quite long") | ||
key := []byte("0123456789abcdef") | ||
|
||
// Choose iv to hit counter overflow. | ||
iv := []byte("\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xf5") | ||
enc := &TransparentEncryptionInfo{iv: iv, key: key} | ||
|
||
// Ensure that we can decrypt text after encryption. | ||
// In CTR mode, implementation for `encrypt` and `decrypt` actually the same | ||
// since we just XOR on input. | ||
encryptedText, err := aesCtrStep(0, enc, originalText) | ||
assert.Equal(t, err, nil) | ||
decryptedText, err := aesCtrStep(0, enc, encryptedText) | ||
assert.Equal(t, err, nil) | ||
assert.Equal(t, originalText, decryptedText) | ||
|
||
// CTR mode allow us to encrypt/decrypt string by chunks | ||
// (using correct offset from start of string). | ||
// Ensure that result equal to one, produced in one step. | ||
encryptedByChunks := make([]byte, 0) | ||
var pos int64 = 0 | ||
for _, x := range []int{5, 7, 6, 4, 28} { | ||
tmp, err := aesCtrStep(pos, enc, originalText[pos:pos+int64(x)]) | ||
assert.Equal(t, err, nil) | ||
encryptedByChunks = append(encryptedByChunks, tmp...) | ||
pos += int64(x) | ||
} | ||
assert.Equal(t, encryptedByChunks, encryptedText) | ||
|
||
// Decrypt string by chunks. | ||
// Ensure that result equal to one, produced in one step. | ||
decryptedByChunks := make([]byte, 0) | ||
pos = 0 | ||
for _, x := range []int{5, 7, 6, 4, 28} { | ||
tmp, err := aesCtrStep(pos, enc, encryptedText[pos:pos+int64(x)]) | ||
assert.Equal(t, err, nil) | ||
decryptedByChunks = append(decryptedByChunks, tmp...) | ||
pos += int64(x) | ||
} | ||
assert.Equal(t, decryptedByChunks, originalText) | ||
} |
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
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.