Update from 1.0.0-stable
[openssl.git] / crypto / engine / eng_cryptodev.c
index a658528d315ccedd4e0aba844a001e3f44457f3a..b5a8a7fd7edcfab7af0dea3fef824bc5c0b22b4f 100644 (file)
@@ -12,9 +12,6 @@
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the author nor the names of contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 #include <openssl/objects.h>
 #include <openssl/engine.h>
 #include <openssl/evp.h>
+#include <openssl/bn.h>
 
-#if (defined(__unix__) || defined(unix)) && !defined(USG)
+#if (defined(__unix__) || defined(unix)) && !defined(USG) && \
+       (defined(OpenBSD) || defined(__FreeBSD_version))
 #include <sys/param.h>
-# if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 50000) || __FreeBSD_version >= 50041)
+# if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041)
 #  define HAVE_CRYPTODEV
 # endif
 # if (OpenBSD >= 200110)
@@ -101,8 +100,8 @@ static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
 static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
     const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I,
-    RSA *rsa);
-static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
+    RSA *rsa, BN_CTX *ctx);
+static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
 static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
     const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
 static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
@@ -261,11 +260,11 @@ get_cryptodev_ciphers(const int **cnids)
        int fd, i, count = 0;
 
        if ((fd = get_dev_crypto()) < 0) {
-               *nids = NULL;
+               *cnids = NULL;
                return (0);
        }
        memset(&sess, 0, sizeof(sess));
-       sess.key = (caddr_t)"123456781234567812345678";
+       sess.key = (caddr_t)"123456789abcdefghijklmno";
 
        for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
                if (ciphers[i].nid == NID_undef)
@@ -300,7 +299,7 @@ get_cryptodev_digests(const int **cnids)
        int fd, i, count = 0;
 
        if ((fd = get_dev_crypto()) < 0) {
-               *nids = NULL;
+               *cnids = NULL;
                return (0);
        }
        memset(&sess, 0, sizeof(sess));
@@ -626,7 +625,7 @@ static int
 bn2crparam(const BIGNUM *a, struct crparam *crp)
 {
        int i, j, k;
-       ssize_t words, bytes, bits;
+       ssize_t bytes, bits;
        u_char *b;
 
        crp->crp_p = NULL;
@@ -638,6 +637,7 @@ bn2crparam(const BIGNUM *a, struct crparam *crp)
        b = malloc(bytes);
        if (b == NULL)
                return (1);
+       memset(b, 0, bytes);
 
        crp->crp_p = b;
        crp->crp_nbits = bits;
@@ -682,7 +682,7 @@ zapparams(struct crypt_kop *kop)
 {
        int i;
 
-       for (i = 0; i <= kop->crk_iparams + kop->crk_oparams; i++) {
+       for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
                if (kop->crk_param[i].crp_p)
                        free(kop->crk_param[i].crp_p);
                kop->crk_param[i].crp_p = NULL;
@@ -757,11 +757,9 @@ err:
 }
 
 static int
-cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
+cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
 {
        int r;
-       BN_CTX *ctx;
-
        ctx = BN_CTX_new();
        r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
        BN_CTX_free(ctx);
@@ -769,7 +767,7 @@ cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
 }
 
 static int
-cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
+cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
 {
        struct crypt_kop kop;
        int ret = 1;
@@ -798,7 +796,7 @@ cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
 
        if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL) == -1) {
                const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
-               ret = (*meth->rsa_mod_exp)(r0, I, rsa);
+               ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
        }
 err:
        zapparams(&kop);
@@ -874,7 +872,6 @@ cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
                goto err;
        }
 
-       printf("bar\n");
        memset(&kop, 0, sizeof kop);
        kop.crk_op = CRK_DSA_SIGN;
 
@@ -1054,14 +1051,17 @@ ENGINE_load_cryptodev(void)
 
        if (engine == NULL)
                return;
-       if ((fd = get_dev_crypto()) < 0)
+       if ((fd = get_dev_crypto()) < 0) {
+               ENGINE_free(engine);
                return;
+       }
 
        /*
         * find out what asymmetric crypto algorithms we support
         */
        if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
                close(fd);
+               ENGINE_free(engine);
                return;
        }
        close(fd);