2018-09-17 17:36:31 +03:00
|
|
|
/*
|
|
|
|
* This file is subject to the terms and conditions defined in
|
|
|
|
* file 'LICENSE.md', which is part of this source code package.
|
|
|
|
*/
|
|
|
|
|
2018-10-04 04:33:32 +03:00
|
|
|
package security
|
2018-09-16 02:48:08 +03:00
|
|
|
|
2020-04-15 00:09:16 +03:00
|
|
|
import (
|
|
|
|
"crypto/cipher"
|
|
|
|
|
|
|
|
"github.com/unidoc/unipdf/v3/common"
|
|
|
|
)
|
2018-09-16 02:48:08 +03:00
|
|
|
|
2018-09-17 17:36:31 +03:00
|
|
|
// ecb implements an Electronic Codebook encryption mode.
|
|
|
|
// This mode is used to compute or validate document permissions for R=6.
|
2018-09-16 02:48:08 +03:00
|
|
|
type ecb struct {
|
|
|
|
b cipher.Block
|
|
|
|
blockSize int
|
|
|
|
}
|
|
|
|
|
|
|
|
func newECB(b cipher.Block) *ecb {
|
|
|
|
return &ecb{
|
|
|
|
b: b,
|
|
|
|
blockSize: b.BlockSize(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ecbEncrypter ecb
|
|
|
|
|
|
|
|
func newECBEncrypter(b cipher.Block) cipher.BlockMode {
|
|
|
|
return (*ecbEncrypter)(newECB(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
|
|
|
|
|
|
|
|
func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
|
|
|
|
if len(src)%x.blockSize != 0 {
|
2020-04-15 00:09:16 +03:00
|
|
|
common.Log.Error("ERROR: ECB encrypt: input not full blocks")
|
|
|
|
return
|
2018-09-16 02:48:08 +03:00
|
|
|
}
|
|
|
|
if len(dst) < len(src) {
|
2020-04-15 00:09:16 +03:00
|
|
|
common.Log.Error("ERROR: ECB encrypt: output smaller than input")
|
|
|
|
return
|
2018-09-16 02:48:08 +03:00
|
|
|
}
|
|
|
|
for len(src) > 0 {
|
|
|
|
x.b.Encrypt(dst, src[:x.blockSize])
|
|
|
|
src = src[x.blockSize:]
|
|
|
|
dst = dst[x.blockSize:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ecbDecrypter ecb
|
|
|
|
|
|
|
|
func newECBDecrypter(b cipher.Block) cipher.BlockMode {
|
|
|
|
return (*ecbDecrypter)(newECB(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *ecbDecrypter) BlockSize() int { return x.blockSize }
|
|
|
|
|
|
|
|
func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
|
|
|
|
if len(src)%x.blockSize != 0 {
|
2020-04-15 00:09:16 +03:00
|
|
|
common.Log.Error("ERROR: ECB decrypt: input not full blocks")
|
|
|
|
return
|
2018-09-16 02:48:08 +03:00
|
|
|
}
|
|
|
|
if len(dst) < len(src) {
|
2020-04-15 00:09:16 +03:00
|
|
|
common.Log.Error("ERROR: ECB decrypt: output smaller than input")
|
|
|
|
return
|
2018-09-16 02:48:08 +03:00
|
|
|
}
|
|
|
|
for len(src) > 0 {
|
|
|
|
x.b.Decrypt(dst, src[:x.blockSize])
|
|
|
|
src = src[x.blockSize:]
|
|
|
|
dst = dst[x.blockSize:]
|
|
|
|
}
|
|
|
|
}
|