crypt: added simple cryptor [1/1]

PD#SWPL-115125

Problem:
no cryptor for pvr

Solution:
added simple cryptor demo

Verify:
PB

Change-Id: I1dd85cff3c7e19ee5b9ef914fc8e816ddf01e4c1
Signed-off-by: Zhiqiang Han <zhiqiang.han@amlogic.com>
diff --git a/src/am_crypt.c b/src/am_crypt.c
index eb56e6c..d85cf60 100644
--- a/src/am_crypt.c
+++ b/src/am_crypt.c
@@ -9,6 +9,60 @@
     Place your crypt logic in the TODO sections to complete the process
 */
 
+static uint8_t g_key[8];
+
+static int simple_crypt_ts_packet(uint8_t* dst, const uint8_t *src, int decrypt)
+{
+    int afc;
+    int afc_len = 0;
+    int crypt_len = 188;
+    const uint8_t *p_in = src;
+    uint8_t *p_out = dst;
+
+    afc = (p_in[3] >> 4) & 0x3;
+    if (afc == 0x0 || afc == 0x2) {
+        /* No payload */
+        return 0;
+    }
+
+    p_in += 4;
+    p_out += 4;
+    crypt_len -= 4;
+    if (afc == 0x3) {
+        /* Adaption field followed by payload */
+        afc_len = p_in[0];
+        p_in++;
+        p_out++;
+        crypt_len--;
+        p_in += afc_len;
+        p_out += afc_len;
+        crypt_len -= afc_len;
+        if (crypt_len < 0) {
+            printf("%s illegal adaption filed len %d\n", __func__, afc_len);
+            return -1;
+        }
+    }
+
+    crypt_len = (crypt_len & 0xfffffff8);
+    if (crypt_len < 8) {
+        printf("%s payload crypt eln too short!!!\n", __func__);
+        return -1;
+    }
+
+    int i;
+    int n_block = crypt_len / 8;
+    uint32_t *po, *pi, *pk = (uint32_t *)g_key;
+    for (i = 0; i < n_block; i++) {
+        po = (uint32_t *)(p_out + (i << 3));
+        pi = (uint32_t *)(p_in + (i << 3));
+
+        po[0] = pi[0] ^ pk[0];
+        po[1] = pi[1] ^ pk[1];
+    }
+
+    return 0;
+}
+
 
 typedef struct {
     void *des_cryptor;
@@ -16,7 +70,6 @@
     int cache_len;
 } am_cryptor_t;
 
-
 void *am_crypt_des_open(const uint8_t *key, const uint8_t *iv, int key_bits)
 {
     am_cryptor_t *cryptor = (am_cryptor_t *)malloc(sizeof(am_cryptor_t));
@@ -25,6 +78,9 @@
 
         {
             /*TODO:init your cryptor here*/
+
+            memset(g_key, 0, 8);
+            memcpy(g_key, key, key_bits/8);
         }
     }
     return cryptor;
@@ -82,6 +138,7 @@
 
         {
             /*TODO:process your crypt on the pkt*/
+            simple_crypt_ts_packet(p_out, p_cache, decrypt);
         }
 
         left -=  (188 - *p_cache_len);
@@ -104,6 +161,7 @@
             memcpy(p_out, p_in, 188);
             {
                 /*TODO:process your crypt on the pkt*/
+                simple_crypt_ts_packet(p_out, p_in, decrypt);
             }
 
             p_in += 188;