| ... | @@ -40,7 +40,7 @@ const Salsa20NonVecImpl = struct { | ... | @@ -40,7 +40,7 @@ const Salsa20NonVecImpl = struct { |
| 40 | d: u6, | 40 | d: u6, |
| 41 | }; | 41 | }; |
| 42 | | 42 | |
| 43 | inline fn Rp(comptime a: usize, comptime b: usize, comptime c: usize, comptime d: u6) QuarterRound { | 43 | inline fn Rp(a: usize, b: usize, c: usize, d: u6) QuarterRound { |
| 44 | return QuarterRound{ | 44 | return QuarterRound{ |
| 45 | .a = a, | 45 | .a = a, |
| 46 | .b = b, | 46 | .b = b, |
| ... | @@ -82,7 +82,7 @@ const Salsa20NonVecImpl = struct { | ... | @@ -82,7 +82,7 @@ const Salsa20NonVecImpl = struct { |
| 82 | } | 82 | } |
| 83 | } | 83 | } |
| 84 | | 84 | |
| 85 | fn salsa20Internal(out: []u8, in: []const u8, key: [8]u32, d: [4]u32) void { | 85 | fn salsa20Xor(out: []u8, in: []const u8, key: [8]u32, d: [4]u32) void { |
| 86 | var ctx = initContext(key, d); | 86 | var ctx = initContext(key, d); |
| 87 | var x: BlockVec = undefined; | 87 | var x: BlockVec = undefined; |
| 88 | var buf: [64]u8 = undefined; | 88 | var buf: [64]u8 = undefined; |
| ... | @@ -174,7 +174,7 @@ pub const Salsa20 = struct { | ... | @@ -174,7 +174,7 @@ pub const Salsa20 = struct { |
| 174 | d[1] = mem.readIntLittle(u32, nonce[4..8]); | 174 | d[1] = mem.readIntLittle(u32, nonce[4..8]); |
| 175 | d[2] = @truncate(u32, counter); | 175 | d[2] = @truncate(u32, counter); |
| 176 | d[3] = @truncate(u32, counter >> 32); | 176 | d[3] = @truncate(u32, counter >> 32); |
| 177 | Salsa20Impl.salsa20Internal(out, in, keyToWords(key), d); | 177 | Salsa20Impl.salsa20Xor(out, in, keyToWords(key), d); |
| 178 | } | 178 | } |
| 179 | }; | 179 | }; |
| 180 | | 180 | |
| ... | @@ -244,7 +244,7 @@ pub const XSalsa20Poly1305 = struct { | ... | @@ -244,7 +244,7 @@ pub const XSalsa20Poly1305 = struct { |
| 244 | mac.final(&computedTag); | 244 | mac.final(&computedTag); |
| 245 | var acc: u8 = 0; | 245 | var acc: u8 = 0; |
| 246 | for (computedTag) |_, i| { | 246 | for (computedTag) |_, i| { |
| 247 | acc |= (computedTag[i] ^ tag[i]); | 247 | acc |= computedTag[i] ^ tag[i]; |
| 248 | } | 248 | } |
| 249 | if (acc != 0) { | 249 | if (acc != 0) { |
| 250 | mem.secureZero(u8, &computedTag); | 250 | mem.secureZero(u8, &computedTag); |
| ... | @@ -261,7 +261,7 @@ pub const XSalsa20Poly1305 = struct { | ... | @@ -261,7 +261,7 @@ pub const XSalsa20Poly1305 = struct { |
| 261 | /// A secret key shared by all the recipients must be already known in order to use this API. | 261 | /// A secret key shared by all the recipients must be already known in order to use this API. |
| 262 | /// | 262 | /// |
| 263 | /// Nonces are 192-bit large and can safely be chosen with a random number generator. | 263 | /// Nonces are 192-bit large and can safely be chosen with a random number generator. |
| 264 | pub const secretBox = struct { | 264 | pub const SecretBox = struct { |
| 265 | /// Key length in bytes. | 265 | /// Key length in bytes. |
| 266 | pub const key_length = XSalsa20Poly1305.key_length; | 266 | pub const key_length = XSalsa20Poly1305.key_length; |
| 267 | /// Nonce length in bytes. | 267 | /// Nonce length in bytes. |
| ... | @@ -295,7 +295,7 @@ pub const secretBox = struct { | ... | @@ -295,7 +295,7 @@ pub const secretBox = struct { |
| 295 | /// and is decrypted using the recipient's secret key and the sender's public key. | 295 | /// and is decrypted using the recipient's secret key and the sender's public key. |
| 296 | /// | 296 | /// |
| 297 | /// Nonces are 192-bit large and can safely be chosen with a random number generator. | 297 | /// Nonces are 192-bit large and can safely be chosen with a random number generator. |
| 298 | pub const box = struct { | 298 | pub const Box = struct { |
| 299 | /// Public key length in bytes. | 299 | /// Public key length in bytes. |
| 300 | pub const public_length = X25519.public_length; | 300 | pub const public_length = X25519.public_length; |
| 301 | /// Secret key length in bytes. | 301 | /// Secret key length in bytes. |
| ... | @@ -323,13 +323,13 @@ pub const box = struct { | ... | @@ -323,13 +323,13 @@ pub const box = struct { |
| 323 | /// Encrypt and authenticate a message using a recipient's public key `public_key` and a sender's `secret_key`. | 323 | /// Encrypt and authenticate a message using a recipient's public key `public_key` and a sender's `secret_key`. |
| 324 | pub fn seal(c: []u8, m: []const u8, npub: [nonce_length]u8, public_key: [public_length]u8, secret_key: [secret_length]u8) !void { | 324 | pub fn seal(c: []u8, m: []const u8, npub: [nonce_length]u8, public_key: [public_length]u8, secret_key: [secret_length]u8) !void { |
| 325 | const shared_key = try createSharedSecret(public_key, secret_key); | 325 | const shared_key = try createSharedSecret(public_key, secret_key); |
| 326 | return secretBox.seal(c, m, npub, shared_key); | 326 | return SecretBox.seal(c, m, npub, shared_key); |
| 327 | } | 327 | } |
| 328 | | 328 | |
| 329 | /// Verify and decrypt a message using a recipient's secret key `public_key` and a sender's `public_key`. | 329 | /// Verify and decrypt a message using a recipient's secret key `public_key` and a sender's `public_key`. |
| 330 | pub fn open(m: []u8, c: []const u8, npub: [nonce_length]u8, public_key: [public_length]u8, secret_key: [secret_length]u8) !void { | 330 | pub fn open(m: []u8, c: []const u8, npub: [nonce_length]u8, public_key: [public_length]u8, secret_key: [secret_length]u8) !void { |
| 331 | const shared_key = try createSharedSecret(public_key, secret_key); | 331 | const shared_key = try createSharedSecret(public_key, secret_key); |
| 332 | return secretBox.open(m, c, npub, shared_key); | 332 | return SecretBox.open(m, c, npub, shared_key); |
| 333 | } | 333 | } |
| 334 | }; | 334 | }; |
| 335 | | 335 | |
| ... | @@ -340,20 +340,20 @@ pub const box = struct { | ... | @@ -340,20 +340,20 @@ pub const box = struct { |
| 340 | /// While the recipient can verify the integrity of the message, it cannot verify the identity of the sender. | 340 | /// While the recipient can verify the integrity of the message, it cannot verify the identity of the sender. |
| 341 | /// | 341 | /// |
| 342 | /// A message is encrypted using an ephemeral key pair, whose secret part is destroyed right after the encryption process. | 342 | /// A message is encrypted using an ephemeral key pair, whose secret part is destroyed right after the encryption process. |
| 343 | pub const sealedBox = struct { | 343 | pub const SealedBox = struct { |
| 344 | pub const public_length = box.public_length; | 344 | pub const public_length = Box.public_length; |
| 345 | pub const secret_length = box.secret_length; | 345 | pub const secret_length = Box.secret_length; |
| 346 | pub const seed_length = box.seed_length; | 346 | pub const seed_length = Box.seed_length; |
| 347 | pub const seal_length = box.public_length + box.tag_length; | 347 | pub const seal_length = Box.public_length + Box.tag_length; |
| 348 | | 348 | |
| 349 | /// A key pair. | 349 | /// A key pair. |
| 350 | pub const KeyPair = box.KeyPair; | 350 | pub const KeyPair = Box.KeyPair; |
| 351 | | 351 | |
| 352 | fn createNonce(pk1: [public_length]u8, pk2: [public_length]u8) [box.nonce_length]u8 { | 352 | fn createNonce(pk1: [public_length]u8, pk2: [public_length]u8) [Box.nonce_length]u8 { |
| 353 | var hasher = Blake2b(box.nonce_length * 8).init(.{}); | 353 | var hasher = Blake2b(Box.nonce_length * 8).init(.{}); |
| 354 | hasher.update(&pk1); | 354 | hasher.update(&pk1); |
| 355 | hasher.update(&pk2); | 355 | hasher.update(&pk2); |
| 356 | var nonce: [box.nonce_length]u8 = undefined; | 356 | var nonce: [Box.nonce_length]u8 = undefined; |
| 357 | hasher.final(&nonce); | 357 | hasher.final(&nonce); |
| 358 | return nonce; | 358 | return nonce; |
| 359 | } | 359 | } |
| ... | @@ -365,7 +365,7 @@ pub const sealedBox = struct { | ... | @@ -365,7 +365,7 @@ pub const sealedBox = struct { |
| 365 | var ekp = try KeyPair.create(null); | 365 | var ekp = try KeyPair.create(null); |
| 366 | const nonce = createNonce(ekp.public_key, public_key); | 366 | const nonce = createNonce(ekp.public_key, public_key); |
| 367 | mem.copy(u8, c[0..public_length], ekp.public_key[0..]); | 367 | mem.copy(u8, c[0..public_length], ekp.public_key[0..]); |
| 368 | try box.seal(c[box.public_length..], m, nonce, public_key, ekp.secret_key); | 368 | try Box.seal(c[Box.public_length..], m, nonce, public_key, ekp.secret_key); |
| 369 | mem.secureZero(u8, ekp.secret_key[0..]); | 369 | mem.secureZero(u8, ekp.secret_key[0..]); |
| 370 | } | 370 | } |
| 371 | | 371 | |
| ... | @@ -377,7 +377,7 @@ pub const sealedBox = struct { | ... | @@ -377,7 +377,7 @@ pub const sealedBox = struct { |
| 377 | } | 377 | } |
| 378 | const epk = c[0..public_length]; | 378 | const epk = c[0..public_length]; |
| 379 | const nonce = createNonce(epk.*, keypair.public_key); | 379 | const nonce = createNonce(epk.*, keypair.public_key); |
| 380 | return box.open(m, c[public_length..], nonce, epk.*, keypair.secret_key); | 380 | return Box.open(m, c[public_length..], nonce, epk.*, keypair.secret_key); |
| 381 | } | 381 | } |
| 382 | }; | 382 | }; |
| 383 | | 383 | |
| ... | @@ -400,37 +400,37 @@ test "xsalsa20poly1305 secretbox" { | ... | @@ -400,37 +400,37 @@ test "xsalsa20poly1305 secretbox" { |
| 400 | var msg: [100]u8 = undefined; | 400 | var msg: [100]u8 = undefined; |
| 401 | var msg2: [msg.len]u8 = undefined; | 401 | var msg2: [msg.len]u8 = undefined; |
| 402 | var key: [XSalsa20Poly1305.key_length]u8 = undefined; | 402 | var key: [XSalsa20Poly1305.key_length]u8 = undefined; |
| 403 | var nonce: [box.nonce_length]u8 = undefined; | 403 | var nonce: [Box.nonce_length]u8 = undefined; |
| 404 | var boxed: [msg.len + box.tag_length]u8 = undefined; | 404 | var boxed: [msg.len + Box.tag_length]u8 = undefined; |
| 405 | try crypto.randomBytes(&msg); | 405 | try crypto.randomBytes(&msg); |
| 406 | try crypto.randomBytes(&key); | 406 | try crypto.randomBytes(&key); |
| 407 | try crypto.randomBytes(&nonce); | 407 | try crypto.randomBytes(&nonce); |
| 408 | | 408 | |
| 409 | secretBox.seal(boxed[0..], msg[0..], nonce, key); | 409 | SecretBox.seal(boxed[0..], msg[0..], nonce, key); |
| 410 | try secretBox.open(msg2[0..], boxed[0..], nonce, key); | 410 | try SecretBox.open(msg2[0..], boxed[0..], nonce, key); |
| 411 | } | 411 | } |
| 412 | | 412 | |
| 413 | test "xsalsa20poly1305 box" { | 413 | test "xsalsa20poly1305 box" { |
| 414 | var msg: [100]u8 = undefined; | 414 | var msg: [100]u8 = undefined; |
| 415 | var msg2: [msg.len]u8 = undefined; | 415 | var msg2: [msg.len]u8 = undefined; |
| 416 | var nonce: [box.nonce_length]u8 = undefined; | 416 | var nonce: [Box.nonce_length]u8 = undefined; |
| 417 | var boxed: [msg.len + box.tag_length]u8 = undefined; | 417 | var boxed: [msg.len + Box.tag_length]u8 = undefined; |
| 418 | try crypto.randomBytes(&msg); | 418 | try crypto.randomBytes(&msg); |
| 419 | try crypto.randomBytes(&nonce); | 419 | try crypto.randomBytes(&nonce); |
| 420 | | 420 | |
| 421 | var kp1 = try box.KeyPair.create(null); | 421 | var kp1 = try Box.KeyPair.create(null); |
| 422 | var kp2 = try box.KeyPair.create(null); | 422 | var kp2 = try Box.KeyPair.create(null); |
| 423 | try box.seal(boxed[0..], msg[0..], nonce, kp1.public_key, kp2.secret_key); | 423 | try Box.seal(boxed[0..], msg[0..], nonce, kp1.public_key, kp2.secret_key); |
| 424 | try box.open(msg2[0..], boxed[0..], nonce, kp2.public_key, kp1.secret_key); | 424 | try Box.open(msg2[0..], boxed[0..], nonce, kp2.public_key, kp1.secret_key); |
| 425 | } | 425 | } |
| 426 | | 426 | |
| 427 | test "xsalsa20poly1305 sealedbox" { | 427 | test "xsalsa20poly1305 sealedbox" { |
| 428 | var msg: [100]u8 = undefined; | 428 | var msg: [100]u8 = undefined; |
| 429 | var msg2: [msg.len]u8 = undefined; | 429 | var msg2: [msg.len]u8 = undefined; |
| 430 | var boxed: [msg.len + sealedBox.seal_length]u8 = undefined; | 430 | var boxed: [msg.len + SealedBox.seal_length]u8 = undefined; |
| 431 | try crypto.randomBytes(&msg); | 431 | try crypto.randomBytes(&msg); |
| 432 | | 432 | |
| 433 | var kp = try box.KeyPair.create(null); | 433 | var kp = try Box.KeyPair.create(null); |
| 434 | try sealedBox.seal(boxed[0..], msg[0..], kp.public_key); | 434 | try SealedBox.seal(boxed[0..], msg[0..], kp.public_key); |
| 435 | try sealedBox.open(msg2[0..], boxed[0..], kp); | 435 | try SealedBox.open(msg2[0..], boxed[0..], kp); |
| 436 | } | 436 | } |