aboutsummaryrefslogtreecommitdiff
path: root/lib/sec_library/include/rambus.h
blob: 780d774b8550b184580041a369f22819189373ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Copyright (C) 2017-2020 Alibaba Group Holding Limited
 */

#ifndef INC_RAMBUS_H
#define INC_RAMBUS_H

#include "device_types.h"
#ifdef SEC_LIB_VERSION
#include "drv/common.h"
#include "device_rw.h"
#include "rambus_log.h"
#include "rambus_errcode.h"
#else
#include "common.h"
#endif

extern uint64_t g_freq_timer;
extern uint64_t g_freq_ip;
extern uint64_t g_start_ctr;
extern uint64_t g_end_ctr;
extern uint64_t g_data_len_in_bits;
extern uint32_t g_type;

enum rambus_cipher_padding_mode {
    PADDING_ZERO,
    PADDING_FF,
    PADDING_RANDOM,
};

uint32_t rb_get_random_byte(uint8_t *buf, uint32_t count);

uint32_t rb_get_random_byte_nozero(uint8_t *buf, uint32_t count);

uint32_t kdf_get_mask(uint8_t *mask, uint32_t len);

/* 1 bpc, 2 tps, 3 bps */
void rb_perf_init(uint32_t data_len_in_bits, uint32_t type);
void rb_perf_start(void);
void rb_perf_end(void);
void rb_perf_get(char *ncase);

#define DEFAULT_TIMEOUT 1000U

#ifdef CONFIG_ALG_PERF_TEST
#define RB_PERF_INIT(bits, type)                                               \
        do {                                                                   \
                if (data_len_in_bits != 0) {                                   \
                        g_data_len_in_bits = data_len_in_bits;                 \
                }                                                              \
                if (type != 0) {                                               \
                        g_type = type;                                         \
                }                                                              \
        } while (0)

#define RB_PERF_START_POINT()                                                  \
        do {                                                                   \
                g_start_ctr = ((((uint64_t)csi_coret_get_valueh() << 32U) |    \
                                csi_coret_get_value()));                       \
        } while (0)

#define RB_PERF_END_POINT()                                                    \
        do {                                                                   \
                g_end_ctr = ((((uint64_t)csi_coret_get_valueh() << 32U) |      \
                              csi_coret_get_value()));                         \
        } while (0)

#else
#define RB_PERF_INIT(...)
#define RB_PERF_START_POINT(...)
#define RB_PERF_END_POINT(...)
#endif

static inline void rb_xor(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t len) {
        for (int i = 0; i < (int)len; i++) {
                c[i] = a[i] ^ b[i];
        }
}

/**
 * @brief Get the aes sca enable config
 *
 * @param is_en is enable
 * @return uint32_t
 */
uint32_t rb_get_aes_sca(uint32_t *is_en);

/**
 * @brief Get the sm4 sca enable config
 *
 * @param is_en is enable
 * @return uint32_t
 */
uint32_t rb_get_sm4_sca(uint32_t *is_en);

/**
 * @brief Get the pka sca enable config
 *
 * @param is_en is enable
 * @return uint32_t
 */
uint32_t rb_get_pka_sca(uint32_t *is_en);

/**
 * @brief rb_cache_en
 * @return uint32_t enable: 1
 *
 */
uint32_t rb_cache_en(void);

/**
 * @brief trng init
 *
 * @return csi_error_t
 */
csi_error_t trng_init(void);

/**
 * @brief rb wait status
 *
 * @param dev
 * @param offset
 * @param mask
 * @param status
 * @return uint32_t
 */
csi_error_t rb_wait_status(Device_Handle_t *dev, const uint32_t offset, uint32_t mask,
                 uint32_t status);

/**
 * \brief          rambus crypto init.
 * \return         0 if successful, or error code
 */
uint32_t rambus_crypto_init(void);

/**
 * @brief rambus crypto uninit.
 *
 */
void rambus_crypto_uninit(void);

/**
 * \brief              rambus set cipher padding type.
 * @param padding_mode cipher padding mode
 * \return             0 if successful, or error code
 */
uint32_t rambus_enable_cipher_padding_type(enum rambus_cipher_padding_mode padding_mode);

#endif