X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilccrypt%2Fpkcs1.c;h=98963739d8565565e05cb5624b4f41107449e0c6;hb=413da0f8686910f5e627393157566ae729ca99c4;hp=2ba6f3efbaf5a62ca753f28a4dc7a661a45d213b;hpb=04f41c4481381e8e7c1e685a4edb6be6ec5d2c66;p=silc.git diff --git a/lib/silccrypt/pkcs1.c b/lib/silccrypt/pkcs1.c index 2ba6f3ef..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,8 +105,8 @@ 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; @@ -163,7 +164,7 @@ RSA_FormatOneBlock(unsigned int modulusLen, RSA_BlockType blockType, for (i = 0; i < padLen; i++) { /* Pad with non-zero random data. */ do { - silc_rng_global_get_byte(bp + i); + bp[i] = silc_rng_global_get_byte(); } while (bp[i] == RSA_BLOCK_AFTER_PAD_OCTET); } bp += padLen; @@ -180,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 @@ -237,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) { @@ -255,6 +256,9 @@ RSA_DecodeOneBlock(unsigned char *data, if (blockType != bt) return NULL; + if (modulusLen < 2 + 1) + return NULL; + dp += 2; switch (blockType) { @@ -313,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); @@ -338,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; } @@ -347,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); @@ -362,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, @@ -370,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; } @@ -383,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; } @@ -392,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); @@ -418,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; } @@ -428,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); @@ -451,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; } @@ -464,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; }