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