Skip to content

Commit ae50082

Browse files
committed
Only compute length of OID data once.
Signed-off-by: Steffen Jaeckel <[email protected]>
1 parent ef1453d commit ae50082

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

src/headers/tomcrypt_private.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,8 @@ int der_length_asn1_length(unsigned long len, unsigned long *outlen);
588588
int der_length_sequence_ex(const ltc_asn1_list *list, unsigned long inlen,
589589
unsigned long *outlen, unsigned long *payloadlen);
590590

591-
unsigned long der_object_identifier_bits(unsigned long x);
591+
int der_length_object_identifier_full(const unsigned long *words, unsigned long nwords,
592+
unsigned long *outlen, unsigned long *datalen);
592593

593594
int der_ia5_char_encode(int c);
594595
int der_ia5_value_decode(int v);

src/pk/asn1/der/object_identifier/der_encode_object_identifier.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,14 @@ int der_encode_object_identifier(const unsigned long *words, unsigned long nwor
2727
LTC_ARGCHK(outlen != NULL);
2828

2929
/* check length */
30-
if ((err = der_length_object_identifier(words, nwords, &x)) != CRYPT_OK) {
30+
if ((err = der_length_object_identifier_full(words, nwords, &x, &z)) != CRYPT_OK) {
3131
return err;
3232
}
3333
if (x > *outlen) {
3434
*outlen = x;
3535
return CRYPT_BUFFER_OVERFLOW;
3636
}
3737

38-
/* compute length to store OID data */
39-
z = 0;
40-
wordbuf = words[0] * 40 + words[1];
41-
for (y = 1; y < nwords; y++) {
42-
t = der_object_identifier_bits(wordbuf);
43-
z += t/7 + ((t%7) ? 1 : 0) + (wordbuf == 0 ? 1 : 0);
44-
if (y < nwords - 1) {
45-
wordbuf = words[y + 1];
46-
}
47-
}
48-
4938
/* store header + length */
5039
x = 0;
5140
out[x++] = 0x06;

src/pk/asn1/der/object_identifier/der_length_object_identifier.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#ifdef LTC_DER
1111

12-
unsigned long der_object_identifier_bits(unsigned long x)
12+
static LTC_INLINE unsigned long s_der_object_identifier_bits(unsigned long x)
1313
{
1414
#if defined(LTC_HAVE_CLZL_BUILTIN)
1515
if (x == 0)
@@ -26,14 +26,7 @@ unsigned long der_object_identifier_bits(unsigned long x)
2626
#endif
2727
}
2828

29-
/**
30-
Gets length of DER encoding of Object Identifier
31-
@param nwords The number of OID words
32-
@param words The actual OID words to get the size of
33-
@param outlen [out] The length of the DER encoding for the given string
34-
@return CRYPT_OK if successful
35-
*/
36-
int der_length_object_identifier(const unsigned long *words, unsigned long nwords, unsigned long *outlen)
29+
int der_length_object_identifier_full(const unsigned long *words, unsigned long nwords, unsigned long *outlen, unsigned long *datalen)
3730
{
3831
unsigned long y, z, t, wordbuf;
3932

@@ -55,14 +48,17 @@ int der_length_object_identifier(const unsigned long *words, unsigned long nword
5548
z = 0;
5649
wordbuf = words[0] * 40 + words[1];
5750
for (y = 1; y < nwords; y++) {
58-
t = der_object_identifier_bits(wordbuf);
51+
t = s_der_object_identifier_bits(wordbuf);
5952
z += t/7 + ((t%7) ? 1 : 0) + (wordbuf == 0 ? 1 : 0);
6053
if (y < nwords - 1) {
6154
/* grab next word */
6255
wordbuf = words[y+1];
6356
}
6457
}
6558

59+
if (datalen) {
60+
*datalen = z;
61+
}
6662
/* now depending on the length our length encoding changes */
6763
if (z < 128) {
6864
z += 2;
@@ -78,4 +74,16 @@ int der_length_object_identifier(const unsigned long *words, unsigned long nword
7874
return CRYPT_OK;
7975
}
8076

77+
/**
78+
Gets length of DER encoding of Object Identifier
79+
@param nwords The number of OID words
80+
@param words The actual OID words to get the size of
81+
@param outlen [out] The length of the DER encoding for the given string
82+
@return CRYPT_OK if successful
83+
*/
84+
int der_length_object_identifier(const unsigned long *words, unsigned long nwords, unsigned long *outlen)
85+
{
86+
return der_length_object_identifier_full(words, nwords, outlen, NULL);
87+
}
88+
8189
#endif

0 commit comments

Comments
 (0)