Retail MAC Calculation in Java

The following java code shows Cryptographic checksum calculated using ISO/IEC 9797-1 MAC algorithm 3 with block cipher DES, zero IV (8 bytes), and ISO/IEC 9797-1 padding method 2. No external library like Bouncy Castle, used.

public byte[] retailMac(byte[] key, byte[] data) {
 int loc = 0;
 byte[] edata;
 // Create Keys
 byte[] key1 = Arrays.copyOf(key, 8);
 byte[] key2 = Arrays.copyOfRange(key, 8, 16);

 // ISO/IEC 9797-1 or ISO 7816d4 Padding for data (adding 80 00 ..)
 byte[] pdata = pad-iso-7816d4(data, 8);

 try {
  SecretKey ka = new SecretKeySpec(key1, "DES");
  Cipher cipherA = Cipher.getInstance("DES/CBC/NoPadding");
  cipherA.init(Cipher.ENCRYPT_MODE, ka, new IvParameterSpec(new byte[8]));

  SecretKey kb = new SecretKeySpec(key2, "DES");
  Cipher cipherB = Cipher.getInstance("DES/CBC/NoPadding");
  cipherB.init(Cipher.DECRYPT_MODE, kb, new IvParameterSpec(new byte[8]));

  // Encrypt block by block with Key-A
  edata = cipherA.doFinal(pdata);

  byte[] x = new byte[8];
  for (loc = 0; loc < pdata.length; loc += 8) {
    System.arraycopy(pdata, loc, x, 0, 8);
    byte[] y = xor-array(edata, x);
    edata = cipherA.doFinal(y);
  }

  // Decrypt the resulting block with Key-B
  edata = cipherB.doFinal(edata);
  // Encrypt the resulting block with Key-A
  edata = cipherA.doFinal(edata);
 } catch (Exception e) {
  e.printStackTrace();
  return null;
 }
 return edata;
} 

This is not a perfect code, like repeated XOR part can be replaced by single doFinal.

Advertisements
Tagged with: ,
Posted in Uncategorized

Moving to WordPress

By April 30th 2013, the Posterous Blog Service is going to be stopping. I used it for last few years. Good platform. Anyway after it took-over by the Twitter, there is no proper road plan. So I got a doubt. Now it became true. WP gives me an option to run my own instance. So I decided to move to WordPress.

Posted in Uncategorized

Oracle VM: LDMP2V Tool

Recently I am playing with the Oracle T4 Servers, to evaluate the virtualization process of Solaris 10 OS Environment from old SunFire V-Series Hardware to new T4. The OVM package comes with a script tool called ldmp2v. It is very handy and helpful to virtulaize the Physical to Virtual machine, in a 3 step process (Collect, Prepare and Convert).

After fresh installation of Solaris 11 on T4 (Target), we will find the factory-default primary LDOM0. You don’t need to create any other LDOM, the ldmp2v will do that for you.

  • {Target-T4}
    • Create Writeable NFS share folder.
  • {Source-V215}
    • Stop all the user process and services.
    • Mount the Target NFS folder
    • Copy the ldmp2v script from target to source
    • ./ldmp2v collect -d /remote/nfs-folder/p2v1 (It may take ~1Hrs, depends on the data size and network speed)
  • {Target}
    • Edit the /etc/ldmp2v.conf to specify your preference.
    • ldmp2v prepare -d /local/nfs-folder/p2v1 p2v1 (It may take ~2Hrs)
    • Switch off the Source machine
    • ldmp2v convert -i SolarisOS.iso -d /local/nfs-folder/p2v1 p2v1 (It may take ~2Hrs)
    • Now your new VM (p2v1) is ready
    • ldm list
    • ldm bind p2v1
    • ldm start p2v1
    • telnet localhost 500x -> Login to LDOM Console.

The entire process may take ~5Hrs approx, YMMV. For more info, plz refer the Oracle OVM Page OR Fujitsu Document on Oracle VM Server Guide.

Tagged with:
Posted in Uncategorized

Android DatePicker Example

It is very simple to use the new DatePicker Widget in Android 4.1:

In Layout:

<DatePicker
 android:id="@+id/dp_dob"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:calendarViewShown="false"
 android:textAlignment="center" /> 

In Code:

dp_dob.init(yy, mm, dd, new OnDateChangedListener(){
 @Override
 public void onDateChanged(DatePicker vv, int yy, int mm, int dd){
     tv.setText(new StringBuilder().append(dd).append("-").
            append(mm+1) .append("-").append(yy).append(" "));
   }
} 
Posted in Uncategorized

Moving to new job

On Aug end, I resigned from International SQLStar and joined in Auctorizium (Netrust) Pte Ltd. I will be working on Passport and Border Control related security and embedded project.

Posted in Uncategorized

Digia acquired Qt from Nokia

Qt, cross-platform application and UI framework programming tool kit, was developed by TrollTech, then acquired by Nokia. Recently lot of rumor about Nokia’s future. Yesterday in Qt Nokia Blog, they mentioned that, Digia acquired the Qt technology & business from Nokia. It is good news for Qt developer and user. But we need to wait and watch about Digia plan & commitment towards the Qt Open Source version.

New Qt Home: http://qt.digia.com/

Qt Link: http://blog.qt.nokia.com/2012/08/09/digia-extends-its-commitment-to-qt-with-p…
Digia Link: http://www.digia.com/en/Home/Company/News/Digia-to-acquire-Qt-from-Nokia/

Posted in Uncategorized

RedHat OpenShift

Openshift-logo

Redhat’s OpenShift is free, auto-scaling Platform as a Service (PaaS) for applications. As an application platform in the cloud, OpenShift manages the stack so you can focus on your code.
Few months back itself, I created an OpenShift account. But never used it, due to some reasons. On 24th Jul, I attended the short OpenShift workshop by RedHat at SMU. It is not an detailed one, but give some idea on OpenShift and its usage. So started exploring it. Planning to run python based REST service to support mobile application.

OpenShift: https://openshift.redhat.com/app/

OpenShift@GitHub: https://github.com/openshift

Tagged with:
Posted in Uncategorized

Android Layer Interaction: Application to Kernel

Like any other OS platform, Android also designed in layered approach. Theoretically explaining the software layer interaction is bit difficult. So let me share my understanding, with an simple Vibrator application example. In this, we will see the control flow from application to kernel.

Application User Interface:

Screenshot-2

Application Source Code:

 public class MyActivity extends Activity{
 Vibrator vs;
 Button bn1;
 @Override
 public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  vs = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
  bn1 = (Button) findViewById(R.id.button1);
  bn1.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View arg0){
    vs.vibrate(1000);
    }
  });
 }
} 

Layer Interaction or Control Flow:
To access the vibrator service, first we are getting the local manager object or handler (vs). In Application Layer, vibrator service is getting called inside onClick() of the Button1. After calling vs.vibrate(), the control goes into vibrator service (Framework Layer), thru the binder interface.

Vibrator Service source code is in the following path:
Android_SRC/frameworks/base/services/java/com/android/server/VibratorService.java

Control reach the below function:
public void vibrate(long milliseconds, IBinder token)

Inside vibrate():

  1. Check for Permission
  2. Check the value (!= 0) and object (currently no vibration activity going on)
  3. Create Vibration object
  4. Start the vibration – startVibrationLocked(vib)

Inside startVibrationLocked():

  1. If mTimeout != 0, then call the native/JNI function vibratorOn().
  2. else, call the code, which handle the rhythmic vibration pattern, (Let us not worry about rhythmic vibration), which intern call the vibratorOn().

Next, to execute the vibratorOn(), control goes into the Native Layer thru JNI.

Native layer code is in the below path:
Android_SRC/frameworks/base/services/jni/com_android_server_VibratorService.cpp

Control reach the below function:
static void vibratorOn(JNIEnv *env, jobject clazz, jlong timeout_ms)

Inside vibratorOn():

  1. Call the vibrator_on(), which is a HAL (Hardware Abstraction Layer) function.

HAL code is in the below path:
Android_SRC/hardware/libhardware_legacy/vibrator/vibrator.c

Control reach the below function:
int vibrator_on()

Inside vibrator_on():

  1. Call sendit().
  2. In sendit, we will open the vibrator device file and write the time-out value into it, which intern pass the value to vibrator device driver (Kernel Layer).
  3. The driver can activate the vibrator thru some hardware interface like I2C, GPIO (Hardware Layer).
Tagged with: ,
Posted in Uncategorized

Eclipse: Android SDK update issue (ICS & JB)

Recently upgraded to latest Ice Cream Sandwich and Jelly Bean based Android SDK (20 & 12) and ADT Plug-in (20.0.0v). But after this upgrade, I am unable to create new Android Application projects from Eclipse. Eclipse is showing the following error and the “Finish” button is disabled.

Screenshot-1

“This template depends on the Android Support library, which is either not installed, or the template depends on a more recent version than the one you have installed”. To solve this, you have to goto the SDK_Folder/extra/android and rename the folder compatibility to support.

In some installation, SDK_Folder/extra/android/compatibility folder might be placed in SDK_Folder/android-compatibility. In this case, create a short-cut SDK_Folder/extra/android/support for SDK_Folder/android-compatibility

After that, restart the Eclipse.

One more issue:
In Linux, When I start the AVD(arm) with old emulator command ($ emulator @myarm-avd), I got this “Initializing hardware OpenGLES emulation support” error and unable to start AVD at all. But If I start the AVD(arm) with emulator-arm command ($ emulator-arm @myarm-avd), AVD(arm) starting with out any issues. So now, I renamed emulator to emulator.old and created a link to emulator-arm as emulator. Now no issues to ARM AVD. But to start AVD(x86), I am using the emulator-x86 command manually. This trick is only to start the emulator successfully and run in software rendering mode. But yet figure out, how to run the emulator with hardware rendering mode.

Posted in Uncategorized

OsciPrime: Open Source Android Oscilloscope

Today I saw this interesting OsciPrime Project, combination of Hardware and Software. Hardware is used to capture the signal from the test points and process it before feeding to Android thru USB interface. The below figure shows top-level block diagram.

Media_httpwwwosciprim_nlomh

Software is running on Android platform, will act as the front end, where user can view the signal shape and do the some measurement. Check-out the video:

Posted in Uncategorized