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.

209 lines
4.9 KiB

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