seal.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "seal.h"
  18. int
  19. msg_seal(Msg *msg, const Box_pkey their_pkey)
  20. {
  21. size_t seal_len = msg->len + crypto_box_SEALBYTES;
  22. unsigned char *sealed = malloc(seal_len);
  23. if (!sealed)
  24. return -1;
  25. if (crypto_box_seal(sealed, msg->buf, msg->len, their_pkey)) {
  26. free(sealed);
  27. return -1;
  28. }
  29. msg_dispose(msg);
  30. msg_send_init(msg, seal_len, sealed, 1);
  31. return 0;
  32. }
  33. int
  34. msg_unseal(Msg *msg, const Box_skey my_skey, const Box_pkey my_pkey)
  35. {
  36. unsigned char *unsealed;
  37. ssize_t unseal_len = (ssize_t) msg->len -
  38. (ssize_t) crypto_box_SEALBYTES;
  39. if (unseal_len < 0)
  40. return -1;
  41. if (!(unsealed = malloc(unseal_len)))
  42. return -1;
  43. if (crypto_box_seal_open(unsealed, msg->buf,
  44. msg->len, my_pkey, my_skey)) {
  45. free(unsealed);
  46. return -1;
  47. }
  48. msg_dispose(msg);
  49. msg_send_init(msg, unseal_len, unsealed, 1);
  50. return 0;
  51. }
  52. int
  53. unsealer_add(Msg *msg, const Box_pkey their_pkey)
  54. {
  55. size_t len = msg->len + sizeof(Box_pkey);
  56. unsigned char *buf = realloc(msg->buf, len);
  57. if (!buf)
  58. return -1;
  59. memcpy(buf + msg->len, their_pkey, sizeof(Box_pkey));
  60. msg->buf = buf;
  61. msg->len = len;
  62. return 0;
  63. }
  64. int
  65. unsealer_remove(Msg *msg, Box_pkey their_pkey)
  66. {
  67. if (unsealer_get(msg, their_pkey) < 0)
  68. return -1;
  69. msg->len -= sizeof(Box_pkey);
  70. return 0;
  71. }
  72. int
  73. unsealer_get(const Msg *msg, Box_pkey their_pkey)
  74. {
  75. if (msg->len < sizeof(Box_pkey))
  76. return -1;
  77. if (their_pkey)
  78. memcpy(their_pkey,
  79. (char *) msg->buf + msg->len - sizeof(Box_pkey),
  80. sizeof(Box_pkey));
  81. return 0;
  82. }
  83. int
  84. unsealer_seal(Msg *msg, const Box_pkey their_pkey)
  85. {
  86. Err err;
  87. Msg copy;
  88. if (msg_copy(&copy, msg, &err) < 0)
  89. return -1;
  90. if (msg_seal(&copy, their_pkey) < 0 ||
  91. unsealer_add(msg, their_pkey) < 0) {
  92. msg_dispose(&copy);
  93. return -1;
  94. }
  95. msg_dispose(msg);
  96. *msg = copy;
  97. return 0;
  98. }