p11_bigint_trim() will trim a Big Integer (using the BIG ENDIAN PKCS#11 representation), taking unnecessary leading zeros.
This function will be useful in properly converting PKCS#11 numbers to native formats so we can check their contents. Signed-off-by: Klaus Heinrich Kiwi <[email protected]> --- usr/lib/pkcs11/common/p11util.c | 50 ++++++++++++++++++++++++++++++++++++++- usr/lib/pkcs11/common/p11util.h | 15 +++++++++++ 2 files changed, 64 insertions(+), 1 deletions(-) diff --git a/usr/lib/pkcs11/common/p11util.c b/usr/lib/pkcs11/common/p11util.c index 6f0921b..af2f84d 100755 --- a/usr/lib/pkcs11/common/p11util.c +++ b/usr/lib/pkcs11/common/p11util.c @@ -289,7 +289,7 @@ /* (C) COPYRIGHT International Business Machines Corp. 2001,2002 */ - +#include <stdlib.h> #include "pkcs11types.h" #define _sym2str(X) case X: return #X @@ -656,3 +656,51 @@ p11_get_ckm(CK_ULONG mechanism) default: return "UNKNOWN"; } } + +// Allocates memory on *dst and puts hex dump from ptr +// with len bytes. +// *dst must be freed by the caller +char * +p11_ahex_dump(char **dst, CK_BYTE_PTR ptr, CK_ULONG len) +{ + CK_ULONG i; + + if (dst == NULL) { + return NULL; + } + + *dst = (char *) calloc(2*len + 1 , sizeof(char)); + if (*dst == NULL) { + return NULL; + } + + for (i = 0; i < len; i++) { + sprintf(*dst + 2*i, "%02hhX", ptr[i]); + } + *(*dst + 2*len) = '\0'; // null-terminate + + return *dst; +} + + +/* p11_bigint_trim() - trim a big integer. Returns pointer that is + * contained within 'in' + '*size' that represents + * the same number, but without leading zeros. + * @in points to a sequence of bytes forming a big integer, + * unsigned, right-aligned and big-endian + * @size points to the size of @in on input, and the minimum + * size that can represent it on output + */ +CK_BYTE_PTR +p11_bigint_trim(CK_BYTE_PTR in, CK_ULONG_PTR size) { + CK_ULONG i; + + for (i = 0; + (i < *size) && in[i] == 0x00; + i++); + *size -= i; + return in + i; +} + + + diff --git a/usr/lib/pkcs11/common/p11util.h b/usr/lib/pkcs11/common/p11util.h index 54f0d1d..2e31bff 100755 --- a/usr/lib/pkcs11/common/p11util.h +++ b/usr/lib/pkcs11/common/p11util.h @@ -311,5 +311,20 @@ char *p11_get_ckm(CK_ULONG); // CK_BBOOL is_attribute_defined( CK_ATTRIBUTE_TYPE type ); +// Allocates memory on *dst and puts hex dump from ptr +// with len bytes. +// *dst must be freed by the caller +char * p11_ahex_dump(char **dst, CK_BYTE_PTR ptr, CK_ULONG len); + +/* p11_bigint_trim() - trim a big integer. Returns pointer that is + * contained within 'in' + '*size' that represents + * the same number, but without leading zeros. + * @in points to a sequence of bytes forming a big integer, + * unsigned, right-aligned and big-endian + * @size points to the size of @in on input, and the minimum + * size that can represent it on output + */ +CK_BYTE_PTR p11_bigint_trim(CK_BYTE_PTR in, CK_ULONG_PTR size); + #endif // #ifndef _P11UTIL_H_ -- 1.7.2.3 ------------------------------------------------------------------------------ Increase Visibility of Your 3D Game App & Earn a Chance To Win $500! Tap into the largest installed PC base & get more eyes on your game by optimizing for Intel(R) Graphics Technology. Get started today with the Intel(R) Software Partner Program. Five $500 cash prizes are up for grabs. http://p.sf.net/sfu/intelisp-dev2dev _______________________________________________ Opencryptoki-tech mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/opencryptoki-tech
