crypto_box_easy.go 4.1 KB

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