You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

340 lines
11 KiB

  1. /**
  2. * @file ed448goldilocks/eddsa.c
  3. * @author Mike Hamburg
  4. *
  5. * @copyright
  6. * Copyright (c) 2015-2016 Cryptography Research, Inc. \n
  7. * Released under the MIT License. See LICENSE.txt for license information.
  8. *
  9. * @cond internal
  10. * @brief EdDSA routines.
  11. *
  12. * @warning This file was automatically generated in Python.
  13. * Please do not edit it.
  14. */
  15. #include "word.h"
  16. #include <decaf/ed448.h>
  17. #include <decaf/shake.h>
  18. #include <decaf/sha512.h>
  19. #include <string.h>
  20. #define API_NAME "decaf_448"
  21. #define API_NS(_id) decaf_448_##_id
  22. #define hash_ctx_t decaf_shake256_ctx_t
  23. #define hash_init decaf_shake256_init
  24. #define hash_update decaf_shake256_update
  25. #define hash_final decaf_shake256_final
  26. #define hash_destroy decaf_shake256_destroy
  27. #define hash_hash decaf_shake256_hash
  28. #define SUPPORTS_CONTEXTS DECAF_EDDSA_448_SUPPORTS_CONTEXTS
  29. #define EDDSA_USE_SIGMA_ISOGENY 0
  30. #define COFACTOR 4
  31. /* EDDSA_BASE_POINT_RATIO = 1 or 2
  32. * Because EdDSA25519 is not on E_d but on the isogenous E_sigma_d,
  33. * its base point is twice ours.
  34. */
  35. #define EDDSA_BASE_POINT_RATIO (1+EDDSA_USE_SIGMA_ISOGENY)
  36. static void clamp (
  37. uint8_t secret_scalar_ser[DECAF_EDDSA_448_PRIVATE_BYTES]
  38. ) {
  39. /* Blarg */
  40. secret_scalar_ser[0] &= -COFACTOR;
  41. uint8_t hibit = (1<<0)>>1;
  42. if (hibit == 0) {
  43. secret_scalar_ser[DECAF_EDDSA_448_PRIVATE_BYTES - 1] = 0;
  44. secret_scalar_ser[DECAF_EDDSA_448_PRIVATE_BYTES - 2] |= 0x80;
  45. } else {
  46. secret_scalar_ser[DECAF_EDDSA_448_PRIVATE_BYTES - 1] &= hibit-1;
  47. secret_scalar_ser[DECAF_EDDSA_448_PRIVATE_BYTES - 1] |= hibit;
  48. }
  49. }
  50. static void hash_init_with_dom(
  51. hash_ctx_t hash,
  52. uint8_t prehashed,
  53. uint8_t for_prehash,
  54. const uint8_t *context,
  55. uint8_t context_len
  56. ) {
  57. hash_init(hash);
  58. #if SUPPORTS_CONTEXTS
  59. const char *dom_s = "SigEd448";
  60. const uint8_t dom[2] = {2+word_is_zero(prehashed)+word_is_zero(for_prehash), context_len};
  61. hash_update(hash,(const unsigned char *)dom_s, strlen(dom_s));
  62. hash_update(hash,dom,2);
  63. hash_update(hash,context,context_len);
  64. #else
  65. (void)prehashed;
  66. (void)for_prehash;
  67. (void)context;
  68. assert(context==NULL);
  69. (void)context_len;
  70. assert(context_len == 0);
  71. #endif
  72. }
  73. void decaf_ed448_prehash_init (
  74. hash_ctx_t hash
  75. #if DECAF_EDDSA_448_SUPPORTS_CONTEXTS
  76. , const uint8_t *context,
  77. uint8_t context_len
  78. #endif
  79. ) {
  80. #if DECAF_EDDSA_448_SUPPORTS_CONTEXTS
  81. hash_init_with_dom(hash,1,1,context,context_len);
  82. #else
  83. hash_init_with_dom(hash,1,1,NULL,0);
  84. #endif
  85. }
  86. void decaf_ed448_derive_public_key (
  87. uint8_t pubkey[DECAF_EDDSA_448_PUBLIC_BYTES],
  88. const uint8_t privkey[DECAF_EDDSA_448_PRIVATE_BYTES]
  89. ) {
  90. /* only this much used for keygen */
  91. uint8_t secret_scalar_ser[DECAF_EDDSA_448_PRIVATE_BYTES];
  92. hash_hash(
  93. secret_scalar_ser,
  94. sizeof(secret_scalar_ser),
  95. privkey,
  96. DECAF_EDDSA_448_PRIVATE_BYTES
  97. );
  98. clamp(secret_scalar_ser);
  99. API_NS(scalar_t) secret_scalar;
  100. API_NS(scalar_decode_long)(secret_scalar, secret_scalar_ser, sizeof(secret_scalar_ser));
  101. /* Since we are going to mul_by_cofactor during encoding, divide by it here.
  102. * However, the EdDSA base point is not the same as the decaf base point if
  103. * the sigma isogeny is in use: the EdDSA base point is on Etwist_d/(1-d) and
  104. * the decaf base point is on Etwist_d, and when converted it effectively
  105. * picks up a factor of 2 from the isogenies. So we might start at 2 instead of 1.
  106. */
  107. for (unsigned int c = EDDSA_BASE_POINT_RATIO; c < COFACTOR; c <<= 1) {
  108. API_NS(scalar_halve)(secret_scalar,secret_scalar);
  109. }
  110. API_NS(point_t) p;
  111. API_NS(precomputed_scalarmul)(p,API_NS(precomputed_base),secret_scalar);
  112. API_NS(point_mul_by_cofactor_and_encode_like_eddsa)(pubkey, p);
  113. /* Cleanup */
  114. API_NS(scalar_destroy)(secret_scalar);
  115. API_NS(point_destroy)(p);
  116. decaf_bzero(secret_scalar_ser, sizeof(secret_scalar_ser));
  117. }
  118. void decaf_ed448_sign (
  119. uint8_t signature[DECAF_EDDSA_448_SIGNATURE_BYTES],
  120. const uint8_t privkey[DECAF_EDDSA_448_PRIVATE_BYTES],
  121. const uint8_t pubkey[DECAF_EDDSA_448_PUBLIC_BYTES],
  122. const uint8_t *message,
  123. size_t message_len,
  124. uint8_t prehashed
  125. #if SUPPORTS_CONTEXTS
  126. , const uint8_t *context,
  127. uint8_t context_len
  128. #endif
  129. ) {
  130. #if !SUPPORTS_CONTEXTS
  131. const uint8_t *const context = NULL;
  132. const uint8_t context_len = 0;
  133. #endif
  134. API_NS(scalar_t) secret_scalar;
  135. hash_ctx_t hash;
  136. {
  137. /* Schedule the secret key */
  138. struct {
  139. uint8_t secret_scalar_ser[DECAF_EDDSA_448_PRIVATE_BYTES];
  140. uint8_t seed[DECAF_EDDSA_448_PRIVATE_BYTES];
  141. } __attribute__((packed)) expanded;
  142. hash_hash(
  143. (uint8_t *)&expanded,
  144. sizeof(expanded),
  145. privkey,
  146. DECAF_EDDSA_448_PRIVATE_BYTES
  147. );
  148. clamp(expanded.secret_scalar_ser);
  149. API_NS(scalar_decode_long)(secret_scalar, expanded.secret_scalar_ser, sizeof(expanded.secret_scalar_ser));
  150. /* Hash to create the nonce */
  151. hash_init_with_dom(hash,prehashed,0,context,context_len);
  152. hash_update(hash,expanded.seed,sizeof(expanded.seed));
  153. hash_update(hash,message,message_len);
  154. decaf_bzero(&expanded, sizeof(expanded));
  155. }
  156. /* Decode the nonce */
  157. API_NS(scalar_t) nonce_scalar;
  158. {
  159. uint8_t nonce[2*DECAF_EDDSA_448_PRIVATE_BYTES];
  160. hash_final(hash,nonce,sizeof(nonce));
  161. API_NS(scalar_decode_long)(nonce_scalar, nonce, sizeof(nonce));
  162. decaf_bzero(nonce, sizeof(nonce));
  163. }
  164. uint8_t nonce_point[DECAF_EDDSA_448_PUBLIC_BYTES] = {0};
  165. {
  166. /* Scalarmul to create the nonce-point */
  167. API_NS(scalar_t) nonce_scalar_2;
  168. API_NS(scalar_halve)(nonce_scalar_2,nonce_scalar);
  169. for (unsigned int c = 2*EDDSA_BASE_POINT_RATIO; c < COFACTOR; c <<= 1) {
  170. API_NS(scalar_halve)(nonce_scalar_2,nonce_scalar_2);
  171. }
  172. API_NS(point_t) p;
  173. API_NS(precomputed_scalarmul)(p,API_NS(precomputed_base),nonce_scalar_2);
  174. API_NS(point_mul_by_cofactor_and_encode_like_eddsa)(nonce_point, p);
  175. API_NS(point_destroy)(p);
  176. API_NS(scalar_destroy)(nonce_scalar_2);
  177. }
  178. API_NS(scalar_t) challenge_scalar;
  179. {
  180. /* Compute the challenge */
  181. hash_init_with_dom(hash,prehashed,0,context,context_len);
  182. hash_update(hash,nonce_point,sizeof(nonce_point));
  183. hash_update(hash,pubkey,DECAF_EDDSA_448_PUBLIC_BYTES);
  184. hash_update(hash,message,message_len);
  185. uint8_t challenge[2*DECAF_EDDSA_448_PRIVATE_BYTES];
  186. hash_final(hash,challenge,sizeof(challenge));
  187. hash_destroy(hash);
  188. API_NS(scalar_decode_long)(challenge_scalar,challenge,sizeof(challenge));
  189. decaf_bzero(challenge,sizeof(challenge));
  190. }
  191. API_NS(scalar_mul)(challenge_scalar,challenge_scalar,secret_scalar);
  192. API_NS(scalar_add)(challenge_scalar,challenge_scalar,nonce_scalar);
  193. decaf_bzero(signature,DECAF_EDDSA_448_SIGNATURE_BYTES);
  194. memcpy(signature,nonce_point,sizeof(nonce_point));
  195. API_NS(scalar_encode)(&signature[DECAF_EDDSA_448_PUBLIC_BYTES],challenge_scalar);
  196. API_NS(scalar_destroy)(secret_scalar);
  197. API_NS(scalar_destroy)(nonce_scalar);
  198. API_NS(scalar_destroy)(challenge_scalar);
  199. }
  200. void decaf_ed448_sign_prehash (
  201. uint8_t signature[DECAF_EDDSA_448_SIGNATURE_BYTES],
  202. const uint8_t privkey[DECAF_EDDSA_448_PRIVATE_BYTES],
  203. const uint8_t pubkey[DECAF_EDDSA_448_PUBLIC_BYTES],
  204. const decaf_ed448_prehash_ctx_t hash
  205. #if DECAF_EDDSA_448_SUPPORTS_CONTEXTS
  206. , const uint8_t *context,
  207. uint8_t context_len
  208. #endif
  209. ) {
  210. uint8_t hash_output[64]; /* MAGIC but true for all existing schemes */
  211. {
  212. decaf_ed448_prehash_ctx_t hash_too;
  213. memcpy(hash_too,hash,sizeof(hash_too));
  214. hash_final(hash_too,hash_output,sizeof(hash_output));
  215. hash_destroy(hash_too);
  216. }
  217. #if DECAF_EDDSA_448_SUPPORTS_CONTEXTS
  218. decaf_ed448_sign(signature,privkey,pubkey,hash_output,sizeof(hash_output),1,context,context_len);
  219. #else
  220. decaf_ed448_sign(signature,privkey,pubkey,hash_output,sizeof(hash_output),1);
  221. #endif
  222. decaf_bzero(hash_output,sizeof(hash_output));
  223. }
  224. decaf_error_t decaf_ed448_verify (
  225. const uint8_t signature[DECAF_EDDSA_448_SIGNATURE_BYTES],
  226. const uint8_t pubkey[DECAF_EDDSA_448_PUBLIC_BYTES],
  227. const uint8_t *message,
  228. size_t message_len,
  229. uint8_t prehashed
  230. #if SUPPORTS_CONTEXTS
  231. , const uint8_t *context,
  232. uint8_t context_len
  233. #endif
  234. ) {
  235. #if !SUPPORTS_CONTEXTS
  236. const uint8_t *const context = NULL;
  237. const uint8_t context_len = 0;
  238. #endif
  239. API_NS(point_t) pk_point, r_point;
  240. decaf_error_t error = API_NS(point_decode_like_eddsa_and_ignore_cofactor)(pk_point,pubkey);
  241. if (DECAF_SUCCESS != error) { return error; }
  242. error = API_NS(point_decode_like_eddsa_and_ignore_cofactor)(r_point,signature);
  243. if (DECAF_SUCCESS != error) { return error; }
  244. API_NS(scalar_t) challenge_scalar;
  245. {
  246. /* Compute the challenge */
  247. hash_ctx_t hash;
  248. hash_init_with_dom(hash,prehashed,0,context,context_len);
  249. hash_update(hash,signature,DECAF_EDDSA_448_PUBLIC_BYTES);
  250. hash_update(hash,pubkey,DECAF_EDDSA_448_PUBLIC_BYTES);
  251. hash_update(hash,message,message_len);
  252. uint8_t challenge[2*DECAF_EDDSA_448_PRIVATE_BYTES];
  253. hash_final(hash,challenge,sizeof(challenge));
  254. hash_destroy(hash);
  255. API_NS(scalar_decode_long)(challenge_scalar,challenge,sizeof(challenge));
  256. decaf_bzero(challenge,sizeof(challenge));
  257. }
  258. API_NS(scalar_sub)(challenge_scalar, API_NS(scalar_zero), challenge_scalar);
  259. API_NS(scalar_t) response_scalar;
  260. API_NS(scalar_decode_long)(
  261. response_scalar,
  262. &signature[DECAF_EDDSA_448_PUBLIC_BYTES],
  263. DECAF_EDDSA_448_PRIVATE_BYTES
  264. );
  265. #if EDDSA_BASE_POINT_RATIO == 2
  266. API_NS(scalar_add)(response_scalar,response_scalar,response_scalar);
  267. #endif
  268. /* pk_point = -c(x(P)) + (cx + k)G = kG */
  269. API_NS(base_double_scalarmul_non_secret)(
  270. pk_point,
  271. response_scalar,
  272. pk_point,
  273. challenge_scalar
  274. );
  275. return decaf_succeed_if(API_NS(point_eq(pk_point,r_point)));
  276. }
  277. decaf_error_t decaf_ed448_verify_prehash (
  278. const uint8_t signature[DECAF_EDDSA_448_SIGNATURE_BYTES],
  279. const uint8_t pubkey[DECAF_EDDSA_448_PUBLIC_BYTES],
  280. const decaf_ed448_prehash_ctx_t hash
  281. #if DECAF_EDDSA_448_SUPPORTS_CONTEXTS
  282. , const uint8_t *context,
  283. uint8_t context_len
  284. #endif
  285. ) {
  286. decaf_error_t ret;
  287. uint8_t hash_output[64]; /* MAGIC but true for all existing schemes */
  288. {
  289. decaf_ed448_prehash_ctx_t hash_too;
  290. memcpy(hash_too,hash,sizeof(hash_too));
  291. hash_final(hash_too,hash_output,sizeof(hash_output));
  292. hash_destroy(hash_too);
  293. }
  294. #if DECAF_EDDSA_448_SUPPORTS_CONTEXTS
  295. ret = decaf_ed448_verify(signature,pubkey,hash_output,sizeof(hash_output),1,context,context_len);
  296. #else
  297. ret = decaf_ed448_verify(signature,pubkey,hash_output,sizeof(hash_output),1);
  298. #endif
  299. return ret;
  300. }