You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
5.5 KiB

  1. package ownrsa
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/big"
  6. "math/rand"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type RSAPublicKey struct {
  12. E *big.Int `json:"e"`
  13. N *big.Int `json:"n"`
  14. }
  15. type RSAPublicKeyString struct {
  16. E string `json:"e"`
  17. N string `json:"n"`
  18. }
  19. type RSAPrivateKey struct {
  20. D *big.Int `json:"d"`
  21. N *big.Int `json:"n"`
  22. }
  23. type RSA struct {
  24. PubK RSAPublicKey
  25. PrivK RSAPrivateKey
  26. }
  27. type PackRSA struct {
  28. PubK string `json:"pubK"`
  29. PrivK string `json:"privK"`
  30. Date time.Time `json:"date"`
  31. PubKSigned string `json:"pubKSigned"`
  32. Verified bool `json:"verified"`
  33. UnblindedSig string `json:"unblindedsig"`
  34. BlockchainRef string `json:"blockchainref"`
  35. }
  36. const maxPrime = 500
  37. const minPrime = 100
  38. func GenerateKeyPair() RSA {
  39. rand.Seed(time.Now().Unix())
  40. p := randPrime(minPrime, maxPrime)
  41. q := randPrime(minPrime, maxPrime)
  42. fmt.Print("p:")
  43. fmt.Println(p)
  44. fmt.Print("q:")
  45. fmt.Println(q)
  46. n := p * q
  47. phi := (p - 1) * (q - 1)
  48. e := 65537
  49. var pubK RSAPublicKey
  50. pubK.E = big.NewInt(int64(e))
  51. pubK.N = big.NewInt(int64(n))
  52. d := new(big.Int).ModInverse(big.NewInt(int64(e)), big.NewInt(int64(phi)))
  53. var privK RSAPrivateKey
  54. privK.D = d
  55. privK.N = big.NewInt(int64(n))
  56. var rsa RSA
  57. rsa.PubK = pubK
  58. rsa.PrivK = privK
  59. return rsa
  60. }
  61. func Encrypt(m string, pubK RSAPublicKey) []int {
  62. var c []int
  63. mBytes := []byte(m)
  64. for _, byte := range mBytes {
  65. c = append(c, EncryptInt(int(byte), pubK))
  66. }
  67. return c
  68. }
  69. func Decrypt(c []int, privK RSAPrivateKey) string {
  70. var m string
  71. var mBytes []byte
  72. for _, indC := range c {
  73. mBytes = append(mBytes, byte(DecryptInt(indC, privK)))
  74. }
  75. m = string(mBytes)
  76. return m
  77. }
  78. func EncryptBigInt(bigint *big.Int, pubK RSAPublicKey) *big.Int {
  79. Me := new(big.Int).Exp(bigint, pubK.E, nil)
  80. c := new(big.Int).Mod(Me, pubK.N)
  81. return c
  82. }
  83. func DecryptBigInt(bigint *big.Int, privK RSAPrivateKey) *big.Int {
  84. Cd := new(big.Int).Exp(bigint, privK.D, nil)
  85. m := new(big.Int).Mod(Cd, privK.N)
  86. return m
  87. }
  88. func EncryptInt(char int, pubK RSAPublicKey) int {
  89. charBig := big.NewInt(int64(char))
  90. Me := charBig.Exp(charBig, pubK.E, nil)
  91. c := Me.Mod(Me, pubK.N)
  92. return int(c.Int64())
  93. }
  94. func DecryptInt(val int, privK RSAPrivateKey) int {
  95. valBig := big.NewInt(int64(val))
  96. Cd := valBig.Exp(valBig, privK.D, nil)
  97. m := Cd.Mod(Cd, privK.N)
  98. return int(m.Int64())
  99. }
  100. func Blind(m []int, r int, pubK RSAPublicKey) []int {
  101. var mBlinded []int
  102. rBigInt := big.NewInt(int64(r))
  103. for i := 0; i < len(m); i++ {
  104. mBigInt := big.NewInt(int64(m[i]))
  105. rE := new(big.Int).Exp(rBigInt, pubK.E, nil)
  106. mrE := new(big.Int).Mul(mBigInt, rE)
  107. mrEmodN := new(big.Int).Mod(mrE, pubK.N)
  108. mBlinded = append(mBlinded, int(mrEmodN.Int64()))
  109. }
  110. return mBlinded
  111. }
  112. func BlindSign(m []int, privK RSAPrivateKey) []int {
  113. var r []int
  114. for i := 0; i < len(m); i++ {
  115. mBigInt := big.NewInt(int64(m[i]))
  116. sigma := new(big.Int).Exp(mBigInt, privK.D, privK.N)
  117. r = append(r, int(sigma.Int64()))
  118. }
  119. return r
  120. }
  121. func Unblind(blindsigned []int, r int, pubK RSAPublicKey) []int {
  122. var mSigned []int
  123. rBigInt := big.NewInt(int64(r))
  124. for i := 0; i < len(blindsigned); i++ {
  125. bsBigInt := big.NewInt(int64(blindsigned[i]))
  126. //r1 := new(big.Int).Exp(rBigInt, big.NewInt(int64(-1)), nil)
  127. r1 := new(big.Int).ModInverse(rBigInt, pubK.N)
  128. bsr := new(big.Int).Mul(bsBigInt, r1)
  129. sig := new(big.Int).Mod(bsr, pubK.N)
  130. mSigned = append(mSigned, int(sig.Int64()))
  131. }
  132. return mSigned
  133. }
  134. func Verify(msg []int, mSigned []int, pubK RSAPublicKey) bool {
  135. if len(msg) != len(mSigned) {
  136. return false
  137. }
  138. var mSignedDecrypted []int
  139. for _, ms := range mSigned {
  140. msBig := big.NewInt(int64(ms))
  141. //decrypt the mSigned with pubK
  142. Cd := new(big.Int).Exp(msBig, pubK.E, nil)
  143. m := new(big.Int).Mod(Cd, pubK.N)
  144. mSignedDecrypted = append(mSignedDecrypted, int(m.Int64()))
  145. }
  146. fmt.Print("msg signed decrypted: ")
  147. fmt.Println(mSignedDecrypted)
  148. r := true
  149. //check if the mSignedDecrypted == msg
  150. for i := 0; i < len(msg); i++ {
  151. if msg[i] != mSignedDecrypted[i] {
  152. r = false
  153. }
  154. }
  155. return r
  156. }
  157. func HomomorphicMultiplication(c1 int, c2 int, pubK RSAPublicKey) int {
  158. c1BigInt := big.NewInt(int64(c1))
  159. c2BigInt := big.NewInt(int64(c2))
  160. c1c2 := new(big.Int).Mul(c1BigInt, c2BigInt)
  161. n2 := new(big.Int).Mul(pubK.N, pubK.N)
  162. d := new(big.Int).Mod(c1c2, n2)
  163. r := int(d.Int64())
  164. return r
  165. }
  166. func PubKStringToBigInt(kS RSAPublicKeyString) (RSAPublicKey, error) {
  167. var k RSAPublicKey
  168. var ok bool
  169. k.E, ok = new(big.Int).SetString(kS.E, 10)
  170. if !ok {
  171. return k, errors.New("error parsing big int E")
  172. }
  173. k.N, ok = new(big.Int).SetString(kS.N, 10)
  174. if !ok {
  175. return k, errors.New("error parsing big int N")
  176. }
  177. return k, nil
  178. }
  179. func PackKey(k RSA) PackRSA {
  180. var p PackRSA
  181. p.PubK = k.PubK.E.String() + "," + k.PubK.N.String()
  182. p.PrivK = k.PrivK.D.String() + "," + k.PrivK.N.String()
  183. return p
  184. }
  185. func UnpackKey(p PackRSA) RSA {
  186. var k RSA
  187. var ok bool
  188. k.PubK.E, ok = new(big.Int).SetString(strings.Split(p.PubK, ",")[0], 10)
  189. k.PubK.N, ok = new(big.Int).SetString(strings.Split(p.PubK, ",")[1], 10)
  190. k.PrivK.D, ok = new(big.Int).SetString(strings.Split(p.PrivK, ",")[0], 10)
  191. k.PrivK.N, ok = new(big.Int).SetString(strings.Split(p.PrivK, ",")[1], 10)
  192. if !ok {
  193. fmt.Println("error on Unpacking Keys")
  194. }
  195. return k
  196. }
  197. func ArrayIntToString(a []int, delim string) string {
  198. return strings.Trim(strings.Replace(fmt.Sprint(a), " ", delim, -1), "[]")
  199. }
  200. func StringToArrayInt(s string, delim string) []int {
  201. var a []int
  202. arrayString := strings.Split(s, delim)
  203. for _, s := range arrayString {
  204. i, err := strconv.Atoi(s)
  205. if err != nil {
  206. fmt.Println(err)
  207. }
  208. a = append(a, i)
  209. }
  210. return a
  211. }