Skip to content

Commit 9d09eae

Browse files
committed
Adding a way to set an explicit padding size
1 parent 0f4cd06 commit 9d09eae

4 files changed

Lines changed: 28 additions & 23 deletions

File tree

nodejs/ece.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,9 @@ function determineRecordSize(params) {
136136
if (isNaN(rs)) {
137137
return 4096;
138138
}
139-
if (rs <= PAD_SIZE) {
140-
throw new Error('The rs parameter has to be greater than ' + PAD_SIZE);
139+
var padSize = params.padSize || PAD_SIZE;
140+
if (rs <= padSize) {
141+
throw new Error('The rs parameter has to be greater than ' + padSize);
141142
}
142143
return rs;
143144
}
@@ -151,22 +152,23 @@ function generateNonce(base, counter) {
151152
return nonce;
152153
}
153154

154-
function decryptRecord(key, counter, buffer) {
155+
function decryptRecord(key, counter, buffer, padSize) {
155156
var nonce = generateNonce(key.nonce, counter);
156157
var gcm = crypto.createDecipheriv(AES_GCM, key.key, nonce);
157158
gcm.setAuthTag(buffer.slice(buffer.length - TAG_LENGTH));
158159
var data = gcm.update(buffer.slice(0, buffer.length - TAG_LENGTH));
159160
data = Buffer.concat([data, gcm.final()]);
160-
var pad = data.readUIntBE(0, PAD_SIZE);
161-
if (pad + PAD_SIZE > data.length) {
161+
padSize = padSize || PAD_SIZE
162+
var pad = data.readUIntBE(0, padSize);
163+
if (pad + padSize > data.length) {
162164
throw new Error('padding exceeds block size');
163165
}
164166
var padCheck = new Buffer(pad);
165167
padCheck.fill(0);
166-
if (padCheck.compare(data.slice(PAD_SIZE, PAD_SIZE + pad)) !== 0) {
168+
if (padCheck.compare(data.slice(padSize, padSize + pad)) !== 0) {
167169
throw new Error('invalid padding');
168170
}
169-
return data.slice(PAD_SIZE + pad);
171+
return data.slice(padSize + pad);
170172
}
171173

172174
// TODO: this really should use the node streams stuff
@@ -195,20 +197,22 @@ function decrypt(buffer, params) {
195197
if (end - start <= TAG_LENGTH) {
196198
throw new Error('Invalid block: too small at ' + i);
197199
}
198-
var block = decryptRecord(key, i, buffer.slice(start, end));
200+
var block = decryptRecord(key, i, buffer.slice(start, end),
201+
params.padSize);
199202
result = Buffer.concat([result, block]);
200203
start = end;
201204
}
202205
return result;
203206
}
204207

205-
function encryptRecord(key, counter, buffer, pad) {
208+
function encryptRecord(key, counter, buffer, pad, padSize) {
206209
pad = pad || 0;
207210
var nonce = generateNonce(key.nonce, counter);
208211
var gcm = crypto.createCipheriv(AES_GCM, key.key, nonce);
209-
var padding = new Buffer(pad + PAD_SIZE);
212+
padSize = padSize || PAD_SIZE;
213+
var padding = new Buffer(pad + padSize);
210214
padding.fill(0);
211-
padding.writeUIntBE(pad, 0, PAD_SIZE);
215+
padding.writeUIntBE(pad, 0, padSize);
212216
var epadding = gcm.update(padding);
213217
var ebuffer = gcm.update(buffer);
214218
gcm.final();
@@ -231,20 +235,22 @@ function encrypt(buffer, params) {
231235
var rs = determineRecordSize(params);
232236
var start = 0;
233237
var result = new Buffer(0);
238+
var padSize = params.padSize || PAD_SIZE;
234239
var pad = isNaN(parseInt(params.pad, 10)) ? 0 : parseInt(params.pad, 10);
235240

236241
// Note the <= here ensures that we write out a padding-only block at the end
237242
// of a buffer.
238243
for (var i = 0; start <= buffer.length; ++i) {
239244
// Pad so that at least one data byte is in a block.
240-
var recordPad = Math.min((1 << (PAD_SIZE * 8)) - 1, // maximum padding
241-
Math.min(rs - PAD_SIZE - 1, pad));
245+
var recordPad = Math.min((1 << (padSize * 8)) - 1, // maximum padding
246+
Math.min(rs - padSize - 1, pad));
242247
pad -= recordPad;
243248

244-
var end = Math.min(start + rs - PAD_SIZE - recordPad, buffer.length);
245-
var block = encryptRecord(key, i, buffer.slice(start, end), recordPad);
249+
var end = Math.min(start + rs - padSize - recordPad, buffer.length);
250+
var block = encryptRecord(key, i, buffer.slice(start, end),
251+
recordPad, padSize);
246252
result = Buffer.concat([result, block]);
247-
start += rs - PAD_SIZE - recordPad;
253+
start += rs - padSize - recordPad;
248254
}
249255
if (pad) {
250256
throw new Error('Unable to pad by requested amount, ' + pad + ' remaining');

nodejs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "http_ece",
3-
"version": "0.4.0",
3+
"version": "0.4.1",
44
"description": "Encrypted Content-Encoding for HTTP",
55
"homepage": "https://github.com/martinthomson/encrypted-content-encoding",
66
"bugs": "https://github.com/martinthomson/encrypted-content-encoding/issues",

python/http_ece/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import os, struct
1+
import os, struct, functools
22
from cryptography.hazmat.primitives import hashes
33
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
44
from cryptography.hazmat.backends import default_backend
@@ -9,7 +9,6 @@
99

1010
keys = {}
1111
labels = {}
12-
padSize = 2
1312

1413
def deriveKey(mode, salt, key=None, dh=None, keyid=None, authSecret=None):
1514
def buildInfo(base, context):
@@ -87,15 +86,15 @@ def iv(base, counter):
8786
(mask,) = struct.unpack("!Q", base[4:])
8887
return base[:4] + struct.pack("!Q", counter ^ mask)
8988

90-
def decrypt(buffer, salt, key=None, keyid=None, dh=None, rs=4096, authSecret=None):
89+
def decrypt(buffer, salt, key=None, keyid=None, dh=None, rs=4096, authSecret=None, padSize=2):
9190
def decryptRecord(key, nonce, counter, buffer):
9291
decryptor = Cipher(
9392
algorithms.AES(key),
9493
modes.GCM(iv(nonce, counter), tag=buffer[-16:]),
9594
backend=default_backend()
9695
).decryptor()
9796
data = decryptor.update(buffer[:-16]) + decryptor.finalize()
98-
(pad,) = struct.unpack("!H", data[0:padSize]);
97+
pad = functools.reduce(lambda x, y: x << 8 | y, struct.unpack("!" + ("B" * padSize), data[0:padSize]))
9998
if data[padSize:padSize+pad] != (b"\x00" * pad):
10099
raise Exception(u"Bad padding")
101100
data = data[padSize+pad:]
@@ -117,7 +116,7 @@ def decryptRecord(key, nonce, counter, buffer):
117116
counter += 1
118117
return result
119118

120-
def encrypt(buffer, salt, key=None, keyid=None, dh=None, rs=4096, authSecret=None):
119+
def encrypt(buffer, salt, key=None, keyid=None, dh=None, rs=4096, authSecret=None, padSize=2):
121120
def encryptRecord(key, nonce, counter, buffer):
122121
encryptor = Cipher(
123122
algorithms.AES(key),

python/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name='http_ece',
6-
version='0.4.0',
6+
version='0.4.1',
77
author='Martin Thomson',
88
author_email='martin.thomson@gmail.com',
99
scripts=[],

0 commit comments

Comments
 (0)