DSA mod inverse fix
[openssl.git] / crypto / dsa / dsa_meth.c
index ed4df5413ccf63bae39b3f812b78f1658c3bd283..ff4fae44a7c309d6f1dc4ddddea4469a773e783f 100644 (file)
-/* ====================================================================
- * Copyright (c) 2016 The OpenSSL Project.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *
- * 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. All advertising materials mentioning features or use of this
- *    software must display the following acknowledgment:
- *    "This product includes software developed by the OpenSSL Project
- *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
- *
- * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
- *    endorse or promote products derived from this software without
- *    prior written permission. For written permission, please contact
- *    openssl-core@OpenSSL.org.
- *
- * 5. Products derived from this software may not be called "OpenSSL"
- *    nor may "OpenSSL" appear in their names without prior written
- *    permission of the OpenSSL Project.
- *
- * 6. Redistributions of any form whatsoever must retain the following
- *    acknowledgment:
- *    "This product includes software developed by the OpenSSL Project
- *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
- *
- * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
- * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- * OF THE POSSIBILITY OF SUCH DAMAGE.
- * ====================================================================
- *
- * This product includes cryptographic software written by Eric Young
- * (eay@cryptsoft.com).  This product includes software written by Tim
- * Hudson (tjh@cryptsoft.com).
+/*
+ * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  *
+ * Licensed under the OpenSSL license (the "License").  You may not use
+ * this file except in compliance with the License.  You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+/*
+ * Licensed under the OpenSSL licenses, (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * https://www.openssl.org/source/license.html
+ * or in the file LICENSE in the source distribution.
  */
 
 #include "dsa_locl.h"
 #include <string.h>
+#include <openssl/err.h>
 
 DSA_METHOD *DSA_meth_new(const char *name, int flags)
 {
-    DSA_METHOD *dsam = OPENSSL_zalloc(sizeof(DSA_METHOD));
+    DSA_METHOD *dsam = OPENSSL_zalloc(sizeof(*dsam));
 
     if (dsam != NULL) {
-        dsam->name = OPENSSL_strdup(name);
         dsam->flags = flags;
+
+        dsam->name = OPENSSL_strdup(name);
+        if (dsam->name != NULL)
+            return dsam;
+
+        OPENSSL_free(dsam);
     }
 
-    return dsam;
+    DSAerr(DSA_F_DSA_METH_NEW, ERR_R_MALLOC_FAILURE);
+    return NULL;
 }
 
 void DSA_meth_free(DSA_METHOD *dsam)
 {
     if (dsam != NULL) {
-        if (dsam->name != NULL)
-            OPENSSL_free(dsam->name);
+        OPENSSL_free(dsam->name);
         OPENSSL_free(dsam);
     }
 }
 
-DSA_METHOD *DSA_meth_dup(const DSA_METHOD *meth)
+DSA_METHOD *DSA_meth_dup(const DSA_METHOD *dsam)
 {
-    DSA_METHOD *ret;
-
-    ret = OPENSSL_malloc(sizeof(DSA_METHOD));
+    DSA_METHOD *ret = OPENSSL_malloc(sizeof(*ret));
 
     if (ret != NULL) {
-        memcpy(ret, meth, sizeof(*meth));
-        ret->name = OPENSSL_strdup(meth->name);
+        memcpy(ret, dsam, sizeof(*dsam));
+
+        ret->name = OPENSSL_strdup(dsam->name);
+        if (ret->name != NULL)
+            return ret;
+
+        OPENSSL_free(ret);
     }
 
-    return ret;
+    DSAerr(DSA_F_DSA_METH_DUP, ERR_R_MALLOC_FAILURE);
+    return NULL;
 }
 
-const char *DSA_meth_get_name(const DSA_METHOD *dsam)
+const char *DSA_meth_get0_name(const DSA_METHOD *dsam)
 {
     return dsam->name;
 }
 
-int DSA_meth_set_name(DSA_METHOD *dsam, const char *name)
+int DSA_meth_set1_name(DSA_METHOD *dsam, const char *name)
 {
+    char *tmpname = OPENSSL_strdup(name);
+
+    if (tmpname == NULL) {
+        DSAerr(DSA_F_DSA_METH_SET1_NAME, ERR_R_MALLOC_FAILURE);
+        return 0;
+    }
+
     OPENSSL_free(dsam->name);
-    dsam->name = OPENSSL_strdup(name);
+    dsam->name = tmpname;
 
-    return dsam->name != NULL;
+    return 1;
 }
 
-int DSA_meth_get_flags(DSA_METHOD *dsam)
+int DSA_meth_get_flags(const DSA_METHOD *dsam)
 {
     return dsam->flags;
 }
@@ -114,12 +94,12 @@ int DSA_meth_set_flags(DSA_METHOD *dsam, int flags)
     return 1;
 }
 
-void *DSA_meth_get_app_data(const DSA_METHOD *dsam)
+void *DSA_meth_get0_app_data(const DSA_METHOD *dsam)
 {
     return dsam->app_data;
 }
 
-int DSA_meth_set_app_data(DSA_METHOD *dsam, void *app_data)
+int DSA_meth_set0_app_data(DSA_METHOD *dsam, void *app_data)
 {
     dsam->app_data = app_data;
     return 1;
@@ -152,7 +132,7 @@ int DSA_meth_set_sign_setup(DSA_METHOD *dsam,
 }
 
 int (*DSA_meth_get_verify(const DSA_METHOD *dsam))
-        (const unsigned char *, int , DSA_SIG *, DSA *)
+        (const unsigned char *, int, DSA_SIG *, DSA *)
 {
     return dsam->dsa_do_verify;
 }
@@ -165,29 +145,30 @@ int DSA_meth_set_verify(DSA_METHOD *dsam,
 }
 
 int (*DSA_meth_get_mod_exp(const DSA_METHOD *dsam))
-        (DSA *, BIGNUM *, BIGNUM *, BIGNUM *, BIGNUM *, BIGNUM *, BIGNUM *,
-         BN_CTX *, BN_MONT_CTX *)
+        (DSA *, BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *,
+         const BIGNUM *, const BIGNUM *, BN_CTX *, BN_MONT_CTX *)
 {
     return dsam->dsa_mod_exp;
 }
 
 int DSA_meth_set_mod_exp(DSA_METHOD *dsam,
-    int (*mod_exp) (DSA *, BIGNUM *, BIGNUM *, BIGNUM *, BIGNUM *, BIGNUM *,
-                    BIGNUM *, BN_CTX *, BN_MONT_CTX *))
+    int (*mod_exp) (DSA *, BIGNUM *, const BIGNUM *, const BIGNUM *,
+                    const BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *,
+                    BN_MONT_CTX *))
 {
     dsam->dsa_mod_exp = mod_exp;
     return 1;
 }
 
 int (*DSA_meth_get_bn_mod_exp(const DSA_METHOD *dsam))
-    (DSA *, BIGNUM *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *,
+    (DSA *, BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *,
      BN_MONT_CTX *)
 {
     return dsam->bn_mod_exp;
 }
 
 int DSA_meth_set_bn_mod_exp(DSA_METHOD *dsam,
-    int (*bn_mod_exp) (DSA *, BIGNUM *, BIGNUM *, const BIGNUM *,
+    int (*bn_mod_exp) (DSA *, BIGNUM *, const BIGNUM *, const BIGNUM *,
                        const BIGNUM *, BN_CTX *, BN_MONT_CTX *))
 {
     dsam->bn_mod_exp = bn_mod_exp;