X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilccrypt%2Fpkcs1.c;h=98963739d8565565e05cb5624b4f41107449e0c6;hb=413da0f8686910f5e627393157566ae729ca99c4;hp=8420b8b100416359a287d395e68fa0ca703574fc;hpb=2ff00506ddea4c01205f03733bef476a460b6a18;p=silc.git diff --git a/lib/silccrypt/pkcs1.c b/lib/silccrypt/pkcs1.c index 8420b8b1..98963739 100644 --- a/lib/silccrypt/pkcs1.c +++ b/lib/silccrypt/pkcs1.c @@ -27,15 +27,15 @@ Hence, the encoding is always in PKCS #1 version 1.5 format. Any questions and comments regarding this modified version should be - sent to priikone@poseidon.pspt.fi. + sent to priikone@silcnet.org. References: ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc, ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1.asc, and RFC 2437. - Copyright notice: All code, including the SILC PKCS API code that is - not part of the Mozilla code, falls under the same license (MPL or GPL) - found attached to this file, below. + Copyright notice: All code in this file, including the SILC PKCS API + code that is not part of the Mozilla code, falls under the same license + (MPL or GPL) found attached to this file, below. */ /* @@ -77,6 +77,7 @@ */ #include "silcincludes.h" +#include "rsa_internal.h" #include "rsa.h" #define RSA_BLOCK_MIN_PAD_LEN 8 @@ -104,12 +105,13 @@ typedef enum { * the rules defined in PKCS #1. */ static unsigned char * -RSA_FormatOneBlock(unsigned int modulusLen, RSA_BlockType blockType, - unsigned char *data, unsigned int data_len) +RSA_FormatOneBlock(SilcUInt32 modulusLen, RSA_BlockType blockType, + unsigned char *data, SilcUInt32 data_len) { unsigned char *block; unsigned char *bp; int padLen; + int i; block = (unsigned char *) silc_malloc(modulusLen); if (block == NULL) @@ -152,15 +154,6 @@ RSA_FormatOneBlock(unsigned int modulusLen, RSA_BlockType blockType, * Blocks intended for public-key operation. */ case RSA_BlockPublic: - - /* XXX For now we can't do this because we can't get the - SilcRNG object down to this level. */ - silc_free(block); - return NULL; - -#if 0 - int i; - /* * 0x00 || BT || Pad || 0x00 || ActualData * 1 1 padLen 1 data_len @@ -171,14 +164,12 @@ RSA_FormatOneBlock(unsigned int modulusLen, RSA_BlockType blockType, for (i = 0; i < padLen; i++) { /* Pad with non-zero random data. */ do { - RNG_GenerateGlobalRandomBytes(bp + i, 1); + bp[i] = silc_rng_global_get_byte(); } while (bp[i] == RSA_BLOCK_AFTER_PAD_OCTET); } bp += padLen; *bp++ = RSA_BLOCK_AFTER_PAD_OCTET; memcpy(bp, data, data_len); -#endif - break; default: @@ -190,10 +181,10 @@ RSA_FormatOneBlock(unsigned int modulusLen, RSA_BlockType blockType, } static int -RSA_FormatBlock(unsigned char **result, unsigned int *result_len, - unsigned int modulusLen, +RSA_FormatBlock(unsigned char **result, SilcUInt32 *result_len, + SilcUInt32 modulusLen, RSA_BlockType blockType, unsigned char *data, - unsigned int data_len) + SilcUInt32 data_len) { /* * XXX For now assume that the data length fits in a single @@ -247,14 +238,14 @@ RSA_FormatBlock(unsigned char **result, unsigned int *result_len, */ unsigned char * RSA_DecodeOneBlock(unsigned char *data, - unsigned int modulusLen, - unsigned int expectedLen, + SilcUInt32 modulusLen, + SilcUInt32 expectedLen, RSA_BlockType bt, - unsigned int *pResultLen) + SilcUInt32 *pResultLen) { RSA_BlockType blockType; unsigned char *dp, *res; - unsigned int i, len = 0; + SilcUInt32 i, len = 0; dp = data; if (dp[0] != RSA_BLOCK_FIRST_OCTET) { @@ -265,6 +256,9 @@ RSA_DecodeOneBlock(unsigned char *data, if (blockType != bt) return NULL; + if (modulusLen < 2 + 1) + return NULL; + dp += 2; switch (blockType) { @@ -323,18 +317,18 @@ RSA_DecodeOneBlock(unsigned char *data, SILC_PKCS_API_ENCRYPT(pkcs1) { RsaKey *key = (RsaKey *)context; - SilcInt mp_tmp; - SilcInt mp_dst; + SilcMPInt mp_tmp; + SilcMPInt mp_dst; unsigned char *padded; - unsigned int padded_len, len = key->bits / 8; + SilcUInt32 padded_len, len = (key->bits + 7) / 8; /* Pad data */ if (!RSA_FormatBlock(&padded, &padded_len, len, RSA_BlockPublic, src, src_len)) return FALSE; - silc_mp_init_set_ui(&mp_tmp, 0); - silc_mp_init_set_ui(&mp_dst, 0); + silc_mp_init(&mp_tmp); + silc_mp_init(&mp_dst); /* Data to MP */ silc_mp_bin2mp(padded, padded_len, &mp_tmp); @@ -348,8 +342,8 @@ SILC_PKCS_API_ENCRYPT(pkcs1) memset(padded, 0, padded_len); silc_free(padded); - silc_mp_clear(&mp_tmp); - silc_mp_clear(&mp_dst); + silc_mp_uninit(&mp_tmp); + silc_mp_uninit(&mp_dst); return TRUE; } @@ -357,13 +351,13 @@ SILC_PKCS_API_ENCRYPT(pkcs1) SILC_PKCS_API_DECRYPT(pkcs1) { RsaKey *key = (RsaKey *)context; - SilcInt mp_tmp; - SilcInt mp_dst; + SilcMPInt mp_tmp; + SilcMPInt mp_dst; unsigned char *padded, *unpadded; - unsigned int padded_len; + SilcUInt32 padded_len; - silc_mp_init_set_ui(&mp_tmp, 0); - silc_mp_init_set_ui(&mp_dst, 0); + silc_mp_init(&mp_tmp); + silc_mp_init(&mp_dst); /* Data to MP */ silc_mp_bin2mp(src, src_len, &mp_tmp); @@ -372,7 +366,7 @@ SILC_PKCS_API_DECRYPT(pkcs1) rsa_en_de_crypt(&mp_dst, &mp_tmp, &key->d, &key->n); /* MP to data */ - padded = silc_mp_mp2bin(&mp_dst, key->bits / 8, &padded_len); + padded = silc_mp_mp2bin(&mp_dst, (key->bits + 7) / 8, &padded_len); /* Unpad data */ unpadded = RSA_DecodeOneBlock(padded, padded_len, 0, @@ -380,8 +374,8 @@ SILC_PKCS_API_DECRYPT(pkcs1) if (!unpadded) { memset(padded, 0, padded_len); silc_free(padded); - silc_mp_clear(&mp_tmp); - silc_mp_clear(&mp_dst); + silc_mp_uninit(&mp_tmp); + silc_mp_uninit(&mp_dst); return FALSE; } @@ -393,8 +387,8 @@ SILC_PKCS_API_DECRYPT(pkcs1) memset(unpadded, 0, padded_len); silc_free(padded); silc_free(unpadded); - silc_mp_clear(&mp_tmp); - silc_mp_clear(&mp_dst); + silc_mp_uninit(&mp_tmp); + silc_mp_uninit(&mp_dst); return TRUE; } @@ -402,19 +396,19 @@ SILC_PKCS_API_DECRYPT(pkcs1) SILC_PKCS_API_SIGN(pkcs1) { RsaKey *key = (RsaKey *)context; - SilcInt mp_tmp; - SilcInt mp_dst; + SilcMPInt mp_tmp; + SilcMPInt mp_dst; unsigned char *padded; - unsigned int padded_len; - unsigned int len = key->bits / 8; + SilcUInt32 padded_len; + SilcUInt32 len = (key->bits + 7) / 8; /* Pad data */ if (!RSA_FormatBlock(&padded, &padded_len, len, RSA_BlockPrivate, src, src_len)) return FALSE; - silc_mp_init_set_ui(&mp_tmp, 0); - silc_mp_init_set_ui(&mp_dst, 0); + silc_mp_init(&mp_tmp); + silc_mp_init(&mp_dst); /* Data to MP */ silc_mp_bin2mp(padded, len, &mp_tmp); @@ -428,8 +422,8 @@ SILC_PKCS_API_SIGN(pkcs1) memset(padded, 0, padded_len); silc_free(padded); - silc_mp_clear(&mp_tmp); - silc_mp_clear(&mp_dst); + silc_mp_uninit(&mp_tmp); + silc_mp_uninit(&mp_dst); return TRUE; } @@ -438,13 +432,13 @@ SILC_PKCS_API_VERIFY(pkcs1) { RsaKey *key = (RsaKey *)context; int ret = TRUE; - SilcInt mp_tmp2; - SilcInt mp_dst; + SilcMPInt mp_tmp2; + SilcMPInt mp_dst; unsigned char *verify, *unpadded; - unsigned int verify_len, len = key->bits / 8; + SilcUInt32 verify_len, len = (key->bits + 7) / 8; - silc_mp_init_set_ui(&mp_tmp2, 0); - silc_mp_init_set_ui(&mp_dst, 0); + silc_mp_init(&mp_tmp2); + silc_mp_init(&mp_dst); /* Format the signature into MP int */ silc_mp_bin2mp(signature, signature_len, &mp_tmp2); @@ -461,8 +455,8 @@ SILC_PKCS_API_VERIFY(pkcs1) if (!unpadded) { memset(verify, 0, verify_len); silc_free(verify); - silc_mp_clear(&mp_tmp2); - silc_mp_clear(&mp_dst); + silc_mp_uninit(&mp_tmp2); + silc_mp_uninit(&mp_dst); return FALSE; } @@ -474,8 +468,8 @@ SILC_PKCS_API_VERIFY(pkcs1) memset(unpadded, 0, verify_len); silc_free(verify); silc_free(unpadded); - silc_mp_clear(&mp_tmp2); - silc_mp_clear(&mp_dst); + silc_mp_uninit(&mp_tmp2); + silc_mp_uninit(&mp_dst); return ret; }