testutils.c: Fix high bits of the mpz_urandomb used with mini-gmp.
[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
47 #include "gmp-glue.h"
48
49 /* From RFC 3447, Public-Key Cryptography Standards (PKCS) #1: RSA
50  * Cryptography Specifications Version 2.1.
51  *
52  *     id-sha512    OBJECT IDENTIFIER ::=
53  *       {joint-iso-itu-t(2) country(16) us(840) organization(1)
54  *         gov(101) csor(3) nistalgorithm(4) hashalgs(2) 3}
55  */
56
57 static const uint8_t
58 sha512_prefix[] =
59 {
60   /* 19 octets prefix, 64 octets hash, total 83 */
61   0x30,      81, /* SEQUENCE */
62     0x30,    13, /* SEQUENCE */
63       0x06,   9, /* OBJECT IDENTIFIER */
64         0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
65       0x05,   0, /* NULL */
66     0x04,    64  /* OCTET STRING */
67       /* Here comes the raw hash value, 64 octets */
68 };
69
70 int
71 pkcs1_rsa_sha512_encode(mpz_t m, size_t key_size, struct sha512_ctx *hash)
72 {
73   uint8_t *p;
74   TMP_GMP_DECL(em, uint8_t);
75
76   TMP_GMP_ALLOC(em, key_size);
77
78   p = _pkcs1_signature_prefix(key_size, em,
79                               sizeof(sha512_prefix),
80                               sha512_prefix,
81                               SHA512_DIGEST_SIZE);
82   if (p)
83     {
84       sha512_digest(hash, SHA512_DIGEST_SIZE, p);
85       nettle_mpz_set_str_256_u(m, key_size, em);
86       TMP_GMP_FREE(em);
87       return 1;
88     }
89   else
90     {
91       TMP_GMP_FREE(em);
92       return 0;
93     }
94 }
95
96 int
97 pkcs1_rsa_sha512_encode_digest(mpz_t m, size_t key_size, const uint8_t *digest)
98 {
99   uint8_t *p;
100   TMP_GMP_DECL(em, uint8_t);
101
102   TMP_GMP_ALLOC(em, key_size);
103
104   p = _pkcs1_signature_prefix(key_size, em,
105                               sizeof(sha512_prefix),
106                               sha512_prefix,
107                               SHA512_DIGEST_SIZE);
108   if (p)
109     {
110       memcpy(p, digest, SHA512_DIGEST_SIZE);
111       nettle_mpz_set_str_256_u(m, key_size, em);
112       TMP_GMP_FREE(em);
113       return 1;
114     }
115   else
116     {
117       TMP_GMP_FREE(em);
118       return 0;
119     }
120 }