How convert int8_t array into integer value in objective c? - objective-c

I have the following array :
static const int8_t ARRAY[8] = {0x18, 0xB8, 0xCE, 0x0, 0x0, 0x0, 0x0, 0x0};
and I need to get an integer value. For this ARRAY final int value is 13547544, because the bytes in the array, followed by in the opposite order little-endian.
Example 0xCEB818 = 13547544
How can I do this?
Can have ready standard solutions?
Thanks in advance!

Since the host platform is little-endian, you can just point a 64-bit integer pointer at the array and the machine will read the array correctly.
static const int8_t ARRAY[8] = {0x18, 0xB8, 0xCE, 0x0, 0x0, 0x0, 0x0, 0x0};
uint64_t *value = (uint64_t *)&ARRAY;
NSLog(#"%llu", *value); // outputs 13547544

Related

Unwrapping a private key wrapped with CKM_AES_KEY_WRAP mechanism

I am currently working on what we call “centralized enrolment” that is:
Issue a keypair either RSA or EC on a HSM;
Issue a symmetric session key on the same HSM;
Wrap the private key with the session key using various mechanisms such as CKM_AES_CBC_PAD, CKM_AES_KEY_WRAP_PAD and CKM_AES_KEY_WRAP;
Wrap the session key with an external RSA master key which is provided to the HSM;
Return both protected keys.
When verifying the results, I succeed in unwrapping the session key (with the private key I own) but I am facing some difficulties in unwrapping the private key, when the mechanism is CKM_AES_KEY_WRAP. Everything works well with he other two.
As the session key is used only once, we let the HSM decide which IV to use (in the case of CKM_AES_CBC_PAD, it will be a 16 byte array of zeros).
What works well is:
case CKM_AES_KEY_WRAP_PAD -> {
Cipher wrapper = Cipher.getInstance("AESWrapPad", "BC");
wrapper.init(Cipher.UNWRAP_MODE, this.clearSecretKey);
clearPrivateKey = (PrivateKey) wrapper.unwrap(privateKeyToRecover, algorithm, Cipher.PRIVATE_KEY);
}
and
case CKM_AES_CBC_PAD -> {
byte[] ivb = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Cipher wrapper = Cipher.getInstance("AES/CBC/Pkcs7Padding", "BC");
wrapper.init(Cipher.UNWRAP_MODE, this.clearSecretKey, new IvParameterSpec(ivb));
clearPrivateKey = (PrivateKey) wrapper.unwrap(privateKeyToRecover, algorithm, Cipher.PRIVATE_KEY);
}
What fails is:
case CKM_AES_KEY_WRAP -> {
Cipher wrapper = Cipher.getInstance("AESWrap", "BC");
wrapper.init(Cipher.UNWRAP_MODE, this.clearSecretKey);
clearPrivateKey = (PrivateKey) wrapper.unwrap(privateKeyToRecover, algorithm, Cipher.PRIVATE_KEY);
}
with the error: Unknown key type encoded key spec not recognized: failed to construct sequence from byte[]: Extra data detected in stream.
Using the default IV specified in RFC 3394 (which is A[0] = IV = A6A6A6A6A6A6A6A6) does not seem to solve the problem.
Could someone explain me how to fix this?
Thanks a lot for reading and taking time to answer
Éric

array<BYTE>^ to BYTE [] exact convert

How to convert exactly:
array<BYTE>^ mntest = gcnew array<BYTE>{0x1A, 0x1B, 0x1C};
to
BYTE unmtest [] = { 0x1A, 0x1B, 0x1C };
Resp. how to initialize unmanaged BYTE array to correct size of managed array.
Dynamic initialization of unmanaged array throws an error.
I found because dynamic initialization of static array is not allowed is conversion above possible only with using array pointer.

Golang LD_PRELOAD to hook SSL_read and SSL_write

Discalaimer, I'm very new to Golang as I used the following article as the basis for this https://blog.gopheracademy.com/advent-2015/libc-hooking-go-shared-libraries/
I'm trying to write an LD_PRELOAD library that would intercept calls to SSL_read and SSL_write of the OpenSSL library.
This is the code I've come up with so far:
package main
import (
"C"
"log"
"fmt"
"github.com/rainycape/dl"
)
// main is required to build a shared library, but does nothing
func main() {}
//export SSL_read
func SSL_read(s C.int, b []byte, i C.int) C.int{
lib, err := dl.Open("libssl", 0)
if err != nil {
log.Fatalln(err)
}
defer lib.Close()
var oldSSL_read func(s C.int, b []byte, i C.int) C.int
lib.Sym("SSL_read", &oldSSL_read)
res := oldSSL_read(s, b, i)
//buf := *(*[]byte)(b)
//fmt.Println(i)
return res
}
/*
//export SSL_write
func SSL_write(){
}
*/
I'm compiling the code this way:
>$ go build -buildmode=c-shared -o preload.so golang_preload.g
And testing it with openssl s_client:
LD_PRELOAD=./preload.so openssl s_client -host www.google.com -port 443
This is however the errors it is causing:
fatal error: unexpected signal during runtime execution
[signal 0xb code=0x1 addr=0x4 pc=0x7f1d91bdeee9]
runtime stack:
runtime.throw(0x7f1d92964580, 0x2a)
/usr/lib/go/src/runtime/panic.go:530 +0x92
runtime.sigpanic()
/usr/lib/go/src/runtime/sigpanic_unix.go:12 +0x5e
goroutine 17 [syscall, locked to thread]:
runtime.cgocall(0x7f1d926d4f30, 0xc82003e930, 0xc800000000)
/usr/lib/go/src/runtime/cgocall.go:123 +0x121 fp=0xc82003e8e0 sp=0xc82003e8b0
github.com/rainycape/dl._Cfunc_call(0x7f1d9231e970, 0xc82008c160, 0xc8200762a0, 0x3, 0xc82008a038, 0xc800000000)
github.com/rainycape/dl/_obj/_cgo_gotypes.go:83 +0x43 fp=0xc82003e930 sp=0xc82003e8e0
github.com/rainycape/dl.makeTrampoline.func1(0xc82008e280, 0x3, 0x3, 0x0, 0x0, 0x0)
/home/asm/gocode/src/github.com/rainycape/dl/trampoline.go:124 +0x8b6 fp=0xc82003ebf8 sp=0xc82003e930
reflect.callReflect(0xc82008c120, 0xc82003edf0)
/usr/lib/go/src/reflect/value.go:508 +0x2cd fp=0xc82003edd8 sp=0xc82003ebf8
reflect.makeFuncStub(0xc8025cec30, 0x4, 0x25c5b70, 0x25c3b60, 0xc8025c3b60, 0x0, 0x0, 0xc820076260, 0xc82008a030, 0x0, ...)
/usr/lib/go/src/reflect/asm_amd64.s:17 +0x38 fp=0xc82003edf0 sp=0xc82003edd8
main.SSL_read(0x25cec30, 0x4, 0x25c5b70, 0x25c3b60, 0x7f1d025c3b60, 0xc800000000)
/tmp/golang_preload.go:44 +0x1e5 fp=0xc82003ee88 sp=0xc82003edf0
main._cgoexpwrap_24df11e45e4b_SSL_read(0x25cec30, 0x4, 0x25c5b70, 0x25c3b60, 0x25c3b60, 0x25c3b60)
command-line-arguments/_obj/_cgo_gotypes.go:68 +0x47 fp=0xc82003eec0 sp=0xc82003ee88
runtime.call64(0x0, 0x7fffb5954988, 0x7fffb5954a10, 0x30)
/usr/lib/go/src/runtime/asm_amd64.s:473 +0x40 fp=0xc82003ef08 sp=0xc82003eec0
runtime.cgocallbackg1()
/usr/lib/go/src/runtime/cgocall.go:267 +0x110 fp=0xc82003ef40 sp=0xc82003ef08
runtime.cgocallbackg()
/usr/lib/go/src/runtime/cgocall.go:180 +0xd9 fp=0xc82003efa0 sp=0xc82003ef40
runtime.cgocallback_gofunc(0x0, 0x0, 0x0)
/usr/lib/go/src/runtime/asm_amd64.s:716 +0x5d fp=0xc82003efb0 sp=0xc82003efa0
runtime.goexit()
/usr/lib/go/src/runtime/asm_amd64.s:1998 +0x1 fp=0xc82003efb8 sp=0xc82003efb0
goroutine 34 [syscall, locked to thread]:
runtime.goexit()
/usr/lib/go/src/runtime/asm_amd64.s:1998 +0x1
What is the proper way to access buffer in the SSL_read function, I've attempted unsafe.Pointer but I have can't bind type to value error.
update:
The SSL struct is defined in openssl.h. Adding that import results in conflict with SSL_read function.
In pure C code, using simple void* pointer would be enough, replacing it with unsafe.Pointer for the SSL and buffer results in the following error:
panic: can't bind value of type unsafe.Pointer
goroutine 17 [running, locked to thread]:
panic(0x7f0a8e933400, 0xc820096060)
/usr/lib/go/src/runtime/panic.go:464 +0x3ea
github.com/rainycape/dl.makeTrampoline.func1(0xc8200980f0, 0x3, 0x3, 0x0, 0x0, 0x0)
/home/asm/gocode/src/github.com/rainycape/dl/trampoline.go:116 +0x186e
reflect.callReflect(0xc82009c040, 0xc82004be10)
/usr/lib/go/src/reflect/value.go:508 +0x2cd
reflect.makeFuncStub(0xa12bd0, 0xa07b00, 0x400, 0x7f0a8e907800, 0xc82009a000, 0x0, 0x0, 0xc820096000, 0xc82009a000, 0x0, ...)
/usr/lib/go/src/reflect/asm_amd64.s:17 +0x38
main.SSL_read(0xa12bd0, 0xa07b00, 0xc800000400, 0x7f0a00000000)
/tmp/preload.go:26 +0x1cd
main._cgoexpwrap_613180a44973_SSL_read(0xa12bd0, 0xa07b00, 0x400, 0xa07b00)
command-line-arguments/_obj/_cgo_gotypes.go:50 +0x35
[]byte is a Go slice, which you can't use in C. The signature for SSL_read is:
SSL_read(SSL *ssl, void *buf, int num)
I order for the call to work, you need to match the signature and use equivalent types in your function definition.
func SSL_read(ssl *C.SSL, buf unsafe.Pointer, num C.int)

iOS: send customized uint8 array

I'm trying to make an app that communicate iPhone with another hardware using dock to RS232 wire (I bought from RedPark). I'm also using the library provided by redpark. I made a simple code at beginning, it worked fine.
UInt8 infoCmd[5] = {0x3E,0x3E,0x05,0x80,0xff};
[rscMgr write:infoCmd Length:5];
Then I want to add more command to it, so I create a method that returns different combinations of command I need.
- (UInt8 *)requestCommand:(int)commandName{
UInt8 * command;
if (commandName == DATADUMP) {
command=[Communication buildDataDump];
}
if (commandName == GETSERIALINFO) {
command=[Communication buildGetSerailInfo];
}
return command;
}
+ (UInt8 *)buildGetSerailInfo{
UInt8 *command = malloc(sizeof(UInt8)*5);
command[0]=SYN;
command[1]=SYN;
command[2]=ENQ;
command[3]=GETSERIALINFO;
//command[4] = {SYN, SYN, ENQ, GETSERIALINFO};
return command;
}
The thing is, some of my commands includes data that can be 200 bytes long. How can I create an UInt8 array that is easier for me to add bytes?
I'm new to programming, please explain to me in detail. Thank you a lot in advance.
Actually you will just send data, row byte over the wire. I do something similar in one project (not wire, but RS232 commands over TCP/IP), and it becomes quite simple, if you use an NSMutableData instance.
A snippet from my code:
static u_int8_t codeTable[] = { 0x1b, 0x74, 0x10 };
static u_int8_t charSet[] = { 0x1b, 0x52, 0x10 };
static u_int8_t formatOff[] = { 0x1b, 0x21, 0x00 };
static u_int8_t reverseOn[] = { 0x1d, 0x42, 0x01 };
static u_int8_t reverseOff[]= { 0x1d, 0x42, 0x00 };
static u_int8_t paperCut[] = { 0x1d, 0x56, 0x0 };
NSMutableData *mdata = [NSMutableData dataWithBytes:&formatOff length:sizeof(formatOff)];
[mdata appendBytes:&formatOff length:sizeof(formatOff)];
[mdata appendBytes:&reverseOff length:sizeof(reverseOff)];
[mdata appendData: [NSData dataWithBytes: &codeTable length:sizeof(codeTable)]];
[mdata appendData: [NSData dataWithBytes: &charSet length:sizeof(charSet)]];
As you see, I am just appending the data byte by byte.

Objective-C: How to correctly initialize char[]?

I need to take a char [] array and copy it's value to another, but I fail every time.
It works using this format:
char array[] = { 0x00, 0x00, 0x00 }
However, when I try to do this:
char array[] = char new_array[];
it fails, even though the new_array is just like the original.
Any help would be kindly appreciated.
Thanks
To copy at runtime, the usual C method is to use the strncpy or memcpy functions.
If you want two char arrays initialized to the same constant initializer at compile time, you're probably stuck with using #define:
#define ARRAY_INIT { 0x00, 0x00, 0x00 }
char array[] = ARRAY_INIT;
char new_array[] = ARRAY_INIT;
Thing is, this is rarely done because there's usually a better implementation.
EDIT: Okay, so you want to copy arrays at runtime. This is done with memcpy, part of <string.h> (of all places).
If I'm reading you right, you have initial conditions like so:
char array[] = { 0x00, 0x00, 0x00 };
char new_array[] = { 0x01, 0x00, 0xFF };
Then you do something, changing the arrays' contents, and after it's done, you want to set array to match new_array. That's just this:
memcpy(new_array, array, sizeof(array));
/* ^ ^ ^
| | +--- size in bytes
| +-------------- source array
+-------------------------destination array
*/
The library writers chose to order the arguments with the destination first because that's the same order as in assignment: destination = source.
There is no language-level built-in means to copy arrays in C, Objective-C, or C++ with primitive arrays like this. C++ encourages people to use std::vector, and Objective-C encourages the use of NSArray.
I'm still not sure of exactly what you want, though.