File: mongo_crypt_spec_helper.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (111 lines) | stat: -rw-r--r-- 3,147 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# frozen_string_literal: true
# rubocop:todo all

module MongoCryptSpecHelper
  def bind_crypto_hooks(mongocrypt)
    Mongo::Crypt::Binding.mongocrypt_setopt_crypto_hooks(
      mongocrypt,
      method(:aes_encrypt),
      method(:aes_decrypt),
      method(:random),
      method(:hmac_sha_512),
      method(:hmac_sha_256),
      method(:hmac_hash),
      nil
    )
  end
  module_function :bind_crypto_hooks

  def mongocrypt_binary_t_from(string)
    bytes = string.unpack('C*')

    p = FFI::MemoryPointer
      .new(bytes.size)
      .write_array_of_type(FFI::TYPE_UINT8, :put_uint8, bytes)

    Mongo::Crypt::Binding.mongocrypt_binary_new_from_data(p, bytes.length)
  end
  module_function :mongocrypt_binary_t_from

  private

  def string_from_binary(binary_p)
    str_p = Mongo::Crypt::Binding.get_binary_data_direct(binary_p)
    len = Mongo::Crypt::Binding.get_binary_len_direct(binary_p)
    str_p.read_string(len)
  end
  module_function :string_from_binary

  def write_to_binary(binary_p, data)
    str_p = Mongo::Crypt::Binding.get_binary_data_direct(binary_p)
    str_p.put_bytes(0, data)
  end
  module_function :write_to_binary

  def aes_encrypt(_, key_binary_p, iv_binary_p, input_binary_p, output_binary_p,
    response_length_p, status_p)
    key = string_from_binary(key_binary_p)
    iv = string_from_binary(iv_binary_p)
    input = string_from_binary(input_binary_p)

    output = Mongo::Crypt::Hooks.aes(key, iv, input)
    write_to_binary(output_binary_p, output)
    response_length_p.write_int(output.length)

    true
  end
  module_function :aes_encrypt

  def aes_decrypt(_, key_binary_p, iv_binary_p, input_binary_p, output_binary_p,
    response_length_p, status_p)
    key = string_from_binary(key_binary_p)
    iv = string_from_binary(iv_binary_p)
    input = string_from_binary(input_binary_p)

    output = Mongo::Crypt::Hooks.aes(key, iv, input, decrypt: true)
    write_to_binary(output_binary_p, output)
    response_length_p.write_int(output.length)

    true
  end
  module_function :aes_decrypt

  def random(_, output_binary_p, num_bytes, status_p)
    output = Mongo::Crypt::Hooks.random(num_bytes)
    write_to_binary(output_binary_p, output)

    true
  end
  module_function :random

  def hmac_sha_512(_, key_binary_p, input_binary_p, output_binary_p, status_p)
    key = string_from_binary(key_binary_p)
    input = string_from_binary(input_binary_p)

    output = Mongo::Crypt::Hooks.hmac_sha('SHA512', key, input)
    write_to_binary(output_binary_p, output)

    true
  end
  module_function :hmac_sha_512

  def hmac_sha_256(_, key_binary_p, input_binary_p, output_binary_p, status_p)
    key = string_from_binary(key_binary_p)
    input = string_from_binary(input_binary_p)

    output = Mongo::Crypt::Hooks.hmac_sha('SHA256', key, input)
    write_to_binary(output_binary_p, output)

    true
  end
  module_function :hmac_sha_256

  def hmac_hash(_, input_binary_p, output_binary_p, status_p)
    input = string_from_binary(input_binary_p)
    output = Mongo::Crypt::Hooks.hash_sha256(input)
    write_to_binary(output_binary_p, output)

    true
  end
  module_function :hmac_hash
end