diff options
Diffstat (limited to 'src/lib/libcrypto/engine')
-rw-r--r-- | src/lib/libcrypto/engine/Makefile | 417 | ||||
-rw-r--r-- | src/lib/libcrypto/engine/eng_aesni.c | 570 | ||||
-rw-r--r-- | src/lib/libcrypto/engine/eng_all.c | 5 | ||||
-rw-r--r-- | src/lib/libcrypto/engine/eng_cryptodev.c | 1418 | ||||
-rw-r--r-- | src/lib/libcrypto/engine/engine.h | 4 | ||||
-rw-r--r-- | src/lib/libcrypto/engine/enginetest.c | 283 | ||||
-rw-r--r-- | src/lib/libcrypto/engine/hw_cryptodev.c | 1367 |
7 files changed, 4061 insertions, 3 deletions
diff --git a/src/lib/libcrypto/engine/Makefile b/src/lib/libcrypto/engine/Makefile new file mode 100644 index 0000000000..9c214824eb --- /dev/null +++ b/src/lib/libcrypto/engine/Makefile | |||
@@ -0,0 +1,417 @@ | |||
1 | # | ||
2 | # OpenSSL/crypto/engine/Makefile | ||
3 | # | ||
4 | |||
5 | DIR= engine | ||
6 | TOP= ../.. | ||
7 | CC= cc | ||
8 | INCLUDES= -I.. -I$(TOP) -I../../include | ||
9 | CFLAG=-g | ||
10 | MAKEFILE= Makefile | ||
11 | AR= ar r | ||
12 | |||
13 | CFLAGS= $(INCLUDES) $(CFLAG) | ||
14 | |||
15 | GENERAL=Makefile | ||
16 | TEST= enginetest.c | ||
17 | APPS= | ||
18 | |||
19 | LIB=$(TOP)/libcrypto.a | ||
20 | LIBSRC= eng_err.c eng_lib.c eng_list.c eng_init.c eng_ctrl.c \ | ||
21 | eng_table.c eng_pkey.c eng_fat.c eng_all.c \ | ||
22 | tb_rsa.c tb_dsa.c tb_ecdsa.c tb_dh.c tb_ecdh.c tb_rand.c tb_store.c \ | ||
23 | tb_cipher.c tb_digest.c tb_pkmeth.c tb_asnmth.c \ | ||
24 | eng_openssl.c eng_cnf.c eng_dyn.c eng_cryptodev.c | ||
25 | LIBOBJ= eng_err.o eng_lib.o eng_list.o eng_init.o eng_ctrl.o \ | ||
26 | eng_table.o eng_pkey.o eng_fat.o eng_all.o \ | ||
27 | tb_rsa.o tb_dsa.o tb_ecdsa.o tb_dh.o tb_ecdh.o tb_rand.o tb_store.o \ | ||
28 | tb_cipher.o tb_digest.o tb_pkmeth.o tb_asnmth.o \ | ||
29 | eng_openssl.o eng_cnf.o eng_dyn.o eng_cryptodev.o | ||
30 | |||
31 | SRC= $(LIBSRC) | ||
32 | |||
33 | EXHEADER= engine.h | ||
34 | HEADER= $(EXHEADER) | ||
35 | |||
36 | ALL= $(GENERAL) $(SRC) $(HEADER) | ||
37 | |||
38 | top: | ||
39 | (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all) | ||
40 | |||
41 | all: lib | ||
42 | |||
43 | lib: $(LIBOBJ) | ||
44 | $(AR) $(LIB) $(LIBOBJ) | ||
45 | $(RANLIB) $(LIB) || echo Never mind. | ||
46 | @touch lib | ||
47 | |||
48 | files: | ||
49 | $(PERL) $(TOP)/util/files.pl Makefile >> $(TOP)/MINFO | ||
50 | |||
51 | links: | ||
52 | @$(PERL) $(TOP)/util/mklink.pl ../../include/openssl $(EXHEADER) | ||
53 | @$(PERL) $(TOP)/util/mklink.pl ../../test $(TEST) | ||
54 | @$(PERL) $(TOP)/util/mklink.pl ../../apps $(APPS) | ||
55 | |||
56 | install: | ||
57 | @[ -n "$(INSTALLTOP)" ] # should be set by top Makefile... | ||
58 | @headerlist="$(EXHEADER)"; for i in $$headerlist ; \ | ||
59 | do \ | ||
60 | (cp $$i $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i; \ | ||
61 | chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i ); \ | ||
62 | done; | ||
63 | |||
64 | tags: | ||
65 | ctags $(SRC) | ||
66 | |||
67 | tests: | ||
68 | |||
69 | lint: | ||
70 | lint -DLINT $(INCLUDES) $(SRC)>fluff | ||
71 | |||
72 | depend: | ||
73 | @[ -n "$(MAKEDEPEND)" ] # should be set by upper Makefile... | ||
74 | $(MAKEDEPEND) -- $(CFLAG) $(INCLUDES) $(DEPFLAG) -- $(PROGS) $(LIBSRC) | ||
75 | |||
76 | dclean: | ||
77 | $(PERL) -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new | ||
78 | mv -f Makefile.new $(MAKEFILE) | ||
79 | |||
80 | clean: | ||
81 | rm -f *.o */*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff | ||
82 | |||
83 | # DO NOT DELETE THIS LINE -- make depend depends on it. | ||
84 | |||
85 | eng_all.o: ../../e_os.h ../../include/openssl/asn1.h | ||
86 | eng_all.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h | ||
87 | eng_all.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h | ||
88 | eng_all.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h | ||
89 | eng_all.o: ../../include/openssl/ecdsa.h ../../include/openssl/engine.h | ||
90 | eng_all.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
91 | eng_all.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h | ||
92 | eng_all.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h | ||
93 | eng_all.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
94 | eng_all.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h | ||
95 | eng_all.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
96 | eng_all.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h | ||
97 | eng_all.o: ../../include/openssl/x509_vfy.h ../cryptlib.h eng_all.c eng_int.h | ||
98 | eng_cnf.o: ../../e_os.h ../../include/openssl/asn1.h | ||
99 | eng_cnf.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h | ||
100 | eng_cnf.o: ../../include/openssl/conf.h ../../include/openssl/crypto.h | ||
101 | eng_cnf.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h | ||
102 | eng_cnf.o: ../../include/openssl/ecdh.h ../../include/openssl/ecdsa.h | ||
103 | eng_cnf.o: ../../include/openssl/engine.h ../../include/openssl/err.h | ||
104 | eng_cnf.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h | ||
105 | eng_cnf.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h | ||
106 | eng_cnf.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h | ||
107 | eng_cnf.o: ../../include/openssl/ossl_typ.h ../../include/openssl/pkcs7.h | ||
108 | eng_cnf.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h | ||
109 | eng_cnf.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
110 | eng_cnf.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h | ||
111 | eng_cnf.o: ../cryptlib.h eng_cnf.c eng_int.h | ||
112 | eng_cryptodev.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h | ||
113 | eng_cryptodev.o: ../../include/openssl/bn.h ../../include/openssl/buffer.h | ||
114 | eng_cryptodev.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h | ||
115 | eng_cryptodev.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h | ||
116 | eng_cryptodev.o: ../../include/openssl/ecdsa.h ../../include/openssl/engine.h | ||
117 | eng_cryptodev.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h | ||
118 | eng_cryptodev.o: ../../include/openssl/obj_mac.h | ||
119 | eng_cryptodev.o: ../../include/openssl/objects.h | ||
120 | eng_cryptodev.o: ../../include/openssl/opensslconf.h | ||
121 | eng_cryptodev.o: ../../include/openssl/opensslv.h | ||
122 | eng_cryptodev.o: ../../include/openssl/ossl_typ.h ../../include/openssl/pkcs7.h | ||
123 | eng_cryptodev.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h | ||
124 | eng_cryptodev.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
125 | eng_cryptodev.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h | ||
126 | eng_cryptodev.o: eng_cryptodev.c | ||
127 | eng_ctrl.o: ../../e_os.h ../../include/openssl/asn1.h | ||
128 | eng_ctrl.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h | ||
129 | eng_ctrl.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h | ||
130 | eng_ctrl.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h | ||
131 | eng_ctrl.o: ../../include/openssl/ecdsa.h ../../include/openssl/engine.h | ||
132 | eng_ctrl.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
133 | eng_ctrl.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h | ||
134 | eng_ctrl.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h | ||
135 | eng_ctrl.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
136 | eng_ctrl.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h | ||
137 | eng_ctrl.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
138 | eng_ctrl.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h | ||
139 | eng_ctrl.o: ../../include/openssl/x509_vfy.h ../cryptlib.h eng_ctrl.c eng_int.h | ||
140 | eng_dyn.o: ../../e_os.h ../../include/openssl/asn1.h | ||
141 | eng_dyn.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h | ||
142 | eng_dyn.o: ../../include/openssl/crypto.h ../../include/openssl/dso.h | ||
143 | eng_dyn.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h | ||
144 | eng_dyn.o: ../../include/openssl/ecdh.h ../../include/openssl/ecdsa.h | ||
145 | eng_dyn.o: ../../include/openssl/engine.h ../../include/openssl/err.h | ||
146 | eng_dyn.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h | ||
147 | eng_dyn.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h | ||
148 | eng_dyn.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h | ||
149 | eng_dyn.o: ../../include/openssl/ossl_typ.h ../../include/openssl/pkcs7.h | ||
150 | eng_dyn.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h | ||
151 | eng_dyn.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
152 | eng_dyn.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h | ||
153 | eng_dyn.o: ../cryptlib.h eng_dyn.c eng_int.h | ||
154 | eng_err.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h | ||
155 | eng_err.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
156 | eng_err.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h | ||
157 | eng_err.o: ../../include/openssl/ecdh.h ../../include/openssl/ecdsa.h | ||
158 | eng_err.o: ../../include/openssl/engine.h ../../include/openssl/err.h | ||
159 | eng_err.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h | ||
160 | eng_err.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h | ||
161 | eng_err.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h | ||
162 | eng_err.o: ../../include/openssl/ossl_typ.h ../../include/openssl/pkcs7.h | ||
163 | eng_err.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h | ||
164 | eng_err.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
165 | eng_err.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h | ||
166 | eng_err.o: eng_err.c | ||
167 | eng_fat.o: ../../e_os.h ../../include/openssl/asn1.h | ||
168 | eng_fat.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h | ||
169 | eng_fat.o: ../../include/openssl/conf.h ../../include/openssl/crypto.h | ||
170 | eng_fat.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h | ||
171 | eng_fat.o: ../../include/openssl/ecdh.h ../../include/openssl/ecdsa.h | ||
172 | eng_fat.o: ../../include/openssl/engine.h ../../include/openssl/err.h | ||
173 | eng_fat.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h | ||
174 | eng_fat.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h | ||
175 | eng_fat.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h | ||
176 | eng_fat.o: ../../include/openssl/ossl_typ.h ../../include/openssl/pkcs7.h | ||
177 | eng_fat.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h | ||
178 | eng_fat.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
179 | eng_fat.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h | ||
180 | eng_fat.o: ../cryptlib.h eng_fat.c eng_int.h | ||
181 | eng_init.o: ../../e_os.h ../../include/openssl/asn1.h | ||
182 | eng_init.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h | ||
183 | eng_init.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h | ||
184 | eng_init.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h | ||
185 | eng_init.o: ../../include/openssl/ecdsa.h ../../include/openssl/engine.h | ||
186 | eng_init.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
187 | eng_init.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h | ||
188 | eng_init.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h | ||
189 | eng_init.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
190 | eng_init.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h | ||
191 | eng_init.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
192 | eng_init.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h | ||
193 | eng_init.o: ../../include/openssl/x509_vfy.h ../cryptlib.h eng_init.c eng_int.h | ||
194 | eng_lib.o: ../../e_os.h ../../include/openssl/asn1.h | ||
195 | eng_lib.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h | ||
196 | eng_lib.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h | ||
197 | eng_lib.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h | ||
198 | eng_lib.o: ../../include/openssl/ecdsa.h ../../include/openssl/engine.h | ||
199 | eng_lib.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
200 | eng_lib.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h | ||
201 | eng_lib.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h | ||
202 | eng_lib.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
203 | eng_lib.o: ../../include/openssl/pkcs7.h ../../include/openssl/rand.h | ||
204 | eng_lib.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h | ||
205 | eng_lib.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
206 | eng_lib.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h | ||
207 | eng_lib.o: ../cryptlib.h eng_int.h eng_lib.c | ||
208 | eng_list.o: ../../e_os.h ../../include/openssl/asn1.h | ||
209 | eng_list.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h | ||
210 | eng_list.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h | ||
211 | eng_list.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h | ||
212 | eng_list.o: ../../include/openssl/ecdsa.h ../../include/openssl/engine.h | ||
213 | eng_list.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
214 | eng_list.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h | ||
215 | eng_list.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h | ||
216 | eng_list.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
217 | eng_list.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h | ||
218 | eng_list.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
219 | eng_list.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h | ||
220 | eng_list.o: ../../include/openssl/x509_vfy.h ../cryptlib.h eng_int.h eng_list.c | ||
221 | eng_openssl.o: ../../e_os.h ../../include/openssl/asn1.h | ||
222 | eng_openssl.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h | ||
223 | eng_openssl.o: ../../include/openssl/crypto.h ../../include/openssl/dh.h | ||
224 | eng_openssl.o: ../../include/openssl/dsa.h ../../include/openssl/dso.h | ||
225 | eng_openssl.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h | ||
226 | eng_openssl.o: ../../include/openssl/ecdh.h ../../include/openssl/ecdsa.h | ||
227 | eng_openssl.o: ../../include/openssl/engine.h ../../include/openssl/err.h | ||
228 | eng_openssl.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h | ||
229 | eng_openssl.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h | ||
230 | eng_openssl.o: ../../include/openssl/opensslconf.h | ||
231 | eng_openssl.o: ../../include/openssl/opensslv.h | ||
232 | eng_openssl.o: ../../include/openssl/ossl_typ.h ../../include/openssl/pem.h | ||
233 | eng_openssl.o: ../../include/openssl/pem2.h ../../include/openssl/pkcs7.h | ||
234 | eng_openssl.o: ../../include/openssl/rand.h ../../include/openssl/rc4.h | ||
235 | eng_openssl.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h | ||
236 | eng_openssl.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
237 | eng_openssl.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h | ||
238 | eng_openssl.o: ../../include/openssl/x509_vfy.h ../cryptlib.h eng_openssl.c | ||
239 | eng_pkey.o: ../../e_os.h ../../include/openssl/asn1.h | ||
240 | eng_pkey.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h | ||
241 | eng_pkey.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h | ||
242 | eng_pkey.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h | ||
243 | eng_pkey.o: ../../include/openssl/ecdsa.h ../../include/openssl/engine.h | ||
244 | eng_pkey.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
245 | eng_pkey.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h | ||
246 | eng_pkey.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h | ||
247 | eng_pkey.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
248 | eng_pkey.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h | ||
249 | eng_pkey.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
250 | eng_pkey.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h | ||
251 | eng_pkey.o: ../../include/openssl/x509_vfy.h ../cryptlib.h eng_int.h eng_pkey.c | ||
252 | eng_table.o: ../../e_os.h ../../include/openssl/asn1.h | ||
253 | eng_table.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h | ||
254 | eng_table.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h | ||
255 | eng_table.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h | ||
256 | eng_table.o: ../../include/openssl/ecdsa.h ../../include/openssl/engine.h | ||
257 | eng_table.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
258 | eng_table.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h | ||
259 | eng_table.o: ../../include/openssl/objects.h | ||
260 | eng_table.o: ../../include/openssl/opensslconf.h | ||
261 | eng_table.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
262 | eng_table.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h | ||
263 | eng_table.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
264 | eng_table.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h | ||
265 | eng_table.o: ../../include/openssl/x509_vfy.h ../cryptlib.h eng_int.h | ||
266 | eng_table.o: eng_table.c | ||
267 | tb_asnmth.o: ../../e_os.h ../../include/openssl/asn1.h | ||
268 | tb_asnmth.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h | ||
269 | tb_asnmth.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h | ||
270 | tb_asnmth.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h | ||
271 | tb_asnmth.o: ../../include/openssl/ecdsa.h ../../include/openssl/engine.h | ||
272 | tb_asnmth.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
273 | tb_asnmth.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h | ||
274 | tb_asnmth.o: ../../include/openssl/objects.h | ||
275 | tb_asnmth.o: ../../include/openssl/opensslconf.h | ||
276 | tb_asnmth.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
277 | tb_asnmth.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h | ||
278 | tb_asnmth.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
279 | tb_asnmth.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h | ||
280 | tb_asnmth.o: ../../include/openssl/x509_vfy.h ../asn1/asn1_locl.h ../cryptlib.h | ||
281 | tb_asnmth.o: eng_int.h tb_asnmth.c | ||
282 | tb_cipher.o: ../../e_os.h ../../include/openssl/asn1.h | ||
283 | tb_cipher.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h | ||
284 | tb_cipher.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h | ||
285 | tb_cipher.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h | ||
286 | tb_cipher.o: ../../include/openssl/ecdsa.h ../../include/openssl/engine.h | ||
287 | tb_cipher.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
288 | tb_cipher.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h | ||
289 | tb_cipher.o: ../../include/openssl/objects.h | ||
290 | tb_cipher.o: ../../include/openssl/opensslconf.h | ||
291 | tb_cipher.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
292 | tb_cipher.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h | ||
293 | tb_cipher.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
294 | tb_cipher.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h | ||
295 | tb_cipher.o: ../../include/openssl/x509_vfy.h ../cryptlib.h eng_int.h | ||
296 | tb_cipher.o: tb_cipher.c | ||
297 | tb_dh.o: ../../e_os.h ../../include/openssl/asn1.h ../../include/openssl/bio.h | ||
298 | tb_dh.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
299 | tb_dh.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h | ||
300 | tb_dh.o: ../../include/openssl/ecdh.h ../../include/openssl/ecdsa.h | ||
301 | tb_dh.o: ../../include/openssl/engine.h ../../include/openssl/err.h | ||
302 | tb_dh.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h | ||
303 | tb_dh.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h | ||
304 | tb_dh.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h | ||
305 | tb_dh.o: ../../include/openssl/ossl_typ.h ../../include/openssl/pkcs7.h | ||
306 | tb_dh.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h | ||
307 | tb_dh.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
308 | tb_dh.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h | ||
309 | tb_dh.o: ../cryptlib.h eng_int.h tb_dh.c | ||
310 | tb_digest.o: ../../e_os.h ../../include/openssl/asn1.h | ||
311 | tb_digest.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h | ||
312 | tb_digest.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h | ||
313 | tb_digest.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h | ||
314 | tb_digest.o: ../../include/openssl/ecdsa.h ../../include/openssl/engine.h | ||
315 | tb_digest.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
316 | tb_digest.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h | ||
317 | tb_digest.o: ../../include/openssl/objects.h | ||
318 | tb_digest.o: ../../include/openssl/opensslconf.h | ||
319 | tb_digest.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
320 | tb_digest.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h | ||
321 | tb_digest.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
322 | tb_digest.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h | ||
323 | tb_digest.o: ../../include/openssl/x509_vfy.h ../cryptlib.h eng_int.h | ||
324 | tb_digest.o: tb_digest.c | ||
325 | tb_dsa.o: ../../e_os.h ../../include/openssl/asn1.h ../../include/openssl/bio.h | ||
326 | tb_dsa.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
327 | tb_dsa.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h | ||
328 | tb_dsa.o: ../../include/openssl/ecdh.h ../../include/openssl/ecdsa.h | ||
329 | tb_dsa.o: ../../include/openssl/engine.h ../../include/openssl/err.h | ||
330 | tb_dsa.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h | ||
331 | tb_dsa.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h | ||
332 | tb_dsa.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h | ||
333 | tb_dsa.o: ../../include/openssl/ossl_typ.h ../../include/openssl/pkcs7.h | ||
334 | tb_dsa.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h | ||
335 | tb_dsa.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
336 | tb_dsa.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h | ||
337 | tb_dsa.o: ../cryptlib.h eng_int.h tb_dsa.c | ||
338 | tb_ecdh.o: ../../e_os.h ../../include/openssl/asn1.h | ||
339 | tb_ecdh.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h | ||
340 | tb_ecdh.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h | ||
341 | tb_ecdh.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h | ||
342 | tb_ecdh.o: ../../include/openssl/ecdsa.h ../../include/openssl/engine.h | ||
343 | tb_ecdh.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
344 | tb_ecdh.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h | ||
345 | tb_ecdh.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h | ||
346 | tb_ecdh.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
347 | tb_ecdh.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h | ||
348 | tb_ecdh.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
349 | tb_ecdh.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h | ||
350 | tb_ecdh.o: ../../include/openssl/x509_vfy.h ../cryptlib.h eng_int.h tb_ecdh.c | ||
351 | tb_ecdsa.o: ../../e_os.h ../../include/openssl/asn1.h | ||
352 | tb_ecdsa.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h | ||
353 | tb_ecdsa.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h | ||
354 | tb_ecdsa.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h | ||
355 | tb_ecdsa.o: ../../include/openssl/ecdsa.h ../../include/openssl/engine.h | ||
356 | tb_ecdsa.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
357 | tb_ecdsa.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h | ||
358 | tb_ecdsa.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h | ||
359 | tb_ecdsa.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
360 | tb_ecdsa.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h | ||
361 | tb_ecdsa.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
362 | tb_ecdsa.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h | ||
363 | tb_ecdsa.o: ../../include/openssl/x509_vfy.h ../cryptlib.h eng_int.h tb_ecdsa.c | ||
364 | tb_pkmeth.o: ../../e_os.h ../../include/openssl/asn1.h | ||
365 | tb_pkmeth.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h | ||
366 | tb_pkmeth.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h | ||
367 | tb_pkmeth.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h | ||
368 | tb_pkmeth.o: ../../include/openssl/ecdsa.h ../../include/openssl/engine.h | ||
369 | tb_pkmeth.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
370 | tb_pkmeth.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h | ||
371 | tb_pkmeth.o: ../../include/openssl/objects.h | ||
372 | tb_pkmeth.o: ../../include/openssl/opensslconf.h | ||
373 | tb_pkmeth.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
374 | tb_pkmeth.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h | ||
375 | tb_pkmeth.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
376 | tb_pkmeth.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h | ||
377 | tb_pkmeth.o: ../../include/openssl/x509_vfy.h ../cryptlib.h eng_int.h | ||
378 | tb_pkmeth.o: tb_pkmeth.c | ||
379 | tb_rand.o: ../../e_os.h ../../include/openssl/asn1.h | ||
380 | tb_rand.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h | ||
381 | tb_rand.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h | ||
382 | tb_rand.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h | ||
383 | tb_rand.o: ../../include/openssl/ecdsa.h ../../include/openssl/engine.h | ||
384 | tb_rand.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
385 | tb_rand.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h | ||
386 | tb_rand.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h | ||
387 | tb_rand.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
388 | tb_rand.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h | ||
389 | tb_rand.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
390 | tb_rand.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h | ||
391 | tb_rand.o: ../../include/openssl/x509_vfy.h ../cryptlib.h eng_int.h tb_rand.c | ||
392 | tb_rsa.o: ../../e_os.h ../../include/openssl/asn1.h ../../include/openssl/bio.h | ||
393 | tb_rsa.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
394 | tb_rsa.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h | ||
395 | tb_rsa.o: ../../include/openssl/ecdh.h ../../include/openssl/ecdsa.h | ||
396 | tb_rsa.o: ../../include/openssl/engine.h ../../include/openssl/err.h | ||
397 | tb_rsa.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h | ||
398 | tb_rsa.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h | ||
399 | tb_rsa.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h | ||
400 | tb_rsa.o: ../../include/openssl/ossl_typ.h ../../include/openssl/pkcs7.h | ||
401 | tb_rsa.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h | ||
402 | tb_rsa.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
403 | tb_rsa.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h | ||
404 | tb_rsa.o: ../cryptlib.h eng_int.h tb_rsa.c | ||
405 | tb_store.o: ../../e_os.h ../../include/openssl/asn1.h | ||
406 | tb_store.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h | ||
407 | tb_store.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h | ||
408 | tb_store.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h | ||
409 | tb_store.o: ../../include/openssl/ecdsa.h ../../include/openssl/engine.h | ||
410 | tb_store.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
411 | tb_store.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h | ||
412 | tb_store.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h | ||
413 | tb_store.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
414 | tb_store.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h | ||
415 | tb_store.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
416 | tb_store.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h | ||
417 | tb_store.o: ../../include/openssl/x509_vfy.h ../cryptlib.h eng_int.h tb_store.c | ||
diff --git a/src/lib/libcrypto/engine/eng_aesni.c b/src/lib/libcrypto/engine/eng_aesni.c new file mode 100644 index 0000000000..5fdb33bfde --- /dev/null +++ b/src/lib/libcrypto/engine/eng_aesni.c | |||
@@ -0,0 +1,570 @@ | |||
1 | /* | ||
2 | * Support for Intel AES-NI intruction set | ||
3 | * Author: Huang Ying <ying.huang@intel.com> | ||
4 | * | ||
5 | * Intel AES-NI is a new set of Single Instruction Multiple Data | ||
6 | * (SIMD) instructions that are going to be introduced in the next | ||
7 | * generation of Intel processor, as of 2009. These instructions | ||
8 | * enable fast and secure data encryption and decryption, using the | ||
9 | * Advanced Encryption Standard (AES), defined by FIPS Publication | ||
10 | * number 197. The architecture introduces six instructions that | ||
11 | * offer full hardware support for AES. Four of them support high | ||
12 | * performance data encryption and decryption, and the other two | ||
13 | * instructions support the AES key expansion procedure. | ||
14 | * | ||
15 | * The white paper can be downloaded from: | ||
16 | * http://softwarecommunity.intel.com/isn/downloads/intelavx/AES-Instructions-Set_WP.pdf | ||
17 | * | ||
18 | * This file is based on engines/e_padlock.c | ||
19 | */ | ||
20 | |||
21 | /* ==================================================================== | ||
22 | * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved. | ||
23 | * | ||
24 | * Redistribution and use in source and binary forms, with or without | ||
25 | * modification, are permitted provided that the following conditions | ||
26 | * are met: | ||
27 | * | ||
28 | * 1. Redistributions of source code must retain the above copyright | ||
29 | * notice, this list of conditions and the following disclaimer. | ||
30 | * | ||
31 | * 2. Redistributions in binary form must reproduce the above copyright | ||
32 | * notice, this list of conditions and the following disclaimer in | ||
33 | * the documentation and/or other materials provided with the | ||
34 | * distribution. | ||
35 | * | ||
36 | * 3. All advertising materials mentioning features or use of this | ||
37 | * software must display the following acknowledgment: | ||
38 | * "This product includes software developed by the OpenSSL Project | ||
39 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
40 | * | ||
41 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
42 | * endorse or promote products derived from this software without | ||
43 | * prior written permission. For written permission, please contact | ||
44 | * licensing@OpenSSL.org. | ||
45 | * | ||
46 | * 5. Products derived from this software may not be called "OpenSSL" | ||
47 | * nor may "OpenSSL" appear in their names without prior written | ||
48 | * permission of the OpenSSL Project. | ||
49 | * | ||
50 | * 6. Redistributions of any form whatsoever must retain the following | ||
51 | * acknowledgment: | ||
52 | * "This product includes software developed by the OpenSSL Project | ||
53 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
54 | * | ||
55 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
56 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
57 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
58 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
59 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
60 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
61 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
62 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
63 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
64 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
65 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
66 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
67 | * ==================================================================== | ||
68 | * | ||
69 | * This product includes cryptographic software written by Eric Young | ||
70 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
71 | * Hudson (tjh@cryptsoft.com). | ||
72 | * | ||
73 | */ | ||
74 | |||
75 | |||
76 | #include <openssl/opensslconf.h> | ||
77 | |||
78 | #if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_AES_NI) && !defined(OPENSSL_NO_AES) | ||
79 | |||
80 | #include <stdio.h> | ||
81 | #include <assert.h> | ||
82 | #include "cryptlib.h" | ||
83 | #include <openssl/dso.h> | ||
84 | #include <openssl/engine.h> | ||
85 | #include <openssl/evp.h> | ||
86 | #include <openssl/aes.h> | ||
87 | #include <openssl/err.h> | ||
88 | |||
89 | /* AES-NI is available *ONLY* on some x86 CPUs. Not only that it | ||
90 | doesn't exist elsewhere, but it even can't be compiled on other | ||
91 | platforms! */ | ||
92 | #undef COMPILE_HW_AESNI | ||
93 | #if (defined(__x86_64) || defined(__x86_64__) || \ | ||
94 | defined(_M_AMD64) || defined(_M_X64) || \ | ||
95 | defined(OPENSSL_IA32_SSE2)) && !defined(OPENSSL_NO_ASM) && !defined(__i386__) | ||
96 | #define COMPILE_HW_AESNI | ||
97 | #endif | ||
98 | static ENGINE *ENGINE_aesni (void); | ||
99 | |||
100 | void ENGINE_load_aesni (void) | ||
101 | { | ||
102 | /* On non-x86 CPUs it just returns. */ | ||
103 | #ifdef COMPILE_HW_AESNI | ||
104 | ENGINE *toadd = ENGINE_aesni(); | ||
105 | if (!toadd) return; | ||
106 | ENGINE_add (toadd); | ||
107 | ENGINE_register_complete (toadd); | ||
108 | ENGINE_free (toadd); | ||
109 | ERR_clear_error (); | ||
110 | #endif | ||
111 | } | ||
112 | |||
113 | #ifdef COMPILE_HW_AESNI | ||
114 | int aesni_set_encrypt_key(const unsigned char *userKey, int bits, | ||
115 | AES_KEY *key); | ||
116 | int aesni_set_decrypt_key(const unsigned char *userKey, int bits, | ||
117 | AES_KEY *key); | ||
118 | |||
119 | void aesni_encrypt(const unsigned char *in, unsigned char *out, | ||
120 | const AES_KEY *key); | ||
121 | void aesni_decrypt(const unsigned char *in, unsigned char *out, | ||
122 | const AES_KEY *key); | ||
123 | |||
124 | void aesni_ecb_encrypt(const unsigned char *in, | ||
125 | unsigned char *out, | ||
126 | size_t length, | ||
127 | const AES_KEY *key, | ||
128 | int enc); | ||
129 | void aesni_cbc_encrypt(const unsigned char *in, | ||
130 | unsigned char *out, | ||
131 | size_t length, | ||
132 | const AES_KEY *key, | ||
133 | unsigned char *ivec, int enc); | ||
134 | |||
135 | /* Function for ENGINE detection and control */ | ||
136 | static int aesni_init(ENGINE *e); | ||
137 | |||
138 | /* Cipher Stuff */ | ||
139 | static int aesni_ciphers(ENGINE *e, const EVP_CIPHER **cipher, | ||
140 | const int **nids, int nid); | ||
141 | |||
142 | #define AESNI_MIN_ALIGN 16 | ||
143 | #define AESNI_ALIGN(x) \ | ||
144 | ((void *)(((unsigned long)(x)+AESNI_MIN_ALIGN-1)&~(AESNI_MIN_ALIGN-1))) | ||
145 | |||
146 | /* Engine names */ | ||
147 | static const char aesni_id[] = "aesni", | ||
148 | aesni_name[] = "Intel AES-NI engine", | ||
149 | no_aesni_name[] = "Intel AES-NI engine (no-aesni)"; | ||
150 | |||
151 | |||
152 | /* The input and output encrypted as though 128bit cfb mode is being | ||
153 | * used. The extra state information to record how much of the | ||
154 | * 128bit block we have used is contained in *num; | ||
155 | */ | ||
156 | static void aesni_cfb128_encrypt(const unsigned char *in, unsigned char *out, | ||
157 | unsigned int len, const void *key, | ||
158 | unsigned char ivec[16], int *num, | ||
159 | int enc) | ||
160 | { | ||
161 | unsigned int n; | ||
162 | size_t l = 0; | ||
163 | |||
164 | assert(in && out && key && ivec && num); | ||
165 | |||
166 | n = *num; | ||
167 | |||
168 | if (enc) { | ||
169 | #if !defined(OPENSSL_SMALL_FOOTPRINT) | ||
170 | if (16%sizeof(size_t) == 0) do { /* always true actually */ | ||
171 | while (n && len) { | ||
172 | *(out++) = ivec[n] ^= *(in++); | ||
173 | --len; | ||
174 | n = (n+1) % 16; | ||
175 | } | ||
176 | while (len>=16) { | ||
177 | aesni_encrypt(ivec, ivec, key); | ||
178 | for (n=0; n<16; n+=sizeof(size_t)) { | ||
179 | *(size_t*)(out+n) = | ||
180 | *(size_t*)(ivec+n) ^= *(size_t*)(in+n); | ||
181 | } | ||
182 | len -= 16; | ||
183 | out += 16; | ||
184 | in += 16; | ||
185 | } | ||
186 | n = 0; | ||
187 | if (len) { | ||
188 | aesni_encrypt(ivec, ivec, key); | ||
189 | while (len--) { | ||
190 | out[n] = ivec[n] ^= in[n]; | ||
191 | ++n; | ||
192 | } | ||
193 | } | ||
194 | *num = n; | ||
195 | return; | ||
196 | } while (0); | ||
197 | /* the rest would be commonly eliminated by x86* compiler */ | ||
198 | #endif | ||
199 | while (l<len) { | ||
200 | if (n == 0) { | ||
201 | aesni_encrypt(ivec, ivec, key); | ||
202 | } | ||
203 | out[l] = ivec[n] ^= in[l]; | ||
204 | ++l; | ||
205 | n = (n+1) % 16; | ||
206 | } | ||
207 | *num = n; | ||
208 | } else { | ||
209 | #if !defined(OPENSSL_SMALL_FOOTPRINT) | ||
210 | if (16%sizeof(size_t) == 0) do { /* always true actually */ | ||
211 | while (n && len) { | ||
212 | unsigned char c; | ||
213 | *(out++) = ivec[n] ^ (c = *(in++)); ivec[n] = c; | ||
214 | --len; | ||
215 | n = (n+1) % 16; | ||
216 | } | ||
217 | while (len>=16) { | ||
218 | aesni_encrypt(ivec, ivec, key); | ||
219 | for (n=0; n<16; n+=sizeof(size_t)) { | ||
220 | size_t t = *(size_t*)(in+n); | ||
221 | *(size_t*)(out+n) = *(size_t*)(ivec+n) ^ t; | ||
222 | *(size_t*)(ivec+n) = t; | ||
223 | } | ||
224 | len -= 16; | ||
225 | out += 16; | ||
226 | in += 16; | ||
227 | } | ||
228 | n = 0; | ||
229 | if (len) { | ||
230 | aesni_encrypt(ivec, ivec, key); | ||
231 | while (len--) { | ||
232 | unsigned char c; | ||
233 | out[n] = ivec[n] ^ (c = in[n]); ivec[n] = c; | ||
234 | ++n; | ||
235 | } | ||
236 | } | ||
237 | *num = n; | ||
238 | return; | ||
239 | } while (0); | ||
240 | /* the rest would be commonly eliminated by x86* compiler */ | ||
241 | #endif | ||
242 | while (l<len) { | ||
243 | unsigned char c; | ||
244 | if (n == 0) { | ||
245 | aesni_encrypt(ivec, ivec, key); | ||
246 | } | ||
247 | out[l] = ivec[n] ^ (c = in[l]); ivec[n] = c; | ||
248 | ++l; | ||
249 | n = (n+1) % 16; | ||
250 | } | ||
251 | *num=n; | ||
252 | } | ||
253 | } | ||
254 | |||
255 | /* The input and output encrypted as though 128bit ofb mode is being | ||
256 | * used. The extra state information to record how much of the | ||
257 | * 128bit block we have used is contained in *num; | ||
258 | */ | ||
259 | static void aesni_ofb128_encrypt(const unsigned char *in, unsigned char *out, | ||
260 | unsigned int len, const void *key, | ||
261 | unsigned char ivec[16], int *num) | ||
262 | { | ||
263 | unsigned int n; | ||
264 | size_t l=0; | ||
265 | |||
266 | assert(in && out && key && ivec && num); | ||
267 | |||
268 | n = *num; | ||
269 | |||
270 | #if !defined(OPENSSL_SMALL_FOOTPRINT) | ||
271 | if (16%sizeof(size_t) == 0) do { /* always true actually */ | ||
272 | while (n && len) { | ||
273 | *(out++) = *(in++) ^ ivec[n]; | ||
274 | --len; | ||
275 | n = (n+1) % 16; | ||
276 | } | ||
277 | while (len>=16) { | ||
278 | aesni_encrypt(ivec, ivec, key); | ||
279 | for (n=0; n<16; n+=sizeof(size_t)) | ||
280 | *(size_t*)(out+n) = | ||
281 | *(size_t*)(in+n) ^ *(size_t*)(ivec+n); | ||
282 | len -= 16; | ||
283 | out += 16; | ||
284 | in += 16; | ||
285 | } | ||
286 | n = 0; | ||
287 | if (len) { | ||
288 | aesni_encrypt(ivec, ivec, key); | ||
289 | while (len--) { | ||
290 | out[n] = in[n] ^ ivec[n]; | ||
291 | ++n; | ||
292 | } | ||
293 | } | ||
294 | *num = n; | ||
295 | return; | ||
296 | } while(0); | ||
297 | /* the rest would be commonly eliminated by x86* compiler */ | ||
298 | #endif | ||
299 | while (l<len) { | ||
300 | if (n==0) { | ||
301 | aesni_encrypt(ivec, ivec, key); | ||
302 | } | ||
303 | out[l] = in[l] ^ ivec[n]; | ||
304 | ++l; | ||
305 | n = (n+1) % 16; | ||
306 | } | ||
307 | |||
308 | *num=n; | ||
309 | } | ||
310 | /* ===== Engine "management" functions ===== */ | ||
311 | |||
312 | #if defined(_WIN32) | ||
313 | typedef unsigned __int64 IA32CAP; | ||
314 | #else | ||
315 | typedef unsigned long long IA32CAP; | ||
316 | #endif | ||
317 | |||
318 | /* Prepare the ENGINE structure for registration */ | ||
319 | static int | ||
320 | aesni_bind_helper(ENGINE *e) | ||
321 | { | ||
322 | int engage; | ||
323 | if (sizeof(OPENSSL_ia32cap_P) > 4) { | ||
324 | engage = ((IA32CAP)OPENSSL_ia32cap_P >> 57) & 1; | ||
325 | } else { | ||
326 | IA32CAP OPENSSL_ia32_cpuid(void); | ||
327 | engage = (OPENSSL_ia32_cpuid() >> 57) & 1; | ||
328 | } | ||
329 | |||
330 | /* Register everything or return with an error */ | ||
331 | if (!ENGINE_set_id(e, aesni_id) || | ||
332 | !ENGINE_set_name(e, engage ? aesni_name : no_aesni_name) || | ||
333 | |||
334 | !ENGINE_set_init_function(e, aesni_init) || | ||
335 | (engage && !ENGINE_set_ciphers (e, aesni_ciphers)) | ||
336 | ) | ||
337 | return 0; | ||
338 | |||
339 | /* Everything looks good */ | ||
340 | return 1; | ||
341 | } | ||
342 | |||
343 | /* Constructor */ | ||
344 | static ENGINE * | ||
345 | ENGINE_aesni(void) | ||
346 | { | ||
347 | ENGINE *eng = ENGINE_new(); | ||
348 | |||
349 | if (!eng) { | ||
350 | return NULL; | ||
351 | } | ||
352 | |||
353 | if (!aesni_bind_helper(eng)) { | ||
354 | ENGINE_free(eng); | ||
355 | return NULL; | ||
356 | } | ||
357 | |||
358 | return eng; | ||
359 | } | ||
360 | |||
361 | /* Check availability of the engine */ | ||
362 | static int | ||
363 | aesni_init(ENGINE *e) | ||
364 | { | ||
365 | return 1; | ||
366 | } | ||
367 | |||
368 | #if defined(NID_aes_128_cfb128) && ! defined (NID_aes_128_cfb) | ||
369 | #define NID_aes_128_cfb NID_aes_128_cfb128 | ||
370 | #endif | ||
371 | |||
372 | #if defined(NID_aes_128_ofb128) && ! defined (NID_aes_128_ofb) | ||
373 | #define NID_aes_128_ofb NID_aes_128_ofb128 | ||
374 | #endif | ||
375 | |||
376 | #if defined(NID_aes_192_cfb128) && ! defined (NID_aes_192_cfb) | ||
377 | #define NID_aes_192_cfb NID_aes_192_cfb128 | ||
378 | #endif | ||
379 | |||
380 | #if defined(NID_aes_192_ofb128) && ! defined (NID_aes_192_ofb) | ||
381 | #define NID_aes_192_ofb NID_aes_192_ofb128 | ||
382 | #endif | ||
383 | |||
384 | #if defined(NID_aes_256_cfb128) && ! defined (NID_aes_256_cfb) | ||
385 | #define NID_aes_256_cfb NID_aes_256_cfb128 | ||
386 | #endif | ||
387 | |||
388 | #if defined(NID_aes_256_ofb128) && ! defined (NID_aes_256_ofb) | ||
389 | #define NID_aes_256_ofb NID_aes_256_ofb128 | ||
390 | #endif | ||
391 | |||
392 | /* List of supported ciphers. */ | ||
393 | static int aesni_cipher_nids[] = { | ||
394 | NID_aes_128_ecb, | ||
395 | NID_aes_128_cbc, | ||
396 | NID_aes_128_cfb, | ||
397 | NID_aes_128_ofb, | ||
398 | |||
399 | NID_aes_192_ecb, | ||
400 | NID_aes_192_cbc, | ||
401 | NID_aes_192_cfb, | ||
402 | NID_aes_192_ofb, | ||
403 | |||
404 | NID_aes_256_ecb, | ||
405 | NID_aes_256_cbc, | ||
406 | NID_aes_256_cfb, | ||
407 | NID_aes_256_ofb, | ||
408 | }; | ||
409 | static int aesni_cipher_nids_num = | ||
410 | (sizeof(aesni_cipher_nids)/sizeof(aesni_cipher_nids[0])); | ||
411 | |||
412 | typedef struct | ||
413 | { | ||
414 | AES_KEY ks; | ||
415 | unsigned int _pad1[3]; | ||
416 | } AESNI_KEY; | ||
417 | |||
418 | static int | ||
419 | aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *user_key, | ||
420 | const unsigned char *iv, int enc) | ||
421 | { | ||
422 | int ret; | ||
423 | AES_KEY *key = AESNI_ALIGN(ctx->cipher_data); | ||
424 | |||
425 | if ((ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_CFB_MODE | ||
426 | || (ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_OFB_MODE | ||
427 | || enc) | ||
428 | ret=aesni_set_encrypt_key(user_key, ctx->key_len * 8, key); | ||
429 | else | ||
430 | ret=aesni_set_decrypt_key(user_key, ctx->key_len * 8, key); | ||
431 | |||
432 | if(ret < 0) { | ||
433 | EVPerr(EVP_F_AESNI_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED); | ||
434 | return 0; | ||
435 | } | ||
436 | |||
437 | return 1; | ||
438 | } | ||
439 | |||
440 | static int aesni_cipher_ecb(EVP_CIPHER_CTX *ctx, unsigned char *out, | ||
441 | const unsigned char *in, size_t inl) | ||
442 | { AES_KEY *key = AESNI_ALIGN(ctx->cipher_data); | ||
443 | aesni_ecb_encrypt(in, out, inl, key, ctx->encrypt); | ||
444 | return 1; | ||
445 | } | ||
446 | static int aesni_cipher_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out, | ||
447 | const unsigned char *in, size_t inl) | ||
448 | { AES_KEY *key = AESNI_ALIGN(ctx->cipher_data); | ||
449 | aesni_cbc_encrypt(in, out, inl, key, | ||
450 | ctx->iv, ctx->encrypt); | ||
451 | return 1; | ||
452 | } | ||
453 | static int aesni_cipher_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out, | ||
454 | const unsigned char *in, size_t inl) | ||
455 | { AES_KEY *key = AESNI_ALIGN(ctx->cipher_data); | ||
456 | |||
457 | aesni_cfb128_encrypt(in, out, inl, key, ctx->iv, | ||
458 | &ctx->num, ctx->encrypt); | ||
459 | return 1; | ||
460 | } | ||
461 | static int aesni_cipher_ofb(EVP_CIPHER_CTX *ctx, unsigned char *out, | ||
462 | const unsigned char *in, size_t inl) | ||
463 | { AES_KEY *key = AESNI_ALIGN(ctx->cipher_data); | ||
464 | aesni_ofb128_encrypt(in, out, inl, key, ctx->iv, &ctx->num); | ||
465 | return 1; | ||
466 | } | ||
467 | |||
468 | #define AES_BLOCK_SIZE 16 | ||
469 | |||
470 | #define EVP_CIPHER_block_size_ECB AES_BLOCK_SIZE | ||
471 | #define EVP_CIPHER_block_size_CBC AES_BLOCK_SIZE | ||
472 | #define EVP_CIPHER_block_size_OFB 1 | ||
473 | #define EVP_CIPHER_block_size_CFB 1 | ||
474 | |||
475 | /* Declaring so many ciphers by hand would be a pain. | ||
476 | Instead introduce a bit of preprocessor magic :-) */ | ||
477 | #define DECLARE_AES_EVP(ksize,lmode,umode) \ | ||
478 | static const EVP_CIPHER aesni_##ksize##_##lmode = { \ | ||
479 | NID_aes_##ksize##_##lmode, \ | ||
480 | EVP_CIPHER_block_size_##umode, \ | ||
481 | ksize / 8, \ | ||
482 | AES_BLOCK_SIZE, \ | ||
483 | 0 | EVP_CIPH_##umode##_MODE, \ | ||
484 | aesni_init_key, \ | ||
485 | aesni_cipher_##lmode, \ | ||
486 | NULL, \ | ||
487 | sizeof(AESNI_KEY), \ | ||
488 | EVP_CIPHER_set_asn1_iv, \ | ||
489 | EVP_CIPHER_get_asn1_iv, \ | ||
490 | NULL, \ | ||
491 | NULL \ | ||
492 | } | ||
493 | |||
494 | DECLARE_AES_EVP(128,ecb,ECB); | ||
495 | DECLARE_AES_EVP(128,cbc,CBC); | ||
496 | DECLARE_AES_EVP(128,cfb,CFB); | ||
497 | DECLARE_AES_EVP(128,ofb,OFB); | ||
498 | |||
499 | DECLARE_AES_EVP(192,ecb,ECB); | ||
500 | DECLARE_AES_EVP(192,cbc,CBC); | ||
501 | DECLARE_AES_EVP(192,cfb,CFB); | ||
502 | DECLARE_AES_EVP(192,ofb,OFB); | ||
503 | |||
504 | DECLARE_AES_EVP(256,ecb,ECB); | ||
505 | DECLARE_AES_EVP(256,cbc,CBC); | ||
506 | DECLARE_AES_EVP(256,cfb,CFB); | ||
507 | DECLARE_AES_EVP(256,ofb,OFB); | ||
508 | |||
509 | static int | ||
510 | aesni_ciphers (ENGINE *e, const EVP_CIPHER **cipher, | ||
511 | const int **nids, int nid) | ||
512 | { | ||
513 | /* No specific cipher => return a list of supported nids ... */ | ||
514 | if (!cipher) { | ||
515 | *nids = aesni_cipher_nids; | ||
516 | return aesni_cipher_nids_num; | ||
517 | } | ||
518 | |||
519 | /* ... or the requested "cipher" otherwise */ | ||
520 | switch (nid) { | ||
521 | case NID_aes_128_ecb: | ||
522 | *cipher = &aesni_128_ecb; | ||
523 | break; | ||
524 | case NID_aes_128_cbc: | ||
525 | *cipher = &aesni_128_cbc; | ||
526 | break; | ||
527 | case NID_aes_128_cfb: | ||
528 | *cipher = &aesni_128_cfb; | ||
529 | break; | ||
530 | case NID_aes_128_ofb: | ||
531 | *cipher = &aesni_128_ofb; | ||
532 | break; | ||
533 | |||
534 | case NID_aes_192_ecb: | ||
535 | *cipher = &aesni_192_ecb; | ||
536 | break; | ||
537 | case NID_aes_192_cbc: | ||
538 | *cipher = &aesni_192_cbc; | ||
539 | break; | ||
540 | case NID_aes_192_cfb: | ||
541 | *cipher = &aesni_192_cfb; | ||
542 | break; | ||
543 | case NID_aes_192_ofb: | ||
544 | *cipher = &aesni_192_ofb; | ||
545 | break; | ||
546 | |||
547 | case NID_aes_256_ecb: | ||
548 | *cipher = &aesni_256_ecb; | ||
549 | break; | ||
550 | case NID_aes_256_cbc: | ||
551 | *cipher = &aesni_256_cbc; | ||
552 | break; | ||
553 | case NID_aes_256_cfb: | ||
554 | *cipher = &aesni_256_cfb; | ||
555 | break; | ||
556 | case NID_aes_256_ofb: | ||
557 | *cipher = &aesni_256_ofb; | ||
558 | break; | ||
559 | |||
560 | default: | ||
561 | /* Sorry, we don't support this NID */ | ||
562 | *cipher = NULL; | ||
563 | return 0; | ||
564 | } | ||
565 | return 1; | ||
566 | } | ||
567 | |||
568 | #endif /* COMPILE_HW_AESNI */ | ||
569 | #endif /* !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_AESNI) && !defined(OPENSSL_NO_AES) */ | ||
570 | |||
diff --git a/src/lib/libcrypto/engine/eng_all.c b/src/lib/libcrypto/engine/eng_all.c index 22c120454f..79d1f2beff 100644 --- a/src/lib/libcrypto/engine/eng_all.c +++ b/src/lib/libcrypto/engine/eng_all.c | |||
@@ -71,6 +71,11 @@ void ENGINE_load_builtin_engines(void) | |||
71 | #if !defined(OPENSSL_NO_HW) && (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)) | 71 | #if !defined(OPENSSL_NO_HW) && (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(HAVE_CRYPTODEV)) |
72 | ENGINE_load_cryptodev(); | 72 | ENGINE_load_cryptodev(); |
73 | #endif | 73 | #endif |
74 | |||
75 | #if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_AESNI) | ||
76 | ENGINE_load_aesni(); | ||
77 | #endif | ||
78 | |||
74 | ENGINE_load_dynamic(); | 79 | ENGINE_load_dynamic(); |
75 | #ifndef OPENSSL_NO_STATIC_ENGINE | 80 | #ifndef OPENSSL_NO_STATIC_ENGINE |
76 | #ifndef OPENSSL_NO_HW | 81 | #ifndef OPENSSL_NO_HW |
diff --git a/src/lib/libcrypto/engine/eng_cryptodev.c b/src/lib/libcrypto/engine/eng_cryptodev.c new file mode 100644 index 0000000000..10b3856b4e --- /dev/null +++ b/src/lib/libcrypto/engine/eng_cryptodev.c | |||
@@ -0,0 +1,1418 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002 Bob Beck <beck@openbsd.org> | ||
3 | * Copyright (c) 2002 Theo de Raadt | ||
4 | * Copyright (c) 2002 Markus Friedl | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | * | ||
27 | */ | ||
28 | |||
29 | #include <openssl/objects.h> | ||
30 | #include <openssl/engine.h> | ||
31 | #include <openssl/evp.h> | ||
32 | #include <openssl/bn.h> | ||
33 | |||
34 | #if (defined(__unix__) || defined(unix)) && !defined(USG) && \ | ||
35 | (defined(__OpenBSD__) || defined(__FreeBSD__)) | ||
36 | #include <sys/param.h> | ||
37 | # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041) | ||
38 | # define HAVE_CRYPTODEV | ||
39 | # endif | ||
40 | # if (OpenBSD >= 200110) | ||
41 | # define HAVE_SYSLOG_R | ||
42 | # endif | ||
43 | #endif | ||
44 | |||
45 | #ifndef HAVE_CRYPTODEV | ||
46 | |||
47 | void | ||
48 | ENGINE_load_cryptodev(void) | ||
49 | { | ||
50 | /* This is a NOP on platforms without /dev/crypto */ | ||
51 | return; | ||
52 | } | ||
53 | |||
54 | #else | ||
55 | |||
56 | #include <sys/types.h> | ||
57 | #include <crypto/cryptodev.h> | ||
58 | #include <crypto/dh/dh.h> | ||
59 | #include <crypto/dsa/dsa.h> | ||
60 | #include <crypto/err/err.h> | ||
61 | #include <crypto/rsa/rsa.h> | ||
62 | #include <sys/ioctl.h> | ||
63 | #include <errno.h> | ||
64 | #include <stdio.h> | ||
65 | #include <unistd.h> | ||
66 | #include <fcntl.h> | ||
67 | #include <stdarg.h> | ||
68 | #include <syslog.h> | ||
69 | #include <errno.h> | ||
70 | #include <string.h> | ||
71 | |||
72 | struct dev_crypto_state { | ||
73 | struct session_op d_sess; | ||
74 | int d_fd; | ||
75 | |||
76 | #ifdef USE_CRYPTODEV_DIGESTS | ||
77 | char dummy_mac_key[HASH_MAX_LEN]; | ||
78 | |||
79 | unsigned char digest_res[HASH_MAX_LEN]; | ||
80 | char *mac_data; | ||
81 | int mac_len; | ||
82 | |||
83 | int copy; | ||
84 | #endif | ||
85 | }; | ||
86 | |||
87 | static u_int32_t cryptodev_asymfeat = 0; | ||
88 | |||
89 | static int get_asym_dev_crypto(void); | ||
90 | static int open_dev_crypto(void); | ||
91 | static int get_dev_crypto(void); | ||
92 | static int get_cryptodev_ciphers(const int **cnids); | ||
93 | #ifdef USE_CRYPTODEV_DIGESTS | ||
94 | static int get_cryptodev_digests(const int **cnids); | ||
95 | #endif | ||
96 | static int cryptodev_usable_ciphers(const int **nids); | ||
97 | static int cryptodev_usable_digests(const int **nids); | ||
98 | static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | ||
99 | const unsigned char *in, size_t inl); | ||
100 | static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | ||
101 | const unsigned char *iv, int enc); | ||
102 | static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx); | ||
103 | static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher, | ||
104 | const int **nids, int nid); | ||
105 | static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest, | ||
106 | const int **nids, int nid); | ||
107 | static int bn2crparam(const BIGNUM *a, struct crparam *crp); | ||
108 | static int crparam2bn(struct crparam *crp, BIGNUM *a); | ||
109 | static void zapparams(struct crypt_kop *kop); | ||
110 | static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, | ||
111 | int slen, BIGNUM *s); | ||
112 | |||
113 | static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, | ||
114 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); | ||
115 | static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, | ||
116 | RSA *rsa, BN_CTX *ctx); | ||
117 | static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx); | ||
118 | static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, | ||
119 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); | ||
120 | static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g, | ||
121 | BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p, | ||
122 | BN_CTX *ctx, BN_MONT_CTX *mont); | ||
123 | static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, | ||
124 | int dlen, DSA *dsa); | ||
125 | static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len, | ||
126 | DSA_SIG *sig, DSA *dsa); | ||
127 | static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a, | ||
128 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, | ||
129 | BN_MONT_CTX *m_ctx); | ||
130 | static int cryptodev_dh_compute_key(unsigned char *key, | ||
131 | const BIGNUM *pub_key, DH *dh); | ||
132 | static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, | ||
133 | void (*f)(void)); | ||
134 | void ENGINE_load_cryptodev(void); | ||
135 | |||
136 | static const ENGINE_CMD_DEFN cryptodev_defns[] = { | ||
137 | { 0, NULL, NULL, 0 } | ||
138 | }; | ||
139 | |||
140 | static struct { | ||
141 | int id; | ||
142 | int nid; | ||
143 | int ivmax; | ||
144 | int keylen; | ||
145 | } ciphers[] = { | ||
146 | { CRYPTO_ARC4, NID_rc4, 0, 16, }, | ||
147 | { CRYPTO_DES_CBC, NID_des_cbc, 8, 8, }, | ||
148 | { CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24, }, | ||
149 | { CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16, }, | ||
150 | { CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24, }, | ||
151 | { CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32, }, | ||
152 | { CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, }, | ||
153 | { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, }, | ||
154 | { 0, NID_undef, 0, 0, }, | ||
155 | }; | ||
156 | |||
157 | #ifdef USE_CRYPTODEV_DIGESTS | ||
158 | static struct { | ||
159 | int id; | ||
160 | int nid; | ||
161 | int keylen; | ||
162 | } digests[] = { | ||
163 | { CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16}, | ||
164 | { CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20}, | ||
165 | { CRYPTO_RIPEMD160_HMAC, NID_ripemd160, 16/*?*/}, | ||
166 | { CRYPTO_MD5_KPDK, NID_undef, 0}, | ||
167 | { CRYPTO_SHA1_KPDK, NID_undef, 0}, | ||
168 | { CRYPTO_MD5, NID_md5, 16}, | ||
169 | { CRYPTO_SHA1, NID_sha1, 20}, | ||
170 | { 0, NID_undef, 0}, | ||
171 | }; | ||
172 | #endif | ||
173 | |||
174 | /* | ||
175 | * Return a fd if /dev/crypto seems usable, 0 otherwise. | ||
176 | */ | ||
177 | static int | ||
178 | open_dev_crypto(void) | ||
179 | { | ||
180 | static int fd = -1; | ||
181 | |||
182 | if (fd == -1) { | ||
183 | if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1) | ||
184 | return (-1); | ||
185 | /* close on exec */ | ||
186 | if (fcntl(fd, F_SETFD, 1) == -1) { | ||
187 | close(fd); | ||
188 | fd = -1; | ||
189 | return (-1); | ||
190 | } | ||
191 | } | ||
192 | return (fd); | ||
193 | } | ||
194 | |||
195 | static int | ||
196 | get_dev_crypto(void) | ||
197 | { | ||
198 | int fd, retfd; | ||
199 | |||
200 | if ((fd = open_dev_crypto()) == -1) | ||
201 | return (-1); | ||
202 | if (ioctl(fd, CRIOGET, &retfd) == -1) | ||
203 | return (-1); | ||
204 | |||
205 | /* close on exec */ | ||
206 | if (fcntl(retfd, F_SETFD, 1) == -1) { | ||
207 | close(retfd); | ||
208 | return (-1); | ||
209 | } | ||
210 | return (retfd); | ||
211 | } | ||
212 | |||
213 | /* Caching version for asym operations */ | ||
214 | static int | ||
215 | get_asym_dev_crypto(void) | ||
216 | { | ||
217 | static int fd = -1; | ||
218 | |||
219 | if (fd == -1) | ||
220 | fd = get_dev_crypto(); | ||
221 | return fd; | ||
222 | } | ||
223 | |||
224 | /* | ||
225 | * Find out what ciphers /dev/crypto will let us have a session for. | ||
226 | * XXX note, that some of these openssl doesn't deal with yet! | ||
227 | * returning them here is harmless, as long as we return NULL | ||
228 | * when asked for a handler in the cryptodev_engine_ciphers routine | ||
229 | */ | ||
230 | static int | ||
231 | get_cryptodev_ciphers(const int **cnids) | ||
232 | { | ||
233 | static int nids[CRYPTO_ALGORITHM_MAX]; | ||
234 | struct session_op sess; | ||
235 | int fd, i, count = 0; | ||
236 | |||
237 | if ((fd = get_dev_crypto()) < 0) { | ||
238 | *cnids = NULL; | ||
239 | return (0); | ||
240 | } | ||
241 | memset(&sess, 0, sizeof(sess)); | ||
242 | sess.key = (caddr_t)"123456789abcdefghijklmno"; | ||
243 | |||
244 | for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) { | ||
245 | if (ciphers[i].nid == NID_undef) | ||
246 | continue; | ||
247 | sess.cipher = ciphers[i].id; | ||
248 | sess.keylen = ciphers[i].keylen; | ||
249 | sess.mac = 0; | ||
250 | if (ioctl(fd, CIOCGSESSION, &sess) != -1 && | ||
251 | ioctl(fd, CIOCFSESSION, &sess.ses) != -1) | ||
252 | nids[count++] = ciphers[i].nid; | ||
253 | } | ||
254 | close(fd); | ||
255 | |||
256 | if (count > 0) | ||
257 | *cnids = nids; | ||
258 | else | ||
259 | *cnids = NULL; | ||
260 | return (count); | ||
261 | } | ||
262 | |||
263 | #ifdef USE_CRYPTODEV_DIGESTS | ||
264 | /* | ||
265 | * Find out what digests /dev/crypto will let us have a session for. | ||
266 | * XXX note, that some of these openssl doesn't deal with yet! | ||
267 | * returning them here is harmless, as long as we return NULL | ||
268 | * when asked for a handler in the cryptodev_engine_digests routine | ||
269 | */ | ||
270 | static int | ||
271 | get_cryptodev_digests(const int **cnids) | ||
272 | { | ||
273 | static int nids[CRYPTO_ALGORITHM_MAX]; | ||
274 | struct session_op sess; | ||
275 | int fd, i, count = 0; | ||
276 | |||
277 | if ((fd = get_dev_crypto()) < 0) { | ||
278 | *cnids = NULL; | ||
279 | return (0); | ||
280 | } | ||
281 | memset(&sess, 0, sizeof(sess)); | ||
282 | sess.mackey = (caddr_t)"123456789abcdefghijklmno"; | ||
283 | for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) { | ||
284 | if (digests[i].nid == NID_undef) | ||
285 | continue; | ||
286 | sess.mac = digests[i].id; | ||
287 | sess.mackeylen = digests[i].keylen; | ||
288 | sess.cipher = 0; | ||
289 | if (ioctl(fd, CIOCGSESSION, &sess) != -1 && | ||
290 | ioctl(fd, CIOCFSESSION, &sess.ses) != -1) | ||
291 | nids[count++] = digests[i].nid; | ||
292 | } | ||
293 | close(fd); | ||
294 | |||
295 | if (count > 0) | ||
296 | *cnids = nids; | ||
297 | else | ||
298 | *cnids = NULL; | ||
299 | return (count); | ||
300 | } | ||
301 | #endif /* 0 */ | ||
302 | |||
303 | /* | ||
304 | * Find the useable ciphers|digests from dev/crypto - this is the first | ||
305 | * thing called by the engine init crud which determines what it | ||
306 | * can use for ciphers from this engine. We want to return | ||
307 | * only what we can do, anythine else is handled by software. | ||
308 | * | ||
309 | * If we can't initialize the device to do anything useful for | ||
310 | * any reason, we want to return a NULL array, and 0 length, | ||
311 | * which forces everything to be done is software. By putting | ||
312 | * the initalization of the device in here, we ensure we can | ||
313 | * use this engine as the default, and if for whatever reason | ||
314 | * /dev/crypto won't do what we want it will just be done in | ||
315 | * software | ||
316 | * | ||
317 | * This can (should) be greatly expanded to perhaps take into | ||
318 | * account speed of the device, and what we want to do. | ||
319 | * (although the disabling of particular alg's could be controlled | ||
320 | * by the device driver with sysctl's.) - this is where we | ||
321 | * want most of the decisions made about what we actually want | ||
322 | * to use from /dev/crypto. | ||
323 | */ | ||
324 | static int | ||
325 | cryptodev_usable_ciphers(const int **nids) | ||
326 | { | ||
327 | return (get_cryptodev_ciphers(nids)); | ||
328 | } | ||
329 | |||
330 | static int | ||
331 | cryptodev_usable_digests(const int **nids) | ||
332 | { | ||
333 | #ifdef USE_CRYPTODEV_DIGESTS | ||
334 | return (get_cryptodev_digests(nids)); | ||
335 | #else | ||
336 | /* | ||
337 | * XXXX just disable all digests for now, because it sucks. | ||
338 | * we need a better way to decide this - i.e. I may not | ||
339 | * want digests on slow cards like hifn on fast machines, | ||
340 | * but might want them on slow or loaded machines, etc. | ||
341 | * will also want them when using crypto cards that don't | ||
342 | * suck moose gonads - would be nice to be able to decide something | ||
343 | * as reasonable default without having hackery that's card dependent. | ||
344 | * of course, the default should probably be just do everything, | ||
345 | * with perhaps a sysctl to turn algoritms off (or have them off | ||
346 | * by default) on cards that generally suck like the hifn. | ||
347 | */ | ||
348 | *nids = NULL; | ||
349 | return (0); | ||
350 | #endif | ||
351 | } | ||
352 | |||
353 | static int | ||
354 | cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | ||
355 | const unsigned char *in, size_t inl) | ||
356 | { | ||
357 | struct crypt_op cryp; | ||
358 | struct dev_crypto_state *state = ctx->cipher_data; | ||
359 | struct session_op *sess = &state->d_sess; | ||
360 | const void *iiv; | ||
361 | unsigned char save_iv[EVP_MAX_IV_LENGTH]; | ||
362 | |||
363 | if (state->d_fd < 0) | ||
364 | return (0); | ||
365 | if (!inl) | ||
366 | return (1); | ||
367 | if ((inl % ctx->cipher->block_size) != 0) | ||
368 | return (0); | ||
369 | |||
370 | memset(&cryp, 0, sizeof(cryp)); | ||
371 | |||
372 | cryp.ses = sess->ses; | ||
373 | cryp.flags = 0; | ||
374 | cryp.len = inl; | ||
375 | cryp.src = (caddr_t) in; | ||
376 | cryp.dst = (caddr_t) out; | ||
377 | cryp.mac = 0; | ||
378 | |||
379 | cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT; | ||
380 | |||
381 | if (ctx->cipher->iv_len) { | ||
382 | cryp.iv = (caddr_t) ctx->iv; | ||
383 | if (!ctx->encrypt) { | ||
384 | iiv = in + inl - ctx->cipher->iv_len; | ||
385 | memcpy(save_iv, iiv, ctx->cipher->iv_len); | ||
386 | } | ||
387 | } else | ||
388 | cryp.iv = NULL; | ||
389 | |||
390 | if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) { | ||
391 | /* XXX need better errror handling | ||
392 | * this can fail for a number of different reasons. | ||
393 | */ | ||
394 | return (0); | ||
395 | } | ||
396 | |||
397 | if (ctx->cipher->iv_len) { | ||
398 | if (ctx->encrypt) | ||
399 | iiv = out + inl - ctx->cipher->iv_len; | ||
400 | else | ||
401 | iiv = save_iv; | ||
402 | memcpy(ctx->iv, iiv, ctx->cipher->iv_len); | ||
403 | } | ||
404 | return (1); | ||
405 | } | ||
406 | |||
407 | static int | ||
408 | cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | ||
409 | const unsigned char *iv, int enc) | ||
410 | { | ||
411 | struct dev_crypto_state *state = ctx->cipher_data; | ||
412 | struct session_op *sess = &state->d_sess; | ||
413 | int cipher = -1, i; | ||
414 | |||
415 | for (i = 0; ciphers[i].id; i++) | ||
416 | if (ctx->cipher->nid == ciphers[i].nid && | ||
417 | ctx->cipher->iv_len <= ciphers[i].ivmax && | ||
418 | ctx->key_len == ciphers[i].keylen) { | ||
419 | cipher = ciphers[i].id; | ||
420 | break; | ||
421 | } | ||
422 | |||
423 | if (!ciphers[i].id) { | ||
424 | state->d_fd = -1; | ||
425 | return (0); | ||
426 | } | ||
427 | |||
428 | memset(sess, 0, sizeof(struct session_op)); | ||
429 | |||
430 | if ((state->d_fd = get_dev_crypto()) < 0) | ||
431 | return (0); | ||
432 | |||
433 | sess->key = (caddr_t)key; | ||
434 | sess->keylen = ctx->key_len; | ||
435 | sess->cipher = cipher; | ||
436 | |||
437 | if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) { | ||
438 | close(state->d_fd); | ||
439 | state->d_fd = -1; | ||
440 | return (0); | ||
441 | } | ||
442 | return (1); | ||
443 | } | ||
444 | |||
445 | /* | ||
446 | * free anything we allocated earlier when initting a | ||
447 | * session, and close the session. | ||
448 | */ | ||
449 | static int | ||
450 | cryptodev_cleanup(EVP_CIPHER_CTX *ctx) | ||
451 | { | ||
452 | int ret = 0; | ||
453 | struct dev_crypto_state *state = ctx->cipher_data; | ||
454 | struct session_op *sess = &state->d_sess; | ||
455 | |||
456 | if (state->d_fd < 0) | ||
457 | return (0); | ||
458 | |||
459 | /* XXX if this ioctl fails, someting's wrong. the invoker | ||
460 | * may have called us with a bogus ctx, or we could | ||
461 | * have a device that for whatever reason just doesn't | ||
462 | * want to play ball - it's not clear what's right | ||
463 | * here - should this be an error? should it just | ||
464 | * increase a counter, hmm. For right now, we return | ||
465 | * 0 - I don't believe that to be "right". we could | ||
466 | * call the gorpy openssl lib error handlers that | ||
467 | * print messages to users of the library. hmm.. | ||
468 | */ | ||
469 | |||
470 | if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) { | ||
471 | ret = 0; | ||
472 | } else { | ||
473 | ret = 1; | ||
474 | } | ||
475 | close(state->d_fd); | ||
476 | state->d_fd = -1; | ||
477 | |||
478 | return (ret); | ||
479 | } | ||
480 | |||
481 | /* | ||
482 | * libcrypto EVP stuff - this is how we get wired to EVP so the engine | ||
483 | * gets called when libcrypto requests a cipher NID. | ||
484 | */ | ||
485 | |||
486 | /* RC4 */ | ||
487 | const EVP_CIPHER cryptodev_rc4 = { | ||
488 | NID_rc4, | ||
489 | 1, 16, 0, | ||
490 | EVP_CIPH_VARIABLE_LENGTH, | ||
491 | cryptodev_init_key, | ||
492 | cryptodev_cipher, | ||
493 | cryptodev_cleanup, | ||
494 | sizeof(struct dev_crypto_state), | ||
495 | NULL, | ||
496 | NULL, | ||
497 | NULL | ||
498 | }; | ||
499 | |||
500 | /* DES CBC EVP */ | ||
501 | const EVP_CIPHER cryptodev_des_cbc = { | ||
502 | NID_des_cbc, | ||
503 | 8, 8, 8, | ||
504 | EVP_CIPH_CBC_MODE, | ||
505 | cryptodev_init_key, | ||
506 | cryptodev_cipher, | ||
507 | cryptodev_cleanup, | ||
508 | sizeof(struct dev_crypto_state), | ||
509 | EVP_CIPHER_set_asn1_iv, | ||
510 | EVP_CIPHER_get_asn1_iv, | ||
511 | NULL | ||
512 | }; | ||
513 | |||
514 | /* 3DES CBC EVP */ | ||
515 | const EVP_CIPHER cryptodev_3des_cbc = { | ||
516 | NID_des_ede3_cbc, | ||
517 | 8, 24, 8, | ||
518 | EVP_CIPH_CBC_MODE, | ||
519 | cryptodev_init_key, | ||
520 | cryptodev_cipher, | ||
521 | cryptodev_cleanup, | ||
522 | sizeof(struct dev_crypto_state), | ||
523 | EVP_CIPHER_set_asn1_iv, | ||
524 | EVP_CIPHER_get_asn1_iv, | ||
525 | NULL | ||
526 | }; | ||
527 | |||
528 | const EVP_CIPHER cryptodev_bf_cbc = { | ||
529 | NID_bf_cbc, | ||
530 | 8, 16, 8, | ||
531 | EVP_CIPH_CBC_MODE, | ||
532 | cryptodev_init_key, | ||
533 | cryptodev_cipher, | ||
534 | cryptodev_cleanup, | ||
535 | sizeof(struct dev_crypto_state), | ||
536 | EVP_CIPHER_set_asn1_iv, | ||
537 | EVP_CIPHER_get_asn1_iv, | ||
538 | NULL | ||
539 | }; | ||
540 | |||
541 | const EVP_CIPHER cryptodev_cast_cbc = { | ||
542 | NID_cast5_cbc, | ||
543 | 8, 16, 8, | ||
544 | EVP_CIPH_CBC_MODE, | ||
545 | cryptodev_init_key, | ||
546 | cryptodev_cipher, | ||
547 | cryptodev_cleanup, | ||
548 | sizeof(struct dev_crypto_state), | ||
549 | EVP_CIPHER_set_asn1_iv, | ||
550 | EVP_CIPHER_get_asn1_iv, | ||
551 | NULL | ||
552 | }; | ||
553 | |||
554 | const EVP_CIPHER cryptodev_aes_cbc = { | ||
555 | NID_aes_128_cbc, | ||
556 | 16, 16, 16, | ||
557 | EVP_CIPH_CBC_MODE, | ||
558 | cryptodev_init_key, | ||
559 | cryptodev_cipher, | ||
560 | cryptodev_cleanup, | ||
561 | sizeof(struct dev_crypto_state), | ||
562 | EVP_CIPHER_set_asn1_iv, | ||
563 | EVP_CIPHER_get_asn1_iv, | ||
564 | NULL | ||
565 | }; | ||
566 | |||
567 | const EVP_CIPHER cryptodev_aes_192_cbc = { | ||
568 | NID_aes_192_cbc, | ||
569 | 16, 24, 16, | ||
570 | EVP_CIPH_CBC_MODE, | ||
571 | cryptodev_init_key, | ||
572 | cryptodev_cipher, | ||
573 | cryptodev_cleanup, | ||
574 | sizeof(struct dev_crypto_state), | ||
575 | EVP_CIPHER_set_asn1_iv, | ||
576 | EVP_CIPHER_get_asn1_iv, | ||
577 | NULL | ||
578 | }; | ||
579 | |||
580 | const EVP_CIPHER cryptodev_aes_256_cbc = { | ||
581 | NID_aes_256_cbc, | ||
582 | 16, 32, 16, | ||
583 | EVP_CIPH_CBC_MODE, | ||
584 | cryptodev_init_key, | ||
585 | cryptodev_cipher, | ||
586 | cryptodev_cleanup, | ||
587 | sizeof(struct dev_crypto_state), | ||
588 | EVP_CIPHER_set_asn1_iv, | ||
589 | EVP_CIPHER_get_asn1_iv, | ||
590 | NULL | ||
591 | }; | ||
592 | |||
593 | /* | ||
594 | * Registered by the ENGINE when used to find out how to deal with | ||
595 | * a particular NID in the ENGINE. this says what we'll do at the | ||
596 | * top level - note, that list is restricted by what we answer with | ||
597 | */ | ||
598 | static int | ||
599 | cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher, | ||
600 | const int **nids, int nid) | ||
601 | { | ||
602 | if (!cipher) | ||
603 | return (cryptodev_usable_ciphers(nids)); | ||
604 | |||
605 | switch (nid) { | ||
606 | case NID_rc4: | ||
607 | *cipher = &cryptodev_rc4; | ||
608 | break; | ||
609 | case NID_des_ede3_cbc: | ||
610 | *cipher = &cryptodev_3des_cbc; | ||
611 | break; | ||
612 | case NID_des_cbc: | ||
613 | *cipher = &cryptodev_des_cbc; | ||
614 | break; | ||
615 | case NID_bf_cbc: | ||
616 | *cipher = &cryptodev_bf_cbc; | ||
617 | break; | ||
618 | case NID_cast5_cbc: | ||
619 | *cipher = &cryptodev_cast_cbc; | ||
620 | break; | ||
621 | case NID_aes_128_cbc: | ||
622 | *cipher = &cryptodev_aes_cbc; | ||
623 | break; | ||
624 | case NID_aes_192_cbc: | ||
625 | *cipher = &cryptodev_aes_192_cbc; | ||
626 | break; | ||
627 | case NID_aes_256_cbc: | ||
628 | *cipher = &cryptodev_aes_256_cbc; | ||
629 | break; | ||
630 | default: | ||
631 | *cipher = NULL; | ||
632 | break; | ||
633 | } | ||
634 | return (*cipher != NULL); | ||
635 | } | ||
636 | |||
637 | |||
638 | #ifdef USE_CRYPTODEV_DIGESTS | ||
639 | |||
640 | /* convert digest type to cryptodev */ | ||
641 | static int | ||
642 | digest_nid_to_cryptodev(int nid) | ||
643 | { | ||
644 | int i; | ||
645 | |||
646 | for (i = 0; digests[i].id; i++) | ||
647 | if (digests[i].nid == nid) | ||
648 | return (digests[i].id); | ||
649 | return (0); | ||
650 | } | ||
651 | |||
652 | |||
653 | static int | ||
654 | digest_key_length(int nid) | ||
655 | { | ||
656 | int i; | ||
657 | |||
658 | for (i = 0; digests[i].id; i++) | ||
659 | if (digests[i].nid == nid) | ||
660 | return digests[i].keylen; | ||
661 | return (0); | ||
662 | } | ||
663 | |||
664 | |||
665 | static int cryptodev_digest_init(EVP_MD_CTX *ctx) | ||
666 | { | ||
667 | struct dev_crypto_state *state = ctx->md_data; | ||
668 | struct session_op *sess = &state->d_sess; | ||
669 | int digest; | ||
670 | |||
671 | if ((digest = digest_nid_to_cryptodev(ctx->digest->type)) == NID_undef){ | ||
672 | printf("cryptodev_digest_init: Can't get digest \n"); | ||
673 | return (0); | ||
674 | } | ||
675 | |||
676 | memset(state, 0, sizeof(struct dev_crypto_state)); | ||
677 | |||
678 | if ((state->d_fd = get_dev_crypto()) < 0) { | ||
679 | printf("cryptodev_digest_init: Can't get Dev \n"); | ||
680 | return (0); | ||
681 | } | ||
682 | |||
683 | sess->mackey = state->dummy_mac_key; | ||
684 | sess->mackeylen = digest_key_length(ctx->digest->type); | ||
685 | sess->mac = digest; | ||
686 | |||
687 | if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) { | ||
688 | close(state->d_fd); | ||
689 | state->d_fd = -1; | ||
690 | printf("cryptodev_digest_init: Open session failed\n"); | ||
691 | return (0); | ||
692 | } | ||
693 | |||
694 | return (1); | ||
695 | } | ||
696 | |||
697 | static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data, | ||
698 | size_t count) | ||
699 | { | ||
700 | struct crypt_op cryp; | ||
701 | struct dev_crypto_state *state = ctx->md_data; | ||
702 | struct session_op *sess = &state->d_sess; | ||
703 | |||
704 | if (!data || state->d_fd < 0) { | ||
705 | printf("cryptodev_digest_update: illegal inputs \n"); | ||
706 | return (0); | ||
707 | } | ||
708 | |||
709 | if (!count) { | ||
710 | return (0); | ||
711 | } | ||
712 | |||
713 | if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) { | ||
714 | /* if application doesn't support one buffer */ | ||
715 | state->mac_data = OPENSSL_realloc(state->mac_data, state->mac_len + count); | ||
716 | |||
717 | if (!state->mac_data) { | ||
718 | printf("cryptodev_digest_update: realloc failed\n"); | ||
719 | return (0); | ||
720 | } | ||
721 | |||
722 | memcpy(state->mac_data + state->mac_len, data, count); | ||
723 | state->mac_len += count; | ||
724 | |||
725 | return (1); | ||
726 | } | ||
727 | |||
728 | memset(&cryp, 0, sizeof(cryp)); | ||
729 | |||
730 | cryp.ses = sess->ses; | ||
731 | cryp.flags = 0; | ||
732 | cryp.len = count; | ||
733 | cryp.src = (caddr_t) data; | ||
734 | cryp.dst = NULL; | ||
735 | cryp.mac = (caddr_t) state->digest_res; | ||
736 | if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) { | ||
737 | printf("cryptodev_digest_update: digest failed\n"); | ||
738 | return (0); | ||
739 | } | ||
740 | return (1); | ||
741 | } | ||
742 | |||
743 | |||
744 | static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md) | ||
745 | { | ||
746 | struct crypt_op cryp; | ||
747 | struct dev_crypto_state *state = ctx->md_data; | ||
748 | struct session_op *sess = &state->d_sess; | ||
749 | |||
750 | int ret = 1; | ||
751 | |||
752 | if (!md || state->d_fd < 0) { | ||
753 | printf("cryptodev_digest_final: illegal input\n"); | ||
754 | return(0); | ||
755 | } | ||
756 | |||
757 | if (! (ctx->flags & EVP_MD_CTX_FLAG_ONESHOT) ) { | ||
758 | /* if application doesn't support one buffer */ | ||
759 | memset(&cryp, 0, sizeof(cryp)); | ||
760 | |||
761 | cryp.ses = sess->ses; | ||
762 | cryp.flags = 0; | ||
763 | cryp.len = state->mac_len; | ||
764 | cryp.src = state->mac_data; | ||
765 | cryp.dst = NULL; | ||
766 | cryp.mac = (caddr_t)md; | ||
767 | |||
768 | if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) { | ||
769 | printf("cryptodev_digest_final: digest failed\n"); | ||
770 | return (0); | ||
771 | } | ||
772 | |||
773 | return 1; | ||
774 | } | ||
775 | |||
776 | memcpy(md, state->digest_res, ctx->digest->md_size); | ||
777 | |||
778 | return (ret); | ||
779 | } | ||
780 | |||
781 | |||
782 | static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx) | ||
783 | { | ||
784 | int ret = 1; | ||
785 | struct dev_crypto_state *state = ctx->md_data; | ||
786 | struct session_op *sess = &state->d_sess; | ||
787 | |||
788 | if (state->d_fd < 0) { | ||
789 | printf("cryptodev_digest_cleanup: illegal input\n"); | ||
790 | return (0); | ||
791 | } | ||
792 | |||
793 | if (state->mac_data) { | ||
794 | OPENSSL_free(state->mac_data); | ||
795 | state->mac_data = NULL; | ||
796 | state->mac_len = 0; | ||
797 | } | ||
798 | |||
799 | if (state->copy) | ||
800 | return 1; | ||
801 | |||
802 | if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) { | ||
803 | printf("cryptodev_digest_cleanup: failed to close session\n"); | ||
804 | ret = 0; | ||
805 | } else { | ||
806 | ret = 1; | ||
807 | } | ||
808 | close(state->d_fd); | ||
809 | state->d_fd = -1; | ||
810 | |||
811 | return (ret); | ||
812 | } | ||
813 | |||
814 | static int cryptodev_digest_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from) | ||
815 | { | ||
816 | struct dev_crypto_state *fstate = from->md_data; | ||
817 | struct dev_crypto_state *dstate = to->md_data; | ||
818 | |||
819 | memcpy(dstate, fstate, sizeof(struct dev_crypto_state)); | ||
820 | |||
821 | if (fstate->mac_len != 0) { | ||
822 | dstate->mac_data = OPENSSL_malloc(fstate->mac_len); | ||
823 | memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len); | ||
824 | } | ||
825 | |||
826 | dstate->copy = 1; | ||
827 | |||
828 | return 1; | ||
829 | } | ||
830 | |||
831 | |||
832 | const EVP_MD cryptodev_sha1 = { | ||
833 | NID_sha1, | ||
834 | NID_undef, | ||
835 | SHA_DIGEST_LENGTH, | ||
836 | EVP_MD_FLAG_ONESHOT, | ||
837 | cryptodev_digest_init, | ||
838 | cryptodev_digest_update, | ||
839 | cryptodev_digest_final, | ||
840 | cryptodev_digest_copy, | ||
841 | cryptodev_digest_cleanup, | ||
842 | EVP_PKEY_NULL_method, | ||
843 | SHA_CBLOCK, | ||
844 | sizeof(struct dev_crypto_state), | ||
845 | }; | ||
846 | |||
847 | const EVP_MD cryptodev_md5 = { | ||
848 | NID_md5, | ||
849 | NID_undef, | ||
850 | 16 /* MD5_DIGEST_LENGTH */, | ||
851 | EVP_MD_FLAG_ONESHOT, | ||
852 | cryptodev_digest_init, | ||
853 | cryptodev_digest_update, | ||
854 | cryptodev_digest_final, | ||
855 | cryptodev_digest_copy, | ||
856 | cryptodev_digest_cleanup, | ||
857 | EVP_PKEY_NULL_method, | ||
858 | 64 /* MD5_CBLOCK */, | ||
859 | sizeof(struct dev_crypto_state), | ||
860 | }; | ||
861 | |||
862 | #endif /* USE_CRYPTODEV_DIGESTS */ | ||
863 | |||
864 | |||
865 | static int | ||
866 | cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest, | ||
867 | const int **nids, int nid) | ||
868 | { | ||
869 | if (!digest) | ||
870 | return (cryptodev_usable_digests(nids)); | ||
871 | |||
872 | switch (nid) { | ||
873 | #ifdef USE_CRYPTODEV_DIGESTS | ||
874 | case NID_md5: | ||
875 | *digest = &cryptodev_md5; | ||
876 | break; | ||
877 | case NID_sha1: | ||
878 | *digest = &cryptodev_sha1; | ||
879 | break; | ||
880 | default: | ||
881 | #endif /* USE_CRYPTODEV_DIGESTS */ | ||
882 | *digest = NULL; | ||
883 | break; | ||
884 | } | ||
885 | return (*digest != NULL); | ||
886 | } | ||
887 | |||
888 | /* | ||
889 | * Convert a BIGNUM to the representation that /dev/crypto needs. | ||
890 | * Upon completion of use, the caller is responsible for freeing | ||
891 | * crp->crp_p. | ||
892 | */ | ||
893 | static int | ||
894 | bn2crparam(const BIGNUM *a, struct crparam *crp) | ||
895 | { | ||
896 | int i, j, k; | ||
897 | ssize_t bytes, bits; | ||
898 | u_char *b; | ||
899 | |||
900 | crp->crp_p = NULL; | ||
901 | crp->crp_nbits = 0; | ||
902 | |||
903 | bits = BN_num_bits(a); | ||
904 | bytes = (bits + 7) / 8; | ||
905 | |||
906 | b = malloc(bytes); | ||
907 | if (b == NULL) | ||
908 | return (1); | ||
909 | memset(b, 0, bytes); | ||
910 | |||
911 | crp->crp_p = (caddr_t) b; | ||
912 | crp->crp_nbits = bits; | ||
913 | |||
914 | for (i = 0, j = 0; i < a->top; i++) { | ||
915 | for (k = 0; k < BN_BITS2 / 8; k++) { | ||
916 | if ((j + k) >= bytes) | ||
917 | return (0); | ||
918 | b[j + k] = a->d[i] >> (k * 8); | ||
919 | } | ||
920 | j += BN_BITS2 / 8; | ||
921 | } | ||
922 | return (0); | ||
923 | } | ||
924 | |||
925 | /* Convert a /dev/crypto parameter to a BIGNUM */ | ||
926 | static int | ||
927 | crparam2bn(struct crparam *crp, BIGNUM *a) | ||
928 | { | ||
929 | u_int8_t *pd; | ||
930 | int i, bytes; | ||
931 | |||
932 | bytes = (crp->crp_nbits + 7) / 8; | ||
933 | |||
934 | if (bytes == 0) | ||
935 | return (-1); | ||
936 | |||
937 | if ((pd = (u_int8_t *) malloc(bytes)) == NULL) | ||
938 | return (-1); | ||
939 | |||
940 | for (i = 0; i < bytes; i++) | ||
941 | pd[i] = crp->crp_p[bytes - i - 1]; | ||
942 | |||
943 | BN_bin2bn(pd, bytes, a); | ||
944 | free(pd); | ||
945 | |||
946 | return (0); | ||
947 | } | ||
948 | |||
949 | static void | ||
950 | zapparams(struct crypt_kop *kop) | ||
951 | { | ||
952 | int i; | ||
953 | |||
954 | for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) { | ||
955 | if (kop->crk_param[i].crp_p) | ||
956 | free(kop->crk_param[i].crp_p); | ||
957 | kop->crk_param[i].crp_p = NULL; | ||
958 | kop->crk_param[i].crp_nbits = 0; | ||
959 | } | ||
960 | } | ||
961 | |||
962 | static int | ||
963 | cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s) | ||
964 | { | ||
965 | int fd, ret = -1; | ||
966 | |||
967 | if ((fd = get_asym_dev_crypto()) < 0) | ||
968 | return (ret); | ||
969 | |||
970 | if (r) { | ||
971 | kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char)); | ||
972 | kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8; | ||
973 | kop->crk_oparams++; | ||
974 | } | ||
975 | if (s) { | ||
976 | kop->crk_param[kop->crk_iparams+1].crp_p = calloc(slen, sizeof(char)); | ||
977 | kop->crk_param[kop->crk_iparams+1].crp_nbits = slen * 8; | ||
978 | kop->crk_oparams++; | ||
979 | } | ||
980 | |||
981 | if (ioctl(fd, CIOCKEY, kop) == 0) { | ||
982 | if (r) | ||
983 | crparam2bn(&kop->crk_param[kop->crk_iparams], r); | ||
984 | if (s) | ||
985 | crparam2bn(&kop->crk_param[kop->crk_iparams+1], s); | ||
986 | ret = 0; | ||
987 | } | ||
988 | |||
989 | return (ret); | ||
990 | } | ||
991 | |||
992 | static int | ||
993 | cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
994 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont) | ||
995 | { | ||
996 | struct crypt_kop kop; | ||
997 | int ret = 1; | ||
998 | |||
999 | /* Currently, we know we can do mod exp iff we can do any | ||
1000 | * asymmetric operations at all. | ||
1001 | */ | ||
1002 | if (cryptodev_asymfeat == 0) { | ||
1003 | ret = BN_mod_exp(r, a, p, m, ctx); | ||
1004 | return (ret); | ||
1005 | } | ||
1006 | |||
1007 | memset(&kop, 0, sizeof kop); | ||
1008 | kop.crk_op = CRK_MOD_EXP; | ||
1009 | |||
1010 | /* inputs: a^p % m */ | ||
1011 | if (bn2crparam(a, &kop.crk_param[0])) | ||
1012 | goto err; | ||
1013 | if (bn2crparam(p, &kop.crk_param[1])) | ||
1014 | goto err; | ||
1015 | if (bn2crparam(m, &kop.crk_param[2])) | ||
1016 | goto err; | ||
1017 | kop.crk_iparams = 3; | ||
1018 | |||
1019 | if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) { | ||
1020 | const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); | ||
1021 | printf("OCF asym process failed, Running in software\n"); | ||
1022 | ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont); | ||
1023 | |||
1024 | } else if (ECANCELED == kop.crk_status) { | ||
1025 | const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); | ||
1026 | printf("OCF hardware operation cancelled. Running in Software\n"); | ||
1027 | ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont); | ||
1028 | } | ||
1029 | /* else cryptodev operation worked ok ==> ret = 1*/ | ||
1030 | |||
1031 | err: | ||
1032 | zapparams(&kop); | ||
1033 | return (ret); | ||
1034 | } | ||
1035 | |||
1036 | static int | ||
1037 | cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) | ||
1038 | { | ||
1039 | int r; | ||
1040 | ctx = BN_CTX_new(); | ||
1041 | r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL); | ||
1042 | BN_CTX_free(ctx); | ||
1043 | return (r); | ||
1044 | } | ||
1045 | |||
1046 | static int | ||
1047 | cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) | ||
1048 | { | ||
1049 | struct crypt_kop kop; | ||
1050 | int ret = 1; | ||
1051 | |||
1052 | if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) { | ||
1053 | /* XXX 0 means failure?? */ | ||
1054 | return (0); | ||
1055 | } | ||
1056 | |||
1057 | memset(&kop, 0, sizeof kop); | ||
1058 | kop.crk_op = CRK_MOD_EXP_CRT; | ||
1059 | /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */ | ||
1060 | if (bn2crparam(rsa->p, &kop.crk_param[0])) | ||
1061 | goto err; | ||
1062 | if (bn2crparam(rsa->q, &kop.crk_param[1])) | ||
1063 | goto err; | ||
1064 | if (bn2crparam(I, &kop.crk_param[2])) | ||
1065 | goto err; | ||
1066 | if (bn2crparam(rsa->dmp1, &kop.crk_param[3])) | ||
1067 | goto err; | ||
1068 | if (bn2crparam(rsa->dmq1, &kop.crk_param[4])) | ||
1069 | goto err; | ||
1070 | if (bn2crparam(rsa->iqmp, &kop.crk_param[5])) | ||
1071 | goto err; | ||
1072 | kop.crk_iparams = 6; | ||
1073 | |||
1074 | if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) { | ||
1075 | const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); | ||
1076 | printf("OCF asym process failed, running in Software\n"); | ||
1077 | ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx); | ||
1078 | |||
1079 | } else if (ECANCELED == kop.crk_status) { | ||
1080 | const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); | ||
1081 | printf("OCF hardware operation cancelled. Running in Software\n"); | ||
1082 | ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx); | ||
1083 | } | ||
1084 | /* else cryptodev operation worked ok ==> ret = 1*/ | ||
1085 | |||
1086 | err: | ||
1087 | zapparams(&kop); | ||
1088 | return (ret); | ||
1089 | } | ||
1090 | |||
1091 | static RSA_METHOD cryptodev_rsa = { | ||
1092 | "cryptodev RSA method", | ||
1093 | NULL, /* rsa_pub_enc */ | ||
1094 | NULL, /* rsa_pub_dec */ | ||
1095 | NULL, /* rsa_priv_enc */ | ||
1096 | NULL, /* rsa_priv_dec */ | ||
1097 | NULL, | ||
1098 | NULL, | ||
1099 | NULL, /* init */ | ||
1100 | NULL, /* finish */ | ||
1101 | 0, /* flags */ | ||
1102 | NULL, /* app_data */ | ||
1103 | NULL, /* rsa_sign */ | ||
1104 | NULL /* rsa_verify */ | ||
1105 | }; | ||
1106 | |||
1107 | static int | ||
1108 | cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p, | ||
1109 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) | ||
1110 | { | ||
1111 | return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx)); | ||
1112 | } | ||
1113 | |||
1114 | static int | ||
1115 | cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g, | ||
1116 | BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p, | ||
1117 | BN_CTX *ctx, BN_MONT_CTX *mont) | ||
1118 | { | ||
1119 | BIGNUM t2; | ||
1120 | int ret = 0; | ||
1121 | |||
1122 | BN_init(&t2); | ||
1123 | |||
1124 | /* v = ( g^u1 * y^u2 mod p ) mod q */ | ||
1125 | /* let t1 = g ^ u1 mod p */ | ||
1126 | ret = 0; | ||
1127 | |||
1128 | if (!dsa->meth->bn_mod_exp(dsa,t1,dsa->g,u1,dsa->p,ctx,mont)) | ||
1129 | goto err; | ||
1130 | |||
1131 | /* let t2 = y ^ u2 mod p */ | ||
1132 | if (!dsa->meth->bn_mod_exp(dsa,&t2,dsa->pub_key,u2,dsa->p,ctx,mont)) | ||
1133 | goto err; | ||
1134 | /* let u1 = t1 * t2 mod p */ | ||
1135 | if (!BN_mod_mul(u1,t1,&t2,dsa->p,ctx)) | ||
1136 | goto err; | ||
1137 | |||
1138 | BN_copy(t1,u1); | ||
1139 | |||
1140 | ret = 1; | ||
1141 | err: | ||
1142 | BN_free(&t2); | ||
1143 | return(ret); | ||
1144 | } | ||
1145 | |||
1146 | static DSA_SIG * | ||
1147 | cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) | ||
1148 | { | ||
1149 | struct crypt_kop kop; | ||
1150 | BIGNUM *r = NULL, *s = NULL; | ||
1151 | DSA_SIG *dsaret = NULL; | ||
1152 | |||
1153 | if ((r = BN_new()) == NULL) | ||
1154 | goto err; | ||
1155 | if ((s = BN_new()) == NULL) { | ||
1156 | BN_free(r); | ||
1157 | goto err; | ||
1158 | } | ||
1159 | |||
1160 | memset(&kop, 0, sizeof kop); | ||
1161 | kop.crk_op = CRK_DSA_SIGN; | ||
1162 | |||
1163 | /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */ | ||
1164 | kop.crk_param[0].crp_p = (caddr_t)dgst; | ||
1165 | kop.crk_param[0].crp_nbits = dlen * 8; | ||
1166 | if (bn2crparam(dsa->p, &kop.crk_param[1])) | ||
1167 | goto err; | ||
1168 | if (bn2crparam(dsa->q, &kop.crk_param[2])) | ||
1169 | goto err; | ||
1170 | if (bn2crparam(dsa->g, &kop.crk_param[3])) | ||
1171 | goto err; | ||
1172 | if (bn2crparam(dsa->priv_key, &kop.crk_param[4])) | ||
1173 | goto err; | ||
1174 | kop.crk_iparams = 5; | ||
1175 | |||
1176 | if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r, | ||
1177 | BN_num_bytes(dsa->q), s) == 0) { | ||
1178 | dsaret = DSA_SIG_new(); | ||
1179 | dsaret->r = r; | ||
1180 | dsaret->s = s; | ||
1181 | } else { | ||
1182 | const DSA_METHOD *meth = DSA_OpenSSL(); | ||
1183 | BN_free(r); | ||
1184 | BN_free(s); | ||
1185 | dsaret = (meth->dsa_do_sign)(dgst, dlen, dsa); | ||
1186 | } | ||
1187 | err: | ||
1188 | kop.crk_param[0].crp_p = NULL; | ||
1189 | zapparams(&kop); | ||
1190 | return (dsaret); | ||
1191 | } | ||
1192 | |||
1193 | static int | ||
1194 | cryptodev_dsa_verify(const unsigned char *dgst, int dlen, | ||
1195 | DSA_SIG *sig, DSA *dsa) | ||
1196 | { | ||
1197 | struct crypt_kop kop; | ||
1198 | int dsaret = 1; | ||
1199 | |||
1200 | memset(&kop, 0, sizeof kop); | ||
1201 | kop.crk_op = CRK_DSA_VERIFY; | ||
1202 | |||
1203 | /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */ | ||
1204 | kop.crk_param[0].crp_p = (caddr_t)dgst; | ||
1205 | kop.crk_param[0].crp_nbits = dlen * 8; | ||
1206 | if (bn2crparam(dsa->p, &kop.crk_param[1])) | ||
1207 | goto err; | ||
1208 | if (bn2crparam(dsa->q, &kop.crk_param[2])) | ||
1209 | goto err; | ||
1210 | if (bn2crparam(dsa->g, &kop.crk_param[3])) | ||
1211 | goto err; | ||
1212 | if (bn2crparam(dsa->pub_key, &kop.crk_param[4])) | ||
1213 | goto err; | ||
1214 | if (bn2crparam(sig->r, &kop.crk_param[5])) | ||
1215 | goto err; | ||
1216 | if (bn2crparam(sig->s, &kop.crk_param[6])) | ||
1217 | goto err; | ||
1218 | kop.crk_iparams = 7; | ||
1219 | |||
1220 | if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) { | ||
1221 | /*OCF success value is 0, if not zero, change dsaret to fail*/ | ||
1222 | if(0 != kop.crk_status) dsaret = 0; | ||
1223 | } else { | ||
1224 | const DSA_METHOD *meth = DSA_OpenSSL(); | ||
1225 | |||
1226 | dsaret = (meth->dsa_do_verify)(dgst, dlen, sig, dsa); | ||
1227 | } | ||
1228 | err: | ||
1229 | kop.crk_param[0].crp_p = NULL; | ||
1230 | zapparams(&kop); | ||
1231 | return (dsaret); | ||
1232 | } | ||
1233 | |||
1234 | static DSA_METHOD cryptodev_dsa = { | ||
1235 | "cryptodev DSA method", | ||
1236 | NULL, | ||
1237 | NULL, /* dsa_sign_setup */ | ||
1238 | NULL, | ||
1239 | NULL, /* dsa_mod_exp */ | ||
1240 | NULL, | ||
1241 | NULL, /* init */ | ||
1242 | NULL, /* finish */ | ||
1243 | 0, /* flags */ | ||
1244 | NULL /* app_data */ | ||
1245 | }; | ||
1246 | |||
1247 | static int | ||
1248 | cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a, | ||
1249 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, | ||
1250 | BN_MONT_CTX *m_ctx) | ||
1251 | { | ||
1252 | return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx)); | ||
1253 | } | ||
1254 | |||
1255 | static int | ||
1256 | cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) | ||
1257 | { | ||
1258 | struct crypt_kop kop; | ||
1259 | int dhret = 1; | ||
1260 | int fd, keylen; | ||
1261 | |||
1262 | if ((fd = get_asym_dev_crypto()) < 0) { | ||
1263 | const DH_METHOD *meth = DH_OpenSSL(); | ||
1264 | |||
1265 | return ((meth->compute_key)(key, pub_key, dh)); | ||
1266 | } | ||
1267 | |||
1268 | keylen = BN_num_bits(dh->p); | ||
1269 | |||
1270 | memset(&kop, 0, sizeof kop); | ||
1271 | kop.crk_op = CRK_DH_COMPUTE_KEY; | ||
1272 | |||
1273 | /* inputs: dh->priv_key pub_key dh->p key */ | ||
1274 | if (bn2crparam(dh->priv_key, &kop.crk_param[0])) | ||
1275 | goto err; | ||
1276 | if (bn2crparam(pub_key, &kop.crk_param[1])) | ||
1277 | goto err; | ||
1278 | if (bn2crparam(dh->p, &kop.crk_param[2])) | ||
1279 | goto err; | ||
1280 | kop.crk_iparams = 3; | ||
1281 | |||
1282 | kop.crk_param[3].crp_p = (caddr_t) key; | ||
1283 | kop.crk_param[3].crp_nbits = keylen * 8; | ||
1284 | kop.crk_oparams = 1; | ||
1285 | |||
1286 | if (ioctl(fd, CIOCKEY, &kop) == -1) { | ||
1287 | const DH_METHOD *meth = DH_OpenSSL(); | ||
1288 | |||
1289 | dhret = (meth->compute_key)(key, pub_key, dh); | ||
1290 | } | ||
1291 | err: | ||
1292 | kop.crk_param[3].crp_p = NULL; | ||
1293 | zapparams(&kop); | ||
1294 | return (dhret); | ||
1295 | } | ||
1296 | |||
1297 | static DH_METHOD cryptodev_dh = { | ||
1298 | "cryptodev DH method", | ||
1299 | NULL, /* cryptodev_dh_generate_key */ | ||
1300 | NULL, | ||
1301 | NULL, | ||
1302 | NULL, | ||
1303 | NULL, | ||
1304 | 0, /* flags */ | ||
1305 | NULL /* app_data */ | ||
1306 | }; | ||
1307 | |||
1308 | /* | ||
1309 | * ctrl right now is just a wrapper that doesn't do much | ||
1310 | * but I expect we'll want some options soon. | ||
1311 | */ | ||
1312 | static int | ||
1313 | cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void)) | ||
1314 | { | ||
1315 | #ifdef HAVE_SYSLOG_R | ||
1316 | struct syslog_data sd = SYSLOG_DATA_INIT; | ||
1317 | #endif | ||
1318 | |||
1319 | switch (cmd) { | ||
1320 | default: | ||
1321 | #ifdef HAVE_SYSLOG_R | ||
1322 | syslog_r(LOG_ERR, &sd, | ||
1323 | "cryptodev_ctrl: unknown command %d", cmd); | ||
1324 | #else | ||
1325 | syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd); | ||
1326 | #endif | ||
1327 | break; | ||
1328 | } | ||
1329 | return (1); | ||
1330 | } | ||
1331 | |||
1332 | void | ||
1333 | ENGINE_load_cryptodev(void) | ||
1334 | { | ||
1335 | ENGINE *engine = ENGINE_new(); | ||
1336 | int fd; | ||
1337 | |||
1338 | if (engine == NULL) | ||
1339 | return; | ||
1340 | if ((fd = get_dev_crypto()) < 0) { | ||
1341 | ENGINE_free(engine); | ||
1342 | return; | ||
1343 | } | ||
1344 | |||
1345 | /* | ||
1346 | * find out what asymmetric crypto algorithms we support | ||
1347 | */ | ||
1348 | if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) { | ||
1349 | close(fd); | ||
1350 | ENGINE_free(engine); | ||
1351 | return; | ||
1352 | } | ||
1353 | close(fd); | ||
1354 | |||
1355 | if (!ENGINE_set_id(engine, "cryptodev") || | ||
1356 | !ENGINE_set_name(engine, "BSD cryptodev engine") || | ||
1357 | !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) || | ||
1358 | !ENGINE_set_digests(engine, cryptodev_engine_digests) || | ||
1359 | !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) || | ||
1360 | !ENGINE_set_cmd_defns(engine, cryptodev_defns)) { | ||
1361 | ENGINE_free(engine); | ||
1362 | return; | ||
1363 | } | ||
1364 | |||
1365 | if (ENGINE_set_RSA(engine, &cryptodev_rsa)) { | ||
1366 | const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay(); | ||
1367 | |||
1368 | cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp; | ||
1369 | cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp; | ||
1370 | cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc; | ||
1371 | cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec; | ||
1372 | cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc; | ||
1373 | cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec; | ||
1374 | if (cryptodev_asymfeat & CRF_MOD_EXP) { | ||
1375 | cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp; | ||
1376 | if (cryptodev_asymfeat & CRF_MOD_EXP_CRT) | ||
1377 | cryptodev_rsa.rsa_mod_exp = | ||
1378 | cryptodev_rsa_mod_exp; | ||
1379 | else | ||
1380 | cryptodev_rsa.rsa_mod_exp = | ||
1381 | cryptodev_rsa_nocrt_mod_exp; | ||
1382 | } | ||
1383 | } | ||
1384 | |||
1385 | if (ENGINE_set_DSA(engine, &cryptodev_dsa)) { | ||
1386 | const DSA_METHOD *meth = DSA_OpenSSL(); | ||
1387 | |||
1388 | memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD)); | ||
1389 | if (cryptodev_asymfeat & CRF_DSA_SIGN) | ||
1390 | cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign; | ||
1391 | if (cryptodev_asymfeat & CRF_MOD_EXP) { | ||
1392 | cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp; | ||
1393 | cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp; | ||
1394 | } | ||
1395 | if (cryptodev_asymfeat & CRF_DSA_VERIFY) | ||
1396 | cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify; | ||
1397 | } | ||
1398 | |||
1399 | if (ENGINE_set_DH(engine, &cryptodev_dh)){ | ||
1400 | const DH_METHOD *dh_meth = DH_OpenSSL(); | ||
1401 | |||
1402 | cryptodev_dh.generate_key = dh_meth->generate_key; | ||
1403 | cryptodev_dh.compute_key = dh_meth->compute_key; | ||
1404 | cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp; | ||
1405 | if (cryptodev_asymfeat & CRF_MOD_EXP) { | ||
1406 | cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh; | ||
1407 | if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY) | ||
1408 | cryptodev_dh.compute_key = | ||
1409 | cryptodev_dh_compute_key; | ||
1410 | } | ||
1411 | } | ||
1412 | |||
1413 | ENGINE_add(engine); | ||
1414 | ENGINE_free(engine); | ||
1415 | ERR_clear_error(); | ||
1416 | } | ||
1417 | |||
1418 | #endif /* HAVE_CRYPTODEV */ | ||
diff --git a/src/lib/libcrypto/engine/engine.h b/src/lib/libcrypto/engine/engine.h index 943aeae215..8ad11b15d7 100644 --- a/src/lib/libcrypto/engine/engine.h +++ b/src/lib/libcrypto/engine/engine.h | |||
@@ -344,6 +344,7 @@ void ENGINE_load_gost(void); | |||
344 | #endif | 344 | #endif |
345 | #endif | 345 | #endif |
346 | void ENGINE_load_cryptodev(void); | 346 | void ENGINE_load_cryptodev(void); |
347 | void ENGINE_load_aesni(void); | ||
347 | void ENGINE_load_builtin_engines(void); | 348 | void ENGINE_load_builtin_engines(void); |
348 | 349 | ||
349 | /* Get and set global flags (ENGINE_TABLE_FLAG_***) for the implementation | 350 | /* Get and set global flags (ENGINE_TABLE_FLAG_***) for the implementation |
@@ -677,7 +678,6 @@ typedef struct st_dynamic_fns { | |||
677 | * can be fully instantiated with IMPLEMENT_DYNAMIC_CHECK_FN(). */ | 678 | * can be fully instantiated with IMPLEMENT_DYNAMIC_CHECK_FN(). */ |
678 | typedef unsigned long (*dynamic_v_check_fn)(unsigned long ossl_version); | 679 | typedef unsigned long (*dynamic_v_check_fn)(unsigned long ossl_version); |
679 | #define IMPLEMENT_DYNAMIC_CHECK_FN() \ | 680 | #define IMPLEMENT_DYNAMIC_CHECK_FN() \ |
680 | OPENSSL_EXPORT unsigned long v_check(unsigned long v); \ | ||
681 | OPENSSL_EXPORT unsigned long v_check(unsigned long v) { \ | 681 | OPENSSL_EXPORT unsigned long v_check(unsigned long v) { \ |
682 | if(v >= OSSL_DYNAMIC_OLDEST) return OSSL_DYNAMIC_VERSION; \ | 682 | if(v >= OSSL_DYNAMIC_OLDEST) return OSSL_DYNAMIC_VERSION; \ |
683 | return 0; } | 683 | return 0; } |
@@ -701,8 +701,6 @@ typedef int (*dynamic_bind_engine)(ENGINE *e, const char *id, | |||
701 | const dynamic_fns *fns); | 701 | const dynamic_fns *fns); |
702 | #define IMPLEMENT_DYNAMIC_BIND_FN(fn) \ | 702 | #define IMPLEMENT_DYNAMIC_BIND_FN(fn) \ |
703 | OPENSSL_EXPORT \ | 703 | OPENSSL_EXPORT \ |
704 | int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns); \ | ||
705 | OPENSSL_EXPORT \ | ||
706 | int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns) { \ | 704 | int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns) { \ |
707 | if(ENGINE_get_static_state() == fns->static_state) goto skip_cbs; \ | 705 | if(ENGINE_get_static_state() == fns->static_state) goto skip_cbs; \ |
708 | if(!CRYPTO_set_mem_functions(fns->mem_fns.malloc_cb, \ | 706 | if(!CRYPTO_set_mem_functions(fns->mem_fns.malloc_cb, \ |
diff --git a/src/lib/libcrypto/engine/enginetest.c b/src/lib/libcrypto/engine/enginetest.c new file mode 100644 index 0000000000..f4d70e7e0a --- /dev/null +++ b/src/lib/libcrypto/engine/enginetest.c | |||
@@ -0,0 +1,283 @@ | |||
1 | /* crypto/engine/enginetest.c */ | ||
2 | /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL | ||
3 | * project 2000. | ||
4 | */ | ||
5 | /* ==================================================================== | ||
6 | * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions | ||
10 | * are met: | ||
11 | * | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in | ||
17 | * the documentation and/or other materials provided with the | ||
18 | * distribution. | ||
19 | * | ||
20 | * 3. All advertising materials mentioning features or use of this | ||
21 | * software must display the following acknowledgment: | ||
22 | * "This product includes software developed by the OpenSSL Project | ||
23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
24 | * | ||
25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
26 | * endorse or promote products derived from this software without | ||
27 | * prior written permission. For written permission, please contact | ||
28 | * licensing@OpenSSL.org. | ||
29 | * | ||
30 | * 5. Products derived from this software may not be called "OpenSSL" | ||
31 | * nor may "OpenSSL" appear in their names without prior written | ||
32 | * permission of the OpenSSL Project. | ||
33 | * | ||
34 | * 6. Redistributions of any form whatsoever must retain the following | ||
35 | * acknowledgment: | ||
36 | * "This product includes software developed by the OpenSSL Project | ||
37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
38 | * | ||
39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
51 | * ==================================================================== | ||
52 | * | ||
53 | * This product includes cryptographic software written by Eric Young | ||
54 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
55 | * Hudson (tjh@cryptsoft.com). | ||
56 | * | ||
57 | */ | ||
58 | |||
59 | #include <stdio.h> | ||
60 | #include <string.h> | ||
61 | #include <openssl/e_os2.h> | ||
62 | |||
63 | #ifdef OPENSSL_NO_ENGINE | ||
64 | int main(int argc, char *argv[]) | ||
65 | { | ||
66 | printf("No ENGINE support\n"); | ||
67 | return(0); | ||
68 | } | ||
69 | #else | ||
70 | #include <openssl/buffer.h> | ||
71 | #include <openssl/crypto.h> | ||
72 | #include <openssl/engine.h> | ||
73 | #include <openssl/err.h> | ||
74 | |||
75 | static void display_engine_list(void) | ||
76 | { | ||
77 | ENGINE *h; | ||
78 | int loop; | ||
79 | |||
80 | h = ENGINE_get_first(); | ||
81 | loop = 0; | ||
82 | printf("listing available engine types\n"); | ||
83 | while(h) | ||
84 | { | ||
85 | printf("engine %i, id = \"%s\", name = \"%s\"\n", | ||
86 | loop++, ENGINE_get_id(h), ENGINE_get_name(h)); | ||
87 | h = ENGINE_get_next(h); | ||
88 | } | ||
89 | printf("end of list\n"); | ||
90 | /* ENGINE_get_first() increases the struct_ref counter, so we | ||
91 | must call ENGINE_free() to decrease it again */ | ||
92 | ENGINE_free(h); | ||
93 | } | ||
94 | |||
95 | int main(int argc, char *argv[]) | ||
96 | { | ||
97 | ENGINE *block[512]; | ||
98 | char buf[256]; | ||
99 | const char *id, *name; | ||
100 | ENGINE *ptr; | ||
101 | int loop; | ||
102 | int to_return = 1; | ||
103 | ENGINE *new_h1 = NULL; | ||
104 | ENGINE *new_h2 = NULL; | ||
105 | ENGINE *new_h3 = NULL; | ||
106 | ENGINE *new_h4 = NULL; | ||
107 | |||
108 | /* enable memory leak checking unless explicitly disabled */ | ||
109 | if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off")))) | ||
110 | { | ||
111 | CRYPTO_malloc_debug_init(); | ||
112 | CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL); | ||
113 | } | ||
114 | else | ||
115 | { | ||
116 | /* OPENSSL_DEBUG_MEMORY=off */ | ||
117 | CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0); | ||
118 | } | ||
119 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); | ||
120 | ERR_load_crypto_strings(); | ||
121 | |||
122 | memset(block, 0, 512 * sizeof(ENGINE *)); | ||
123 | if(((new_h1 = ENGINE_new()) == NULL) || | ||
124 | !ENGINE_set_id(new_h1, "test_id0") || | ||
125 | !ENGINE_set_name(new_h1, "First test item") || | ||
126 | ((new_h2 = ENGINE_new()) == NULL) || | ||
127 | !ENGINE_set_id(new_h2, "test_id1") || | ||
128 | !ENGINE_set_name(new_h2, "Second test item") || | ||
129 | ((new_h3 = ENGINE_new()) == NULL) || | ||
130 | !ENGINE_set_id(new_h3, "test_id2") || | ||
131 | !ENGINE_set_name(new_h3, "Third test item") || | ||
132 | ((new_h4 = ENGINE_new()) == NULL) || | ||
133 | !ENGINE_set_id(new_h4, "test_id3") || | ||
134 | !ENGINE_set_name(new_h4, "Fourth test item")) | ||
135 | { | ||
136 | printf("Couldn't set up test ENGINE structures\n"); | ||
137 | goto end; | ||
138 | } | ||
139 | printf("\nenginetest beginning\n\n"); | ||
140 | display_engine_list(); | ||
141 | if(!ENGINE_add(new_h1)) | ||
142 | { | ||
143 | printf("Add failed!\n"); | ||
144 | goto end; | ||
145 | } | ||
146 | display_engine_list(); | ||
147 | ptr = ENGINE_get_first(); | ||
148 | if(!ENGINE_remove(ptr)) | ||
149 | { | ||
150 | printf("Remove failed!\n"); | ||
151 | goto end; | ||
152 | } | ||
153 | if (ptr) | ||
154 | ENGINE_free(ptr); | ||
155 | display_engine_list(); | ||
156 | if(!ENGINE_add(new_h3) || !ENGINE_add(new_h2)) | ||
157 | { | ||
158 | printf("Add failed!\n"); | ||
159 | goto end; | ||
160 | } | ||
161 | display_engine_list(); | ||
162 | if(!ENGINE_remove(new_h2)) | ||
163 | { | ||
164 | printf("Remove failed!\n"); | ||
165 | goto end; | ||
166 | } | ||
167 | display_engine_list(); | ||
168 | if(!ENGINE_add(new_h4)) | ||
169 | { | ||
170 | printf("Add failed!\n"); | ||
171 | goto end; | ||
172 | } | ||
173 | display_engine_list(); | ||
174 | if(ENGINE_add(new_h3)) | ||
175 | { | ||
176 | printf("Add *should* have failed but didn't!\n"); | ||
177 | goto end; | ||
178 | } | ||
179 | else | ||
180 | printf("Add that should fail did.\n"); | ||
181 | ERR_clear_error(); | ||
182 | if(ENGINE_remove(new_h2)) | ||
183 | { | ||
184 | printf("Remove *should* have failed but didn't!\n"); | ||
185 | goto end; | ||
186 | } | ||
187 | else | ||
188 | printf("Remove that should fail did.\n"); | ||
189 | ERR_clear_error(); | ||
190 | if(!ENGINE_remove(new_h3)) | ||
191 | { | ||
192 | printf("Remove failed!\n"); | ||
193 | goto end; | ||
194 | } | ||
195 | display_engine_list(); | ||
196 | if(!ENGINE_remove(new_h4)) | ||
197 | { | ||
198 | printf("Remove failed!\n"); | ||
199 | goto end; | ||
200 | } | ||
201 | display_engine_list(); | ||
202 | /* Depending on whether there's any hardware support compiled | ||
203 | * in, this remove may be destined to fail. */ | ||
204 | ptr = ENGINE_get_first(); | ||
205 | if(ptr) | ||
206 | if(!ENGINE_remove(ptr)) | ||
207 | printf("Remove failed!i - probably no hardware " | ||
208 | "support present.\n"); | ||
209 | if (ptr) | ||
210 | ENGINE_free(ptr); | ||
211 | display_engine_list(); | ||
212 | if(!ENGINE_add(new_h1) || !ENGINE_remove(new_h1)) | ||
213 | { | ||
214 | printf("Couldn't add and remove to an empty list!\n"); | ||
215 | goto end; | ||
216 | } | ||
217 | else | ||
218 | printf("Successfully added and removed to an empty list!\n"); | ||
219 | printf("About to beef up the engine-type list\n"); | ||
220 | for(loop = 0; loop < 512; loop++) | ||
221 | { | ||
222 | sprintf(buf, "id%i", loop); | ||
223 | id = BUF_strdup(buf); | ||
224 | sprintf(buf, "Fake engine type %i", loop); | ||
225 | name = BUF_strdup(buf); | ||
226 | if(((block[loop] = ENGINE_new()) == NULL) || | ||
227 | !ENGINE_set_id(block[loop], id) || | ||
228 | !ENGINE_set_name(block[loop], name)) | ||
229 | { | ||
230 | printf("Couldn't create block of ENGINE structures.\n" | ||
231 | "I'll probably also core-dump now, damn.\n"); | ||
232 | goto end; | ||
233 | } | ||
234 | } | ||
235 | for(loop = 0; loop < 512; loop++) | ||
236 | { | ||
237 | if(!ENGINE_add(block[loop])) | ||
238 | { | ||
239 | printf("\nAdding stopped at %i, (%s,%s)\n", | ||
240 | loop, ENGINE_get_id(block[loop]), | ||
241 | ENGINE_get_name(block[loop])); | ||
242 | goto cleanup_loop; | ||
243 | } | ||
244 | else | ||
245 | printf("."); fflush(stdout); | ||
246 | } | ||
247 | cleanup_loop: | ||
248 | printf("\nAbout to empty the engine-type list\n"); | ||
249 | while((ptr = ENGINE_get_first()) != NULL) | ||
250 | { | ||
251 | if(!ENGINE_remove(ptr)) | ||
252 | { | ||
253 | printf("\nRemove failed!\n"); | ||
254 | goto end; | ||
255 | } | ||
256 | ENGINE_free(ptr); | ||
257 | printf("."); fflush(stdout); | ||
258 | } | ||
259 | for(loop = 0; loop < 512; loop++) | ||
260 | { | ||
261 | OPENSSL_free((void *)ENGINE_get_id(block[loop])); | ||
262 | OPENSSL_free((void *)ENGINE_get_name(block[loop])); | ||
263 | } | ||
264 | printf("\nTests completed happily\n"); | ||
265 | to_return = 0; | ||
266 | end: | ||
267 | if(to_return) | ||
268 | ERR_print_errors_fp(stderr); | ||
269 | if(new_h1) ENGINE_free(new_h1); | ||
270 | if(new_h2) ENGINE_free(new_h2); | ||
271 | if(new_h3) ENGINE_free(new_h3); | ||
272 | if(new_h4) ENGINE_free(new_h4); | ||
273 | for(loop = 0; loop < 512; loop++) | ||
274 | if(block[loop]) | ||
275 | ENGINE_free(block[loop]); | ||
276 | ENGINE_cleanup(); | ||
277 | CRYPTO_cleanup_all_ex_data(); | ||
278 | ERR_free_strings(); | ||
279 | ERR_remove_thread_state(NULL); | ||
280 | CRYPTO_mem_leaks_fp(stderr); | ||
281 | return to_return; | ||
282 | } | ||
283 | #endif | ||
diff --git a/src/lib/libcrypto/engine/hw_cryptodev.c b/src/lib/libcrypto/engine/hw_cryptodev.c new file mode 100644 index 0000000000..0e80ca051a --- /dev/null +++ b/src/lib/libcrypto/engine/hw_cryptodev.c | |||
@@ -0,0 +1,1367 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2004 Theo de Raadt | ||
3 | * Copyright (c) 2002 Bob Beck <beck@openbsd.org> | ||
4 | * Copyright (c) 2002 Markus Friedl | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | * | ||
27 | */ | ||
28 | |||
29 | #include <openssl/objects.h> | ||
30 | #include <openssl/engine.h> | ||
31 | #include <openssl/evp.h> | ||
32 | |||
33 | #if (defined(__unix__) || defined(unix)) && !defined(USG) | ||
34 | #include <sys/param.h> | ||
35 | # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041) | ||
36 | # define HAVE_CRYPTODEV | ||
37 | # endif | ||
38 | # if (OpenBSD >= 200110) | ||
39 | # define HAVE_SYSLOG_R | ||
40 | # endif | ||
41 | #endif | ||
42 | |||
43 | #ifndef HAVE_CRYPTODEV | ||
44 | |||
45 | void | ||
46 | ENGINE_load_cryptodev(void) | ||
47 | { | ||
48 | /* This is a NOP on platforms without /dev/crypto */ | ||
49 | return; | ||
50 | } | ||
51 | |||
52 | #else | ||
53 | |||
54 | #include <sys/types.h> | ||
55 | #include <crypto/cryptodev.h> | ||
56 | #include <sys/ioctl.h> | ||
57 | |||
58 | #include <errno.h> | ||
59 | #include <fcntl.h> | ||
60 | #include <limits.h> | ||
61 | #include <stdarg.h> | ||
62 | #include <stdio.h> | ||
63 | #include <string.h> | ||
64 | #include <syslog.h> | ||
65 | #include <unistd.h> | ||
66 | |||
67 | #if defined(__i386__) || defined(__amd64__) | ||
68 | #include <sys/sysctl.h> | ||
69 | #include <machine/cpu.h> | ||
70 | #include <machine/specialreg.h> | ||
71 | |||
72 | #include <ssl/aes.h> | ||
73 | |||
74 | static int check_viac3aes(void); | ||
75 | #endif | ||
76 | |||
77 | #define CRYPTO_VIAC3_MAX 3 | ||
78 | |||
79 | struct dev_crypto_state { | ||
80 | struct session_op d_sess; | ||
81 | int d_fd; | ||
82 | }; | ||
83 | |||
84 | struct dev_crypto_cipher { | ||
85 | int c_id; | ||
86 | int c_nid; | ||
87 | int c_ivmax; | ||
88 | int c_keylen; | ||
89 | }; | ||
90 | |||
91 | static u_int32_t cryptodev_asymfeat = 0; | ||
92 | |||
93 | static int get_asym_dev_crypto(void); | ||
94 | static int open_dev_crypto(void); | ||
95 | static int get_dev_crypto(void); | ||
96 | static struct dev_crypto_cipher *cipher_nid_to_cryptodev(int nid); | ||
97 | static int get_cryptodev_ciphers(const int **cnids); | ||
98 | /*static int get_cryptodev_digests(const int **cnids);*/ | ||
99 | static int cryptodev_usable_ciphers(const int **nids); | ||
100 | static int cryptodev_usable_digests(const int **nids); | ||
101 | static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | ||
102 | const unsigned char *in, size_t inl); | ||
103 | static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | ||
104 | const unsigned char *iv, int enc); | ||
105 | static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx); | ||
106 | static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher, | ||
107 | const int **nids, int nid); | ||
108 | static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest, | ||
109 | const int **nids, int nid); | ||
110 | static int bn2crparam(const BIGNUM *a, struct crparam *crp); | ||
111 | static int crparam2bn(struct crparam *crp, BIGNUM *a); | ||
112 | static void zapparams(struct crypt_kop *kop); | ||
113 | static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, | ||
114 | int slen, BIGNUM *s); | ||
115 | |||
116 | static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, | ||
117 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); | ||
118 | static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, | ||
119 | RSA *rsa, BN_CTX *ctx); | ||
120 | static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, | ||
121 | BN_CTX *ctx); | ||
122 | static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, | ||
123 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); | ||
124 | static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g, | ||
125 | BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p, | ||
126 | BN_CTX *ctx, BN_MONT_CTX *mont); | ||
127 | static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, | ||
128 | int dlen, DSA *dsa); | ||
129 | static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len, | ||
130 | DSA_SIG *sig, DSA *dsa); | ||
131 | static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a, | ||
132 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, | ||
133 | BN_MONT_CTX *m_ctx); | ||
134 | static int cryptodev_dh_compute_key(unsigned char *key, | ||
135 | const BIGNUM *pub_key, DH *dh); | ||
136 | static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, | ||
137 | void (*f)()); | ||
138 | void ENGINE_load_cryptodev(void); | ||
139 | |||
140 | static const ENGINE_CMD_DEFN cryptodev_defns[] = { | ||
141 | { 0, NULL, NULL, 0 } | ||
142 | }; | ||
143 | |||
144 | static struct dev_crypto_cipher ciphers[] = { | ||
145 | { CRYPTO_DES_CBC, NID_des_cbc, 8, 8, }, | ||
146 | { CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24, }, | ||
147 | { CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16, }, | ||
148 | { CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24, }, | ||
149 | { CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32, }, | ||
150 | { CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, }, | ||
151 | { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, }, | ||
152 | { 0, NID_undef, 0, 0, }, | ||
153 | }; | ||
154 | |||
155 | #if 0 /* UNUSED */ | ||
156 | static struct { | ||
157 | int id; | ||
158 | int nid; | ||
159 | } digests[] = { | ||
160 | { CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, }, | ||
161 | { CRYPTO_RIPEMD160_HMAC, NID_ripemd160, }, | ||
162 | { CRYPTO_MD5_KPDK, NID_undef, }, | ||
163 | { CRYPTO_SHA1_KPDK, NID_undef, }, | ||
164 | { CRYPTO_MD5, NID_md5, }, | ||
165 | { CRYPTO_SHA1, NID_undef, }, | ||
166 | { 0, NID_undef, }, | ||
167 | }; | ||
168 | #endif | ||
169 | |||
170 | /* | ||
171 | * Return a fd if /dev/crypto seems usable, -1 otherwise. | ||
172 | */ | ||
173 | static int | ||
174 | open_dev_crypto(void) | ||
175 | { | ||
176 | static int fd = -1; | ||
177 | |||
178 | if (fd == -1) { | ||
179 | if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1) | ||
180 | return (-1); | ||
181 | /* close on exec */ | ||
182 | if (fcntl(fd, F_SETFD, 1) == -1) { | ||
183 | close(fd); | ||
184 | fd = -1; | ||
185 | return (-1); | ||
186 | } | ||
187 | } | ||
188 | return (fd); | ||
189 | } | ||
190 | |||
191 | static int | ||
192 | get_dev_crypto(void) | ||
193 | { | ||
194 | int fd, retfd; | ||
195 | |||
196 | if ((fd = open_dev_crypto()) == -1) | ||
197 | return (-1); | ||
198 | if (ioctl(fd, CRIOGET, &retfd) == -1) { | ||
199 | close(fd); | ||
200 | return (-1); | ||
201 | } | ||
202 | |||
203 | /* close on exec */ | ||
204 | if (fcntl(retfd, F_SETFD, 1) == -1) { | ||
205 | close(retfd); | ||
206 | return (-1); | ||
207 | } | ||
208 | return (retfd); | ||
209 | } | ||
210 | |||
211 | /* Caching version for asym operations */ | ||
212 | static int | ||
213 | get_asym_dev_crypto(void) | ||
214 | { | ||
215 | static int fd = -1; | ||
216 | |||
217 | if (fd == -1) | ||
218 | fd = get_dev_crypto(); | ||
219 | return fd; | ||
220 | } | ||
221 | |||
222 | /* convert libcrypto nids to cryptodev */ | ||
223 | static struct dev_crypto_cipher * | ||
224 | cipher_nid_to_cryptodev(int nid) | ||
225 | { | ||
226 | int i; | ||
227 | |||
228 | for (i = 0; ciphers[i].c_id; i++) | ||
229 | if (ciphers[i].c_nid == nid) | ||
230 | return (&ciphers[i]); | ||
231 | return (NULL); | ||
232 | } | ||
233 | |||
234 | /* | ||
235 | * Find out what ciphers /dev/crypto will let us have a session for. | ||
236 | * XXX note, that some of these openssl doesn't deal with yet! | ||
237 | * returning them here is harmless, as long as we return NULL | ||
238 | * when asked for a handler in the cryptodev_engine_ciphers routine | ||
239 | */ | ||
240 | static int | ||
241 | get_cryptodev_ciphers(const int **cnids) | ||
242 | { | ||
243 | static int nids[CRYPTO_ALGORITHM_MAX + CRYPTO_VIAC3_MAX + 1]; | ||
244 | struct session_op sess; | ||
245 | int fd, i, count = 0; | ||
246 | |||
247 | if ((fd = get_dev_crypto()) < 0) { | ||
248 | *cnids = NULL; | ||
249 | return (0); | ||
250 | } | ||
251 | memset(&sess, 0, sizeof(sess)); | ||
252 | sess.key = (caddr_t)"123456781234567812345678"; | ||
253 | |||
254 | for (i = 0; ciphers[i].c_id && count <= CRYPTO_ALGORITHM_MAX; i++) { | ||
255 | if (ciphers[i].c_nid == NID_undef) | ||
256 | continue; | ||
257 | sess.cipher = ciphers[i].c_id; | ||
258 | sess.keylen = ciphers[i].c_keylen; | ||
259 | sess.mac = 0; | ||
260 | if (ioctl(fd, CIOCGSESSION, &sess) != -1 && | ||
261 | ioctl(fd, CIOCFSESSION, &sess.ses) != -1) | ||
262 | nids[count++] = ciphers[i].c_nid; | ||
263 | } | ||
264 | close(fd); | ||
265 | |||
266 | #if defined(__i386__) || defined(__amd64__) | ||
267 | /* | ||
268 | * Always check for the VIA C3 AES instructions; | ||
269 | * even if /dev/crypto is disabled. | ||
270 | */ | ||
271 | if (check_viac3aes() >= 1) { | ||
272 | int have_NID_aes_128_cbc = 0; | ||
273 | int have_NID_aes_192_cbc = 0; | ||
274 | int have_NID_aes_256_cbc = 0; | ||
275 | |||
276 | for (i = 0; i < count; i++) { | ||
277 | if (nids[i] == NID_aes_128_cbc) | ||
278 | have_NID_aes_128_cbc = 1; | ||
279 | if (nids[i] == NID_aes_192_cbc) | ||
280 | have_NID_aes_192_cbc = 1; | ||
281 | if (nids[i] == NID_aes_256_cbc) | ||
282 | have_NID_aes_256_cbc = 1; | ||
283 | } | ||
284 | if (!have_NID_aes_128_cbc) | ||
285 | nids[count++] = NID_aes_128_cbc; | ||
286 | if (!have_NID_aes_192_cbc) | ||
287 | nids[count++] = NID_aes_192_cbc; | ||
288 | if (!have_NID_aes_256_cbc) | ||
289 | nids[count++] = NID_aes_256_cbc; | ||
290 | } | ||
291 | #endif | ||
292 | |||
293 | if (count > 0) | ||
294 | *cnids = nids; | ||
295 | else | ||
296 | *cnids = NULL; | ||
297 | return (count); | ||
298 | } | ||
299 | |||
300 | /* | ||
301 | * Find out what digests /dev/crypto will let us have a session for. | ||
302 | * XXX note, that some of these openssl doesn't deal with yet! | ||
303 | * returning them here is harmless, as long as we return NULL | ||
304 | * when asked for a handler in the cryptodev_engine_digests routine | ||
305 | */ | ||
306 | #if 0 /* UNUSED */ | ||
307 | static int | ||
308 | get_cryptodev_digests(const int **cnids) | ||
309 | { | ||
310 | static int nids[CRYPTO_ALGORITHM_MAX]; | ||
311 | struct session_op sess; | ||
312 | int fd, i, count = 0; | ||
313 | |||
314 | if ((fd = get_dev_crypto()) < 0) { | ||
315 | *cnids = NULL; | ||
316 | return (0); | ||
317 | } | ||
318 | memset(&sess, 0, sizeof(sess)); | ||
319 | for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) { | ||
320 | if (digests[i].nid == NID_undef) | ||
321 | continue; | ||
322 | sess.mac = digests[i].id; | ||
323 | sess.cipher = 0; | ||
324 | if (ioctl(fd, CIOCGSESSION, &sess) != -1 && | ||
325 | ioctl(fd, CIOCFSESSION, &sess.ses) != -1) | ||
326 | nids[count++] = digests[i].nid; | ||
327 | } | ||
328 | close(fd); | ||
329 | |||
330 | if (count > 0) | ||
331 | *cnids = nids; | ||
332 | else | ||
333 | *cnids = NULL; | ||
334 | return (count); | ||
335 | } | ||
336 | #endif | ||
337 | |||
338 | /* | ||
339 | * Find the useable ciphers|digests from dev/crypto - this is the first | ||
340 | * thing called by the engine init crud which determines what it | ||
341 | * can use for ciphers from this engine. We want to return | ||
342 | * only what we can do, anythine else is handled by software. | ||
343 | * | ||
344 | * If we can't initialize the device to do anything useful for | ||
345 | * any reason, we want to return a NULL array, and 0 length, | ||
346 | * which forces everything to be done is software. By putting | ||
347 | * the initalization of the device in here, we ensure we can | ||
348 | * use this engine as the default, and if for whatever reason | ||
349 | * /dev/crypto won't do what we want it will just be done in | ||
350 | * software | ||
351 | * | ||
352 | * This can (should) be greatly expanded to perhaps take into | ||
353 | * account speed of the device, and what we want to do. | ||
354 | * (although the disabling of particular alg's could be controlled | ||
355 | * by the device driver with sysctl's.) - this is where we | ||
356 | * want most of the decisions made about what we actually want | ||
357 | * to use from /dev/crypto. | ||
358 | */ | ||
359 | static int | ||
360 | cryptodev_usable_ciphers(const int **nids) | ||
361 | { | ||
362 | return (get_cryptodev_ciphers(nids)); | ||
363 | } | ||
364 | |||
365 | static int | ||
366 | cryptodev_usable_digests(const int **nids) | ||
367 | { | ||
368 | /* | ||
369 | * XXXX just disable all digests for now, because it sucks. | ||
370 | * we need a better way to decide this - i.e. I may not | ||
371 | * want digests on slow cards like hifn on fast machines, | ||
372 | * but might want them on slow or loaded machines, etc. | ||
373 | * will also want them when using crypto cards that don't | ||
374 | * suck moose gonads - would be nice to be able to decide something | ||
375 | * as reasonable default without having hackery that's card dependent. | ||
376 | * of course, the default should probably be just do everything, | ||
377 | * with perhaps a sysctl to turn algoritms off (or have them off | ||
378 | * by default) on cards that generally suck like the hifn. | ||
379 | */ | ||
380 | *nids = NULL; | ||
381 | return (0); | ||
382 | } | ||
383 | |||
384 | static int | ||
385 | cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | ||
386 | const unsigned char *in, size_t inl) | ||
387 | { | ||
388 | struct crypt_op cryp; | ||
389 | struct dev_crypto_state *state = ctx->cipher_data; | ||
390 | struct session_op *sess = &state->d_sess; | ||
391 | void *iiv; | ||
392 | unsigned char save_iv[EVP_MAX_IV_LENGTH]; | ||
393 | |||
394 | if (state->d_fd < 0) | ||
395 | return (0); | ||
396 | if (!inl) | ||
397 | return (1); | ||
398 | if ((inl % ctx->cipher->block_size) != 0) | ||
399 | return (0); | ||
400 | |||
401 | memset(&cryp, 0, sizeof(cryp)); | ||
402 | |||
403 | cryp.ses = sess->ses; | ||
404 | cryp.flags = 0; | ||
405 | cryp.len = inl; | ||
406 | cryp.src = (caddr_t) in; | ||
407 | cryp.dst = (caddr_t) out; | ||
408 | cryp.mac = 0; | ||
409 | |||
410 | cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT; | ||
411 | |||
412 | if (ctx->cipher->iv_len) { | ||
413 | cryp.iv = (caddr_t) ctx->iv; | ||
414 | if (!ctx->encrypt) { | ||
415 | iiv = (void *) in + inl - ctx->cipher->iv_len; | ||
416 | memcpy(save_iv, iiv, ctx->cipher->iv_len); | ||
417 | } | ||
418 | } else | ||
419 | cryp.iv = NULL; | ||
420 | |||
421 | if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) { | ||
422 | /* XXX need better errror handling | ||
423 | * this can fail for a number of different reasons. | ||
424 | */ | ||
425 | return (0); | ||
426 | } | ||
427 | |||
428 | if (ctx->cipher->iv_len) { | ||
429 | if (ctx->encrypt) | ||
430 | iiv = (void *) out + inl - ctx->cipher->iv_len; | ||
431 | else | ||
432 | iiv = save_iv; | ||
433 | memcpy(ctx->iv, iiv, ctx->cipher->iv_len); | ||
434 | } | ||
435 | return (1); | ||
436 | } | ||
437 | |||
438 | static int | ||
439 | cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | ||
440 | const unsigned char *iv, int enc) | ||
441 | { | ||
442 | struct dev_crypto_state *state = ctx->cipher_data; | ||
443 | struct session_op *sess = &state->d_sess; | ||
444 | struct dev_crypto_cipher *cipher; | ||
445 | |||
446 | if ((cipher = cipher_nid_to_cryptodev(ctx->cipher->nid)) == NULL) | ||
447 | return (0); | ||
448 | |||
449 | if (ctx->cipher->iv_len > cipher->c_ivmax) | ||
450 | return (0); | ||
451 | |||
452 | if (ctx->key_len != cipher->c_keylen) | ||
453 | return (0); | ||
454 | |||
455 | memset(sess, 0, sizeof(struct session_op)); | ||
456 | |||
457 | if ((state->d_fd = get_dev_crypto()) < 0) | ||
458 | return (0); | ||
459 | |||
460 | sess->key = (unsigned char *)key; | ||
461 | sess->keylen = ctx->key_len; | ||
462 | sess->cipher = cipher->c_id; | ||
463 | |||
464 | if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) { | ||
465 | close(state->d_fd); | ||
466 | state->d_fd = -1; | ||
467 | return (0); | ||
468 | } | ||
469 | return (1); | ||
470 | } | ||
471 | |||
472 | /* | ||
473 | * free anything we allocated earlier when initting a | ||
474 | * session, and close the session. | ||
475 | */ | ||
476 | static int | ||
477 | cryptodev_cleanup(EVP_CIPHER_CTX *ctx) | ||
478 | { | ||
479 | int ret = 0; | ||
480 | struct dev_crypto_state *state = ctx->cipher_data; | ||
481 | struct session_op *sess = &state->d_sess; | ||
482 | |||
483 | if (state->d_fd < 0) | ||
484 | return (0); | ||
485 | |||
486 | /* XXX if this ioctl fails, someting's wrong. the invoker | ||
487 | * may have called us with a bogus ctx, or we could | ||
488 | * have a device that for whatever reason just doesn't | ||
489 | * want to play ball - it's not clear what's right | ||
490 | * here - should this be an error? should it just | ||
491 | * increase a counter, hmm. For right now, we return | ||
492 | * 0 - I don't believe that to be "right". we could | ||
493 | * call the gorpy openssl lib error handlers that | ||
494 | * print messages to users of the library. hmm.. | ||
495 | */ | ||
496 | |||
497 | if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) { | ||
498 | ret = 0; | ||
499 | } else { | ||
500 | ret = 1; | ||
501 | } | ||
502 | close(state->d_fd); | ||
503 | state->d_fd = -1; | ||
504 | |||
505 | return (ret); | ||
506 | } | ||
507 | |||
508 | /* | ||
509 | * libcrypto EVP stuff - this is how we get wired to EVP so the engine | ||
510 | * gets called when libcrypto requests a cipher NID. | ||
511 | */ | ||
512 | |||
513 | /* DES CBC EVP */ | ||
514 | const EVP_CIPHER cryptodev_des_cbc = { | ||
515 | NID_des_cbc, | ||
516 | 8, 8, 8, | ||
517 | EVP_CIPH_CBC_MODE, | ||
518 | cryptodev_init_key, | ||
519 | cryptodev_cipher, | ||
520 | cryptodev_cleanup, | ||
521 | sizeof(struct dev_crypto_state), | ||
522 | EVP_CIPHER_set_asn1_iv, | ||
523 | EVP_CIPHER_get_asn1_iv, | ||
524 | NULL | ||
525 | }; | ||
526 | |||
527 | /* 3DES CBC EVP */ | ||
528 | const EVP_CIPHER cryptodev_3des_cbc = { | ||
529 | NID_des_ede3_cbc, | ||
530 | 8, 24, 8, | ||
531 | EVP_CIPH_CBC_MODE, | ||
532 | cryptodev_init_key, | ||
533 | cryptodev_cipher, | ||
534 | cryptodev_cleanup, | ||
535 | sizeof(struct dev_crypto_state), | ||
536 | EVP_CIPHER_set_asn1_iv, | ||
537 | EVP_CIPHER_get_asn1_iv, | ||
538 | NULL | ||
539 | }; | ||
540 | |||
541 | const EVP_CIPHER cryptodev_bf_cbc = { | ||
542 | NID_bf_cbc, | ||
543 | 8, 16, 8, | ||
544 | EVP_CIPH_CBC_MODE, | ||
545 | cryptodev_init_key, | ||
546 | cryptodev_cipher, | ||
547 | cryptodev_cleanup, | ||
548 | sizeof(struct dev_crypto_state), | ||
549 | EVP_CIPHER_set_asn1_iv, | ||
550 | EVP_CIPHER_get_asn1_iv, | ||
551 | NULL | ||
552 | }; | ||
553 | |||
554 | const EVP_CIPHER cryptodev_cast_cbc = { | ||
555 | NID_cast5_cbc, | ||
556 | 8, 16, 8, | ||
557 | EVP_CIPH_CBC_MODE, | ||
558 | cryptodev_init_key, | ||
559 | cryptodev_cipher, | ||
560 | cryptodev_cleanup, | ||
561 | sizeof(struct dev_crypto_state), | ||
562 | EVP_CIPHER_set_asn1_iv, | ||
563 | EVP_CIPHER_get_asn1_iv, | ||
564 | NULL | ||
565 | }; | ||
566 | |||
567 | EVP_CIPHER cryptodev_aes_128_cbc = { | ||
568 | NID_aes_128_cbc, | ||
569 | 16, 16, 16, | ||
570 | EVP_CIPH_CBC_MODE, | ||
571 | cryptodev_init_key, | ||
572 | cryptodev_cipher, | ||
573 | cryptodev_cleanup, | ||
574 | sizeof(struct dev_crypto_state), | ||
575 | EVP_CIPHER_set_asn1_iv, | ||
576 | EVP_CIPHER_get_asn1_iv, | ||
577 | NULL | ||
578 | }; | ||
579 | |||
580 | EVP_CIPHER cryptodev_aes_192_cbc = { | ||
581 | NID_aes_192_cbc, | ||
582 | 16, 24, 16, | ||
583 | EVP_CIPH_CBC_MODE, | ||
584 | cryptodev_init_key, | ||
585 | cryptodev_cipher, | ||
586 | cryptodev_cleanup, | ||
587 | sizeof(struct dev_crypto_state), | ||
588 | EVP_CIPHER_set_asn1_iv, | ||
589 | EVP_CIPHER_get_asn1_iv, | ||
590 | NULL | ||
591 | }; | ||
592 | |||
593 | EVP_CIPHER cryptodev_aes_256_cbc = { | ||
594 | NID_aes_256_cbc, | ||
595 | 16, 32, 16, | ||
596 | EVP_CIPH_CBC_MODE, | ||
597 | cryptodev_init_key, | ||
598 | cryptodev_cipher, | ||
599 | cryptodev_cleanup, | ||
600 | sizeof(struct dev_crypto_state), | ||
601 | EVP_CIPHER_set_asn1_iv, | ||
602 | EVP_CIPHER_get_asn1_iv, | ||
603 | NULL | ||
604 | }; | ||
605 | |||
606 | #if defined(__i386__) || defined(__amd64__) | ||
607 | |||
608 | static inline void | ||
609 | viac3_xcrypt_cbc(int *cw, const void *src, void *dst, void *key, int rep, | ||
610 | void *iv) | ||
611 | { | ||
612 | #ifdef notdef | ||
613 | printf("cw %p[%x %x %x %x] src %p dst %p key %p rep %x iv %p\n", | ||
614 | cw, cw[0], cw[1], cw[2], cw[3], | ||
615 | src, dst, key, rep, iv); | ||
616 | #endif | ||
617 | #if defined(__i386__) | ||
618 | |||
619 | /* | ||
620 | * Clear bit 30 of EFLAGS. | ||
621 | */ | ||
622 | __asm __volatile("pushfl; popfl"); | ||
623 | |||
624 | /* | ||
625 | * Cannot simply place key into "b" register, since the compiler | ||
626 | * -pic mode uses that register; so instead we must dance a little. | ||
627 | */ | ||
628 | __asm __volatile("pushl %%ebx; movl %0, %%ebx; rep xcrypt-cbc; popl %%ebx" : | ||
629 | : "m" (key), "a" (iv), "c" (rep), "d" (cw), "S" (src), "D" (dst) | ||
630 | : "memory", "cc"); | ||
631 | #else | ||
632 | |||
633 | /* | ||
634 | * Clear bit 30 of EFLAGS. | ||
635 | */ | ||
636 | __asm __volatile("pushfq; popfq"); | ||
637 | __asm __volatile("rep xcrypt-cbc" : | ||
638 | : "b" (key), "a" (iv), "c" (rep), "d" (cw), "S" (src), "D" (dst) | ||
639 | : "memory", "cc"); | ||
640 | #endif | ||
641 | |||
642 | } | ||
643 | |||
644 | #define ISUNALIGNED(x) ((long)(x)) & 15 | ||
645 | #define DOALIGN(v) ((void *)(((long)(v) + 15) & ~15)) | ||
646 | |||
647 | static int | ||
648 | xcrypt_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | ||
649 | const unsigned char *in, size_t inl) | ||
650 | { | ||
651 | unsigned char *save_iv_store[EVP_MAX_IV_LENGTH + 15]; | ||
652 | unsigned char *save_iv = DOALIGN(save_iv_store); | ||
653 | unsigned char *ivs_store[EVP_MAX_IV_LENGTH + 15]; | ||
654 | unsigned char *ivs = DOALIGN(ivs_store); | ||
655 | void *iiv, *iv = NULL, *ivp = NULL; | ||
656 | const void *usein = in; | ||
657 | void *useout = out, *spare; | ||
658 | int cws[4 + 3], *cw = DOALIGN(cws); | ||
659 | |||
660 | if (!inl) | ||
661 | return (1); | ||
662 | if ((inl % ctx->cipher->block_size) != 0) | ||
663 | return (0); | ||
664 | if (inl > UINT_MAX) | ||
665 | return (0); | ||
666 | |||
667 | if (ISUNALIGNED(in) || ISUNALIGNED(out)) { | ||
668 | spare = malloc(inl); | ||
669 | if (spare == NULL) | ||
670 | return (0); | ||
671 | |||
672 | if (ISUNALIGNED(in)) { | ||
673 | bcopy(in, spare, inl); | ||
674 | usein = spare; | ||
675 | } | ||
676 | if (ISUNALIGNED(out)) | ||
677 | useout = spare; | ||
678 | } | ||
679 | |||
680 | cw[0] = C3_CRYPT_CWLO_ALG_AES | C3_CRYPT_CWLO_KEYGEN_SW | | ||
681 | C3_CRYPT_CWLO_NORMAL; | ||
682 | cw[0] |= ctx->encrypt ? C3_CRYPT_CWLO_ENCRYPT : C3_CRYPT_CWLO_DECRYPT; | ||
683 | cw[1] = cw[2] = cw[3] = 0; | ||
684 | |||
685 | switch (ctx->key_len * 8) { | ||
686 | case 128: | ||
687 | cw[0] |= C3_CRYPT_CWLO_KEY128; | ||
688 | break; | ||
689 | case 192: | ||
690 | cw[0] |= C3_CRYPT_CWLO_KEY192; | ||
691 | break; | ||
692 | case 256: | ||
693 | cw[0] |= C3_CRYPT_CWLO_KEY256; | ||
694 | break; | ||
695 | } | ||
696 | |||
697 | if (ctx->cipher->iv_len) { | ||
698 | iv = (caddr_t) ctx->iv; | ||
699 | if (!ctx->encrypt) { | ||
700 | iiv = (void *) in + inl - ctx->cipher->iv_len; | ||
701 | memcpy(save_iv, iiv, ctx->cipher->iv_len); | ||
702 | } | ||
703 | } | ||
704 | |||
705 | ivp = iv; | ||
706 | if (ISUNALIGNED(iv)) { | ||
707 | bcopy(iv, ivs, ctx->cipher->iv_len); | ||
708 | ivp = ivs; | ||
709 | } | ||
710 | |||
711 | viac3_xcrypt_cbc(cw, usein, useout, ctx->cipher_data, inl / 16, ivp); | ||
712 | |||
713 | if (ISUNALIGNED(in) || ISUNALIGNED(out)) { | ||
714 | if (ISUNALIGNED(out)) | ||
715 | bcopy(spare, out, inl); | ||
716 | free(spare); | ||
717 | } | ||
718 | |||
719 | if (ivp == ivs) | ||
720 | bcopy(ivp, iv, ctx->cipher->iv_len); | ||
721 | |||
722 | if (ctx->cipher->iv_len) { | ||
723 | if (ctx->encrypt) | ||
724 | iiv = (void *) out + inl - ctx->cipher->iv_len; | ||
725 | else | ||
726 | iiv = save_iv; | ||
727 | memcpy(ctx->iv, iiv, ctx->cipher->iv_len); | ||
728 | } | ||
729 | return (1); | ||
730 | } | ||
731 | |||
732 | static int | ||
733 | xcrypt_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | ||
734 | const unsigned char *iv, int enc) | ||
735 | { | ||
736 | AES_KEY *k = ctx->cipher_data; | ||
737 | #ifndef AES_ASM | ||
738 | int i; | ||
739 | #endif | ||
740 | |||
741 | bzero(k, sizeof *k); | ||
742 | if (enc) | ||
743 | AES_set_encrypt_key(key, ctx->key_len * 8, k); | ||
744 | else | ||
745 | AES_set_decrypt_key(key, ctx->key_len * 8, k); | ||
746 | |||
747 | #ifndef AES_ASM | ||
748 | /* | ||
749 | * XXX Damn OpenSSL byte swaps the expanded key!! | ||
750 | * | ||
751 | * XXX But only if we're using the C implementation of AES | ||
752 | */ | ||
753 | for (i = 0; i < 4 * (AES_MAXNR + 1); i++) | ||
754 | k->rd_key[i] = htonl(k->rd_key[i]); | ||
755 | #endif | ||
756 | |||
757 | return (1); | ||
758 | } | ||
759 | |||
760 | static int | ||
761 | xcrypt_cleanup(EVP_CIPHER_CTX *ctx) | ||
762 | { | ||
763 | bzero(ctx->cipher_data, ctx->cipher->ctx_size); | ||
764 | return (1); | ||
765 | } | ||
766 | |||
767 | static int | ||
768 | check_viac3aes(void) | ||
769 | { | ||
770 | int mib[2] = { CTL_MACHDEP, CPU_XCRYPT }, value; | ||
771 | size_t size = sizeof(value); | ||
772 | |||
773 | if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &value, &size, | ||
774 | NULL, 0) < 0) | ||
775 | return (0); | ||
776 | if (value == 0) | ||
777 | return (0); | ||
778 | |||
779 | if (value & C3_HAS_AES) { | ||
780 | cryptodev_aes_128_cbc.init = xcrypt_init_key; | ||
781 | cryptodev_aes_128_cbc.do_cipher = xcrypt_cipher; | ||
782 | cryptodev_aes_128_cbc.cleanup = xcrypt_cleanup; | ||
783 | cryptodev_aes_128_cbc.ctx_size = sizeof(AES_KEY); | ||
784 | |||
785 | cryptodev_aes_192_cbc.init = xcrypt_init_key; | ||
786 | cryptodev_aes_192_cbc.do_cipher = xcrypt_cipher; | ||
787 | cryptodev_aes_192_cbc.cleanup = xcrypt_cleanup; | ||
788 | cryptodev_aes_192_cbc.ctx_size = sizeof(AES_KEY); | ||
789 | |||
790 | cryptodev_aes_256_cbc.init = xcrypt_init_key; | ||
791 | cryptodev_aes_256_cbc.do_cipher = xcrypt_cipher; | ||
792 | cryptodev_aes_256_cbc.cleanup = xcrypt_cleanup; | ||
793 | cryptodev_aes_256_cbc.ctx_size = sizeof(AES_KEY); | ||
794 | } | ||
795 | return (value); | ||
796 | } | ||
797 | #endif /* __i386__ || __amd64__ */ | ||
798 | |||
799 | /* | ||
800 | * Registered by the ENGINE when used to find out how to deal with | ||
801 | * a particular NID in the ENGINE. this says what we'll do at the | ||
802 | * top level - note, that list is restricted by what we answer with | ||
803 | */ | ||
804 | static int | ||
805 | cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher, | ||
806 | const int **nids, int nid) | ||
807 | { | ||
808 | if (!cipher) | ||
809 | return (cryptodev_usable_ciphers(nids)); | ||
810 | |||
811 | switch (nid) { | ||
812 | case NID_des_ede3_cbc: | ||
813 | *cipher = &cryptodev_3des_cbc; | ||
814 | break; | ||
815 | case NID_des_cbc: | ||
816 | *cipher = &cryptodev_des_cbc; | ||
817 | break; | ||
818 | case NID_bf_cbc: | ||
819 | *cipher = &cryptodev_bf_cbc; | ||
820 | break; | ||
821 | case NID_cast5_cbc: | ||
822 | *cipher = &cryptodev_cast_cbc; | ||
823 | break; | ||
824 | case NID_aes_128_cbc: | ||
825 | *cipher = &cryptodev_aes_128_cbc; | ||
826 | break; | ||
827 | case NID_aes_192_cbc: | ||
828 | *cipher = &cryptodev_aes_192_cbc; | ||
829 | break; | ||
830 | case NID_aes_256_cbc: | ||
831 | *cipher = &cryptodev_aes_256_cbc; | ||
832 | break; | ||
833 | default: | ||
834 | *cipher = NULL; | ||
835 | break; | ||
836 | } | ||
837 | return (*cipher != NULL); | ||
838 | } | ||
839 | |||
840 | static int | ||
841 | cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest, | ||
842 | const int **nids, int nid) | ||
843 | { | ||
844 | if (!digest) | ||
845 | return (cryptodev_usable_digests(nids)); | ||
846 | |||
847 | switch (nid) { | ||
848 | case NID_md5: | ||
849 | *digest = NULL; /* need to make a clean md5 critter */ | ||
850 | break; | ||
851 | default: | ||
852 | *digest = NULL; | ||
853 | break; | ||
854 | } | ||
855 | return (*digest != NULL); | ||
856 | } | ||
857 | |||
858 | /* | ||
859 | * Convert a BIGNUM to the representation that /dev/crypto needs. | ||
860 | * Upon completion of use, the caller is responsible for freeing | ||
861 | * crp->crp_p. | ||
862 | */ | ||
863 | static int | ||
864 | bn2crparam(const BIGNUM *a, struct crparam *crp) | ||
865 | { | ||
866 | int i, j, k; | ||
867 | ssize_t bytes, bits; | ||
868 | u_char *b; | ||
869 | |||
870 | crp->crp_p = NULL; | ||
871 | crp->crp_nbits = 0; | ||
872 | |||
873 | bits = BN_num_bits(a); | ||
874 | bytes = (bits + 7) / 8; | ||
875 | |||
876 | b = malloc(bytes); | ||
877 | if (b == NULL) | ||
878 | return (1); | ||
879 | |||
880 | crp->crp_p = b; | ||
881 | crp->crp_nbits = bits; | ||
882 | |||
883 | for (i = 0, j = 0; i < a->top; i++) { | ||
884 | for (k = 0; k < BN_BITS2 / 8; k++) { | ||
885 | if ((j + k) >= bytes) | ||
886 | return (0); | ||
887 | b[j + k] = a->d[i] >> (k * 8); | ||
888 | } | ||
889 | j += BN_BITS2 / 8; | ||
890 | } | ||
891 | return (0); | ||
892 | } | ||
893 | |||
894 | /* Convert a /dev/crypto parameter to a BIGNUM */ | ||
895 | static int | ||
896 | crparam2bn(struct crparam *crp, BIGNUM *a) | ||
897 | { | ||
898 | u_int8_t *pd; | ||
899 | int i, bytes; | ||
900 | |||
901 | bytes = (crp->crp_nbits + 7) / 8; | ||
902 | |||
903 | if (bytes == 0) | ||
904 | return (-1); | ||
905 | |||
906 | if ((pd = (u_int8_t *) malloc(bytes)) == NULL) | ||
907 | return (-1); | ||
908 | |||
909 | for (i = 0; i < bytes; i++) | ||
910 | pd[i] = crp->crp_p[bytes - i - 1]; | ||
911 | |||
912 | BN_bin2bn(pd, bytes, a); | ||
913 | free(pd); | ||
914 | |||
915 | return (0); | ||
916 | } | ||
917 | |||
918 | static void | ||
919 | zapparams(struct crypt_kop *kop) | ||
920 | { | ||
921 | int i; | ||
922 | |||
923 | for (i = 0; i <= kop->crk_iparams + kop->crk_oparams; i++) { | ||
924 | if (kop->crk_param[i].crp_p) | ||
925 | free(kop->crk_param[i].crp_p); | ||
926 | kop->crk_param[i].crp_p = NULL; | ||
927 | kop->crk_param[i].crp_nbits = 0; | ||
928 | } | ||
929 | } | ||
930 | |||
931 | static int | ||
932 | cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s) | ||
933 | { | ||
934 | int fd, ret = -1; | ||
935 | |||
936 | if ((fd = get_asym_dev_crypto()) < 0) | ||
937 | return (ret); | ||
938 | |||
939 | if (r) { | ||
940 | kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char)); | ||
941 | kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8; | ||
942 | kop->crk_oparams++; | ||
943 | } | ||
944 | if (s) { | ||
945 | kop->crk_param[kop->crk_iparams+1].crp_p = calloc(slen, sizeof(char)); | ||
946 | kop->crk_param[kop->crk_iparams+1].crp_nbits = slen * 8; | ||
947 | kop->crk_oparams++; | ||
948 | } | ||
949 | |||
950 | if (ioctl(fd, CIOCKEY, kop) == 0) { | ||
951 | if (r) | ||
952 | crparam2bn(&kop->crk_param[kop->crk_iparams], r); | ||
953 | if (s) | ||
954 | crparam2bn(&kop->crk_param[kop->crk_iparams+1], s); | ||
955 | ret = 0; | ||
956 | } | ||
957 | |||
958 | return (ret); | ||
959 | } | ||
960 | |||
961 | static int | ||
962 | cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
963 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont) | ||
964 | { | ||
965 | struct crypt_kop kop; | ||
966 | int ret = 1; | ||
967 | |||
968 | /* Currently, we know we can do mod exp iff we can do any | ||
969 | * asymmetric operations at all. | ||
970 | */ | ||
971 | if (cryptodev_asymfeat == 0) { | ||
972 | ret = BN_mod_exp(r, a, p, m, ctx); | ||
973 | return (ret); | ||
974 | } | ||
975 | |||
976 | memset(&kop, 0, sizeof kop); | ||
977 | kop.crk_op = CRK_MOD_EXP; | ||
978 | |||
979 | /* inputs: a^p % m */ | ||
980 | if (bn2crparam(a, &kop.crk_param[0])) | ||
981 | goto err; | ||
982 | if (bn2crparam(p, &kop.crk_param[1])) | ||
983 | goto err; | ||
984 | if (bn2crparam(m, &kop.crk_param[2])) | ||
985 | goto err; | ||
986 | kop.crk_iparams = 3; | ||
987 | |||
988 | if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL) == -1) { | ||
989 | const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); | ||
990 | ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont); | ||
991 | } | ||
992 | err: | ||
993 | zapparams(&kop); | ||
994 | return (ret); | ||
995 | } | ||
996 | |||
997 | static int | ||
998 | cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, | ||
999 | BN_CTX *ctx) | ||
1000 | { | ||
1001 | return (RSA_PKCS1_SSLeay()->rsa_mod_exp)(r0, I, rsa, ctx); | ||
1002 | } | ||
1003 | |||
1004 | static int | ||
1005 | cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) | ||
1006 | { | ||
1007 | struct crypt_kop kop; | ||
1008 | int ret = 1; | ||
1009 | |||
1010 | if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) { | ||
1011 | /* XXX 0 means failure?? */ | ||
1012 | return (0); | ||
1013 | } | ||
1014 | |||
1015 | memset(&kop, 0, sizeof kop); | ||
1016 | kop.crk_op = CRK_MOD_EXP_CRT; | ||
1017 | /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */ | ||
1018 | if (bn2crparam(rsa->p, &kop.crk_param[0])) | ||
1019 | goto err; | ||
1020 | if (bn2crparam(rsa->q, &kop.crk_param[1])) | ||
1021 | goto err; | ||
1022 | if (bn2crparam(I, &kop.crk_param[2])) | ||
1023 | goto err; | ||
1024 | if (bn2crparam(rsa->dmp1, &kop.crk_param[3])) | ||
1025 | goto err; | ||
1026 | if (bn2crparam(rsa->dmq1, &kop.crk_param[4])) | ||
1027 | goto err; | ||
1028 | if (bn2crparam(rsa->iqmp, &kop.crk_param[5])) | ||
1029 | goto err; | ||
1030 | kop.crk_iparams = 6; | ||
1031 | |||
1032 | if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL) == -1) { | ||
1033 | const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); | ||
1034 | ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx); | ||
1035 | } | ||
1036 | err: | ||
1037 | zapparams(&kop); | ||
1038 | return (ret); | ||
1039 | } | ||
1040 | |||
1041 | static RSA_METHOD cryptodev_rsa = { | ||
1042 | "cryptodev RSA method", | ||
1043 | NULL, /* rsa_pub_enc */ | ||
1044 | NULL, /* rsa_pub_dec */ | ||
1045 | NULL, /* rsa_priv_enc */ | ||
1046 | NULL, /* rsa_priv_dec */ | ||
1047 | NULL, | ||
1048 | NULL, | ||
1049 | NULL, /* init */ | ||
1050 | NULL, /* finish */ | ||
1051 | 0, /* flags */ | ||
1052 | NULL, /* app_data */ | ||
1053 | NULL, /* rsa_sign */ | ||
1054 | NULL /* rsa_verify */ | ||
1055 | }; | ||
1056 | |||
1057 | static int | ||
1058 | cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p, | ||
1059 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) | ||
1060 | { | ||
1061 | return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx)); | ||
1062 | } | ||
1063 | |||
1064 | static int | ||
1065 | cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g, | ||
1066 | BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p, | ||
1067 | BN_CTX *ctx, BN_MONT_CTX *mont) | ||
1068 | { | ||
1069 | BIGNUM t2; | ||
1070 | int ret = 0; | ||
1071 | |||
1072 | BN_init(&t2); | ||
1073 | |||
1074 | /* v = ( g^u1 * y^u2 mod p ) mod q */ | ||
1075 | /* let t1 = g ^ u1 mod p */ | ||
1076 | ret = 0; | ||
1077 | |||
1078 | if (!dsa->meth->bn_mod_exp(dsa,t1,dsa->g,u1,dsa->p,ctx,mont)) | ||
1079 | goto err; | ||
1080 | |||
1081 | /* let t2 = y ^ u2 mod p */ | ||
1082 | if (!dsa->meth->bn_mod_exp(dsa,&t2,dsa->pub_key,u2,dsa->p,ctx,mont)) | ||
1083 | goto err; | ||
1084 | /* let u1 = t1 * t2 mod p */ | ||
1085 | if (!BN_mod_mul(u1,t1,&t2,dsa->p,ctx)) | ||
1086 | goto err; | ||
1087 | |||
1088 | BN_copy(t1,u1); | ||
1089 | |||
1090 | ret = 1; | ||
1091 | err: | ||
1092 | BN_free(&t2); | ||
1093 | return(ret); | ||
1094 | } | ||
1095 | |||
1096 | static DSA_SIG * | ||
1097 | cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) | ||
1098 | { | ||
1099 | struct crypt_kop kop; | ||
1100 | BIGNUM *r = NULL, *s = NULL; | ||
1101 | DSA_SIG *dsaret = NULL; | ||
1102 | |||
1103 | if ((r = BN_new()) == NULL) | ||
1104 | goto err; | ||
1105 | if ((s = BN_new()) == NULL) { | ||
1106 | BN_free(r); | ||
1107 | goto err; | ||
1108 | } | ||
1109 | |||
1110 | memset(&kop, 0, sizeof kop); | ||
1111 | kop.crk_op = CRK_DSA_SIGN; | ||
1112 | |||
1113 | /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */ | ||
1114 | kop.crk_param[0].crp_p = (caddr_t)dgst; | ||
1115 | kop.crk_param[0].crp_nbits = dlen * 8; | ||
1116 | if (bn2crparam(dsa->p, &kop.crk_param[1])) | ||
1117 | goto err; | ||
1118 | if (bn2crparam(dsa->q, &kop.crk_param[2])) | ||
1119 | goto err; | ||
1120 | if (bn2crparam(dsa->g, &kop.crk_param[3])) | ||
1121 | goto err; | ||
1122 | if (bn2crparam(dsa->priv_key, &kop.crk_param[4])) | ||
1123 | goto err; | ||
1124 | kop.crk_iparams = 5; | ||
1125 | |||
1126 | if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r, | ||
1127 | BN_num_bytes(dsa->q), s) == 0) { | ||
1128 | dsaret = DSA_SIG_new(); | ||
1129 | dsaret->r = r; | ||
1130 | dsaret->s = s; | ||
1131 | } else { | ||
1132 | const DSA_METHOD *meth = DSA_OpenSSL(); | ||
1133 | BN_free(r); | ||
1134 | BN_free(s); | ||
1135 | dsaret = (meth->dsa_do_sign)(dgst, dlen, dsa); | ||
1136 | } | ||
1137 | err: | ||
1138 | kop.crk_param[0].crp_p = NULL; | ||
1139 | zapparams(&kop); | ||
1140 | return (dsaret); | ||
1141 | } | ||
1142 | |||
1143 | static int | ||
1144 | cryptodev_dsa_verify(const unsigned char *dgst, int dlen, | ||
1145 | DSA_SIG *sig, DSA *dsa) | ||
1146 | { | ||
1147 | struct crypt_kop kop; | ||
1148 | int dsaret = 1; | ||
1149 | |||
1150 | memset(&kop, 0, sizeof kop); | ||
1151 | kop.crk_op = CRK_DSA_VERIFY; | ||
1152 | |||
1153 | /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */ | ||
1154 | kop.crk_param[0].crp_p = (caddr_t)dgst; | ||
1155 | kop.crk_param[0].crp_nbits = dlen * 8; | ||
1156 | if (bn2crparam(dsa->p, &kop.crk_param[1])) | ||
1157 | goto err; | ||
1158 | if (bn2crparam(dsa->q, &kop.crk_param[2])) | ||
1159 | goto err; | ||
1160 | if (bn2crparam(dsa->g, &kop.crk_param[3])) | ||
1161 | goto err; | ||
1162 | if (bn2crparam(dsa->pub_key, &kop.crk_param[4])) | ||
1163 | goto err; | ||
1164 | if (bn2crparam(sig->r, &kop.crk_param[5])) | ||
1165 | goto err; | ||
1166 | if (bn2crparam(sig->s, &kop.crk_param[6])) | ||
1167 | goto err; | ||
1168 | kop.crk_iparams = 7; | ||
1169 | |||
1170 | if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) { | ||
1171 | dsaret = kop.crk_status; | ||
1172 | } else { | ||
1173 | const DSA_METHOD *meth = DSA_OpenSSL(); | ||
1174 | |||
1175 | dsaret = (meth->dsa_do_verify)(dgst, dlen, sig, dsa); | ||
1176 | } | ||
1177 | err: | ||
1178 | kop.crk_param[0].crp_p = NULL; | ||
1179 | zapparams(&kop); | ||
1180 | return (dsaret); | ||
1181 | } | ||
1182 | |||
1183 | static DSA_METHOD cryptodev_dsa = { | ||
1184 | "cryptodev DSA method", | ||
1185 | NULL, | ||
1186 | NULL, /* dsa_sign_setup */ | ||
1187 | NULL, | ||
1188 | NULL, /* dsa_mod_exp */ | ||
1189 | NULL, | ||
1190 | NULL, /* init */ | ||
1191 | NULL, /* finish */ | ||
1192 | 0, /* flags */ | ||
1193 | NULL /* app_data */ | ||
1194 | }; | ||
1195 | |||
1196 | static int | ||
1197 | cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a, | ||
1198 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, | ||
1199 | BN_MONT_CTX *m_ctx) | ||
1200 | { | ||
1201 | return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx)); | ||
1202 | } | ||
1203 | |||
1204 | static int | ||
1205 | cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) | ||
1206 | { | ||
1207 | struct crypt_kop kop; | ||
1208 | int dhret = 1; | ||
1209 | int fd, keylen; | ||
1210 | |||
1211 | if ((fd = get_asym_dev_crypto()) < 0) { | ||
1212 | const DH_METHOD *meth = DH_OpenSSL(); | ||
1213 | |||
1214 | return ((meth->compute_key)(key, pub_key, dh)); | ||
1215 | } | ||
1216 | |||
1217 | keylen = BN_num_bits(dh->p); | ||
1218 | |||
1219 | memset(&kop, 0, sizeof kop); | ||
1220 | kop.crk_op = CRK_DH_COMPUTE_KEY; | ||
1221 | |||
1222 | /* inputs: dh->priv_key pub_key dh->p key */ | ||
1223 | if (bn2crparam(dh->priv_key, &kop.crk_param[0])) | ||
1224 | goto err; | ||
1225 | if (bn2crparam(pub_key, &kop.crk_param[1])) | ||
1226 | goto err; | ||
1227 | if (bn2crparam(dh->p, &kop.crk_param[2])) | ||
1228 | goto err; | ||
1229 | kop.crk_iparams = 3; | ||
1230 | |||
1231 | kop.crk_param[3].crp_p = key; | ||
1232 | kop.crk_param[3].crp_nbits = keylen * 8; | ||
1233 | kop.crk_oparams = 1; | ||
1234 | |||
1235 | if (ioctl(fd, CIOCKEY, &kop) == -1) { | ||
1236 | const DH_METHOD *meth = DH_OpenSSL(); | ||
1237 | |||
1238 | dhret = (meth->compute_key)(key, pub_key, dh); | ||
1239 | } | ||
1240 | err: | ||
1241 | kop.crk_param[3].crp_p = NULL; | ||
1242 | zapparams(&kop); | ||
1243 | return (dhret); | ||
1244 | } | ||
1245 | |||
1246 | static DH_METHOD cryptodev_dh = { | ||
1247 | "cryptodev DH method", | ||
1248 | NULL, /* cryptodev_dh_generate_key */ | ||
1249 | NULL, | ||
1250 | NULL, | ||
1251 | NULL, | ||
1252 | NULL, | ||
1253 | 0, /* flags */ | ||
1254 | NULL /* app_data */ | ||
1255 | }; | ||
1256 | |||
1257 | /* | ||
1258 | * ctrl right now is just a wrapper that doesn't do much | ||
1259 | * but I expect we'll want some options soon. | ||
1260 | */ | ||
1261 | static int | ||
1262 | cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)()) | ||
1263 | { | ||
1264 | #ifdef HAVE_SYSLOG_R | ||
1265 | struct syslog_data sd = SYSLOG_DATA_INIT; | ||
1266 | #endif | ||
1267 | |||
1268 | switch (cmd) { | ||
1269 | default: | ||
1270 | #ifdef HAVE_SYSLOG_R | ||
1271 | syslog_r(LOG_ERR, &sd, | ||
1272 | "cryptodev_ctrl: unknown command %d", cmd); | ||
1273 | #else | ||
1274 | syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd); | ||
1275 | #endif | ||
1276 | break; | ||
1277 | } | ||
1278 | return (1); | ||
1279 | } | ||
1280 | |||
1281 | void | ||
1282 | ENGINE_load_cryptodev(void) | ||
1283 | { | ||
1284 | ENGINE *engine = ENGINE_new(); | ||
1285 | int fd; | ||
1286 | |||
1287 | if (engine == NULL) | ||
1288 | return; | ||
1289 | if ((fd = get_dev_crypto()) < 0) { | ||
1290 | ENGINE_free(engine); | ||
1291 | return; | ||
1292 | } | ||
1293 | |||
1294 | /* | ||
1295 | * find out what asymmetric crypto algorithms we support | ||
1296 | */ | ||
1297 | if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) { | ||
1298 | close(fd); | ||
1299 | ENGINE_free(engine); | ||
1300 | return; | ||
1301 | } | ||
1302 | close(fd); | ||
1303 | |||
1304 | if (!ENGINE_set_id(engine, "cryptodev") || | ||
1305 | !ENGINE_set_name(engine, "BSD cryptodev engine") || | ||
1306 | !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) || | ||
1307 | !ENGINE_set_digests(engine, cryptodev_engine_digests) || | ||
1308 | !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) || | ||
1309 | !ENGINE_set_cmd_defns(engine, cryptodev_defns)) { | ||
1310 | ENGINE_free(engine); | ||
1311 | return; | ||
1312 | } | ||
1313 | |||
1314 | if (ENGINE_set_RSA(engine, &cryptodev_rsa)) { | ||
1315 | const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay(); | ||
1316 | |||
1317 | cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp; | ||
1318 | cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp; | ||
1319 | cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc; | ||
1320 | cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec; | ||
1321 | cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc; | ||
1322 | cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec; | ||
1323 | if (cryptodev_asymfeat & CRF_MOD_EXP) { | ||
1324 | cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp; | ||
1325 | if (cryptodev_asymfeat & CRF_MOD_EXP_CRT) | ||
1326 | cryptodev_rsa.rsa_mod_exp = | ||
1327 | cryptodev_rsa_mod_exp; | ||
1328 | else | ||
1329 | cryptodev_rsa.rsa_mod_exp = | ||
1330 | cryptodev_rsa_nocrt_mod_exp; | ||
1331 | } | ||
1332 | } | ||
1333 | |||
1334 | if (ENGINE_set_DSA(engine, &cryptodev_dsa)) { | ||
1335 | const DSA_METHOD *meth = DSA_OpenSSL(); | ||
1336 | |||
1337 | memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD)); | ||
1338 | if (cryptodev_asymfeat & CRF_DSA_SIGN) | ||
1339 | cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign; | ||
1340 | if (cryptodev_asymfeat & CRF_MOD_EXP) { | ||
1341 | cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp; | ||
1342 | cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp; | ||
1343 | } | ||
1344 | if (cryptodev_asymfeat & CRF_DSA_VERIFY) | ||
1345 | cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify; | ||
1346 | } | ||
1347 | |||
1348 | if (ENGINE_set_DH(engine, &cryptodev_dh)){ | ||
1349 | const DH_METHOD *dh_meth = DH_OpenSSL(); | ||
1350 | |||
1351 | cryptodev_dh.generate_key = dh_meth->generate_key; | ||
1352 | cryptodev_dh.compute_key = dh_meth->compute_key; | ||
1353 | cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp; | ||
1354 | if (cryptodev_asymfeat & CRF_MOD_EXP) { | ||
1355 | cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh; | ||
1356 | if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY) | ||
1357 | cryptodev_dh.compute_key = | ||
1358 | cryptodev_dh_compute_key; | ||
1359 | } | ||
1360 | } | ||
1361 | |||
1362 | ENGINE_add(engine); | ||
1363 | ENGINE_free(engine); | ||
1364 | ERR_clear_error(); | ||
1365 | } | ||
1366 | |||
1367 | #endif /* HAVE_CRYPTODEV */ | ||