0

I am trying to prepare a simple GUI based AES-CMAC calculator.For this I have decided to create c dll out of open ssl libraries.[I Dont want to use .net for calculating AES-CMAC].This DLL ,I have tested with test application created in c++(console) and value generated are as per test vectors. But when I am trying to call this function from c#.I get wrong values.Here I am using byte[] instead of unsigned char*.

My code snippet for c function is

double calc_AES_CMAC(unsigned char* message ,unsigned char* key,unsigned char* cmac_16)
{

    size_t mactlen;

    CMAC_CTX *ctx = CMAC_CTX_new();
    CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL);
    CMAC_Update(ctx, message, sizeof(message));
    CMAC_Final(ctx, cmac_16, &mactlen);
    CMAC_CTX_free(ctx);

    return 0;
}

And my calling C# code is

Firstly Function import

[DllImport("C:\\Users\\Sudhanwa\\Documents\\Visual Studio 2010\\Projects\\Ccsharpdll\\Debug\\Ccsharpdll.dll", CallingConvention = CallingConvention.Cdecl)]

public static extern double calc_AES_CMAC(byte[] message, byte[] key, byte[] output);

Secondly Button click event

byte [] null_arr = new byte[16];
// K: 2b7e1516 28aed2a6 abf71588 09cf4f3c
byte[] key = { 0x2b,0x7e,0x15,0x16,
0x28,0xae,0xd2,0xa6,
0xab,0xf7,0x15,0x88,
0x09,0xcf,0x4f,0x3c };

// M: 6bc1bee2 2e409f96 e93d7e11 7393172a Mlen: 128
byte[] message= { 0x6b,0xc1,0xbe,0xe2,
0x2e,0x40,0x9f,0x96,
0xe9,0x3d,0x7e,0x11,
0x73,0x93,0x17,0x2a };

byte [] cmac = new byte [16];

c = calc_AES_CMAC(message, key, cmac);
string ans = ByteArrayToString(cmac);
MessageBox.Show(ans);

In this code, I get 16 Byte hex output but this does not match with correct result.

Dale K
  • 25,246
  • 15
  • 42
  • 71

1 Answers1

0

You need to indicate to the marshaller that you expect that data is returned (and how much data) in the output parameter:

public static extern double calc_AES_CMAC(byte[] message, byte[] key, 
    [In, Out, MarshalAs(UnmanagedType.LPArray, SizeConst=16)] byte[] output);

Otherwise a copy of the current content of the array will be passed to the C++ function but any modifications will not be copied back to the C# caller.

Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36
  • Tried this --public static extern double calc_AES_CMAC(byte[] message, byte[] key, [In, Out, MarshalAs(UnmanagedType.LPArray, SizeConst=16)] byte[] output); but still getting incorrct result – Sudhanwa Deo Feb 26 '19 at 07:17
  • Are you sure calc_AES_CMAC works correctly? See Antti comment. Best verify the function first with a test in C++ unmanaged code. – Klaus Gütter Feb 26 '19 at 07:35
  • Yes this function is verified and works with c++ driver application.I have reffered this link for doing so and works perfectly... https://stackoverflow.com/questions/28354844/how-to-calculate-aes-cmac-using-openssls-cmac-xxx-functions.Also you can find correct expected output in this post – Sudhanwa Deo Feb 26 '19 at 07:37
  • No, in the referenced code, message is an `unsigned char[]`, in your code it is `unsigned char*`. `sizeof(message)` will be very different in these cases. Please have a look at Antti's comment. – Klaus Gütter Feb 26 '19 at 08:06