Skip to content

Commit 01a61e6

Browse files
authored
Merge pull request #302 from libtom/fix/pr/301
re-work PK crypto im- & export
2 parents c2f0675 + 7f302da commit 01a61e6

16 files changed

+86
-58
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ The following list is a small part of the available, but the most often required
4848
| ---- | -------- |
4949
| `LTC_NO_TEST` | Remove all algorithm self-tests from the library |
5050
| `LTC_NO_FILE` | Remove all API functions requiring a pre-defined `FILE` data-type (mostly useful for embedded targets) |
51-
| `MAX_RSA_SIZE` | Per default set to `4096`, if you need support for importing or generating bigger RSA keys, change this at compile-time. |
5251
| `GMP_DESC` | enable [gmp](https://gmplib.org/) as MPI provider *\*1* |
5352
| `LTM_DESC` | enable [libtommath](http://www.libtom.net/) as MPI provider *\*1* |
5453
| `TFM_DESC` | enable [tomsfastmath](http://www.libtom.net/) as MPI provider *\*1* *\*2* |

demos/demo_dynamic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def inprint(s, indent=0):
150150
b'ENDIAN_LITTLE',
151151
b'ENDIAN_64BITWORD',
152152
b'PK_PUBLIC',
153-
b'MAX_RSA_SIZE',
153+
b'LTC_MILLER_RABIN_REPS',
154154
b'CTR_COUNTER_BIG_ENDIAN',
155155
]
156156
for name in names:

makefile_include.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ endif
7676
LTC_CFLAGS += -Wno-type-limits
7777

7878
ifdef LTC_DEBUG
79+
$(info Debug build)
7980
# compile for DEBUGGING (required for ccmalloc checking!!!)
8081
LTC_CFLAGS += -g3 -DLTC_NO_ASM
8182
ifneq (,$(strip $(LTC_DEBUG)))

src/headers/tomcrypt_custom.h

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -425,30 +425,6 @@
425425
#define LTC_ECC_TIMING_RESISTANT
426426
#endif
427427

428-
/* define these PK sizes out of LTC_NO_PK
429-
* to have them always defined
430-
*/
431-
#if defined(LTC_MRSA)
432-
/* Min and Max RSA key sizes (in bits) */
433-
#ifndef MIN_RSA_SIZE
434-
#define MIN_RSA_SIZE 1024
435-
#endif
436-
#ifndef MAX_RSA_SIZE
437-
#define MAX_RSA_SIZE 4096
438-
#endif
439-
#endif
440-
441-
/* in cases where you want ASN.1/DER functionality, but no
442-
* RSA, you can define this externally if 1024 is not enough
443-
*/
444-
#if defined(LTC_MRSA)
445-
#define LTC_DER_MAX_PUBKEY_SIZE MAX_RSA_SIZE
446-
#elif !defined(LTC_DER_MAX_PUBKEY_SIZE)
447-
/* this includes DSA */
448-
#define LTC_DER_MAX_PUBKEY_SIZE 1024
449-
#endif
450-
451-
452428
/* PKCS #1 (RSA) and #5 (Password Handling) stuff */
453429
#ifndef LTC_NO_PKCS
454430

src/misc/crypt/crypt.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,6 @@ const char *crypt_build_settings =
399399
#if defined(LTC_DER)
400400
" DER "
401401
#endif
402-
#if defined(LTC_DER_MAX_PUBKEY_SIZE)
403-
" " NAME_VALUE(LTC_DER_MAX_PUBKEY_SIZE) " "
404-
#endif
405402
#if defined(LTC_PKCS_1)
406403
" PKCS#1 "
407404
#endif

src/misc/crypt/crypt_constants.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ static const crypt_constant _crypt_constants[] = {
7777

7878
#ifdef LTC_MRSA
7979
{"LTC_MRSA", 1},
80-
_C_STRINGIFY(MIN_RSA_SIZE),
81-
_C_STRINGIFY(MAX_RSA_SIZE),
8280
#else
8381
{"LTC_MRSA", 0},
8482
#endif
@@ -107,9 +105,6 @@ static const crypt_constant _crypt_constants[] = {
107105
{"LTC_MDSA", 0},
108106
#endif
109107

110-
#ifdef LTC_DER_MAX_PUBKEY_SIZE
111-
_C_STRINGIFY(LTC_DER_MAX_PUBKEY_SIZE),
112-
#endif
113108
#ifdef LTC_MILLER_RABIN_REPS
114109
_C_STRINGIFY(LTC_MILLER_RABIN_REPS),
115110
#endif

src/pk/asn1/der/bit/der_decode_raw_bit_string.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifdef LTC_DER
1818

1919
#define SETBIT(v, n) (v=((unsigned char)(v) | (1U << (unsigned char)(n))))
20+
#define CLRBIT(v, n) (v=((unsigned char)(v) & ~(1U << (unsigned char)(n))))
2021

2122
/**
2223
Store a BIT STRING
@@ -84,12 +85,14 @@ int der_decode_raw_bit_string(const unsigned char *in, unsigned long inlen,
8485

8586
/* decode/store the bits */
8687
for (y = 0; y < blen; y++) {
87-
if (in[x] & (1 << (7 - (y & 7)))) {
88-
SETBIT(out[y/8], 7-(y%8));
89-
}
90-
if ((y & 7) == 7) {
91-
++x;
92-
}
88+
if (in[x] & (1 << (7 - (y & 7)))) {
89+
SETBIT(out[y/8], 7-(y%8));
90+
} else {
91+
CLRBIT(out[y/8], 7-(y%8));
92+
}
93+
if ((y & 7) == 7) {
94+
++x;
95+
}
9396
}
9497

9598
/* we done */

src/pk/asn1/der/bit/der_encode_raw_bit_string.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/**
2222
Store a BIT STRING
2323
@param in The array of bits to store (8 per char)
24-
@param inlen The number of bits tostore
24+
@param inlen The number of bits to store
2525
@param out [out] The destination for the DER encoded BIT STRING
2626
@param outlen [in/out] The max size and resulting size of the DER BIT STRING
2727
@return CRYPT_OK if successful
@@ -68,11 +68,11 @@ int der_encode_raw_bit_string(const unsigned char *in, unsigned long inlen,
6868

6969
/* store the bits in big endian format */
7070
for (y = buf = 0; y < inlen; y++) {
71-
buf |= (getbit(in[y/8],7-y%8)?1:0) << (7 - (y & 7));
72-
if ((y & 7) == 7) {
73-
out[x++] = buf;
74-
buf = 0;
75-
}
71+
buf |= (getbit(in[y/8],7-y%8)?1:0) << (7 - (y & 7));
72+
if ((y & 7) == 7) {
73+
out[x++] = buf;
74+
buf = 0;
75+
}
7676
}
7777
/* store last byte */
7878
if (inlen & 7) {

src/pk/asn1/der/sequence/der_decode_subject_public_key_info.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ int der_decode_subject_public_key_info(const unsigned char *in, unsigned long in
5858
}
5959

6060
/* see if the OpenSSL DER format RSA public key will work */
61-
tmpbuf = XCALLOC(1, LTC_DER_MAX_PUBKEY_SIZE*8);
61+
tmpbuf = XCALLOC(1, inlen);
6262
if (tmpbuf == NULL) {
6363
err = CRYPT_MEM;
6464
goto LBL_ERR;
@@ -72,7 +72,7 @@ int der_decode_subject_public_key_info(const unsigned char *in, unsigned long in
7272
* in a **BIT** string ... so we have to extract it then proceed to convert bit to octet
7373
*/
7474
LTC_SET_ASN1(subject_pubkey, 0, LTC_ASN1_SEQUENCE, alg_id, 2);
75-
LTC_SET_ASN1(subject_pubkey, 1, LTC_ASN1_RAW_BIT_STRING, tmpbuf, LTC_DER_MAX_PUBKEY_SIZE*8);
75+
LTC_SET_ASN1(subject_pubkey, 1, LTC_ASN1_RAW_BIT_STRING, tmpbuf, inlen*8U);
7676

7777
err=der_decode_sequence(in, inlen, subject_pubkey, 2UL);
7878
if (err != CRYPT_OK) {

src/pk/asn1/der/sequence/der_encode_subject_public_key_info.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ int der_encode_subject_public_key_info(unsigned char *out, unsigned long *outlen
5858

5959
return der_encode_sequence_multi(out, outlen,
6060
LTC_ASN1_SEQUENCE, (unsigned long)sizeof(alg_id)/sizeof(alg_id[0]), alg_id,
61-
LTC_ASN1_RAW_BIT_STRING, (unsigned long)(public_key_len*8), public_key,
61+
LTC_ASN1_RAW_BIT_STRING, public_key_len*8U, public_key,
6262
LTC_ASN1_EOL, 0UL, NULL);
6363

6464
}

0 commit comments

Comments
 (0)