crypto_box_easy.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package cryptobox
  2. // #cgo pkg-config: libsodium
  3. // #include <stdlib.h>
  4. // #include <sodium.h>
  5. import "C"
  6. import support "boiler-go/locals/gosodium/support"
  7. func CryptoBoxDetachedAfterNm(mac []byte, m []byte, n []byte, k []byte) ([]byte, int) {
  8. support.CheckSize(mac, CryptoBoxMacBytes(), "mac")
  9. support.CheckSize(n, CryptoBoxNonceBytes(), "nonce")
  10. support.CheckSize(k, CryptoBoxBeforeNmBytes(), "shared secret key")
  11. c := make([]byte, len(m)+CryptoBoxMacBytes())
  12. exit := int(C.crypto_box_detached_afternm(
  13. (*C.uchar)(&c[0]),
  14. (*C.uchar)(&mac[0]),
  15. (*C.uchar)(&m[0]),
  16. (C.ulonglong)(len(m)),
  17. (*C.uchar)(&n[0]),
  18. (*C.uchar)(&k[0])))
  19. return c, exit
  20. }
  21. func CryptoBoxDetached(mac []byte, m []byte, n []byte, pk []byte, sk []byte) ([]byte, int) {
  22. support.CheckSize(mac, CryptoBoxMacBytes(), "mac")
  23. support.CheckSize(n, CryptoBoxNonceBytes(), "nonce")
  24. support.CheckSize(pk, CryptoBoxPublicKeyBytes(), "public key")
  25. support.CheckSize(sk, CryptoBoxSecretKeyBytes(), "sender's secret key")
  26. c := make([]byte, len(m)+CryptoBoxMacBytes())
  27. exit := int(C.crypto_box_detached(
  28. (*C.uchar)(&c[0]),
  29. (*C.uchar)(&mac[0]),
  30. (*C.uchar)(&m[0]),
  31. (C.ulonglong)(len(m)),
  32. (*C.uchar)(&n[0]),
  33. (*C.uchar)(&pk[0]),
  34. (*C.uchar)(&sk[0])))
  35. return c, exit
  36. }
  37. func CryptoBoxEasyAfterNm(m []byte, n []byte, k []byte) ([]byte, int) {
  38. support.CheckSize(n, CryptoBoxNonceBytes(), "nonce")
  39. support.CheckSize(k, CryptoBoxBeforeNmBytes(), "shared secret key")
  40. c := make([]byte, len(m)+CryptoBoxMacBytes())
  41. exit := int(C.crypto_box_easy_afternm(
  42. (*C.uchar)(&c[0]),
  43. (*C.uchar)(&m[0]),
  44. (C.ulonglong)(len(m)),
  45. (*C.uchar)(&n[0]),
  46. (*C.uchar)(&k[0])))
  47. return c, exit
  48. }
  49. func CryptoBoxEasy(m []byte, n []byte, pk []byte, sk []byte) ([]byte, int) {
  50. support.CheckSize(n, CryptoBoxNonceBytes(), "nonce")
  51. support.CheckSize(pk, CryptoBoxPublicKeyBytes(), "public key")
  52. support.CheckSize(sk, CryptoBoxSecretKeyBytes(), "secret key")
  53. c := make([]byte, len(m)+CryptoBoxMacBytes())
  54. exit := int(C.crypto_box_easy(
  55. (*C.uchar)(&c[0]),
  56. (*C.uchar)(&m[0]),
  57. (C.ulonglong)(len(m)),
  58. (*C.uchar)(&n[0]),
  59. (*C.uchar)(&pk[0]),
  60. (*C.uchar)(&sk[0])))
  61. return c, exit
  62. }
  63. func CryptoBoxOpenDetachedAfterNm(c []byte, mac []byte, n []byte, k []byte) ([]byte, int) {
  64. support.CheckSize(mac, CryptoBoxMacBytes(), "mac")
  65. support.CheckSize(n, CryptoBoxNonceBytes(), "nonce")
  66. support.CheckSize(k, CryptoBoxBeforeNmBytes(), "shared secret key")
  67. m := make([]byte, len(c)-CryptoBoxMacBytes())
  68. exit := int(C.crypto_box_open_detached_afternm(
  69. (*C.uchar)(&m[0]),
  70. (*C.uchar)(&c[0]),
  71. (*C.uchar)(&mac[0]),
  72. (C.ulonglong)(len(c)),
  73. (*C.uchar)(&n[0]),
  74. (*C.uchar)(&k[0])))
  75. return m, exit
  76. }
  77. func CryptoBoxOpenDetached(c []byte, mac []byte, n []byte, pk []byte, sk []byte) ([]byte, int) {
  78. support.CheckSize(mac, CryptoBoxMacBytes(), "mac")
  79. support.CheckSize(n, CryptoBoxNonceBytes(), "nonce")
  80. support.CheckSize(pk, CryptoBoxPublicKeyBytes(), "public key")
  81. support.CheckSize(sk, CryptoBoxSecretKeyBytes(), "secret key")
  82. m := make([]byte, len(c)-CryptoBoxMacBytes())
  83. exit := int(C.crypto_box_open_detached(
  84. (*C.uchar)(&m[0]),
  85. (*C.uchar)(&c[0]),
  86. (*C.uchar)(&mac[0]),
  87. (C.ulonglong)(len(c)),
  88. (*C.uchar)(&n[0]),
  89. (*C.uchar)(&pk[0]),
  90. (*C.uchar)(&sk[0])))
  91. return m, exit
  92. }
  93. func CryptoBoxOpenEasyAfterNm(c []byte, n []byte, k []byte) ([]byte, int) {
  94. support.CheckSize(n, CryptoBoxNonceBytes(), "nonce")
  95. support.CheckSize(k, CryptoBoxBeforeNmBytes(), "shared secret key")
  96. m := make([]byte, len(c)-CryptoBoxMacBytes())
  97. exit := int(C.crypto_box_open_easy_afternm(
  98. (*C.uchar)(&m[0]),
  99. (*C.uchar)(&c[0]),
  100. (C.ulonglong)(len(c)),
  101. (*C.uchar)(&n[0]),
  102. (*C.uchar)(&k[0])))
  103. return m, exit
  104. }
  105. func CryptoBoxOpenEasy(c []byte, n []byte, pk []byte, sk []byte) ([]byte, int) {
  106. support.CheckSize(n, CryptoBoxNonceBytes(), "nonce")
  107. support.CheckSize(pk, CryptoBoxPublicKeyBytes(), "public key")
  108. support.CheckSize(sk, CryptoBoxSecretKeyBytes(), "secret key")
  109. m := make([]byte, len(c)-CryptoBoxMacBytes())
  110. exit := int(C.crypto_box_open_easy(
  111. (*C.uchar)(&m[0]),
  112. (*C.uchar)(&c[0]),
  113. (C.ulonglong)(len(c)),
  114. (*C.uchar)(&n[0]),
  115. (*C.uchar)(&pk[0]),
  116. (*C.uchar)(&sk[0])))
  117. return m, exit
  118. }