comparison pkcs_1_v15_sa_encode.c @ 15:6362d3854bb4 libtomcrypt-orig

0.96 release of LibTomCrypt
author Matt Johnston <matt@ucc.asn.au>
date Tue, 15 Jun 2004 14:07:21 +0000
parents
children
comparison
equal deleted inserted replaced
3:7faae8f46238 15:6362d3854bb4
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 "mycrypt.h"
12
13 /* PKCS #1 v1.5 Signature Padding -- Tom St Denis */
14
15 #ifdef PKCS_1
16
17 int pkcs_1_v15_sa_encode(const unsigned char *msghash, unsigned long msghashlen,
18 int hash_idx, unsigned long modulus_bitlen,
19 unsigned char *out, unsigned long *outlen)
20 {
21 unsigned long derlen, modulus_bytelen, x, y;
22 int err;
23
24 _ARGCHK(msghash != NULL)
25 _ARGCHK(out != NULL);
26 _ARGCHK(outlen != NULL);
27
28 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
29 return err;
30 }
31
32 /* hack, to detect any hash without a DER OID */
33 if (hash_descriptor[hash_idx].DERlen == 0) {
34 return CRYPT_INVALID_ARG;
35 }
36
37 /* get modulus len */
38 modulus_bytelen = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0);
39
40 /* get der len ok? Forgive my lame German accent.... */
41 derlen = hash_descriptor[hash_idx].DERlen;
42
43 /* valid sizes? */
44 if (msghashlen + 3 + derlen > modulus_bytelen) {
45 return CRYPT_PK_INVALID_SIZE;
46 }
47
48 if (*outlen < modulus_bytelen) {
49 return CRYPT_BUFFER_OVERFLOW;
50 }
51
52 /* packet is 0x00 0x01 PS 0x00 T, where PS == 0xFF repeated modulus_bytelen - 3 - derlen - msghashlen times, T == DER || hash */
53 x = 0;
54 out[x++] = 0x00;
55 out[x++] = 0x01;
56 for (y = 0; y < (modulus_bytelen - 3 - derlen - msghashlen); y++) {
57 out[x++] = 0xFF;
58 }
59 out[x++] = 0x00;
60 for (y = 0; y < derlen; y++) {
61 out[x++] = hash_descriptor[hash_idx].DER[y];
62 }
63 for (y = 0; y < msghashlen; y++) {
64 out[x++] = msghash[y];
65 }
66
67 *outlen = modulus_bytelen;
68 return CRYPT_OK;
69 }
70
71 #endif /* PKCS_1 */