summaryrefslogtreecommitdiffstats
path: root/challenge/c17.go
blob: 78c949c92b791c50d928864171460e0d8811a865 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright © 2021 rsiddharth <s@ricketyspace.net>
// SPDX-License-Identifier: ISC

package challenge

import (
	"fmt"

	"ricketyspace.net/cryptopals/lib"
)

// Cryptopals #17 - CBC padding oracle attack
func C17() {
	key, err := lib.RandomKey(16)
	if err != nil {
		fmt.Printf("key generation: error: %v\n", err)
	}
	cookies := []string{
		"MDAwMDAwTm93IHRoYXQgdGhlIHBhcnR5IGlzIGp1bXBpbmc=",
		"MDAwMDAxV2l0aCB0aGUgYmFzcyBraWNrZWQgaW4gYW5kIHRoZSBWZWdhJ3MgYXJlIHB1bXBpbic=",
		"MDAwMDAyUXVpY2sgdG8gdGhlIHBvaW50LCB0byB0aGUgcG9pbnQsIG5vIGZha2luZw==",
		"MDAwMDAzQ29va2luZyBNQydzIGxpa2UgYSBwb3VuZCBvZiBiYWNvbg==",
		"MDAwMDA0QnVybmluZyAnZW0sIGlmIHlvdSBhaW4ndCBxdWljayBhbmQgbmltYmxl",
		"MDAwMDA1SSBnbyBjcmF6eSB3aGVuIEkgaGVhciBhIGN5bWJhbA==",
		"MDAwMDA2QW5kIGEgaGlnaCBoYXQgd2l0aCBhIHNvdXBlZCB1cCB0ZW1wbw==",
		"MDAwMDA3SSdtIG9uIGEgcm9sbCwgaXQncyB0aW1lIHRvIGdvIHNvbG8=",
		"MDAwMDA4b2xsaW4nIGluIG15IGZpdmUgcG9pbnQgb2g=",
		"MDAwMDA5aXRoIG15IHJhZy10b3AgZG93biBzbyBteSBoYWlyIGNhbiBibG93",
	}
	encrypt := func() ([]byte, []byte) {
		r := lib.RandomInt(0, int64(len(cookies)-1))
		p := lib.Base64ToBytes(cookies[r])
		k := key
		iv, err := lib.RandomKey(16)
		if err != nil {
			fmt.Printf("iv generation: error: %v\n", err)
		}
		c := lib.AESEncryptCBC(p, k, iv)

		return c, iv
	}
	decrypt := func(c, iv []byte) bool {
		k := key
		_, err := lib.AESDecryptCBC(c, k, iv)
		if err != nil {
			return false
		}
		return true
	}
	setKnownBytes := func(ivc, pb []byte, pd byte, d int) []byte {
		for i := d + 1; i < 16; i++ {
			io := ivc[i] ^ pb[i] // Get intermediate output
			id := io ^ pd        // iv' at position i
			ivc[i] = id
		}
		return ivc
	}
	inMap := func(pm map[int][]byte, d int, pg byte) bool {
		for _, p := range pm[d] {
			if p == pg {
				return true
			}
		}
		return false
	}
	decryptBlock := func(cb, iv []byte) []byte {
		pb := make([]byte, 16)        // Plaintext block
		d := 15                       // Represents the current byte position getting decrypted.
		pm := make(map[int][]byte, 0) // Plain text guesses at position d

		for d >= 0 { // An iteration decrypts the d^th byte position
			pd := byte(16 - d) // Padding character

			// Init guesses at positon 'd' array in pm map.
			if _, ok := pm[d]; !ok {
				pm[d] = make([]byte, 0)
			}

			found := false // Set when decrypt works on a modified iv'
			for i := 255; i >= 0; i-- {
				pg := byte(i)    // Guess byte at d^th position
				io := iv[d] ^ pg // Get intermediate output
				id := io ^ pd    // iv' at position d

				// Make fresh copy of iv
				ivc := make([]byte, 16)
				copy(ivc, iv)

				// Set appropriate bytes from d+1 .. 15
				ivc = setKnownBytes(ivc, pb, pd, d)

				// Mody d^th character of ivc to id
				ivc[d] = id

				if decrypt(cb, ivc) {
					// (Possible) Plain text byte
					// at position d found. Check
					// if this was guessed before.
					if inMap(pm, d, pg) {
						// Guess was already
						// found; so ignore
						// this.
						continue
					}
					// Add to guess list.
					pm[d] = append(pm[d], pg)

					pb[d] = pg
					found = true
					break
				}
			}
			if found {
				d -= 1
			} else {
				// Our guess at d+1 is incorrect; backtrack.
				d += 1
			}
		}
		return pb
	}
	attack := func(c, iv []byte) []byte {
		n := len(c) / 16     // Number of cipher blocks
		p := make([]byte, 0) // Plaintext
		for i := 0; i < n; i++ {
			// Decrypt i^th block
			s := i * 16
			e := s + 16
			pb := decryptBlock(c[s:e], iv)
			p = append(p, pb...) // Append i^th plaintext block

			// Current cipher block becomes the
			// initialization vector for decrypting the
			// next cipher block.
			iv = c[s:e]
		}
		return p
	}
	c, iv := encrypt()
	p := attack(c, iv)
	p, e := lib.Pkcs7PaddingUndo(p)
	if e != nil {
		panic("plaintext padding undo failed!")
	}
	fmt.Printf("%s\n", lib.BytesToStr(p))
}

// Output
// $ while true; do
// > ./cryptopals -c 17
// > done
// 000001With the bass kicked in and the Vega's are pumpin'
// 000001With the bass kicked in and the Vega's are pumpin'
// 000009ith my rag-top down so my hair can blow
// 000008ollin' in my five point oh
// 000008ollin' in my five point oh
// 000000Now that the party is jumping
// 000009ith my rag-top down so my hair can blow
// 000008ollin' in my five point oh
// 000003Cooking MC's like a pound of bacon
// 000001With the bass kicked in and the Vega's are pumpin'
// 000009ith my rag-top down so my hair can blow
// 000002Quick to the point, to the point, no faking
// 000008ollin' in my five point oh
// 000002Quick to the point, to the point, no faking
// 000002Quick to the point, to the point, no faking
// 000003Cooking MC's like a pound of bacon
// 000005I go crazy when I hear a cymbal
// 000003Cooking MC's like a pound of bacon
// 000002Quick to the point, to the point, no faking
// 000001With the bass kicked in and the Vega's are pumpin'
// 000006And a high hat with a souped up tempo
// 000000Now that the party is jumping
// 000000Now that the party is jumping
// 000008ollin' in my five point oh
// 000003Cooking MC's like a pound of bacon
// 000000Now that the party is jumping
// 000006And a high hat with a souped up tempo
// 000006And a high hat with a souped up tempo
// 000006And a high hat with a souped up tempo
// 000007I'm on a roll, it's time to go solo
// 000006And a high hat with a souped up tempo
// 000004Burning 'em, if you ain't quick and nimble
// 000009ith my rag-top down so my hair can blow
// 000008ollin' in my five point oh
// 000006And a high hat with a souped up tempo
// 000007I'm on a roll, it's time to go solo
// 000001With the bass kicked in and the Vega's are pumpin'
// 000006And a high hat with a souped up tempo
// 000006And a high hat with a souped up tempo
// 000009ith my rag-top down so my hair can blow
// ^C