| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
// Implements the Ed25519 signature scheme. |
| 5 |
// |
| 6 |
// This implementation is a straightforward port of TweetNaCl, |
| 7 |
// with the API of crypto/ed25519 from the Go standard library. |
| 8 |
use bytes; |
| 9 |
use crypto::sha512; |
| 10 |
use hash; |
| 11 |
|
| 12 |
// The size of an Ed25519 seed. |
| 13 |
export def SEEDSZ: size = 32; |
| 14 |
|
| 15 |
// The size of an Ed25519 public key. |
| 16 |
export def PUBKEYSZ: size = 32; |
| 17 |
|
| 18 |
// The size of an Ed25519 private key. |
| 19 |
export def PRIVKEYSZ: size = 64; |
| 20 |
|
| 21 |
// The size of an Ed25519 signature. |
| 22 |
export def SIGNATURESZ: size = 64; |
| 23 |
|
| 24 |
export type privkey = [PRIVKEYSZ]u8; |
| 25 |
export type pubkey = [PUBKEYSZ]u8; |
| 26 |
export type seed = [SEEDSZ]u8; |
| 27 |
|
| 28 |
// Invalid signature |
| 29 |
export type invalidsig = !void; |
| 30 |
|
| 31 |
// Invalid key |
| 32 |
export type invalidkey = !void; |
| 33 |
|
| 34 |
// Possible errors returned by this module. |
| 35 |
export type error = !(invalidsig | invalidkey); |
| 36 |
|
| 37 |
// String representation of error 'e'. |
| 38 |
export fn strerror(e: error) str = { |
| 39 |
match (e) { |
| 40 |
case invalidsig => |
| 41 |
return "invalid signature"; |
| 42 |
case invalidkey => |
| 43 |
return "invalid key"; |
| 44 |
}; |
| 45 |
}; |
| 46 |
|
| 47 |
// Derives a new Ed25519 private key from a given seed. The seed must be |
| 48 |
// initialized to cryptographically random data; [[crypto::random::]] is |
| 49 |
// recommended for this purpose. |
| 50 |
export fn privkey_init(priv: []u8, seed: []u8) void = { |
| 51 |
assert(len(priv) == PRIVKEYSZ); |
| 52 |
assert(len(seed) == SEEDSZ); |
| 53 |
|
| 54 |
let h: [64]u8 = [0...]; |
| 55 |
let sha = sha512::sha512(); |
| 56 |
hash::write(&sha, seed[..]); |
| 57 |
hash::sum(&sha, h[..]); |
| 58 |
hash::close(&sha); |
| 59 |
|
| 60 |
let s: scalar = [0...]; |
| 61 |
s[..] = h[..SCALARSZ]; |
| 62 |
scalar_clamp(&s); |
| 63 |
|
| 64 |
let A = point { ... }; |
| 65 |
scalarmult_base(&A, &s); |
| 66 |
let A_bytes: [POINTSZ]u8 = [0...]; |
| 67 |
point_encode(&A_bytes, &A); |
| 68 |
|
| 69 |
priv[0..SEEDSZ] = seed[..]; |
| 70 |
priv[SEEDSZ..PRIVKEYSZ] = A_bytes[..]; |
| 71 |
}; |
| 72 |
|
| 73 |
// Derive the public key for a given private key. ' |
| 74 |
export fn privkey_getpubkey(priv: []u8) pubkey = { |
| 75 |
assert(len(priv) == PRIVKEYSZ); |
| 76 |
let pk: pubkey = [0...]; |
| 77 |
pk[0..] = priv[SEEDSZ..]; |
| 78 |
return pk; |
| 79 |
}; |
| 80 |
|
| 81 |
// Signs a message with a private key, returning the signature. |
| 82 |
export fn sign(priv: []u8, msg: []u8) [SIGNATURESZ]u8 = { |
| 83 |
assert(len(priv) == PRIVKEYSZ); |
| 84 |
|
| 85 |
let h: [64]u8 = [0...]; |
| 86 |
let sha = sha512::sha512(); |
| 87 |
hash::write(&sha, priv[0..SEEDSZ]); |
| 88 |
hash::sum(&sha, h); |
| 89 |
let esk: scalar = [0...]; |
| 90 |
esk[..] = h[0..32]; |
| 91 |
scalar_clamp(&esk); |
| 92 |
|
| 93 |
hash::reset(&sha); |
| 94 |
hash::write(&sha, h[32..64]); |
| 95 |
hash::write(&sha, msg); |
| 96 |
let msg_digest: [64]u8 = [0...]; |
| 97 |
hash::sum(&sha, msg_digest); |
| 98 |
let msg_reduced: scalar = [0...]; |
| 99 |
scalar_reduce(&msg_reduced, &msg_digest); |
| 100 |
|
| 101 |
let R = point {...}; |
| 102 |
scalarmult_base(&R, &msg_reduced); |
| 103 |
let R_bytes: [POINTSZ]u8 = [0...]; |
| 104 |
point_encode(&R_bytes, &R); |
| 105 |
|
| 106 |
hash::reset(&sha); |
| 107 |
hash::write(&sha, R_bytes[..]); |
| 108 |
hash::write(&sha, priv[32..64]); |
| 109 |
hash::write(&sha, msg); |
| 110 |
let hram: [64]u8 = [0...]; |
| 111 |
hash::sum(&sha, hram); |
| 112 |
hash::close(&sha); |
| 113 |
let hram_reduced: scalar = [0...]; |
| 114 |
scalar_reduce(&hram_reduced, &hram); |
| 115 |
|
| 116 |
let s: scalar = [0...]; |
| 117 |
scalar_multiply_add(&s, &hram_reduced, &esk, &msg_reduced); |
| 118 |
|
| 119 |
let sig: [SIGNATURESZ]u8 =[0...]; |
| 120 |
sig[0..32] = R_bytes[..]; |
| 121 |
sig[32..64] = s[..]; |
| 122 |
return sig; |
| 123 |
}; |
| 124 |
|
| 125 |
export type hash = struct { |
| 126 |
h: sha512::digest, |
| 127 |
pub: []u8, |
| 128 |
point: point, |
| 129 |
sig: []u8, |
| 130 |
}; |
| 131 |
|
| 132 |
// Creates a [[hash::hash]] function in that the message to be verified can |
| 133 |
// be written into. [[verify]] needs to be called afterwards, to verify |
| 134 |
// if the signature is valid for the given message. |
| 135 |
export fn verifier(pub: []u8, sig: []u8) (hash | invalidkey | invalidsig) = { |
| 136 |
if (len(pub) != PUBKEYSZ) { |
| 137 |
return invalidkey; |
| 138 |
}; |
| 139 |
|
| 140 |
if (len(sig) != SIGNATURESZ) { |
| 141 |
return invalidsig; |
| 142 |
}; |
| 143 |
|
| 144 |
let h = hash { |
| 145 |
h = sha512::sha512(), |
| 146 |
pub = pub, |
| 147 |
point = point { ... }, |
| 148 |
sig = sig, |
| 149 |
}; |
| 150 |
|
| 151 |
|
| 152 |
if (!point_decode(&h.point, pub)) { |
| 153 |
return invalidkey; |
| 154 |
}; |
| 155 |
|
| 156 |
hash::write(&h.h, sig[0..32]); |
| 157 |
hash::write(&h.h, pub[..]); |
| 158 |
return h; |
| 159 |
}; |
| 160 |
|
| 161 |
// Given a public key, verifies a signature produced with the |
| 162 |
// corresponding private key for a given message, returning true if the |
| 163 |
// signature is valid and false otherwise. |
| 164 |
export fn verify_buf(pub: []u8, msg: []u8, sig: []u8) (void | error) = { |
| 165 |
let h = verifier(pub, sig)?; |
| 166 |
hash::write(&h, msg); |
| 167 |
verify(&h)?; |
| 168 |
}; |
| 169 |
|
| 170 |
// Checks if the given hash 'h' is valid. |
| 171 |
export fn verify(h: *hash) (void | invalidsig) = { |
| 172 |
let A = point { ... }; |
| 173 |
|
| 174 |
let hram: [64]u8 = [0...]; |
| 175 |
hash::sum(&h.h, hram); |
| 176 |
hash::close(&h.h); |
| 177 |
|
| 178 |
let hram_reduced: scalar = [0...]; |
| 179 |
scalar_reduce(&hram_reduced, &hram); |
| 180 |
let check_R = point { ... }; |
| 181 |
scalarmult(&check_R, &h.point, &hram_reduced); |
| 182 |
|
| 183 |
let s: scalar = [0...]; |
| 184 |
s[..] = h.sig[32..64]; |
| 185 |
scalarmult_base(&A, &s); |
| 186 |
point_add(&check_R, &check_R, &A); |
| 187 |
let R_bytes: [POINTSZ]u8 = [0...]; |
| 188 |
point_encode(&R_bytes, &check_R); |
| 189 |
if (!bytes::equal(R_bytes, h.sig[0..32])) { |
| 190 |
return invalidsig; |
| 191 |
}; |
| 192 |
}; |