1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
3
* LibTomCrypt is a library that provides various cryptographic
4
* algorithms in a highly modular and flexible manner.
6
* The library is free for all purposes without any express
9
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
13
/* PKCS #1 v1.5 Signature Padding -- Tom St Denis */
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)
21
unsigned long derlen, modulus_bytelen, x, y;
24
_ARGCHK(msghash != NULL)
26
_ARGCHK(outlen != NULL);
28
if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
32
/* hack, to detect any hash without a DER OID */
33
if (hash_descriptor[hash_idx].DERlen == 0) {
34
return CRYPT_INVALID_ARG;
38
modulus_bytelen = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0);
40
/* get der len ok? Forgive my lame German accent.... */
41
derlen = hash_descriptor[hash_idx].DERlen;
44
if (msghashlen + 3 + derlen > modulus_bytelen) {
45
return CRYPT_PK_INVALID_SIZE;
48
if (*outlen < modulus_bytelen) {
49
return CRYPT_BUFFER_OVERFLOW;
52
/* packet is 0x00 0x01 PS 0x00 T, where PS == 0xFF repeated modulus_bytelen - 3 - derlen - msghashlen times, T == DER || hash */
56
for (y = 0; y < (modulus_bytelen - 3 - derlen - msghashlen); y++) {
60
for (y = 0; y < derlen; y++) {
61
out[x++] = hash_descriptor[hash_idx].DER[y];
63
for (y = 0; y < msghashlen; y++) {
64
out[x++] = msghash[y];
67
*outlen = modulus_bytelen;