aaf3b4bdc903f1b7a78251f3eb80a7b329ac5041
[platform/upstream/nettle.git] / pkcs1-rsa-md5.c
1 /* pkcs1-rsa-md5.c
2
3    PKCS stuff for rsa-md5.
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  *   md5 OBJECT IDENTIFIER ::=
52  *     {iso(1) member-body(2) US(840) rsadsi(113549) digestAlgorithm(2) 5}
53  *
54  * The parameters part of the algorithm identifier is NULL:
55  *
56  *   md5Identifier ::= AlgorithmIdentifier {md5, NULL}
57  */
58
59 static const uint8_t
60 md5_prefix[] =
61 {
62   /* 18 octets prefix, 16 octets hash, 34 total. */
63   0x30,       32, /* SEQUENCE */
64     0x30,     12, /* SEQUENCE */
65       0x06,    8, /* OBJECT IDENTIFIER */
66         0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05,
67       0x05,    0, /* NULL */
68     0x04,     16  /* OCTET STRING */
69       /* Here comes the raw hash value */
70 };
71
72 int
73 pkcs1_rsa_md5_encode(mpz_t m, size_t key_size, struct md5_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(md5_prefix),
82                               md5_prefix,
83                               MD5_DIGEST_SIZE);
84   if (p)
85     {
86       md5_digest(hash, MD5_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_md5_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(md5_prefix),
108                               md5_prefix,
109                               MD5_DIGEST_SIZE);
110   if (p)
111     {
112       memcpy(p, digest, MD5_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 }