MIPS32R3 provides the EXT instruction to extract bits from
[openssl.git] / test / ecdsatest.c
index a3234814d47df88abd92664fc2677deaf90f4325..bc3adc004c240ba690fc1334686ed43054af86b8 100644 (file)
@@ -1,39 +1,21 @@
 /*
- * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  *
- * Licensed under the OpenSSL license (the "License").  You may not use
+ * Licensed under the Apache License 2.0 (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
  */
 
-/* ====================================================================
- * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
- *
- * Portions of the attached software ("Contribution") are developed by
- * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
- *
- * The Contribution is licensed pursuant to the OpenSSL open source
- * license provided above.
- *
- * The elliptic curve binary polynomial software is originally written by
- * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
- *
- */
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include <openssl/opensslconf.h> /* To see if OPENSSL_NO_EC is defined */
+#include "testutil.h"
 
-#ifdef OPENSSL_NO_EC
-int main(int argc, char *argv[])
-{
-    puts("Elliptic curves are disabled.");
-    return 0;
-}
-#else
+#ifndef OPENSSL_NO_EC
 
 # include <openssl/crypto.h>
 # include <openssl/bio.h>
@@ -43,13 +25,9 @@ int main(int argc, char *argv[])
 # ifndef OPENSSL_NO_ENGINE
 #  include <openssl/engine.h>
 # endif
+# include <openssl/sha.h>
 # include <openssl/err.h>
 # include <openssl/rand.h>
-# include "testutil.h"
-
-static const char rnd_seed[] = "string to make the random number generator "
-    "think it has entropy";
-
 
 /* functions to change the RAND_METHOD */
 static int fbytes(unsigned char *buf, int num);
@@ -126,7 +104,7 @@ static int x9_62_test_internal(int nid, const char *r_in, const char *s_in)
 {
     int ret = 0;
     const char message[] = "abc";
-    unsigned char digest[20];
+    unsigned char digest[SHA_DIGEST_LENGTH];
     unsigned int dgst_len = 0;
     EVP_MD_CTX *md_ctx;
     EC_KEY *key = NULL;
@@ -158,7 +136,8 @@ static int x9_62_test_internal(int nid, const char *r_in, const char *s_in)
     /* Use ECDSA_sign_setup to avoid use of ECDSA nonces */
     if (!TEST_true(ECDSA_sign_setup(key, NULL, &kinv, &rp)))
         goto x962_int_err;
-    if (!TEST_ptr(signature = ECDSA_do_sign_ex(digest, 20, kinv, rp, key)))
+    if (!TEST_ptr(signature =
+                  ECDSA_do_sign_ex(digest, SHA_DIGEST_LENGTH, kinv, rp, key)))
         goto x962_int_err;
 
     /* compare the created signature with the expected signature */
@@ -172,7 +151,8 @@ static int x9_62_test_internal(int nid, const char *r_in, const char *s_in)
         goto x962_int_err;
 
     /* verify the signature */
-    if (!TEST_int_eq(ECDSA_do_verify(digest, 20, signature, key), 1))
+    if (!TEST_int_eq(ECDSA_do_verify(digest, SHA_DIGEST_LENGTH,
+                                     signature, key), 1))
         goto x962_int_err;
 
     ret = 1;
@@ -188,7 +168,7 @@ static int x9_62_test_internal(int nid, const char *r_in, const char *s_in)
     return ret;
 }
 
-static int x9_62_tests()
+static int x9_62_tests(void)
 {
     int ret = 0;
 
@@ -234,7 +214,8 @@ static int test_builtin(void)
     EC_KEY *eckey = NULL, *wrong_eckey = NULL;
     EC_GROUP *group;
     ECDSA_SIG *ecdsa_sig = NULL, *modified_sig = NULL;
-    unsigned char digest[20], wrong_digest[20];
+    unsigned char digest[SHA512_DIGEST_LENGTH];
+    unsigned char wrong_digest[SHA512_DIGEST_LENGTH];
     unsigned char *signature = NULL;
     const unsigned char *sig_ptr;
     unsigned char *sig_ptr2;
@@ -242,12 +223,12 @@ static int test_builtin(void)
     const BIGNUM *sig_r, *sig_s;
     BIGNUM *modified_r = NULL, *modified_s = NULL;
     BIGNUM *unmodified_r = NULL, *unmodified_s = NULL;
-    unsigned int sig_len, degree, r_len, s_len, bn_len, buf_len;
+    unsigned int sig_len, order, r_len, s_len, bn_len, buf_len;
     int nid, ret = 0;
 
     /* fill digest values with some random data */
-    if (!TEST_true(RAND_bytes(digest, 20))
-            || !TEST_true(RAND_bytes(wrong_digest, 20)))
+    if (!TEST_true(RAND_bytes(digest, SHA512_DIGEST_LENGTH))
+            || !TEST_true(RAND_bytes(wrong_digest, SHA512_DIGEST_LENGTH)))
         goto builtin_err;
 
     /* create and verify a ecdsa signature with every available curve */
@@ -262,7 +243,7 @@ static int test_builtin(void)
         unsigned char dirt, offset;
 
         nid = curves[n].nid;
-        if (nid == NID_ipsec4 || nid == NID_X25519)
+        if (nid == NID_ipsec4 || nid == NID_ipsec3)
             continue;
         /* create new ecdsa key (== EC_KEY) */
         if (!TEST_ptr(eckey = EC_KEY_new())
@@ -270,13 +251,8 @@ static int test_builtin(void)
                 || !TEST_true(EC_KEY_set_group(eckey, group)))
             goto builtin_err;
         EC_GROUP_free(group);
-        degree = EC_GROUP_get_degree(EC_KEY_get0_group(eckey));
-        if (degree < 160) {
-            /* drop the curve */
-            EC_KEY_free(eckey);
-            eckey = NULL;
-            continue;
-        }
+        order = EC_GROUP_order_bits(EC_KEY_get0_group(eckey));
+
         TEST_info("testing %s", OBJ_nid2sn(nid));
 
         /* create key */
@@ -298,28 +274,32 @@ static int test_builtin(void)
         /* create signature */
         sig_len = ECDSA_size(eckey);
         if (!TEST_ptr(signature = OPENSSL_malloc(sig_len))
-                || !TEST_true(ECDSA_sign(0, digest, 20, signature, &sig_len,
-                                         eckey)))
+                || !TEST_true(ECDSA_sign(0, digest, SHA512_DIGEST_LENGTH,
+                                         signature, &sig_len, eckey)))
             goto builtin_err;
 
         /* verify signature */
-        if (!TEST_int_eq(ECDSA_verify(0, digest, 20, signature, sig_len,
-                                      eckey), 1))
+        if (!TEST_int_eq(ECDSA_verify(0, digest, SHA512_DIGEST_LENGTH,
+                                      signature, sig_len, eckey),
+                         1))
             goto builtin_err;
 
         /* verify signature with the wrong key */
-        if (!TEST_int_ne(ECDSA_verify(0, digest, 20, signature, sig_len,
-                                      wrong_eckey), 1))
+        if (!TEST_int_ne(ECDSA_verify(0, digest, SHA512_DIGEST_LENGTH,
+                                      signature, sig_len, wrong_eckey),
+                         1))
             goto builtin_err;
 
         /* wrong digest */
-        if (!TEST_int_ne(ECDSA_verify(0, wrong_digest, 20, signature,
-                                      sig_len, eckey), 1))
+        if (!TEST_int_ne(ECDSA_verify(0, wrong_digest, SHA512_DIGEST_LENGTH,
+                                      signature, sig_len, eckey),
+                         1))
             goto builtin_err;
 
         /* wrong length */
-        if (!TEST_int_ne(ECDSA_verify(0, digest, 20, signature,
-                                      sig_len - 1, eckey), 1))
+        if (!TEST_int_ne(ECDSA_verify(0, digest, SHA512_DIGEST_LENGTH,
+                                      signature, sig_len - 1, eckey),
+                         1))
             goto builtin_err;
 
         /*
@@ -336,7 +316,7 @@ static int test_builtin(void)
         /* Store the two BIGNUMs in raw_buf. */
         r_len = BN_num_bytes(sig_r);
         s_len = BN_num_bytes(sig_s);
-        bn_len = (degree + 7) / 8;
+        bn_len = (order + 7) / 8;
         if (!TEST_false(r_len > bn_len)
                 || !TEST_false(s_len > bn_len))
             goto builtin_err;
@@ -365,14 +345,15 @@ static int test_builtin(void)
         }
         sig_ptr2 = signature;
         sig_len = i2d_ECDSA_SIG(modified_sig, &sig_ptr2);
-        if (!TEST_false(ECDSA_verify(0, digest, 20, signature, sig_len, eckey)))
+        if (!TEST_false(ECDSA_verify(0, digest, SHA512_DIGEST_LENGTH,
+                                     signature, sig_len, eckey)))
             goto builtin_err;
 
         /* Sanity check: undo the modification and verify signature. */
         raw_buf[offset] ^= dirt;
         if (!TEST_ptr(unmodified_r = BN_bin2bn(raw_buf, bn_len, NULL))
                 || !TEST_ptr(unmodified_s = BN_bin2bn(raw_buf + bn_len,
-                                                       bn_len, NULL))
+                                                      bn_len, NULL))
                 || !TEST_true(ECDSA_SIG_set0(modified_sig, unmodified_r,
                                              unmodified_s))) {
             BN_free(unmodified_r);
@@ -382,7 +363,8 @@ static int test_builtin(void)
 
         sig_ptr2 = signature;
         sig_len = i2d_ECDSA_SIG(modified_sig, &sig_ptr2);
-        if (!TEST_true(ECDSA_verify(0, digest, 20, signature, sig_len, eckey)))
+        if (!TEST_true(ECDSA_verify(0, digest, SHA512_DIGEST_LENGTH,
+                                    signature, sig_len, eckey)))
             goto builtin_err;
 
         /* cleanup */
@@ -413,12 +395,15 @@ static int test_builtin(void)
 
     return ret;
 }
+#endif
 
-void register_tests(void)
+int setup_tests(void)
 {
-    /* initialize the prng */
-    RAND_seed(rnd_seed, sizeof(rnd_seed));
+#ifdef OPENSSL_NO_EC
+    TEST_note("Elliptic curves are disabled.");
+#else
     ADD_TEST(x9_62_tests);
     ADD_TEST(test_builtin);
-}
 #endif
+    return 1;
+}