Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce the size of javacard applet

I wrote an applet which has 19 KB size on disk. It has three classes. The first one is extended from Applet, the second one has static functions and third one is a class that i create an instance from it in my applet.

I have three questions:

  1. Is there any way to find out how much size is taken by my applet instance in my javacard?
  2. Is there any tool to reduce the size of a javacard applet (.cap file)?
  3. Can you explain rules that help me to reduce my applet size?
like image 578
Mohsen Gorgani Avatar asked Dec 24 '22 18:12

Mohsen Gorgani


1 Answers

  1. Is there any way to find out how much size is taken by my applet instance in my javacard?
  • (AFAIK) There is no official way to do that (in GlobalPlatform / Java Card).

  • You can estimate the real memory usage from the difference in free memory before applet loading and after installation (and most likely after personalization -- as you probably will create some objects during the personalization). Some ways to find out free memory information are:

    • Using JCSystem.getAvailableMemory() (see here) which gives information for all memory types (if implemented).

    • Using Extended Card Resources Information tag retrievable with GET DATA (see TS 102 226) (if implemented).

    • Using proprietary command (ask you vendor).

  • You can have a look inside your .cap file and see the sizes of the parts that are loaded into the card -- this one is surely VERY INACCURATE as card OS is free to deal with the content at its own discretion.

  • I remember JCOP Tools have some special eclipse view which shows various statistics for the current applet -- might be informative as well.

  • The Reference Implementation offers an option to get some resource consumption statistics -- might be useful as well (I have never used this, though).

  1. Is there any tool to reduce the size of a javacard applet (.cap file)?
  • I used ProGuard in the past to improve applet performance (which in fact increased applet size as I used it mostly for method inlining) -- but it should work to reduce the applet size as well (e.g. eliminate dead code -- see shrinking options). There are many different optimizations as well -- just have a look, but do not expect miracles.
  1. Can you explain rules that help me to reduce my applet size?
  • I would emphasize good design and proper code re-use, but there are definitely many resources regarding generic optimization techniques -- I don't know any Java Card specific ones -- can't help here :(

  • If you have more applets loaded into a single card you might place common code into a shared library.


Some additional (random) notes:

  • It might be more practical to just get a card with a larger memory.

  • Free memory information given by the card might be inaccurate.

  • I wonder you have problems with your applet size as usually there are problems with transient memory size (AFAIK).

  • Your applet might be simply leaking memory and thus use more and more memory.

  • Do not sacrifice security for lesser applet size!

Good luck!

like image 198
vlp Avatar answered Feb 06 '23 13:02

vlp