Simplify some code
[openssl.git] / crypto / ec / curve448 / eddsa.c
index aa182cdbe7d5681a7cfe9bed6ff14169d364a041..71757157036143a2e2c0c18685de1e57dfc90158 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
  * Copyright 2015-2016 Cryptography Research, Inc.
  *
  * Licensed under the OpenSSL license (the "License").  You may not use
@@ -9,13 +9,12 @@
  *
  * Originally written by Mike Hamburg
  */
+#include <string.h>
 #include <openssl/crypto.h>
 #include <openssl/evp.h>
-
 #include "curve448_lcl.h"
 #include "word.h"
 #include "ed448.h"
-#include <string.h>
 #include "internal/numbers.h"
 
 #define COFACTOR 4
@@ -29,8 +28,8 @@ static c448_error_t oneshot_hash(uint8_t *out, size_t outlen,
         return C448_FAILURE;
 
     if (!EVP_DigestInit_ex(hashctx, EVP_shake256(), NULL)
-        || !EVP_DigestUpdate(hashctx, in, inlen)
-        || !EVP_DigestFinalXOF(hashctx, out, outlen)) {
+            || !EVP_DigestUpdate(hashctx, in, inlen)
+            || !EVP_DigestFinalXOF(hashctx, out, outlen)) {
         EVP_MD_CTX_free(hashctx);
         return C448_FAILURE;
     }
@@ -41,17 +40,9 @@ static c448_error_t oneshot_hash(uint8_t *out, size_t outlen,
 
 static void clamp(uint8_t secret_scalar_ser[EDDSA_448_PRIVATE_BYTES])
 {
-    uint8_t hibit = (1 << 0) >> 1;
-
-    /* Blarg */
     secret_scalar_ser[0] &= -COFACTOR;
-    if (hibit == 0) {
-        secret_scalar_ser[EDDSA_448_PRIVATE_BYTES - 1] = 0;
-        secret_scalar_ser[EDDSA_448_PRIVATE_BYTES - 2] |= 0x80;
-    } else {
-        secret_scalar_ser[EDDSA_448_PRIVATE_BYTES - 1] &= hibit - 1;
-        secret_scalar_ser[EDDSA_448_PRIVATE_BYTES - 1] |= hibit;
-    }
+    secret_scalar_ser[EDDSA_448_PRIVATE_BYTES - 1] = 0;
+    secret_scalar_ser[EDDSA_448_PRIVATE_BYTES - 2] |= 0x80;
 }
 
 static c448_error_t hash_init_with_dom(EVP_MD_CTX *hashctx, uint8_t prehashed,
@@ -62,16 +53,17 @@ static c448_error_t hash_init_with_dom(EVP_MD_CTX *hashctx, uint8_t prehashed,
     const char *dom_s = "SigEd448";
     uint8_t dom[2];
 
-    dom[0] = 2 + word_is_zero(prehashed) + word_is_zero(for_prehash);
-    dom[1] = (uint8_t)context_len;
-
     if (context_len > UINT8_MAX)
         return C448_FAILURE;
 
+    dom[0] = (uint8_t)(2 - (prehashed == 0 ? 1 : 0)
+                       - (for_prehash == 0 ? 1 : 0));
+    dom[1] = (uint8_t)context_len;
+
     if (!EVP_DigestInit_ex(hashctx, EVP_shake256(), NULL)
-        || !EVP_DigestUpdate(hashctx, dom_s, strlen(dom_s))
-        || !EVP_DigestUpdate(hashctx, dom, sizeof(dom))
-        || !EVP_DigestUpdate(hashctx, context, context_len))
+            || !EVP_DigestUpdate(hashctx, dom_s, strlen(dom_s))
+            || !EVP_DigestUpdate(hashctx, dom, sizeof(dom))
+            || !EVP_DigestUpdate(hashctx, context, context_len))
         return C448_FAILURE;
 
     return C448_SUCCESS;
@@ -150,27 +142,29 @@ c448_error_t c448_ed448_sign(
         return C448_FAILURE;
 
     {
-        /* Schedule the secret key */
-        struct {
-            uint8_t secret_scalar_ser[EDDSA_448_PRIVATE_BYTES];
-            uint8_t seed[EDDSA_448_PRIVATE_BYTES];
-        } __attribute__ ((packed)) expanded;
+        /*
+         * Schedule the secret key, First EDDSA_448_PRIVATE_BYTES is serialised
+         * secret scalar,next EDDSA_448_PRIVATE_BYTES bytes is the seed.
+         */
+        uint8_t expanded[EDDSA_448_PRIVATE_BYTES * 2];
 
-        if (!oneshot_hash((uint8_t *)&expanded, sizeof(expanded), privkey,
+        if (!oneshot_hash(expanded, sizeof(expanded), privkey,
                           EDDSA_448_PRIVATE_BYTES))
             goto err;
-        clamp(expanded.secret_scalar_ser);
-        curve448_scalar_decode_long(secret_scalar, expanded.secret_scalar_ser,
-                                    sizeof(expanded.secret_scalar_ser));
+        clamp(expanded);
+        curve448_scalar_decode_long(secret_scalar, expanded,
+                                    EDDSA_448_PRIVATE_BYTES);
 
         /* Hash to create the nonce */
         if (!hash_init_with_dom(hashctx, prehashed, 0, context, context_len)
-            || !EVP_DigestUpdate(hashctx, expanded.seed, sizeof(expanded.seed))
-            || !EVP_DigestUpdate(hashctx, message, message_len)) {
-            OPENSSL_cleanse(&expanded, sizeof(expanded));
+                || !EVP_DigestUpdate(hashctx,
+                                     expanded + EDDSA_448_PRIVATE_BYTES,
+                                     EDDSA_448_PRIVATE_BYTES)
+                || !EVP_DigestUpdate(hashctx, message, message_len)) {
+            OPENSSL_cleanse(expanded, sizeof(expanded));
             goto err;
         }
-        OPENSSL_cleanse(&expanded, sizeof(expanded));
+        OPENSSL_cleanse(expanded, sizeof(expanded));
     }
 
     /* Decode the nonce */
@@ -189,9 +183,8 @@ c448_error_t c448_ed448_sign(
         curve448_point_t p;
 
         curve448_scalar_halve(nonce_scalar_2, nonce_scalar);
-        for (c = 2; c < C448_EDDSA_ENCODE_RATIO; c <<= 1) {
+        for (c = 2; c < C448_EDDSA_ENCODE_RATIO; c <<= 1)
             curve448_scalar_halve(nonce_scalar_2, nonce_scalar_2);
-        }
 
         curve448_precomputed_scalarmul(p, curve448_precomputed_base,
                                        nonce_scalar_2);
@@ -205,10 +198,10 @@ c448_error_t c448_ed448_sign(
 
         /* Compute the challenge */
         if (!hash_init_with_dom(hashctx, prehashed, 0, context, context_len)
-            || !EVP_DigestUpdate(hashctx, nonce_point, sizeof(nonce_point))
-            || !EVP_DigestUpdate(hashctx, pubkey, EDDSA_448_PUBLIC_BYTES)
-            || !EVP_DigestUpdate(hashctx, message, message_len)
-            || !EVP_DigestFinalXOF(hashctx, challenge, sizeof(challenge)))
+                || !EVP_DigestUpdate(hashctx, nonce_point, sizeof(nonce_point))
+                || !EVP_DigestUpdate(hashctx, pubkey, EDDSA_448_PUBLIC_BYTES)
+                || !EVP_DigestUpdate(hashctx, message, message_len)
+                || !EVP_DigestFinalXOF(hashctx, challenge, sizeof(challenge)))
             goto err;
 
         curve448_scalar_decode_long(challenge_scalar, challenge,
@@ -273,12 +266,12 @@ c448_error_t c448_ed448_verify(
         uint8_t challenge[2 * EDDSA_448_PRIVATE_BYTES];
 
         if (hashctx == NULL
-            || !hash_init_with_dom(hashctx, prehashed, 0, context, context_len)
-            || !EVP_DigestUpdate(hashctx, signature,
-                                 EDDSA_448_PUBLIC_BYTES)
-            || !EVP_DigestUpdate(hashctx, pubkey, EDDSA_448_PUBLIC_BYTES)
-            || !EVP_DigestUpdate(hashctx, message, message_len)
-            || !EVP_DigestFinalXOF(hashctx, challenge, sizeof(challenge))) {
+                || !hash_init_with_dom(hashctx, prehashed, 0, context,
+                                       context_len)
+                || !EVP_DigestUpdate(hashctx, signature, EDDSA_448_PUBLIC_BYTES)
+                || !EVP_DigestUpdate(hashctx, pubkey, EDDSA_448_PUBLIC_BYTES)
+                || !EVP_DigestUpdate(hashctx, message, message_len)
+                || !EVP_DigestFinalXOF(hashctx, challenge, sizeof(challenge))) {
             EVP_MD_CTX_free(hashctx);
             return C448_FAILURE;
         }
@@ -311,19 +304,14 @@ c448_error_t c448_ed448_verify_prehash(
                     const uint8_t hash[64], const uint8_t *context,
                     uint8_t context_len)
 {
-    c448_error_t ret;
-
-    ret = c448_ed448_verify(signature, pubkey, hash, 64, 1, context,
-                            context_len);
-
-    return ret;
+    return c448_ed448_verify(signature, pubkey, hash, 64, 1, context,
+                             context_len);
 }
 
 int ED448_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
                const uint8_t public_key[57], const uint8_t private_key[57],
                const uint8_t *context, size_t context_len)
 {
-
     return c448_ed448_sign(out_sig, private_key, public_key, message,
                            message_len, 0, context, context_len)
         == C448_SUCCESS;
@@ -334,7 +322,7 @@ int ED448_verify(const uint8_t *message, size_t message_len,
                  const uint8_t *context, size_t context_len)
 {
     return c448_ed448_verify(signature, public_key, message, message_len, 0,
-                             context, context_len) == C448_SUCCESS;
+                             context, (uint8_t)context_len) == C448_SUCCESS;
 }
 
 int ED448ph_sign(uint8_t *out_sig, const uint8_t hash[64],
@@ -351,7 +339,7 @@ int ED448ph_verify(const uint8_t hash[64], const uint8_t signature[114],
                    size_t context_len)
 {
     return c448_ed448_verify_prehash(signature, public_key, hash, context,
-                                     context_len) == C448_SUCCESS;
+                                     (uint8_t)context_len) == C448_SUCCESS;
 }
 
 int ED448_public_from_private(uint8_t out_public_key[57],