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.

112 lines
2.6 KiB

  1. package rsa
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "math/big"
  6. )
  7. const (
  8. bits = 512 // 2048
  9. )
  10. var bigOne = big.NewInt(int64(1))
  11. // PublicKey stores the public key data
  12. type PublicKey struct {
  13. E *big.Int `json:"e"`
  14. N *big.Int `json:"n"`
  15. }
  16. // PrivateKey stores the private key data
  17. type PrivateKey struct {
  18. D *big.Int `json:"d"`
  19. N *big.Int `json:"n"`
  20. }
  21. // Key stores the public and private key data
  22. type Key struct {
  23. PubK PublicKey
  24. PrivK PrivateKey
  25. }
  26. // GenerateKeyPair generates a random private and public key
  27. func GenerateKeyPair() (key Key, err error) {
  28. p, err := rand.Prime(rand.Reader, bits/2)
  29. if err != nil {
  30. return key, err
  31. }
  32. q, err := rand.Prime(rand.Reader, bits/2)
  33. if err != nil {
  34. return key, err
  35. }
  36. n := new(big.Int).Mul(p, q)
  37. p1 := new(big.Int).Sub(p, bigOne)
  38. q1 := new(big.Int).Sub(q, bigOne)
  39. phi := new(big.Int).Mul(p1, q1)
  40. e := 65537
  41. var pubK PublicKey
  42. pubK.E = big.NewInt(int64(e))
  43. pubK.N = n
  44. d := new(big.Int).ModInverse(big.NewInt(int64(e)), phi)
  45. var privK PrivateKey
  46. privK.D = d
  47. privK.N = n
  48. key.PubK = pubK
  49. key.PrivK = privK
  50. return key, nil
  51. }
  52. // Encrypt encrypts a message m with given PublicKey
  53. func Encrypt(m *big.Int, pubK PublicKey) *big.Int {
  54. c := new(big.Int).Exp(m, pubK.E, pubK.N)
  55. return c
  56. }
  57. // Decrypt deencrypts a ciphertext c with given PrivateKey
  58. func Decrypt(c *big.Int, privK PrivateKey) *big.Int {
  59. m := new(big.Int).Exp(c, privK.D, privK.N)
  60. return m
  61. }
  62. // Blind blinds a message
  63. func Blind(m *big.Int, r *big.Int, pubK PublicKey) *big.Int {
  64. rE := new(big.Int).Exp(r, pubK.E, nil)
  65. mrE := new(big.Int).Mul(m, rE)
  66. mBlinded := new(big.Int).Mod(mrE, pubK.N)
  67. return mBlinded
  68. }
  69. // BlindSign blind signs a message without knowing the content
  70. func BlindSign(m *big.Int, privK PrivateKey) *big.Int {
  71. sigma := new(big.Int).Exp(m, privK.D, privK.N)
  72. return sigma
  73. }
  74. // Unblind unblinds the Blinded Signature
  75. func Unblind(sigma *big.Int, r *big.Int, pubK PublicKey) *big.Int {
  76. r1 := new(big.Int).ModInverse(r, pubK.N)
  77. bsr := new(big.Int).Mul(sigma, r1)
  78. sig := new(big.Int).Mod(bsr, pubK.N)
  79. return sig
  80. }
  81. // Verify verifies the signature of a message given the PublicKey of the signer
  82. func Verify(msg *big.Int, mSigned *big.Int, pubK PublicKey) bool {
  83. //decrypt the mSigned with pubK
  84. Cd := new(big.Int).Exp(mSigned, pubK.E, nil)
  85. m := new(big.Int).Mod(Cd, pubK.N)
  86. return bytes.Equal(msg.Bytes(), m.Bytes())
  87. }
  88. // HomomorphicMul calculates the multiplication of tow encrypted values given a PublicKey
  89. func HomomorphicMul(c1 *big.Int, c2 *big.Int, pubK PublicKey) *big.Int {
  90. c1c2 := new(big.Int).Mul(c1, c2)
  91. n2 := new(big.Int).Mul(pubK.N, pubK.N)
  92. d := new(big.Int).Mod(c1c2, n2)
  93. return d
  94. }