1 /* $FreeBSD: src/tools/tools/crypto/cryptokeytest.c,v 1.1.2.1 2003/02/24 22:52:36 sam Exp $ */
3 * The big num stuff is a bit broken at the moment and I've not yet fixed it.
4 * The symtom is that odd size big nums will fail. Test code below (it only
5 * uses modexp currently).
10 #include <sys/ioctl.h>
11 #include <machine/endian.h>
13 #include <crypto/cryptodev.h>
14 #include <openssl/bn.h>
21 static int crypto_fd = -1;
24 * Convert a little endian byte string in 'p' that
25 * is 'plen' bytes long to a BIGNUM. If 'dst' is NULL,
26 * a new BIGNUM is allocated. Returns NULL on failure.
28 * XXX there has got to be a more efficient way to do
29 * this, but I haven't figured out enough of the OpenSSL
33 le_to_bignum(BIGNUM *dst, u_int8_t *p, int plen)
41 if ((pd = (u_int8_t *)malloc(plen)) == NULL)
44 for (i = 0; i < plen; i++)
45 pd[i] = p[plen - i - 1];
47 dst = BN_bin2bn(pd, plen, dst);
53 * Convert a BIGNUM to a little endian byte string.
54 * If 'rd' is NULL, allocate space for it, otherwise
55 * 'rd' is assumed to have room for BN_num_bytes(n)
56 * bytes. Returns NULL on failure.
59 bignum_to_le(BIGNUM *n, u_int8_t *rd)
62 int blen = BN_num_bytes(n);
67 rd = (u_int8_t *)malloc(blen);
71 for (i = 0, j = 0; i < n->top; i++) {
72 for (k = 0; k < BN_BITS2 / 8; k++) {
75 rd[j + k] = n->d[i] >> (k * 8);
84 UB_mod_exp(BIGNUM *res, BIGNUM *a, BIGNUM *b, BIGNUM *c, BN_CTX *ctx)
87 u_int8_t *ale, *ble, *cle;
89 if (crypto_fd == -1) {
90 int fd, fdc = open("/dev/crypto", O_RDONLY);
93 err(1, "/dev/crypto");
94 if (ioctl(fdc, CRIOGET, &fd) == -1)
100 if ((ale = bignum_to_le(a, NULL)) == NULL)
101 err(1, "bignum_to_le, a");
102 if ((ble = bignum_to_le(b, NULL)) == NULL)
103 err(1, "bignum_to_le, b");
104 if ((cle = bignum_to_le(c, NULL)) == NULL)
105 err(1, "bignum_to_le, c");
107 bzero(&kop, sizeof(kop));
108 kop.crk_op = CRK_MOD_EXP;
111 kop.crk_param[0].crp_p = ale;
112 kop.crk_param[0].crp_nbits = BN_num_bytes(a) * 8;
113 kop.crk_param[1].crp_p = ble;
114 kop.crk_param[1].crp_nbits = BN_num_bytes(b) * 8;
115 kop.crk_param[2].crp_p = cle;
116 kop.crk_param[2].crp_nbits = BN_num_bytes(c) * 8;
117 kop.crk_param[3].crp_p = cle;
118 kop.crk_param[3].crp_nbits = BN_num_bytes(c) * 8;
120 if (ioctl(crypto_fd, CIOCKEY, &kop) == -1)
123 bzero(ale, BN_num_bytes(a));
125 bzero(ble, BN_num_bytes(b));
128 if (kop.crk_status != 0) {
129 printf("error %d\n", kop.crk_status);
130 bzero(cle, BN_num_bytes(c));
134 res = le_to_bignum(res, cle, BN_num_bytes(c));
135 bzero(cle, BN_num_bytes(c));
138 err(1, "le_to_bignum");
145 show_result(a, b, c, sw, hw)
146 BIGNUM *a, *b, *c, *sw, *hw;
151 BN_print_fp(stdout, a);
155 BN_print_fp(stdout, b);
159 BN_print_fp(stdout, c);
163 BN_print_fp(stdout, sw);
167 BN_print_fp(stdout, hw);
176 BIGNUM *a, *b, *c, *r1, *r2;
187 BN_pseudo_rand(a, 1023, 0, 0);
188 BN_pseudo_rand(b, 1023, 0, 0);
189 BN_pseudo_rand(c, 1024, 0, 0);
191 if (BN_cmp(a, c) > 0) {
192 BIGNUM *rem = BN_new();
194 BN_mod(rem, a, c, ctx);
195 UB_mod_exp(r2, rem, b, c, ctx);
198 UB_mod_exp(r2, a, b, c, ctx);
200 BN_mod_exp(r1, a, b, c, ctx);
202 if (BN_cmp(r1, r2) != 0) {
203 show_result(a, b, c, r1, r2);
219 for (i = 0; i < 1000; i++) {
220 fprintf(stderr, "test %d\n", i);