~ubuntu-branches/ubuntu/hoary/nettle/hoary

1 by Marek Habersack
Import upstream version 1.10
1
/* pkcs1-rsa-md5.c
2
 *
3
 * PKCS stuff for rsa-md5.
4
 */
5
6
/* nettle, low-level cryptographics library
7
 *
8
 * Copyright (C) 2001, 2003 Niels Möller
9
 *  
10
 * The nettle library is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU Lesser General Public License as published by
12
 * the Free Software Foundation; either version 2.1 of the License, or (at your
13
 * option) any later version.
14
 * 
15
 * The nettle library is distributed in the hope that it will be useful, but
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
18
 * License for more details.
19
 * 
20
 * You should have received a copy of the GNU Lesser General Public License
21
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
22
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23
 * MA 02111-1307, USA.
24
 */
25
26
#if HAVE_CONFIG_H
27
# include "config.h"
28
#endif
29
30
#if WITH_PUBLIC_KEY
31
32
#include <assert.h>
33
#include <stdlib.h>
34
#include <string.h>
35
36
#include "rsa.h"
37
38
#include "bignum.h"
39
#include "pkcs1.h"
40
41
#include "nettle-internal.h"
42
43
/* From pkcs-1v2
44
 *
45
 *   id-sha1 OBJECT IDENTIFIER ::=
46
 *     {iso(1) identified-organization(3) oiw(14) secsig(3)
47
 *   	 algorithms(2) 26}
48
 *   
49
 *   The default hash function is SHA-1: 
50
 *   sha1Identifier ::= AlgorithmIdentifier {id-sha1, NULL}
51
 */
52
53
static const uint8_t
54
sha1_prefix[] =
55
{
56
  /* 15 octets prefix, 20 octets hash, total 35 */
57
  0x30,       33, /* SEQUENCE */
58
    0x30,      9, /* SEQUENCE */
59
      0x06,    5, /* OBJECT IDENTIFIER */
60
  	  0x2b, 0x0e, 0x03, 0x02, 0x1a,
61
      0x05,    0, /* NULL */
62
    0x04,     20  /* OCTET STRING */
63
      /* Here comes the raw hash value */
64
};
65
66
void
67
pkcs1_rsa_sha1_encode(mpz_t m, unsigned length, struct sha1_ctx *hash)
68
{
69
  TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_BITS / 8);
70
  TMP_ALLOC(em, length);
71
72
  assert(length >= SHA1_DIGEST_SIZE);
73
  pkcs1_signature_prefix(length - SHA1_DIGEST_SIZE, em,
74
			 sizeof(sha1_prefix),
75
			 sha1_prefix);
76
  
77
  sha1_digest(hash, SHA1_DIGEST_SIZE, em + length - SHA1_DIGEST_SIZE);
78
  nettle_mpz_set_str_256_u(m, length, em);
79
}
80
81
void
82
pkcs1_rsa_sha1_encode_digest(mpz_t m, unsigned length, const uint8_t *digest)
83
{
84
  TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_BITS / 8);
85
  TMP_ALLOC(em, length);
86
87
  assert(length >= SHA1_DIGEST_SIZE);
88
  pkcs1_signature_prefix(length - SHA1_DIGEST_SIZE, em,
89
			 sizeof(sha1_prefix),
90
			 sha1_prefix);
91
92
  memcpy(em + length - SHA1_DIGEST_SIZE, digest, SHA1_DIGEST_SIZE);
93
  nettle_mpz_set_str_256_u(m, length, em);
94
}
95
96
#endif /* WITH_PUBLIC_KEY */