Scoop API

scoop:scoop.crypto:0.1.0

src/cipher.scoop

Selected production source · src/cipher.scoop

1package scoop.crypto
3import scoop.crypto.native.*
5/**
6 * The supported AES-GCM key sizes. Both variants require a 12-byte nonce and produce a 16-byte
7 * authentication tag.
8 */
9public enum AeadAlgorithm {
10 /**
11 * AES-GCM with a 16-byte key.
12 */
13 Aes128Gcm,
14 /**
15 * AES-GCM with a 32-byte key.
16 */
17 Aes256Gcm }
18/**
19 * Ciphertext and its separate 16-byte GCM authentication tag; the nonce and associated data are
20 * supplied separately.
21 */
22public struct AeadCiphertext(val ciphertext: Array<Byte>, val tag: Array<Byte>)
24private fun aeadCode(algorithm: AeadAlgorithm, key: SecretKey, nonce: List<Byte>): Int {
25 val size = if (algorithm == AeadAlgorithm.Aes128Gcm) 16L else 32L
26 if (key.size != size) throw IllegalArgumentException(Some("AES-GCM key length does not match algorithm"))
27 if (nonce.size != 12L) throw IllegalArgumentException(Some("AES-GCM nonce must contain 12 bytes"))
28 return if (algorithm == AeadAlgorithm.Aes128Gcm) 0 else 1
31/**
32 * Encrypts and authenticates plaintext and associated data with AES-GCM. The caller must use a
33 * fresh nonce for each encryption under the same key. Key size and 12-byte nonce length are
34 * checked.
35 */
36public fun encrypt(algorithm: AeadAlgorithm, key: SecretKey, nonce: List<Byte>, plaintext: List<Byte>, aad: List<Byte> = Array<Byte>(0L) { _ -> 0.toByte() }): AeadCiphertext {
37 val code = aeadCode(algorithm, key, nonce)
38 val bytes = transform(code, true, key, nonce, aad, plaintext, Array<Byte>(0L) { _ -> 0.toByte() })!!
39 val count = bytes.size - 16L
40 return AeadCiphertext(Array<Byte>(count) { index -> bytes[index] },
41 Array<Byte>(16L) { index -> bytes[count + index] })
44// Authenticates and decrypts; only an authentication mismatch returns None.
45/**
46 * Authenticates before returning plaintext. Only authentication mismatch returns None; invalid
47 * sizes and backend failures throw.
48 */
49public fun decryptOrNone(algorithm: AeadAlgorithm, key: SecretKey, nonce: List<Byte>, encrypted: AeadCiphertext, aad: List<Byte> = Array<Byte>(0L) { _ -> 0.toByte() }): Array<Byte>? {
50 val code = aeadCode(algorithm, key, nonce)
51 if (encrypted.tag.size != 16L) throw IllegalArgumentException(Some("AES-GCM tag must contain 16 bytes"))
52 return transform(code, false, key, nonce, aad, encrypted.ciphertext, encrypted.tag)
55private fun transform(code: Int, encrypting: Boolean, key: SecretKey, nonce: List<Byte>, aad: List<Byte>, input: List<Byte>, tag: List<Byte>): Array<Byte>? {
56 val iv = nonce.toArray()
57 val associated = aad.toArray()
58 val bytes = input.toArray()
59 val auth = tag.toArray()
60 val handle = key.nativeHandle()
61 @Unsafe { return iv.withDataPointer { ivPointer, _ ->
62 @Unsafe { return@withDataPointer associated.withDataPointer { aadPointer, aadCount ->
63 @Unsafe { return@withDataPointer bytes.withDataPointer { dataPointer, count ->
64 @Unsafe { return@withDataPointer auth.withDataPointer { tagPointer, _ ->
65 @Unsafe {
66 val result = nativeAead(code, if (encrypting) 1 else 0, handle,
67 ivPointer, aadPointer, aadCount, dataPointer, count,
68 if (encrypting) None else Some(tagPointer))
69 val owned = cryptoValue(result, "AES-GCM")
70 return@withDataPointer if (owned == 0UL) None else Some(consumeCryptoBytes(owned))
71 }
72 } }
73 } }
74 } }
75 } }