Module: Crypto::Box
- Extended by:
- FFI::Library, Sodium::Utils
- Defined in:
- lib/crypto/box.rb
Constant Summary collapse
- PRIMITIVE =
primitive.freeze
- SEEDBYTES =
seedbytes.freeze
- PUBLICKEYBYTES =
publickeybytes.freeze
- SECRETKEYBYTES =
secretkeybytes.freeze
- NONCEBYTES =
noncebytes.freeze
- MACBYTES =
macbytes.freeze
- BEFORENMBYTES =
beforenmbytes.freeze
Constants included from Sodium::Utils
Sodium::Utils::HEXY, Sodium::Utils::ZERO
Class Method Summary collapse
- .beforenm(public_key, secret_key) ⇒ Object
- .box(message, nonce, public_key, secret_key) ⇒ Object
- .box!(data, nonce, public_key, secret_key) ⇒ Object
- .detached(message, nonce, public_key, secret_key) ⇒ Object
- .detached!(message, nonce, public_key, secret_key) ⇒ Object
- .keypair ⇒ Object
- .memory_locked_keypair ⇒ Object
- .memory_locked_seed_keypair(seed) ⇒ Object
- .nonce ⇒ Object
- .open(ciphertext, nonce, public_key, secret_key, encoding = nil) ⇒ Object
- .open!(data, nonce, public_key, secret_key, encoding = nil) ⇒ Object
- .open_detached(ciphertext, mac, nonce, public_key, secret_key, encoding = nil) ⇒ Object
- .open_detached!(ciphertext, mac, nonce, public_key, secret_key, encoding = nil) ⇒ Object
- .seed_keypair(seed) ⇒ Object
Methods included from Sodium::Utils
bin2hex, check_length, get_size, hex2bin, zeros
Class Method Details
.beforenm(public_key, secret_key) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/crypto/box.rb', line 91 def beforenm(public_key, secret_key) check_length(public_key, PUBLICKEYBYTES, :PublicKey) check_length(secret_key, SECRETKEYBYTES, :SecretKey) shared_secret = Sodium::SecretBuffer.new(BEFORENMBYTES) secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer) crypto_box_beforenm(shared_secret, public_key, secret_key) shared_secret.noaccess shared_secret ensure secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer) end |
.box(message, nonce, public_key, secret_key) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/crypto/box.rb', line 105 def box(, nonce, public_key, secret_key) = get_size() check_length(nonce, NONCEBYTES, :Nonce) check_length(public_key, PUBLICKEYBYTES, :PublicKey) check_length(secret_key, SECRETKEYBYTES, :SecretKey) ciphertext = zeros( + MACBYTES) secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer) crypto_box_easy(ciphertext, , , nonce, public_key, secret_key) ciphertext ensure secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer) end |
.box!(data, nonce, public_key, secret_key) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/crypto/box.rb', line 141 def box!(data, nonce, public_key, secret_key) = String(data) check_length(nonce, NONCEBYTES, :Nonce) check_length(public_key, PUBLICKEYBYTES, :PublicKey) check_length(secret_key, SECRETKEYBYTES, :SecretKey) = .bytesize << zeros(MACBYTES) secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer) crypto_box_easy(, , , nonce, public_key, secret_key) ensure secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer) end |
.detached(message, nonce, public_key, secret_key) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/crypto/box.rb', line 183 def detached(, nonce, public_key, secret_key) = get_size() check_length(nonce, NONCEBYTES, :Nonce) check_length(public_key, PUBLICKEYBYTES, :PublicKey) check_length(secret_key, SECRETKEYBYTES, :SecretKey) ciphertext = zeros() mac = zeros(MACBYTES) secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer) crypto_box_detached(ciphertext, mac, , , nonce, public_key, secret_key) [ciphertext, mac] ensure secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer) end |
.detached!(message, nonce, public_key, secret_key) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/crypto/box.rb', line 220 def detached!(, nonce, public_key, secret_key) check_length(nonce, NONCEBYTES, :Nonce) check_length(public_key, PUBLICKEYBYTES, :PublicKey) check_length(secret_key, SECRETKEYBYTES, :SecretKey) mac = zeros(MACBYTES) secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer) crypto_box_detached(, mac, , get_size(), nonce, public_key, secret_key) [, mac] ensure secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer) end |
.keypair ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/crypto/box.rb', line 47 def keypair public_key = zeros(PUBLICKEYBYTES) secret_key = zeros(SECRETKEYBYTES) crypto_box_keypair(public_key, secret_key) [public_key, secret_key] end |
.memory_locked_keypair ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/crypto/box.rb', line 68 def memory_locked_keypair public_key = zeros(PUBLICKEYBYTES) secret_key = Sodium::SecretBuffer.new(SECRETKEYBYTES) crypto_box_keypair(public_key, secret_key) secret_key.noaccess [public_key, secret_key] end |
.memory_locked_seed_keypair(seed) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/crypto/box.rb', line 77 def memory_locked_seed_keypair(seed) check_length(seed, SEEDBYTES, :Seed) public_key = zeros(PUBLICKEYBYTES) secret_key = Sodium::SecretBuffer.new(SECRETKEYBYTES) seed.readonly if seed.is_a?(Sodium::SecretBuffer) crypto_box_seed_keypair(public_key, secret_key, seed) secret_key.noaccess [public_key, secret_key] ensure seed.noaccess if seed.is_a?(Sodium::SecretBuffer) end |
.nonce ⇒ Object
43 44 45 |
# File 'lib/crypto/box.rb', line 43 def nonce RandomBytes.buf(NONCEBYTES) end |
.open(ciphertext, nonce, public_key, secret_key, encoding = nil) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/crypto/box.rb', line 120 def open(ciphertext, nonce, public_key, secret_key, encoding = nil) ciphertext_len = get_size(ciphertext) check_length(nonce, NONCEBYTES, :Nonce) check_length(public_key, PUBLICKEYBYTES, :PublicKey) check_length(secret_key, SECRETKEYBYTES, :SecretKey) = zeros(ciphertext_len - MACBYTES) secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer) if crypto_box_open_easy(, ciphertext, ciphertext_len, nonce, public_key, secret_key) == -1 raise Sodium::CryptoError, "Message forged", caller end if encoding .force_encoding(encoding) end ensure secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer) end |
.open!(data, nonce, public_key, secret_key, encoding = nil) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/crypto/box.rb', line 157 def open!(data, nonce, public_key, secret_key, encoding = nil) ciphertext = String(data) ciphertext_len = ciphertext.bytesize if ( = ciphertext_len - MACBYTES) < 0 fail Sodium::LengthError, "Ciphertext is too short", caller end check_length(nonce, NONCEBYTES, :Nonce) check_length(public_key, PUBLICKEYBYTES, :PublicKey) check_length(secret_key, SECRETKEYBYTES, :SecretKey) secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer) if crypto_box_open_easy(ciphertext, ciphertext, ciphertext_len, nonce, public_key, secret_key) == -1 raise Sodium::CryptoError, "Message forged", caller end ciphertext.slice!(..-1) if encoding ciphertext.force_encoding(encoding) end ciphertext ensure secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer) end |
.open_detached(ciphertext, mac, nonce, public_key, secret_key, encoding = nil) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/crypto/box.rb', line 198 def open_detached(ciphertext, mac, nonce, public_key, secret_key, encoding = nil) ciphertext_len = get_size(ciphertext) check_length(mac, MACBYTES, :Mac) check_length(nonce, NONCEBYTES, :Nonce) check_length(public_key, PUBLICKEYBYTES, :PublicKey) check_length(secret_key, SECRETKEYBYTES, :SecretKey) = zeros(ciphertext_len) secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer) if crypto_box_open_detached(, ciphertext, mac, ciphertext_len, nonce, public_key, secret_key) == -1 raise Sodium::CryptoError, "Message forged", caller end if encoding .force_encoding(encoding) end ensure secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer) end |
.open_detached!(ciphertext, mac, nonce, public_key, secret_key, encoding = nil) ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/crypto/box.rb', line 233 def open_detached!(ciphertext, mac, nonce, public_key, secret_key, encoding = nil) check_length(mac, MACBYTES, :Mac) check_length(nonce, NONCEBYTES, :Nonce) check_length(public_key, PUBLICKEYBYTES, :PublicKey) check_length(secret_key, SECRETKEYBYTES, :SecretKey) secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer) if crypto_box_open_detached(ciphertext, ciphertext, mac, get_size(ciphertext), nonce, public_key, secret_key) == -1 raise Sodium::CryptoError, "Message forged", caller end if encoding && ciphertext.respond_to?(:force_encoding) ciphertext.force_encoding(encoding) end ciphertext ensure secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer) end |
.seed_keypair(seed) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/crypto/box.rb', line 55 def seed_keypair(seed) check_length(seed, SEEDBYTES, :Seed) public_key = zeros(PUBLICKEYBYTES) secret_key = zeros(SECRETKEYBYTES) seed.readonly if seed.is_a?(Sodium::SecretBuffer) crypto_box_seed_keypair(public_key, secret_key, seed) [public_key, secret_key] ensure seed.noaccess if seed.is_a?(Sodium::SecretBuffer) end |