view pkcs_1_v15_es_encode.c @ 147:c2b93763dac9 libtomcrypt

Fixes for it to compile and work nicely with Dropbear. In particular, OS X's 'ar' doesn't seem to like arrays which don't have initialising values.
author Matt Johnston <matt@ucc.asn.au>
date Sun, 19 Dec 2004 16:23:32 +0000
parents 5d99163f7e32
children
line wrap: on
line source

/* LibTomCrypt, modular cryptographic library -- Tom St Denis
 *
 * LibTomCrypt is a library that provides various cryptographic
 * algorithms in a highly modular and flexible manner.
 *
 * The library is free for all purposes without any express
 * guarantee it works.
 *
 * Tom St Denis, [email protected], http://libtomcrypt.org
 */
#include "mycrypt.h"

/* v1.5 Encryption Padding for PKCS #1 -- Tom St Denis */

#ifdef PKCS_1

int pkcs_1_v15_es_encode(const unsigned char *msg,    unsigned long msglen,
                               unsigned long  modulus_bitlen, 
                               prng_state    *prng,   int           prng_idx,
                               unsigned char *out,    unsigned long *outlen)
{ 
   unsigned long modulus_bytelen, x, y;

   _ARGCHK(msg    != NULL);
   _ARGCHK(out    != NULL);
   _ARGCHK(outlen != NULL);

   /* get modulus len */
   modulus_bytelen = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0);
   if (modulus_bytelen < 12) {
      return CRYPT_INVALID_ARG;
   }

   /* verify length */
   if (msglen > (modulus_bytelen - 11) || *outlen < modulus_bytelen) {
      return CRYPT_PK_INVALID_SIZE;
   }

   /* 0x00 0x02 PS 0x00 M */
   x = 0;
   out[x++] = 0x00;
   out[x++] = 0x02;
   y = modulus_bytelen - msglen - 3;
   if (prng_descriptor[prng_idx].read(out+x, y, prng) != y) {
      return CRYPT_ERROR_READPRNG;
   }
   x += y;
   out[x++] = 0x00;
   XMEMCPY(out+x, msg, msglen);
   *outlen = modulus_bytelen;

   return CRYPT_OK;
}

#endif /* PKCS_1 */