| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use bytes; |
| 5 |
use crypto::math; |
| 6 |
use endian; |
| 7 |
use errors; |
| 8 |
use hash; |
| 9 |
use io; |
| 10 |
|
| 11 |
export type default = void; |
| 12 |
|
| 13 |
// Required minimum buffer size for [[pss_verify]] |
| 14 |
export def PSS_VERIFYBUFSZ = PUBEXP_BUFSZ + ((BITSZ + 7) / 8); |
| 15 |
|
| 16 |
// Required minimum buffer size for [[pss_sign]] |
| 17 |
export def PSS_SIGNBUFSZ = PRIVEXP_BUFSZ; |
| 18 |
|
| 19 |
// Signs the hash 'msghash' using the private key 'privkey' by applying the PSS |
| 20 |
// signature scheme as defined in RFC 8017. 'sig' must be in the the size of the |
| 21 |
// modulus n (see [[privkey_nsize]]) |
| 22 |
// |
| 23 |
// It is recommended that 'hf' is the same hash function that was used to |
| 24 |
// generate 'msgmhash'. 'buf' needs to be at least the size of |
| 25 |
// [[PSS_SIGNBUFSZ]]. 'rand' must be an [[io::reader]] that returns a |
| 26 |
// cryptographiclly random data on read like [[crypto::random::stream]]. The |
| 27 |
// expected size of the salt is provided with 'saltsz'. Default is the maximum |
| 28 |
// possible salt size. |
| 29 |
// |
| 30 |
// Returns [[errors::invalid]], if one of the parameters are invalid. |
| 31 |
// [[errors::overflow]] is returned, if 'buf' is to small. Errors that occur by |
| 32 |
// reading from 'rand' are returned as [[io::error]]. |
| 33 |
export fn pss_sign( |
| 34 |
privkey: []u8, |
| 35 |
msghash: []u8, |
| 36 |
sig: []u8, |
| 37 |
hf: *hash::hash, |
| 38 |
rand: io::handle, |
| 39 |
buf: []u8, |
| 40 |
saltsz: (size | default) = default, |
| 41 |
) (void | error | io::error) = { |
| 42 |
let priv = privkey_params(privkey); |
| 43 |
|
| 44 |
// Use var names that match the rfc. |
| 45 |
let embits = priv.nbitlen - 1; |
| 46 |
if (len(sig) != (embits + 7) / 8) { |
| 47 |
return errors::invalid: error; |
| 48 |
}; |
| 49 |
let em = sig; |
| 50 |
let hlen = len(msghash); |
| 51 |
let emlen = len(em); |
| 52 |
const slen = dsaltsz(len(sig), hlen, saltsz); |
| 53 |
|
| 54 |
if (emlen < hlen + slen + 2) { |
| 55 |
return errors::invalid: error; |
| 56 |
}; |
| 57 |
|
| 58 |
let db = em[..emlen - hlen - 1]; |
| 59 |
db[..] = [0...]; |
| 60 |
db[len(db) - slen - 1] = 0x01; |
| 61 |
let salt = db[len(db) - slen..]; |
| 62 |
io::readall(rand, salt)?; |
| 63 |
|
| 64 |
let h = em[emlen - hlen - 1..emlen - 1]; |
| 65 |
const padding: [8]u8 = [0...]; |
| 66 |
hash::reset(hf); |
| 67 |
hash::write(hf, padding); |
| 68 |
hash::write(hf, msghash); |
| 69 |
hash::write(hf, salt); |
| 70 |
hash::sum(hf, h); |
| 71 |
|
| 72 |
mgfxor(db, hf, h, buf); |
| 73 |
|
| 74 |
em[0] &= 0xff >> (8*emlen - embits): u8; |
| 75 |
em[len(db)..emlen - 1] = h[..]; |
| 76 |
em[emlen - 1] = 0xbc; |
| 77 |
|
| 78 |
privexp(&priv, em, buf)?; |
| 79 |
}; |
| 80 |
|
| 81 |
fn dsaltsz(nsz: size, hsz: size, s: (size | default)) size = { |
| 82 |
match (s) { |
| 83 |
case let s: size => |
| 84 |
return s; |
| 85 |
case default => |
| 86 |
return nsz - hsz - 2; |
| 87 |
}; |
| 88 |
}; |
| 89 |
|
| 90 |
// Verifies a PSS signature 'sig' of the mesage hash 'msghash' using the public |
| 91 |
// key 'pupkey' as defined in RFC 8017. |
| 92 |
// |
| 93 |
// 'hf' must be the hash that was used to create the signature. 'buf' needs to |
| 94 |
// be at least the size of [[PSS_VERIFYBUFSZ]]. The expected size of the salt is |
| 95 |
// provided with 'saltsz'. Default is the maximum possible salt size. The |
| 96 |
// function will fail, if the signature's salt size does not match the expected. |
| 97 |
// |
| 98 |
// Returns [[badsig]], if the signature verification fails. [[errors::overflow]] |
| 99 |
// is returned, if 'buf' is to small. |
| 100 |
export fn pss_verify( |
| 101 |
pubkey: []u8, |
| 102 |
msghash: []u8, |
| 103 |
sig: []u8, |
| 104 |
hf: *hash::hash, |
| 105 |
buf: []u8, |
| 106 |
saltsz: (size | default) = default, |
| 107 |
) (void | error) = { |
| 108 |
let pub = pubkey_params(pubkey); |
| 109 |
if (len(sig) != len(pub.n)) { |
| 110 |
return badsig; |
| 111 |
}; |
| 112 |
|
| 113 |
// rename some variables to match the ones in the RFC |
| 114 |
let mhash = msghash; |
| 115 |
const hlen = hash::sz(hf); |
| 116 |
const slen = dsaltsz(len(pub.n), hlen, saltsz); |
| 117 |
let em = buf[..len(sig)]; |
| 118 |
const emlen = len(em); |
| 119 |
em[..] = sig[..]; |
| 120 |
|
| 121 |
if (emlen < hlen + slen + 2) { |
| 122 |
return badsig; |
| 123 |
}; |
| 124 |
|
| 125 |
let pubbuf = buf[len(sig)..]; |
| 126 |
match (pubexp(&pub, em, pubbuf)) { |
| 127 |
case void => void; |
| 128 |
case errors::invalid => |
| 129 |
return badsig; |
| 130 |
case let e: error => |
| 131 |
return e; |
| 132 |
}; |
| 133 |
|
| 134 |
if (em[emlen - 1] != 0xbc) { |
| 135 |
return badsig; |
| 136 |
}; |
| 137 |
|
| 138 |
const maskdbsz = emlen - hlen - 1; |
| 139 |
let maskeddb = em[..maskdbsz]; |
| 140 |
let h = em[maskdbsz..maskdbsz + hlen]; |
| 141 |
|
| 142 |
const embitlen = pubkey_nbitlen(pubkey) - 1; |
| 143 |
const zerobitsh = 8 - (8*len(em) - embitlen); |
| 144 |
if (maskeddb[0] >> zerobitsh != 0) { |
| 145 |
return badsig; |
| 146 |
}; |
| 147 |
|
| 148 |
let db = maskeddb; |
| 149 |
mgfxor(db, hf, h, pubbuf); |
| 150 |
db[0] &= 0xff >> (8*len(em) - embitlen): u8; |
| 151 |
|
| 152 |
const seppos = len(em) - hlen - slen - 2; |
| 153 |
for (let i = 0z; i < seppos; i += 1) { |
| 154 |
if (db[i] != 0x00) { |
| 155 |
return badsig; |
| 156 |
}; |
| 157 |
}; |
| 158 |
if (db[seppos] != 0x01) { |
| 159 |
return badsig; |
| 160 |
}; |
| 161 |
|
| 162 |
const salt = db[len(db) - slen..]; |
| 163 |
const padding: [8]u8 = [0...]; |
| 164 |
hash::reset(hf); |
| 165 |
hash::write(hf, padding); |
| 166 |
hash::write(hf, mhash); |
| 167 |
hash::write(hf, salt); |
| 168 |
|
| 169 |
let genh = pubbuf[..hlen]; |
| 170 |
hash::sum(hf, genh); |
| 171 |
|
| 172 |
if (math::eqslice(genh, h) != 1) { |
| 173 |
return badsig; |
| 174 |
}; |
| 175 |
}; |
| 176 |
|
| 177 |
// dest = dest XOR mgf(h, seed, len(dest)). 'buf' must be hash::sz(h) bytes |
| 178 |
// long. |
| 179 |
fn mgfxor(dest: []u8, h: *hash::hash, seed: []u8, buf: []u8) void = { |
| 180 |
assert(len(buf) >= hash::sz(h)); |
| 181 |
|
| 182 |
let ctrbuf: [4]u8 = [0...]; |
| 183 |
let sum = buf[..hash::sz(h)]; |
| 184 |
const iterations = (len(dest) + len(sum) - 1) / len(sum); |
| 185 |
|
| 186 |
for (let ctr: u32 = 0; ctr < iterations; ctr += 1) { |
| 187 |
endian::beputu32(ctrbuf, ctr); |
| 188 |
hash::reset(h); |
| 189 |
hash::write(h, seed); |
| 190 |
hash::write(h, ctrbuf); |
| 191 |
hash::sum(h, sum); |
| 192 |
|
| 193 |
const start = ctr * len(sum); |
| 194 |
const remain = len(dest) - start; |
| 195 |
const chunksz = if (remain < len(sum)) remain else len(sum); |
| 196 |
|
| 197 |
let chunk = dest[start..start + chunksz]; |
| 198 |
math::xor(chunk, chunk, sum[..chunksz]); |
| 199 |
}; |
| 200 |
|
| 201 |
bytes::zero(sum); |
| 202 |
}; |