box_curve25519xchacha20poly1305.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include <limits.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "core.h"
  6. #include "crypto_box_curve25519xchacha20poly1305.h"
  7. #include "crypto_core_hchacha20.h"
  8. #include "crypto_hash_sha512.h"
  9. #include "crypto_scalarmult_curve25519.h"
  10. #include "crypto_secretbox_xchacha20poly1305.h"
  11. #include "private/common.h"
  12. #include "randombytes.h"
  13. #include "utils.h"
  14. int
  15. crypto_box_curve25519xchacha20poly1305_seed_keypair(unsigned char *pk,
  16. unsigned char *sk,
  17. const unsigned char *seed)
  18. {
  19. unsigned char hash[64];
  20. crypto_hash_sha512(hash, seed, 32);
  21. memcpy(sk, hash, 32);
  22. sodium_memzero(hash, sizeof hash);
  23. return crypto_scalarmult_curve25519_base(pk, sk);
  24. }
  25. int
  26. crypto_box_curve25519xchacha20poly1305_keypair(unsigned char *pk,
  27. unsigned char *sk)
  28. {
  29. randombytes_buf(sk, 32);
  30. return crypto_scalarmult_curve25519_base(pk, sk);
  31. }
  32. int
  33. crypto_box_curve25519xchacha20poly1305_beforenm(unsigned char *k,
  34. const unsigned char *pk,
  35. const unsigned char *sk)
  36. {
  37. static const unsigned char zero[16] = { 0 };
  38. unsigned char s[32];
  39. if (crypto_scalarmult_curve25519(s, sk, pk) != 0) {
  40. return -1;
  41. }
  42. return crypto_core_hchacha20(k, zero, s, NULL);
  43. }
  44. int
  45. crypto_box_curve25519xchacha20poly1305_detached_afternm(
  46. unsigned char *c, unsigned char *mac, const unsigned char *m,
  47. unsigned long long mlen, const unsigned char *n, const unsigned char *k)
  48. {
  49. return crypto_secretbox_xchacha20poly1305_detached(c, mac, m, mlen, n, k);
  50. }
  51. int
  52. crypto_box_curve25519xchacha20poly1305_detached(
  53. unsigned char *c, unsigned char *mac, const unsigned char *m,
  54. unsigned long long mlen, const unsigned char *n, const unsigned char *pk,
  55. const unsigned char *sk)
  56. {
  57. unsigned char k[crypto_box_curve25519xchacha20poly1305_BEFORENMBYTES];
  58. int ret;
  59. COMPILER_ASSERT(crypto_box_curve25519xchacha20poly1305_BEFORENMBYTES >=
  60. crypto_secretbox_xchacha20poly1305_KEYBYTES);
  61. if (crypto_box_curve25519xchacha20poly1305_beforenm(k, pk, sk) != 0) {
  62. return -1;
  63. }
  64. ret = crypto_box_curve25519xchacha20poly1305_detached_afternm(c, mac, m,
  65. mlen, n, k);
  66. sodium_memzero(k, sizeof k);
  67. return ret;
  68. }
  69. int
  70. crypto_box_curve25519xchacha20poly1305_easy_afternm(unsigned char *c,
  71. const unsigned char *m,
  72. unsigned long long mlen,
  73. const unsigned char *n,
  74. const unsigned char *k)
  75. {
  76. if (mlen > crypto_box_curve25519xchacha20poly1305_MESSAGEBYTES_MAX) {
  77. sodium_misuse();
  78. }
  79. return crypto_box_curve25519xchacha20poly1305_detached_afternm(
  80. c + crypto_box_curve25519xchacha20poly1305_MACBYTES, c, m, mlen, n, k);
  81. }
  82. int
  83. crypto_box_curve25519xchacha20poly1305_easy(
  84. unsigned char *c, const unsigned char *m, unsigned long long mlen,
  85. const unsigned char *n, const unsigned char *pk, const unsigned char *sk)
  86. {
  87. if (mlen > crypto_box_curve25519xchacha20poly1305_MESSAGEBYTES_MAX) {
  88. sodium_misuse();
  89. }
  90. return crypto_box_curve25519xchacha20poly1305_detached(
  91. c + crypto_box_curve25519xchacha20poly1305_MACBYTES, c, m, mlen, n, pk,
  92. sk);
  93. }
  94. int
  95. crypto_box_curve25519xchacha20poly1305_open_detached_afternm(
  96. unsigned char *m, const unsigned char *c, const unsigned char *mac,
  97. unsigned long long clen, const unsigned char *n, const unsigned char *k)
  98. {
  99. return crypto_secretbox_xchacha20poly1305_open_detached(m, c, mac, clen, n,
  100. k);
  101. }
  102. int
  103. crypto_box_curve25519xchacha20poly1305_open_detached(
  104. unsigned char *m, const unsigned char *c, const unsigned char *mac,
  105. unsigned long long clen, const unsigned char *n, const unsigned char *pk,
  106. const unsigned char *sk)
  107. {
  108. unsigned char k[crypto_box_curve25519xchacha20poly1305_BEFORENMBYTES];
  109. int ret;
  110. if (crypto_box_curve25519xchacha20poly1305_beforenm(k, pk, sk) != 0) {
  111. return -1;
  112. }
  113. ret = crypto_box_curve25519xchacha20poly1305_open_detached_afternm(
  114. m, c, mac, clen, n, k);
  115. sodium_memzero(k, sizeof k);
  116. return ret;
  117. }
  118. int
  119. crypto_box_curve25519xchacha20poly1305_open_easy_afternm(
  120. unsigned char *m, const unsigned char *c, unsigned long long clen,
  121. const unsigned char *n, const unsigned char *k)
  122. {
  123. if (clen < crypto_box_curve25519xchacha20poly1305_MACBYTES) {
  124. return -1;
  125. }
  126. return crypto_box_curve25519xchacha20poly1305_open_detached_afternm(
  127. m, c + crypto_box_curve25519xchacha20poly1305_MACBYTES, c,
  128. clen - crypto_box_curve25519xchacha20poly1305_MACBYTES, n, k);
  129. }
  130. int
  131. crypto_box_curve25519xchacha20poly1305_open_easy(
  132. unsigned char *m, const unsigned char *c, unsigned long long clen,
  133. const unsigned char *n, const unsigned char *pk, const unsigned char *sk)
  134. {
  135. if (clen < crypto_box_curve25519xchacha20poly1305_MACBYTES) {
  136. return -1;
  137. }
  138. return crypto_box_curve25519xchacha20poly1305_open_detached(
  139. m, c + crypto_box_curve25519xchacha20poly1305_MACBYTES, c,
  140. clen - crypto_box_curve25519xchacha20poly1305_MACBYTES, n, pk, sk);
  141. }
  142. size_t
  143. crypto_box_curve25519xchacha20poly1305_seedbytes(void)
  144. {
  145. return crypto_box_curve25519xchacha20poly1305_SEEDBYTES;
  146. }
  147. size_t
  148. crypto_box_curve25519xchacha20poly1305_publickeybytes(void)
  149. {
  150. return crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES;
  151. }
  152. size_t
  153. crypto_box_curve25519xchacha20poly1305_secretkeybytes(void)
  154. {
  155. return crypto_box_curve25519xchacha20poly1305_SECRETKEYBYTES;
  156. }
  157. size_t
  158. crypto_box_curve25519xchacha20poly1305_beforenmbytes(void)
  159. {
  160. return crypto_box_curve25519xchacha20poly1305_BEFORENMBYTES;
  161. }
  162. size_t
  163. crypto_box_curve25519xchacha20poly1305_noncebytes(void)
  164. {
  165. return crypto_box_curve25519xchacha20poly1305_NONCEBYTES;
  166. }
  167. size_t
  168. crypto_box_curve25519xchacha20poly1305_macbytes(void)
  169. {
  170. return crypto_box_curve25519xchacha20poly1305_MACBYTES;
  171. }
  172. size_t
  173. crypto_box_curve25519xchacha20poly1305_messagebytes_max(void)
  174. {
  175. return crypto_box_curve25519xchacha20poly1305_MESSAGEBYTES_MAX;
  176. }