comparison src/pk/pkcs1/pkcs_1_v15_sa_encode.c @ 191:1c15b283127b libtomcrypt-orig

Import of libtomcrypt 1.02 with manual path rename rearrangement etc
author Matt Johnston <matt@ucc.asn.au>
date Fri, 06 May 2005 13:23:02 +0000
parents
children
comparison
equal deleted inserted replaced
143:5d99163f7e32 191:1c15b283127b
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 *
9 * Tom St Denis, [email protected], http://libtomcrypt.org
10 */
11 #include "tomcrypt.h"
12
13 /**
14 @file pkcs_1_v15_sa_encode.c
15 PKCS #1 v1.5 Signature Padding, Tom St Denis
16 */
17
18 #ifdef PKCS_1
19
20 /**
21 Perform PKCS #1 v1.5 Signature Padding
22 @param msghash The hash you wish to incorporate in the padding
23 @param msghashlen The length of the hash
24 @param hash_idx The index of the hash used
25 @param modulus_bitlen The length of the RSA modulus that will sign this (bits)
26 @param out [out] Where to store the padded data
27 @param outlen [in/out] Max size and resulting size of the padded data
28 @return CRYPT_OK if successful
29 */
30 int pkcs_1_v15_sa_encode(const unsigned char *msghash, unsigned long msghashlen,
31 int hash_idx, unsigned long modulus_bitlen,
32 unsigned char *out, unsigned long *outlen)
33 {
34 unsigned long derlen, modulus_bytelen, x, y;
35 int err;
36
37 LTC_ARGCHK(msghash != NULL)
38 LTC_ARGCHK(out != NULL);
39 LTC_ARGCHK(outlen != NULL);
40
41 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
42 return err;
43 }
44
45 /* hack, to detect any hash without a DER OID */
46 if (hash_descriptor[hash_idx].DERlen == 0) {
47 return CRYPT_INVALID_ARG;
48 }
49
50 /* get modulus len */
51 modulus_bytelen = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0);
52
53 /* get der len ok? Forgive my lame German accent.... */
54 derlen = hash_descriptor[hash_idx].DERlen;
55
56 /* valid sizes? */
57 if (msghashlen + 3 + derlen > modulus_bytelen) {
58 return CRYPT_PK_INVALID_SIZE;
59 }
60
61 if (*outlen < modulus_bytelen) {
62 return CRYPT_BUFFER_OVERFLOW;
63 }
64
65 /* packet is 0x00 0x01 PS 0x00 T, where PS == 0xFF repeated modulus_bytelen - 3 - derlen - msghashlen times, T == DER || hash */
66 x = 0;
67 out[x++] = 0x00;
68 out[x++] = 0x01;
69 for (y = 0; y < (modulus_bytelen - 3 - derlen - msghashlen); y++) {
70 out[x++] = 0xFF;
71 }
72 out[x++] = 0x00;
73 for (y = 0; y < derlen; y++) {
74 out[x++] = hash_descriptor[hash_idx].DER[y];
75 }
76 for (y = 0; y < msghashlen; y++) {
77 out[x++] = msghash[y];
78 }
79
80 *outlen = modulus_bytelen;
81 return CRYPT_OK;
82 }
83
84 #endif /* PKCS_1 */