All files / src/classes/MFKDFDerivedKey auth.js

100% Statements 35/35
100% Branches 0/0
100% Functions 9/9
100% Lines 35/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289                    1x                                                         7x 7x   1x                                                         7x 7x   1x                                                         7x 7x 7x   1x                                                 4x 4x 4x 4x 4x   1x                                                 4x 4x 4x 4x 4x   1x                                                 4x 4x 4x 4x 4x   1x                                                     7x   1x                                                     7x   1x                                                     7x   1x  
/**
 * @file Multi-Factor Derived Key Authentication Functions
 * @copyright Multifactor 2022 All Rights Reserved
 *
 * @description
 * Authentication operations using a multi-factor derived key
 *
 * @author Vivek Nair (https://nair.me) <[email protected]>
 */
 
const crypto = require('crypto')
 
/**
 * ISO 9798-2 2-Pass Unilateral Authentication
 *
 * @example
 * // setup multi-factor derived key
 * const key = await mfkdf.setup.key([ await mfkdf.setup.factors.password('password') ])
 
 * // challenger: create random challenge
 * const challenge = crypto.randomBytes(32)
 * const identity = Buffer.from('Challenger')
 
 * // responder: generate response
 * const response = await key.ISO97982PassUnilateralAuthSymmetric(challenge, identity)
 
 * // verifier: verify response
 * const authKey = await key.ISO9798SymmetricKey()
 * const valid = await mfkdf.auth.VerifyISO97982PassUnilateralAuthSymmetric(challenge, identity, response, authKey) // -> true
 *
 * @param {Buffer} challenge - The nonce value provided by the challenger
 * @param {Buffer} identity - The identity of the challenger
 * @returns {Buffer} The response value
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.17.0
 * @memberOf MFKDFDerivedKey
 * @async
 */
async function ISO97982PassUnilateralAuthSymmetric (challenge, identity) {
  const plaintext = Buffer.concat([challenge, identity])
  return await this.encrypt(plaintext, 'aes256', 'CBC', true)
}
module.exports.ISO97982PassUnilateralAuthSymmetric = ISO97982PassUnilateralAuthSymmetric
 
/**
 * ISO 9798-2 Public Key 2-Pass Unilateral Authentication
 *
 * @example
 * // setup multi-factor derived key
 * const key = await mfkdf.setup.key([ await mfkdf.setup.factors.password('password') ])
 *
 * // challenger: create random challenge
 * const challenge = crypto.randomBytes(32)
 * const identity = Buffer.from('Challenger')
 *
 * // responder: generate response
 * const response = await key.ISO97982PassUnilateralAuthAsymmetric(challenge, identity)
 *
 * // verifier: verify response
 * const authKey = await key.ISO9798AsymmetricKey()
 * const valid = await mfkdf.auth.VerifyISO97982PassUnilateralAuthAsymmetric(challenge, identity, response, authKey) // -> true
 *
 * @param {Buffer} challenge - The nonce value provided by the challenger
 * @param {Buffer} identity - The identity of the challenger
 * @returns {Buffer} The response value
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.17.0
 * @memberOf MFKDFDerivedKey
 * @async
 */
async function ISO97982PassUnilateralAuthAsymmetric (challenge, identity) {
  const plaintext = Buffer.concat([challenge, identity])
  return await this.sign(plaintext, 'rsa1024', true)
}
module.exports.ISO97982PassUnilateralAuthAsymmetric = ISO97982PassUnilateralAuthAsymmetric
 
/**
 * ISO 9798-2 2-Pass Unilateral Authentication over CCF
 *
 * @example
 * // setup multi-factor derived key
 * const key = await mfkdf.setup.key([ await mfkdf.setup.factors.password('password') ])
 *
 * // challenger: create random challenge
 * const challenge = crypto.randomBytes(32)
 * const identity = Buffer.from('Challenger')
 *
 * // responder: generate response
 * const response = await key.ISO97982PassUnilateralAuthCCF(challenge, identity)
 *
 * // verifier: verify response
 * const authKey = await key.ISO9798CCFKey()
 * const valid = await mfkdf.auth.VerifyISO97982PassUnilateralAuthCCF(challenge, identity, response, authKey) // -> true
 *
 * @param {Buffer} challenge - The nonce value provided by the challenger
 * @param {Buffer} identity - The identity of the challenger
 * @returns {Buffer} The response value
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.17.0
 * @memberOf MFKDFDerivedKey
 * @async
 */
async function ISO97982PassUnilateralAuthCCF (challenge, identity) {
  const key = await this.getSubkey(32, 'SHA256AUTH', 'sha256')
  const ct = Buffer.concat([challenge, identity, key])
  return crypto.createHash('sha256').update(ct).digest()
}
module.exports.ISO97982PassUnilateralAuthCCF = ISO97982PassUnilateralAuthCCF
 
/**
 * ISO 9798-2 1-Pass Unilateral Authentication
 *
 * @example
 * // setup multi-factor derived key
 * const key = await mfkdf.setup.key([ await mfkdf.setup.factors.password('password') ])
 * const identity = Buffer.from('Challenger')
 *
 * // responder: generate response
 * const response = await key.ISO97981PassUnilateralAuthSymmetric(identity)
 *
 * // verifier: verify response
 * const authKey = await key.ISO9798SymmetricKey()
 * const valid = await mfkdf.auth.VerifyISO97981PassUnilateralAuthSymmetric(identity, response, authKey) // -> true
 *
 * @param {Buffer} identity - The identity of the challenger
 * @returns {Buffer} The response value
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.17.0
 * @memberOf MFKDFDerivedKey
 * @async
 */
async function ISO97981PassUnilateralAuthSymmetric (identity) {
  const date = Math.floor(Date.now() / 1000)
  const challenge = Buffer.allocUnsafe(4)
  challenge.writeUInt32BE(date, 0)
  const response = await this.ISO97982PassUnilateralAuthSymmetric(challenge, identity)
  return Buffer.concat([challenge, response])
}
module.exports.ISO97981PassUnilateralAuthSymmetric = ISO97981PassUnilateralAuthSymmetric
 
/**
 * ISO 9798-2 Public Key 1-Pass Unilateral Authentication
 *
 * @example
 * // setup multi-factor derived key
 * const key = await mfkdf.setup.key([ await mfkdf.setup.factors.password('password') ])
 * const identity = Buffer.from('Challenger')
 *
 * // responder: generate response
 * const response = await key.ISO97981PassUnilateralAuthAsymmetric(identity)
 *
 * // verifier: verify response
 * const authKey = await key.ISO9798AsymmetricKey()
 * const valid = await mfkdf.auth.VerifyISO97981PassUnilateralAuthAsymmetric(identity, response, authKey) // -> true
 *
 * @param {Buffer} identity - The identity of the challenger
 * @returns {Buffer} The response value
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.17.0
 * @memberOf MFKDFDerivedKey
 * @async
 */
async function ISO97981PassUnilateralAuthAsymmetric (identity) {
  const date = Math.floor(Date.now() / 1000)
  const challenge = Buffer.allocUnsafe(4)
  challenge.writeUInt32BE(date, 0)
  const response = await this.ISO97982PassUnilateralAuthAsymmetric(challenge, identity)
  return Buffer.concat([challenge, response])
}
module.exports.ISO97981PassUnilateralAuthAsymmetric = ISO97981PassUnilateralAuthAsymmetric
 
/**
 * ISO 9798-2 1-Pass Unilateral Authentication over CCF
 *
 * @example
 * // setup multi-factor derived key
 * const key = await mfkdf.setup.key([ await mfkdf.setup.factors.password('password') ])
 * const identity = Buffer.from('Challenger')
 *
 * // responder: generate response
 * const response = await key.ISO97981PassUnilateralAuthCCF(identity)
 *
 * // verifier: verify response
 * const authKey = await key.ISO9798CCFKey()
 * const valid = await mfkdf.auth.VerifyISO97981PassUnilateralAuthCCF(identity, response, authKey) // -> true
 *
 * @param {Buffer} identity - The identity of the challenger
 * @returns {Buffer} The response value
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.17.0
 * @memberOf MFKDFDerivedKey
 * @async
 */
async function ISO97981PassUnilateralAuthCCF (identity) {
  const date = Math.floor(Date.now() / 1000)
  const challenge = Buffer.allocUnsafe(4)
  challenge.writeUInt32BE(date, 0)
  const response = await this.ISO97982PassUnilateralAuthCCF(challenge, identity)
  return Buffer.concat([challenge, response])
}
module.exports.ISO97981PassUnilateralAuthCCF = ISO97981PassUnilateralAuthCCF
 
/**
 * Get the symmetric key used for ISO 9798-2 Unilateral Authentication
 *
 * @example
 * // setup multi-factor derived key
 * const key = await mfkdf.setup.key([ await mfkdf.setup.factors.password('password') ])
 
 * // challenger: create random challenge
 * const challenge = crypto.randomBytes(32)
 * const identity = Buffer.from('Challenger')
 
 * // responder: generate response
 * const response = await key.ISO97982PassUnilateralAuthSymmetric(challenge, identity)
 
 * // verifier: verify response
 * const authKey = await key.ISO9798SymmetricKey()
 * const valid = await mfkdf.auth.VerifyISO97982PassUnilateralAuthSymmetric(challenge, identity, response, authKey) // -> true
 *
 * @returns {Buffer} Symmetric key
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.17.0
 * @memberOf MFKDFDerivedKey
 * @async
 */
async function ISO9798SymmetricKey () {
  return await this.getSymmetricKey('aes256', true)
}
module.exports.ISO9798SymmetricKey = ISO9798SymmetricKey
 
/**
 * Get the public key used for ISO 9798-2 Public Key Unilateral Authentication
 *
 * @example
 * // setup multi-factor derived key
 * const key = await mfkdf.setup.key([ await mfkdf.setup.factors.password('password') ])
 *
 * // challenger: create random challenge
 * const challenge = crypto.randomBytes(32)
 * const identity = Buffer.from('Challenger')
 *
 * // responder: generate response
 * const response = await key.ISO97982PassUnilateralAuthAsymmetric(challenge, identity)
 *
 * // verifier: verify response
 * const authKey = await key.ISO9798AsymmetricKey()
 * const valid = await mfkdf.auth.VerifyISO97982PassUnilateralAuthAsymmetric(challenge, identity, response, authKey) // -> true
 *
 * @returns {Buffer} Public key (spki-der encoded)
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.17.0
 * @memberOf MFKDFDerivedKey
 * @async
 */
async function ISO9798AsymmetricKey () {
  return (await this.getAsymmetricKeyPair('rsa1024', true)).publicKey
}
module.exports.ISO9798AsymmetricKey = ISO9798AsymmetricKey
 
/**
 * Get the CCF key used for ISO 9798-2 Unilateral Authentication over CCF
 *
 * @example
 * // setup multi-factor derived key
 * const key = await mfkdf.setup.key([ await mfkdf.setup.factors.password('password') ])
 *
 * // challenger: create random challenge
 * const challenge = crypto.randomBytes(32)
 * const identity = Buffer.from('Challenger')
 *
 * // responder: generate response
 * const response = await key.ISO97982PassUnilateralAuthCCF(challenge, identity)
 *
 * // verifier: verify response
 * const authKey = await key.ISO9798CCFKey()
 * const valid = await mfkdf.auth.VerifyISO97982PassUnilateralAuthCCF(challenge, identity, response, authKey) // -> true
 *
 * @returns {Buffer} CCF key
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.17.0
 * @memberOf MFKDFDerivedKey
 * @async
 */
async function ISO9798CCFKey () {
  return await this.getSubkey(32, 'SHA256AUTH', 'sha256')
}
module.exports.ISO9798CCFKey = ISO9798CCFKey