2 * rsa.c - routines to check RSA hash signature combos not present in libnettle
4 * Copyright 2019 Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <https://www.gnu.org/licenses/>.
19 #include "build-config.h"
24 #include <nettle/ripemd160.h>
25 #include <nettle/rsa.h>
26 #include <nettle/sha.h>
30 const uint8_t ripemd160_prefix[] = { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
31 0x2B, 0x24, 0x03, 0x02, 0x01,
36 const uint8_t sha224_prefix[] = { 0x30, 0x2D, 0x30, 0x0d, 0x06, 0x09,
37 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04,
42 const uint8_t sha384_prefix[] = { 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09,
43 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
48 int rsa_ripemd160_verify_digest(const struct rsa_public_key *key,
49 const uint8_t *digest,
52 uint8_t buf[sizeof(ripemd160_prefix) + RIPEMD160_DIGEST_SIZE];
54 memcpy(buf, ripemd160_prefix, sizeof(ripemd160_prefix));
55 memcpy(buf + sizeof(ripemd160_prefix), digest, RIPEMD160_DIGEST_SIZE);
57 return rsa_pkcs1_verify(key, sizeof(buf), buf, s);
60 int rsa_sha224_verify_digest(const struct rsa_public_key *key,
61 const uint8_t *digest,
64 uint8_t buf[sizeof(sha224_prefix) + SHA224_DIGEST_SIZE];
66 memcpy(buf, sha224_prefix, sizeof(sha224_prefix));
67 memcpy(buf + sizeof(sha224_prefix), digest, SHA224_DIGEST_SIZE);
69 return rsa_pkcs1_verify(key, sizeof(buf), buf, s);
72 int rsa_sha384_verify_digest(const struct rsa_public_key *key,
73 const uint8_t *digest,
76 uint8_t buf[sizeof(sha384_prefix) + SHA384_DIGEST_SIZE];
78 memcpy(buf, sha384_prefix, sizeof(sha384_prefix));
79 memcpy(buf + sizeof(sha384_prefix), digest, SHA384_DIGEST_SIZE);
81 return rsa_pkcs1_verify(key, sizeof(buf), buf, s);
83 #endif /* HAVE_NETTLE */