box.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* This file is part of libmissive.
  2. *
  3. * libmissive is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Lesser General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * libmissive is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public License
  14. * along with libmissive. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <string.h>
  17. #include "box.h"
  18. int
  19. msg_box(Msg *msg, const Nonce nonce,
  20. const Box_skey my_skey, const Box_pkey their_pkey)
  21. {
  22. size_t box_len = msg->len + crypto_box_MACBYTES;
  23. unsigned char *boxed = malloc(box_len);
  24. if (!boxed)
  25. return -1;
  26. if (crypto_box_easy(boxed, msg->buf, msg->len,
  27. nonce, their_pkey, my_skey)) {
  28. free(boxed);
  29. return -1;
  30. }
  31. msg_dispose(msg);
  32. msg_send_init(msg, box_len, boxed, 1);
  33. return 0;
  34. }
  35. int
  36. msg_unbox(Msg *msg, const Nonce nonce,
  37. const Box_skey my_skey, const Box_pkey their_pkey)
  38. {
  39. ssize_t unbox_len = (ssize_t) msg->len - (ssize_t) crypto_box_MACBYTES;
  40. unsigned char *unboxed;
  41. if (unbox_len < 0)
  42. return -1;
  43. if (!(unboxed = malloc(unbox_len)))
  44. return -1;
  45. if (crypto_box_open_easy(unboxed, msg->buf, msg->len,
  46. nonce, their_pkey, my_skey)) {
  47. free(unboxed);
  48. return -1;
  49. }
  50. msg_dispose(msg);
  51. msg_send_init(msg, unbox_len, unboxed, 1);
  52. return 0;
  53. }
  54. int
  55. boxinfo_add(Msg *msg, const Nonce nonce, const Box_pkey my_pkey)
  56. {
  57. size_t len = msg->len + sizeof(Nonce) + sizeof(Box_pkey);
  58. unsigned char *buf = realloc(msg->buf, len);
  59. if (!buf)
  60. return -1;
  61. memcpy(buf + msg->len, nonce, sizeof(Nonce));
  62. memcpy(buf + msg->len + sizeof(Nonce), my_pkey, sizeof(Box_pkey));
  63. msg->buf = buf;
  64. msg->len = len;
  65. return 0;
  66. }
  67. int
  68. boxinfo_remove(Msg *msg, Nonce nonce, Box_pkey their_pkey)
  69. {
  70. if (boxinfo_get(msg, nonce, their_pkey) < 0)
  71. return -1;
  72. msg->len -= sizeof(Nonce) + sizeof(Box_pkey);
  73. return 0;
  74. }
  75. int
  76. boxinfo_get(Msg *msg, Nonce nonce, Box_pkey their_pkey)
  77. {
  78. if (msg->len < sizeof(Nonce) + sizeof(Box_pkey))
  79. return -1;
  80. if (nonce)
  81. memcpy(nonce,
  82. (char *) msg->buf + msg->len -
  83. (sizeof(Box_pkey) + sizeof(Nonce)),
  84. sizeof(Nonce));
  85. if (their_pkey)
  86. memcpy(their_pkey,
  87. (char *) msg->buf + msg->len - sizeof(Box_pkey),
  88. sizeof(Box_pkey));
  89. return 0;
  90. }
  91. int
  92. boxinfo_box(Msg *msg, const Nonce nonce, const Box_skey my_skey,
  93. const Box_pkey my_pkey, const Box_pkey their_pkey)
  94. {
  95. Err err;
  96. Msg copy;
  97. if (msg_copy(&copy, msg, &err) < 0)
  98. return -1;
  99. if (msg_box(&copy, nonce, my_skey, their_pkey) < 0 ||
  100. boxinfo_add(&copy, nonce, my_pkey) < 0) {
  101. msg_dispose(&copy);
  102. return -1;
  103. }
  104. msg_dispose(msg);
  105. *msg = copy;
  106. return 0;
  107. }
  108. int
  109. boxinfo_unbox(Msg *msg, Nonce nonce,
  110. const Box_skey my_skey, Box_pkey their_pkey)
  111. {
  112. Err err;
  113. Msg copy;
  114. Nonce tmp_nonce;
  115. Box_pkey tmp_pkey;
  116. if (msg_copy(&copy, msg, &err) < 0)
  117. return -1;
  118. if (!nonce)
  119. nonce = tmp_nonce;
  120. if (!their_pkey)
  121. their_pkey = tmp_pkey;
  122. if (boxinfo_remove(&copy, nonce, their_pkey) < 0 ||
  123. boxinfo_unbox(&copy, nonce, my_skey, their_pkey) < 0) {
  124. msg_dispose(&copy);
  125. return -1;
  126. }
  127. msg_dispose(msg);
  128. *msg = copy;
  129. return 0;
  130. }