tools/tools/crypto: cryptokeytest: Fix build with newer OpenSSL

Also, drag into this decade.
This commit is contained in:
cem 2019-08-09 02:11:47 +00:00
parent 299463b587
commit 879fe2f638

View File

@ -7,18 +7,20 @@
* --Jason L. Wright * --Jason L. Wright
*/ */
#include <sys/types.h> #include <sys/types.h>
#include <sys/endian.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <machine/endian.h>
#include <sys/time.h> #include <sys/time.h>
#include <crypto/cryptodev.h> #include <crypto/cryptodev.h>
#include <openssl/bn.h>
#include <paths.h>
#include <fcntl.h>
#include <err.h> #include <err.h>
#include <fcntl.h>
#include <paths.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h>
#include <openssl/bn.h>
#include <openssl/err.h>
int crid = CRYPTO_FLAG_HARDWARE; int crid = CRYPTO_FLAG_HARDWARE;
int verbose = 0; int verbose = 0;
@ -63,80 +65,64 @@ crfind(int crid)
} }
/* /*
* Convert a little endian byte string in 'p' that * Convert a little endian byte string in 'p' that is 'plen' bytes long to a
* is 'plen' bytes long to a BIGNUM. If 'dst' is NULL, * BIGNUM. A new BIGNUM is allocated. Returns NULL on failure.
* a new BIGNUM is allocated. Returns NULL on failure.
*
* XXX there has got to be a more efficient way to do
* this, but I haven't figured out enough of the OpenSSL
* magic.
*/ */
BIGNUM * static BIGNUM *
le_to_bignum(BIGNUM *dst, u_int8_t *p, int plen) le_to_bignum(BIGNUM *res, const void *p, int plen)
{ {
u_int8_t *pd;
int i;
if (plen == 0) res = BN_lebin2bn(p, plen, res);
return (NULL); if (res == NULL)
ERR_print_errors_fp(stderr);
if ((pd = (u_int8_t *)malloc(plen)) == NULL) return (res);
return (NULL);
for (i = 0; i < plen; i++)
pd[i] = p[plen - i - 1];
dst = BN_bin2bn(pd, plen, dst);
free(pd);
return (dst);
} }
/* /*
* Convert a BIGNUM to a little endian byte string. * Convert a BIGNUM to a little endian byte string. Space for BN_num_bytes(n)
* If 'rd' is NULL, allocate space for it, otherwise * is allocated.
* 'rd' is assumed to have room for BN_num_bytes(n) * Returns NULL on failure.
* bytes. Returns NULL on failure.
*/ */
u_int8_t * static void *
bignum_to_le(BIGNUM *n, u_int8_t *rd) bignum_to_le(const BIGNUM *n)
{ {
int i, j, k; int blen, error;
int blen = BN_num_bytes(n); void *rd;
blen = BN_num_bytes(n);
if (blen == 0) if (blen == 0)
return (NULL); return (NULL);
if (rd == NULL)
rd = (u_int8_t *)malloc(blen); rd = malloc(blen);
if (rd == NULL) if (rd == NULL)
return (NULL); return (NULL);
for (i = 0, j = 0; i < n->top; i++) { error = BN_bn2lebinpad(n, rd, blen);
for (k = 0; k < BN_BITS2 / 8; k++) { if (error < 0) {
if ((j + k) >= blen) ERR_print_errors_fp(stderr);
goto out; free(rd);
rd[j + k] = n->d[i] >> (k * 8); return (NULL);
}
j += BN_BITS2 / 8;
} }
out:
return (rd); return (rd);
} }
int static int
UB_mod_exp(BIGNUM *res, BIGNUM *a, BIGNUM *b, BIGNUM *c, BN_CTX *ctx) UB_mod_exp(BIGNUM *res, const BIGNUM *a, const BIGNUM *b, const BIGNUM *c)
{ {
struct crypt_kop kop; struct crypt_kop kop;
u_int8_t *ale, *ble, *cle; void *ale, *ble, *cle;
static int crypto_fd = -1; static int crypto_fd = -1;
if (crypto_fd == -1 && ioctl(devcrypto(), CRIOGET, &crypto_fd) == -1) if (crypto_fd == -1 && ioctl(devcrypto(), CRIOGET, &crypto_fd) == -1)
err(1, "CRIOGET"); err(1, "CRIOGET");
if ((ale = bignum_to_le(a, NULL)) == NULL) if ((ale = bignum_to_le(a)) == NULL)
err(1, "bignum_to_le, a"); err(1, "bignum_to_le, a");
if ((ble = bignum_to_le(b, NULL)) == NULL) if ((ble = bignum_to_le(b)) == NULL)
err(1, "bignum_to_le, b"); err(1, "bignum_to_le, b");
if ((cle = bignum_to_le(c, NULL)) == NULL) if ((cle = bignum_to_le(c)) == NULL)
err(1, "bignum_to_le, c"); err(1, "bignum_to_le, c");
bzero(&kop, sizeof(kop)); bzero(&kop, sizeof(kop));
@ -158,19 +144,19 @@ UB_mod_exp(BIGNUM *res, BIGNUM *a, BIGNUM *b, BIGNUM *c, BN_CTX *ctx)
if (verbose) if (verbose)
printf("device = %s\n", crfind(kop.crk_crid)); printf("device = %s\n", crfind(kop.crk_crid));
bzero(ale, BN_num_bytes(a)); explicit_bzero(ale, BN_num_bytes(a));
free(ale); free(ale);
bzero(ble, BN_num_bytes(b)); explicit_bzero(ble, BN_num_bytes(b));
free(ble); free(ble);
if (kop.crk_status != 0) { if (kop.crk_status != 0) {
printf("error %d\n", kop.crk_status); printf("error %d\n", kop.crk_status);
bzero(cle, BN_num_bytes(c)); explicit_bzero(cle, BN_num_bytes(c));
free(cle); free(cle);
return (-1); return (-1);
} else { } else {
res = le_to_bignum(res, cle, BN_num_bytes(c)); res = le_to_bignum(res, cle, BN_num_bytes(c));
bzero(cle, BN_num_bytes(c)); explicit_bzero(cle, BN_num_bytes(c));
free(cle); free(cle);
if (res == NULL) if (res == NULL)
err(1, "le_to_bignum"); err(1, "le_to_bignum");
@ -179,9 +165,9 @@ UB_mod_exp(BIGNUM *res, BIGNUM *a, BIGNUM *b, BIGNUM *c, BN_CTX *ctx)
return (0); return (0);
} }
void static void
show_result(a, b, c, sw, hw) show_result(const BIGNUM *a, const BIGNUM *b, const BIGNUM *c,
BIGNUM *a, *b, *c, *sw, *hw; const BIGNUM *sw, const BIGNUM *hw)
{ {
printf("\n"); printf("\n");
@ -208,7 +194,7 @@ BIGNUM *a, *b, *c, *sw, *hw;
printf("\n"); printf("\n");
} }
void static void
testit(void) testit(void)
{ {
BIGNUM *a, *b, *c, *r1, *r2; BIGNUM *a, *b, *c, *r1, *r2;
@ -230,10 +216,10 @@ testit(void)
BIGNUM *rem = BN_new(); BIGNUM *rem = BN_new();
BN_mod(rem, a, c, ctx); BN_mod(rem, a, c, ctx);
UB_mod_exp(r2, rem, b, c, ctx); UB_mod_exp(r2, rem, b, c);
BN_free(rem); BN_free(rem);
} else { } else {
UB_mod_exp(r2, a, b, c, ctx); UB_mod_exp(r2, a, b, c);
} }
BN_mod_exp(r1, a, b, c, ctx); BN_mod_exp(r1, a, b, c, ctx);