34 #include <openssl/aes.h>
35 #include "mfrCrypto.h"
36 #include "safec_lib.h"
38 #define MFR_CRYPTO_KEY_LENGTH 16
39 #define MFR_CRYPTO_IVEC_LENGTH 16
43 static void mfr_create_key(
const mfrSerializedData_t * pSerialNumber,
unsigned char * keybuf,
unsigned char * ivec)
46 int i = 0, j = 0, k = 0, keylen = pSerialNumber->bufLen;
47 unsigned char normalizedkeybuf[MFR_CRYPTO_KEY_LENGTH] = {0};
48 unsigned char refkey[MFR_CRYPTO_KEY_LENGTH] = {0xD6, 0xC1, 0x5C, 0x1E, 0x1D, 0x64, 0x2D, 0x44,
49 0x68, 0x5C, 0xCE, 0xA0, 0x8D, 0x9F, 0x85, 0xCB};
50 unsigned char digkey[MFR_CRYPTO_KEY_LENGTH] = {0x2D, 0x6C, 0x1B, 0xFB, 0x7B, 0x86, 0x87, 0x35,
51 0xD0, 0x81, 0xD9, 0x6D, 0x25, 0x2B, 0xFB, 0xF1};
52 errno_t safec_rc = -1;
54 if(keylen > MFR_CRYPTO_KEY_LENGTH) keylen = MFR_CRYPTO_KEY_LENGTH;
55 if(keylen <= 0) keylen = 1;
56 safec_rc = memset_s(keybuf, MFR_CRYPTO_KEY_LENGTH, 0, MFR_CRYPTO_KEY_LENGTH);
59 for(i = 0, k = 0; i < MFR_CRYPTO_KEY_LENGTH; i+= keylen, k = !k)
61 for(j = 0; (j < keylen) && ((j+i) < MFR_CRYPTO_KEY_LENGTH); j++)
63 unsigned char ix = (
unsigned char)((j+7)%keylen);
64 unsigned char ir = (
unsigned char)((j+3)%MFR_CRYPTO_KEY_LENGTH);
65 unsigned char cSN = (pSerialNumber->buf[ix]);
67 if(0==cSN) cSN = refkey[ir];
71 normalizedkeybuf[i+j] = (cSN^0xFF);
75 normalizedkeybuf[i+j] = (cSN);
83 unsigned char digestkeybuf[MFR_CRYPTO_KEY_LENGTH] = {0};
84 unsigned char iv[AES_BLOCK_SIZE] = {0};
86 safec_rc = memcpy_s(digestkeybuf, keylen, pSerialNumber->buf, keylen);
92 for(i = 0; i < MFR_CRYPTO_KEY_LENGTH; i+= 1)
94 digestkeybuf[i] += (
unsigned char)(digkey[i]+refkey[i]);
98 AES_set_encrypt_key(digestkeybuf, 128, &ctx);
100 AES_cbc_encrypt(normalizedkeybuf, keybuf, AES_BLOCK_SIZE, &ctx, iv, AES_ENCRYPT);
102 AES_cbc_encrypt(keybuf, ivec, AES_BLOCK_SIZE, &ctx, iv, AES_ENCRYPT);
108 errno_t safec_rc = -1;
110 if((NULL == pSerialNumber) || (NULL == pSerialNumber->buf) || (pSerialNumber->bufLen<= 0))
112 return mfrERR_INVALID_PARAM;
118 return mfrERR_INVALID_PARAM;
124 mfrError_t mfrCrypto_term( )
134 unsigned char keybuf[MFR_CRYPTO_KEY_LENGTH];
135 unsigned char iv[AES_BLOCK_SIZE];
136 int nBytes = pPlainText->bufLen;
137 unsigned char * pbuf = (
unsigned char *) pPlainText->buf;
138 unsigned char * cbuf = NULL;
139 errno_t safec_rc = -1;
141 pCipherText->buf = NULL;
144 if(NULL == pbuf)
return mfrERR_INVALID_PARAM;
147 if(0 != (nBytes%AES_BLOCK_SIZE))
150 nBytes = ((nBytes/AES_BLOCK_SIZE)+1)*AES_BLOCK_SIZE;
151 pbuf = (
unsigned char *)malloc(nBytes);
152 if(NULL == pbuf)
return mfrERR_GENERAL;
153 safec_rc = memset_s(pbuf, nBytes, 0, nBytes);
156 safec_rc = memcpy_s(pbuf, nBytes, pPlainText->buf, pPlainText->bufLen);
162 return mfrERR_GENERAL;
167 pCipherText->buf = (
char *)malloc(nBytes);
168 cbuf = (
unsigned char *) pCipherText->buf;
174 if( pbuf != (
unsigned char *) pPlainText->buf ) free(pbuf);
175 return mfrERR_GENERAL;
179 if(0 != (nBytes%AES_BLOCK_SIZE))
181 return mfrERR_INVALID_PARAM;
185 mfr_create_key(&serialNumber, keybuf, iv);
186 AES_set_encrypt_key(keybuf, 128, &ctx);
189 while (count < nBytes)
192 AES_cbc_encrypt(pbuf+count, cbuf+count, AES_BLOCK_SIZE, &ctx, iv, AES_ENCRYPT);
193 count += AES_BLOCK_SIZE;
197 if(pbuf != (
unsigned char *) pPlainText->buf)
202 pCipherText->bufLen = nBytes;
212 unsigned char keybuf[MFR_CRYPTO_KEY_LENGTH];
214 unsigned char iv[AES_BLOCK_SIZE];
215 int nBytes = pCipherText->bufLen;
216 unsigned char * cbuf = (
unsigned char *) pCipherText->buf;
217 unsigned char * pbuf = NULL;
218 errno_t safec_rc = -1;
220 pPlainText->buf = NULL;
223 if(NULL == cbuf)
return mfrERR_INVALID_PARAM;
226 if(0 != (nBytes%AES_BLOCK_SIZE))
229 nBytes = ((nBytes/AES_BLOCK_SIZE)+1)*AES_BLOCK_SIZE;
230 cbuf = (
unsigned char *)malloc(nBytes);
231 if(NULL == cbuf)
return mfrERR_GENERAL;
232 safec_rc = memset_s(cbuf, nBytes, 0, nBytes);
235 safec_rc = memcpy_s(cbuf, nBytes, pCipherText->buf, pCipherText->bufLen);
241 return mfrERR_GENERAL;
246 pPlainText->buf = (
char *)malloc(nBytes);
247 pbuf = (
unsigned char*) pPlainText->buf;
253 if( cbuf != (
unsigned char*) pCipherText->buf ) free( cbuf );
254 return mfrERR_GENERAL;
258 if(0 != (nBytes%AES_BLOCK_SIZE))
260 return mfrERR_INVALID_PARAM;
264 mfr_create_key(&serialNumber, keybuf, iv);
265 AES_set_decrypt_key(keybuf, 128, &ctx);
268 while (count < nBytes)
271 AES_cbc_encrypt(cbuf+count, pbuf+count, AES_BLOCK_SIZE, &ctx, iv, AES_DECRYPT);
272 count += AES_BLOCK_SIZE;
276 if(cbuf != (
unsigned char*) pCipherText->buf)
281 pPlainText->bufLen = nBytes;