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

Commit 9d4f6963 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Fix fsverity root hash format"

parents 2ef26bf2 2120323f
Loading
Loading
Loading
Loading
+12 −9
Original line number Original line Diff line number Diff line
@@ -106,18 +106,22 @@ abstract class ApkVerityBuilder {
        calculateFsveritySignatureInternal(apk, signatureInfo, null, null, header, extensions);
        calculateFsveritySignatureInternal(apk, signatureInfo, null, null, header, extensions);


        MessageDigest md = MessageDigest.getInstance(JCA_DIGEST_ALGORITHM);
        MessageDigest md = MessageDigest.getInstance(JCA_DIGEST_ALGORITHM);
        md.update(DEFAULT_SALT);
        md.update(header);
        md.update(verityBlock);
        md.update(extensions);
        md.update(apkDigest);
        md.update(apkDigest);
        return md.digest();
        return md.digest();
    }
    }


    /**
     * Internal method to generate various parts of FSVerity constructs, including the header,
     * extensions, Merkle tree, and the tree's root hash.  The output buffer is flipped to the
     * generated data size and is readey for consuming.
     */
    private static void calculateFsveritySignatureInternal(
    private static void calculateFsveritySignatureInternal(
            RandomAccessFile apk, SignatureInfo signatureInfo, ByteBuffer treeOutput,
            RandomAccessFile apk, SignatureInfo signatureInfo, ByteBuffer treeOutput,
            ByteBuffer rootHashOutput, ByteBuffer headerOutput, ByteBuffer extensionsOutput)
            ByteBuffer rootHashOutput, ByteBuffer headerOutput, ByteBuffer extensionsOutput)
            throws IOException, NoSuchAlgorithmException, DigestException {
            throws IOException, NoSuchAlgorithmException, DigestException {
        assertSigningBlockAlignedAndHasFullPages(signatureInfo);
        assertSigningBlockAlignedAndHasFullPages(signatureInfo);

        long signingBlockSize =
        long signingBlockSize =
                signatureInfo.centralDirOffset - signatureInfo.apkSigningBlockOffset;
                signatureInfo.centralDirOffset - signatureInfo.apkSigningBlockOffset;
        long dataSize = apk.length() - signingBlockSize - ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_SIZE;
        long dataSize = apk.length() - signingBlockSize - ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_SIZE;
@@ -128,6 +132,7 @@ abstract class ApkVerityBuilder {
                    levelOffset, treeOutput);
                    levelOffset, treeOutput);
            if (rootHashOutput != null) {
            if (rootHashOutput != null) {
                rootHashOutput.put(apkRootHash);
                rootHashOutput.put(apkRootHash);
                rootHashOutput.flip();
            }
            }
        }
        }


@@ -333,9 +338,9 @@ abstract class ApkVerityBuilder {
        buffer.put((byte) 0);               // auth block offset, disabled here
        buffer.put((byte) 0);               // auth block offset, disabled here
        buffer.put((byte) 2);               // extension count
        buffer.put((byte) 2);               // extension count
        buffer.put(salt);                   // salt (8 bytes)
        buffer.put(salt);                   // salt (8 bytes)
        // skip(buffer, 22);                // reserved
        skip(buffer, 22);                   // reserved


        buffer.rewind();
        buffer.flip();
        return buffer;
        return buffer;
    }
    }


@@ -396,12 +401,10 @@ abstract class ApkVerityBuilder {
            buffer.put((byte) ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_SIZE);  // length
            buffer.put((byte) ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_SIZE);  // length
            skip(buffer, 7);                                            // reserved
            skip(buffer, 7);                                            // reserved
            buffer.putInt(Math.toIntExact(signingBlockOffset));         // databytes
            buffer.putInt(Math.toIntExact(signingBlockOffset));         // databytes

            skip(buffer, kPadding);                                     // padding
            // There are extra kPadding bytes of 0s here, included in the total size field of the
            // extension header. The output ByteBuffer is assumed to be initialized to 0.
        }
        }


        buffer.rewind();
        buffer.flip();
        return buffer;
        return buffer;
    }
    }