1 // SPDX-License-Identifier: BSD-2-Clause 2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis 3 * 4 * LibTomCrypt is a library that provides various cryptographic 5 * algorithms in a highly modular and flexible manner. 6 * 7 * The library is free for all purposes without any express 8 * guarantee it works. 9 */ 10 #include "tomcrypt_private.h" 11 12 /** 13 @file der_decode_short_integer.c 14 ASN.1 DER, decode an integer, Tom St Denis 15 */ 16 17 18 #ifdef LTC_DER 19 20 /** 21 Read a short integer 22 @param in The DER encoded data 23 @param inlen Size of data 24 @param num [out] The integer to decode 25 @return CRYPT_OK if successful 26 */ der_decode_short_integer(const unsigned char * in,unsigned long inlen,unsigned long * num)27int der_decode_short_integer(const unsigned char *in, unsigned long inlen, unsigned long *num) 28 { 29 unsigned long len, x, y; 30 31 LTC_ARGCHK(num != NULL); 32 LTC_ARGCHK(in != NULL); 33 34 /* check length */ 35 if (inlen < 2) { 36 return CRYPT_INVALID_PACKET; 37 } 38 39 /* check header */ 40 x = 0; 41 if ((in[x++] & 0x1F) != 0x02) { 42 return CRYPT_INVALID_PACKET; 43 } 44 45 /* get the packet len */ 46 len = in[x++]; 47 48 if (x + len > inlen) { 49 return CRYPT_INVALID_PACKET; 50 } 51 52 /* read number */ 53 y = 0; 54 while (len--) { 55 y = (y<<8) | (unsigned long)in[x++]; 56 } 57 *num = y; 58 59 return CRYPT_OK; 60 61 } 62 63 #endif 64 65 /* ref: $Format:%D$ */ 66 /* git commit: $Format:%H$ */ 67 /* commit time: $Format:%ai$ */ 68