sign.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 <stdlib.h>
  17. #include <string.h>
  18. #include "sign.h"
  19. int
  20. msg_sign(Msg *msg, const Sign_skey their_skey)
  21. {
  22. unsigned long long sign_len = msg->len + crypto_sign_BYTES;
  23. unsigned char *signed_buf = malloc(sign_len);
  24. if (!signed_buf)
  25. return -1;
  26. if (crypto_sign(signed_buf, &sign_len, msg->buf,
  27. msg->len, their_skey)) {
  28. free(signed_buf);
  29. return -1;
  30. }
  31. msg_dispose(msg);
  32. msg_send_init(msg, sign_len, signed_buf, 1);
  33. return 0;
  34. }
  35. int
  36. msg_sign_open(Msg *msg, const Sign_pkey their_pkey)
  37. {
  38. unsigned char *unsigned_buf;
  39. unsigned long long unsign_len;
  40. if (msg->len < crypto_sign_BYTES)
  41. return -1;
  42. if (!(unsigned_buf = malloc(msg->len - crypto_sign_BYTES)))
  43. return -1;
  44. if (crypto_sign_open(unsigned_buf, &unsign_len,
  45. msg->buf, msg->len, their_pkey)) {
  46. free(unsigned_buf);
  47. return -1;
  48. }
  49. msg_dispose(msg);
  50. msg_send_init(msg, unsign_len, unsigned_buf, 1);
  51. return 0;
  52. }
  53. int
  54. signer_add(Msg *msg, const Sign_pkey my_pkey)
  55. {
  56. size_t len = msg->len + sizeof(Sign_pkey);
  57. unsigned char *buf = realloc(msg->buf, len);
  58. if (!buf)
  59. return -1;
  60. memcpy(buf + msg->len, my_pkey, sizeof(Sign_pkey));
  61. msg->buf = buf;
  62. msg->len = len;
  63. return 0;
  64. }
  65. int
  66. signer_remove(Msg *msg, Sign_pkey their_pkey)
  67. {
  68. if (signer_get(msg, their_pkey) < 0)
  69. return -1;
  70. msg->len -= sizeof(Sign_pkey);
  71. return 0;
  72. }
  73. int
  74. signer_get(const Msg *msg, Sign_pkey their_pkey)
  75. {
  76. if (msg->len < sizeof(Sign_pkey))
  77. return -1;
  78. if (their_pkey)
  79. memcpy(their_pkey,
  80. (char *) msg->buf + msg->len - sizeof(Sign_pkey),
  81. sizeof(Sign_pkey));
  82. return 0;
  83. }
  84. int
  85. signer_open(Msg *msg, Sign_pkey their_pkey)
  86. {
  87. Err err;
  88. Msg copy;
  89. Sign_pkey tmp;
  90. if (!their_pkey)
  91. their_pkey = tmp;
  92. if (msg_copy(&copy, msg, &err) < 0)
  93. return -1;
  94. if (signer_remove(&copy, their_pkey) < 0 ||
  95. msg_sign_open(&copy, their_pkey)) {
  96. msg_dispose(&copy);
  97. return -1;
  98. }
  99. msg_dispose(msg);
  100. *msg = copy;
  101. return 0;
  102. }