From owner-svn-src-stable-12@freebsd.org Fri Dec 4 01:11:10 2020 Return-Path: Delivered-To: svn-src-stable-12@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 1B6444B5F73; Fri, 4 Dec 2020 01:11:10 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4CnF5V0KmSz3Jm9; Fri, 4 Dec 2020 01:11:10 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id F247522F81; Fri, 4 Dec 2020 01:11:09 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 0B41B9Vq012993; Fri, 4 Dec 2020 01:11:09 GMT (envelope-from jhb@FreeBSD.org) Received: (from jhb@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 0B41B950012992; Fri, 4 Dec 2020 01:11:09 GMT (envelope-from jhb@FreeBSD.org) Message-Id: <202012040111.0B41B950012992@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: jhb set sender to jhb@FreeBSD.org using -f From: John Baldwin Date: Fri, 4 Dec 2020 01:11:09 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r368320 - stable/12/tools/tools/crypto X-SVN-Group: stable-12 X-SVN-Commit-Author: jhb X-SVN-Commit-Paths: stable/12/tools/tools/crypto X-SVN-Commit-Revision: 368320 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable-12@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: SVN commit messages for only the 12-stable src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 04 Dec 2020 01:11:10 -0000 Author: jhb Date: Fri Dec 4 01:11:09 2020 New Revision: 368320 URL: https://svnweb.freebsd.org/changeset/base/368320 Log: MFC 350813: tools/tools/crypto: cryptokeytest: Fix build with newer OpenSSL Also, drag into this decade. Modified: stable/12/tools/tools/crypto/cryptokeytest.c Directory Properties: stable/12/ (props changed) Modified: stable/12/tools/tools/crypto/cryptokeytest.c ============================================================================== --- stable/12/tools/tools/crypto/cryptokeytest.c Fri Dec 4 01:09:51 2020 (r368319) +++ stable/12/tools/tools/crypto/cryptokeytest.c Fri Dec 4 01:11:09 2020 (r368320) @@ -7,19 +7,21 @@ * --Jason L. Wright */ #include +#include #include -#include #include #include -#include -#include -#include #include +#include +#include +#include #include #include -#include +#include +#include + int crid = CRYPTO_FLAG_HARDWARE; int verbose = 0; @@ -63,80 +65,64 @@ crfind(int crid) } /* - * Convert a little endian byte string in 'p' that - * is 'plen' bytes long to a BIGNUM. If 'dst' is NULL, - * 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. + * Convert a little endian byte string in 'p' that is 'plen' bytes long to a + * BIGNUM. A new BIGNUM is allocated. Returns NULL on failure. */ -BIGNUM * -le_to_bignum(BIGNUM *dst, u_int8_t *p, int plen) +static BIGNUM * +le_to_bignum(BIGNUM *res, const void *p, int plen) { - u_int8_t *pd; - int i; - if (plen == 0) - return (NULL); + res = BN_lebin2bn(p, plen, res); + if (res == NULL) + ERR_print_errors_fp(stderr); - if ((pd = (u_int8_t *)malloc(plen)) == NULL) - return (NULL); - - for (i = 0; i < plen; i++) - pd[i] = p[plen - i - 1]; - - dst = BN_bin2bn(pd, plen, dst); - free(pd); - return (dst); + return (res); } /* - * Convert a BIGNUM to a little endian byte string. - * If 'rd' is NULL, allocate space for it, otherwise - * 'rd' is assumed to have room for BN_num_bytes(n) - * bytes. Returns NULL on failure. + * Convert a BIGNUM to a little endian byte string. Space for BN_num_bytes(n) + * is allocated. + * Returns NULL on failure. */ -u_int8_t * -bignum_to_le(BIGNUM *n, u_int8_t *rd) +static void * +bignum_to_le(const BIGNUM *n) { - int i, j, k; - int blen = BN_num_bytes(n); + int blen, error; + void *rd; + blen = BN_num_bytes(n); if (blen == 0) return (NULL); + + rd = malloc(blen); if (rd == NULL) - rd = (u_int8_t *)malloc(blen); - if (rd == NULL) return (NULL); - for (i = 0, j = 0; i < n->top; i++) { - for (k = 0; k < BN_BITS2 / 8; k++) { - if ((j + k) >= blen) - goto out; - rd[j + k] = n->d[i] >> (k * 8); - } - j += BN_BITS2 / 8; + error = BN_bn2lebinpad(n, rd, blen); + if (error < 0) { + ERR_print_errors_fp(stderr); + free(rd); + return (NULL); } -out: + return (rd); } -int -UB_mod_exp(BIGNUM *res, BIGNUM *a, BIGNUM *b, BIGNUM *c, BN_CTX *ctx) +static int +UB_mod_exp(BIGNUM *res, const BIGNUM *a, const BIGNUM *b, const BIGNUM *c) { struct crypt_kop kop; - u_int8_t *ale, *ble, *cle; + void *ale, *ble, *cle; static int crypto_fd = -1; if (crypto_fd == -1 && ioctl(devcrypto(), CRIOGET, &crypto_fd) == -1) err(1, "CRIOGET"); - if ((ale = bignum_to_le(a, NULL)) == NULL) + if ((ale = bignum_to_le(a)) == NULL) 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"); - if ((cle = bignum_to_le(c, NULL)) == NULL) + if ((cle = bignum_to_le(c)) == NULL) err(1, "bignum_to_le, c"); bzero(&kop, sizeof(kop)); @@ -158,19 +144,19 @@ UB_mod_exp(BIGNUM *res, BIGNUM *a, BIGNUM *b, BIGNUM * if (verbose) printf("device = %s\n", crfind(kop.crk_crid)); - bzero(ale, BN_num_bytes(a)); + explicit_bzero(ale, BN_num_bytes(a)); free(ale); - bzero(ble, BN_num_bytes(b)); + explicit_bzero(ble, BN_num_bytes(b)); free(ble); if (kop.crk_status != 0) { printf("error %d\n", kop.crk_status); - bzero(cle, BN_num_bytes(c)); + explicit_bzero(cle, BN_num_bytes(c)); free(cle); return (-1); } else { 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); if (res == NULL) err(1, "le_to_bignum"); @@ -179,9 +165,9 @@ UB_mod_exp(BIGNUM *res, BIGNUM *a, BIGNUM *b, BIGNUM * return (0); } -void -show_result(a, b, c, sw, hw) -BIGNUM *a, *b, *c, *sw, *hw; +static void +show_result(const BIGNUM *a, const BIGNUM *b, const BIGNUM *c, + const BIGNUM *sw, const BIGNUM *hw) { printf("\n"); @@ -208,7 +194,7 @@ BIGNUM *a, *b, *c, *sw, *hw; printf("\n"); } -void +static void testit(void) { BIGNUM *a, *b, *c, *r1, *r2; @@ -230,10 +216,10 @@ testit(void) BIGNUM *rem = BN_new(); BN_mod(rem, a, c, ctx); - UB_mod_exp(r2, rem, b, c, ctx); + UB_mod_exp(r2, rem, b, c); BN_free(rem); } else { - UB_mod_exp(r2, a, b, c, ctx); + UB_mod_exp(r2, a, b, c); } BN_mod_exp(r1, a, b, c, ctx);