HMAC-Whirlpool implementation in Android - android

Has anyone implemented or using Hmac-Whirlpool on Android phone?
I found whirlpool.java on internet but default security provider in Android SDK seems not to have Whirlpool nor Hmac-Whirlpool.

Android has cut-down version of Bouncy Castle and seems not to allow access to org.bouncycastle.crypto.macs.HMac class, instead javax.crypto.Mac.getInstance(String algorithm) should be used (here). Again only some MAC algorithms seems to be allowed (I saw "HMAC-SHA512" is working). But if you decide to use SpongyCastle library, you can do this (here):
CipherParameters p = new KeyParameter(key.getBytes("UTF-8"));
WhirlpoolDigest w = new WhirlpoolDigest();
HMac hm = new HMac(w);
hm.init(p);
hm.update(inbytes, 0, inbytes.length);
byte[] result = new byte[hm.getMacSize()];
hm.doFinal(result, 0);
Including SpongyCastle may be problematic to many becuase it increased app size by 1.84MB in android 2.2. Then only relevant files could be imported into the project:
// interfaces
org.bouncycastle.crypto.CipherParameters
org.bouncycastle.crypto.Digest
org.bouncycastle.crypto.ExtendedDigest
org.bouncycastle.crypto.Mac
// classes
org.bouncycastle.crypto.params.KeyParameter
org.bouncycastle.crypto.digests.WhirlpoolDigest
org.bouncycastle.crypto.macs.HMac
org.bouncycastle.crypto.DataLengthException
org.bouncycastle.crypto.RuntimeCryptoException

Related

TFlite with delegate gpu is give wrong result

I'm trying to inference my tflite model on c++ code at an embedded device.
So, I type simple tflite inference code which is using GPU.
And I cross-compile in my PC and run at the embedded device which is running android.
However, (1) If I use delegate gpu option, c++ codes give random results.
(2) It has given same input, but the results changed every time.
(3) When I turn off the gpu option, it gives me a correct results.
When I test my tflite model in python, it gives the correct output.
So I think the model file has no problem.
Should I re-build TensorFlow-lite because I use a prebuilt .so file? or Should I change the GPU option?
I don't know what should I check more.
Please Help!
Here is my C++ code
// Load model
std::unique_ptr<tflite::FlatBufferModel> model =
tflite::FlatBufferModel::BuildFromFile(model_file.c_str());
// Build the interpreter
tflite::ops::builtin::BuiltinOpResolver resolver;
std::unique_ptr<tflite::Interpreter> interpreter;
tflite::InterpreterBuilder(*model, resolver)(&interpreter);
// set delegate option
bool use_gpu = true;
if(use_gpu)
{
TfLiteDelegate* delegate;
auto options = TfLiteGpuDelegateOptionsV2Default();
options.inference_preference = TFLITE_GPU_INFERENCE_PREFERENCE_FAST_SINGLE_ANSWER;
options.inference_priority1 = TFLITE_GPU_INFERENCE_PRIORITY_AUTO;
delegate = TfLiteGpuDelegateV2Create(&options);
interpreter->ModifyGraphWithDelegate(delegate);
}
interpreter->AllocateTensors();
// set input
float* input = interpreter->typed_input_tensor<float>(0);
for(int i=0; i<width*height*channel; i++)
*(input+i) = 1;
TfLiteTensor* output_tensor = nullptr;
// Inference
interpreter->Invoke();
// Check output
output_tensor = interpreter->tensor(interpreter->outputs()[0]);
printf("Result : %f\n",output_tensor->data.f[0]);
//float* output = interpreter->typed_output_tensor<float>(0);
//printf("output : %f\n",*(output));
Luckily, I found the answer.
This was caused by the mismatch of the NDK version. (The prebuilt so file I used is built another version)
After unifying the NDK version to 21, it was tested again, and it worked normally.
In my case same application using gpu on S10 lite was not working but on S22,S21 Ultra 5G was working.
So you can consider running on different chipset configuration on different devices with same application.

Titanium Hyperloop access to android.os.SystemProperties

I have been trying a ( i hope) simple bit of Android hyperloop code directly within a titanium project (using SDK 7.0.1.GA and hyperloop 3).
var sysProp = require('android.os.SystemProperties');
var serialNumber = sysProp.get("sys.serialnumber", "none");
But when the app is run it reports
Requested module not found:android.os.SystemProperties
I think this maybe due to the fact that when compiling the app (using the cli) it reports
hyperloop:generateSources: Skipping Hyperloop wrapper generation, no usage found ...
I have similar code in a jar and if I use this then it does work, so I am wondering why the hyperloop generation is not being triggered, as I assume that is the issue.
Sorry should have explained better.
This is the jar source that I use, the extraction of the serial number was just an example (I need access to other info manufacturer specific data as well), I wanted to see if I could replicate the JAR functionality using just hyperloop rather that including the JAR file. Guess if it's not broke don't fix it, but was curious to see if it could be done.
So with the feedback from #miga and a bit of trial and error, I have come up with a solution that works really well and will do the method reflection that is required. My new Hyperloop function is
function getData(data){
var result = false;
var Class = require("java.lang.Class");
var String = require("java.lang.String");
var c = Class.forName("android.os.SystemProperties");
var get = c.getMethod("get", String.class, String.class);
result = get.invoke(c, data, "Error");
return result;
}
Where data is a string of the system property I want.
I am using it to extract and match a serial number from a Samsung device that is a System Property call "ril.serialnumber" or "sys.serialnumber". Now I can use the above function to do what I was using the JAR file for. Just thought I'd share in case anyone else needed something similar.
It is because android.os.SystemProperties is not class you can import. Check the android documentation at https://developer.android.com/reference/android/os/package-summary.html
You could use
var build = require('android.os.Build');
console.log(build.SERIAL);
to access the serial number.

jamod with Modbus Serial Slave

I'm using jamod (ModBus api) with a Beaglebone Black which runs on Android. I successfully used it for ModBus TCP Slave app. It worked like a charm. Now I'm trying to use it for Modbus serial slave app. But I am having an error. I think the problem is portname. I'm not realy sure what to use as a portname. Here is the code I tried:
ModbusSerialListener listener = null;
SimpleProcessImage spi = null;
spi = new SimpleProcessImage();
spi.addRegister(new SimpleRegister(45));
spi.addRegister(new SimpleRegister(56));
spi.addDigitalOut(new SimpleDigitalOut(false));
spi.addDigitalOut(new SimpleDigitalOut(false));
ModbusCoupler.getReference().setProcessImage(spi);
ModbusCoupler.getReference().setMaster(false);
ModbusCoupler.getReference().setUnitID(1);
SerialParameters params = new SerialParameters();
params.setPortName("/dev/ttyO0");
params.setBaudRate(9600);
params.setDatabits(8);
params.setParity("None");
params.setStopbits(1);
params.setEncoding("ascii");
params.setEcho(false);
listener = new ModbusSerialListener(params);
listener.setListening(true);
Error: java.lang.NoClassDefFoundError: net.wimpi.modbus.net.SerialConnection .
And I'm getting it at "listener = new ModbusSerialListener(params);" part.
----------------------Edit ----------------------
I found what causes the problem. jamod trying to use javax.comm api. Since there isn't one on Android, the app crashes. Is there any way to import javax.comm into a project??
for me it helped to drag "comm.jar" and "RXTXcomm.jar" in the lib folder of my project.
This jar files are in the jamod.zip from sourceforge

android crypto implementation android 4.3

I am referring "Android crypto implementation" document. w.r.t Android 4.3 source.
In point 2 it says The framework starts up, and sees that vold.decrypt is set to "1"
But I am not able to locate the code that does things mentioned in point 2. Is anybody able to locate it? Is it somewhere related to class BackupRestoreConfirmation?
Just a simple git grep will get the all the reference to this property:
cmds/bootanimation/BootAnimation.cpp:276: property_get("vold.decrypt", decrypt, "");
services/java/com/android/server/DevicePolicyManagerService.java:1869: String state = SystemProperties.get("vold.decrypt");
services/java/com/android/server/SystemServer.java:239: String cryptState = SystemProperties.get("vold.decrypt");
services/java/com/android/server/usb/UsbDeviceManager.java:173: boolean dataEncrypted = "1".equals(SystemProperties.get("vold.decrypt"));
And I think the one you need is in DevicePolicyManagerService.java or SystemServer.java

PROPFIND Box.com and WebDav (JackRabbit)

In an attempt to bypass Box file/folder IDs and supporting a number of other services as well I decided to implement with WebDAV since I'm somewhat familiar with it on my linux box. I chose a library based on JackRabbit modified to work on Android which seemed to suit my needs. However, it wasn't long until I ran into a problem.
When attempting to list Box's root entries, multiStatus.getResponses() returns an empty array. When accessing another webdav server I get the responses as expected. Both servers return status code 207, as expected.
My code is below, any thoughts?
EDIT: I can move a file, though listing a directory's entries won't work :/
String host = "https://www.box.com/dav/";
//String host = "http://demo.sabredav.org/";
hostConfig = new HostConfiguration();
hostConfig.setHost(host);
HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
HttpConnectionManagerParams params = new HttpConnectionManagerParams();
int maxHostConnections = 20;
params.setMaxConnectionsPerHost(hostConfig, maxHostConnections);
connectionManager.setParams(params);
client = new HttpClient(connectionManager);
Credentials creds = new UsernamePasswordCredentials("BOXEMAILADDRESS", "MYBOXPASSWORD");
//Credentials creds = new UsernamePasswordCredentials("testuser", "test");
client.getState().setCredentials(AuthScope.ANY, creds);
client.setHostConfiguration(hostConfig);
try
{
String propfindUri = host;
DavMethod method = new PropFindMethod(propfindUri, DavConstants.PROPFIND_ALL_PROP, DavConstants.DEPTH_1);
client.executeMethod(method);
Log.i("Status: " + method.getStatusCode());
MultiStatus multiStatus = method.getResponseBodyAsMultiStatus();
MultiStatusResponse[] responses = multiStatus.getResponses();
Log.i("Length: " + responses.length);
for(MultiStatusResponse response : responses)
{
Log.i("File: " + response.getHref());
}
}
catch (Exception e)
{
Log.printStackTrace(e);
}
While Box has some support for WebDAV, we only officially support it for iOS at the moment. Our testing has shown that our implementation of DAV works pretty well with the Windows native DAV client, as well as the Panic-Transmit Mac-specific client. Though the interactions there are not completely perfect.
Box WebDAV does not work well with the native osX (Mac) webDAV client. Expect huge delays as it looks like that client tries to load the whole tree before it displays anything.
Linux users may be able to tell you here on StackTrace which of the various OS webDAV clients/libs they've tried and which ones have worked better than others.
We do have plans to turn the crank and 10x improve our webDAV support sometime later this year, but we do not have a specific date, and just the nature of webDAV clients is such that even when we fix many of the issues with it, some client experiences on webDAV may still suck. For that reason we may only officially endorse a couple webDAV clients/libs per platform.
Hope that helps.

Categories

Resources