Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5f01de42 authored by Victor Hsieh's avatar Victor Hsieh
Browse files

Remove deadcode in VerityBuilder

generateFsVerityTree is never used now, thus and code path for
skipSigningBlock == false is dead.

Test: build
Bug: N/A
Change-Id: I32a3a0ad50aae6fb47ae4af2f70a5114d832b907
parent 8f2e6498
Loading
Loading
Loading
Loading
+12 −41
Original line number Original line Diff line number Diff line
@@ -68,22 +68,6 @@ public abstract class VerityBuilder {
        }
        }
    }
    }


    /**
     * Generates the 4k, SHA-256 based Merkle tree for the given APK and stores in the {@link
     * ByteBuffer} created by the {@link ByteBufferFactory}.  The output is suitable to be used as
     * the on-disk format for fs-verity to use.
     *
     * @return VerityResult containing a buffer with the generated Merkle tree stored at the
     *         front, the tree size, and the calculated root hash.
     */
    @NonNull
    public static VerityResult generateFsVerityTree(@NonNull RandomAccessFile apk,
            @NonNull ByteBufferFactory bufferFactory)
            throws IOException, SecurityException, NoSuchAlgorithmException, DigestException {
        return generateVerityTreeInternal(apk, bufferFactory, null /* signatureInfo */,
                false /* skipSigningBlock */);
    }

    /**
    /**
     * Generates the 4k, SHA-256 based Merkle tree for the given APK and stores in the {@link
     * Generates the 4k, SHA-256 based Merkle tree for the given APK and stores in the {@link
     * ByteBuffer} created by the {@link ByteBufferFactory}.  The Merkle tree does not cover Signing
     * ByteBuffer} created by the {@link ByteBufferFactory}.  The Merkle tree does not cover Signing
@@ -97,21 +81,16 @@ public abstract class VerityBuilder {
    public static VerityResult generateApkVerityTree(@NonNull RandomAccessFile apk,
    public static VerityResult generateApkVerityTree(@NonNull RandomAccessFile apk,
            @Nullable SignatureInfo signatureInfo, @NonNull ByteBufferFactory bufferFactory)
            @Nullable SignatureInfo signatureInfo, @NonNull ByteBufferFactory bufferFactory)
            throws IOException, SecurityException, NoSuchAlgorithmException, DigestException {
            throws IOException, SecurityException, NoSuchAlgorithmException, DigestException {
        return generateVerityTreeInternal(apk, bufferFactory, signatureInfo,
        return generateVerityTreeInternal(apk, bufferFactory, signatureInfo);
                true /* skipSigningBlock */);
    }
    }


    @NonNull
    @NonNull
    private static VerityResult generateVerityTreeInternal(@NonNull RandomAccessFile apk,
    private static VerityResult generateVerityTreeInternal(@NonNull RandomAccessFile apk,
            @NonNull ByteBufferFactory bufferFactory, @Nullable SignatureInfo signatureInfo,
            @NonNull ByteBufferFactory bufferFactory, @Nullable SignatureInfo signatureInfo)
            boolean skipSigningBlock)
            throws IOException, SecurityException, NoSuchAlgorithmException, DigestException {
            throws IOException, SecurityException, NoSuchAlgorithmException, DigestException {
        long dataSize = apk.length();
        if (skipSigningBlock) {
        long signingBlockSize =
        long signingBlockSize =
                signatureInfo.centralDirOffset - signatureInfo.apkSigningBlockOffset;
                signatureInfo.centralDirOffset - signatureInfo.apkSigningBlockOffset;
            dataSize -= signingBlockSize;
        long dataSize = apk.length() - signingBlockSize;
        }
        int[] levelOffset = calculateVerityLevelOffset(dataSize);
        int[] levelOffset = calculateVerityLevelOffset(dataSize);
        int merkleTreeSize = levelOffset[levelOffset.length - 1];
        int merkleTreeSize = levelOffset[levelOffset.length - 1];


@@ -120,10 +99,8 @@ public abstract class VerityBuilder {
                + CHUNK_SIZE_BYTES);  // maximum size of apk-verity metadata
                + CHUNK_SIZE_BYTES);  // maximum size of apk-verity metadata
        output.order(ByteOrder.LITTLE_ENDIAN);
        output.order(ByteOrder.LITTLE_ENDIAN);
        ByteBuffer tree = slice(output, 0, merkleTreeSize);
        ByteBuffer tree = slice(output, 0, merkleTreeSize);
        // Only use default salt in legacy case.
        byte[] apkRootHash = generateVerityTreeInternal(apk, signatureInfo, DEFAULT_SALT,
        byte[] salt = skipSigningBlock ? DEFAULT_SALT : null;
                levelOffset, tree);
        byte[] apkRootHash = generateVerityTreeInternal(apk, signatureInfo, salt, levelOffset,
                tree, skipSigningBlock);
        return new VerityResult(output, merkleTreeSize, apkRootHash);
        return new VerityResult(output, merkleTreeSize, apkRootHash);
    }
    }


@@ -173,8 +150,7 @@ public abstract class VerityBuilder {
            throws IOException, SignatureNotFoundException, SecurityException, DigestException,
            throws IOException, SignatureNotFoundException, SecurityException, DigestException,
                   NoSuchAlgorithmException {
                   NoSuchAlgorithmException {
        try (RandomAccessFile apk = new RandomAccessFile(apkPath, "r")) {
        try (RandomAccessFile apk = new RandomAccessFile(apkPath, "r")) {
            VerityResult result = generateVerityTreeInternal(apk, bufferFactory, signatureInfo,
            VerityResult result = generateVerityTreeInternal(apk, bufferFactory, signatureInfo);
                    true /* skipSigningBlock */);
            ByteBuffer footer = slice(result.verityData, result.merkleTreeSize,
            ByteBuffer footer = slice(result.verityData, result.merkleTreeSize,
                    result.verityData.limit());
                    result.verityData.limit());
            generateApkVerityFooter(apk, signatureInfo, footer);
            generateApkVerityFooter(apk, signatureInfo, footer);
@@ -351,17 +327,12 @@ public abstract class VerityBuilder {
    @NonNull
    @NonNull
    private static byte[] generateVerityTreeInternal(@NonNull RandomAccessFile apk,
    private static byte[] generateVerityTreeInternal(@NonNull RandomAccessFile apk,
            @Nullable SignatureInfo signatureInfo, @Nullable byte[] salt,
            @Nullable SignatureInfo signatureInfo, @Nullable byte[] salt,
            @NonNull int[] levelOffset, @NonNull ByteBuffer output, boolean skipSigningBlock)
            @NonNull int[] levelOffset, @NonNull ByteBuffer output)
            throws IOException, NoSuchAlgorithmException, DigestException {
            throws IOException, NoSuchAlgorithmException, DigestException {
        // 1. Digest the apk to generate the leaf level hashes.
        // 1. Digest the apk to generate the leaf level hashes.
        if (skipSigningBlock) {
        assertSigningBlockAlignedAndHasFullPages(signatureInfo);
        assertSigningBlockAlignedAndHasFullPages(signatureInfo);
        generateApkVerityDigestAtLeafLevel(apk, signatureInfo, salt, slice(output,
        generateApkVerityDigestAtLeafLevel(apk, signatureInfo, salt, slice(output,
                    levelOffset[levelOffset.length - 2], levelOffset[levelOffset.length - 1]));
                    levelOffset[levelOffset.length - 2], levelOffset[levelOffset.length - 1]));
        } else {
            generateFsVerityDigestAtLeafLevel(apk, slice(output,
                        levelOffset[levelOffset.length - 2], levelOffset[levelOffset.length - 1]));
        }


        // 2. Digest the lower level hashes bottom up.
        // 2. Digest the lower level hashes bottom up.
        for (int level = levelOffset.length - 3; level >= 0; level--) {
        for (int level = levelOffset.length - 3; level >= 0; level--) {