1 /* $FreeBSD: src/tools/tools/crypto/cryptokeytest.c,v 1.1.2.1 2003/02/24 22:52:36 sam Exp $ */
2 /* $DragonFly: src/tools/tools/crypto/cryptokeytest.c,v 1.2 2003/06/17 04:29:11 dillon Exp $ */
4 * The big num stuff is a bit broken at the moment and I've not yet fixed it.
5 * The symtom is that odd size big nums will fail. Test code below (it only
6 * uses modexp currently).
10 #include <sys/types.h>
11 #include <sys/ioctl.h>
12 #include <machine/endian.h>
14 #include <crypto/cryptodev.h>
15 #include <openssl/bn.h>
22 static int crypto_fd = -1;
25 * Convert a little endian byte string in 'p' that
26 * is 'plen' bytes long to a BIGNUM. If 'dst' is NULL,
27 * a new BIGNUM is allocated. Returns NULL on failure.
29 * XXX there has got to be a more efficient way to do
30 * this, but I haven't figured out enough of the OpenSSL
34 le_to_bignum(BIGNUM *dst, u_int8_t *p, int plen)
42 if ((pd = (u_int8_t *)malloc(plen)) == NULL)
45 for (i = 0; i < plen; i++)
46 pd[i] = p[plen - i - 1];
48 dst = BN_bin2bn(pd, plen, dst);
54 * Convert a BIGNUM to a little endian byte string.
55 * If 'rd' is NULL, allocate space for it, otherwise
56 * 'rd' is assumed to have room for BN_num_bytes(n)
57 * bytes. Returns NULL on failure.
60 bignum_to_le(BIGNUM *n, u_int8_t *rd)
63 int blen = BN_num_bytes(n);
68 rd = (u_int8_t *)malloc(blen);
72 for (i = 0, j = 0; i < n->top; i++) {
73 for (k = 0; k < BN_BITS2 / 8; k++) {
76 rd[j + k] = n->d[i] >> (k * 8);
85 UB_mod_exp(BIGNUM *res, BIGNUM *a, BIGNUM *b, BIGNUM *c, BN_CTX *ctx)
88 u_int8_t *ale, *ble, *cle;
90 if (crypto_fd == -1) {
91 int fd, fdc = open("/dev/crypto", O_RDONLY);
94 err(1, "/dev/crypto");
95 if (ioctl(fdc, CRIOGET, &fd) == -1)
101 if ((ale = bignum_to_le(a, NULL)) == NULL)
102 err(1, "bignum_to_le, a");
103 if ((ble = bignum_to_le(b, NULL)) == NULL)
104 err(1, "bignum_to_le, b");
105 if ((cle = bignum_to_le(c, NULL)) == NULL)
106 err(1, "bignum_to_le, c");
108 bzero(&kop, sizeof(kop));
109 kop.crk_op = CRK_MOD_EXP;
112 kop.crk_param[0].crp_p = ale;
113 kop.crk_param[0].crp_nbits = BN_num_bytes(a) * 8;
114 kop.crk_param[1].crp_p = ble;
115 kop.crk_param[1].crp_nbits = BN_num_bytes(b) * 8;
116 kop.crk_param[2].crp_p = cle;
117 kop.crk_param[2].crp_nbits = BN_num_bytes(c) * 8;
118 kop.crk_param[3].crp_p = cle;
119 kop.crk_param[3].crp_nbits = BN_num_bytes(c) * 8;
121 if (ioctl(crypto_fd, CIOCKEY, &kop) == -1)
124 bzero(ale, BN_num_bytes(a));
126 bzero(ble, BN_num_bytes(b));
129 if (kop.crk_status != 0) {
130 printf("error %d\n", kop.crk_status);
131 bzero(cle, BN_num_bytes(c));
135 res = le_to_bignum(res, cle, BN_num_bytes(c));
136 bzero(cle, BN_num_bytes(c));
139 err(1, "le_to_bignum");
146 show_result(a, b, c, sw, hw)
147 BIGNUM *a, *b, *c, *sw, *hw;
152 BN_print_fp(stdout, a);
156 BN_print_fp(stdout, b);
160 BN_print_fp(stdout, c);
164 BN_print_fp(stdout, sw);
168 BN_print_fp(stdout, hw);
177 BIGNUM *a, *b, *c, *r1, *r2;
188 BN_pseudo_rand(a, 1023, 0, 0);
189 BN_pseudo_rand(b, 1023, 0, 0);
190 BN_pseudo_rand(c, 1024, 0, 0);
192 if (BN_cmp(a, c) > 0) {
193 BIGNUM *rem = BN_new();
195 BN_mod(rem, a, c, ctx);
196 UB_mod_exp(r2, rem, b, c, ctx);
199 UB_mod_exp(r2, a, b, c, ctx);
201 BN_mod_exp(r1, a, b, c, ctx);
203 if (BN_cmp(r1, r2) != 0) {
204 show_result(a, b, c, r1, r2);
220 for (i = 0; i < 1000; i++) {
221 fprintf(stderr, "test %d\n", i);