Module: Crypto::SecretBox

Extended by:
FFI::Library, Sodium::Utils
Defined in:
lib/crypto/secret_box.rb

Constant Summary collapse

PRIMITIVE =
primitive.freeze
KEYBYTES =
keybytes.freeze
NONCEBYTES =
noncebytes.freeze
MACBYTES =
macbytes.freeze

Constants included from Sodium::Utils

Sodium::Utils::HEXY, Sodium::Utils::ZERO

Class Method Summary collapse

Methods included from Sodium::Utils

bin2hex, check_length, get_size, hex2bin, zeros

Class Method Details

.detached(message, nonce, key) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/crypto/secret_box.rb', line 110

def detached(message, nonce, key)
  message_len = get_size(message)
  check_length(nonce, NONCEBYTES, :Nonce)
  check_length(key, KEYBYTES, :SecretKey)

  ciphertext = zeros(message_len)
  mac = zeros(MACBYTES)
  key.readonly if key.is_a?(Sodium::SecretBuffer)
  crypto_secretbox_detached(ciphertext, mac, message, message_len, nonce, key)

  [ciphertext, mac]
ensure
  key.noaccess if key.is_a?(Sodium::SecretBuffer)
end

.detached!(message, nonce, key) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/crypto/secret_box.rb', line 146

def detached!(message, nonce, key)
  check_length(nonce, NONCEBYTES, :Nonce)
  check_length(key, KEYBYTES, :SecretKey)

  mac = zeros(MACBYTES)
  key.readonly if key.is_a?(Sodium::SecretBuffer)
  crypto_secretbox_detached(message, mac, message, get_size(message), nonce, key)

  [message, mac]
ensure
  key.noaccess if key.is_a?(Sodium::SecretBuffer)
end

.nonceObject



32
33
34
# File 'lib/crypto/secret_box.rb', line 32

def nonce
  RandomBytes.buf(NONCEBYTES)
end

.open(ciphertext, nonce, key, encoding = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/crypto/secret_box.rb', line 50

def open(ciphertext, nonce, key, encoding = nil)
  ciphertext_len = get_size(ciphertext)
  check_length(nonce, NONCEBYTES, :Nonce)
  check_length(key, KEYBYTES, :SecretKey)

  decrypted = zeros(ciphertext_len - MACBYTES)
  key.readonly if key.is_a?(Sodium::SecretBuffer)
  if crypto_secretbox_open_easy(decrypted, ciphertext, ciphertext_len, nonce, key) == -1
    raise Sodium::CryptoError, "Message forged", caller
  end

  if encoding
    decrypted.force_encoding(encoding)
  end

  decrypted
ensure
  key.noaccess if key.is_a?(Sodium::SecretBuffer)
end

.open!(data, nonce, key, encoding = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/crypto/secret_box.rb', line 85

def open!(data, nonce, key, encoding = nil)
  ciphertext = String(data)

  if (message_len = (ciphertext_len = ciphertext.bytesize) - MACBYTES) < 0
    fail Sodium::LengthError, "Ciphertext is too short", caller
  end

  check_length(nonce, NONCEBYTES, :Nonce)
  check_length(key, KEYBYTES, :SecretKey)

  key.readonly if key.is_a?(Sodium::SecretBuffer)
  if crypto_secretbox_open_easy(ciphertext, ciphertext, ciphertext_len, nonce, key) == -1
    raise Sodium::CryptoError, "Message forged", caller
  end

  ciphertext.slice!(message_len..-1)
  if encoding
    ciphertext.force_encoding(encoding)
  end

  ciphertext
ensure
  key.noaccess if key.is_a?(Sodium::SecretBuffer)
end

.open_detached(ciphertext, mac, nonce, key, encoding = nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/crypto/secret_box.rb', line 125

def open_detached(ciphertext, mac, nonce, key, encoding = nil)
  ciphertext_len = get_size(ciphertext)
  check_length(mac, MACBYTES, :Mac)
  check_length(nonce, NONCEBYTES, :Nonce)
  check_length(key, KEYBYTES, :SecretKey)

  message = zeros(ciphertext_len)
  key.readonly if key.is_a?(Sodium::SecretBuffer)
  if crypto_secretbox_open_detached(message, ciphertext, mac, ciphertext_len, nonce, key) == -1
    raise Sodium::CryptoError, "Message forged", caller
  end

  if encoding
    message.force_encoding(encoding)
  end

  message
ensure
  key.noaccess if key.is_a?(Sodium::SecretBuffer)
end

.open_detached!(ciphertext, mac, nonce, key, encoding = nil) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/crypto/secret_box.rb', line 159

def open_detached!(ciphertext, mac, nonce, key, encoding = nil)
  check_length(mac, MACBYTES, :Mac)
  check_length(nonce, NONCEBYTES, :Nonce)
  check_length(key, KEYBYTES, :SecretKey)

  key.readonly if key.is_a?(Sodium::SecretBuffer)
  if crypto_secretbox_open_detached(ciphertext, ciphertext, mac, get_size(ciphertext), nonce, key) == -1
    raise Sodium::CryptoError, "Message forged", caller
  end

  if encoding && ciphertext.respond_to?(:force_encoding)
    ciphertext.force_encoding(encoding)
  end

  ciphertext
ensure
  key.noaccess if key.is_a?(Sodium::SecretBuffer)
end

.secretbox(message, nonce, key) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/crypto/secret_box.rb', line 36

def secretbox(message, nonce, key)
  message_len = get_size(message)
  check_length(nonce, NONCEBYTES, :Nonce)
  check_length(key, KEYBYTES, :SecretKey)

  ciphertext = zeros(message_len + MACBYTES)
  key.readonly if key.is_a?(Sodium::SecretBuffer)
  crypto_secretbox_easy(ciphertext, message, message_len, nonce, key)

  ciphertext
ensure
  key.noaccess if key.is_a?(Sodium::SecretBuffer)
end

.secretbox!(data, nonce, key) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/crypto/secret_box.rb', line 70

def secretbox!(data, nonce, key)
  message = String(data)
  check_length(nonce, NONCEBYTES, :Nonce)
  check_length(key, KEYBYTES, :SecretKey)

  message_len = message.bytesize
  message << zeros(MACBYTES)
  key.readonly if key.is_a?(Sodium::SecretBuffer)
  crypto_secretbox_easy(message, message, message_len, nonce, key)

  message.force_encoding(Encoding::ASCII_8BIT)
ensure
  key.noaccess if key.is_a?(Sodium::SecretBuffer)
end