summaryrefslogtreecommitdiff
path: root/vendor/github.com/dvsekhvalnov/jose2go/aes/ecb.go
blob: ec9af99dce0ba36d019de610130838c1176cfc9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Package aes contains provides AES Key Wrap and ECB mode implementations
package aes

import (
	"crypto/cipher"
)

type ecb struct {
	b cipher.Block
}

type ecbEncrypter ecb
type ecbDecrypter ecb

// NewECBEncrypter creates BlockMode for AES encryption in ECB mode
func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
	return &ecbEncrypter{b: b}
}

// NewECBDecrypter creates BlockMode for AES decryption in ECB mode
func NewECBDecrypter(b cipher.Block) cipher.BlockMode {
	return &ecbDecrypter{b: b}
}

func (x *ecbEncrypter) BlockSize() int { return x.b.BlockSize() }
func (x *ecbDecrypter) BlockSize() int { return x.b.BlockSize() }

func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
	bs := x.BlockSize()

	if len(src)%bs != 0 {
		panic("ecbDecrypter.CryptBlocks(): input not full blocks")
	}

	if len(dst) < len(src) {
		panic("ecbDecrypter.CryptBlocks(): output smaller than input")
	}

	if len(src) == 0 {
		return
	}

	for len(src) > 0 {
		x.b.Decrypt(dst, src)
		src = src[bs:]
	}
}

func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
	bs := x.BlockSize()

	if len(src)%bs != 0 {
		panic("ecbEncrypter.CryptBlocks(): input not full blocks")
	}

	if len(dst) < len(src) {
		panic("ecbEncrypter.CryptBlocks(): output smaller than input")
	}

	if len(src) == 0 {
		return
	}

	for len(src) > 0 {
		x.b.Encrypt(dst, src)
		src = src[bs:]
	}
}