1/* 2 * Copyright (c) 2022-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#ifndef __HVB_CRYPTO_H_ 16#define __HVB_CRYPTO_H_ 17 18#include <stdint.h> 19 20#define HASH_OK 0 21#define VERIFY_OK 0x5A5A 22 23#define BLK_WORD_SIZE_SHA256 16 24#define BLK_BYTE_SIZE_SHA256 (BLK_WORD_SIZE_SHA256 * sizeof(uint32_t)) 25 26#define IV_WORD_SIZE_SHA256 8 27#define IV_BYTE_SIZE_SHA256 (IV_WORD_SIZE_SHA256 * sizeof(uint32_t)) 28 29#define HVB_SHA256_DIGEST_BYTES 32 30#define HVB_SHA512_DIGEST_BYTES 64 31/* sha512 is 64 bytes */ 32#define HVB_HASH_MAX_BYTES 64 33 34struct hvb_rsa_pubkey { 35 uint32_t width; 36 uint32_t e; 37 uint8_t *pn; 38 uint32_t nlen; 39 uint8_t *p_rr; 40 uint32_t rlen; 41 uint64_t n_n0_i; 42}; 43 44enum hash_alg_type { 45 HASH_ALG_SHA256, 46}; 47 48struct hash_ctx_t { 49 uint32_t alg_type; 50 51 uint32_t buf_len; 52 uint32_t total_len; 53 54 uint32_t iv[IV_BYTE_SIZE_SHA256]; 55 56 uint8_t blk_buf[BLK_BYTE_SIZE_SHA256]; 57}; 58 59int hash_ctx_init(struct hash_ctx_t *hash_ctx, enum hash_alg_type); 60 61int hash_calc_update(struct hash_ctx_t *hash_ctx, const void *msg, uint32_t msg_len); 62 63int hash_calc_do_final(struct hash_ctx_t *hash_ctx, const void *msg, uint32_t msg_len, uint8_t *out, uint32_t out_len); 64 65int hash_sha256_single(const void *msg, uint32_t msg_len, uint8_t *out, uint32_t out_len); 66 67/* 68* Use the key provided in the |pkey| to verify the correctness 69* of the RSA |psign| with the length of |signlen| against an 70* expected |pdigest| of length |digestlen|. 71* 72* The data in |pkey| must match the format defined in |hvb_rsa_pubkey|. 73* 74* Return VERIFY_OK if verification success, error code otherwise. 75*/ 76int hvb_rsa_verify_pss(const struct hvb_rsa_pubkey *pkey, const uint8_t *pdigest, 77 uint32_t digestlen, uint8_t *psign, 78 uint32_t signlen, uint32_t saltlen); 79 80#endif