Created
February 25, 2017 00:42
-
-
Save btoews/2e4f82785687d86e2fc017b60e03e1ed to your computer and use it in GitHub Desktop.
Golang CK_GCM_PARAMS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
#include <stdio.h> | |
#include <stdlib.h> | |
#define CK_BYTE_PTR unsigned char * | |
#define CK_ULONG unsigned long long | |
typedef struct CK_GCM_PARAMS { | |
CK_BYTE_PTR pIv; | |
CK_ULONG ulIvLen; | |
CK_ULONG ulIvBits; | |
CK_BYTE_PTR pAAD; | |
CK_ULONG ulAADLen; | |
CK_ULONG ulTagBits; | |
} CK_GCM_PARAMS; | |
#define CK_GCM_PARAMS_SIZE sizeof(CK_GCM_PARAMS) | |
*/ | |
import "C" | |
import "unsafe" | |
func gcmParamBytes(iv []byte) (paramBytes []byte, free func()) { | |
ivBytes := C.CBytes(iv) | |
free = func() { | |
C.free(ivBytes) | |
} | |
params := C.CK_GCM_PARAMS{ | |
pIv: C.CK_BYTE_PTR(ivBytes), | |
ulIvLen: C.CK_ULONG(len(iv)), | |
ulIvBits: C.CK_ULONG(len(iv) * 8), | |
ulTagBits: C.CK_ULONG(128), | |
} | |
paramBytes = C.GoBytes(unsafe.Pointer(¶ms), C.CK_GCM_PARAMS_SIZE) | |
return | |
} | |
func gcmParamsFromBytes(paramBytes []byte) (iv []byte, ivLen int, ivBits int, tagBits int) { | |
paramBytesPtr := C.CBytes(paramBytes) | |
defer C.free(paramBytesPtr) | |
params := (*C.CK_GCM_PARAMS)(paramBytesPtr) | |
ivLen = int(params.ulIvLen) | |
ivBits = int(params.ulIvBits) | |
tagBits = int(params.ulTagBits) | |
iv = C.GoBytes(unsafe.Pointer(params.pIv), C.int(ivLen)) | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment