• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  *
4  * This program is free software; you can redistribute  it and/or modify it
5  * under  the terms of  the GNU General  Public License as published by the
6  * Free Software Foundation;  either version 2 of the  License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Description   : head file for cipher osal lib
18  */
19 
20 #ifndef __CIPHER_OSAL_H__
21 #define __CIPHER_OSAL_H__
22 
23 #ifndef HI_MINIBOOT_SUPPORT
24 #include <common.h>
25 #else
26 #include "string.h"
27 #include "stdio.h"
28 #endif
29 #include "common.h"
30 #include "malloc.h"
31 #include "hi_types.h"
32 #include "hi_drv_cipher.h"
33 #include "drv_cipher_ioctl.h"
34 #include "hi_mpi_cipher.h"
35 #define CIPHER_KLAD_SUPPORT
36 #define HASH_BLOCK_SIZE                     128
37 #define HASH_RESULT_MAX_LEN                 64
38 #define HASH_RESULT_MAX_LEN_IN_WORD         16
39 
40 #define HI_ERR_CIPHER_NOT_INIT              (hi_s32)(0x804D0001)
41 #define HI_ERR_CIPHER_INVALID_HANDLE        (hi_s32)(0x804D0002)
42 #define HI_ERR_CIPHER_INVALID_POINT         (hi_s32)(0x804D0003)
43 #define HI_ERR_CIPHER_INVALID_PARA          (hi_s32)(0x804D0004)
44 #define HI_ERR_CIPHER_FAILED_INIT           (hi_s32)(0x804D0005)
45 #define HI_ERR_CIPHER_FAILED_GETHANDLE      (hi_s32)(0x804D0006)
46 #define HI_ERR_CIPHER_FAILED_RELEASEHANDLE  (hi_s32)(0x804D0007)
47 #define HI_ERR_CIPHER_FAILED_CONFIGAES      (hi_s32)(0x804D0008)
48 #define HI_ERR_CIPHER_FAILED_CONFIGDES      (hi_s32)(0x804D0009)
49 #define HI_ERR_CIPHER_FAILED_ENCRYPT        (hi_s32)(0x804D000A)
50 #define HI_ERR_CIPHER_FAILED_DECRYPT        (hi_s32)(0x804D000B)
51 #define HI_ERR_CIPHER_BUSY                  (hi_s32)(0x804D000C)
52 #define HI_ERR_CIPHER_NO_AVAILABLE_RNG      (hi_s32)(0x804D000D)
53 
54 #define cipher_cpu_to_be16(v)               (((v) << 8) | ((v) >> 8))
55 
56 #define cipher_cpu_to_be32(v)                                   \
57     ((((hi_u32)(v)) >> 24) | ((((hi_u32)(v)) >> 8) & 0xff00) |  \
58      ((((hi_u32)(v)) << 8) & 0xff0000) | (((hi_u32)(v)) << 24))
59 
60 #define cipher_cpu_to_be64(x)                                           \
61     ((((hi_u64)(x) & (hi_u64)0x00000000000000ffULL) << 56) |            \
62      (((hi_u64)(x) & (hi_u64)0x000000000000ff00ULL) << 40) |            \
63      (((hi_u64)(x) & (hi_u64)0x0000000000ff0000ULL) << 24) |            \
64      (((hi_u64)(x) & (hi_u64)0x00000000ff000000ULL) << 8)  |            \
65      (((hi_u64)(x) & (hi_u64)0x000000ff00000000ULL) >> 8)  |            \
66      (((hi_u64)(x) & (hi_u64)0x0000ff0000000000ULL) >> 24) |            \
67      (((hi_u64)(x) & (hi_u64)0x00ff000000000000ULL) >> 40) |            \
68      (((hi_u64)(x) & (hi_u64)0xff00000000000000ULL) >> 56))
69 
70 #define cipher_min(a, b) ((a) < (b) ? (a) : (b))
71 
72 #define UMAP_DEVNAME_CIPHER "cipher"
73 #define UMAP_CIPHER_MINOR_BASE              50
74 
75 #define get_ulong_low(dw) (unsigned int)(dw)
76 #define get_ulong_high(dw) 0
77 #define make_ulong(low, high) (low)
78 
79 #define HI_PRINT printf
80 #define hi_err_cipher(fmt...)       HI_PRINT(fmt)
81 #define hi_info_cipher(fmt...)      // HI_PRINT(fmt)
82 
83 /* ************************* params check api ******************** */
84 #define inlet_var_over_max_return(val, max)    \
85     do {                                       \
86         if ((val) > (max)) {                   \
87             hi_err_cipher("inlet param %s = 0x%x overflow, must less than %s(0x%x).\n", \
88                 #val, val, #max, max);         \
89             return HI_ERR_CIPHER_INVALID_PARA; \
90         }                                      \
91     } while (0)
92 
93 #define inlet_var_is_zero_return(val)                           \
94     do {                                                        \
95         if ((val) == 0) {                                       \
96             hi_err_cipher("inlet param " #val " is zero\n");    \
97             return HI_ERR_CIPHER_INVALID_PARA;                  \
98         }                                                       \
99     } while (0)
100 
101 #define inlet_var_is_null_return(p)                         \
102     do {                                                    \
103         if ((p) == HI_NULL) {                               \
104             hi_err_cipher("inlet param " #p " is null\n");  \
105             return HI_ERR_CIPHER_INVALID_POINT;             \
106         }                                                   \
107     } while (0)
108 
109 #define CIPHER_MUTEX hi_void *
110 #define cipher_mutex_init(x)
111 #define cipher_mutex_lock(x)
112 #define cipher_mutex_unlock(x)
113 #define cipher_mutex_destroy(x)
114 
115 #define cipher_open(a, b, c)            (cipher_module_init(), 1)
116 #define cipher_close(x)                 (cipher_module_exit())
117 #define cipher_ioctl(dev, cmd, argp)    drv_cipher_ioctl(cmd, argp, NULL)
118 
119 #define cipher_malloc(x) malloc(x)
120 #define cipher_free(x) free(x)
121 
122 hi_void *crypto_memset(hi_void *dst, unsigned int dlen, unsigned val, unsigned int len);
123 int crypto_memcmp(const hi_void *a, const hi_void *b, unsigned len);
124 hi_void *crypto_memcpy(hi_void *dst, unsigned dstlen, const hi_void *src, unsigned len);
125 
126 hi_s32 mpi_cipher_get_random_number(hi_u32 *random_number, hi_u32 time_out_us);
127 
128 extern hi_s32 g_cipher_dev_fd;
129 extern hi_s32 g_cipher_init_counter;
130 
131 #define check_cipher_not_open_return()              \
132     do {                                            \
133         if (g_cipher_init_counter < 0) {            \
134             hi_err_cipher("CIPHER is not open.\n"); \
135             return HI_ERR_CIPHER_NOT_INIT;          \
136         }                                           \
137     } while (0)
138 
139 typedef struct {
140     hi_void *start_vir_addr;
141     hi_size_t start_phy_addr;
142     hi_u32 mmz_size;
143 } cipher_mmz_buf_t;
144 
145 #endif
146