cryptor_executor.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. import functools
  19. class CryptoExecutor(object):
  20. def __init__(self, cipher_core):
  21. self.cipher_core = cipher_core
  22. def init(self):
  23. self.cipher_core.init()
  24. def renew(self, cipher_core):
  25. self.cipher_core = cipher_core
  26. def map_hash_encrypt(self, plaintable, mode, hash_operator, salt):
  27. """
  28. Process the input Table as (k, v)
  29. (k, enc_k) for mode == 0
  30. (enc_k, -1) for mode == 1
  31. (enc_k, v) for mode == 2
  32. (k, (enc_k, v)) for mode == 3
  33. (enc_k, k) for mode == 4
  34. (enc_k, (k, v)) for mode == 5
  35. :param plaintable: Table
  36. :param mode: int
  37. :return: Table
  38. """
  39. if mode == 0:
  40. return plaintable.map(
  41. lambda k, v: (
  42. k, self.cipher_core.encrypt(
  43. hash_operator.compute(
  44. k, suffix_salt=salt))))
  45. elif mode == 1:
  46. return plaintable.map(
  47. lambda k, v: (
  48. self.cipher_core.encrypt(
  49. hash_operator.compute(
  50. k, suffix_salt=salt)), -1))
  51. elif mode == 2:
  52. return plaintable.map(
  53. lambda k, v: (
  54. self.cipher_core.encrypt(
  55. hash_operator.compute(
  56. k, suffix_salt=salt)), v))
  57. elif mode == 3:
  58. return plaintable.map(
  59. lambda k, v: (
  60. k, (self.cipher_core.encrypt(
  61. hash_operator.compute(
  62. k, suffix_salt=salt)), v)))
  63. elif mode == 4:
  64. return plaintable.map(
  65. lambda k, v: (
  66. self.cipher_core.encrypt(
  67. hash_operator.compute(
  68. k, suffix_salt=salt)), k))
  69. elif mode == 5:
  70. return plaintable.map(
  71. lambda k, v: (self.cipher_core.encrypt(hash_operator.compute(k, suffix_salt=salt)), (k, v)))
  72. else:
  73. raise ValueError("Unsupported mode for crypto_executor map encryption")
  74. def map_encrypt(self, plaintable, mode):
  75. """
  76. Process the input Table as (k, v)
  77. (k, enc_k) for mode == 0
  78. (enc_k, -1) for mode == 1
  79. (enc_k, v) for mode == 2
  80. (k, (enc_k, v)) for mode == 3
  81. (enc_k, k) for mode == 4
  82. (enc_k, (k, v)) for mode == 5
  83. :param plaintable: Table
  84. :param mode: int
  85. :return: Table
  86. """
  87. if mode == 0:
  88. return plaintable.map(lambda k, v: (k, self.cipher_core.encrypt(k)))
  89. elif mode == 1:
  90. return plaintable.map(lambda k, v: (self.cipher_core.encrypt(k), -1))
  91. elif mode == 2:
  92. return plaintable.map(lambda k, v: (self.cipher_core.encrypt(k), v))
  93. elif mode == 3:
  94. return plaintable.map(lambda k, v: (k, (self.cipher_core.encrypt(k), v)))
  95. elif mode == 4:
  96. return plaintable.map(lambda k, v: (self.cipher_core.encrypt(k), k))
  97. elif mode == 5:
  98. return plaintable.map(lambda k, v: (self.cipher_core.encrypt(k), (k, v)))
  99. else:
  100. raise ValueError("Unsupported mode for crypto_executor map encryption")
  101. def map_values_encrypt(self, plaintable, mode):
  102. """
  103. Process the input Table as v
  104. enc_v if mode == 0
  105. :param plaintable: Table
  106. :param mode: int
  107. :return:
  108. """
  109. if mode == 0:
  110. return plaintable.mapValues(lambda v: self.cipher_core.encrypt(v))
  111. else:
  112. raise ValueError("Unsupported mode for crypto_executor map_values encryption")
  113. def map_decrypt(self, ciphertable, mode):
  114. """
  115. Process the input Table as (k, v)
  116. (k, dec_k) for mode == 0
  117. (dec_k, -1) for mode == 1
  118. (dec_k, v) for mode == 2
  119. (k, (dec_k, v)) for mode == 3
  120. :param ciphertable: Table
  121. :param mode: int
  122. :return: Table
  123. """
  124. if mode == 0:
  125. return ciphertable.map(lambda k, v: (k, self.cipher_core.decrypt(k)))
  126. elif mode == 1:
  127. return ciphertable.map(lambda k, v: (self.cipher_core.decrypt(k), -1))
  128. elif mode == 2:
  129. return ciphertable.map(lambda k, v: (self.cipher_core.decrypt(k), v))
  130. elif mode == 3:
  131. return ciphertable.map(lambda k, v: (k, (self.cipher_core.decrypt(k), v)))
  132. elif mode == 4:
  133. return ciphertable.map(lambda k, v: (self.cipher_core.decrypt(k), v))
  134. elif mode == 5:
  135. return ciphertable.map(lambda k, v: (self.cipher_core.decrypt(k), v))
  136. else:
  137. raise ValueError("Unsupported mode for crypto_executor map decryption")
  138. def map_values_decrypt(self, ciphertable, mode):
  139. """
  140. Process the input Table as v
  141. dec_v if mode == 0
  142. decode(dec_v) if mode == 1
  143. :param ciphertable: Table
  144. :param mode: int
  145. :return:
  146. """
  147. if mode == 0:
  148. return ciphertable.mapValues(lambda v: self.cipher_core.decrypt(v))
  149. elif mode == 1:
  150. f = functools.partial(self.cipher_core.decrypt, decode_output=True)
  151. return ciphertable.mapValues(lambda v: f(v))
  152. else:
  153. raise ValueError("Unsupported mode for crypto_executor map_values encryption")
  154. def get_nonce(self):
  155. return self.cipher_core.get_nonce()