2
0

eddsa.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2015-2016 Cryptography Research, Inc.
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. *
  10. * Originally written by Mike Hamburg
  11. */
  12. #include <string.h>
  13. #include <openssl/crypto.h>
  14. #include <openssl/evp.h>
  15. #include "curve448_lcl.h"
  16. #include "word.h"
  17. #include "ed448.h"
  18. #include "internal/numbers.h"
  19. #define COFACTOR 4
  20. static c448_error_t oneshot_hash(uint8_t *out, size_t outlen,
  21. const uint8_t *in, size_t inlen)
  22. {
  23. EVP_MD_CTX *hashctx = EVP_MD_CTX_new();
  24. if (hashctx == NULL)
  25. return C448_FAILURE;
  26. if (!EVP_DigestInit_ex(hashctx, EVP_shake256(), NULL)
  27. || !EVP_DigestUpdate(hashctx, in, inlen)
  28. || !EVP_DigestFinalXOF(hashctx, out, outlen)) {
  29. EVP_MD_CTX_free(hashctx);
  30. return C448_FAILURE;
  31. }
  32. EVP_MD_CTX_free(hashctx);
  33. return C448_SUCCESS;
  34. }
  35. static void clamp(uint8_t secret_scalar_ser[EDDSA_448_PRIVATE_BYTES])
  36. {
  37. secret_scalar_ser[0] &= -COFACTOR;
  38. secret_scalar_ser[EDDSA_448_PRIVATE_BYTES - 1] = 0;
  39. secret_scalar_ser[EDDSA_448_PRIVATE_BYTES - 2] |= 0x80;
  40. }
  41. static c448_error_t hash_init_with_dom(EVP_MD_CTX *hashctx, uint8_t prehashed,
  42. uint8_t for_prehash,
  43. const uint8_t *context,
  44. size_t context_len)
  45. {
  46. const char *dom_s = "SigEd448";
  47. uint8_t dom[2];
  48. if (context_len > UINT8_MAX)
  49. return C448_FAILURE;
  50. dom[0] = (uint8_t)(2 - (prehashed == 0 ? 1 : 0)
  51. - (for_prehash == 0 ? 1 : 0));
  52. dom[1] = (uint8_t)context_len;
  53. if (!EVP_DigestInit_ex(hashctx, EVP_shake256(), NULL)
  54. || !EVP_DigestUpdate(hashctx, dom_s, strlen(dom_s))
  55. || !EVP_DigestUpdate(hashctx, dom, sizeof(dom))
  56. || (context_len > 0
  57. && !EVP_DigestUpdate(hashctx, context, context_len)))
  58. return C448_FAILURE;
  59. return C448_SUCCESS;
  60. }
  61. /* In this file because it uses the hash */
  62. c448_error_t c448_ed448_convert_private_key_to_x448(
  63. uint8_t x[X448_PRIVATE_BYTES],
  64. const uint8_t ed [EDDSA_448_PRIVATE_BYTES])
  65. {
  66. /* pass the private key through oneshot_hash function */
  67. /* and keep the first X448_PRIVATE_BYTES bytes */
  68. return oneshot_hash(x, X448_PRIVATE_BYTES, ed,
  69. EDDSA_448_PRIVATE_BYTES);
  70. }
  71. c448_error_t c448_ed448_derive_public_key(
  72. uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
  73. const uint8_t privkey[EDDSA_448_PRIVATE_BYTES])
  74. {
  75. /* only this much used for keygen */
  76. uint8_t secret_scalar_ser[EDDSA_448_PRIVATE_BYTES];
  77. curve448_scalar_t secret_scalar;
  78. unsigned int c;
  79. curve448_point_t p;
  80. if (!oneshot_hash(secret_scalar_ser, sizeof(secret_scalar_ser), privkey,
  81. EDDSA_448_PRIVATE_BYTES))
  82. return C448_FAILURE;
  83. clamp(secret_scalar_ser);
  84. curve448_scalar_decode_long(secret_scalar, secret_scalar_ser,
  85. sizeof(secret_scalar_ser));
  86. /*
  87. * Since we are going to mul_by_cofactor during encoding, divide by it
  88. * here. However, the EdDSA base point is not the same as the decaf base
  89. * point if the sigma isogeny is in use: the EdDSA base point is on
  90. * Etwist_d/(1-d) and the decaf base point is on Etwist_d, and when
  91. * converted it effectively picks up a factor of 2 from the isogenies. So
  92. * we might start at 2 instead of 1.
  93. */
  94. for (c = 1; c < C448_EDDSA_ENCODE_RATIO; c <<= 1)
  95. curve448_scalar_halve(secret_scalar, secret_scalar);
  96. curve448_precomputed_scalarmul(p, curve448_precomputed_base, secret_scalar);
  97. curve448_point_mul_by_ratio_and_encode_like_eddsa(pubkey, p);
  98. /* Cleanup */
  99. curve448_scalar_destroy(secret_scalar);
  100. curve448_point_destroy(p);
  101. OPENSSL_cleanse(secret_scalar_ser, sizeof(secret_scalar_ser));
  102. return C448_SUCCESS;
  103. }
  104. c448_error_t c448_ed448_sign(
  105. uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
  106. const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
  107. const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
  108. const uint8_t *message, size_t message_len,
  109. uint8_t prehashed, const uint8_t *context,
  110. size_t context_len)
  111. {
  112. curve448_scalar_t secret_scalar;
  113. EVP_MD_CTX *hashctx = EVP_MD_CTX_new();
  114. c448_error_t ret = C448_FAILURE;
  115. curve448_scalar_t nonce_scalar;
  116. uint8_t nonce_point[EDDSA_448_PUBLIC_BYTES] = { 0 };
  117. unsigned int c;
  118. curve448_scalar_t challenge_scalar;
  119. if (hashctx == NULL)
  120. return C448_FAILURE;
  121. {
  122. /*
  123. * Schedule the secret key, First EDDSA_448_PRIVATE_BYTES is serialised
  124. * secret scalar,next EDDSA_448_PRIVATE_BYTES bytes is the seed.
  125. */
  126. uint8_t expanded[EDDSA_448_PRIVATE_BYTES * 2];
  127. if (!oneshot_hash(expanded, sizeof(expanded), privkey,
  128. EDDSA_448_PRIVATE_BYTES))
  129. goto err;
  130. clamp(expanded);
  131. curve448_scalar_decode_long(secret_scalar, expanded,
  132. EDDSA_448_PRIVATE_BYTES);
  133. /* Hash to create the nonce */
  134. if (!hash_init_with_dom(hashctx, prehashed, 0, context, context_len)
  135. || !EVP_DigestUpdate(hashctx,
  136. expanded + EDDSA_448_PRIVATE_BYTES,
  137. EDDSA_448_PRIVATE_BYTES)
  138. || (message_len > 0
  139. && !EVP_DigestUpdate(hashctx, message, message_len))) {
  140. OPENSSL_cleanse(expanded, sizeof(expanded));
  141. goto err;
  142. }
  143. OPENSSL_cleanse(expanded, sizeof(expanded));
  144. }
  145. /* Decode the nonce */
  146. {
  147. uint8_t nonce[2 * EDDSA_448_PRIVATE_BYTES];
  148. if (!EVP_DigestFinalXOF(hashctx, nonce, sizeof(nonce)))
  149. goto err;
  150. curve448_scalar_decode_long(nonce_scalar, nonce, sizeof(nonce));
  151. OPENSSL_cleanse(nonce, sizeof(nonce));
  152. }
  153. {
  154. /* Scalarmul to create the nonce-point */
  155. curve448_scalar_t nonce_scalar_2;
  156. curve448_point_t p;
  157. curve448_scalar_halve(nonce_scalar_2, nonce_scalar);
  158. for (c = 2; c < C448_EDDSA_ENCODE_RATIO; c <<= 1)
  159. curve448_scalar_halve(nonce_scalar_2, nonce_scalar_2);
  160. curve448_precomputed_scalarmul(p, curve448_precomputed_base,
  161. nonce_scalar_2);
  162. curve448_point_mul_by_ratio_and_encode_like_eddsa(nonce_point, p);
  163. curve448_point_destroy(p);
  164. curve448_scalar_destroy(nonce_scalar_2);
  165. }
  166. {
  167. uint8_t challenge[2 * EDDSA_448_PRIVATE_BYTES];
  168. /* Compute the challenge */
  169. if (!hash_init_with_dom(hashctx, prehashed, 0, context, context_len)
  170. || !EVP_DigestUpdate(hashctx, nonce_point, sizeof(nonce_point))
  171. || !EVP_DigestUpdate(hashctx, pubkey, EDDSA_448_PUBLIC_BYTES)
  172. || (message_len > 0
  173. && !EVP_DigestUpdate(hashctx, message, message_len))
  174. || !EVP_DigestFinalXOF(hashctx, challenge, sizeof(challenge)))
  175. goto err;
  176. curve448_scalar_decode_long(challenge_scalar, challenge,
  177. sizeof(challenge));
  178. OPENSSL_cleanse(challenge, sizeof(challenge));
  179. }
  180. curve448_scalar_mul(challenge_scalar, challenge_scalar, secret_scalar);
  181. curve448_scalar_add(challenge_scalar, challenge_scalar, nonce_scalar);
  182. OPENSSL_cleanse(signature, EDDSA_448_SIGNATURE_BYTES);
  183. memcpy(signature, nonce_point, sizeof(nonce_point));
  184. curve448_scalar_encode(&signature[EDDSA_448_PUBLIC_BYTES],
  185. challenge_scalar);
  186. curve448_scalar_destroy(secret_scalar);
  187. curve448_scalar_destroy(nonce_scalar);
  188. curve448_scalar_destroy(challenge_scalar);
  189. ret = C448_SUCCESS;
  190. err:
  191. EVP_MD_CTX_free(hashctx);
  192. return ret;
  193. }
  194. c448_error_t c448_ed448_sign_prehash(
  195. uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
  196. const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
  197. const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
  198. const uint8_t hash[64], const uint8_t *context,
  199. size_t context_len)
  200. {
  201. return c448_ed448_sign(signature, privkey, pubkey, hash, 64, 1, context,
  202. context_len);
  203. }
  204. c448_error_t c448_ed448_verify(
  205. const uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
  206. const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
  207. const uint8_t *message, size_t message_len,
  208. uint8_t prehashed, const uint8_t *context,
  209. uint8_t context_len)
  210. {
  211. curve448_point_t pk_point, r_point;
  212. c448_error_t error =
  213. curve448_point_decode_like_eddsa_and_mul_by_ratio(pk_point, pubkey);
  214. curve448_scalar_t challenge_scalar;
  215. curve448_scalar_t response_scalar;
  216. if (C448_SUCCESS != error)
  217. return error;
  218. error =
  219. curve448_point_decode_like_eddsa_and_mul_by_ratio(r_point, signature);
  220. if (C448_SUCCESS != error)
  221. return error;
  222. {
  223. /* Compute the challenge */
  224. EVP_MD_CTX *hashctx = EVP_MD_CTX_new();
  225. uint8_t challenge[2 * EDDSA_448_PRIVATE_BYTES];
  226. if (hashctx == NULL
  227. || !hash_init_with_dom(hashctx, prehashed, 0, context,
  228. context_len)
  229. || !EVP_DigestUpdate(hashctx, signature, EDDSA_448_PUBLIC_BYTES)
  230. || !EVP_DigestUpdate(hashctx, pubkey, EDDSA_448_PUBLIC_BYTES)
  231. || !EVP_DigestUpdate(hashctx, message, message_len)
  232. || !EVP_DigestFinalXOF(hashctx, challenge, sizeof(challenge))) {
  233. EVP_MD_CTX_free(hashctx);
  234. return C448_FAILURE;
  235. }
  236. EVP_MD_CTX_free(hashctx);
  237. curve448_scalar_decode_long(challenge_scalar, challenge,
  238. sizeof(challenge));
  239. OPENSSL_cleanse(challenge, sizeof(challenge));
  240. }
  241. curve448_scalar_sub(challenge_scalar, curve448_scalar_zero,
  242. challenge_scalar);
  243. curve448_scalar_decode_long(response_scalar,
  244. &signature[EDDSA_448_PUBLIC_BYTES],
  245. EDDSA_448_PRIVATE_BYTES);
  246. /* pk_point = -c(x(P)) + (cx + k)G = kG */
  247. curve448_base_double_scalarmul_non_secret(pk_point,
  248. response_scalar,
  249. pk_point, challenge_scalar);
  250. return c448_succeed_if(curve448_point_eq(pk_point, r_point));
  251. }
  252. c448_error_t c448_ed448_verify_prehash(
  253. const uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
  254. const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
  255. const uint8_t hash[64], const uint8_t *context,
  256. uint8_t context_len)
  257. {
  258. return c448_ed448_verify(signature, pubkey, hash, 64, 1, context,
  259. context_len);
  260. }
  261. int ED448_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
  262. const uint8_t public_key[57], const uint8_t private_key[57],
  263. const uint8_t *context, size_t context_len)
  264. {
  265. return c448_ed448_sign(out_sig, private_key, public_key, message,
  266. message_len, 0, context, context_len)
  267. == C448_SUCCESS;
  268. }
  269. int ED448_verify(const uint8_t *message, size_t message_len,
  270. const uint8_t signature[114], const uint8_t public_key[57],
  271. const uint8_t *context, size_t context_len)
  272. {
  273. return c448_ed448_verify(signature, public_key, message, message_len, 0,
  274. context, (uint8_t)context_len) == C448_SUCCESS;
  275. }
  276. int ED448ph_sign(uint8_t *out_sig, const uint8_t hash[64],
  277. const uint8_t public_key[57], const uint8_t private_key[57],
  278. const uint8_t *context, size_t context_len)
  279. {
  280. return c448_ed448_sign_prehash(out_sig, private_key, public_key, hash,
  281. context, context_len) == C448_SUCCESS;
  282. }
  283. int ED448ph_verify(const uint8_t hash[64], const uint8_t signature[114],
  284. const uint8_t public_key[57], const uint8_t *context,
  285. size_t context_len)
  286. {
  287. return c448_ed448_verify_prehash(signature, public_key, hash, context,
  288. (uint8_t)context_len) == C448_SUCCESS;
  289. }
  290. int ED448_public_from_private(uint8_t out_public_key[57],
  291. const uint8_t private_key[57])
  292. {
  293. return c448_ed448_derive_public_key(out_public_key, private_key)
  294. == C448_SUCCESS;
  295. }