summaryrefslogtreecommitdiffstats
path: root/lib/blocks.go
blob: 76f34d3d5918070def77c123b7e78e90fbc09d70 (plain) (blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright © 2021 siddharth ravikumar <s@ricketyspace.net>
// SPDX-License-Identifier: ISC

package lib

// Breaks 'cb' into blocks of 'keysize'
func BreakIntoBlocks(cb []byte, keysize int) [][]byte {
	if len(cb) < 1 {
		return make([][]byte, 0)
	}

	// Compute the number of blocks.
	nb := len(cb) / keysize
	if len(cb)%keysize != 0 {
		nb += 1
	}
	blocks := make([][]byte, nb)

	for i, j, k := 0, 0, 0; i < len(cb); i++ {
		if len(blocks[k]) == 0 {
			blocks[k] = make([]byte, keysize)
		}
		blocks[k][j] = cb[i]

		j += 1
		if j == keysize {
			j = 0
			k += 1
		}
	}
	return blocks
}

func TransposeBlocks(blocks [][]byte, keysize int) [][]byte {
	if len(blocks) < 1 {
		return make([][]byte, 0)
	}

	tblocks := make([][]byte, keysize)
	for i := 0; i < len(blocks); i++ {
		for j := 0; j < len(blocks[i]); j++ {
			if len(tblocks[j]) == 0 {
				tblocks[j] = make([]byte, len(blocks))
			}
			tblocks[j][i] = blocks[i][j]
		}
	}
	return tblocks
}

func BlocksEqual(a, b []byte) bool {
	if len(a) != len(b) {
		return false
	}

	for i := 0; i < len(a); i++ {
		if a[i] != b[i] {
			return false
		}
	}
	return true
}

func CipherUsesECB(bs []byte) []byte {
	blocks := BreakIntoBlocks(bs, 16)

	for i := 0; i < len(blocks); i++ {
		if hasMatchingBlock(i, blocks) {
			return blocks[i]
		}
	}
	return nil
}

// Returns (index, found); where `index` is the starting index of the
// consecutive matching blocks; `index` is -1 when consective matching
// blocks are not found.
func HasConsecutiveMatchingBlocks(bs []byte, bsize int) (int, bool) {
	blocks := BreakIntoBlocks(bs, bsize)

	for i := 0; i < len(blocks)-1; i++ {
		if BlocksEqual(blocks[i], blocks[i+1]) {
			return i * bsize, true
		}
	}
	return -1, false
}

func hasMatchingBlock(id int, blocks [][]byte) bool {
	for i := 0; i < len(blocks); i++ {
		if i == id {
			continue
		}
		if BlocksEqual(blocks[i], blocks[id]) {
			return true
		}
	}
	return false
}

// Performs PKCS#7 Padding on the input `in` and block size `k`.
// Assumes 0 > `k` < 256
// Reference: https://tools.ietf.org/html/rfc5652#section-6.3
func Pkcs7Padding(in []byte, k int) []byte {
	lth := len(in)
	pd := k - (lth % k) // padding character and padding length

	for i := 0; i < pd; i++ {
		in = append(in, byte(pd))
	}
	return in
}

// Removes PKCS#7 Padding from input `in`
func Pkcs7PaddingUndo(in []byte) ([]byte, error) {
	pd := in[len(in)-1] // padding character
	pl := int(pd)       // padding length

	// Validate
	for i := len(in) - 1; i >= len(in)-pl; i-- {
		if in[i] != pd {
			return []byte{}, CPError{"input is not pkcs#7 padded"}
		}
	}
	return in[0:(len(in) - pl)], nil
}