libmobi
C library for handling MOBI format ebook documents
Data Structures | Macros | Functions
encryption.c File Reference

Functions to handle encryption. More...

#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "util.h"
#include "debug.h"
#include "randombytes.h"
#include "sha1.h"
#include "encryption.h"

Data Structures

struct  MOBIPk1
 Structure for PK1 routines. More...
 
struct  MOBIVoucher
 Structure for parsed drm record in record 0 header. More...
 
struct  MOBIExthDrm
 Drm components extracted from EXTH records. More...
 

Macros

#define INTERNAL_READER_KEY   ((unsigned char*) "\x72\x38\x33\xb0\xb4\xf2\xe3\xca\xdf\x09\x01\xd6\xe2\xe0\x3f\x96")
 
#define INTERNAL_PUBLISHER_KEY   ((unsigned char*) "\x95\xda\x7b\xed\x90\x5e\x10\x2e\x44\x4c\xb5\xe5\xc0\x25\xdf\x2c")
 
#define INTERNAL_READER_KEY_V1   ((unsigned char*) "QDCVEPMU675RUBSZ")
 
#define PIDSIZE   10
 
#define SERIALSIZE   16
 
#define SERIALLONGSIZE   40
 
#define KEYSIZE   16
 
#define COOKIESIZE   32
 
#define VOUCHERSIZE   48
 
#define VOUCHERS_COUNT_MAX   1024
 
#define VOUCHERS_SIZE_MIN   288
 
#define pk1_swap(a, b)   { uint16_t tmp = a; (a) = b; (b) = tmp; }
 

Functions

void mobi_free_drm (MOBIData *m)
 Free DRM structure. More...
 
MOBI_RET mobi_buffer_decrypt (unsigned char *out, const unsigned char *in, const size_t length, const MOBIData *m)
 Decrypt buffer with PK1 algorithm. More...
 
MOBI_RET mobi_drmkey_set_serial (MOBIData *m, const char *serial)
 Store key for encryption in MOBIData stucture. Pid will be calculated from device serial number. More...
 
MOBI_RET mobi_voucher_add (MOBIData *m, const char *serial, const time_t valid_from, const time_t valid_to, const MOBIExthTag *tamperkeys, const size_t tamperkeys_count)
 Add DRM voucher. More...
 
MOBI_RET mobi_drmkey_set (MOBIData *m, const char *pid)
 Store key for encryption in MOBIData stucture. More...
 
MOBI_RET mobi_drmkey_delete (MOBIData *m)
 Remove key from MOBIData structure. More...
 
MOBI_RET mobi_drm_decrypt (MOBIData *m)
 Decrypt document. More...
 
MOBI_RET mobi_drm_serialize_v2 (MOBIBuffer *buf, const MOBIData *m)
 Serialize encryption scheme version 2. More...
 
MOBI_RET mobi_drm_serialize_v1 (MOBIBuffer *buf, const MOBIData *m)
 Serialize encryption scheme version 1. More...
 
MOBI_RET mobi_drm_encrypt (MOBIData *m)
 Encrypt document. More...
 

Detailed Description

Functions to handle encryption.

Copyright (c) 2014 Bartek Fabiszewski http://www.fabiszewski.net

This file is part of libmobi. Licensed under LGPL, either version 3, or any later. See http://www.gnu.org/licenses/

Function Documentation

◆ mobi_buffer_decrypt()

MOBI_RET mobi_buffer_decrypt ( unsigned char *  out,
const unsigned char *  in,
const size_t  length,
const MOBIData m 
)

Decrypt buffer with PK1 algorithm.

Parameters
[in,out]outDecrypted buffer
[in]inEncrypted buffer
[in]lengthBuffer length
[in]mMOBIData structure with loaded key
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_drm_serialize_v1()

MOBI_RET mobi_drm_serialize_v1 ( MOBIBuffer buf,
const MOBIData m 
)

Serialize encryption scheme version 1.

Parameters
[in,out]bufOutput buffer
[in]mMOBIData structure with raw data and metadata
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_drm_serialize_v2()

MOBI_RET mobi_drm_serialize_v2 ( MOBIBuffer buf,
const MOBIData m 
)

Serialize encryption scheme version 2.

Parameters
[in,out]bufOutput buffer
[in]mMOBIData structure with raw data and metadata
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_drmkey_delete()

MOBI_RET mobi_drmkey_delete ( MOBIData m)

Remove key from MOBIData structure.

Parameters
[in,out]mMOBIData structure with raw data and metadata
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_drmkey_set()

MOBI_RET mobi_drmkey_set ( MOBIData m,
const char *  pid 
)

Store key for encryption in MOBIData stucture.

In case of encrypted document key is extracted from document. PID may be needed.

Parameters
[in,out]mMOBIData structure with raw data and metadata
[in]pidPID, may be NULL in case of encryption type 1, which does not use PID
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_drmkey_set_serial()

MOBI_RET mobi_drmkey_set_serial ( MOBIData m,
const char *  serial 
)

Store key for encryption in MOBIData stucture. Pid will be calculated from device serial number.

Parameters
[in,out]mMOBIData structure with raw data and metadata
[in]serialSerial
Returns
MOBI_RET status code (on success MOBI_SUCCESS)

◆ mobi_free_drm()

void mobi_free_drm ( MOBIData m)

Free DRM structure.

Parameters
[in,out]mMOBIData structure with raw data and metadata

◆ mobi_voucher_add()

MOBI_RET mobi_voucher_add ( MOBIData m,
const char *  serial,
const time_t  valid_from,
const time_t  valid_to,
const MOBIExthTag tamperkeys,
const size_t  tamperkeys_count 
)

Add DRM voucher.

See also
mobi_drm_addvoucher
Parameters
[in,out]mMOBIData structure with raw data and metadata
[in]serialDevice serial number
[in]valid_fromVoucher validity start time, -1 if not set
[in]valid_toVoucher expire time, -1 if not set
[in]tamperkeysArray of EXTH tags to include in PID generation, NULL if none
[in]tamperkeys_countCount of EXTH tags
Returns
MOBI_RET status code (on success MOBI_SUCCESS)