rsa-compute-root-test: Fix qsize. Try more keys.
[gd/nettle] / pkcs1-rsa-sha1.c
1 /* pkcs1-rsa-sha1.c
2
3    PKCS stuff for rsa-sha1.
4
5    Copyright (C) 2001, 2003 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 pkcs-1v2
50  *
51  *   id-sha1 OBJECT IDENTIFIER ::=
52  *     {iso(1) identified-organization(3) oiw(14) secsig(3)
53  *       algorithms(2) 26}
54  *   
55  *   The default hash function is SHA-1: 
56  *   sha1Identifier ::= AlgorithmIdentifier {id-sha1, NULL}
57  */
58
59 static const uint8_t
60 sha1_prefix[] =
61 {
62   /* 15 octets prefix, 20 octets hash, total 35 */
63   0x30,       33, /* SEQUENCE */
64     0x30,      9, /* SEQUENCE */
65       0x06,    5, /* OBJECT IDENTIFIER */
66           0x2b, 0x0e, 0x03, 0x02, 0x1a,
67       0x05,    0, /* NULL */
68     0x04,     20  /* OCTET STRING */
69       /* Here comes the raw hash value */
70 };
71
72 int
73 pkcs1_rsa_sha1_encode(mpz_t m, size_t key_size, struct sha1_ctx *hash)
74 {
75   uint8_t *p;
76   TMP_GMP_DECL(em, uint8_t);
77
78   TMP_GMP_ALLOC(em, key_size);
79
80   p = _pkcs1_signature_prefix(key_size, em,
81                               sizeof(sha1_prefix),
82                               sha1_prefix,
83                               SHA1_DIGEST_SIZE);
84   if (p)
85     {
86       sha1_digest(hash, SHA1_DIGEST_SIZE, p);
87       nettle_mpz_set_str_256_u(m, key_size, em);
88       TMP_GMP_FREE(em);
89       return 1;
90     }
91   else
92     {
93       TMP_GMP_FREE(em);
94       return 0;
95     }
96 }
97
98 int
99 pkcs1_rsa_sha1_encode_digest(mpz_t m, size_t key_size, const uint8_t *digest)
100 {
101   uint8_t *p;
102   TMP_GMP_DECL(em, uint8_t);
103
104   TMP_GMP_ALLOC(em, key_size);
105
106   p = _pkcs1_signature_prefix(key_size, em,
107                               sizeof(sha1_prefix),
108                               sha1_prefix,
109                               SHA1_DIGEST_SIZE);
110   if (p)
111     {
112       memcpy(p, digest, SHA1_DIGEST_SIZE);
113       nettle_mpz_set_str_256_u(m, key_size, em);
114       TMP_GMP_FREE(em);
115       return 1;
116     }
117   else
118     {
119       TMP_GMP_FREE(em);
120       return 0;
121     }
122 }