Skip to content

Commit

Permalink
retry decryption with unpadded cipher txt
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshu committed Apr 30, 2024
1 parent 48cf350 commit 6b5261f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/helpers/metadataUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ export async function decryptNodeData(eciesData: EciesHex, ciphertextHex: string
return decryptedSigBuffer;
}

export async function decryptNodeDataWithPadding(eciesData: EciesHex, ciphertextHex: string, privKey: Buffer): Promise<Buffer> {
const ciphertextHexPadding = ciphertextHex.padStart(64, "0");
const metadata = encParamsHexToBuf(eciesData);

try {
const decryptedSigBuffer = await decrypt(privKey, {
...metadata,
ciphertext: Buffer.from(ciphertextHexPadding, "hex"),
});
return decryptedSigBuffer;
} catch (error) {
log.warn("Failed to decrypt padded share cipher", error);
// try without cipher text padding
return decrypt(privKey, { ...metadata, ciphertext: Buffer.from(ciphertextHex, "hex") });
}
}

export function generateMetadataParams(ecCurve: ec, serverTimeOffset: number, message: string, privateKey: BN): MetadataParams {
const key = ecCurve.keyFromPrivate(privateKey.toString("hex", 64));
const setData = {
Expand Down
7 changes: 3 additions & 4 deletions src/helpers/nodeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
} from "./common";
import { derivePubKey, generateAddressFromPrivKey, generateAddressFromPubKey, generatePrivateKey, generateShares, keccak256 } from "./keyUtils";
import { lagrangeInterpolation } from "./langrangeInterpolatePoly";
import { decryptNodeData, decryptSeedData, getMetadata, getOrSetNonce } from "./metadataUtils";
import { decryptNodeData, decryptNodeDataWithPadding, decryptSeedData, getMetadata, getOrSetNonce } from "./metadataUtils";

export const GetPubKeyOrKeyAssign = async (params: {
endpoints: string[];
Expand Down Expand Up @@ -548,12 +548,11 @@ export async function retrieveOrImportShare(params: {
if (keys?.length > 0) {
const latestKey = currentShareResponse.result.keys[0];
nodeIndexes.push(new BN(latestKey.node_index));

if (latestKey.share_metadata) {
sharePromises.push(
decryptNodeData(
decryptNodeDataWithPadding(
latestKey.share_metadata,
Buffer.from(latestKey.share, "base64").toString("binary").padStart(64, "0"),
Buffer.from(latestKey.share, "base64").toString("binary"),
sessionAuthKey
).catch((err) => log.error("share decryption", err))
);
Expand Down

0 comments on commit 6b5261f

Please sign in to comment.