Remove some unneeded code
[openssl.git] / crypto / ec / curve448 / curve448_test.c
index 0e2e58eedd633876140e37cc067a78408aed6ef0..c63bded7d59451520a6384f184de3f564b8db1e0 100644 (file)
@@ -1,6 +1,15 @@
+/*
+ * Copyright 2017 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
+ */
 #include <stdio.h>
 #include <string.h>
 #include <openssl/e_os2.h>
+#include <openssl/evp.h>
 #include "curve448_lcl.h"
 
 /* Test vectors from RFC7748 for X448 */
@@ -63,28 +72,37 @@ const uint8_t in_u3[56] = {
 
 const uint8_t out_u3[3][56] = {
     {
-        0x3f, 0x48, 0x2c, 0x8a, 0x9f, 0x19, 0xb0, 0x1e, 0x6c, 0x46, 0xee, 0x97,
-        0x11, 0xd9, 0xdc, 0x14, 0xfd, 0x4b, 0xf6, 0x7a, 0xf3, 0x07, 0x65, 0xc2,
-        0xae, 0x2b, 0x84, 0x6a, 0x4d, 0x23, 0xa8, 0xcd, 0x0d, 0xb8, 0x97, 0x08,
-        0x62, 0x39, 0x49, 0x2c, 0xaf, 0x35, 0x0b, 0x51, 0xf8, 0x33, 0x86, 0x8b,
-        0x9b, 0xc2, 0xb3, 0xbc, 0xa9, 0xcf, 0x41, 0x13
-    }, {
-        0xaa, 0x3b, 0x47, 0x49, 0xd5, 0x5b, 0x9d, 0xaf, 0x1e, 0x5b, 0x00, 0x28,
-        0x88, 0x26, 0xc4, 0x67, 0x27, 0x4c, 0xe3, 0xeb, 0xbd, 0xd5, 0xc1, 0x7b,
-        0x97, 0x5e, 0x09, 0xd4, 0xaf, 0x6c, 0x67, 0xcf, 0x10, 0xd0, 0x87, 0x20,
-        0x2d, 0xb8, 0x82, 0x86, 0xe2, 0xb7, 0x9f, 0xce, 0xea, 0x3e, 0xc3, 0x53,
-        0xef, 0x54, 0xfa, 0xa2, 0x6e, 0x21, 0x9f, 0x38
-    }, {
-        0x07, 0x7f, 0x45, 0x36, 0x81, 0xca, 0xca, 0x36, 0x93, 0x19, 0x84, 0x20,
-        0xbb, 0xe5, 0x15, 0xca, 0xe0, 0x00, 0x24, 0x72, 0x51, 0x9b, 0x3e, 0x67,
-        0x66, 0x1a, 0x7e, 0x89, 0xca, 0xb9, 0x46, 0x95, 0xc8, 0xf4, 0xbc, 0xd6,
-        0x6e, 0x61, 0xb9, 0xb9, 0xc9, 0x46, 0xda, 0x8d, 0x52, 0x4d, 0xe3, 0xd6,
-        0x9b, 0xd9, 0xd9, 0xd6, 0x6b, 0x99, 0x7e, 0x37
-    }
+     0x3f, 0x48, 0x2c, 0x8a, 0x9f, 0x19, 0xb0, 0x1e, 0x6c, 0x46, 0xee, 0x97,
+     0x11, 0xd9, 0xdc, 0x14, 0xfd, 0x4b, 0xf6, 0x7a, 0xf3, 0x07, 0x65, 0xc2,
+     0xae, 0x2b, 0x84, 0x6a, 0x4d, 0x23, 0xa8, 0xcd, 0x0d, 0xb8, 0x97, 0x08,
+     0x62, 0x39, 0x49, 0x2c, 0xaf, 0x35, 0x0b, 0x51, 0xf8, 0x33, 0x86, 0x8b,
+     0x9b, 0xc2, 0xb3, 0xbc, 0xa9, 0xcf, 0x41, 0x13}, {
+                                                       0xaa, 0x3b, 0x47, 0x49,
+                                                       0xd5, 0x5b, 0x9d, 0xaf,
+                                                       0x1e, 0x5b, 0x00, 0x28,
+                                                       0x88, 0x26, 0xc4, 0x67,
+                                                       0x27, 0x4c, 0xe3, 0xeb,
+                                                       0xbd, 0xd5, 0xc1, 0x7b,
+                                                       0x97, 0x5e, 0x09, 0xd4,
+                                                       0xaf, 0x6c, 0x67, 0xcf,
+                                                       0x10, 0xd0, 0x87, 0x20,
+                                                       0x2d, 0xb8, 0x82, 0x86,
+                                                       0xe2, 0xb7, 0x9f, 0xce,
+                                                       0xea, 0x3e, 0xc3, 0x53,
+                                                       0xef, 0x54, 0xfa, 0xa2,
+                                                       0x6e, 0x21, 0x9f, 0x38},
+    {
+     0x07, 0x7f, 0x45, 0x36, 0x81, 0xca, 0xca, 0x36, 0x93, 0x19, 0x84, 0x20,
+     0xbb, 0xe5, 0x15, 0xca, 0xe0, 0x00, 0x24, 0x72, 0x51, 0x9b, 0x3e, 0x67,
+     0x66, 0x1a, 0x7e, 0x89, 0xca, 0xb9, 0x46, 0x95, 0xc8, 0xf4, 0xbc, 0xd6,
+     0x6e, 0x61, 0xb9, 0xb9, 0xc9, 0x46, 0xda, 0x8d, 0x52, 0x4d, 0xe3, 0xd6,
+     0x9b, 0xd9, 0xd9, 0xd6, 0x6b, 0x99, 0x7e, 0x37}
 };
 
 /* Test vectors from RFC8032 for Ed448 */
 
+/* Pure Ed448 */
+
 const uint8_t privkey1[57] = {
     0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10, 0xd6, 0x32, 0xbe, 0x89,
     0xc8, 0x51, 0x3e, 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
@@ -494,65 +512,175 @@ const uint8_t sig9[114] = {
     0xc3, 0x20, 0x86, 0x9e, 0x1a, 0x00
 };
 
+/* Prehash Ed448 */
+
+const uint8_t phprivkey1[57] = {
+    0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d, 0x62, 0xec, 0x77, 0x58,
+    0x75, 0x20, 0x91, 0x1e, 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
+    0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42, 0xef, 0x78, 0x22, 0xe0,
+    0xd5, 0x10, 0x41, 0x27, 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
+    0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c, 0x49
+};
+
+const uint8_t phpubkey1[57] = {
+    0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77, 0xa7, 0xab, 0xd2, 0x65,
+    0x24, 0xcb, 0xdb, 0x31, 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
+    0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43, 0xc0, 0x94, 0x28, 0xa1,
+    0x31, 0xd6, 0xb1, 0xb5, 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
+    0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38, 0x80
+};
+
+const uint8_t phmsg1[3] = {
+    0x61, 0x62, 0x63
+};
+
+const uint8_t phsig1[114] = {
+    0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d, 0x5f, 0x56, 0x2c, 0x59,
+    0x29, 0x94, 0xd9, 0x69, 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56,
+    0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38, 0x1f, 0x54, 0xd6, 0xbc,
+    0xe2, 0xea, 0x91, 0x15, 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd,
+    0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b, 0x80, 0x1a, 0x0d, 0x9b,
+    0x3f, 0x40, 0x30, 0xcd, 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94,
+    0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5, 0x7d, 0xd3, 0x2d, 0xbc,
+    0x56, 0x0a, 0x9a, 0x94, 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3,
+    0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60, 0xc3, 0xcd, 0x36, 0x96,
+    0xd9, 0xd9, 0xfa, 0xb9, 0x0f, 0x00
+};
+
+const uint8_t phprivkey2[57] = {
+    0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d, 0x62, 0xec, 0x77, 0x58,
+    0x75, 0x20, 0x91, 0x1e, 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
+    0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42, 0xef, 0x78, 0x22, 0xe0,
+    0xd5, 0x10, 0x41, 0x27, 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
+    0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c, 0x49
+};
+
+const uint8_t phpubkey2[57] = {
+    0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77, 0xa7, 0xab, 0xd2, 0x65,
+    0x24, 0xcb, 0xdb, 0x31, 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
+    0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43, 0xc0, 0x94, 0x28, 0xa1,
+    0x31, 0xd6, 0xb1, 0xb5, 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
+    0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38, 0x80
+};
+
+const uint8_t phmsg2[3] = {
+    0x61, 0x62, 0x63
+};
+
+const uint8_t phcontext2[3] = {
+    0x66, 0x6f, 0x6f
+};
+
+const uint8_t phsig2[114] = {
+    0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02, 0xb5, 0x45, 0x40, 0x98,
+    0x28, 0x14, 0xdc, 0xe9, 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49,
+    0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48, 0x10, 0x65, 0xb1, 0x58,
+    0x04, 0x23, 0xef, 0x92, 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda,
+    0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2, 0x80, 0xd9, 0x12, 0x24,
+    0xba, 0x99, 0x11, 0xa3, 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23,
+    0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30, 0x4b, 0xb7, 0x74, 0x5a,
+    0x73, 0x51, 0x4c, 0xdb, 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28,
+    0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a, 0x6b, 0x62, 0xd9, 0x7b,
+    0xeb, 0x51, 0x1d, 0x13, 0x21, 0x00
+};
+
+static const uint8_t *dohash(EVP_MD_CTX *hashctx, const uint8_t *msg,
+                             size_t msglen)
+{
+    static uint8_t hashout[64];
+
+    if (!EVP_DigestInit_ex(hashctx, EVP_shake256(), NULL)
+        || !EVP_DigestUpdate(hashctx, msg, msglen)
+        || !EVP_DigestFinalXOF(hashctx, hashout, sizeof(hashout)))
+        return NULL;
+
+    return hashout;
+}
+
 static int test_eddsa(void)
 {
     uint8_t outsig[114];
+    EVP_MD_CTX *hashctx = EVP_MD_CTX_new();
+    int ret = 0;
+
+    if (hashctx == NULL) {
+        printf("Failed to allocate EVP_MD_CTX\n");
+        return 0;
+    }
 
     ED448_sign(outsig, NULL, 0, pubkey1, privkey1, NULL, 0);
     if (memcmp(sig1, outsig, sizeof(sig1)) != 0) {
         printf("Calculated sig and expected sig differ (1)\n");
-        return 0;
+        goto err;
     }
 
     ED448_sign(outsig, msg2, sizeof(msg2), pubkey2, privkey2, NULL, 0);
     if (memcmp(sig2, outsig, sizeof(sig2)) != 0) {
         printf("Calculated sig and expected sig differ (2)\n");
-        return 0;
+        goto err;
     }
 
-    ED448_sign(outsig, msg3, sizeof(msg3), pubkey3, privkey3, context3, sizeof(context3));
+    ED448_sign(outsig, msg3, sizeof(msg3), pubkey3, privkey3, context3,
+               sizeof(context3));
     if (memcmp(sig3, outsig, sizeof(sig3)) != 0) {
         printf("Calculated sig and expected sig differ (3)\n");
-        return 0;
+        goto err;
     }
 
     ED448_sign(outsig, msg4, sizeof(msg4), pubkey4, privkey4, NULL, 0);
     if (memcmp(sig4, outsig, sizeof(sig4)) != 0) {
         printf("Calculated sig and expected sig differ (4)\n");
-        return 0;
+        goto err;
     }
 
     ED448_sign(outsig, msg5, sizeof(msg5), pubkey5, privkey5, NULL, 0);
     if (memcmp(sig5, outsig, sizeof(sig5)) != 0) {
         printf("Calculated sig and expected sig differ (5)\n");
-        return 0;
+        goto err;
     }
 
     ED448_sign(outsig, msg6, sizeof(msg6), pubkey6, privkey6, NULL, 0);
     if (memcmp(sig6, outsig, sizeof(sig6)) != 0) {
         printf("Calculated sig and expected sig differ (6)\n");
-        return 0;
+        goto err;
     }
 
     ED448_sign(outsig, msg7, sizeof(msg7), pubkey7, privkey7, NULL, 0);
     if (memcmp(sig7, outsig, sizeof(sig7)) != 0) {
         printf("Calculated sig and expected sig differ (7)\n");
-        return 0;
+        goto err;
     }
 
     ED448_sign(outsig, msg8, sizeof(msg8), pubkey8, privkey8, NULL, 0);
     if (memcmp(sig8, outsig, sizeof(sig8)) != 0) {
         printf("Calculated sig and expected sig differ (8)\n");
-        return 0;
+        goto err;
     }
 
     ED448_sign(outsig, msg9, sizeof(msg9), pubkey9, privkey9, NULL, 0);
     if (memcmp(sig9, outsig, sizeof(sig9)) != 0) {
         printf("Calculated sig and expected sig differ (9)\n");
-        return 0;
+        goto err;
+    }
+
+    ED448ph_sign(outsig, dohash(hashctx, phmsg1, sizeof(phmsg1)), phpubkey1,
+                 phprivkey1, NULL, 0);
+    if (memcmp(phsig1, outsig, sizeof(phsig1)) != 0) {
+        printf("Calculated sig and expected sig differ (ph 1)\n");
+        goto err;
+    }
+
+    ED448ph_sign(outsig, dohash(hashctx, phmsg2, sizeof(phmsg2)), phpubkey2,
+                 phprivkey2, phcontext2, sizeof(phcontext2));
+    if (memcmp(phsig2, outsig, sizeof(phsig2)) != 0) {
+        printf("Calculated sig and expected sig differ (ph 2)\n");
+        goto err;
     }
 
-    return 1;
+    ret = 1;
+ err:
+    EVP_MD_CTX_free(hashctx);
+    return ret;
 }
 
 int main(int argc, char *argv[])
@@ -562,7 +690,7 @@ int main(int argc, char *argv[])
     int j = -1;
 
     if (argc != 1 && (argc != 2 || strcmp(argv[1], "-f") != 0)) {
-        printf ("Usage: curve448_test [-f]\n");
+        printf("Usage: curve448_test [-f]\n");
         return 1;
     }
 
@@ -604,8 +732,9 @@ int main(int argc, char *argv[])
         if (i == 1 || i == 1000 || i == 1000000) {
             j++;
             if (memcmp(out, out_u3[j], sizeof(out)) != 0) {
-                printf("Calculated output and expected output differ (3, %ud)\n",
-                       i);
+                printf
+                    ("Calculated output and expected output differ (3, %ud)\n",
+                     i);
                 return 1;
             }
         }