Debian Codegraph @

cipher/asm-inline-s390x.h

raw
1 /* asm-inline-s390x.h - Common macros for zSeries inline assembly
2 *
3 * Copyright (C) 2020 Jussi Kivilinna <[email protected]>
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifndef GCRY_ASM_INLINE_S390X_H
22 #define GCRY_ASM_INLINE_S390X_H
23
24 #include <config.h>
25
26 #define ALWAYS_INLINE inline __attribute__((always_inline))
27
28 typedef unsigned int u128_t __attribute__ ((mode (TI)));
29
30 enum kmxx_functions_e
31 {
32 KM_FUNCTION_AES_128 = 18,
33 KM_FUNCTION_AES_192 = 19,
34 KM_FUNCTION_AES_256 = 20,
35 KM_FUNCTION_XTS_AES_128 = 50,
36 KM_FUNCTION_XTS_AES_256 = 52,
37
38 KMID_FUNCTION_SHA1 = 1,
39 KMID_FUNCTION_SHA256 = 2,
40 KMID_FUNCTION_SHA512 = 3,
41 KMID_FUNCTION_SHA3_224 = 32,
42 KMID_FUNCTION_SHA3_256 = 33,
43 KMID_FUNCTION_SHA3_384 = 34,
44 KMID_FUNCTION_SHA3_512 = 35,
45 KMID_FUNCTION_SHAKE128 = 36,
46 KMID_FUNCTION_SHAKE256 = 37,
47 KMID_FUNCTION_GHASH = 65,
48
49 PCC_FUNCTION_NIST_P256 = 64,
50 PCC_FUNCTION_NIST_P384 = 65,
51 PCC_FUNCTION_NIST_P521 = 66,
52 PCC_FUNCTION_ED25519 = 72,
53 PCC_FUNCTION_ED448 = 73,
54 PCC_FUNCTION_X25519 = 80,
55 PCC_FUNCTION_X448 = 81
56 };
57
58 enum kmxx_function_flags_e
59 {
60 KM_ENCRYPT = 0 << 7,
61 KM_DECRYPT = 1 << 7,
62
63 KMF_LCFB_16 = 16 << 24,
64
65 KMA_LPC = 1 << 8,
66 KMA_LAAD = 1 << 9,
67 KMA_HS = 1 << 10,
68
69 KLMD_PADDING_STATE = 1 << 8,
70 };
71
72 static ALWAYS_INLINE u128_t km_function_to_mask(enum kmxx_functions_e func)
73 {
74 return (u128_t)1 << (127 - func);
75 }
76
77 static inline u128_t kimd_query(void)
78 {
79 static u128_t function_codes = 0;
80 static int initialized = 0;
81 register unsigned long reg0 asm("0") = 0;
82 register void *reg1 asm("1") = &function_codes;
83 u128_t r1;
84
85 if (initialized)
86 return function_codes;
87
88 asm volatile ("0: .insn rre,0xb93e << 16, 0, %[r1]\n\t"
89 " brc 1,0b\n\t"
90 : [r1] "=a" (r1)
91 : [reg0] "r" (reg0), [reg1] "r" (reg1)
92 : "cc", "memory");
93
94 initialized = 1;
95 return function_codes;
96 }
97
98 static inline u128_t klmd_query(void)
99 {
100 static u128_t function_codes = 0;
101 static int initialized = 0;
102 register unsigned long reg0 asm("0") = 0;
103 register void *reg1 asm("1") = &function_codes;
104 u128_t r1;
105
106 if (initialized)
107 return function_codes;
108
109 asm volatile ("0: .insn rre,0xb93f << 16, 0, %[r1]\n\t"
110 " brc 1,0b\n\t"
111 : [r1] "=a" (r1)
112 : [reg0] "r" (reg0), [reg1] "r" (reg1)
113 : "cc", "memory");
114
115 initialized = 1;
116 return function_codes;
117 }
118
119 static inline u128_t pcc_query(void)
120 {
121 static u128_t function_codes = 0;
122 static int initialized = 0;
123 register unsigned long reg0 asm("0") = 0;
124 register void *reg1 asm("1") = &function_codes;
125
126 if (initialized)
127 return function_codes;
128
129 asm volatile ("0: .insn rre,0xb92c << 16, 0, 0\n\t"
130 " brc 1,0b\n\t"
131 :
132 : [reg0] "r" (reg0), [reg1] "r" (reg1)
133 : "cc", "memory");
134
135 initialized = 1;
136 return function_codes;
137 }
138
139 static ALWAYS_INLINE void
140 kimd_execute(unsigned int func, void *param_block, const void *src,
141 size_t src_len)
142 {
143 register unsigned long reg0 asm("0") = func;
144 register byte *reg1 asm("1") = param_block;
145 u128_t r1 = ((u128_t)(uintptr_t)src << 64) | (u64)src_len;
146
147 asm volatile ("0: .insn rre,0xb93e << 16, 0, %[r1]\n\t"
148 " brc 1,0b\n\t"
149 : [r1] "+a" (r1)
150 : [func] "r" (reg0), [param_ptr] "r" (reg1)
151 : "cc", "memory");
152 }
153
154 static ALWAYS_INLINE void
155 klmd_execute(unsigned int func, void *param_block, const void *src,
156 size_t src_len)
157 {
158 register unsigned long reg0 asm("0") = func;
159 register byte *reg1 asm("1") = param_block;
160 u128_t r1 = ((u128_t)(uintptr_t)src << 64) | (u64)src_len;
161
162 asm volatile ("0: .insn rre,0xb93f << 16, 0, %[r1]\n\t"
163 " brc 1,0b\n\t"
164 : [func] "+r" (reg0), [r1] "+a" (r1)
165 : [param_ptr] "r" (reg1)
166 : "cc", "memory");
167 }
168
169 static ALWAYS_INLINE void
170 klmd_shake_execute(unsigned int func, void *param_block, void *dst,
171 size_t dst_len, const void *src, size_t src_len)
172 {
173 register unsigned long reg0 asm("0") = func;
174 register byte *reg1 asm("1") = param_block;
175 u128_t r1 = ((u128_t)(uintptr_t)dst << 64) | (u64)dst_len;
176 u128_t r2 = ((u128_t)(uintptr_t)src << 64) | (u64)src_len;
177
178 asm volatile ("0: .insn rre,0xb93f << 16, %[r1], %[r2]\n\t"
179 " brc 1,0b\n\t"
180 : [func] "+r" (reg0), [r1] "+a" (r1), [r2] "+a" (r2)
181 : [param_ptr] "r" (reg1)
182 : "cc", "memory");
183 }
184
185 static ALWAYS_INLINE unsigned int
186 pcc_scalar_multiply(unsigned int func, void *param_block)
187 {
188 register unsigned long reg0 asm("0") = func;
189 register byte *reg1 asm("1") = param_block;
190 register unsigned long error = 0;
191
192 asm volatile ("0: .insn rre,0xb92c << 16, 0, 0\n\t"
193 " brc 1,0b\n\t"
194 " brc 7,1f\n\t"
195 " j 2f\n\t"
196 "1: lhi %[error], 1\n\t"
197 "2:\n\t"
198 : [func] "+r" (reg0), [error] "+r" (error)
199 : [param_ptr] "r" (reg1)
200 : "cc", "memory");
201
202 return error;
203 }
204
205 #endif /* GCRY_ASM_INLINE_S390X_H */