Skip to content

Commit

Permalink
fix: don't store CID length with encrypted, use CID.decodeFirst()
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Jan 29, 2021
1 parent cc617bc commit b6b58ce
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 37 deletions.
33 changes: 3 additions & 30 deletions src/crypto/aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,6 @@ import CID from '../cid.js'
import random from 'js-crypto-random'
import aes from 'js-crypto-aes'

/**
* @param {number} value
*/
const enc32 = value => {
value = +value
const buff = new Uint8Array(4)
buff[3] = (value >>> 24)
buff[2] = (value >>> 16)
buff[1] = (value >>> 8)
buff[0] = (value & 0xff)
return buff
}

/**
* @param {Uint8Array} buffer
*/
const readUInt32LE = (buffer) => {
const offset = buffer.byteLength - 4
return ((buffer[offset]) |
(buffer[offset + 1] << 8) |
(buffer[offset + 2] << 16)) +
(buffer[offset + 3] * 0x1000000)
}

/**
* @param {Uint8Array[]} buffers
*/
Expand All @@ -52,10 +28,8 @@ const mkcrypto = ({ name, code, ivsize }) => {
*/
const decrypt = async ({ key, value: { iv, bytes } }) => {
bytes = await aes.decrypt(bytes, key, { name: cyperType, iv, tagLength: 16 })
const len = readUInt32LE(bytes.subarray(0, 4))
const cid = CID.decode(bytes.subarray(4, 4 + len))
bytes = bytes.subarray(4 + len)
return { cid, bytes }
const [cid, remainder] = CID.decodeFirst(bytes)
return { cid, bytes: remainder }
}
/**
* @param {Object} options
Expand All @@ -64,9 +38,8 @@ const mkcrypto = ({ name, code, ivsize }) => {
* @param {CID} options.cid
*/
const encrypt = async ({ key, cid, bytes }) => {
const len = enc32(cid.bytes.byteLength)
const iv = random.getRandomBytes(ivsize)
const msg = concat([len, cid.bytes, bytes])
const msg = concat([cid.bytes, bytes])
bytes = await aes.encrypt(msg, key, { name: cyperType, iv, tagLength: 16 })
return { bytes, iv, code }
}
Expand Down
14 changes: 7 additions & 7 deletions test/test-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,22 @@ describe('block', () => {
describe('ciphers', () => {
const createTest = name => {
const json = codec
test(`aes-${name}`, async () => {
test.only(`aes-${name}`, async () => {
const block = await main.encode({ value: fixture, codec: json, hasher })
const crypto = ciphers[name]
const key = random.getRandomBytes(32)
const value = await crypto.encrypt({ ...block, key })
const eblock = await main.encode({ codec: encrypted, value, hasher })
const eeblock = await main.decode({ codec: encrypted, bytes: eblock.bytes, hasher })
same(eblock.cid.toString(), eeblock.cid.toString())
same([...eblock.bytes], [...eeblock.bytes])
same([...eblock.value.bytes], [...eeblock.value.bytes])
same([...eblock.value.iv], [...eeblock.value.iv])
same(eeblock.cid.toString(), eblock.cid.toString())
same([...eeblock.bytes], [...eblock.bytes])
same([...eeblock.value.bytes], [...eblock.value.bytes])
same([...eeblock.value.iv], [...eblock.value.iv])
const { cid, bytes } = await crypto.decrypt({ ...eblock, key })
same(block.cid.toString(), cid.toString())
same(cid.toString(), block.cid.toString())
same([...bytes], [...block.bytes])
const dblock = await main.create({ cid, bytes, codec: json, hasher })
same(block.value, dblock.value)
same(dblock.value, block.value)
})
}
createTest('gcm')
Expand Down

0 comments on commit b6b58ce

Please sign in to comment.