elliptic_curve_encryption.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2019 The FATE Authors. All Rights Reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. from fate_crypto.psi import Curve25519
  19. class EllipticCurve(object):
  20. """
  21. Symmetric encryption key
  22. """
  23. def __init__(self, curve_name, curve_key=None):
  24. self.curve = self.__get_curve_instance(curve_name, curve_key)
  25. @staticmethod
  26. def __get_curve_instance(curve_name, curve_key):
  27. if curve_key is None:
  28. return Curve25519()
  29. return Curve25519(curve_key)
  30. def get_curve_key(self):
  31. return self.curve.get_private_key()
  32. def encrypt(self, plaintext):
  33. """
  34. Encryption method
  35. :param plaintext:
  36. :return:
  37. """
  38. return self.curve.encrypt(plaintext)
  39. def sign(self, ciphertext):
  40. return self.curve.diffie_hellman(ciphertext)
  41. def map_hash_encrypt(self, plaintable, mode, hash_operator, salt):
  42. """
  43. adapted from CryptorExecutor
  44. Process the input Table as (k, v)
  45. (k, enc_k) for mode == 0
  46. (enc_k, -1) for mode == 1
  47. (enc_k, v) for mode == 2
  48. (k, (enc_k, v)) for mode == 3
  49. (enc_k, k) for mode == 4
  50. (enc_k, (k, v)) for mode == 5
  51. :param plaintable: Table
  52. :param mode: int
  53. :return: Table
  54. """
  55. if mode == 0:
  56. return plaintable.map(
  57. lambda k, v: (
  58. k, self.curve.encrypt(
  59. hash_operator.compute(
  60. k, suffix_salt=salt))))
  61. elif mode == 1:
  62. return plaintable.map(
  63. lambda k, v: (
  64. self.curve.encrypt(
  65. hash_operator.compute(
  66. k, suffix_salt=salt)), -1))
  67. elif mode == 2:
  68. return plaintable.map(
  69. lambda k, v: (
  70. self.curve.encrypt(
  71. hash_operator.compute(
  72. k, suffix_salt=salt)), v))
  73. elif mode == 3:
  74. return plaintable.map(
  75. lambda k, v: (
  76. k, (self.curve.encrypt(
  77. hash_operator.compute(
  78. k, suffix_salt=salt)), v)))
  79. elif mode == 4:
  80. return plaintable.map(
  81. lambda k, v: (
  82. self.curve.encrypt(
  83. hash_operator.compute(
  84. k, suffix_salt=salt)), k))
  85. elif mode == 5:
  86. return plaintable.map(
  87. lambda k, v: (self.curve.encrypt(hash_operator.compute(k, suffix_salt=salt)), (k, v)))
  88. else:
  89. raise ValueError("Unsupported mode for elliptic curve map encryption")
  90. def map_encrypt(self, plaintable, mode):
  91. """
  92. adapted from CryptorExecutor
  93. Process the input Table as (k, v)
  94. (k, enc_k) for mode == 0
  95. (enc_k, -1) for mode == 1
  96. (enc_k, v) for mode == 2
  97. (k, (enc_k, v)) for mode == 3
  98. (enc_k, k) for mode == 4
  99. (enc_k, (k, v)) for mode == 5
  100. :param plaintable: Table
  101. :param mode: int
  102. :return: Table
  103. """
  104. if mode == 0:
  105. return plaintable.map(lambda k, v: (k, self.curve.encrypt(k)))
  106. elif mode == 1:
  107. return plaintable.map(lambda k, v: (self.curve.encrypt(k), -1))
  108. elif mode == 2:
  109. return plaintable.map(lambda k, v: (self.curve.encrypt(k), v))
  110. elif mode == 3:
  111. return plaintable.map(lambda k, v: (k, (self.curve.encrypt(k), v)))
  112. elif mode == 4:
  113. return plaintable.map(lambda k, v: (self.curve.encrypt(k), k))
  114. elif mode == 5:
  115. return plaintable.map(lambda k, v: (self.curve.encrypt(k), (k, v)))
  116. else:
  117. raise ValueError("Unsupported mode for elliptic curve map encryption")
  118. def map_sign(self, plaintable, mode):
  119. """
  120. adapted from CryptorExecutor
  121. Process the input Table as (k, v)
  122. (k, enc_k) for mode == 0
  123. (enc_k, -1) for mode == 1
  124. (enc_k, v) for mode == 2
  125. (k, (enc_k, v)) for mode == 3
  126. (enc_k, k) for mode == 4
  127. (enc_k, (k, v)) for mode == 5
  128. :param plaintable: Table
  129. :param mode: int
  130. :return: Table
  131. """
  132. if mode == 0:
  133. return plaintable.map(lambda k, v: (k, self.curve.diffie_hellman(k)))
  134. elif mode == 1:
  135. return plaintable.map(lambda k, v: (self.curve.diffie_hellman(k), -1))
  136. elif mode == 2:
  137. return plaintable.map(lambda k, v: (self.curve.diffie_hellman(k), v))
  138. elif mode == 3:
  139. return plaintable.map(lambda k, v: (k, (self.curve.diffie_hellman(k), v)))
  140. elif mode == 4:
  141. return plaintable.map(lambda k, v: (self.curve.diffie_hellman(k), k))
  142. elif mode == 5:
  143. return plaintable.map(lambda k, v: (self.curve.diffie_hellman(k), (k, v)))
  144. else:
  145. raise ValueError("Unsupported mode for elliptic curve map sign")