12 // aesCtrCipherBuilder for building ContentCipher
13 type aesCtrCipherBuilder struct {
14 MasterCipher MasterCipher
17 // aesCtrCipher will use aes ctr algorithm
18 type aesCtrCipher struct {
23 // CreateAesCtrCipher creates ContentCipherBuilder
24 func CreateAesCtrCipher(cipher MasterCipher) ContentCipherBuilder {
25 return aesCtrCipherBuilder{MasterCipher: cipher}
28 // createCipherData create CipherData for encrypt object data
29 func (builder aesCtrCipherBuilder) createCipherData() (CipherData, error) {
32 err = cd.RandomKeyIv(aesKeySize, ivSize)
37 cd.WrapAlgorithm = builder.MasterCipher.GetWrapAlgorithm()
38 cd.CEKAlgorithm = AesCtrAlgorithm
39 cd.MatDesc = builder.MasterCipher.GetMatDesc()
42 cd.EncryptedKey, err = builder.MasterCipher.Encrypt(cd.Key)
48 cd.EncryptedIV, err = builder.MasterCipher.Encrypt(cd.IV)
56 // contentCipherCD is used to create ContentCipher with CipherData
57 func (builder aesCtrCipherBuilder) contentCipherCD(cd CipherData) (ContentCipher, error) {
58 cipher, err := newAesCtr(cd)
69 // ContentCipher is used to create ContentCipher interface
70 func (builder aesCtrCipherBuilder) ContentCipher() (ContentCipher, error) {
71 cd, err := builder.createCipherData()
75 return builder.contentCipherCD(cd)
78 // ContentCipherEnv is used to create a decrption ContentCipher from Envelope
79 func (builder aesCtrCipherBuilder) ContentCipherEnv(envelope Envelope) (ContentCipher, error) {
81 cd.EncryptedKey = make([]byte, len(envelope.CipherKey))
82 copy(cd.EncryptedKey, []byte(envelope.CipherKey))
84 plainKey, err := builder.MasterCipher.Decrypt([]byte(envelope.CipherKey))
88 cd.Key = make([]byte, len(plainKey))
89 copy(cd.Key, plainKey)
91 cd.EncryptedIV = make([]byte, len(envelope.IV))
92 copy(cd.EncryptedIV, []byte(envelope.IV))
94 plainIV, err := builder.MasterCipher.Decrypt([]byte(envelope.IV))
99 cd.IV = make([]byte, len(plainIV))
102 cd.MatDesc = envelope.MatDesc
103 cd.WrapAlgorithm = envelope.WrapAlg
104 cd.CEKAlgorithm = envelope.CEKAlg
106 return builder.contentCipherCD(cd)
109 // GetMatDesc is used to get MasterCipher's MatDesc
110 func (builder aesCtrCipherBuilder) GetMatDesc() string {
111 return builder.MasterCipher.GetMatDesc()
114 // EncryptContents will generate a random key and iv and encrypt the data using ctr
115 func (cc *aesCtrCipher) EncryptContent(src io.Reader) (io.ReadCloser, error) {
116 reader := cc.Cipher.Encrypt(src)
117 return &CryptoEncrypter{Body: src, Encrypter: reader}, nil
120 // DecryptContent is used to decrypt object using ctr
121 func (cc *aesCtrCipher) DecryptContent(src io.Reader) (io.ReadCloser, error) {
122 reader := cc.Cipher.Decrypt(src)
123 return &CryptoDecrypter{Body: src, Decrypter: reader}, nil
126 // GetCipherData is used to get cipher data information
127 func (cc *aesCtrCipher) GetCipherData() *CipherData {
128 return &(cc.CipherData)
131 // GetCipherData returns cipher data
132 func (cc *aesCtrCipher) GetEncryptedLen(plainTextLen int64) int64 {
133 // AES CTR encryption mode does not change content length
137 // GetAlignLen is used to get align length
138 func (cc *aesCtrCipher) GetAlignLen() int {
139 return len(cc.CipherData.IV)
142 // Clone is used to create a new aesCtrCipher from itself
143 func (cc *aesCtrCipher) Clone(cd CipherData) (ContentCipher, error) {
144 cipher, err := newAesCtr(cd)
149 return &aesCtrCipher{