DeviceKey

interface DeviceKey

One device's own key: it signs, and its id is the public name other devices check those signatures against.

A device proves it is the same device as before by signing something fresh with this key. Nobody else can make that signature, and the key never needs to leave the device to make it, so the private half has no accessor here. A key kept in a hardware or browser store that will not hand its bytes out at all is still a DeviceKey: all it has to do is sign.

generate makes a software key from the platform's own random source. It lives only as long as the object does; a key that outlives the process comes from a key store.

Samples

// This device's key. A real app gets it from a key store, so it is the same key tomorrow.
val key = DeviceKey.generate()

// The app signs its own bytes under a context naming what they are for; anyone holding only the
// device's public name can check the signature.
val move = "move 12: e4".encodeToByteArray()
val signature = key.signApp("chess/move", move)
check(key.id.verifyApp("chess/move", move, signature))

// A signature proves this key, this context and this message, nothing else.
val someoneElse = DeviceKey.generate()
check(!someoneElse.id.verifyApp("chess/move", move, signature))
check(!key.id.verifyApp("chess/resign", move, signature))
check(!key.id.verifyApp("chess/move", "move 12: d4".encodeToByteArray(), signature))

// "kuilt" is kuilt's own context, so no app signature can pass for one of its statements.
val refused = try {
    key.signApp("kuilt/admit-proof", move)
    false
} catch (_: IllegalArgumentException) {
    true
}
check(refused)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
abstract val id: DidKey

This key's public name. DidKey.verify on it accepts every signature sign makes, and DidKey.verifyApp every one signApp makes.

Functions

Link copied to clipboard
abstract suspend fun sign(message: ByteArray): ByteArray

Signs message: ECDSA on P-256 over SHA-256 of message, as the 64-byte raw r ‖ s form that id's DidKey.verify accepts.

Link copied to clipboard
suspend fun DeviceKey.signApp(context: String, message: ByteArray): ByteArray

Signs message for the app, under context: the signature DidKey.verifyApp accepts for the same context and message, and never a signature over one of kuilt's own statements.