]> the.earth.li Git - onak.git/blob - rsa.c
ceb81e1eada3ecaf24d7669a848a0f183659929a
[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 <string.h>
20
21 #include <nettle/ripemd160.h>
22 #include <nettle/rsa.h>
23 #include <nettle/sha.h>
24
25 #include "rsa.h"
26
27 const uint8_t ripemd160_prefix[] = { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
28         0x2B, 0x24, 0x03, 0x02, 0x01,
29         0x05, 0x00,
30         0x04, 0x14
31 };
32
33 const uint8_t sha224_prefix[] = { 0x30, 0x2D, 0x30, 0x0d, 0x06, 0x09,
34         0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04,
35         0x05, 0x00,
36         0x04, 0x1c
37 };
38
39 const uint8_t sha384_prefix[] = { 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09,
40         0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
41         0x05, 0x00,
42         0x04, 0x3
43 };
44
45 int rsa_ripemd160_verify_digest(const struct rsa_public_key *key,
46                          const uint8_t *digest,
47                          const mpz_t s)
48 {
49         uint8_t buf[sizeof(ripemd160_prefix) + RIPEMD160_DIGEST_SIZE];
50
51         memcpy(buf, ripemd160_prefix, sizeof(ripemd160_prefix));
52         memcpy(buf + sizeof(ripemd160_prefix), digest, RIPEMD160_DIGEST_SIZE);
53
54         return rsa_pkcs1_verify(key, sizeof(buf), buf, s);
55 }
56
57 int rsa_sha224_verify_digest(const struct rsa_public_key *key,
58                          const uint8_t *digest,
59                          const mpz_t s)
60 {
61         uint8_t buf[sizeof(sha224_prefix) + SHA224_DIGEST_SIZE];
62
63         memcpy(buf, sha224_prefix, sizeof(sha224_prefix));
64         memcpy(buf + sizeof(sha224_prefix), digest, SHA224_DIGEST_SIZE);
65
66         return rsa_pkcs1_verify(key, sizeof(buf), buf, s);
67 }
68
69 int rsa_sha384_verify_digest(const struct rsa_public_key *key,
70                          const uint8_t *digest,
71                          const mpz_t s)
72 {
73         uint8_t buf[sizeof(sha384_prefix) + SHA384_DIGEST_SIZE];
74
75         memcpy(buf, sha384_prefix, sizeof(sha384_prefix));
76         memcpy(buf + sizeof(sha384_prefix), digest, SHA384_DIGEST_SIZE);
77
78         return rsa_pkcs1_verify(key, sizeof(buf), buf, s);
79 }