crypto_box.go 3.7 KB

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