1 /* 2 * Copyright (c) 2023-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package com.ohos.hapsigntool.codesigning.fsverity; 17 18 import java.nio.ByteBuffer; 19 import java.nio.ByteOrder; 20 import java.nio.charset.StandardCharsets; 21 22 /** 23 * Format of FsVerity digest 24 * int8[8] magic "FSVerity" 25 * le16 digestAlgorithm sha256 = 1, sha512 = 2 26 * le16 digestSize 27 * uint8[] digest 28 * 29 * @since 2023/06/05 30 */ 31 public class FsVerityDigest { 32 private static final String FSVERITY_DIGEST_MAGIC = "FSVerity"; 33 34 private static final int DIGEST_HEADER_SIZE = 12; 35 36 /** 37 * Get formatted FsVerity digest 38 * 39 * @param algoID hash algorithm id 40 * @param digest raw digest computed from input 41 * @return formatted FsVerity digest bytes 42 */ getFsVerityDigest(byte algoID, byte[] digest)43 public static byte[] getFsVerityDigest(byte algoID, byte[] digest) { 44 int size = DIGEST_HEADER_SIZE + digest.length; 45 ByteBuffer buffer = ByteBuffer.allocate(size).order(ByteOrder.LITTLE_ENDIAN); 46 buffer.put(FSVERITY_DIGEST_MAGIC.getBytes(StandardCharsets.UTF_8)); 47 buffer.putShort(algoID); 48 buffer.putShort((short) digest.length); 49 buffer.put(digest); 50 return buffer.array(); 51 } 52 } 53