Skip to content

Commit

Permalink
remove gokrb5 dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmturner committed Sep 29, 2017
1 parent c06fdb5 commit f6abebb
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions aescts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"crypto/cipher"
"errors"
"fmt"
"github.com/jcmturner/gokrb5/crypto/common"
)

// Encrypt the message with the key and the initial vector.
Expand All @@ -31,7 +30,7 @@ func Encrypt(key, iv, plaintext []byte) ([]byte, []byte, error) {
subsequent encryption is the next-to-last block of the encryption
output; this is the encrypted form of the last plaintext block.*/
if l <= aes.BlockSize {
m, _ = common.ZeroPad(m, aes.BlockSize)
m, _ = zeroPad(m, aes.BlockSize)
mode.CryptBlocks(m, m)
return m, m, nil
}
Expand All @@ -41,7 +40,7 @@ func Encrypt(key, iv, plaintext []byte) ([]byte, []byte, error) {
rb, _ := swapLastTwoBlocks(m, aes.BlockSize)
return iv, rb, nil
}
m, _ = common.ZeroPad(m, aes.BlockSize)
m, _ = zeroPad(m, aes.BlockSize)
rb, pb, lb, err := tailBlocks(m, aes.BlockSize)
if err != nil {
return []byte{}, []byte{}, fmt.Errorf("Error tailing blocks: %v", err)
Expand Down Expand Up @@ -169,3 +168,19 @@ func swapLastTwoBlocks(b []byte, c int) ([]byte, error) {
out = append(out, pb...)
return out, nil
}

// zeroPad pads bytes with zeros to nearest multiple of message size m.
func zeroPad(b []byte, m int) ([]byte, error) {
if m <= 0 {
return nil, errors.New("Invalid message block size when padding")
}
if b == nil || len(b) == 0 {
return nil, errors.New("Data not valid to pad: Zero size")
}
if l := len(b) % m; l != 0 {
n := m - l
z := make([]byte, n)
b = append(b, z...)
}
return b, nil
}

0 comments on commit f6abebb

Please sign in to comment.