comparison libtomcrypt/src/pk/rsa/rsa_sign_hash.c @ 382:0cbe8f6dbf9e

propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f) to branch 'au.asn.ucc.matt.dropbear' (head 02c413252c90e9de8e03d91e9939dde3029f5c0a)
author Matt Johnston <matt@ucc.asn.au>
date Thu, 11 Jan 2007 02:41:05 +0000
parents 1b9e69c058d2
children f849a5ca2efc
comparison
equal deleted inserted replaced
379:b66a00272a90 382:0cbe8f6dbf9e
4 * algorithms in a highly modular and flexible manner. 4 * algorithms in a highly modular and flexible manner.
5 * 5 *
6 * The library is free for all purposes without any express 6 * The library is free for all purposes without any express
7 * guarantee it works. 7 * guarantee it works.
8 * 8 *
9 * Tom St Denis, [email protected], http://libtomcrypt.org 9 * Tom St Denis, [email protected], http://libtomcrypt.com
10 */ 10 */
11 #include "tomcrypt.h" 11 #include "tomcrypt.h"
12 12
13 /** 13 /**
14 @file rsa_sign_hash.c 14 @file rsa_sign_hash.c
15 RSA PKCS v2 PSS sign hash, Tom St Denis 15 RSA PKCS #1 v1.5 and v2 PSS sign hash, Tom St Denis and Andreas Lange
16 */ 16 */
17 17
18 #ifdef MRSA 18 #ifdef MRSA
19 19
20 /** 20 /**
21 (PKCS #1, v2.0) PSS pad then sign 21 PKCS #1 pad then sign
22 @param in The hash to sign 22 @param in The hash to sign
23 @param inlen The length of the hash to sign (octets) 23 @param inlen The length of the hash to sign (octets)
24 @param out [out] The signature 24 @param out [out] The signature
25 @param outlen [in/out] The max size and resulting size of the signature 25 @param outlen [in/out] The max size and resulting size of the signature
26 @param padding Type of padding (LTC_PKCS_1_PSS or LTC_PKCS_1_V1_5)
26 @param prng An active PRNG state 27 @param prng An active PRNG state
27 @param prng_idx The index of the PRNG desired 28 @param prng_idx The index of the PRNG desired
28 @param hash_idx The index of the hash desired 29 @param hash_idx The index of the hash desired
29 @param saltlen The length of the salt desired (octets) 30 @param saltlen The length of the salt desired (octets)
30 @param key The private RSA key to use 31 @param key The private RSA key to use
31 @return CRYPT_OK if successful 32 @return CRYPT_OK if successful
32 */ 33 */
33 int rsa_sign_hash(const unsigned char *in, unsigned long inlen, 34 int rsa_sign_hash_ex(const unsigned char *in, unsigned long inlen,
34 unsigned char *out, unsigned long *outlen, 35 unsigned char *out, unsigned long *outlen,
35 prng_state *prng, int prng_idx, 36 int padding,
36 int hash_idx, unsigned long saltlen, 37 prng_state *prng, int prng_idx,
37 rsa_key *key) 38 int hash_idx, unsigned long saltlen,
39 rsa_key *key)
38 { 40 {
39 unsigned long modulus_bitlen, modulus_bytelen, x; 41 unsigned long modulus_bitlen, modulus_bytelen, x, y;
40 int err; 42 int err;
41 43
42 LTC_ARGCHK(in != NULL); 44 LTC_ARGCHK(in != NULL);
43 LTC_ARGCHK(out != NULL); 45 LTC_ARGCHK(out != NULL);
44 LTC_ARGCHK(outlen != NULL); 46 LTC_ARGCHK(outlen != NULL);
45 LTC_ARGCHK(key != NULL); 47 LTC_ARGCHK(key != NULL);
46 48
47 /* valid prng and hash ? */ 49 /* valid padding? */
48 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) { 50 if ((padding != LTC_PKCS_1_V1_5) && (padding != LTC_PKCS_1_PSS)) {
49 return err; 51 return CRYPT_PK_INVALID_PADDING;
50 } 52 }
51 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { 53
52 return err; 54 if (padding == LTC_PKCS_1_PSS) {
53 } 55 /* valid prng and hash ? */
54 56 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) {
55 /* get modulus len in bits */ 57 return err;
56 modulus_bitlen = mp_count_bits(&(key->N)); 58 }
59 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
60 return err;
61 }
62 }
63
64 /* get modulus len in bits */
65 modulus_bitlen = mp_count_bits((key->N));
57 66
58 /* outlen must be at least the size of the modulus */ 67 /* outlen must be at least the size of the modulus */
59 modulus_bytelen = mp_unsigned_bin_size(&(key->N)); 68 modulus_bytelen = mp_unsigned_bin_size((key->N));
60 if (modulus_bytelen > *outlen) { 69 if (modulus_bytelen > *outlen) {
70 *outlen = modulus_bytelen;
61 return CRYPT_BUFFER_OVERFLOW; 71 return CRYPT_BUFFER_OVERFLOW;
62 } 72 }
63 73
64 /* PSS pad the key */ 74 if (padding == LTC_PKCS_1_PSS) {
65 x = *outlen; 75 /* PSS pad the key */
66 if ((err = pkcs_1_pss_encode(in, inlen, saltlen, prng, prng_idx, 76 x = *outlen;
67 hash_idx, modulus_bitlen, out, &x)) != CRYPT_OK) { 77 if ((err = pkcs_1_pss_encode(in, inlen, saltlen, prng, prng_idx,
68 return err; 78 hash_idx, modulus_bitlen, out, &x)) != CRYPT_OK) {
79 return err;
80 }
81 } else {
82 /* PKCS #1 v1.5 pad the hash */
83 unsigned char *tmpin;
84 ltc_asn1_list digestinfo[2], siginfo[2];
85
86 /* not all hashes have OIDs... so sad */
87 if (hash_descriptor[hash_idx].OIDlen == 0) {
88 return CRYPT_INVALID_ARG;
89 }
90
91 /* construct the SEQUENCE
92 SEQUENCE {
93 SEQUENCE {hashoid OID
94 blah NULL
95 }
96 hash OCTET STRING
97 }
98 */
99 LTC_SET_ASN1(digestinfo, 0, LTC_ASN1_OBJECT_IDENTIFIER, hash_descriptor[hash_idx].OID, hash_descriptor[hash_idx].OIDlen);
100 LTC_SET_ASN1(digestinfo, 1, LTC_ASN1_NULL, NULL, 0);
101 LTC_SET_ASN1(siginfo, 0, LTC_ASN1_SEQUENCE, digestinfo, 2);
102 LTC_SET_ASN1(siginfo, 1, LTC_ASN1_OCTET_STRING, in, inlen);
103
104 /* allocate memory for the encoding */
105 y = mp_unsigned_bin_size(key->N);
106 tmpin = XMALLOC(y);
107 if (tmpin == NULL) {
108 return CRYPT_MEM;
109 }
110
111 if ((err = der_encode_sequence(siginfo, 2, tmpin, &y)) != CRYPT_OK) {
112 XFREE(tmpin);
113 return err;
114 }
115
116 x = *outlen;
117 if ((err = pkcs_1_v1_5_encode(tmpin, y, LTC_PKCS_1_EMSA,
118 modulus_bitlen, NULL, 0,
119 out, &x)) != CRYPT_OK) {
120 XFREE(tmpin);
121 return err;
122 }
123 XFREE(tmpin);
69 } 124 }
70 125
71 /* RSA encode it */ 126 /* RSA encode it */
72 return rsa_exptmod(out, x, out, outlen, PK_PRIVATE, key); 127 return ltc_mp.rsa_me(out, x, out, outlen, PK_PRIVATE, key);
73 } 128 }
74 129
75 #endif /* MRSA */ 130 #endif /* MRSA */
76 131
77 /* $Source: /cvs/libtom/libtomcrypt/src/pk/rsa/rsa_sign_hash.c,v $ */ 132 /* $Source: /cvs/libtom/libtomcrypt/src/pk/rsa/rsa_sign_hash.c,v $ */
78 /* $Revision: 1.3 $ */ 133 /* $Revision: 1.9 $ */
79 /* $Date: 2005/05/05 14:35:59 $ */ 134 /* $Date: 2006/11/09 23:15:39 $ */