Multos Forums

   

Trouble with C-API instructions

Rank

Total Posts: 3

Joined 2014-04-11

PM

Hello,
I am writing an aplication for Multos MC1-36K-R2 card, and I have some troubles with C-API instructions from multos.h. First trouble is with instructions kind of multosTripleDESECBDecipher, multosBlockDecipherECB. They work perfect on card emulator hdb, but when I load program in to the card, the program will crush on such a instruction. However instructions multosDESECBEncipher or multosDESECBDecipher always works. The second trouble I have with multosBlockClear. In some cases I cant compile program. For example when I try use it like this multosBlockClear(255, samAppData.fileSys.data), where data is a global array: data[1024] in static memory, everything is ok. But when I try do this:  multosBlockClear(255, samAppData.fileSys.data+255) I have compilation error. How can I solve this troubles?

     
RankRankRank

Total Posts: 74

Joined 2012-02-21

PM

First trouble is with instructions kind of multosTripleDESECBDecipher, multosBlockDecipherECB.
They work perfect on card emulator hdb, but when I load program in to the card, the program will crush on such a instruction. However instructions multosDESECBEncipher or multosDESECBDecipher always works.

This is because the card you are using is quite old and does not support the newer primitives you are using in your application. You can find out which primitives your card supports by looking at the Implementation Reports section of the website. You will have to write your own functions to achieve the same result using the primitives that are available. For example

void encipher(BYTE *keyBYTE *dataBYTE numBlocks)
{
 BYTE i
,b;
 
BYTE ICV[8] {0,0,0,0,0,0,0,0};

 
// Triple DES CBC, two-key encipher using basic primitives
 
for(0numBlocksb++)
 
{
  
// XOR block with ICV -> ICV
  
for(i=0;i<8;i++)
   
ICV[i] ICV[i] data[(b*8)+i];

  
// Encrypt ICV -> block location
  
multosDESECBEncipher(ICV,ICV,key);
  
multosDESECBDecipher(ICV,ICV,key+8);
  
multosDESECBEncipher(ICV,data+(b*8),key);

  
// Update ICV
  
memcpy(ICV,data+(b*8),8);
 
}

For the other issue, can you please post your compilation error. Thanks.

     
Rank

Total Posts: 3

Joined 2014-04-11

PM

Thank You for response. Now I’m using function TripleDES2KeyECBEncipherMessageNoPad from DES.h instead multosTripleDESECBEncipher, and it works. I was hope, that there is posibility to run multosTripleDESECBEncipher because, as I think it would work faster.

About multosBlockClear issue, the compilation error is like this:

SamFile.c(17): error E8000: arguments to __code can only be locals, arguments, globals, or integer constants

but usage of the function is exactly like this:

int next;
next 255;
multosBlockClear(255samAppData.fileSys.data+next); 

I understand the error, but is there any way to use this primitive in similar situations? Because if I want to clear huge block in this way:

memset(samAppData.fileSys.data04096); 

it takes to much time.

     
RankRankRank

Total Posts: 74

Joined 2012-02-21

PM

The following gets around your compiler error (because all the offsets can be determined at compile time) and is MUCH faster than memset. It doesn’t look pretty in ‘C’ but results in reasonably compact (and fast) code. If you use it quite often, put it in a function of course.

multosBlockClear(255,block);
multosBlockClear(255,block+255);
multosBlockClear(255,block+(255*2));
multosBlockClear(255,block+(255*3));
//etc...
multosBlockClear(255,block+(255*15));
multosBlockClear(16,block+(255*16)); 
     
Rank

Total Posts: 3

Joined 2014-04-11

PM

Thank you for fast answer. It’s not a perfect solution, but I can use it in this case. But is there any other instruction to clear fast some block of data? Because I have a lot of instructions which looks like this:

memset(samAppData.modules[modIdx].dataMacIv08); 

where modules[] is an array of structures, and there is no possibility to know modIdx during compilation.

     
RankRankRank

Total Posts: 74

Joined 2012-02-21

PM

I’ve just seen that the cards you are using should support multosFillAdditionalStatic(). This is usually used to manipulate static memory outside of the normal address space, but it should work for any address in static memory. Try that.

A couple of points to remember:
1) any data in session memory (RAM) is automatically initialised to 0x00 after a reset.
2) data in static memory (EEPROM) is initialised to 0x00 by default when the application is first loaded (unless you have specifically set it to other values).

     
Rank

Total Posts: 3

Joined 2014-04-11

PM

Thank you very much. It works perfekt.