From 24b1790c9935feff5f1c50e7c2b7b3bef072f81b Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sat, 30 Jan 2021 20:02:21 +0100 Subject: [PATCH] Add hmac{Sha256, 512} (#861) * Add hmac{Sha256, 512} * Adjust * private hmac --- CHANGELOG.md | 3 ++- .../util-crypto/src/hd/ledger/derivePrivate.ts | 12 ++++-------- packages/util-crypto/src/hd/ledger/master.ts | 7 +++---- packages/util-crypto/src/hmac/index.ts | 5 +++++ packages/util-crypto/src/hmac/sha256.ts | 14 ++++++++++++++ packages/util-crypto/src/hmac/sha512.ts | 14 ++++++++++++++ packages/util-crypto/src/index.ts | 1 + packages/util-crypto/src/pbkdf2/pbkdf2.ts | 12 ++++-------- 8 files changed, 47 insertions(+), 21 deletions(-) create mode 100644 packages/util-crypto/src/hmac/index.ts create mode 100644 packages/util-crypto/src/hmac/sha256.ts create mode 100644 packages/util-crypto/src/hmac/sha512.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 71b4b83c8..0ff702282 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,8 @@ Changes: -- Added `hdLedger` to allow for Ledger-compatible bip32+ed25519 derivation +- Added `hdLedger` to util-crypto for Ledger-compatible bip32+ed25519 derivation +- Added `hmac{Sha256, 512}` to util-crypto ## 5.4.1 Jan 24, 2021 diff --git a/packages/util-crypto/src/hd/ledger/derivePrivate.ts b/packages/util-crypto/src/hd/ledger/derivePrivate.ts index a0c9c1053..c738e7dac 100644 --- a/packages/util-crypto/src/hd/ledger/derivePrivate.ts +++ b/packages/util-crypto/src/hd/ledger/derivePrivate.ts @@ -2,10 +2,11 @@ // SPDX-License-Identifier: Apache-2.0 import BN from 'bn.js'; -import hash from 'hash.js'; import { bnToU8a, u8aConcat } from '@polkadot/util'; +import { hmacSha512 } from '../../hmac'; + // performs hard-only derivation on the xprv export function ledgerDerivePrivate (xprv: Uint8Array, index: number): Uint8Array { const kl = xprv.slice(0, 32); @@ -19,16 +20,11 @@ export function ledgerDerivePrivate (xprv: Uint8Array, index: number): Uint8Arra data.set(kr, 1 + 32); data[0] = 0x00; - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - const z = hash.hmac(hash.sha512, cc).update(data).digest(); + const z = hmacSha512(cc, data); data[0] = 0x01; - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - const i = hash.hmac(hash.sha512, cc).update(data).digest(); - const chainCode = i.slice(32, 64); + const chainCode = hmacSha512(cc, data).slice(32, 64); const zl = z.slice(0, 32); const zr = z.slice(32, 64); const left = bnToU8a(new BN(kl, 16, 'le').add(new BN(zl.slice(0, 28), 16, 'le').mul(new BN(8))), { bitLength: 512, isLe: true }).slice(0, 32); diff --git a/packages/util-crypto/src/hd/ledger/master.ts b/packages/util-crypto/src/hd/ledger/master.ts index 02f926457..f9ad4af35 100644 --- a/packages/util-crypto/src/hd/ledger/master.ts +++ b/packages/util-crypto/src/hd/ledger/master.ts @@ -1,10 +1,9 @@ // Copyright 2017-2021 @polkadot/util-crypto authors & contributors // SPDX-License-Identifier: Apache-2.0 -import hash from 'hash.js'; - import { u8aConcat } from '@polkadot/util'; +import { hmacSha256, hmacSha512 } from '../../hmac'; import { mnemonicToSeedSync } from '../../mnemonic/bip39'; const ED25519_CRYPTO = 'ed25519 seed'; @@ -15,13 +14,13 @@ export function ledgerMaster (mnemonic: string): Uint8Array { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - const chainCode = hash.hmac(hash.sha256, ED25519_CRYPTO).update(new Uint8Array([1, ...seed])).digest(); + const chainCode = hmacSha256(ED25519_CRYPTO, new Uint8Array([1, ...seed])); let priv; while (!priv || (priv[31] & 0b0010_0000)) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - priv = hash.hmac(hash.sha512, ED25519_CRYPTO).update(priv || seed).digest(); + priv = hmacSha512(ED25519_CRYPTO, priv || seed); } priv[0] &= 0b1111_1000; diff --git a/packages/util-crypto/src/hmac/index.ts b/packages/util-crypto/src/hmac/index.ts new file mode 100644 index 000000000..e853f7de7 --- /dev/null +++ b/packages/util-crypto/src/hmac/index.ts @@ -0,0 +1,5 @@ +// Copyright 2017-2021 @polkadot/util-crypto authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +export { hmacSha256 } from './sha256'; +export { hmacSha512 } from './sha512'; diff --git a/packages/util-crypto/src/hmac/sha256.ts b/packages/util-crypto/src/hmac/sha256.ts new file mode 100644 index 000000000..005219373 --- /dev/null +++ b/packages/util-crypto/src/hmac/sha256.ts @@ -0,0 +1,14 @@ +// Copyright 2017-2021 @polkadot/util-crypto authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import hash from 'hash.js'; + +export function hmacSha256 (key: Uint8Array | string, data: Uint8Array): Uint8Array { + return Uint8Array.from( + hash + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + .hmac(hash.sha256, key) + .update(data) + .digest()); +} diff --git a/packages/util-crypto/src/hmac/sha512.ts b/packages/util-crypto/src/hmac/sha512.ts new file mode 100644 index 000000000..7b796e7ef --- /dev/null +++ b/packages/util-crypto/src/hmac/sha512.ts @@ -0,0 +1,14 @@ +// Copyright 2017-2021 @polkadot/util-crypto authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import hash from 'hash.js'; + +export function hmacSha512 (key: Uint8Array | string, data: Uint8Array): Uint8Array { + return Uint8Array.from( + hash + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + .hmac(hash.sha512, key) + .update(data) + .digest()); +} diff --git a/packages/util-crypto/src/index.ts b/packages/util-crypto/src/index.ts index f0843c978..439c6b9e0 100644 --- a/packages/util-crypto/src/index.ts +++ b/packages/util-crypto/src/index.ts @@ -13,6 +13,7 @@ export * from './blake2'; export * from './crypto'; export * from './ethereum'; export * from './hd'; +export * from './hmac'; export * from './keccak'; export * from './key'; export * from './mnemonic'; diff --git a/packages/util-crypto/src/pbkdf2/pbkdf2.ts b/packages/util-crypto/src/pbkdf2/pbkdf2.ts index f72b3d41b..49f7585d1 100644 --- a/packages/util-crypto/src/pbkdf2/pbkdf2.ts +++ b/packages/util-crypto/src/pbkdf2/pbkdf2.ts @@ -3,10 +3,10 @@ // Adapted from https://gist.github.com/calvinmetcalf/91e8e84dc63c75f2aa53 -import hash from 'hash.js'; - import { u8aConcat } from '@polkadot/util'; +import { hmacSha512 } from '../hmac'; + export function pbkdf2Sync (password: Uint8Array, salt: Uint8Array, rounds: number, len = 64): Uint8Array { let out = new Uint8Array(); let num = 0; @@ -16,16 +16,12 @@ export function pbkdf2Sync (password: Uint8Array, salt: Uint8Array, rounds: numb num++; block.writeUInt32BE(num, salt.length); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - let prev = hash.hmac(hash.sha512, password).update(block).digest(); + let prev = hmacSha512(password, block); const md = prev; let i = 0; while (++i < rounds) { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - prev = hash.hmac(hash.sha512, password).update(prev).digest(); + prev = hmacSha512(password, prev); let j = -1;