crypto_box.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 (
  9. "errors"
  10. support "syncscan-go/locals/gosodium/support"
  11. )
  12. func CryptoBoxSeedBytes() int {
  13. return int(C.crypto_box_seedbytes())
  14. }
  15. func CryptoBoxPublicKeyBytes() int {
  16. return int(C.crypto_box_publickeybytes())
  17. }
  18. func CryptoBoxSecretKeyBytes() int {
  19. return int(C.crypto_box_secretkeybytes())
  20. }
  21. func CryptoBoxNonceBytes() int {
  22. return int(C.crypto_box_noncebytes())
  23. }
  24. func CryptoBoxMacBytes() int {
  25. return int(C.crypto_box_macbytes())
  26. }
  27. func CryptoBoxPrimitive() string {
  28. return C.GoString(C.crypto_box_primitive())
  29. }
  30. func CryptoBoxBeforeNmBytes() int {
  31. return int(C.crypto_box_beforenmbytes())
  32. }
  33. func CryptoBoxZeroBytes() int {
  34. return int(C.crypto_box_zerobytes())
  35. }
  36. func CryptoBoxBoxZeroBytes() int {
  37. return int(C.crypto_box_boxzerobytes())
  38. }
  39. func CryptoBoxSeedKeyPair(seed []byte) ([]byte, []byte, int) {
  40. support.CheckSize(seed, CryptoBoxSeedBytes(), "seed")
  41. sk := make([]byte, CryptoBoxSecretKeyBytes())
  42. pk := make([]byte, CryptoBoxPublicKeyBytes())
  43. exit := int(C.crypto_box_seed_keypair(
  44. (*C.uchar)(&pk[0]),
  45. (*C.uchar)(&sk[0]),
  46. (*C.uchar)(&seed[0])))
  47. return sk, pk, exit
  48. }
  49. func CryptoBoxKeyPair() ([]byte, []byte, int) {
  50. sk := make([]byte, CryptoBoxSecretKeyBytes())
  51. pk := make([]byte, CryptoBoxPublicKeyBytes())
  52. exit := int(C.crypto_box_keypair(
  53. (*C.uchar)(&pk[0]),
  54. (*C.uchar)(&sk[0])))
  55. return sk, pk, exit
  56. }
  57. func CryptoBoxBeforeNm(pk []byte, sk []byte) ([]byte, int) {
  58. support.CheckSize(pk, CryptoBoxPublicKeyBytes(), "public key")
  59. support.CheckSize(sk, CryptoBoxSecretKeyBytes(), "sender's secret key")
  60. k := make([]byte, CryptoBoxBeforeNmBytes())
  61. exit := int(C.crypto_box_beforenm(
  62. (*C.uchar)(&k[0]),
  63. (*C.uchar)(&pk[0]),
  64. (*C.uchar)(&sk[0])))
  65. return k, exit
  66. }
  67. func CryptoBox(m []byte, n []byte, pk []byte, sk []byte) ([]byte, int) {
  68. support.CheckSize(n, CryptoBoxNonceBytes(), "nonce")
  69. support.CheckSize(pk, CryptoBoxPublicKeyBytes(), "public key")
  70. support.CheckSize(sk, CryptoBoxSecretKeyBytes(), "sender's secret key")
  71. c := make([]byte, len(m))
  72. exit := int(C.crypto_box(
  73. (*C.uchar)(&c[0]),
  74. (*C.uchar)(&m[0]),
  75. (C.ulonglong)(len(m)),
  76. (*C.uchar)(&n[0]),
  77. (*C.uchar)(&pk[0]),
  78. (*C.uchar)(&sk[0])))
  79. return c, exit
  80. }
  81. func CryptoBoxOpen(c []byte, n []byte, pk []byte, sk []byte) ([]byte, int) {
  82. support.CheckSize(n, CryptoBoxNonceBytes(), "nonce")
  83. support.CheckSize(pk, CryptoBoxPublicKeyBytes(), "public key")
  84. support.CheckSize(sk, CryptoBoxPublicKeyBytes(), "secret key")
  85. m := make([]byte, len(c))
  86. exit := int(C.crypto_box_open(
  87. (*C.uchar)(&m[0]),
  88. (*C.uchar)(&c[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 CryptoBoxAfterNm(m []byte, n []byte, k []byte) ([]byte, int) {
  96. support.CheckSize(n, CryptoBoxNonceBytes(), "nonce")
  97. support.CheckSize(k, CryptoBoxBeforeNmBytes(), "shared secret key")
  98. c := make([]byte, len(m))
  99. exit := int(C.crypto_box_afternm(
  100. (*C.uchar)(&c[0]),
  101. (*C.uchar)(&m[0]),
  102. (C.ulonglong)(len(m)),
  103. (*C.uchar)(&n[0]),
  104. (*C.uchar)(&k[0])))
  105. return c, exit
  106. }
  107. func CryptoBoxOpenAfterNm(c []byte, n []byte, k []byte) ([]byte, int) {
  108. support.CheckSize(n, CryptoBoxNonceBytes(), "nonce")
  109. support.CheckSize(k, CryptoBoxBeforeNmBytes(), "shared secret key")
  110. m := make([]byte, len(c))
  111. exit := int(C.crypto_box_open_afternm(
  112. (*C.uchar)(&m[0]),
  113. (*C.uchar)(&c[0]),
  114. (C.ulonglong)(len(c)),
  115. (*C.uchar)(&n[0]),
  116. (*C.uchar)(&k[0])))
  117. return m, exit
  118. }
  119. func CryptoBoxGetSecretPublicKeyFrom(keypair []byte) (sk, pk []byte, err error) {
  120. if len(keypair) != 64 {
  121. err = errors.New("keypair length must be 64")
  122. return
  123. }
  124. sk = keypair[:32]
  125. pk = keypair[32:]
  126. return
  127. }