Set AudioWorkletProcessor input to Float32Array - web-audio-api

I have been trying to extract input from one AudioWorkletProcessor using postMessage, then insert that input into another AudioWorkletProcessor.
I managed to get the Float32Array into the second AudioWorkletProcessor process method but RingBuffer doesn't seem to work.
I don't know how to detect errors inside the process method or any error related to RingBuffer.
I get silence output, no errors in the console.
I need to know how can I pass the extracted Float32Array to ring-buffer-worklet-processor output.
Thank you in advance.
EDIT: After I added ( outputChannelCount: [2] ) to ringBufferWorkletNode options I started to get audio in the output but not clear at all.
const ringBufferWorkletNode = new AudioWorkletNode(
audioCtx,
"ring-buffer-worklet-processor",
{
outputChannelCount: [2],
processorOptions: {
kernelBufferSize: audioCtx.sampleRate,
channelCount: 2,
},
}
);
Main thread:
navigator.mediaDevices.getUserMedia({ audio: true, video: false }).then(async (stream) => {
const source = audioCtx.createMediaStreamSource(stream);
await audioCtx.audioWorklet.addModule("http://localhost:3003/lib/audio-processor.js");
await audioCtx.audioWorklet.addModule("http://localhost:3003/lib/ring-buffer-worklet-
processor.js");
const voiceNode = new AudioWorkletNode(audioCtx, "audio-processor");
const ringBufferWorkletNode = new AudioWorkletNode(audioCtx, "ring-buffer-worklet-processor",{
// EDIT: added outputChannelCount
outputChannelCount: [2],
processorOptions: {
kernelBufferSize: audioCtx.sampleRate / 2, // 48000/2
channelCount: 2,
},
}
);
voiceNode.port.onmessage = (e) => {
ringBufferWorkletNode.port.postMessage(e.data);
};
ringBufferWorkletNode.port.onmessage = (e) => {
console.log(e.data);
};
source.connect(voiceNode);
ringBufferWorkletNode.connect(audioCtx.destination);
}
audio-processor.js
class AudioProcessor extends AudioWorkletProcessor {
constructor(...args) {
super(...args);
}
process(inputList, outputList, parameters) {
this.port.postMessage(inputList[0]);
return true;
}
}
registerProcessor("audio-processor", AudioProcessor);
ring-buffer-worklet-processor.js
import Module from "./variable-buffer-kernel.wasmmodule.js";
import { HeapAudioBuffer, RingBuffer } from "./wasm-audio-helper.js";
class RingBufferWorkletProcessor extends AudioWorkletProcessor {
static get parameterDescriptors() {
return [
{
name: "input",
defaultValue: null,
},
];
}
constructor(options) {
super();
this._kernelBufferSize = options.processorOptions.kernelBufferSize;
this._channelCount = options.processorOptions.channelCount;
// RingBuffers for input and output.
this._inputRingBuffer = new RingBuffer(
this._kernelBufferSize,
this._channelCount
);
this._outputRingBuffer = new RingBuffer(
this._kernelBufferSize,
this._channelCount
);
// For WASM memory, also for input and output.
this._heapInputBuffer = new HeapAudioBuffer(
Module,
this._kernelBufferSize,
this._channelCount
);
this._heapOutputBuffer = new HeapAudioBuffer(
Module,
this._kernelBufferSize,
this._channelCount
);
// WASM audio processing kernel.
this._kernel = new Module.VariableBufferKernel(this._kernelBufferSize);
this.inputData = null;
this.port.onmessage = this.onmessage.bind(this);
}
onmessage = (e) => {
const { data } = e;
if (data) {
this.inputData = data;
} else {
this.inputData = null;
}
};
process(inputs, outputs, parameters) {
let output = outputs[0];
let input = this.inputData;
// AudioWorkletProcessor always gets 128 frames in and 128 frames out. Here
// we push 128 frames into the ring buffer.
if (input) {
this.port.postMessage(input);
// I get : (2) [Float32Array(128), Float32Array(128)] in console full with numbers not 0s
this._inputRingBuffer.push(input);
// Process only if we have enough frames for the kernel.
if (this._inputRingBuffer.framesAvailable >= this._kernelBufferSize) {
// Get the queued data from the input ring buffer.
this._inputRingBuffer.pull(this._heapInputBuffer.getChannelData());
// This WASM process function can be replaced with ScriptProcessor's
// |onaudioprocess| callback funciton. However, if the event handler
// touches DOM in the main scope, it needs to be translated with the
// async messaging via MessagePort.
this._kernel.process(
this._heapInputBuffer.getHeapAddress(),
this._heapOutputBuffer.getHeapAddress(),
this._channelCount
);
// Fill the output ring buffer with the processed data.
this._outputRingBuffer.push(this._heapOutputBuffer.getChannelData());
}
// Always pull 128 frames out. If the ring buffer does not have enough
// frames, the output will be silent.
this._outputRingBuffer.pull(output);
}
return true;
}
}
registerProcessor("ring-buffer-worklet-processor", RingBufferWorkletProcessor);
console.log sample coming from main thread:
ringBufferWorkletNode.port.onmessage = (e) => {
console.log(e.data);
};
(2) [Float32Array(128), Float32Array(128)]
0: Float32Array(128)
[0 … 99]
0: -0.00013565909466706216
1: -0.00004953467214363627
2: -0.00008576592517783865
3: -0.00005288537431624718
4: -0.000025271740014431998
5: -0.000051156635890947655
6: -0.00003429186836001463
7: -0.000021470399587997235
8: -0.000034319222322665155
9: -0.0000439783361798618
10: -0.000009586839041730855
11: -0.00003202698644599877
12: 0.000033984630135819316
13: -0.00002201009374402929
14: -0.00007097060733940452
15: 0.000004624932444130536
16: -0.00009633887384552509
17: 0.00000770429596741451
18: -0.00004159680975135416
19: -0.00012190178676974028
20: -0.00001845001861511264
21: -0.00008007502037798986
22: -0.00004237010216456838
23: -0.00001076792977983132
24: -0.00006972716801101342
25: -0.0000477194698760286
26: -0.000021934287360636517
27: -0.00009244760440196842
28: -0.000007403687050100416
29: 0.000007816993274900597
30: -0.00008117098332149908
31: -0.00003129038304905407
32: 0.00009489256626693532
33: 0.000023729033273411915
34: -0.000009693003448774107
35: -0.00003678350549307652
36: -0.00011439441732363775
37: -0.00003462867607595399
38: 0.000029057020583422855
39: -0.00003098553133895621
40: -0.00004036649261252023
41: -0.00001566937135066837
42: -0.00003948688390664756
43: -0.000021292273231665604
44: -0.000031062725611263886
45: -0.00006067131835152395
46: -0.00008801861258689314
47: -0.0000940829049795866
48: -0.000027806054276879877
49: 0.000005677926765201846
50: -0.00004410342808114365
51: -0.00005494384822668508
52: -0.00012077790597686544
53: -0.00005381474693422206
54: -0.00004889833144261502
55: -0.00006171152199385688
56: -0.00007169923628680408
57: -0.000027956590201938525
58: -0.0000925964122870937
59: -0.00008822995005175471
60: -0.00011014055053237826
61: -0.00009332459740107879
62: -0.00007393134728772566
63: -0.00009183597285300493
64: -0.000051114031521137804
65: -0.00009899734141072258
66: -0.00001619849535927642
67: -0.00006849400961073115
68: -0.00007494576129829511
69: -0.00004512929081101902
70: -0.00007846889639040455
71: -0.0000887925925781019
72: -0.00011394681496312842
73: -0.0000661616213619709
74: -0.00006388152542058378
75: -0.000028652870241785422
76: -0.000049569716793484986
77: -0.000008633718607597984
78: -0.00003698172440635972
79: -0.00007338733121287078
80: -0.00004050061761518009
81: -0.00011863364488817751
82: -0.00005003352271160111
83: 0.00009503970795776695
84: -0.000020715609934995882
85: -0.000040291022742167115
86: -0.00006244835822144523
87: -0.00013285929162520915
88: -0.00009266978304367512
89: -0.00015499485016334802
90: 0.000009959074304788373
91: -0.00002722918361541815
92: -0.000045168246288085356
93: 0.00005641198004013859
94: -6.97990401477e-7
95: -0.00008256734145106748
96: -0.00011380571231711656
97: -0.00010966734407702461
98: -0.00010636053775670007
99: -0.00009042541933013126
[100 … 127]
buffer:
ArrayBuffer(512)
byteLength: 512
byteOffset: 0
length: 128
Symbol(Symbol.toStringTag): (...)
[[Prototype]]: TypedArray
1: Float32Array(128)
[0 … 99]
[100 … 127]
buffer:
ArrayBuffer(512)
byteLength: 512
byteOffset: 0
length: 128
Symbol(Symbol.toStringTag): (...)
[[Prototype]]: TypedArray
length: 2
[[Prototype]]: Array(0)
This is the GitHub I was following: https://github.com/GoogleChromeLabs/web-audio-samples/tree/main/audio-worklet/design-pattern/wasm-ring-buffer

Related

Can eBPF's perf_submit() be used in a socket_filter program as well?

So I was trying to send some data from the kernel space program to the user space program using perf_submit.
I've done some studies and here(https://github.com/iovisor/bcc/issues/2423), yonghong-song answered(the last comment) that a socket_filter program can not access bpf_perf_event_output helper and therefore it can only be used for tracing program types.
However, on BCC reference site(https://github.com/iovisor/bcc/blob/master/docs/reference_guide.md#2-bpf_perf_output), if you ctrl+f and search for : 3. perf_submit()
, it says on the fifth line that "for SOCKET_FILTER programs, the struct __sk_buff *skb must be used instead."
I believe this infers that perf_submit() can be used for socket_filter programs as well?
So I have hard time figuring out if perf_submit() can indeed be used for a socket filter program. Maybe some functionalities have been added since Yonghong-song answered the question above?
I'm checking if perf_submit() would work with a socket filter and there's not really a line of code that grabs the data output by perf_submit because just addint perf_submit() in the kernel program already omitted an error.
Here's the code for my program :
from bcc import BPF
# Network interface to be monoitored
INTERFACE = "br-netrome"
bpf_text = """
#include <uapi/linux/ptrace.h>
#include <net/sock.h>
#include <bcc/proto.h>
#include <linux/bpf.h>
#define IP_TCP 6
#define IP_UDP 17
#define IP_ICMP 1
#define ETH_HLEN 14
BPF_PERF_OUTPUT(events); // has to be delcared outside any function
int packet_monitor(struct __sk_buff *skb) {
u8 *cursor = 0;
u64 saddr;
u64 daddr;
u64 ttl;
u64 hchecksum;
struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet));
if (!(ethernet -> type == 0x0800)) {
return 0; // drop
}
struct ip_t *ip = cursor_advance(cursor, sizeof(*ip));
/*
if (ip->nextp != IP_TCP)
{
if (ip -> nextp != IP_UDP)
{
if (ip -> nextp != IP_ICMP)
return 0;
}
}
*/
saddr = ip -> src;
daddr = ip -> dst;
ttl = ip -> ttl;
hchecksum = ip -> hchecksum;
events.perf_submit(skb, &saddr, sizeof(saddr));
// bpf_trace_printk("saddr = %llu, daddr = %llu, ttl = %llu", saddr, daddr, ttl); // only three arguments can be passed using printk
// bpf_trace_printk("Incoming packet!!\\n");
return -1;
}
and here is the error code :
R0=inv2048 R6=ctx(id=0,off=0,imm=0) R7=inv0 R10=fp0,call_-1
4: (20) r0 = *(u32 *)skb[26]
5: (7b) *(u64 *)(r10 -8) = r0
6: (18) r2 = 0xffff9bde204ffa00
8: (18) r7 = 0xffffffff
10: (bf) r4 = r10
11: (07) r4 += -8
12: (bf) r1 = r6
13: (18) r3 = 0xffffffff
15: (b7) r5 = 8
16: (85) call bpf_perf_event_output#25
unknown func bpf_perf_event_output#25
Traceback (most recent call last):
File "packet_monitor.py", line 68, in <module>
function_skb_matching = bpf.load_func("packet_monitor", BPF.SOCKET_FILTER)
File "/usr/lib/python2.7/dist-packages/bcc/__init__.py", line 397, in load_func
(func_name, errstr))
TL;DR. BPF programs of type BPF_PROG_TYPE_SOCKET_FILTER can use bpf_perf_event_output only starting with Linux 5.4.
Which helpers a given BPF program has access to is defined by the get_func_proto member of objects struct bpf_verifier_ops. You can find which bpf_verifier_ops object corresponds to which program type by reading function find_prog_type() and file bpf_types.h. In the case of BPF_PROG_TYPE_SOCKET_FILTER, the corresponding function is sk_filter_func_proto().
If you git blame that function on recent kernel sources, you will get something like the following (you can do the same with GitHub's blame feature):
$ git blame net/core/filter.c
[...]
2492d3b867043 (Daniel Borkmann 2017-01-24 01:06:27 +0100 6080) static const struct bpf_func_proto *
5e43f899b03a3 (Andrey Ignatov 2018-03-30 15:08:00 -0700 6081) sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
2492d3b867043 (Daniel Borkmann 2017-01-24 01:06:27 +0100 6082) {
2492d3b867043 (Daniel Borkmann 2017-01-24 01:06:27 +0100 6083) switch (func_id) {
2492d3b867043 (Daniel Borkmann 2017-01-24 01:06:27 +0100 6084) case BPF_FUNC_skb_load_bytes:
2492d3b867043 (Daniel Borkmann 2017-01-24 01:06:27 +0100 6085) return &bpf_skb_load_bytes_proto;
4e1ec56cdc597 (Daniel Borkmann 2018-05-04 01:08:15 +0200 6086) case BPF_FUNC_skb_load_bytes_relative:
4e1ec56cdc597 (Daniel Borkmann 2018-05-04 01:08:15 +0200 6087) return &bpf_skb_load_bytes_relative_proto;
91b8270f2a4d1 (Chenbo Feng 2017-03-22 17:27:34 -0700 6088) case BPF_FUNC_get_socket_cookie:
91b8270f2a4d1 (Chenbo Feng 2017-03-22 17:27:34 -0700 6089) return &bpf_get_socket_cookie_proto;
6acc5c2910689 (Chenbo Feng 2017-03-22 17:27:35 -0700 6090) case BPF_FUNC_get_socket_uid:
6acc5c2910689 (Chenbo Feng 2017-03-22 17:27:35 -0700 6091) return &bpf_get_socket_uid_proto;
7c4b90d79d0f4 (Allan Zhang 2019-07-23 17:07:24 -0700 6092) case BPF_FUNC_perf_event_output:
7c4b90d79d0f4 (Allan Zhang 2019-07-23 17:07:24 -0700 6093) return &bpf_skb_event_output_proto;
2492d3b867043 (Daniel Borkmann 2017-01-24 01:06:27 +0100 6094) default:
2492d3b867043 (Daniel Borkmann 2017-01-24 01:06:27 +0100 6095) return bpf_base_func_proto(func_id);
2492d3b867043 (Daniel Borkmann 2017-01-24 01:06:27 +0100 6096) }
2492d3b867043 (Daniel Borkmann 2017-01-24 01:06:27 +0100 6097) }
[...]
As you can see, BPF_FUNC_perf_event_output was only recently added to the list of helpers these BPF programs can call. The commit which added this support, 7c4b90d79d0f4, was merged in Linux v5.4:
$ git describe --contains 7c4b90d79d0f4
v5.4-rc1~131^2~248^2~20

running file diff on two files on Mac

Im comparing to files, but my understanding is that + signifies addition and - symbols deletion. The new file has a typo:
if (KEY_STATUS.spacr) {
The why is it represented by -. It should be +, right ? When I run diff -u game_new.js game_old.js:
--- game_new.js 2018-06-12 02:03:32.000000000 -0700
+++ game_old.js 2018-06-12 02:03:22.000000000 -0700
## -4,9 +4,9 ##
//
KEY_CODES = {
- 13: 'enter',
32: 'space',
37: 'left',
+ 38: 'up',
39: 'right',
40: 'down',
70: 'f',
## -392,7 +392,7 ##
this.vel.rot = 0;
}
- if (KEY_STATUS.spacr) {
+ if (KEY_STATUS.up) {
var rad = ((this.rot-90) * Math.PI)/180;
this.acc.x = 0.5 * Math.cos(rad);
this.acc.y = 0.5 * Math.sin(rad);
## -406,7 +406,7 ##
if (this.delayBeforeBullet > 0) {
this.delayBeforeBullet -= delta;
}
- if (KEY_STATUS.enter) {
+ if (KEY_STATUS.space) {
if (this.delayBeforeBullet <= 0) {
this.delayBeforeBullet = 10;
for (var i = 0; i < this.bullets.length; i++) {
## -919,7 +919,7 ##
waiting: function () {
Text.renderText(ipad ? 'Touch Sreen to Start' : 'Press Space to Start', 36, Game.canvasWidth/2 - 270, Game.canvasHeight/2);
if (KEY_STATUS.space || window.gameStart) {
- KEY_STATUS.space = false; // hack so we don't move right away
+ KEY_STATUS.space = false; // hack so we don't shoot right away
window.gameStart = false;
this.state = 'start';
}
I believe that when you run:
diff -u game_new.js game_old.js
The changes coming from the file on the left are interpreted as being the source, and marked with a minus, while the changes coming from the file on the right are treated as the destination, and marked with a plus.
If you want the - and + labels to appear as you want, then run diff with the files in the reverse order:
diff -u game_old.js game_new.js

How to get correct result from my RSA Sign?

I have written a piece of code to hash and rsa encrypt (somehow sign) a data sent to java card.
This is java card code:
public class HelloWorldApplet extends Applet {
final static byte APLET_CLA = (byte)0x80;
final static byte INITIALIZE = (byte)0x00;
final static byte SIGN = (byte)0x01;
final static byte HASHVERIFY = (byte)0x07;
final static byte GETHASH = (byte)0x08;
final static short SW_WRONG_DATA_LENGTH = 0x6300;
final static short SW_KEY_NOT_INITIALIZED = 0x6301;
final static short SW_KEY_IS_INITIALIZED = 0x6302;
final static short SW_KEY_IS_NOT_INITIALIZED = 0x6303;
final static short SW_INCORRECT_PARAMETER = 0x6304;
public static byte[] Message;
public static short message_len = 0;
public static short hashLen = 0;
public static short signLen = 0;
public static boolean key_initialization_flag256 = false;
public static boolean key_initialization_flag128 = false;
// use unpadded RSA cipher for signing
Cipher cipherRSA256 = Cipher.getInstance(Cipher.ALG_RSA_NOPAD, false);
Cipher cipherRSA128 = Cipher.getInstance(Cipher.ALG_RSA_NOPAD, false);
KeyPair rsaPair256 = new KeyPair(KeyPair.ALG_RSA_CRT, KeyBuilder.LENGTH_RSA_2048);
KeyPair rsaPair128 = new KeyPair(KeyPair.ALG_RSA_CRT, KeyBuilder.LENGTH_RSA_1024);
RSAPrivateCrtKey rsaKeyPriv256;
RSAPrivateCrtKey rsaKeyPriv128;
RSAPublicKey rsaKeyPub256;
RSAPublicKey rsaKeyPub128;
byte[] hashBuffer = JCSystem.makeTransientByteArray((short)256, JCSystem.CLEAR_ON_DESELECT);
byte[] signBuffer = JCSystem.makeTransientByteArray((short)256, JCSystem.CLEAR_ON_DESELECT);
byte[] dataBuffer = JCSystem.makeTransientByteArray((short)256, JCSystem.CLEAR_ON_DESELECT);
MessageDigest md = MessageDigest.getInstance(MessageDigest.ALG_SHA_256, false);
public static void install(byte[] bArray, short bOffset, byte bLength)
{
Message = new byte[256];
new HelloWorldApplet().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
}
public void process(APDU apdu)
{
if (selectingApplet())
{
return;
}
byte[] buffer = apdu.getBuffer();
if (buffer[ISO7816.OFFSET_CLA] == APPLET_CLA) {
switch (buffer[ISO7816.OFFSET_INS]) {
case INITIALIZE:
// generate a new key
initialize(apdu);
break;
case SIGN:
// sign a given incoming message
sign_message(apdu);
break;
case HASHVERIFY:
verify_hash(apdu);
break;
case GETHASH:
get_hash(apdu);
break;
default:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
} else {
ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
}
}
public void initialize(APDU apdu)
{
byte[] buffer = apdu.getBuffer();
switch(buffer[ISO7816.OFFSET_P1]) {
case 0x00: // gen 256 byte RSA key (P1=00)
if (key_initialization_flag256)
ISOException.throwIt(SW_KEY_IS_INITIALIZED);
rsaPair256.genKeyPair();
rsaKeyPriv256 = (RSAPrivateCrtKey) rsaPair256.getPrivate();
rsaKeyPub256 = (RSAPublicKey) rsaPair256.getPublic();
key_initialization_flag256 = true;
break;
case 0x01: // gen 128 byte RSA key (P1=01)
if (key_initialization_flag128)
ISOException.throwIt(SW_KEY_IS_INITIALIZED);
rsaPair128.genKeyPair();
rsaKeyPriv128 = (RSAPrivateCrtKey) rsaPair128.getPrivate();
rsaKeyPub128 = (RSAPublicKey) rsaPair128.getPublic();
key_initialization_flag128 = true;
break;
}
}
// P1=0 for modulus, P1=1 for exponent
private void getPublicRSA(APDU apdu)
{
byte[] buffer = apdu.getBuffer();
short length = 0;
switch (buffer[ISO7816.OFFSET_P1])
{
case 0x00: // 256 byte RSA (P1)
if (!key_initialization_flag256)
ISOException.throwIt(SW_KEY_IS_INITIALIZED);
switch (buffer[ISO7816.OFFSET_P2]) {
case 0x00: // get the modulus (P2)
length = rsaKeyPub256.getModulus(buffer, (short) 0);
break;
case 0x01: // get the exponent (P2)
length = rsaKeyPub256.getExponent(buffer, (short) 0);
break;
default:
ISOException.throwIt(SW_INCORRECT_PARAMETER);
break;
}
break;
case 0x01: // 128 byte RSA (P1)
if (!key_initialization_flag128)
ISOException.throwIt(SW_KEY_IS_INITIALIZED);
switch (buffer[ISO7816.OFFSET_P2]) {
case 0x00: // get the modulus (P2)
length = rsaKeyPub128.getModulus(buffer, (short) 0);
break;
case 0x01: // get the exponent (P2)
length = rsaKeyPub128.getExponent(buffer, (short) 0);
break;
default:
ISOException.throwIt(SW_INCORRECT_PARAMETER);
break;
}
break;
default:
ISOException.throwIt(SW_INCORRECT_PARAMETER);
}
apdu.setOutgoingAndSend((short) 0, length);
}
public void sign_message(APDU apdu)
{
byte[] buffer = apdu.getBuffer();
switch(message_len) {
case 256:
if(!key_initialization_flag256)
ISOException.throwIt(SW_KEY_IS_NOT_INITIALIZED);
cipherRSA256.init(rsaPair256.getPrivate(), Cipher.MODE_ENCRYPT);
Util.arrayCopyNonAtomic(Message, (short) 0, dataBuffer, (short) 0, message_len);
pkcs1_sha(dataBuffer, (short) 0, message_len, hashBuffer); // 32 Bytes
signLen = cipherRSA256.doFinal(hashBuffer, (short) 0, message_len, signBuffer, (short) 0); // 128 Bytes
Util.arrayCopy(signBuffer,(short)0,buffer,(short)0,signLen);
apdu.setOutgoingAndSend((short) 0, signLen);
break;
case 128:
if(!key_initialization_flag128)
ISOException.throwIt(SW_KEY_IS_NOT_INITIALIZED);
cipherRSA128.init(rsaPair128.getPrivate(), Cipher.MODE_ENCRYPT);
Util.arrayCopyNonAtomic(Message, (short) 0, dataBuffer, (short) 0, message_len);
pkcs1_sha(dataBuffer, (short) 0, message_len, hashBuffer); // 32 Bytes
signLen = cipherRSA128.doFinal(hashBuffer, (short) 0, message_len, signBuffer, (short) 0); // 128 Bytes
Util.arrayCopy(signBuffer, (short) 0, buffer, (short) 0, signLen);
apdu.setOutgoingAndSend((short) 0, signLen);
break;
default:
ISOException.throwIt(SW_WRONG_DATA_LENGTH);
break;
}
}
public void verify_hash(APDU apdu) {
byte[] buffer = apdu.getBuffer();
switch(message_len) {
case 256:
if(!key_initialization_flag256)
ISOException.throwIt(SW_KEY_IS_INITIALIZED);
cipherRSA256.init(rsaPair256.getPublic(), Cipher.MODE_DECRYPT);
hashLen = cipherRSA256.doFinal(signBuffer, (short) 0, message_len, buffer, (short) 0);
apdu.setOutgoingAndSend((short) 0, message_len);
break;
case 128:
if(!key_initialization_flag128)
ISOException.throwIt(SW_KEY_IS_INITIALIZED);
cipherRSA128.init(rsaPair128.getPublic(), Cipher.MODE_DECRYPT);
hashLen = cipherRSA128.doFinal(signBuffer, (short) 0, message_len, buffer, (short) 0);
apdu.setOutgoingAndSend((short) 0, message_len);
break;
default:
ISOException.throwIt(SW_WRONG_DATA_LENGTH);
break;
}
}
public void get_hash(APDU apdu) {
byte[] buffer = apdu.getBuffer();
Util.arrayCopy(hashBuffer,(short)0,buffer,(short)0,message_len);
apdu.setOutgoingAndSend((short)0,message_len);
}
// this function will leave tempBuffer with the data to be signed
public void pkcs1_sha(byte[] toSign, short bOffset, short bLength,byte[] out)
{
md.reset();
hashLen = md.doFinal(toSign, bOffset, bLength, out, (short) 0);
}
}
the problem is, when I execute sign, verify and get hash commands, I desire to get same answers from verify and get hash. for some data the card answers correctly:
mode_211
enable_trace
establish_context
enable_trace
enable_timer
card_connect
command time: 593 ms
select -AID E0E1E2E3E4E501
Command --> 00A4040007E0E1E2E3E4E501
Wrapped command --> 00A4040007E0E1E2E3E4E501
Response <-- 9000
command time: 31 ms
send_apdu -sc 1 -APDU 8000010000 // Gen RSA Key 128
Command --> 8000010000
Wrapped command --> 8000010000
Response <-- 6302
send_APDU() returns 0x80206302 (Unknown ISO7816 error: 0x6302)
command time: 0 ms
send_apdu -sc 1 -APDU 80040000802EEEEFF1115D0B637ED81CE45EA86984E37521409EB67A7D
9E7DE88CF3BDC693B2B4F05748F9E705B2FE1C1D8CB9288B32D06952B6193935DD14FF9C89B9860B
0B31B9BA3C4130A4CC1CEC5CE430A784525B10706EC971C25B4C45CA3C9D98ECDB0825DADB499F31
36CB7322DFC44F4DAD71133CA894A14446416021684B9029
Command --> 80040000802EEEEFF1115D0B637ED81CE45EA86984E37521409EB67A7D9E7DE88CF3
BDC693B2B4F05748F9E705B2FE1C1D8CB9288B32D06952B6193935DD14FF9C89B9860B0B31B9BA3C
4130A4CC1CEC5CE430A784525B10706EC971C25B4C45CA3C9D98ECDB0825DADB499F3136CB7322DF
C44F4DAD71133CA894A14446416021684B9029
Wrapped command --> 80040000802EEEEFF1115D0B637ED81CE45EA86984E37521409EB67A7D9E
7DE88CF3BDC693B2B4F05748F9E705B2FE1C1D8CB9288B32D06952B6193935DD14FF9C89B9860B0B
31B9BA3C4130A4CC1CEC5CE430A784525B10706EC971C25B4C45CA3C9D98ECDB0825DADB499F3136
CB7322DFC44F4DAD71133CA894A14446416021684B9029
Response <-- 9000
send_APDU() returns 0x80209000 (9000: Success. No error.)
command time: 78 ms
send_apdu -sc 1 -APDU 8001000000 // Sign Message
Command --> 8001000000
Wrapped command --> 8001000000
Response <-- BF9E0C7BE366F6E9B2E78A8E7E101F8BFEDB3497AE68A7B8FCCA158DBDB937E6F62
76971AC7BF2B96F4258D4745719CBC93DEEDD344B512BCB1B8D6105837FB2C5F983C92F01FF0D8B5
3B009DA8EB76124EB4BFE24D598144A6726926A2A84E7F0C8FD00CE13121CDF68A3B87D6DCD06ED4
6EB56DE0E073B3BDBF37EC9934BCD9000
send_APDU() returns 0x80209000 (9000: Success. No error.)
command time: 203 ms
send_apdu -sc 1 -APDU 8008000000 // get hash
Command --> 8008000000
Wrapped command --> 8008000000
Response <-- 0BDE2856A3EC1083EBE56E8FCC7294ED06A7B63633C438A6CCB3C3B1D1C88811000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000009000
send_APDU() returns 0x80209000 (9000: Success. No error.)
command time: 47 ms
send_apdu -sc 1 -APDU 8007000000 // verify hash
Command --> 8007000000
Wrapped command --> 8007000000
Response <-- 0BDE2856A3EC1083EBE56E8FCC7294ED06A7B63633C438A6CCB3C3B1D1C88811000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000009000
send_APDU() returns 0x80209000 (9000: Success. No error.)
command time: 78 ms
card_disconnect
command time: 203 ms
release_context
command time: 0 ms
but for some other data, I get incorrect respond from card and I don't know why this happens. here is the second data answering incorrectly:
mode_211
enable_trace
establish_context
enable_trace
enable_timer
card_connect
command time: 577 ms
select -AID E0E1E2E3E4E501
Command --> 00A4040007E0E1E2E3E4E501
Wrapped command --> 00A4040007E0E1E2E3E4E501
Response <-- 9000
command time: 32 ms
send_apdu -sc 1 -APDU 8000010000 // Gen RSA Key 128
Command --> 8000010000
Wrapped command --> 8000010000
Response <-- 6302
send_APDU() returns 0x80206302 (Unknown ISO7816 error: 0x6302)
command time: 15 ms
send_apdu -sc 1 -APDU 8004000080335BE314CBC5C739DB41CCDD8FD0F53BB8F80E57DD3C18A9
091A715347BAFC5FD912A8973389BDFF05CF50A6E1B4716969A2C828A924D399D0A6A93DB0427666
7C9B623065D13192E5F372C8584A7111E74FC61923E1C1353DD1AE7D18991BEB3D83C49E6B756ABC
3359F9920E10BBEA24A999FB3E9A0CC1AC07E5C368F463DF
Command --> 8004000080335BE314CBC5C739DB41CCDD8FD0F53BB8F80E57DD3C18A9091A715347
BAFC5FD912A8973389BDFF05CF50A6E1B4716969A2C828A924D399D0A6A93DB04276667C9B623065
D13192E5F372C8584A7111E74FC61923E1C1353DD1AE7D18991BEB3D83C49E6B756ABC3359F9920E
10BBEA24A999FB3E9A0CC1AC07E5C368F463DF
Wrapped command --> 8004000080335BE314CBC5C739DB41CCDD8FD0F53BB8F80E57DD3C18A909
1A715347BAFC5FD912A8973389BDFF05CF50A6E1B4716969A2C828A924D399D0A6A93DB04276667C
9B623065D13192E5F372C8584A7111E74FC61923E1C1353DD1AE7D18991BEB3D83C49E6B756ABC33
59F9920E10BBEA24A999FB3E9A0CC1AC07E5C368F463DF
Response <-- 9000
send_APDU() returns 0x80209000 (9000: Success. No error.)
command time: 63 ms
send_apdu -sc 1 -APDU 8001000000 // Sign Message
Command --> 8001000000
Wrapped command --> 8001000000
Response <-- 9581924BA1F374489F83845FEEB7D71D71D3240C915CB1462434230982CA87A8AF3
0C2A716127C184E5DBB9EFF890A74A67967AC3EFE7E03FB433A3E52989459FDEF2CA8C19CD7BCD98
16434C4D84CF639F1D542F50B19BB56251BEA965F88168803F98906567CF1C2C6CE8B38999E16C50
49E2329F13156FA964F7477C07EF79000
send_APDU() returns 0x80209000 (9000: Success. No error.)
command time: 202 ms
send_apdu -sc 1 -APDU 8008000000 // get hash
Command --> 8008000000
Wrapped command --> 8008000000
Response <-- D9FE50E885FA4F2F85C70DC66FF42B5ABFAFB81820BF66F1967CB33D6E08AAE6000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000009000
send_APDU() returns 0x80209000 (9000: Success. No error.)
command time: 47 ms
send_apdu -sc 1 -APDU 8007000000 // verify hash
Command --> 8007000000
Wrapped command --> 8007000000
Response <-- 13AD6610CA13A7064193753BE50F268EC3F0D0AE8A5F079C0D40548835EAB0E9267
63B8EC02435CE8E126A4C97643D6BD2051E146CE353D351FF5FC58C0D2AD8BC5ADB40067F807EFE1
B2E1A37E027E6F50BA82DCE55DC37584F5941B6F29439234D97BE7612B03987E99C1F9F9A5A3B949
1E9758ECC7E02767D76B7C6C7A0B99000
send_APDU() returns 0x80209000 (9000: Success. No error.)
command time: 78 ms
card_disconnect
command time: 203 ms
release_context
command time: 0 ms
I will be so thankful if anyone can give me a hint or has had the same experience!!!
Your hash is placed at the most significant end of the value, as RSA uses big endian encoding. This means that the value may be returned mod N (the modulus), which will result in a different value than that you put in.
Place the hash at the least significant end of the message and your code should run.
Notes:
RSA without padding is not secure;
Signature generation is not the same as encrypting a hash value with a private key if you consider padding, side channel attacks, performance etc. etc. etc..

Why aren't firstResult and maxResult working in withCriteria?

I'm getting the following error:
Class
groovy.lang.MissingMethodException
Message
No signature of method: com.apposit.terra.connect.service.OrganizationUserService.firstResult() is applicable for argument types: (java.lang.Long) values: [2] Possible solutions: findResult(groovy.lang.Closure), findResult(java.lang.Object, groovy.lang.Closure)
Around line 35 of grails-app/services/com/apposit/terra/connect/service/OrganizationUserService.groovy
32: if(null != organization)
33: return User.withCriteria { eq( "organization" , organization)
34: eq( "accountExpired" , false )
35: firstResult( offset )
36: maxResults( max )
37:
38: }
I'm using mongodb.

PHP Fatal error: Using $this when not in object context for script

I've got problem:
I have a script that I'm working with and just upgraded to PHP 5.3.
In my kit_parser.php I'm getting the following fatal error:
Fatal error: Using $this when not in object context in /home/sitename/public_html/secure/includes/hooks/kits/kit_parser.php on line 71
This is the section of code it's referencing:
LINE 71---> $this->kit__log_add(array("<b>PHP Warning</b> [$errno] $errstr on line $errline in file $errfile"));
function kit_error($errno, $errstr, $errfile, $errline, $die = false) {
if (1==1){//$this->displayErrors ) {
switch ($errno) {
/* Custom Errors */
case E_USER_ERROR:
break;
case E_USER_WARNING:
break;
case E_USER_NOTICE:
break;
case E_ERROR:
$this->kit__log_add(array("<b>PHP Error</b> [$errno] $errstr on line $errline in file $errfile"));
die();
break;
case E_WARNING:
$this->kit__log_add(array("<b>PHP Warning</b> [$errno] $errstr on line $errline in file $errfile"));
break;
}
return true;
}
return false;
}
Why is the error coming? Nothing found, never seen before in my other scripts. Can anyone help me please?
This error usually means that you're using $this in a static class method. Make sure that the method this code is in is not static. If it is, you should probably be using this syntax instead:
YourClassNameGoesHere::kit__log_add(array("<b>PHP Warning</b> [$errno] $errstr on line $errline in file $errfile"));