Module: SecureDBFields::Crypto

Defined in:
lib/secure_db_fields/crypto.rb

Constant Summary collapse

ENVELOPE_MAGIC =
"MCEN".b
KEY_BYTES =
32
BIDX_BYTES =
32

Class Method Summary collapse

Class Method Details

.aad(table, column, secure_row_uid) ⇒ Object

Raises:

  • (ArgumentError)


105
106
107
108
109
110
111
112
113
# File 'lib/secure_db_fields/crypto.rb', line 105

def aad(table, column, secure_row_uid)
  uid = secure_row_uid.to_s
  raise ArgumentError, "secure_row_uid must be 16 bytes" unless uid.bytesize == 16
  table = table.to_s.b
  column = column.to_s.b
  raise ArgumentError, "table name is too long" if table.bytesize > 0xffffffff
  raise ArgumentError, "column name is too long" if column.bytesize > 0xffffffff
  "SDF1".b + [table.bytesize].pack("N") + table + [column.bytesize].pack("N") + column + uid.b
end

.assert_key!(key) ⇒ Object

Raises:



99
100
101
102
103
# File 'lib/secure_db_fields/crypto.rb', line 99

def assert_key!(key)
  key = key.to_s
  raise KeyError, "key must be exactly #{KEY_BYTES} bytes" unless key.bytesize == KEY_BYTES
  key
end

.blind_index(value, key:) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
# File 'lib/secure_db_fields/crypto.rb', line 30

def blind_index(value, key:)
  raise ArgumentError, "value must not be nil" if value.nil?
  Native.blind_index(value.to_s, assert_key!(key))
end

.blind_index_many(values, key:) ⇒ Object



71
72
73
# File 'lib/secure_db_fields/crypto.rb', line 71

def blind_index_many(values, key:)
  Native.blind_index_many(values, assert_key!(key))
end

.blind_index_many_packed(values, key:) ⇒ Object



75
76
77
# File 'lib/secure_db_fields/crypto.rb', line 75

def blind_index_many_packed(values, key:)
  Native.blind_index_many_packed(values, assert_key!(key))
end

.blind_index_raw(value, key) ⇒ Object



51
52
53
# File 'lib/secure_db_fields/crypto.rb', line 51

def blind_index_raw(value, key)
  Native.blind_index(value, key)
end

.decrypt(envelope, key:, aad:) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
# File 'lib/secure_db_fields/crypto.rb', line 16

def decrypt(envelope, key:, aad:)
  raise ArgumentError, "envelope must not be nil" if envelope.nil?
  Native.decrypt(envelope.to_s, assert_key!(key), aad.to_s)
end

.decrypt_many(envelopes, key:, aads:) ⇒ Object



67
68
69
# File 'lib/secure_db_fields/crypto.rb', line 67

def decrypt_many(envelopes, key:, aads:)
  Native.decrypt_many(envelopes, assert_key!(key), aads)
end

.decrypt_raw(envelope, key, aad) ⇒ Object



47
48
49
# File 'lib/secure_db_fields/crypto.rb', line 47

def decrypt_raw(envelope, key, aad)
  Native.decrypt(envelope, key, aad)
end

.encrypt(value, key:, aad:, key_id: 1) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
# File 'lib/secure_db_fields/crypto.rb', line 11

def encrypt(value, key:, aad:, key_id: 1)
  raise ArgumentError, "value must not be nil" if value.nil?
  Native.encrypt(value.to_s, assert_key!(key), aad.to_s, Integer(key_id))
end

.encrypt_many(values, key:, aads:, key_id: 1) ⇒ Object



63
64
65
# File 'lib/secure_db_fields/crypto.rb', line 63

def encrypt_many(values, key:, aads:, key_id: 1)
  Native.encrypt_many(values, assert_key!(key), aads, Integer(key_id))
end

.encrypt_raw(value, key, aad, key_id = 1) ⇒ Object



43
44
45
# File 'lib/secure_db_fields/crypto.rb', line 43

def encrypt_raw(value, key, aad, key_id = 1)
  Native.encrypt(value, key, aad, key_id)
end

.hex_decode_key(hex) ⇒ Object



95
96
97
# File 'lib/secure_db_fields/crypto.rb', line 95

def hex_decode_key(hex)
  Native.hex_decode_key(hex.to_s)
end

.key_id(envelope) ⇒ Object



21
22
23
# File 'lib/secure_db_fields/crypto.rb', line 21

def key_id(envelope)
  Native.key_id(envelope.to_s)
end

.phone_blind_index(e164, key:) ⇒ Object



35
36
37
# File 'lib/secure_db_fields/crypto.rb', line 35

def phone_blind_index(e164, key:)
  Native.phone_blind_index(e164.to_s, assert_key!(key))
end

.phone_blind_index_many(e164_values, key:) ⇒ Object



79
80
81
# File 'lib/secure_db_fields/crypto.rb', line 79

def phone_blind_index_many(e164_values, key:)
  Native.phone_blind_index_many(e164_values, assert_key!(key))
end

.phone_blind_index_many_packed(e164_values, key:) ⇒ Object



83
84
85
# File 'lib/secure_db_fields/crypto.rb', line 83

def phone_blind_index_many_packed(e164_values, key:)
  Native.phone_blind_index_many_packed(e164_values, assert_key!(key))
end

.phone_blind_index_raw(e164, key) ⇒ Object



55
56
57
# File 'lib/secure_db_fields/crypto.rb', line 55

def phone_blind_index_raw(e164, key)
  Native.phone_blind_index(e164, key)
end

.phone_prefix_blind_index(e164, prefix_digits:, key:) ⇒ Object



39
40
41
# File 'lib/secure_db_fields/crypto.rb', line 39

def phone_prefix_blind_index(e164, prefix_digits:, key:)
  Native.phone_prefix_blind_index(e164.to_s, Integer(prefix_digits), assert_key!(key))
end

.phone_prefix_blind_index_many(e164_values, prefix_digits:, key:) ⇒ Object



87
88
89
# File 'lib/secure_db_fields/crypto.rb', line 87

def phone_prefix_blind_index_many(e164_values, prefix_digits:, key:)
  Native.phone_prefix_blind_index_many(e164_values, Integer(prefix_digits), assert_key!(key))
end

.phone_prefix_blind_index_many_packed(e164_values, prefix_digits:, key:) ⇒ Object



91
92
93
# File 'lib/secure_db_fields/crypto.rb', line 91

def phone_prefix_blind_index_many_packed(e164_values, prefix_digits:, key:)
  Native.phone_prefix_blind_index_many_packed(e164_values, Integer(prefix_digits), assert_key!(key))
end

.phone_prefix_blind_index_raw(e164, prefix_digits, key) ⇒ Object



59
60
61
# File 'lib/secure_db_fields/crypto.rb', line 59

def phone_prefix_blind_index_raw(e164, prefix_digits, key)
  Native.phone_prefix_blind_index(e164, prefix_digits, key)
end

.valid_envelope?(envelope) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/secure_db_fields/crypto.rb', line 25

def valid_envelope?(envelope)
  return false if envelope.nil?
  Native.valid_envelope?(envelope.to_s)
end