From 689d0cc0f40c3a03d2e4dacf1f0d891da329a9c9 Mon Sep 17 00:00:00 2001 From: diego Date: Fri, 3 Nov 2006 12:25:56 +0000 Subject: Move libdvdcss into its own subdirectory. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@20629 b3059339-0415-0410-9bf9-f77b7e298cf2 --- libdvdcss/Makefile | 63 ++ libdvdcss/bsdi_dvd.h | 344 ++++++++ libdvdcss/bsdi_ioctl.c | 770 ++++++++++++++++ libdvdcss/common.h | 84 ++ libdvdcss/css.c | 1695 ++++++++++++++++++++++++++++++++++++ libdvdcss/css.h | 56 ++ libdvdcss/csstables.h | 392 +++++++++ libdvdcss/device.c | 973 +++++++++++++++++++++ libdvdcss/device.h | 58 ++ libdvdcss/dvdcss/dvdcss.h | 105 +++ libdvdcss/error.c | 68 ++ libdvdcss/ioctl.c | 2123 +++++++++++++++++++++++++++++++++++++++++++++ libdvdcss/ioctl.h | 429 +++++++++ libdvdcss/libdvdcss.c | 797 +++++++++++++++++ libdvdcss/libdvdcss.h | 104 +++ 15 files changed, 8061 insertions(+) create mode 100644 libdvdcss/Makefile create mode 100644 libdvdcss/bsdi_dvd.h create mode 100644 libdvdcss/bsdi_ioctl.c create mode 100644 libdvdcss/common.h create mode 100644 libdvdcss/css.c create mode 100644 libdvdcss/css.h create mode 100644 libdvdcss/csstables.h create mode 100644 libdvdcss/device.c create mode 100644 libdvdcss/device.h create mode 100644 libdvdcss/dvdcss/dvdcss.h create mode 100644 libdvdcss/error.c create mode 100644 libdvdcss/ioctl.c create mode 100644 libdvdcss/ioctl.h create mode 100644 libdvdcss/libdvdcss.c create mode 100644 libdvdcss/libdvdcss.h (limited to 'libdvdcss') diff --git a/libdvdcss/Makefile b/libdvdcss/Makefile new file mode 100644 index 0000000000..6e0d3ee0bf --- /dev/null +++ b/libdvdcss/Makefile @@ -0,0 +1,63 @@ + +ifneq ($(wildcard ../config.mak),) +include ../config.mak +endif + +SRCS = css.c \ + device.c \ + error.c \ + ioctl.c \ + libdvdcss.c \ + #bsdi_ioctl \ + +OBJS = $(SRCS:.c=.o) + +LIB=libdvdcss + +.SUFFIXES: .c .o + +ifeq ($(OPTFLAGS),) +OPTFLAGS = -mcpu=pentium -march=pentium -O4 +endif + +# \ -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 + +# -funroll-loops removed, triggered gcc 3.0.4 (3.x?) bug +CFLAGS= -I.. $(OPTFLAGS) -D__USE_UNIX98 -D_GNU_SOURCE \ + -DHAVE_LIMITS_H -DHAVE_ERRNO_H -DHAVE_INTTYPES_H -DHAVE_UNISTD_H \ + -DVERSION=\"1.2.9\" + +ifeq ($(TARGET_OS),CYGWIN) +CFLAGS+=-DSYS_CYGWIN -DWIN32 +endif + +ifeq ($(TARGET_OS),Darwin) +CFLAGS+=-D__DARWIN__ +endif + +.c.o: + $(CC) $(CFLAGS) -c -o $@ $< + +all: $(LIB).a + +$(LIB).a: $(OBJS) + $(AR) rc $(LIB).a $(OBJS) + $(RANLIB) $(LIB).a + +clean: + rm -f *.o *.a *~ *.so + +distclean: clean + rm -f .depend + +dep: depend + +depend: + $(CC) -MM $(CFLAGS) $(SRCS) 1>.depend + +# +# include dependency files if they exist +# +ifneq ($(wildcard .depend),) +include .depend +endif diff --git a/libdvdcss/bsdi_dvd.h b/libdvdcss/bsdi_dvd.h new file mode 100644 index 0000000000..850504a064 --- /dev/null +++ b/libdvdcss/bsdi_dvd.h @@ -0,0 +1,344 @@ +/* + * $Id$ +*/ + +#ifndef _DVD_H_ +#define _DVD_H_ + +#include +#include +#include + +__BEGIN_DECLS +int dvd_cdrom_ioctl(int, unsigned long, void *); +int cdrom_blocksize(int, int); +void dvd_cdrom_debug(int); +__END_DECLS + +#define ioctl(a,b,c) dvd_cdrom_ioctl((a),(b),(c)) + +typedef unsigned char __u8; +typedef unsigned short __u16; +typedef unsigned int __u32; + +#define DVD_READ_STRUCT 0x5390 /* Read structure */ +#define DVD_WRITE_STRUCT 0x5391 /* Write structure */ +#define DVD_AUTH 0x5392 /* Authentication */ + +#define DVD_STRUCT_PHYSICAL 0x00 +#define DVD_STRUCT_COPYRIGHT 0x01 +#define DVD_STRUCT_DISCKEY 0x02 +#define DVD_STRUCT_BCA 0x03 +#define DVD_STRUCT_MANUFACT 0x04 + +struct dvd_layer { + __u8 book_version : 4; + __u8 book_type : 4; + __u8 min_rate : 4; + __u8 disc_size : 4; + __u8 layer_type : 4; + __u8 track_path : 1; + __u8 nlayers : 2; + __u8 track_density : 4; + __u8 linear_density : 4; + __u8 bca : 1; + __u32 start_sector; + __u32 end_sector; + __u32 end_sector_l0; +}; + +struct dvd_physical { + __u8 type; + __u8 layer_num; + struct dvd_layer layer[4]; +}; + +struct dvd_copyright { + __u8 type; + + __u8 layer_num; + __u8 cpst; + __u8 rmi; +}; + +struct dvd_disckey { + __u8 type; + + unsigned agid : 2; + __u8 value[2048]; +}; + +struct dvd_bca { + __u8 type; + + int len; + __u8 value[188]; +}; + +struct dvd_manufact { + __u8 type; + + __u8 layer_num; + int len; + __u8 value[2048]; +}; + +typedef union { + __u8 type; + + struct dvd_physical physical; + struct dvd_copyright copyright; + struct dvd_disckey disckey; + struct dvd_bca bca; + struct dvd_manufact manufact; +} dvd_struct; + +/* + * DVD authentication ioctl + */ + +/* Authentication states */ +#define DVD_LU_SEND_AGID 0 +#define DVD_HOST_SEND_CHALLENGE 1 +#define DVD_LU_SEND_KEY1 2 +#define DVD_LU_SEND_CHALLENGE 3 +#define DVD_HOST_SEND_KEY2 4 + +/* Termination states */ +#define DVD_AUTH_ESTABLISHED 5 +#define DVD_AUTH_FAILURE 6 + +/* Other functions */ +#define DVD_LU_SEND_TITLE_KEY 7 +#define DVD_LU_SEND_ASF 8 +#define DVD_INVALIDATE_AGID 9 +#define DVD_LU_SEND_RPC_STATE 10 +#define DVD_HOST_SEND_RPC_STATE 11 + +/* State data */ +typedef __u8 dvd_key[5]; /* 40-bit value, MSB is first elem. */ +typedef __u8 dvd_challenge[10]; /* 80-bit value, MSB is first elem. */ + +struct dvd_lu_send_agid { + __u8 type; + unsigned agid : 2; +}; + +struct dvd_host_send_challenge { + __u8 type; + unsigned agid : 2; + + dvd_challenge chal; +}; + +struct dvd_send_key { + __u8 type; + unsigned agid : 2; + + dvd_key key; +}; + +struct dvd_lu_send_challenge { + __u8 type; + unsigned agid : 2; + + dvd_challenge chal; +}; + +#define DVD_CPM_NO_COPYRIGHT 0 +#define DVD_CPM_COPYRIGHTED 1 + +#define DVD_CP_SEC_NONE 0 +#define DVD_CP_SEC_EXIST 1 + +#define DVD_CGMS_UNRESTRICTED 0 +#define DVD_CGMS_SINGLE 2 +#define DVD_CGMS_RESTRICTED 3 + +struct dvd_lu_send_title_key { + __u8 type; + unsigned agid : 2; + + dvd_key title_key; + int lba; + unsigned cpm : 1; + unsigned cp_sec : 1; + unsigned cgms : 2; +}; + +struct dvd_lu_send_asf { + __u8 type; + unsigned agid : 2; + + unsigned asf : 1; +}; + +struct dvd_host_send_rpcstate { + __u8 type; + __u8 pdrc; +}; + +struct dvd_lu_send_rpcstate { + __u8 type : 2; + __u8 vra : 3; + __u8 ucca : 3; + __u8 region_mask; + __u8 rpc_scheme; +}; + +typedef union { + __u8 type; + + struct dvd_lu_send_agid lsa; + struct dvd_host_send_challenge hsc; + struct dvd_send_key lsk; + struct dvd_lu_send_challenge lsc; + struct dvd_send_key hsk; + struct dvd_lu_send_title_key lstk; + struct dvd_lu_send_asf lsasf; + struct dvd_host_send_rpcstate hrpcs; + struct dvd_lu_send_rpcstate lrpcs; +} dvd_authinfo; + + +typedef struct { + __u16 report_key_length; + __u8 reserved1; + __u8 reserved2; +#if BYTE_ORDER == BIG_ENDIAN + __u8 type_code : 2; + __u8 vra : 3; + __u8 ucca : 3; +#elif BYTE_ORDER == LITTLE_ENDIAN + __u8 ucca : 3; + __u8 vra : 3; + __u8 type_code : 2; +#endif + __u8 region_mask; + __u8 rpc_scheme; + __u8 reserved3; +} rpc_state_t; + +/* + * Stuff for the CDROM ioctls +*/ + +#define CDROMREADTOCHDR 0x5305 /* Read TOC header (cdrom_tochdr) */ +#define CDROMREADTOCENTRY 0x5306 /* Read TOC entry (cdrom_tocentry) */ +#define CDROMEJECT 0x5309 /* Ejects the cdrom media */ +#define CDROMCLOSETRAY 0x5319 /* Reverse of CDROMEJECT */ +#define CDROM_DRIVE_STATUS 0x5326 /* Get tray position, etc. */ +#define CDROM_DISC_STATUS 0x5327 /* Get disc type, etc. */ +#define CDROMREADMODE2 0x530c /* Read CDROM mode 2 data (2336 Bytes) */ +#define CDROMREADMODE1 0x530d /* Read CDROM mode 1 data (2048 Bytes) */ +#define CDROMREADRAW 0x5314 /* read data in raw mode (2352 bytes) */ + +#define CD_MINS 74 /* max. minutes per CD, not really a limit */ +#define CD_SECS 60 /* seconds per minute */ +#define CD_FRAMES 75 /* frames per second */ +#define CD_MSF_OFFSET 150 /* MSF numbering offset of first frame */ + +#define CD_HEAD_SIZE 4 /* header (address) bytes per raw data frame */ +#define CD_SYNC_SIZE 12 /* 12 sync bytes per raw data frame */ +#define CD_FRAMESIZE 2048 /* bytes per frame, "cooked" mode */ +#define CD_FRAMESIZE_RAW 2352 /* bytes per frame, "raw" mode */ +#define CD_FRAMESIZE_RAW0 (CD_FRAMESIZE_RAW-CD_SYNC_SIZE-CD_HEAD_SIZE) /*2336*/ +#define CD_FRAMESIZE_RAW1 (CD_FRAMESIZE_RAW-CD_SYNC_SIZE) /*2340*/ + +/* CD-ROM address types (cdrom_tocentry.cdte_format) */ +#define CDROM_LBA 0x01 /* logical block: first frame is #0 */ +#define CDROM_MSF 0x02 /* minute-second-frame: binary. not bcd here!*/ + +/* bit to tell whether track is data or audio (cdrom_tocentry.cdte_ctrl) */ +#define CDROM_DATA_TRACK 0x04 + +/* The leadout track is always 0xAA, regardless of # of tracks on disc */ +#define CDROM_LEADOUT 0xAA + +/* drive status returned by CDROM_DRIVE_STATUS ioctl */ +#define CDS_NO_INFO 0 /* if not implemented */ +#define CDS_NO_DISC 1 +#define CDS_TRAY_OPEN 2 +#define CDS_DRIVE_NOT_READY 3 +#define CDS_DISC_OK 4 + +/* + * Return values for CDROM_DISC_STATUS ioctl. + * Can also return CDS_NO_INFO and CDS_NO_DISC from above +*/ +#define CDS_AUDIO 100 +#define CDS_DATA_1 101 +#define CDS_DATA_2 102 +#define CDS_XA_2_1 103 +#define CDS_XA_2_2 104 +#define CDS_MIXED 105 + +/* For compile compatibility only - we don't support changers */ +#define CDSL_NONE ((int) (~0U>>1)-1) +#define CDSL_CURRENT ((int) (~0U>>1)) + +struct cdrom_msf +{ + __u8 cdmsf_min0; /* start minute */ + __u8 cdmsf_sec0; /* start second */ + __u8 cdmsf_frame0; /* start frame */ + __u8 cdmsf_min1; /* end minute */ + __u8 cdmsf_sec1; /* end second */ + __u8 cdmsf_frame1; /* end frame */ +}; + +struct cdrom_tochdr + { + __u8 cdth_trk0; /* start track */ + __u8 cdth_trk1; /* end track */ + }; + +struct cdrom_msf0 +{ + __u8 minute; + __u8 second; + __u8 frame; +}; + +union cdrom_addr +{ + struct cdrom_msf0 msf; + int lba; +}; + +struct cdrom_tocentry +{ + __u8 cdte_track; + __u8 cdte_adr :4; + __u8 cdte_ctrl :4; + __u8 cdte_format; + union cdrom_addr cdte_addr; + __u8 cdte_datamode; +}; + +struct modesel_head +{ + __u8 reserved1; + __u8 medium; + __u8 reserved2; + __u8 block_desc_length; + __u8 density; + __u8 number_of_blocks_hi; + __u8 number_of_blocks_med; + __u8 number_of_blocks_lo; + __u8 reserved3; + __u8 block_length_hi; + __u8 block_length_med; + __u8 block_length_lo; +}; + +typedef struct +{ + int data; + int audio; + int cdi; + int xa; + int error; +} tracktype; + +#endif /* _DVD_H_ */ diff --git a/libdvdcss/bsdi_ioctl.c b/libdvdcss/bsdi_ioctl.c new file mode 100644 index 0000000000..ef4c56c30e --- /dev/null +++ b/libdvdcss/bsdi_ioctl.c @@ -0,0 +1,770 @@ +#include "config.h" + +/* + * Hacked version of the linux cdrom.c kernel module - everything except the + * DVD handling ripped out and the rest rewritten to use raw SCSI commands + * on BSD/OS 4.2 (but should work with earlier versions as well). +*/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "bsdi_dvd.h" + +/* + * Now get rid of the override/intercept macro so we can call the real ioctl() + * routine! +*/ +#undef ioctl + +#define CMD_READ_10 0x28 +#define CMD_READ_TOC_PMA_ATIP 0x43 +#define CMD_READ_CD 0xbe +#define CMD_START_STOP_UNIT 0x1b + +#define CMD_SEND_KEY 0xa3 +#define CMD_REPORT_KEY 0xa4 +#define CMD_READ_DVD_STRUCTURE 0xad + +#define copy_key(dest,src) memcpy((dest), (src), sizeof(dvd_key)) +#define copy_chal(dest,src) memcpy((dest), (src), sizeof(dvd_challenge)) + +/* Define the Cdrom Generic Command structure */ +typedef struct cgc + { + u_char cdb[12]; + u_char *buf; + int buflen; + int rw; + int timeout; + scsi_user_sense_t *sus; + } cgc_t; + +static int scsi_cmd(int, cgc_t *); +static int cdrom_ioctl(int, u_long, void *); +static int cdrom_tray_move(int, int); +static void cdrom_count_tracks(int, tracktype *); +static int dvd_ioctl(int, u_long, void *); +static int debug = 0; + +void dvd_cdrom_debug(int flag) + { + debug = flag; + } + +/* + * This is the published entry point. Actually applications should simply + * include and not refer to this at all. +*/ +int dvd_cdrom_ioctl(int fd, unsigned long cmd, void *arg) + { + switch (cmd) + { + case DVD_AUTH: + case DVD_READ_STRUCT: + return(dvd_ioctl(fd, cmd, arg)); + case CDROMREADTOCHDR: + case CDROMREADTOCENTRY: + case CDROMEJECT: + case CDROMREADRAW: + case CDROMREADMODE1: + case CDROMREADMODE2: + case CDROMCLOSETRAY: + case CDROM_DRIVE_STATUS: + case CDROM_DISC_STATUS: + return(cdrom_ioctl(fd, cmd, arg)); + default: + return(ioctl(fd, cmd, arg)); + } + } + +static void setup_report_key(cgc_t *cgc, u_int agid, u_int type) + { + + cgc->cdb[0] = CMD_REPORT_KEY; + cgc->cdb[10] = type | (agid << 6); + switch (type) + { + case 0: + case 5: + case 8: + cgc->buflen = 8; + break; + case 1: + cgc->buflen = 16; + break; + case 2: + case 4: + cgc->buflen = 12; + break; + } + cgc->cdb[9] = cgc->buflen; + cgc->rw = SUC_READ;; + } + +static void setup_send_key(cgc_t *cgc, u_int agid, u_int type) + { + + cgc->cdb[0] = CMD_SEND_KEY; + cgc->cdb[10] = type | (agid << 6); + switch (type) + { + case 1: + cgc->buflen = 16; + break; + case 3: + cgc->buflen = 12; + break; + case 6: + cgc->buflen = 8; + break; + } + cgc->cdb[9] = cgc->buflen; + cgc->rw = SUC_WRITE; + } + +static void cgc_init(cgc_t *cgc, void *buf, int len, int type) + { + + memset(cgc, 0, sizeof (*cgc)); + if (buf) + memset(buf, 0, len); + cgc->buf = (u_char *)buf; + cgc->buflen = len; + cgc->rw = type; + cgc->timeout = 5; /* 5 second timeout */ + } + +static int dvd_do_auth(int fd, dvd_authinfo *ai) + { + int ret; + u_char buf[20]; + cgc_t cgc; + rpc_state_t rpc_state; + + memset(buf, 0, sizeof(buf)); + cgc_init(&cgc, buf, 0, SUC_READ); + + switch (ai->type) + { + case DVD_LU_SEND_AGID: /* LU data send */ + setup_report_key(&cgc, ai->lsa.agid, 0); + if (ret = scsi_cmd(fd, &cgc)) + return ret; + ai->lsa.agid = buf[7] >> 6; + break; + case DVD_LU_SEND_KEY1: + setup_report_key(&cgc, ai->lsk.agid, 2); + if (ret = scsi_cmd(fd, &cgc)) + return ret; + copy_key(ai->lsk.key, &buf[4]); + break; + case DVD_LU_SEND_CHALLENGE: + setup_report_key(&cgc, ai->lsc.agid, 1); + if (ret = scsi_cmd(fd, &cgc)) + return ret; + copy_chal(ai->lsc.chal, &buf[4]); + break; + case DVD_LU_SEND_TITLE_KEY: /* Post-auth key */ + setup_report_key(&cgc, ai->lstk.agid, 4); + cgc.cdb[5] = ai->lstk.lba; + cgc.cdb[4] = ai->lstk.lba >> 8; + cgc.cdb[3] = ai->lstk.lba >> 16; + cgc.cdb[2] = ai->lstk.lba >> 24; + if (ret = scsi_cmd(fd, &cgc)) + return ret; + ai->lstk.cpm = (buf[4] >> 7) & 1; + ai->lstk.cp_sec = (buf[4] >> 6) & 1; + ai->lstk.cgms = (buf[4] >> 4) & 3; + copy_key(ai->lstk.title_key, &buf[5]); + break; + case DVD_LU_SEND_ASF: + setup_report_key(&cgc, ai->lsasf.agid, 5); + if (ret = scsi_cmd(fd, &cgc)) + return ret; + ai->lsasf.asf = buf[7] & 1; + break; + case DVD_HOST_SEND_CHALLENGE: /* LU data receive (LU changes state) */ + setup_send_key(&cgc, ai->hsc.agid, 1); + buf[1] = 0xe; + copy_chal(&buf[4], ai->hsc.chal); + if (ret = scsi_cmd(fd, &cgc)) + return ret; + ai->type = DVD_LU_SEND_KEY1; + break; + case DVD_HOST_SEND_KEY2: + setup_send_key(&cgc, ai->hsk.agid, 3); + buf[1] = 0xa; + copy_key(&buf[4], ai->hsk.key); + if (ret = scsi_cmd(fd, &cgc)) + { + ai->type = DVD_AUTH_FAILURE; + return ret; + } + ai->type = DVD_AUTH_ESTABLISHED; + break; + case DVD_INVALIDATE_AGID: + setup_report_key(&cgc, ai->lsa.agid, 0x3f); + if (ret = scsi_cmd(fd, &cgc)) + return ret; + break; + case DVD_LU_SEND_RPC_STATE: /* Get region settings */ + setup_report_key(&cgc, 0, 8); + memset(&rpc_state, 0, sizeof(rpc_state_t)); + cgc.buf = (char *) &rpc_state; + if (ret = scsi_cmd(fd, &cgc)) + { + ai->lrpcs.type = 0; + ai->lrpcs.rpc_scheme = 0; + } + else + { + ai->lrpcs.type = rpc_state.type_code; + ai->lrpcs.vra = rpc_state.vra; + ai->lrpcs.ucca = rpc_state.ucca; + ai->lrpcs.region_mask = rpc_state.region_mask; + ai->lrpcs.rpc_scheme = rpc_state.rpc_scheme; + } + break; + case DVD_HOST_SEND_RPC_STATE: /* Set region settings */ + setup_send_key(&cgc, 0, 6); + buf[1] = 6; + buf[4] = ai->hrpcs.pdrc; + if (ret = scsi_cmd(fd, &cgc)) + return ret; + break; + default: + return EINVAL; + } + return 0; + } + +static int dvd_read_physical(int fd, dvd_struct *s) + { + int ret, i; + u_char buf[4 + 4 * 20], *base; + struct dvd_layer *layer; + cgc_t cgc; + + cgc_init(&cgc, buf, sizeof(buf), SUC_READ); + cgc.cdb[0] = CMD_READ_DVD_STRUCTURE; + cgc.cdb[6] = s->physical.layer_num; + cgc.cdb[7] = s->type; + cgc.cdb[9] = cgc.buflen & 0xff; + + if (ret = scsi_cmd(fd, &cgc)) + return ret; + + base = &buf[4]; + layer = &s->physical.layer[0]; + + /* place the data... really ugly, but at least we won't have to + worry about endianess in userspace or here. */ + for (i = 0; i < 4; ++i, base += 20, ++layer) + { + memset(layer, 0, sizeof(*layer)); + layer->book_version = base[0] & 0xf; + layer->book_type = base[0] >> 4; + layer->min_rate = base[1] & 0xf; + layer->disc_size = base[1] >> 4; + layer->layer_type = base[2] & 0xf; + layer->track_path = (base[2] >> 4) & 1; + layer->nlayers = (base[2] >> 5) & 3; + layer->track_density = base[3] & 0xf; + layer->linear_density = base[3] >> 4; + layer->start_sector = base[5] << 16 | base[6] << 8 | base[7]; + layer->end_sector = base[9] << 16 | base[10] << 8 | base[11]; + layer->end_sector_l0 = base[13] << 16 | base[14] << 8 | base[15]; + layer->bca = base[16] >> 7; + } + return 0; + } + +static int dvd_read_copyright(int fd, dvd_struct *s) + { + int ret; + u_char buf[8]; + cgc_t cgc; + + cgc_init(&cgc, buf, sizeof(buf), SUC_READ); + cgc.cdb[0] = CMD_READ_DVD_STRUCTURE; + cgc.cdb[6] = s->copyright.layer_num; + cgc.cdb[7] = s->type; + cgc.cdb[8] = cgc.buflen >> 8; + cgc.cdb[9] = cgc.buflen & 0xff; + + if (ret = scsi_cmd(fd, &cgc)) + return ret; + s->copyright.cpst = buf[4]; + s->copyright.rmi = buf[5]; + return 0; + } + +static int dvd_read_disckey(int fd, dvd_struct *s) + { + int ret, size; + u_char *buf; + cgc_t cgc; + + size = sizeof(s->disckey.value) + 4; + + if ((buf = (u_char *) malloc(size)) == NULL) + return ENOMEM; + + cgc_init(&cgc, buf, size, SUC_READ); + cgc.cdb[0] = CMD_READ_DVD_STRUCTURE; + cgc.cdb[7] = s->type; + cgc.cdb[8] = size >> 8; + cgc.cdb[9] = size & 0xff; + cgc.cdb[10] = s->disckey.agid << 6; + + if (!(ret = scsi_cmd(fd, &cgc))) + memcpy(s->disckey.value, &buf[4], sizeof(s->disckey.value)); + free(buf); + return ret; + } + +static int dvd_read_bca(int fd, dvd_struct *s) + { + int ret; + u_char buf[4 + 188]; + cgc_t cgc; + + cgc_init(&cgc, buf, sizeof(buf), SUC_READ); + cgc.cdb[0] = CMD_READ_DVD_STRUCTURE; + cgc.cdb[7] = s->type; + cgc.cdb[9] = cgc.buflen = 0xff; + + if (ret = scsi_cmd(fd, &cgc)) + return ret; + s->bca.len = buf[0] << 8 | buf[1]; + if (s->bca.len < 12 || s->bca.len > 188) + return EIO; + memcpy(s->bca.value, &buf[4], s->bca.len); + return 0; + } + +static int dvd_read_manufact(int fd, dvd_struct *s) + { + int ret = 0, size; + u_char *buf; + cgc_t cgc; + + size = sizeof(s->manufact.value) + 4; + + if ((buf = (u_char *) malloc(size)) == NULL) + return ENOMEM; + + cgc_init(&cgc, buf, size, SUC_READ); + cgc.cdb[0] = CMD_READ_DVD_STRUCTURE; + cgc.cdb[7] = s->type; + cgc.cdb[8] = size >> 8; + cgc.cdb[9] = size & 0xff; + + if (ret = scsi_cmd(fd, &cgc)) + { + free(buf); + return ret; + } + s->manufact.len = buf[0] << 8 | buf[1]; + if (s->manufact.len < 0 || s->manufact.len > 2048) + ret = -EIO; + else + memcpy(s->manufact.value, &buf[4], s->manufact.len); + free(buf); + return ret; + } + +static int dvd_read_struct(int fd, dvd_struct *s) + { + switch (s->type) + { + case DVD_STRUCT_PHYSICAL: + return dvd_read_physical(fd, s); + case DVD_STRUCT_COPYRIGHT: + return dvd_read_copyright(fd, s); + case DVD_STRUCT_DISCKEY: + return dvd_read_disckey(fd, s); + case DVD_STRUCT_BCA: + return dvd_read_bca(fd, s); + case DVD_STRUCT_MANUFACT: + return dvd_read_manufact(fd, s); + default: + return EINVAL; + } + } + +static u_char scsi_cdblen[8] = {6, 10, 10, 12, 12, 12, 10, 10}; + +static int scsi_cmd(int fd, cgc_t *cgc) + { + int i, scsistatus, cdblen; + unsigned char *cp; + struct scsi_user_cdb suc; + + /* safety checks */ + if (cgc->rw != SUC_READ && cgc->rw != SUC_WRITE) + return(EINVAL); + + suc.suc_flags = cgc->rw; + cdblen = scsi_cdblen[(cgc->cdb[0] >> 5) & 7]; + suc.suc_cdblen = cdblen; + bcopy(cgc->cdb, suc.suc_cdb, cdblen); + suc.suc_data = cgc->buf; + suc.suc_datalen = cgc->buflen; + suc.suc_timeout = cgc->timeout; + if (ioctl(fd, SCSIRAWCDB, &suc) == -1) + return(errno); + scsistatus = suc.suc_sus.sus_status; + +/* + * If the device returns a scsi sense error and debugging is enabled print + * some hopefully useful information on stderr. +*/ + if (scsistatus && debug) + { + cp = suc.suc_sus.sus_sense; + fprintf(stderr,"scsistatus = %x cdb =", + scsistatus); + for (i = 0; i < cdblen; i++) + fprintf(stderr, " %x", cgc->cdb[i]); + fprintf(stderr, "\nsense ="); + for (i = 0; i < 16; i++) + fprintf(stderr, " %x", cp[i]); + fprintf(stderr, "\n"); + } + if (cgc->sus) + bcopy(&suc.suc_sus, cgc->sus, sizeof (struct scsi_user_sense)); + if (scsistatus) + return(EIO); /* generic i/o error for unsuccessful status */ + return(0); + } + +/* + * The entry point for the DVDioctls for BSD/OS. +*/ +static int dvd_ioctl(int fd, u_long cmd, void *arg) + { + int ret; + + switch (cmd) + { + case DVD_READ_STRUCT: + ret = dvd_read_struct(fd, (dvd_struct *)arg); + if (ret) + errno = ret; + return(ret ? -1 : 0); + case DVD_AUTH: + ret = dvd_do_auth(fd, (dvd_authinfo *)arg); + if (ret) + errno = ret; + return(ret ? -1 : 0); + default: + errno = EINVAL; + return(-1); + } + } + +/* + * The entry point for the CDROMioctls for BSD/OS +*/ +static int cdrom_read_block(int, cgc_t *, int, int, int, int); +static int cdrom_read_cd(int, cgc_t *, int, int, int ); + int cdrom_blocksize(int, int ); + +static inline +int msf_to_lba(char m, char s, char f) +{ + return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET; +} + +cdrom_ioctl(int fd, u_long cmd, void *arg) + { + int ret; + cgc_t cgc; + + switch (cmd) + { + case CDROMREADRAW: + case CDROMREADMODE1: + case CDROMREADMODE2: + { + struct cdrom_msf *msf; + int blocksize = 0, format = 0, lba; + + switch (cmd) + { + case CDROMREADRAW: + blocksize = CD_FRAMESIZE_RAW; + break; + case CDROMREADMODE1: + blocksize = CD_FRAMESIZE; + format = 2; + break; + case CDROMREADMODE2: + blocksize = CD_FRAMESIZE_RAW0; + break; + } + msf = (struct cdrom_msf *)arg; + lba = msf_to_lba(msf->cdmsf_min0,msf->cdmsf_sec0, + msf->cdmsf_frame0); + ret = EINVAL; + if (lba < 0) + break; + + cgc_init(&cgc, arg, blocksize, SUC_READ); + ret = cdrom_read_block(fd, &cgc, lba, 1, format, blocksize); + if (ret) + { +/* + * SCSI-II devices are not required to support CMD_READ_CD (which specifies + * the blocksize to read) so try switching the block size with a mode select, + * doing the normal read sector command and then changing the sector size back + * to 2048. + * + * If the program dies before changing the blocksize back sdopen() + * in the kernel will fail opens with a message that looks something like: + * + * "sr1: blksize 2336 not multiple of 512: cannot use" + * + * At that point the drive has to be power cycled (or reset in some other way). +*/ + if (ret = cdrom_blocksize(fd, blocksize)) + break; + ret = cdrom_read_cd(fd, &cgc, lba, blocksize, 1); + ret |= cdrom_blocksize(fd, 2048); + } + break; + } + case CDROMREADTOCHDR: + { + struct cdrom_tochdr *tochdr = (struct cdrom_tochdr *) arg; + u_char buffer[12]; + + cgc_init(&cgc, buffer, sizeof (buffer), SUC_READ); + cgc.cdb[0] = CMD_READ_TOC_PMA_ATIP; + cgc.cdb[1] = 0x2; /* MSF */ + cgc.cdb[8] = 12; /* LSB of length */ + + ret = scsi_cmd(fd, &cgc); + if (!ret) + { + tochdr->cdth_trk0 = buffer[2]; + tochdr->cdth_trk1 = buffer[3]; + } + break; + } + case CDROMREADTOCENTRY: + { + struct cdrom_tocentry *tocentry = (struct cdrom_tocentry *) arg; + u_char buffer[12]; + + cgc_init(&cgc, buffer, sizeof (buffer), SUC_READ); + cgc.cdb[0] = CMD_READ_TOC_PMA_ATIP; + cgc.cdb[1] = (tocentry->cdte_format == CDROM_MSF) ? 0x02 : 0; + cgc.cdb[6] = tocentry->cdte_track; + cgc.cdb[8] = 12; /* LSB of length */ + + ret = scsi_cmd(fd, &cgc); + if (ret) + break; + + tocentry->cdte_ctrl = buffer[5] & 0xf; + tocentry->cdte_adr = buffer[5] >> 4; + tocentry->cdte_datamode = (tocentry->cdte_ctrl & 0x04) ? 1 : 0; + if (tocentry->cdte_format == CDROM_MSF) + { + tocentry->cdte_addr.msf.minute = buffer[9]; + tocentry->cdte_addr.msf.second = buffer[10]; + tocentry->cdte_addr.msf.frame = buffer[11]; + } + else + tocentry->cdte_addr.lba = (((((buffer[8] << 8) + + buffer[9]) << 8) + + buffer[10]) << 8) + + buffer[11]; + break; + } + case CDROMEJECT: /* NO-OP for now */ + ret = cdrom_tray_move(fd, 1); + break; + case CDROMCLOSETRAY: + ret = cdrom_tray_move(fd, 0); + break; +/* + * This sucks but emulates the expected behaviour. Instead of the return + * value being the actual status a success/fail indicator should have been + * returned and the 3rd arg to the ioctl should have been an 'int *' to update + * with the actual status. Both the drive and disc status ioctl calls are + * similarily braindamaged. +*/ + case CDROM_DRIVE_STATUS: + return(CDS_NO_INFO); /* XXX */ + case CDROM_DISC_STATUS: + { + tracktype tracks; + int cnt; + + cdrom_count_tracks(fd, &tracks); + if (tracks.error) + return(tracks.error); + if (tracks.audio > 0) + { + cnt = tracks.data + tracks.cdi + tracks.xa; + if (cnt == 0) + return(CDS_AUDIO); + else + return(CDS_MIXED); + } + if (tracks.cdi) + return(CDS_XA_2_2); + if (tracks.xa) + return(CDS_XA_2_1); + if (tracks.data) + return(CDS_DATA_1); + return(CDS_NO_INFO); + } + } + errno = ret; + return(ret ? -1 : 0); + } + +static int cdrom_read_cd(int fd, cgc_t *cgc, int lba, int blocksize, int nblocks) + { + + memset(&cgc->cdb, 0, sizeof(cgc->cdb)); + cgc->cdb[0] = CMD_READ_10; + cgc->cdb[2] = (lba >> 24) & 0xff; + cgc->cdb[3] = (lba >> 16) & 0xff; + cgc->cdb[4] = (lba >> 8) & 0xff; + cgc->cdb[5] = lba & 0xff; + cgc->cdb[6] = (nblocks >> 16) & 0xff; + cgc->cdb[7] = (nblocks >> 8) & 0xff; + cgc->cdb[8] = nblocks & 0xff; + cgc->buflen = blocksize * nblocks; + return(scsi_cmd(fd, cgc)); + } + +static int cdrom_read_block(int fd, cgc_t *cgc, + int lba, int nblocks, int format, int blksize) + { + + memset(&cgc->cdb, 0, sizeof(cgc->cdb)); + cgc->cdb[0] = CMD_READ_CD; + /* expected sector size - cdda,mode1,etc. */ + cgc->cdb[1] = format << 2; + /* starting address */ + cgc->cdb[2] = (lba >> 24) & 0xff; + cgc->cdb[3] = (lba >> 16) & 0xff; + cgc->cdb[4] = (lba >> 8) & 0xff; + cgc->cdb[5] = lba & 0xff; + /* number of blocks */ + cgc->cdb[6] = (nblocks >> 16) & 0xff; + cgc->cdb[7] = (nblocks >> 8) & 0xff; + cgc->cdb[8] = nblocks & 0xff; + cgc->buflen = blksize * nblocks; + + /* set the header info returned */ + switch (blksize) + { + case CD_FRAMESIZE_RAW0: + cgc->cdb[9] = 0x58; + break; + case CD_FRAMESIZE_RAW1: + cgc->cdb[9] = 0x78; + break; + case CD_FRAMESIZE_RAW: + cgc->cdb[9] = 0xf8; + break; + default: + cgc->cdb[9] = 0x10; + } + return(scsi_cmd(fd, cgc)); + } + +static void cdrom_count_tracks(int fd, tracktype *tracks) + { + struct cdrom_tochdr header; + struct cdrom_tocentry entry; + int ret, i; + + memset(tracks, 0, sizeof (*tracks)); + ret = cdrom_ioctl(fd, CDROMREADTOCHDR, &header); +/* + * This whole business is a crock anyhow so we don't bother distinguishing + * between no media, drive not ready, etc and on any error just say we have + * no info. +*/ + if (ret) + { + tracks->error = CDS_NO_INFO; + return; + } + + entry.cdte_format = CDROM_MSF; + for (i = header.cdth_trk0; i <= header.cdth_trk1; i++) + { + entry.cdte_track = i; + if (cdrom_ioctl(fd, CDROMREADTOCENTRY, &entry)) + { + tracks->error = CDS_NO_INFO; + return; + } + if (entry.cdte_ctrl & CDROM_DATA_TRACK) + { + if (entry.cdte_format == 0x10) + tracks->cdi++; + else if (entry.cdte_format == 0x20) + tracks->xa++; + else + tracks->data++; + } + else + tracks->audio++; + } + return; + } + +static int cdrom_tray_move(int fd, int flag) + { + cgc_t cgc; + + cgc_init(&cgc, NULL, 0, SUC_READ); + cgc.cdb[0] = CMD_START_STOP_UNIT; + cgc.cdb[1] = 1; /* immediate */ + cgc.cdb[4] = flag ? 0x2 : 0x3; /* eject : close */ + return(scsi_cmd(fd, &cgc)); + } + +/* + * Required when we need to use READ_10 to issue other than 2048 block + * reads + */ +int cdrom_blocksize(int fd, int size) + { + cgc_t cgc; + struct modesel_head mh; + + memset(&mh, 0, sizeof(mh)); + mh.block_desc_length = 0x08; + mh.block_length_med = (size >> 8) & 0xff; + mh.block_length_lo = size & 0xff; + + memset(&cgc, 0, sizeof(cgc)); + cgc.cdb[0] = 0x15; + cgc.cdb[1] = 1 << 4; + cgc.cdb[4] = 12; + cgc.buflen = sizeof(mh); + cgc.buf = (u_char *) &mh; + cgc.rw = SUC_WRITE; + mh.block_desc_length = 0x08; + mh.block_length_med = (size >> 8) & 0xff; + mh.block_length_lo = size & 0xff; + return(scsi_cmd(fd, &cgc)); + } diff --git a/libdvdcss/common.h b/libdvdcss/common.h new file mode 100644 index 0000000000..9a7ca3b510 --- /dev/null +++ b/libdvdcss/common.h @@ -0,0 +1,84 @@ +/***************************************************************************** + * common.h: common definitions + * Collection of useful common types and macros definitions + ***************************************************************************** + * Copyright (C) 1998, 1999, 2000 VideoLAN + * $Id$ + * + * Authors: Samuel Hocevar + * Vincent Seguin + * Gildas Bazin + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA. + *****************************************************************************/ + +/***************************************************************************** + * Basic types definitions + *****************************************************************************/ +#if defined( HAVE_STDINT_H ) +# include +#elif defined( HAVE_INTTYPES_H ) +# include +#elif defined( SYS_CYGWIN ) +# include + /* Cygwin only defines half of these... */ + typedef u_int8_t uint8_t; + typedef u_int32_t uint32_t; +#else + /* Fallback types (very x86-centric, sorry) */ + typedef unsigned char uint8_t; + typedef signed char int8_t; + typedef unsigned int uint32_t; + typedef signed int int32_t; +#endif + +#if defined( WIN32 ) + +# ifndef PATH_MAX +# define PATH_MAX MAX_PATH +# endif + +/* several type definitions */ +# if defined( __MINGW32__ ) +# define lseek64 _lseeki64 +# if !defined( _OFF_T_ ) +typedef long long _off_t; +typedef _off_t off_t; +# define _OFF_T_ +# else +# define off_t long long +# endif +# endif + +# if defined( _MSC_VER ) +# if !defined( _OFF_T_DEFINED ) +typedef __int64 off_t; +# define _OFF_T_DEFINED +# else +# define off_t __int64 +# endif +# define stat _stati64 +# endif + +# ifndef snprintf +# define snprintf _snprintf /* snprintf not defined in mingw32 (bug?) */ +# endif + +#else + +# define lseek64 lseek + +#endif + diff --git a/libdvdcss/css.c b/libdvdcss/css.c new file mode 100644 index 0000000000..a704a3fcf9 --- /dev/null +++ b/libdvdcss/css.c @@ -0,0 +1,1695 @@ +/***************************************************************************** + * css.c: Functions for DVD authentication and descrambling + ***************************************************************************** + * Copyright (C) 1999-2003 VideoLAN + * $Id$ + * + * Authors: Stéphane Borel + * Håkan Hjort + * + * based on: + * - css-auth by Derek Fawcus + * - DVD CSS ioctls example program by Andrew T. Veliath + * - The Divide and conquer attack by Frank A. Stevenson + * (see http://www-2.cs.cmu.edu/~dst/DeCSS/FrankStevenson/index.html) + * - DeCSSPlus by Ethan Hawke + * - DecVOB + * see http://www.lemuria.org/DeCSS/ by Tom Vogt for more information. + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA. + *****************************************************************************/ + +/***************************************************************************** + * Preamble + *****************************************************************************/ +#include "config.h" + +#include +#include +#include +#include +#include +#ifdef HAVE_SYS_PARAM_H +# include +#endif +#ifdef HAVE_UNISTD_H +# include +#endif +#include + +#ifdef HAVE_LIMITS_H +# include +#endif + +#include "dvdcss/dvdcss.h" + +#include "common.h" +#include "css.h" +#include "libdvdcss.h" +#include "csstables.h" +#include "ioctl.h" +#include "device.h" + +/***************************************************************************** + * Local prototypes + *****************************************************************************/ +static void PrintKey ( dvdcss_t, char *, uint8_t const * ); + +static int GetBusKey ( dvdcss_t ); +static int GetASF ( dvdcss_t ); + +static void CryptKey ( int, int, uint8_t const *, uint8_t * ); +static void DecryptKey ( uint8_t, + uint8_t const *, uint8_t const *, uint8_t * ); + +static int DecryptDiscKey ( dvdcss_t, uint8_t const *, dvd_key_t ); +static int CrackDiscKey ( dvdcss_t, uint8_t * ); + +static void DecryptTitleKey ( dvd_key_t, dvd_key_t ); +static int RecoverTitleKey ( int, uint8_t const *, + uint8_t const *, uint8_t const *, uint8_t * ); +static int CrackTitleKey ( dvdcss_t, int, int, dvd_key_t ); + +static int AttackPattern ( uint8_t const[], int, uint8_t * ); +#if 0 +static int AttackPadding ( uint8_t const[], int, uint8_t * ); +#endif + +/***************************************************************************** + * _dvdcss_test: check if the disc is encrypted or not + *****************************************************************************/ +int _dvdcss_test( dvdcss_t dvdcss ) +{ + int i_ret, i_copyright; + + i_ret = ioctl_ReadCopyright( dvdcss->i_fd, 0 /* i_layer */, &i_copyright ); + +#ifdef WIN32 + if( i_ret < 0 ) + { + /* Maybe we didn't have enough privileges to read the copyright + * (see ioctl_ReadCopyright comments). + * Apparently, on unencrypted DVDs _dvdcss_disckey() always fails, so + * we can check this as a workaround. */ + i_ret = 0; + i_copyright = 1; + if( _dvdcss_disckey( dvdcss ) < 0 ) + { + i_copyright = 0; + } + } +#endif + + if( i_ret < 0 ) + { + /* Since it's the first ioctl we try to issue, we add a notice */ + print_error( dvdcss, "css error: ioctl_ReadCopyright failed, " + "make sure there is a DVD in the drive, and that " + "you have used the correct device node." ); + + return i_ret; + } + + return i_copyright; +} + +/***************************************************************************** + * _dvdcss_title: crack or decrypt the current title key if needed + ***************************************************************************** + * This function should only be called by dvdcss->pf_seek and should eventually + * not be external if possible. + *****************************************************************************/ +int _dvdcss_title ( dvdcss_t dvdcss, int i_block ) +{ + dvd_title_t *p_title; + dvd_title_t *p_newtitle; + dvd_key_t p_title_key; + int i_fd, i_ret = -1, b_cache = 0; + + if( ! dvdcss->b_scrambled ) + { + return 0; + } + + /* Check if we've already cracked this key */ + p_title = dvdcss->p_titles; + while( p_title != NULL + && p_title->p_next != NULL + && p_title->p_next->i_startlb <= i_block ) + { + p_title = p_title->p_next; + } + + if( p_title != NULL + && p_title->i_startlb == i_block ) + { + /* We've already cracked this key, nothing to do */ + memcpy( dvdcss->css.p_title_key, p_title->p_key, sizeof(dvd_key_t) ); + return 0; + } + + /* Check whether the key is in our disk cache */ + if( dvdcss->psz_cachefile[0] ) + { + /* XXX: be careful, we use sprintf and not snprintf */ + sprintf( dvdcss->psz_block, "%.10x", i_block ); + i_fd = open( dvdcss->psz_cachefile, O_RDONLY ); + b_cache = 1; + + if( i_fd >= 0 ) + { + char psz_key[KEY_SIZE * 3]; + unsigned int k0, k1, k2, k3, k4; + + psz_key[KEY_SIZE * 3 - 1] = '\0'; + + if( read( i_fd, psz_key, KEY_SIZE * 3 - 1 ) == KEY_SIZE * 3 - 1 + && sscanf( psz_key, "%x:%x:%x:%x:%x", + &k0, &k1, &k2, &k3, &k4 ) == 5 ) + { + p_title_key[0] = k0; + p_title_key[1] = k1; + p_title_key[2] = k2; + p_title_key[3] = k3; + p_title_key[4] = k4; + PrintKey( dvdcss, "title key found in cache ", p_title_key ); + + /* Don't try to save it again */ + b_cache = 0; + i_ret = 1; + } + + close( i_fd ); + } + } + + /* Crack or decrypt CSS title key for current VTS */ + if( i_ret < 0 ) + { + i_ret = _dvdcss_titlekey( dvdcss, i_block, p_title_key ); + + if( i_ret < 0 ) + { + print_error( dvdcss, "fatal error in vts css key" ); + return i_ret; + } + + if( i_ret == 0 ) + { + print_debug( dvdcss, "unencrypted title" ); + /* We cache this anyway, so we don't need to check again. */ + } + } + + /* Key is valid, we store it on disk. */ + if( dvdcss->psz_cachefile[0] && b_cache ) + { + i_fd = open( dvdcss->psz_cachefile, O_RDWR|O_CREAT, 0644 ); + if( i_fd >= 0 ) + { + char psz_key[KEY_SIZE * 3 + 2]; + + sprintf( psz_key, "%02x:%02x:%02x:%02x:%02x\r\n", + p_title_key[0], p_title_key[1], p_title_key[2], + p_title_key[3], p_title_key[4] ); + + write( i_fd, psz_key, KEY_SIZE * 3 + 1 ); + close( i_fd ); + } + } + + /* Find our spot in the list */ + p_newtitle = NULL; + p_title = dvdcss->p_titles; + while( ( p_title != NULL ) && ( p_title->i_startlb < i_block ) ) + { + p_newtitle = p_title; + p_title = p_title->p_next; + } + + /* Save the found title */ + p_title = p_newtitle; + + /* Write in the new title and its key */ + p_newtitle = malloc( sizeof( dvd_title_t ) ); + p_newtitle->i_startlb = i_block; + memcpy( p_newtitle->p_key, p_title_key, KEY_SIZE ); + + /* Link it at the head of the (possibly empty) list */ + if( p_title == NULL ) + { + p_newtitle->p_next = dvdcss->p_titles; + dvdcss->p_titles = p_newtitle; + } + /* Link the new title inside the list */ + else + { + p_newtitle->p_next = p_title->p_next; + p_title->p_next = p_newtitle; + } + + memcpy( dvdcss->css.p_title_key, p_title_key, KEY_SIZE ); + return 0; +} + +/***************************************************************************** + * _dvdcss_disckey: get disc key. + ***************************************************************************** + * This function should only be called if DVD ioctls are present. + * It will set dvdcss->i_method = DVDCSS_METHOD_TITLE if it fails to find + * a valid disc key. + * Two decryption methods are offered: + * -disc key hash crack, + * -decryption with player keys if they are available. + *****************************************************************************/ +int _dvdcss_disckey( dvdcss_t dvdcss ) +{ + unsigned char p_buffer[ DVD_DISCKEY_SIZE ]; + dvd_key_t p_disc_key; + int i; + + if( GetBusKey( dvdcss ) < 0 ) + { + return -1; + } + + /* Get encrypted disc key */ + if( ioctl_ReadDiscKey( dvdcss->i_fd, &dvdcss->css.i_agid, p_buffer ) < 0 ) + { + print_error( dvdcss, "ioctl ReadDiscKey failed" ); + return -1; + } + + /* This should have invaidated the AGID and got us ASF=1. */ + if( GetASF( dvdcss ) != 1 ) + { + /* Region mismatch (or region not set) is the most likely source. */ + print_error( dvdcss, + "ASF not 1 after reading disc key (region mismatch?)" ); + ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); + return -1; + } + + /* Shuffle disc key using bus key */ + for( i = 0 ; i < DVD_DISCKEY_SIZE ; i++ ) + { + p_buffer[ i ] ^= dvdcss->css.p_bus_key[ 4 - (i % KEY_SIZE) ]; + } + + /* Decrypt disc key */ + switch( dvdcss->i_method ) + { + case DVDCSS_METHOD_KEY: + + /* Decrypt disc key with player key. */ + PrintKey( dvdcss, "decrypting disc key ", p_buffer ); + if( ! DecryptDiscKey( dvdcss, p_buffer, p_disc_key ) ) + { + PrintKey( dvdcss, "decrypted disc key is ", p_disc_key ); + break; + } + print_debug( dvdcss, "failed to decrypt the disc key, " + "faulty drive/kernel? " + "cracking title keys instead" ); + + /* Fallback, but not to DISC as the disc key might be faulty */ + dvdcss->i_method = DVDCSS_METHOD_TITLE; + break; + + case DVDCSS_METHOD_DISC: + + /* Crack Disc key to be able to use it */ + memcpy( p_disc_key, p_buffer, KEY_SIZE ); + PrintKey( dvdcss, "cracking disc key ", p_disc_key ); + if( ! CrackDiscKey( dvdcss, p_disc_key ) ) + { + PrintKey( dvdcss, "cracked disc key is ", p_disc_key ); + break; + } + print_debug( dvdcss, "failed to crack the disc key" ); + memset( p_disc_key, 0, KEY_SIZE ); + dvdcss->i_method = DVDCSS_METHOD_TITLE; + break; + + default: + + print_debug( dvdcss, "disc key needs not be decrypted" ); + memset( p_disc_key, 0, KEY_SIZE ); + break; + } + + memcpy( dvdcss->css.p_disc_key, p_disc_key, KEY_SIZE ); + + return 0; +} + + +/***************************************************************************** + * _dvdcss_titlekey: get title key. + *****************************************************************************/ +int _dvdcss_titlekey( dvdcss_t dvdcss, int i_pos, dvd_key_t p_title_key ) +{ + static uint8_t p_garbage[ DVDCSS_BLOCK_SIZE ]; /* we never read it back */ + uint8_t p_key[ KEY_SIZE ]; + int i, i_ret = 0; + + if( dvdcss->b_ioctls && ( dvdcss->i_method == DVDCSS_METHOD_KEY || + dvdcss->i_method == DVDCSS_METHOD_DISC ) ) + { + /* We have a decrypted Disc key and the ioctls are available, + * read the title key and decrypt it. + */ + + print_debug( dvdcss, "getting title key at block %i the classic way", + i_pos ); + + /* We need to authenticate again every time to get a new session key */ + if( GetBusKey( dvdcss ) < 0 ) + { + return -1; + } + + /* Get encrypted title key */ + if( ioctl_ReadTitleKey( dvdcss->i_fd, &dvdcss->css.i_agid, + i_pos, p_key ) < 0 ) + { + print_debug( dvdcss, + "ioctl ReadTitleKey failed (region mismatch?)" ); + i_ret = -1; + } + + /* Test ASF, it will be reset to 0 if we got a Region error */ + switch( GetASF( dvdcss ) ) + { + case -1: + /* An error getting the ASF status, something must be wrong. */ + print_debug( dvdcss, "lost ASF requesting title key" ); + ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); + i_ret = -1; + break; + + case 0: + /* This might either be a title that has no key, + * or we encountered a region error. */ + print_debug( dvdcss, "lost ASF requesting title key" ); + break; + + case 1: + /* Drive status is ok. */ + /* If the title key request failed, but we did not loose ASF, + * we might stil have the AGID. Other code assume that we + * will not after this so invalidate it(?). */ + if( i_ret < 0 ) + { + ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); + } + break; + } + + if( !( i_ret < 0 ) ) + { + /* Decrypt title key using the bus key */ + for( i = 0 ; i < KEY_SIZE ; i++ ) + { + p_key[ i ] ^= dvdcss->css.p_bus_key[ 4 - (i % KEY_SIZE) ]; + } + + /* If p_key is all zero then there really wasn't any key present + * even though we got to read it without an error. */ + if( !( p_key[0] | p_key[1] | p_key[2] | p_key[3] | p_key[4] ) ) + { + i_ret = 0; + } + else + { + PrintKey( dvdcss, "initial disc key ", dvdcss->css.p_disc_key ); + DecryptTitleKey( dvdcss->css.p_disc_key, p_key ); + PrintKey( dvdcss, "decrypted title key ", p_key ); + i_ret = 1; + } + + /* All went well either there wasn't a key or we have it now. */ + memcpy( p_title_key, p_key, KEY_SIZE ); + PrintKey( dvdcss, "title key is ", p_title_key ); + + return i_ret; + } + + /* The title key request failed */ + print_debug( dvdcss, "resetting drive and cracking title key" ); + + /* Read an unscrambled sector and reset the drive */ + dvdcss->pf_seek( dvdcss, 0 ); + dvdcss->pf_read( dvdcss, p_garbage, 1 ); + dvdcss->pf_seek( dvdcss, 0 ); + _dvdcss_disckey( dvdcss ); + + /* Fallback */ + } + + /* METHOD is TITLE, we can't use the ioctls or requesting the title key + * failed above. For these cases we try to crack the key instead. */ + + /* For now, the read limit is 9Gb / 2048 = 4718592 sectors. */ + i_ret = CrackTitleKey( dvdcss, i_pos, 4718592, p_key ); + + memcpy( p_title_key, p_key, KEY_SIZE ); + PrintKey( dvdcss, "title key is ", p_title_key ); + + return i_ret; +} + +/***************************************************************************** + * _dvdcss_unscramble: does the actual descrambling of data + ***************************************************************************** + * sec : sector to unscramble + * key : title key for this sector + *****************************************************************************/ +int _dvdcss_unscramble( dvd_key_t p_key, uint8_t *p_sec ) +{ + unsigned int i_t1, i_t2, i_t3, i_t4, i_t5, i_t6; + uint8_t *p_end = p_sec + DVDCSS_BLOCK_SIZE; + + /* PES_scrambling_control */ + if( !(p_sec[0x14] & 0x30) ) + { + return 0; + } + + i_t1 = (p_key[0] ^ p_sec[0x54]) | 0x100; + i_t2 = p_key[1] ^ p_sec[0x55]; + i_t3 = (p_key[2] | (p_key[3] << 8) | + (p_key[4] << 16)) ^ (p_sec[0x56] | + (p_sec[0x57] << 8) | (p_sec[0x58] << 16)); + i_t4 = i_t3 & 7; + i_t3 = i_t3 * 2 + 8 - i_t4; + p_sec += 0x80; + i_t5 = 0; + + while( p_sec != p_end ) + { + i_t4 = p_css_tab2[i_t2] ^ p_css_tab3[i_t1]; + i_t2 = i_t1>>1; + i_t1 = ( ( i_t1 & 1 ) << 8 ) ^ i_t4; + i_t4 = p_css_tab5[i_t4]; + i_t6 = ((((((( i_t3 >> 3 ) ^ i_t3 ) >> 1 ) ^ + i_t3 ) >> 8 ) ^ i_t3 ) >> 5 ) & 0xff; + i_t3 = (i_t3 << 8 ) | i_t6; + i_t6 = p_css_tab4[i_t6]; + i_t5 += i_t6 + i_t4; + *p_sec = p_css_tab1[*p_sec] ^ ( i_t5 & 0xff ); + p_sec++; + i_t5 >>= 8; + } + + return 0; +} + +/* Following functions are local */ + +/***************************************************************************** + * GetBusKey : Go through the CSS Authentication process + ***************************************************************************** + * It simulates the mutual authentication between logical unit and host, + * and stops when a session key (called bus key) has been established. + * Always do the full auth sequence. Some drives seem to lie and always + * respond with ASF=1. For instance the old DVD roms on Compaq Armada says + * that ASF=1 from the start and then later fail with a 'read of scrambled + * block without authentication' error. + *****************************************************************************/ +static int GetBusKey( dvdcss_t dvdcss ) +{ + uint8_t p_buffer[10]; + uint8_t p_challenge[2*KEY_SIZE]; + dvd_key_t p_key1; + dvd_key_t p_key2; + dvd_key_t p_key_check; + uint8_t i_variant = 0; + int i_ret = -1; + int i; + + print_debug( dvdcss, "requesting AGID" ); + i_ret = ioctl_ReportAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); + + /* We might have to reset hung authentication processes in the drive + * by invalidating the corresponding AGID'. As long as we haven't got + * an AGID, invalidate one (in sequence) and try again. */ + for( i = 0; i_ret == -1 && i < 4 ; ++i ) + { + print_debug( dvdcss, "ioctl ReportAgid failed, " + "invalidating AGID %d", i ); + + /* This is really _not good_, should be handled by the OS. + * Invalidating an AGID could make another process fail somewhere + * in its authentication process. */ + dvdcss->css.i_agid = i; + ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); + + print_debug( dvdcss, "requesting AGID" ); + i_ret = ioctl_ReportAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); + } + + /* Unable to authenticate without AGID */ + if( i_ret == -1 ) + { + print_error( dvdcss, "ioctl ReportAgid failed, fatal" ); + return -1; + } + + /* Setup a challenge, any values should work */ + for( i = 0 ; i < 10; ++i ) + { + p_challenge[i] = i; + } + + /* Get challenge from host */ + for( i = 0 ; i < 10 ; ++i ) + { + p_buffer[9-i] = p_challenge[i]; + } + + /* Send challenge to LU */ + if( ioctl_SendChallenge( dvdcss->i_fd, + &dvdcss->css.i_agid, p_buffer ) < 0 ) + { + print_error( dvdcss, "ioctl SendChallenge failed" ); + ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); + return -1; + } + + /* Get key1 from LU */ + if( ioctl_ReportKey1( dvdcss->i_fd, &dvdcss->css.i_agid, p_buffer ) < 0) + { + print_error( dvdcss, "ioctl ReportKey1 failed" ); + ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); + return -1; + } + + /* Send key1 to host */ + for( i = 0 ; i < KEY_SIZE ; i++ ) + { + p_key1[i] = p_buffer[4-i]; + } + + for( i = 0 ; i < 32 ; ++i ) + { + CryptKey( 0, i, p_challenge, p_key_check ); + + if( memcmp( p_key_check, p_key1, KEY_SIZE ) == 0 ) + { + print_debug( dvdcss, "drive authenticated, using variant %d", i ); + i_variant = i; + break; + } + } + + if( i == 32 ) + { + print_error( dvdcss, "drive would not authenticate" ); + ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); + return -1; + } + + /* Get challenge from LU */ + if( ioctl_ReportChallenge( dvdcss->i_fd, + &dvdcss->css.i_agid, p_buffer ) < 0 ) + { + print_error( dvdcss, "ioctl ReportKeyChallenge failed" ); + ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); + return -1; + } + + /* Send challenge to host */ + for( i = 0 ; i < 10 ; ++i ) + { + p_challenge[i] = p_buffer[9-i]; + } + + CryptKey( 1, i_variant, p_challenge, p_key2 ); + + /* Get key2 from host */ + for( i = 0 ; i < KEY_SIZE ; ++i ) + { + p_buffer[4-i] = p_key2[i]; + } + + /* Send key2 to LU */ + if( ioctl_SendKey2( dvdcss->i_fd, &dvdcss->css.i_agid, p_buffer ) < 0 ) + { + print_error( dvdcss, "ioctl SendKey2 failed" ); + ioctl_InvalidateAgid( dvdcss->i_fd, &dvdcss->css.i_agid ); + return -1; + } + + /* The drive has accepted us as authentic. */ + print_debug( dvdcss, "authentication established" ); + + memcpy( p_challenge, p_key1, KEY_SIZE ); + memcpy( p_challenge + KEY_SIZE, p_key2, KEY_SIZE ); + + CryptKey( 2, i_variant, p_challenge, dvdcss->css.p_bus_key ); + + return 0; +} + +/***************************************************************************** + * PrintKey : debug function that dumps a key value + *****************************************************************************/ +static void PrintKey( dvdcss_t dvdcss, char *prefix, uint8_t const *data ) +{ + print_debug( dvdcss, "%s%02x:%02x:%02x:%02x:%02x", prefix, + data[0], data[1], data[2], data[3], data[4] ); +} + +/***************************************************************************** + * GetASF : Get Authentication success flag + ***************************************************************************** + * Returns : + * -1 on ioctl error, + * 0 if the device needs to be authenticated, + * 1 either. + *****************************************************************************/ +static int GetASF( dvdcss_t dvdcss ) +{ + int i_asf = 0; + + if( ioctl_ReportASF( dvdcss->i_fd, NULL, &i_asf ) != 0 ) + { + /* The ioctl process has failed */ + print_error( dvdcss, "GetASF fatal error" ); + return -1; + } + + if( i_asf ) + { + print_debug( dvdcss, "GetASF authenticated, ASF=1" ); + } + else + { + print_debug( dvdcss, "GetASF not authenticated, ASF=0" ); + } + + return i_asf; +} + +/***************************************************************************** + * CryptKey : shuffles bits and unencrypt keys. + ***************************************************************************** + * Used during authentication and disc key negociation in GetBusKey. + * i_key_type : 0->key1, 1->key2, 2->buskey. + * i_variant : between 0 and 31. + *****************************************************************************/ +static void CryptKey( int i_key_type, int i_variant, + uint8_t const *p_challenge, uint8_t *p_key ) +{ + /* Permutation table for challenge */ + uint8_t pp_perm_challenge[3][10] = + { { 1, 3, 0, 7, 5, 2, 9, 6, 4, 8 }, + { 6, 1, 9, 3, 8, 5, 7, 4, 0, 2 }, + { 4, 0, 3, 5, 7, 2, 8, 6, 1, 9 } }; + + /* Permutation table for variant table for key2 and buskey */ + uint8_t pp_perm_variant[2][32] = + { { 0x0a, 0x08, 0x0e, 0x0c, 0x0b, 0x09, 0x0f, 0x0d, + 0x1a, 0x18, 0x1e, 0x1c, 0x1b, 0x19, 0x1f, 0x1d, + 0x02, 0x00, 0x06, 0x04, 0x03, 0x01, 0x07, 0x05, + 0x12, 0x10, 0x16, 0x14, 0x13, 0x11, 0x17, 0x15 }, + { 0x12, 0x1a, 0x16, 0x1e, 0x02, 0x0a, 0x06, 0x0e, + 0x10, 0x18, 0x14, 0x1c, 0x00, 0x08, 0x04, 0x0c, + 0x13, 0x1b, 0x17, 0x1f, 0x03, 0x0b, 0x07, 0x0f, + 0x11, 0x19, 0x15, 0x1d, 0x01, 0x09, 0x05, 0x0d } }; + + uint8_t p_variants[32] = + { 0xB7, 0x74, 0x85, 0xD0, 0xCC, 0xDB, 0xCA, 0x73, + 0x03, 0xFE, 0x31, 0x03, 0x52, 0xE0, 0xB7, 0x42, + 0x63, 0x16, 0xF2, 0x2A, 0x79, 0x52, 0xFF, 0x1B, + 0x7A, 0x11, 0xCA, 0x1A, 0x9B, 0x40, 0xAD, 0x01 }; + + /* The "secret" key */ + uint8_t p_secret[5] = { 0x55, 0xD6, 0xC4, 0xC5, 0x28 }; + + uint8_t p_bits[30], p_scratch[10], p_tmp1[5], p_tmp2[5]; + uint8_t i_lfsr0_o; /* 1 bit used */ + uint8_t i_lfsr1_o; /* 1 bit used */ + uint8_t i_css_variant, i_cse, i_index, i_combined, i_carry; + uint8_t i_val = 0; + uint32_t i_lfsr0, i_lfsr1; + int i_term = 0; + int i_bit; + int i; + + for (i = 9; i >= 0; --i) + p_scratch[i] = p_challenge[pp_perm_challenge[i_key_type][i]]; + + i_css_variant = ( i_key_type == 0 ) ? i_variant : + pp_perm_variant[i_key_type-1][i_variant]; + + /* + * This encryption engine implements one of 32 variations + * one the same theme depending upon the choice in the + * variant parameter (0 - 31). + * + * The algorithm itself manipulates a 40 bit input into + * a 40 bit output. + * The parameter 'input' is 80 bits. It consists of + * the 40 bit input value that is to be encrypted followed + * by a 40 bit seed value for the pseudo random number + * generators. + */ + + /* Feed the secret into the input values such that + * we alter the seed to the LFSR's used above, then + * generate the bits to play with. + */ + for( i = 5 ; --i >= 0 ; ) + { + p_tmp1[i] = p_scratch[5 + i] ^ p_secret[i] ^ p_crypt_tab2[i]; + } + + /* + * We use two LFSR's (seeded from some of the input data bytes) to + * generate two streams of pseudo-random bits. These two bit streams + * are then combined by simply adding with carry to generate a final + * sequence of pseudo-random bits which is stored in the buffer that + * 'output' points to the end of - len is the size of this buffer. + * + * The first LFSR is of degree 25, and has a polynomial of: + * x^13 + x^5 + x^4 + x^1 + 1 + * + * The second LSFR is of degree 17, and has a (primitive) polynomial of: + * x^15 + x^1 + 1 + * + * I don't know if these polynomials are primitive modulo 2, and thus + * represent maximal-period LFSR's. + * + * + * Note that we take the output of each LFSR from the new shifted in + * bit, not the old shifted out bit. Thus for ease of use the LFSR's + * are implemented in bit reversed order. + * + */ + + /* In order to ensure that the LFSR works we need to ensure that the + * initial values are non-zero. Thus when we initialise them from + * the seed, we ensure that a bit is set. + */ + i_lfsr0 = ( p_tmp1[0] << 17 ) | ( p_tmp1[1] << 9 ) | + (( p_tmp1[2] & ~7 ) << 1 ) | 8 | ( p_tmp1[2] & 7 ); + i_lfsr1 = ( p_tmp1[3] << 9 ) | 0x100 | p_tmp1[4]; + + i_index = sizeof(p_bits); + i_carry = 0; + + do + { + for( i_bit = 0, i_val = 0 ; i_bit < 8 ; ++i_bit ) + { + + i_lfsr0_o = ( ( i_lfsr0 >> 24 ) ^ ( i_lfsr0 >> 21 ) ^ + ( i_lfsr0 >> 20 ) ^ ( i_lfsr0 >> 12 ) ) & 1; + i_lfsr0 = ( i_lfsr0 << 1 ) | i_lfsr0_o; + + i_lfsr1_o = ( ( i_lfsr1 >> 16 ) ^ ( i_lfsr1 >> 2 ) ) & 1; + i_lfsr1 = ( i_lfsr1 << 1 ) | i_lfsr1_o; + + i_combined = !i_lfsr1_o + i_carry + !i_lfsr0_o; + /* taking bit 1 */ + i_carry = ( i_combined >> 1 ) & 1; + i_val |= ( i_combined & 1 ) << i_bit; + } + + p_bits[--i_index] = i_val; + } while( i_index > 0 ); + + /* This term is used throughout the following to + * select one of 32 different variations on the + * algorithm. + */ + i_cse = p_variants[i_css_variant] ^ p_crypt_tab2[i_css_variant]; + + /* Now the actual blocks doing the encryption. Each + * of these works on 40 bits at a time and are quite + * similar. + */ + i_index = 0; + for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_scratch[i] ) + { + i_index = p_bits[25 + i] ^ p_scratch[i]; + i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse; + + p_tmp1[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term; + } + p_tmp1[4] ^= p_tmp1[0]; + + for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp1[i] ) + { + i_index = p_bits[20 + i] ^ p_tmp1[i]; + i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse; + + p_tmp2[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term; + } + p_tmp2[4] ^= p_tmp2[0]; + + for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp2[i] ) + { + i_index = p_bits[15 + i] ^ p_tmp2[i]; + i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse; + i_index = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term; + + p_tmp1[i] = p_crypt_tab0[i_index] ^ p_crypt_tab2[i_index]; + } + p_tmp1[4] ^= p_tmp1[0]; + + for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp1[i] ) + { + i_index = p_bits[10 + i] ^ p_tmp1[i]; + i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse; + + i_index = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term; + + p_tmp2[i] = p_crypt_tab0[i_index] ^ p_crypt_tab2[i_index]; + } + p_tmp2[4] ^= p_tmp2[0]; + + for( i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp2[i] ) + { + i_index = p_bits[5 + i] ^ p_tmp2[i]; + i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse; + + p_tmp1[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term; + } + p_tmp1[4] ^= p_tmp1[0]; + + for(i = 5, i_term = 0 ; --i >= 0 ; i_term = p_tmp1[i] ) + { + i_index = p_bits[i] ^ p_tmp1[i]; + i_index = p_crypt_tab1[i_index] ^ ~p_crypt_tab2[i_index] ^ i_cse; + + p_key[i] = p_crypt_tab2[i_index] ^ p_crypt_tab3[i_index] ^ i_term; + } + + return; +} + +/***************************************************************************** + * DecryptKey: decrypt p_crypted with p_key. + ***************************************************************************** + * Used to decrypt the disc key, with a player key, after requesting it + * in _dvdcss_disckey and to decrypt title keys, with a disc key, requested + * in _dvdcss_titlekey. + * The player keys and the resulting disc key are only used as KEKs + * (key encryption keys). + * Decryption is slightly dependant on the type of key: + * -for disc key, invert is 0x00, + * -for title key, invert if 0xff. + *****************************************************************************/ +static void DecryptKey( uint8_t invert, uint8_t const *p_key, + uint8_t const *p_crypted, uint8_t *p_result ) +{ + unsigned int i_lfsr1_lo; + uns