From: Maytham Alsudany <maytha8thedev@gmail.com>
Description: use uint for 32-bit builds
 C.malloc accepts C.ulong in 64-bit builds, but C.uint in 32-bit builds.
 This patch ensures the correct type is passed to C.malloc.

--- a/pkcs11/pkcs11.go
+++ b/pkcs11/pkcs11.go
@@ -297,7 +297,7 @@
 //
 // This method also returns a function to free the allocated C memory.
 func ckCString(s string) (cstring *C.CK_UTF8CHAR, free func()) {
-	b := (*C.CK_UTF8CHAR)(C.malloc(C.sizeof_CK_UTF8CHAR * C.ulong(len(s))))
+	b := (*C.CK_UTF8CHAR)(C.malloc(C.sizeof_CK_UTF8CHAR * malloc_arg(len(s))))
 	bs := unsafe.Slice(b, len(s))
 	for i, c := range []byte(s) {
 		bs[i] = C.CK_UTF8CHAR(c)
@@ -1016,7 +1016,7 @@
 	}
 	n := attrs[0].ulValueLen
 
-	cLabel := (*C.CK_UTF8CHAR)(C.malloc(C.ulong(n)))
+	cLabel := (*C.CK_UTF8CHAR)(C.malloc(malloc_arg(n)))
 	defer C.free(unsafe.Pointer(cLabel))
 	attrs[0].pValue = C.CK_VOID_PTR(cLabel)
 
@@ -1097,11 +1097,11 @@
 		return nil, fmt.Errorf("no public exponent returned")
 	}
 
-	cN := (C.CK_VOID_PTR)(C.malloc(attrs[0].ulValueLen * C.sizeof_CK_BYTE))
+	cN := (C.CK_VOID_PTR)(C.malloc(malloc_arg(attrs[0].ulValueLen) * C.sizeof_CK_BYTE))
 	defer C.free(unsafe.Pointer(cN))
 	attrs[0].pValue = cN
 
-	cE := (C.CK_VOID_PTR)(C.malloc(attrs[1].ulValueLen))
+	cE := (C.CK_VOID_PTR)(C.malloc(malloc_arg(attrs[1].ulValueLen)))
 	defer C.free(unsafe.Pointer(cE))
 	attrs[1].pValue = cE
 
@@ -1134,11 +1134,11 @@
 		return nil, fmt.Errorf("no ec point available")
 	}
 
-	cParam := (C.CK_VOID_PTR)(C.malloc(attrs[0].ulValueLen))
+	cParam := (C.CK_VOID_PTR)(C.malloc(malloc_arg(attrs[0].ulValueLen)))
 	defer C.free(unsafe.Pointer(cParam))
 	attrs[0].pValue = cParam
 
-	cPoint := (C.CK_VOID_PTR)(C.malloc(attrs[1].ulValueLen))
+	cPoint := (C.CK_VOID_PTR)(C.malloc(malloc_arg(attrs[1].ulValueLen)))
 	defer C.free(unsafe.Pointer(cPoint))
 	attrs[1].pValue = cPoint
 
@@ -1437,7 +1437,7 @@
 	if n == 0 {
 		return nil, fmt.Errorf("certificate value not present")
 	}
-	cRaw := (C.CK_VOID_PTR)(C.malloc(C.ulong(n)))
+	cRaw := (C.CK_VOID_PTR)(C.malloc(malloc_arg(n)))
 	defer C.free(unsafe.Pointer(cRaw))
 
 	attrs[0].pValue = cRaw
--- /dev/null
+++ b/pkcs11/32bit.go
@@ -0,0 +1,9 @@
+//go:build 386 || amd64p32 || arm || armbe || mips || mipsle || mips64p32 || mips64p32le || ppc || riscv || s390 || sparc
+
+package pkcs11
+
+import "C"
+
+func malloc_arg[T int | C.uint | C.ulong](arg T) C.uint {
+	return C.uint(arg)
+}
--- /dev/null
+++ b/pkcs11/64bit.go
@@ -0,0 +1,9 @@
+//go:build !(386 || amd64p32 || arm || armbe || mips || mipsle || mips64p32 || mips64p32le || ppc || riscv || s390 || sparc)
+
+package pkcs11
+
+import "C"
+
+func malloc_arg[T int | C.uint | C.ulong](arg T) C.ulong {
+	return C.ulong(arg)
+}
