hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit f88a995c95b3110a4f8114e37556bf26b41c92d7
parent 0aa7dfdc9d77fab0ba0735a82730e2b5eb9c1d43
Author: Armin Preiml <apreiml@strohwolke.at>
Date:   Tue, 29 Nov 2022 13:24:01 +0100

crypto::rsa: add pkcs1 sign and verify support

Signed-off-by: Armin Preiml <apreiml@strohwolke.at>

Diffstat:
Acrypto/rsa/+test/pkcs1.ha | 297+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrypto/rsa/pkcs1.ha | 161+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 458 insertions(+), 0 deletions(-)

diff --git a/crypto/rsa/+test/pkcs1.ha b/crypto/rsa/+test/pkcs1.ha @@ -0,0 +1,297 @@ +// License: MPL-2.0 +// (c) 2022 Armin Preiml <apreiml@strohwolke.at> +use bytes; +use crypto::sha1; +use crypto::sha256; +use crypto::sha512; +use hash; +use io; + +const msg: [_]u8 = [ + 0x53, 0x53, 0x48, 0x53, 0x49, 0x47, 0x00, 0x00, 0x00, 0x03, 0x6a, 0x6f, + 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x73, 0x68, 0x61, + 0x35, 0x31, 0x32, 0x00, 0x00, 0x00, 0x40, 0xce, 0x25, 0x8d, 0xf2, 0x3a, + 0x14, 0xf2, 0x80, 0xeb, 0xea, 0x9b, 0xd4, 0xd7, 0xb8, 0x94, 0xce, 0x23, + 0x98, 0xa8, 0x27, 0x30, 0x42, 0x61, 0x6a, 0xd2, 0x15, 0x59, 0x97, 0xc2, + 0xb4, 0x79, 0x8b, 0x16, 0xdc, 0x49, 0xb5, 0x26, 0x51, 0x77, 0xe3, 0x27, + 0x5c, 0xdf, 0xc0, 0xa5, 0x46, 0x97, 0xdb, 0x7b, 0x2c, 0x3b, 0x9b, 0x97, + 0x3e, 0xc9, 0xca, 0xea, 0x9a, 0xcd, 0xbd, 0x2c, 0xaa, 0xcc, 0x77, +]; + +const sig1: [_] u8 = [ + 0x3a, 0x29, 0x89, 0x83, 0xf5, 0xb6, 0x25, 0x0c, 0x00, 0xd4, 0x44, 0x4a, + 0x16, 0xa9, 0xcf, 0xb6, 0x14, 0x3d, 0xc1, 0xa9, 0x45, 0x24, 0xb1, 0x54, + 0x53, 0x2b, 0x59, 0x81, 0xf8, 0x7c, 0xdb, 0x4c, 0x16, 0xd1, 0x0e, 0x68, + 0xb8, 0x1a, 0x48, 0x02, 0x3d, 0x34, 0x08, 0x14, 0xb7, 0xc1, 0x90, 0x1f, + 0x4d, 0x7f, 0xbc, 0xa1, 0xef, 0x8c, 0xf0, 0x71, 0x24, 0x11, 0x27, 0xa7, + 0x1c, 0xc9, 0x44, 0x11, 0x65, 0x1e, 0xca, 0xa0, 0x4b, 0xb5, 0x27, 0x67, + 0xe3, 0x80, 0x4d, 0x79, 0xbf, 0x28, 0xb9, 0x85, 0xe8, 0x27, 0xad, 0x7d, + 0xca, 0x2e, 0x0d, 0xb2, 0xe6, 0x42, 0x29, 0x37, 0x8b, 0xb2, 0xc3, 0x0c, + 0x49, 0x9f, 0x78, 0x2c, 0x03, 0xe4, 0x2e, 0x83, 0x79, 0xb0, 0x6c, 0x09, + 0xf4, 0x3c, 0xc7, 0x14, 0xfa, 0x73, 0x41, 0x28, 0x39, 0x1f, 0x8a, 0x23, + 0x7b, 0x71, 0x5d, 0x15, 0x23, 0x69, 0x95, 0x1c, 0x8d, 0x50, 0x6c, 0x4e, + 0x6e, 0x7d, 0xab, 0xcf, 0xab, 0xbd, 0xa2, 0xf1, 0x18, 0x2d, 0xb3, 0x70, + 0x5d, 0x62, 0x45, 0xad, 0xa9, 0x1b, 0x40, 0x1f, 0xe8, 0xe1, 0x25, 0x19, + 0x78, 0xfd, 0x73, 0x49, 0x45, 0x09, 0x21, 0x5c, 0x25, 0xff, 0x86, 0x1d, + 0x6c, 0x63, 0xc6, 0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08, 0x6e, 0x5f, 0xe1, 0xa5, 0xeb, 0x23, 0x8a, 0xc9, 0xe3, 0xeb, + 0x6b, 0xd1, 0x6d, 0x95, 0x64, 0xc0, 0x25, 0x37, 0xc9, 0xb4, 0x14, 0x85, + 0xa9, 0x7e, 0x44, 0xbc, 0x15, 0x56, 0xdb, 0x03, 0xac, 0x4d, 0x19, 0x61, + 0x24, 0x5a, 0xc3, 0x48, 0x34, 0x72, 0xff, 0xc6, 0x97, 0xee, 0xf8, 0xf7, + 0xdb, 0x07, 0xce, 0x47, 0x61, 0x53, 0x40, 0xe7, 0xf4, 0x2b, 0xca, 0xa0, + 0xcc, 0xd6, 0xd8, 0x46, 0x5b, 0x88, 0xd9, 0xf0, 0xd9, 0x01, 0x87, 0x85, + 0x33, 0x6b, 0x34, 0xb8, 0x34, 0xc8, 0x50, 0x86, 0x1f, 0xbe, 0xf9, 0xca, + 0x07, 0x13, 0x2b, 0x31, 0x20, 0x92, 0xb2, 0xec, 0x31, 0xf0, 0xd1, 0x45, + 0xab, 0x8c, 0xaa, 0x19, 0xb6, 0x81, 0x98, 0x31, 0x74, 0xd2, 0x96, 0x4e, + 0x6a, 0xac, 0x90, 0xbe, 0xfe, 0xb7, 0xbb, 0x56, 0x3b, 0x72, 0x68, 0x0e, + 0xc4, 0xef, 0x63, 0xc9, 0xfe, 0xe8, 0xa2, 0xf6, 0x92, 0x82, 0x05, 0x24, + 0xf2, 0x52, 0xb0, 0x5a, 0x90, 0xdf, 0x04, 0xff, 0x6c, 0xb0, 0x79, 0x80, + 0x28, 0xb0, 0x64, 0x08, 0x14, 0x93, 0x12, 0x02, 0x13, 0xd3, 0x6d, 0x8c, + 0x9c, 0xfe, 0x42, 0x89, 0xcd, 0xc5, 0x94, 0x9f, 0x46, 0x72, 0xda, 0x49, + 0x4d, 0xd9, 0xa6, 0x97, 0x21, 0x80, 0xa2, 0xfa, 0x57, 0x79, 0xd0, 0xc6, + 0x83, 0x30, 0x29, 0x26, 0x09, 0x81, 0xba, 0x17, 0x60, 0x06, 0x31, 0x21, + 0x6f, 0xfc, 0xf6, 0x53, 0x93, 0xe9, 0x7f, 0xe2, 0x64, 0x56, 0xd5, 0xf5, + 0x11, 0x4f, 0x8b, 0xe1, 0xce, 0x3f, 0x41, 0x54, 0xf4, 0x5f, 0x08, 0xbc, + 0xfd, 0x13, 0x21, 0x62, 0x99, 0x4a, 0x4e, 0x8c, 0x14, 0xd2, 0x80, 0xf0, + 0x18, 0x9c, 0xbf, 0x3a, 0xb4, 0x80, 0x5b, 0x3d, 0x28, 0xa5, 0xbc, 0x12, + 0x96, 0x9d, 0xa9, 0x92, 0x01, 0xbe, 0x62, 0xbb, 0xf6, 0x1f, 0x24, 0x37, + 0xfa, 0xa2, 0xe4, 0x73, 0x65, 0x27, 0x0b, 0xc6, 0x95, 0x86, 0x2e, 0x46, +]; + +const sig512: [_]u8 = [ + 0x6e, 0xb5, 0x5f, 0xbd, 0x48, 0x09, 0x08, 0x7c, 0x01, 0xb3, 0x74, 0x26, + 0x73, 0x9d, 0x3e, 0xd5, 0x08, 0x7d, 0xe8, 0x11, 0x01, 0x21, 0x31, 0x11, + 0x91, 0x34, 0x5b, 0xe5, 0x6c, 0x37, 0x79, 0x7b, 0xdb, 0x75, 0x16, 0x62, + 0x0e, 0x7d, 0x1d, 0xd1, 0x04, 0x45, 0xbc, 0xa9, 0x79, 0xd0, 0xb1, 0x1d, + 0x1e, 0x20, 0x65, 0x37, 0x92, 0x90, 0xa8, 0xd3, 0x5f, 0x07, 0x24, 0x54, + 0x53, 0x97, 0x69, 0x84, 0xe2, 0xbb, 0xc0, 0xb5, 0x82, 0x4f, 0x29, 0xac, + 0xc9, 0x07, 0xa7, 0x75, 0x08, 0x1c, 0x0c, 0x72, 0x9f, 0x35, 0x1b, 0x75, + 0xb2, 0x79, 0x3f, 0x41, 0xa5, 0xcb, 0x9d, 0x69, 0x02, 0xa8, 0x08, 0xfe, + 0x11, 0x19, 0x2f, 0xc2, 0xdb, 0x0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xb3, 0x87, 0x79, 0xc6, + 0x24, 0xe6, 0x98, 0x58, 0xda, 0xdc, 0x0c, 0x67, 0x4c, 0x1f, 0xe7, 0xc3, + 0x26, 0xec, 0xdd, 0x7e, 0x91, 0xb0, 0x31, 0x99, 0x5c, 0x93, 0x52, 0x17, + 0xa2, 0x0f, 0xb1, 0xfb, 0x09, 0xd2, 0xa9, 0xe5, 0xdf, 0x1e, 0x5c, 0xa8, + 0xf5, 0x0c, 0x20, 0xc3, 0xe3, 0x07, 0x32, 0x1b, 0x42, 0xc1, 0x58, 0xb2, + 0x1c, 0x52, 0x7d, 0x56, 0xf8, 0x0c, 0xad, 0x03, 0xf5, 0x40, 0x07, 0x9c, + 0xf4, 0x41, 0xf5, 0x54, 0xed, 0x66, 0x11, 0xd6, 0x98, 0xa4, 0x32, 0xd2, + 0x94, 0x02, 0x74, 0xa9, 0xe1, 0x3a, 0x61, 0x18, 0x37, 0x54, 0xce, 0x03, + 0x17, 0xc9, 0xc9, 0x99, 0x22, 0xd7, 0x3f, 0x71, 0x7f, 0xf5, 0x8d, 0xa3, +]; + +const sig512_224: [_]u8 = [ + 0x65, 0xda, 0xf7, 0x42, 0x16, 0xe9, 0xcb, 0xc0, 0xed, 0x1e, 0xb7, 0xf1, + 0x18, 0xab, 0x30, 0xf4, 0xe9, 0xf3, 0x4c, 0x1f, 0x8f, 0x94, 0x61, 0x79, + 0xe1, 0x89, 0x89, 0xbb, 0x6d, 0x26, 0xcb, 0xfc, 0xaf, 0x99, 0xdc, 0x6b, + 0x16, 0x4c, 0x51, 0x4f, 0x49, 0xb0, 0xd8, 0x98, 0x89, 0x95, 0x10, 0x6f, + 0xa2, 0x50, 0x6e, 0x04, 0xaa, 0x98, 0xd7, 0x63, 0x91, 0x21, 0x8f, 0x5a, + 0xd5, 0xd1, 0x9c, 0x31, 0xe2, 0x0c, 0xd1, 0xb1, 0xf8, 0xf5, 0x06, 0x75, + 0x50, 0x33, 0x31, 0x49, 0xa4, 0xac, 0x87,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, 0x96, 0x2b, 0x9f, 0x7a, + 0x6f, 0xae, 0xc4, 0xa5, 0xe4, 0x23, 0x3b, 0x06, 0xf5, 0x95, 0x89, 0xa6, + 0x15, 0xb1, 0xd5, 0xa3, 0xdd, 0x2c, 0x17, 0xf5, 0x60, 0x5d, 0x09, 0x69, + 0x42, 0x88, 0xe7, 0x21, 0x60, 0x79, 0xe9, 0x69, 0x9d, 0x12, 0x2e, 0x6a, + 0xdf, 0x51, 0x45, 0x54, 0x91, 0x54, 0x17, 0xe7, 0x6c, 0x8c, 0xbf, 0xe3, + 0x69, 0xc2, 0xfa, 0xb4, 0x7b, 0x72, 0xda, 0x48, 0xe4, 0xd1, 0xce, 0x86, + 0x8f, 0x17, 0x5d, 0x18, 0x21, 0x72, 0x28, 0x8a, 0x78, 0xca, 0xa1, 0x90, + 0x6b, 0x6d, 0xc0, 0x39, 0xb3, 0xe9, 0x71, 0x65, 0xe8, 0xbb, 0x19, 0xf4, + 0x8b, 0x59, 0xc2, 0x9e, 0x7b, 0xa3, 0xf2, 0xcb, 0x0d, 0xa0, 0xb4, 0x32, + 0xe2, 0xdb, 0x72, 0x5c, 0x7f, 0xe2, 0x32, 0x77, 0x62, 0xf1, 0xb9, 0x34, + 0xe9, 0x95, 0x8d, 0x76, 0xa4, 0xf2, 0xd2, 0x31, 0x46, 0xc1, 0xf9, 0xdb, +]; + +const sig512_256: [_]u8 = [ + 0xb9, 0x33, 0xe4, 0x78, 0x0f, 0x91, 0xa3, 0xe4, 0x2c, 0x34, 0x9a, 0xfc, + 0x00, 0xba, 0x37, 0x8e, 0x1f, 0x94, 0xe4, 0xf2, 0xae, 0x75, 0x96, 0x9f, + 0xe1, 0x9c, 0xef, 0xf9, 0x72, 0x50, 0x21, 0x3b, 0xe6, 0xef, 0xa5, 0x3e, + 0x04, 0x9e, 0x75, 0x25, 0x1a, 0x2c, 0xa1, 0x74, 0x26, 0x74, 0x20, 0x57, + 0x2c, 0xda, 0x83, 0x1f, 0x21, 0xb0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x0c, 0x37, 0xa8, 0xff, 0x87, 0xa7, + 0x78, 0x2f, 0x68, 0xd2, 0x85, 0xa8, 0xf2, 0x93, 0x88, 0x2a, 0x90, 0x03, + 0xe1, 0x4a, 0x2e, 0x58, 0x63, 0x92, 0x21, 0xf2, 0x8c, 0x3d, 0xdd, 0x49, + 0xc1, 0x0e, 0x15, 0x1b, 0xcf, 0x21, 0xd5, 0x0d, 0x24, 0xba, 0xd6, 0x29, + 0xdb, 0xb3, 0x53, 0xfc, 0x56, 0xa9, 0x55, 0xb6, 0x1e, 0x8d, 0xd5, 0xd7, + 0x16, 0x0c, 0x65, 0x53, 0xc7, 0xda, 0xc7, 0x8b, 0x35, 0x99, 0x38, 0x80, + 0xa8, 0xe7, 0x5e, 0x57, 0x60, 0xad, 0x24, 0xe6, 0x0c, 0x4c, 0xa3, 0xbf, + 0xe5, 0x79, 0x9c, 0x1b, 0xa2, 0x1f, 0x6c, 0xf4, 0x4b, 0x50, 0x36, 0x15, + 0xf7, 0x9b, 0xab, 0x17, 0x8f, 0x15, 0x1f, 0x5b, 0x93, 0x67, 0x56, 0x70, + 0x5c, 0x5b, 0x59, 0xef, 0x1b, 0x41, 0xf9, 0x36, 0xc5, 0x6f, 0x49, 0x1e, + 0x2f, 0x99, 0xdd, 0x13, 0xb6, 0xd1, 0x2b, 0xb9, 0x68, 0x36, 0xbf, 0x02, + 0xd6, 0x27, 0x6f, 0xeb, 0x83, 0x8a, 0x77, 0x12, 0xf7, 0x07, 0xd8, 0x3f, + 0x60, 0x57, 0x5b, 0x24, 0x73, 0x64, 0x85, 0x90, 0xf0, 0xbd, 0x76, 0xcd, +]; + +@test fn pkcs1() void = { + let pk = sign3072.priv; + let priv: [PRIVKEYSIZE]u8 = [0...]; + privkey_init(priv, sign3072.priv)!; + + let pub: [PUBKEYSIZE]u8 = [0...]; + pubkey_init(pub, sign3072.pub)!; + + assertpkcs1(priv, pub, msg, sig1, pkcs1_hashalgo::SHA1); + assertpkcs1(priv, pub, msg, sig256, pkcs1_hashalgo::SHA256); + assertpkcs1(priv, pub, msg, sig384, pkcs1_hashalgo::SHA384); + assertpkcs1(priv, pub, msg, sig512, pkcs1_hashalgo::SHA512); + assertpkcs1(priv, pub, msg, sig512_224, pkcs1_hashalgo::SHA512_224); + assertpkcs1(priv, pub, msg, sig512_256, pkcs1_hashalgo::SHA512_256); +}; + +fn assertpkcs1( + priv: []u8, + pub: []u8, + msg: []u8, + sig: []u8, + algo: pkcs1_hashalgo +) void = { + let nsig: []u8 = alloc([0...], len(sig)); + defer free(nsig); + + let msghash = pkcs1_hash(msg, algo); + defer free(msghash); + + pkcs1_sign(priv, msghash, nsig, algo, pkcs1_signbuf)!; + assert(bytes::equal(nsig, sig)); + + pkcs1_verify(pub, msghash, sig, algo, pkcs1_verifybuf)!; + + const tmp = msghash[0]; + msghash[0] += 1; + assert(pkcs1_verify(pub, msghash, sig, algo, pkcs1_verifybuf) is error); + + msghash[0] = tmp; + nsig[0] += 1; + assert(pkcs1_verify(pub, msghash, nsig, algo, pkcs1_verifybuf) is error); +}; + +fn pkcs1_hash(msg: []u8, algo: pkcs1_hashalgo) []u8 = { + let h: *hash::hash = switch (algo) { + case pkcs1_hashalgo::SHA1 => + yield &sha1::sha1(); + case pkcs1_hashalgo::SHA256 => + yield &sha256::sha256(); + case pkcs1_hashalgo::SHA384 => + yield &sha512::sha384(); + case pkcs1_hashalgo::SHA512 => + yield &sha512::sha512(); + case pkcs1_hashalgo::SHA512_224 => + yield &sha512::sha512_224(); + case pkcs1_hashalgo::SHA512_256 => + yield &sha512::sha512_256(); + case => + abort("unreachable"); + }; + defer hash::close(h); + let out: []u8 = alloc([0...], hash::sz(h)); + + io::writeall(h, msg)!; + hash::sum(h, out); + return out; +}; diff --git a/crypto/rsa/pkcs1.ha b/crypto/rsa/pkcs1.ha @@ -0,0 +1,161 @@ +// License: MPL-2.0 +// (c) 2022 Armin Preiml <apreiml@strohwolke.at> +use bufio; +use crypto::sha1; +use crypto::sha256; +use crypto::sha512; +use crypto::math; +use errors; +use hash; +use io; + +// Supported hash algorithms for [[pkcs1_sign]] and [[pkcs1_verify]]. +export type pkcs1_hashalgo = enum { + SHA1, + // SHA224, We don't have this one yet + SHA256, + SHA384, + SHA512, + SHA512_224, + SHA512_256, +}; + +const OID_SHA1: [_]u8 = [ + 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, + 0x1a, 0x05, 0x00, 0x04, 0x14, +]; + +const OID_SHA224: [_]u8 = [ + 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, + 0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c, +]; + +const OID_SHA256: [_]u8 = [ + 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, + 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20, +]; + +const OID_SHA384: [_]u8 = [ + 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, + 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30, +]; + +const OID_SHA512: [_]u8 = [ + 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, + 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40, +]; + +const OID_SHA512_224: [_]u8 = [ + 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, + 0x65, 0x03, 0x04, 0x02, 0x05, 0x05, 0x00, 0x04, 0x1c, +]; + +const OID_SHA512_256: [_]u8 = [ + 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, + 0x65, 0x03, 0x04, 0x02, 0x06, 0x05, 0x00, 0x04, 0x20, +]; + +// Required buffer size for [[pkcs1_verify]]. +export def PKCS1_VERIFYBUFSIZE: size = PUBEXP_BUFSIZE + (BITSIZE / 8); + +// Verifies a PKCS#1 v1.5 signature given a public key 'pubkey', the message +// hash 'msghash', the signature 'sig' and the hash algorithm 'algo'. 'algo' +// must reflect the hash algorithm 'sig' was created with. +// +// A temporary buffer 'buf' of size [[PKCS1_VERIFYBUFSIZE]] must be provided. +export fn pkcs1_verify( + pubkey: []u8, + msghash: []u8, + sig: []u8, + algo: pkcs1_hashalgo, + buf: []u8 +) (void | error) = { + let pub = pubkey_params(pubkey); + + let actualsig = buf[..len(sig)]; + let pubbuf = buf[len(sig)..]; + + actualsig[..] = sig[..]; + match (pubexp(&pub, actualsig, pubbuf)) { + case let e: error => + return e; + case void => + yield; + }; + + let expectedsig = pubbuf[..len(sig)]; + pkcs1_sig_encode(expectedsig, msghash, algo)?; + + if (crypto::math::eqslice(expectedsig, actualsig) == 0) { + return badsig; + }; +}; + +// Required buffer size for [[pkcs1_sign]]. +export def PKCS1_SIGNBUFSIZE: size = PRIVEXP_BUFSIZE; + +// Signs a message hash 'msghash' using the PKCS#1 V1.5 signature scheme. The +// signature will be written to 'sig' which must be in the the size of the +// modulus n (see [[privkey_nsize]]). 'algo' defines the hash algorithm +// 'msghash' was created with. +// +// A temporary buffer 'buf' of size [[PKCS1_SIGNBUFSIZE]] must be provided. +export fn pkcs1_sign( + priv: []u8, + msghash: []u8, + sig: []u8, + algo: pkcs1_hashalgo, + buf: []u8 +) (void | error) = { + let priv = privkey_params(priv); + pkcs1_sig_encode(sig, msghash, algo)?; + privexp(&priv, sig, buf)?; +}; + +// Returns hash id and hash size for given 'algo'. +fn pkcs1_hashinfo(algo: pkcs1_hashalgo) (const []u8, size) = { + switch (algo) { + case pkcs1_hashalgo::SHA1 => + return (OID_SHA1, sha1::SIZE); + case pkcs1_hashalgo::SHA256 => + return (OID_SHA256, sha256::SIZE); + case pkcs1_hashalgo::SHA384 => + return (OID_SHA384, sha512::SIZE384); + case pkcs1_hashalgo::SHA512 => + return (OID_SHA512, sha512::SIZE); + case pkcs1_hashalgo::SHA512_224 => + return (OID_SHA512_224, sha512::SIZE224); + case pkcs1_hashalgo::SHA512_256 => + return (OID_SHA512_256, sha512::SIZE256); + case => + abort("unreachable"); + }; +}; + +// Creates an unauthenticated signature of 'msg' and writes it into 'sig' using +// given hash algorithm 'algo'. +fn pkcs1_sig_encode( + sig: []u8, + msghash: []u8, + algo: pkcs1_hashalgo +) (void | error) = { + let (hid, hsz) = pkcs1_hashinfo(algo); + if (len(msghash) != hsz) { + return errors::invalid; + }; + + let tlen = len(hid) + hsz; + if (len(sig) < tlen + 11) { + return badsig; + }; + + const hsep = len(sig) - tlen - 1; + + sig[..2] = [0x00, 0x01]; + for (let i = 2z; i < hsep; i += 1) { + sig[i] = 0xff; + }; + sig[hsep] = 0x00; + sig[hsep + 1..len(sig) - hsz] = hid[..]; + sig[len(sig) - hsz..] = msghash[..]; +};