Skip to content

Commit

Permalink
fix: type check
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed Jan 26, 2021
1 parent 46c2a2b commit 686b544
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
37 changes: 33 additions & 4 deletions src/crypto/aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ 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)
Expand All @@ -12,6 +15,9 @@ const enc32 = value => {
return buff
}

/**
* @param {Uint8Array} buffer
*/
const readUInt32LE = (buffer) => {
const offset = buffer.byteLength - 4
return ((buffer[offset]) |
Expand All @@ -20,18 +26,32 @@ const readUInt32LE = (buffer) => {
(buffer[offset + 3] * 0x1000000)
}

/**
* @param {Uint8Array[]} buffers
*/
const concat = buffers => Uint8Array.from(buffers.map(b => [...b]).flat())

/**
* @template {'aes-gcm' | 'aes-cbc' | 'aes-ctr'} Name
* @template {number} Code
* @param {Object} options
* @param {Name} options.name
* @param {Code} options.code
* @param {number} options.ivsize
*/
const mkcrypto = ({ name, code, ivsize }) => {
// Line below does a type cast, because type checker can't infer that
// `toUpperCase` will result in desired string literal.
const cyperType = /** @type {import('js-crypto-aes/dist/params').cipherTypes} */(name.toUpperCase())
/**
* @param {Object} options
* @param {Object} options.value
* @param {Uint8Array} options.key
* @param {Object} options.value
* @param {Uint8Array} options.value.bytes
* @param {Uint8Array} options.value.iv
*/
const decrypt = async ({ key, value: { iv, bytes } }) => {
bytes = await aes.decrypt(bytes, key, { name: name.toUpperCase(), iv, tagLength: 16 })
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)
Expand All @@ -47,10 +67,19 @@ const mkcrypto = ({ name, code, ivsize }) => {
const len = enc32(cid.bytes.byteLength)
const iv = random.getRandomBytes(ivsize)
const msg = concat([len, cid.bytes, bytes])
bytes = await aes.encrypt(msg, key, { name: name.toUpperCase(), iv, tagLength: 16 })
bytes = await aes.encrypt(msg, key, { name: cyperType, iv, tagLength: 16 })
return { bytes, iv, code }
}
return { encrypt, decrypt, code, name: name.toLowerCase(), ivsize }

return {
code,
// Note: Do a type cast becasue `toLowerCase()` turns liternal type
// into a string.
name: /** @type {Name} */(name.toLowerCase()),
encrypt,
decrypt,
ivsize
}
}

const gcm = mkcrypto({ name: 'aes-gcm', code: 0x1401, ivsize: 12 })
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"strict": true,
"alwaysStrict": true,
"esModuleInterop": true,
"target": "ES2018",
"target": "ES2019",
"moduleResolution": "node",
"declaration": true,
"declarationMap": true,
Expand Down

0 comments on commit 686b544

Please sign in to comment.