scoop:scoop.crypto:0.1.0
src/keys.scoop
Selected production source · src/keys.scoop
1
package scoop.crypto3
import scoop.crypto.native.*5
/**6
* The recognized asymmetric key family; Other represents an imported family outside these7
* supported operations.8
*/9
public enum KeyAlgorithm {10
/**11
* An RSA key.12
*/13
Rsa,14
/**15
* An elliptic-curve key on P-256.16
*/17
P256,18
/**19
* An Ed25519 signing key.20
*/21
Ed25519,22
/**23
* An imported key family not recognized by these operation selectors.24
*/25
Other }27
/**28
* An owned native asymmetric private key. Operations require a live key; close it when finished.29
*/30
public class PrivateKey private constructor(private var handle: ULong) : Closeable {31
/**32
* The recognized key family of this live key.33
*/34
public val algorithm: KeyAlgorithm get() = nativeKeyAlgorithm(nativeHandle())36
/**37
* Parses private key data into owned native handles.38
*/39
public companion object {40
/**41
* Generates an RSA private key. The bit count must be at least 2048 and divisible by 256.42
*/43
public fun generateRsa(bits: Int = 2048): PrivateKey {44
if (bits < 2048 || bits % 256 != 0)45
throw IllegalArgumentException(Some("RSA bits must be a multiple of 256 and at least 2048"))46
return generateKey(0, bits)47
}48
/**49
* Generates a private key on the P-256 elliptic curve.50
*/51
public fun generateP256(): PrivateKey = generateKey(1)52
/**53
* Generates an Ed25519 private key.54
*/55
public fun generateEd25519(): PrivateKey = generateKey(2)57
private fun generateKey(code: Int, bits: Int = 0): PrivateKey {58
@Unsafe { return PrivateKey(cryptoValue(keyGenerate(code, bits), "generate key")) }59
}60
/**61
* Parses PEM input into a new owned PrivateKey; malformed input reports a cryptographic62
* backend failure.63
*/64
public fun fromPem(pem: String): PrivateKey = PrivateKey(importKey(pem.toByteArray(), true, true))65
/**66
* Parses DER input into a new owned PrivateKey; malformed input reports a cryptographic67
* backend failure.68
*/69
public fun fromDer(der: List<Byte>): PrivateKey = PrivateKey(importKey(der, true, false))70
}72
internal fun nativeHandle(): ULong {73
if (handle == 0UL) throw IllegalStateException(Some("private key is closed"))74
return handle75
}77
/**78
* Returns an independently owned public-key handle that must be closed separately.79
*/80
public fun publicKey(): PublicKey { @Unsafe { return PublicKey(cryptoValue(keyPublic(nativeHandle()), "extract public key")) } }81
/**82
* Exports the live PrivateKey as DER data in an independent managed value.83
*/84
public fun toDer(): Array<Byte> { @Unsafe { return cryptoBytes(keyExport(nativeHandle(), 1, 0), "export private key") } }85
/**86
* Exports the live PrivateKey as PEM data in an independent managed value.87
*/88
public fun toPem(): String { @Unsafe { return String.fromUtf8Unchecked(cryptoBytes(keyExport(nativeHandle(), 1, 1), "export private key")) } }89
/**90
* Releases the native PrivateKey handle. Repeated closes are harmless; other operations after91
* close fail.92
*/93
public override fun close() {94
val owned = handle95
handle = 0UL96
if (owned != 0UL) @Unsafe { keyFree(owned) }97
}98
}100
/**101
* An independently owned native asymmetric public key. Close it after use.102
*/103
public class PublicKey internal constructor(private var handle: ULong) : Closeable {104
/**105
* The recognized key family of this live key.106
*/107
public val algorithm: KeyAlgorithm get() = nativeKeyAlgorithm(nativeHandle())109
/**110
* Parses public key data into owned native handles.111
*/112
public companion object {113
/**114
* Parses PEM input into a new owned PublicKey; malformed input reports a cryptographic115
* backend failure.116
*/117
public fun fromPem(pem: String): PublicKey = PublicKey(importKey(pem.toByteArray(), false, true))118
/**119
* Parses DER input into a new owned PublicKey; malformed input reports a cryptographic120
* backend failure.121
*/122
public fun fromDer(der: List<Byte>): PublicKey = PublicKey(importKey(der, false, false))123
}125
internal fun nativeHandle(): ULong {126
if (handle == 0UL) throw IllegalStateException(Some("public key is closed"))127
return handle128
}130
/**131
* Exports the live PublicKey as DER data in an independent managed value.132
*/133
public fun toDer(): Array<Byte> { @Unsafe { return cryptoBytes(keyExport(nativeHandle(), 0, 0), "export public key") } }134
/**135
* Exports the live PublicKey as PEM data in an independent managed value.136
*/137
public fun toPem(): String { @Unsafe { return String.fromUtf8Unchecked(cryptoBytes(keyExport(nativeHandle(), 0, 1), "export public key")) } }138
/**139
* Releases the native PublicKey handle. Repeated closes are harmless; other operations after140
* close fail.141
*/142
public override fun close() {143
val owned = handle144
handle = 0UL145
if (owned != 0UL) @Unsafe { keyFree(owned) }146
}147
}149
private fun nativeKeyAlgorithm(handle: ULong): KeyAlgorithm {150
@Unsafe { return when (keyAlgorithm(handle)) {151
case 0 -> KeyAlgorithm.Rsa152
case 1 -> KeyAlgorithm.P256153
case 2 -> KeyAlgorithm.Ed25519154
else -> KeyAlgorithm.Other155
} }156
}158
private fun importKey(input: List<Byte>, privateKey: Boolean, pem: Boolean): ULong {159
val data = input.toArray()160
@Unsafe { return data.withDataPointer { pointer, count ->161
@Unsafe { return@withDataPointer cryptoValue(keyImport(if (privateKey) 1 else 0, if (pem) 1 else 0, pointer, count), "import key") }162
} }163
}