cfb8: Fix decrypt path
[gd/nettle] / pkcs1-rsa-sha512.c
1 /* pkcs1-rsa-sha512.c
2
3    PKCS stuff for rsa-sha512.
4
5    Copyright (C) 2001, 2003, 2006, 2010 Niels Möller
6
7    This file is part of GNU Nettle.
8
9    GNU Nettle is free software: you can redistribute it and/or
10    modify it under the terms of either:
11
12      * the GNU Lesser General Public License as published by the Free
13        Software Foundation; either version 3 of the License, or (at your
14        option) any later version.
15
16    or
17
18      * the GNU General Public License as published by the Free
19        Software Foundation; either version 2 of the License, or (at your
20        option) any later version.
21
22    or both in parallel, as here.
23
24    GNU Nettle is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27    General Public License for more details.
28
29    You should have received copies of the GNU General Public License and
30    the GNU Lesser General Public License along with this program.  If
31    not, see http://www.gnu.org/licenses/.
32 */
33
34 #if HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #include <assert.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include "rsa.h"
43
44 #include "bignum.h"
45 #include "pkcs1.h"
46 #include "hogweed-internal.h"
47
48 #include "gmp-glue.h"
49
50 /* From RFC 3447, Public-Key Cryptography Standards (PKCS) #1: RSA
51  * Cryptography Specifications Version 2.1.
52  *
53  *     id-sha512    OBJECT IDENTIFIER ::=
54  *       {joint-iso-itu-t(2) country(16) us(840) organization(1)
55  *         gov(101) csor(3) nistalgorithm(4) hashalgs(2) 3}
56  */
57
58 static const uint8_t
59 sha512_prefix[] =
60 {
61   /* 19 octets prefix, 64 octets hash, total 83 */
62   0x30,      81, /* SEQUENCE */
63     0x30,    13, /* SEQUENCE */
64       0x06,   9, /* OBJECT IDENTIFIER */
65         0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
66       0x05,   0, /* NULL */
67     0x04,    64  /* OCTET STRING */
68       /* Here comes the raw hash value, 64 octets */
69 };
70
71 int
72 pkcs1_rsa_sha512_encode(mpz_t m, size_t key_size, struct sha512_ctx *hash)
73 {
74   uint8_t *p;
75   TMP_GMP_DECL(em, uint8_t);
76
77   TMP_GMP_ALLOC(em, key_size);
78
79   p = _pkcs1_signature_prefix(key_size, em,
80                               sizeof(sha512_prefix),
81                               sha512_prefix,
82                               SHA512_DIGEST_SIZE);
83   if (p)
84     {
85       sha512_digest(hash, SHA512_DIGEST_SIZE, p);
86       nettle_mpz_set_str_256_u(m, key_size, em);
87       TMP_GMP_FREE(em);
88       return 1;
89     }
90   else
91     {
92       TMP_GMP_FREE(em);
93       return 0;
94     }
95 }
96
97 int
98 pkcs1_rsa_sha512_encode_digest(mpz_t m, size_t key_size, const uint8_t *digest)
99 {
100   uint8_t *p;
101   TMP_GMP_DECL(em, uint8_t);
102
103   TMP_GMP_ALLOC(em, key_size);
104
105   p = _pkcs1_signature_prefix(key_size, em,
106                               sizeof(sha512_prefix),
107                               sha512_prefix,
108                               SHA512_DIGEST_SIZE);
109   if (p)
110     {
111       memcpy(p, digest, SHA512_DIGEST_SIZE);
112       nettle_mpz_set_str_256_u(m, key_size, em);
113       TMP_GMP_FREE(em);
114       return 1;
115     }
116   else
117     {
118       TMP_GMP_FREE(em);
119       return 0;
120     }
121 }