]> the.earth.li Git - onak.git/blob - rsa.c
Fix compilation without libnettle
[onak.git] / rsa.c
1 /*
2  * rsa.c - routines to check RSA hash signature combos not present in libnettle
3  *
4  * Copyright 2019 Jonathan McDowell <noodles@earth.li>
5  *
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.
9  *
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
13  * more details.
14  *
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/>.
17  */
18
19 #include "build-config.h"
20
21 #include <string.h>
22
23 #ifdef HAVE_NETTLE
24 #include <nettle/ripemd160.h>
25 #include <nettle/rsa.h>
26 #include <nettle/sha.h>
27
28 #include "rsa.h"
29
30 const uint8_t ripemd160_prefix[] = { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
31         0x2B, 0x24, 0x03, 0x02, 0x01,
32         0x05, 0x00,
33         0x04, 0x14
34 };
35
36 const uint8_t sha224_prefix[] = { 0x30, 0x2D, 0x30, 0x0d, 0x06, 0x09,
37         0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04,
38         0x05, 0x00,
39         0x04, 0x1c
40 };
41
42 const uint8_t sha384_prefix[] = { 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09,
43         0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
44         0x05, 0x00,
45         0x04, 0x3
46 };
47
48 int rsa_ripemd160_verify_digest(const struct rsa_public_key *key,
49                          const uint8_t *digest,
50                          const mpz_t s)
51 {
52         uint8_t buf[sizeof(ripemd160_prefix) + RIPEMD160_DIGEST_SIZE];
53
54         memcpy(buf, ripemd160_prefix, sizeof(ripemd160_prefix));
55         memcpy(buf + sizeof(ripemd160_prefix), digest, RIPEMD160_DIGEST_SIZE);
56
57         return rsa_pkcs1_verify(key, sizeof(buf), buf, s);
58 }
59
60 int rsa_sha224_verify_digest(const struct rsa_public_key *key,
61                          const uint8_t *digest,
62                          const mpz_t s)
63 {
64         uint8_t buf[sizeof(sha224_prefix) + SHA224_DIGEST_SIZE];
65
66         memcpy(buf, sha224_prefix, sizeof(sha224_prefix));
67         memcpy(buf + sizeof(sha224_prefix), digest, SHA224_DIGEST_SIZE);
68
69         return rsa_pkcs1_verify(key, sizeof(buf), buf, s);
70 }
71
72 int rsa_sha384_verify_digest(const struct rsa_public_key *key,
73                          const uint8_t *digest,
74                          const mpz_t s)
75 {
76         uint8_t buf[sizeof(sha384_prefix) + SHA384_DIGEST_SIZE];
77
78         memcpy(buf, sha384_prefix, sizeof(sha384_prefix));
79         memcpy(buf + sizeof(sha384_prefix), digest, SHA384_DIGEST_SIZE);
80
81         return rsa_pkcs1_verify(key, sizeof(buf), buf, s);
82 }
83 #endif /* HAVE_NETTLE */