?? csa.c
字號:
/***************************************************************************** * libcsa.c: CSA scrambler/descrambler ***************************************************************************** * Copyright (C) 2004-2005 Laurent Aimar * $Id: csa.c 13905 2006-01-12 23:10:04Z dionoea $ * * Authors: Laurent Aimar <fenrir@via.ecp.fr> * Jean-Paul Saman <jpsaman #_at_# m2x.nl> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************//* * XXX: A great part is just a copy/past of deCSA but I can't find the * author and the license. If there is a problem with it please e-mail me. */#include <stdlib.h>#include <vlc/vlc.h>#include "csa.h"struct csa_t{ /* odd and even keys */ uint8_t o_ck[8]; uint8_t e_ck[8]; uint8_t o_kk[57]; uint8_t e_kk[57]; /* cypher state */ int A[11]; int B[11]; int X, Y, Z; int D, E, F; int p, q, r;};static void csa_ComputeKey( uint8_t kk[57], uint8_t ck[8] );static void csa_StreamCypher( csa_t *c, int b_init, uint8_t *ck, uint8_t *sb, uint8_t *cb );static void csa_BlockDecypher( uint8_t kk[57], uint8_t ib[8], uint8_t bd[8] );static void csa_BlockCypher( uint8_t kk[57], uint8_t bd[8], uint8_t ib[8] );/***************************************************************************** * csa_New: *****************************************************************************/csa_t *csa_New(){ csa_t *c = malloc( sizeof( csa_t ) ); memset( c, 0, sizeof( csa_t ) ); return c;}/***************************************************************************** * csa_Delete: *****************************************************************************/void csa_Delete( csa_t *c ){ free( c );}/***************************************************************************** * csa_SetCW: *****************************************************************************/void csa_SetCW( csa_t *c, uint8_t o_ck[8], uint8_t e_ck[8] ){ memcpy( c->o_ck, o_ck, 8 ); csa_ComputeKey( c->o_kk, o_ck ); memcpy( c->e_ck, e_ck, 8 ); csa_ComputeKey( c->e_kk, e_ck );}/***************************************************************************** * csa_Decrypt: *****************************************************************************/void csa_Decrypt( csa_t *c, uint8_t *pkt, int i_pkt_size ){ uint8_t *ck; uint8_t *kk; uint8_t ib[8], stream[8], block[8]; int i_hdr, i_residue; int i, j, n; /* transport scrambling control */ if( (pkt[3]&0x80) == 0 ) { /* not scrambled */ return; } if( pkt[3]&0x40 ) { ck = c->o_ck; kk = c->o_kk; } else { ck = c->e_ck; kk = c->e_kk; } /* clear transport scrambling control */ pkt[3] &= 0x3f; i_hdr = 4; if( pkt[3]&0x20 ) { /* skip adaption field */ i_hdr += pkt[4] + 1; } if( 188 - i_hdr < 8 ) return; /* init csa state */ csa_StreamCypher( c, 1, ck, &pkt[i_hdr], ib ); /* */ n = (i_pkt_size - i_hdr) / 8; if( n < 0 ) return; i_residue = (i_pkt_size - i_hdr) % 8; for( i = 1; i < n + 1; i++ ) { csa_BlockDecypher( kk, ib, block ); if( i != n ) { csa_StreamCypher( c, 0, ck, NULL, stream ); for( j = 0; j < 8; j++ ) { /* xor ib with stream */ ib[j] = pkt[i_hdr+8*i+j] ^ stream[j]; } } else { /* last block */ for( j = 0; j < 8; j++ ) { ib[j] = 0; } } /* xor ib with block */ for( j = 0; j < 8; j++ ) { pkt[i_hdr+8*(i-1)+j] = ib[j] ^ block[j]; } } if( i_residue > 0 ) { csa_StreamCypher( c, 0, ck, NULL, stream ); for( j = 0; j < i_residue; j++ ) { pkt[i_pkt_size - i_residue + j] ^= stream[j]; } }}/***************************************************************************** * csa_Encrypt: *****************************************************************************/void csa_Encrypt( csa_t *c, uint8_t *pkt, int i_pkt_size, int b_odd ){ uint8_t *ck; uint8_t *kk; int i, j; int i_hdr = 4; /* hdr len */ uint8_t ib[184/8+2][8], stream[8], block[8]; int n, i_residue; /* set transport scrambling control */ pkt[3] |= 0x80; if( b_odd ) { pkt[3] |= 0x40; } if( b_odd ) { ck = c->o_ck; kk = c->o_kk; } else { ck = c->e_ck; kk = c->e_kk; } /* hdr len */ i_hdr = 4; if( pkt[3]&0x20 ) { /* skip adaption field */ i_hdr += pkt[4] + 1; } n = (i_pkt_size - i_hdr) / 8; i_residue = (i_pkt_size - i_hdr) % 8; if( n <= 0 ) { pkt[3] &= 0x3f; return; } /* */ for( i = 0; i < 8; i++ ) { ib[n+1][i] = 0; } for( i = n; i > 0; i-- ) { for( j = 0; j < 8; j++ ) { block[j] = pkt[i_hdr+8*(i-1)+j] ^ib[i+1][j]; } csa_BlockCypher( kk, block, ib[i] ); } /* init csa state */ csa_StreamCypher( c, 1, ck, ib[1], stream ); for( i = 0; i < 8; i++ ) { pkt[i_hdr+i] = ib[1][i]; } for( i = 2; i < n+1; i++ ) { csa_StreamCypher( c, 0, ck, NULL, stream ); for( j = 0; j < 8; j++ ) { pkt[i_hdr+8*(i-1)+j] = ib[i][j] ^ stream[j]; } } if( i_residue > 0 ) { csa_StreamCypher( c, 0, ck, NULL, stream ); for( j = 0; j < i_residue; j++ ) { pkt[i_pkt_size - i_residue + j] ^= stream[j]; } }}/***************************************************************************** * Divers *****************************************************************************/static const uint8_t key_perm[0x40] ={ 0x12,0x24,0x09,0x07,0x2A,0x31,0x1D,0x15,0x1C,0x36,0x3E,0x32,0x13,0x21,0x3B,0x40, 0x18,0x14,0x25,0x27,0x02,0x35,0x1B,0x01,0x22,0x04,0x0D,0x0E,0x39,0x28,0x1A,0x29, 0x33,0x23,0x34,0x0C,0x16,0x30,0x1E,0x3A,0x2D,0x1F,0x08,0x19,0x17,0x2F,0x3D,0x11, 0x3C,0x05,0x38,0x2B,0x0B,0x06,0x0A,0x2C,0x20,0x3F,0x2E,0x0F,0x03,0x26,0x10,0x37,};static void csa_ComputeKey( uint8_t kk[57], uint8_t ck[8] ){ int i,j,k; int bit[64]; int newbit[64]; int kb[9][8]; /* from a cw create 56 key bytes, here kk[1..56] */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -