4

Although recursive programming style isn't recommended in Java Card, I want to make a little test on the Fibonacci algorithm. I wrote a function that compute the Fibonacci's suite for big integers (represented by byte arrays).

My code is the following:

public static byte[] fibonacci(byte[] n) {
    if (isLEThan1(n)) {
        return n;
    }
    else {
        return add(fibonacci(subtract(n, new byte[]{0x01})),fibonacci(subtract(n,new byte[]{0x02})));
    }
}

where boolean isLEThan(byte[]) returns true if the integer represented by the byte array is less or equal than 1, false if not.

byte[] add(byte[], byte[]) and byte[] subtract(byte[], byte[]) implement addition and subtraction for big integers represented by byte arrays. They return a new byte array containing the result of the operation.

I thought that by giving a large array to the function described above, I will get an exception such as SystemException.NO_RESOURCE because of the number of array instantiated by subtract due to recursive calls.

But I have to think that I don't catch the right exception because I get 6F00 as status word.

Here's the list of exceptions that I consider:

try {
        fibonacci(array);
    } catch (ArithmeticException e) {
        ISOException.throwIt((short) 0x0100);
    } catch (ArrayStoreException e) {
        ISOException.throwIt((short) 0x0200);
    } catch (APDUException e) {
        ISOException.throwIt(Util.makeShort((byte) 0x03,
            (byte) e.getReason()));
    } catch (CryptoException e) {
        ISOException.throwIt(Util.makeShort((byte) 0x04,
            (byte) e.getReason()));
    } catch (ISOException e) {
        ISOException.throwIt(Util.makeShort((byte) 0x05,
            (byte) e.getReason()));
    } catch (PINException e) {
        ISOException.throwIt(Util.makeShort((byte) 0x06,
            (byte) e.getReason()));
    } catch (ServiceException e) {
        ISOException.throwIt(Util.makeShort((byte) 0x07,
            (byte) e.getReason()));
    } catch (SystemException e) {
        ISOException.throwIt(Util.makeShort((byte) 0x08,
            (byte) e.getReason()));
    } catch (TransactionException e) {
        ISOException.throwIt(Util.makeShort((byte) 0x09,
            (byte) e.getReason()));
    } catch (ClassCastException e) {
        ISOException.throwIt((short) 0x0A00);
    } catch (IndexOutOfBoundsException e) {
        ISOException.throwIt((short) 0x0B00);
    } catch (NegativeArraySizeException e) {
        ISOException.throwIt((short) 0x0C00);
    } catch (NullPointerException e) {
        ISOException.throwIt((short) 0x0D00);
    } catch (SecurityException e) {
        ISOException.throwIt((short) 0x0E00);
    }
    }

So, does somebody have an idea about the exception concerned in that case?

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
Raoul722
  • 1,222
  • 13
  • 30
  • 3
    Needless to say, if you perform any kind of recursive calls on a system that usually has to cope with 8 KiB of memory *in total*, you're doing something wrong. Creating an array as output is equally bad on Java Card. Reconsider what you are learning by doing this. Recursive programming should not be necessary here. – Maarten Bodewes Sep 07 '15 at 15:13

1 Answers1

5

It's a SystemException. However, ISO/IEC 7816-4 doesn't allow you to use just any status word. Instead use e.g. (short) (0x6700 | 0x0080 | e.getReason()) as reason for your ISOException.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • 3
    Always "fun" when your error reporting code errors out. I'm still laughing about the "Error: Error lookup table not found" that I got from any error in a DOS program :) – Maarten Bodewes Sep 07 '15 at 15:08