17310c0d0Sopenharmony_ci/*
27310c0d0Sopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
37310c0d0Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
47310c0d0Sopenharmony_ci * you may not use this file except in compliance with the License.
57310c0d0Sopenharmony_ci * You may obtain a copy of the License at
67310c0d0Sopenharmony_ci *
77310c0d0Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
87310c0d0Sopenharmony_ci *
97310c0d0Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
107310c0d0Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
117310c0d0Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127310c0d0Sopenharmony_ci * See the License for the specific language governing permissions and
137310c0d0Sopenharmony_ci * limitations under the License.
147310c0d0Sopenharmony_ci */
157310c0d0Sopenharmony_ci#ifndef __HVB_CRYPTO_H_
167310c0d0Sopenharmony_ci#define __HVB_CRYPTO_H_
177310c0d0Sopenharmony_ci
187310c0d0Sopenharmony_ci#include <stdint.h>
197310c0d0Sopenharmony_ci
207310c0d0Sopenharmony_ci#define HASH_OK     0
217310c0d0Sopenharmony_ci#define VERIFY_OK   0x5A5A
227310c0d0Sopenharmony_ci
237310c0d0Sopenharmony_ci#define BLK_WORD_SIZE_SHA256 16
247310c0d0Sopenharmony_ci#define BLK_BYTE_SIZE_SHA256 (BLK_WORD_SIZE_SHA256 * sizeof(uint32_t))
257310c0d0Sopenharmony_ci
267310c0d0Sopenharmony_ci#define IV_WORD_SIZE_SHA256  8
277310c0d0Sopenharmony_ci#define IV_BYTE_SIZE_SHA256  (IV_WORD_SIZE_SHA256 * sizeof(uint32_t))
287310c0d0Sopenharmony_ci
297310c0d0Sopenharmony_ci#define HVB_SHA256_DIGEST_BYTES 32
307310c0d0Sopenharmony_ci#define HVB_SHA512_DIGEST_BYTES 64
317310c0d0Sopenharmony_ci/* sha512 is 64 bytes */
327310c0d0Sopenharmony_ci#define HVB_HASH_MAX_BYTES      64
337310c0d0Sopenharmony_ci
347310c0d0Sopenharmony_cistruct hvb_rsa_pubkey {
357310c0d0Sopenharmony_ci    uint32_t width;
367310c0d0Sopenharmony_ci    uint32_t e;
377310c0d0Sopenharmony_ci    uint8_t *pn;
387310c0d0Sopenharmony_ci    uint32_t nlen;
397310c0d0Sopenharmony_ci    uint8_t *p_rr;
407310c0d0Sopenharmony_ci    uint32_t rlen;
417310c0d0Sopenharmony_ci    uint64_t n_n0_i;
427310c0d0Sopenharmony_ci};
437310c0d0Sopenharmony_ci
447310c0d0Sopenharmony_cienum hash_alg_type {
457310c0d0Sopenharmony_ci    HASH_ALG_SHA256,
467310c0d0Sopenharmony_ci};
477310c0d0Sopenharmony_ci
487310c0d0Sopenharmony_cistruct hash_ctx_t {
497310c0d0Sopenharmony_ci    uint32_t alg_type;
507310c0d0Sopenharmony_ci
517310c0d0Sopenharmony_ci    uint32_t buf_len;
527310c0d0Sopenharmony_ci    uint32_t total_len;
537310c0d0Sopenharmony_ci
547310c0d0Sopenharmony_ci    uint32_t iv[IV_BYTE_SIZE_SHA256];
557310c0d0Sopenharmony_ci
567310c0d0Sopenharmony_ci    uint8_t  blk_buf[BLK_BYTE_SIZE_SHA256];
577310c0d0Sopenharmony_ci};
587310c0d0Sopenharmony_ci
597310c0d0Sopenharmony_ciint hash_ctx_init(struct hash_ctx_t *hash_ctx, enum hash_alg_type);
607310c0d0Sopenharmony_ci
617310c0d0Sopenharmony_ciint hash_calc_update(struct hash_ctx_t *hash_ctx, const void *msg, uint32_t msg_len);
627310c0d0Sopenharmony_ci
637310c0d0Sopenharmony_ciint hash_calc_do_final(struct hash_ctx_t *hash_ctx, const void *msg, uint32_t msg_len, uint8_t *out, uint32_t out_len);
647310c0d0Sopenharmony_ci
657310c0d0Sopenharmony_ciint hash_sha256_single(const void *msg, uint32_t msg_len, uint8_t *out, uint32_t out_len);
667310c0d0Sopenharmony_ci
677310c0d0Sopenharmony_ci/*
687310c0d0Sopenharmony_ci* Use the key provided in the |pkey| to verify the correctness
697310c0d0Sopenharmony_ci* of the RSA |psign| with the length of |signlen| against an
707310c0d0Sopenharmony_ci* expected |pdigest| of length |digestlen|.
717310c0d0Sopenharmony_ci*
727310c0d0Sopenharmony_ci* The data in |pkey| must match the format defined in |hvb_rsa_pubkey|.
737310c0d0Sopenharmony_ci*
747310c0d0Sopenharmony_ci* Return VERIFY_OK if verification success, error code otherwise.
757310c0d0Sopenharmony_ci*/
767310c0d0Sopenharmony_ciint hvb_rsa_verify_pss(const struct hvb_rsa_pubkey *pkey, const uint8_t *pdigest,
777310c0d0Sopenharmony_ci                       uint32_t digestlen, uint8_t *psign,
787310c0d0Sopenharmony_ci                       uint32_t signlen, uint32_t saltlen);
797310c0d0Sopenharmony_ci
807310c0d0Sopenharmony_ci#endif