Dostlar merhaba. Nagravision 2 C++ kaynak kodları internet üzerinde yayınlandı. Bu koldar Digital + paketine ait. Bende sizlerle paylaşmak istedim. Tabi meraklı arkadaşlar için. Özellikle programcı arkadaşların dikkatini çekecektir...
Bu bölümde C++ Kütüphane fonksiyonlarını verecem.
=================================
Dosya adı: nagra2.h
=================================
#ifndef _NAGRA2_H
#define _NAGRA2_H
#include <string.h>
#include "idea.h"
#include "nn.h"
int nagra2_ecm(byte *source, byte *dw, byte *opkey, byte *ecmkey);
void nagra2_emm(byte *source, byte *opkey, byte *emmkey);
#endif
=================================
Dosya Adı: idea.h
=================================
#ifndef _IDEA_H
#define _IDEA_H
#include <string.h>
typedef unsigned char byte;
typedef unsigned short u16;
typedef unsigned long u32;
// Internal use functions
u16 MUL(u16 x,u16 y);
u16 mul_inv( u16 x );
void expand_key( byte *IDEAkey, u16 *ek );
void invert_subkeys( u16 *ek, u16 *dk );
void setsubkeys( u16 *ek, u16 *dk, byte *IDEAkey );
void cipher( byte *outbuf, byte *inbuf, u16 *subkey );
// External use function
void idea( byte *IDEAkey, byte *outbuf, byte *inbuf, int crypt);
void ideaCBC(byte *IDEAkey, byte *out, byte *in, int crypt, int octets);
void ideasignature(byte *verifykey, byte *out, byte *msg, int octets);
#endif
=================================
Dosya Adı: nn.h
=================================
/* NN.H - header file for NN.C
*/
/* Copyright (C) RSA Laboratories, a division of RSA Data Security,
Inc., created 1991. All rights reserved.
*/
#ifndef NN_H
#define NN_H
#ifdef __cplusplus
extern "C" {
#endif
#include <string.h>
#ifdef _WIN32
#define INT64 __int64
#endif
#ifdef __GNUC__
#define INT64 long long
#endif
/* PROTOTYPES should be set to one if and only if the compiler supports
function argument prototyping.
The following makes PROTOTYPES default to 1 if it has not already been
defined as 0 with C compiler flags.
*/
#ifndef PROTOTYPES
#define PROTOTYPES 1
#endif
/* PROTO_LIST is defined depending on how PROTOTYPES is defined above.
If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it
returns an empty list.
*/
#if PROTOTYPES
#define PROTO_LIST(list) list
#else
#define PROTO_LIST(list) ()
#endif
#define R_memset memset
/* POINTER defines a generic pointer type */
typedef unsigned char *POINTER;
/* UINT2 defines a two byte word */
typedef unsigned short int UINT2;
/* UINT4 defines a four byte word */
typedef unsigned int UINT4;
/* Type definitions.
*/
typedef UINT4 NN_DIGIT;
typedef UINT2 NN_HALF_DIGIT;
typedef unsigned char byte;
/* Constants.
Note: MAX_NN_DIGITS is long enough to hold any RSA modulus, plus
one more digit as required by R_GeneratePEMKeys (for n and phiN,
whose lengths must be even). All natural numbers have at most
MAX_NN_DIGITS digits, except for double-length intermediate values
in NN_Mult (t), NN_ModMult (t), NN_ModInv (w), and NN_Div (c).
*/
/* RSA key lengths */
#define MAX_RSA_MODULUS_BITS 4096
#define MAX_RSA_MODULUS_LEN ((MAX_RSA_MODULUS_BITS + 7) / 8)
/* Length of digit in bits */
#define NN_DIGIT_BITS 32
#define NN_HALF_DIGIT_BITS 16
/* Length of digit in bytes */
#define NN_DIGIT_LEN (NN_DIGIT_BITS / 8)
/* Maximum length in digits */
#define MAX_NN_DIGITS ((MAX_RSA_MODULUS_LEN + NN_DIGIT_LEN - 1) / NN_DIGIT_LEN + 1)
/* Maximum digits */
#define MAX_NN_DIGIT 0xffffffff
#define MAX_NN_HALF_DIGIT 0xffff
/* Macros.
*/
#define LOW_HALF(x) ((x) & MAX_NN_HALF_DIGIT)
#define HIGH_HALF(x) (((x) >> NN_HALF_DIGIT_BITS) & MAX_NN_HALF_DIGIT)
#define TO_HIGH_HALF(x) (((NN_DIGIT)(x)) << NN_HALF_DIGIT_BITS)
#define DIGIT_MSB(x) (unsigned int)(((x) >> (NN_DIGIT_BITS - 1)) & 1)
#define DIGIT_2MSB(x) (unsigned int)(((x) >> (NN_DIGIT_BITS - 2)) & 3)
/* CONVERSIONS
NN_Decode (a, digits, b, len) Decodes character string b into a.
NN_Encode (a, len, b, digits) Encodes a into character string b.
ASSIGNMENTS
NN_Assign (a, b, digits) Assigns a = b.
NN_ASSIGN_DIGIT (a, b, digits) Assigns a = b, where b is a digit.
NN_AssignZero (a, b, digits) Assigns a = 0.
NN_Assign2Exp (a, b, digits) Assigns a = 2^b.
ARITHMETIC OPERATIONS
NN_Add (a, b, c, digits) Computes a = b + c.
NN_Sub (a, b, c, digits) Computes a = b - c.
NN_Mult (a, b, c, digits) Computes a = b * c.
NN_LShift (a, b, c, digits) Computes a = b * 2^c.
NN_RShift (a, b, c, digits) Computes a = b / 2^c.
NN_Div (a, b, c, cDigits, d, dDigits) Computes a = c div d and b = c mod d.
NUMBER THEORY
NN_Mod (a, b, bDigits, c, cDigits) Computes a = b mod c.
NN_ModMult (a, b, c, d, digits) Computes a = b * c mod d.
NN_ModExp (a, b, c, cDigits, d, dDigits) Computes a = b^c mod d.
NN_ModInv (a, b, c, digits) Computes a = 1/b mod c.
NN_Gcd (a, b, c, digits) Computes a = gcd (b, c).
OTHER OPERATIONS
NN_EVEN (a, digits) Returns 1 iff a is even.
NN_Cmp (a, b, digits) Returns sign of a - b.
NN_EQUAL (a, digits) Returns 1 iff a = b.
NN_Zero (a, digits) Returns 1 iff a = 0.
NN_Digits (a, digits) Returns significant length of a in digits.
NN_Bits (a, digits) Returns significant length of a in bits.
*/
void NN_Decode PROTO_LIST
((NN_DIGIT *, unsigned int, unsigned char *, unsigned int));
void NN_Encode PROTO_LIST
((unsigned char *, unsigned int, NN_DIGIT *, unsigned int));
void NN_DecodeLE PROTO_LIST
((NN_DIGIT *, unsigned int, unsigned char *, unsigned int));
void NN_EncodeLE PROTO_LIST
((unsigned char *, unsigned int, NN_DIGIT *, unsigned int));
void NN_Assign PROTO_LIST ((NN_DIGIT *, NN_DIGIT *, unsigned int));
void NN_AssignZero PROTO_LIST ((NN_DIGIT *, unsigned int));
void NN_Assign2Exp PROTO_LIST ((NN_DIGIT *, unsigned int, unsigned int));
NN_DIGIT NN_Add PROTO_LIST
((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int));
NN_DIGIT NN_Sub PROTO_LIST
((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int));
void NN_Mult PROTO_LIST ((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int));
void NN_Div PROTO_LIST
((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int, NN_DIGIT *,
unsigned int));
NN_DIGIT NN_LShift PROTO_LIST
((NN_DIGIT *, NN_DIGIT *, unsigned int, unsigned int));
NN_DIGIT NN_RShift PROTO_LIST
((NN_DIGIT *, NN_DIGIT *, unsigned int, unsigned int));
void NN_Mod PROTO_LIST
((NN_DIGIT *, NN_DIGIT *, unsigned int, NN_DIGIT *, unsigned int));
void NN_ModMult PROTO_LIST
((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int));
void NN_ModExp PROTO_LIST
((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int, NN_DIGIT *,
unsigned int));
void NN_ModInv PROTO_LIST
((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int));
void NN_Gcd PROTO_LIST ((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int));
int NN_Cmp PROTO_LIST ((NN_DIGIT *, NN_DIGIT *, unsigned int));
int NN_Zero PROTO_LIST ((NN_DIGIT *, unsigned int));
unsigned int NN_Bits PROTO_LIST ((NN_DIGIT *, unsigned int));
unsigned int NN_Digits PROTO_LIST ((NN_DIGIT *, unsigned int));
void NN_DigitMult PROTO_LIST ((NN_DIGIT[2], NN_DIGIT, NN_DIGIT));
void NN_DigitDiv PROTO_LIST ((NN_DIGIT *, NN_DIGIT[2], NN_DIGIT));
#define NN_ASSIGN_DIGIT(a, b, digits) {NN_AssignZero (a, digits); a[0] = b;}
#define NN_EQUAL(a, b, digits) (! NN_Cmp (a, b, digits))
#define NN_EVEN(a, digits) (((digits) == 0) || ! (a[0] & 1))
#ifdef __cplusplus
}
#endif
#endif
İkinci bölümde C++ kodlarını vereceğim.
Saygılar...
Bu bölümde C++ Kütüphane fonksiyonlarını verecem.
=================================
Dosya adı: nagra2.h
=================================
#ifndef _NAGRA2_H
#define _NAGRA2_H
#include <string.h>
#include "idea.h"
#include "nn.h"
int nagra2_ecm(byte *source, byte *dw, byte *opkey, byte *ecmkey);
void nagra2_emm(byte *source, byte *opkey, byte *emmkey);
#endif
=================================
Dosya Adı: idea.h
=================================
#ifndef _IDEA_H
#define _IDEA_H
#include <string.h>
typedef unsigned char byte;
typedef unsigned short u16;
typedef unsigned long u32;
// Internal use functions
u16 MUL(u16 x,u16 y);
u16 mul_inv( u16 x );
void expand_key( byte *IDEAkey, u16 *ek );
void invert_subkeys( u16 *ek, u16 *dk );
void setsubkeys( u16 *ek, u16 *dk, byte *IDEAkey );
void cipher( byte *outbuf, byte *inbuf, u16 *subkey );
// External use function
void idea( byte *IDEAkey, byte *outbuf, byte *inbuf, int crypt);
void ideaCBC(byte *IDEAkey, byte *out, byte *in, int crypt, int octets);
void ideasignature(byte *verifykey, byte *out, byte *msg, int octets);
#endif
=================================
Dosya Adı: nn.h
=================================
/* NN.H - header file for NN.C
*/
/* Copyright (C) RSA Laboratories, a division of RSA Data Security,
Inc., created 1991. All rights reserved.
*/
#ifndef NN_H
#define NN_H
#ifdef __cplusplus
extern "C" {
#endif
#include <string.h>
#ifdef _WIN32
#define INT64 __int64
#endif
#ifdef __GNUC__
#define INT64 long long
#endif
/* PROTOTYPES should be set to one if and only if the compiler supports
function argument prototyping.
The following makes PROTOTYPES default to 1 if it has not already been
defined as 0 with C compiler flags.
*/
#ifndef PROTOTYPES
#define PROTOTYPES 1
#endif
/* PROTO_LIST is defined depending on how PROTOTYPES is defined above.
If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it
returns an empty list.
*/
#if PROTOTYPES
#define PROTO_LIST(list) list
#else
#define PROTO_LIST(list) ()
#endif
#define R_memset memset
/* POINTER defines a generic pointer type */
typedef unsigned char *POINTER;
/* UINT2 defines a two byte word */
typedef unsigned short int UINT2;
/* UINT4 defines a four byte word */
typedef unsigned int UINT4;
/* Type definitions.
*/
typedef UINT4 NN_DIGIT;
typedef UINT2 NN_HALF_DIGIT;
typedef unsigned char byte;
/* Constants.
Note: MAX_NN_DIGITS is long enough to hold any RSA modulus, plus
one more digit as required by R_GeneratePEMKeys (for n and phiN,
whose lengths must be even). All natural numbers have at most
MAX_NN_DIGITS digits, except for double-length intermediate values
in NN_Mult (t), NN_ModMult (t), NN_ModInv (w), and NN_Div (c).
*/
/* RSA key lengths */
#define MAX_RSA_MODULUS_BITS 4096
#define MAX_RSA_MODULUS_LEN ((MAX_RSA_MODULUS_BITS + 7) / 8)
/* Length of digit in bits */
#define NN_DIGIT_BITS 32
#define NN_HALF_DIGIT_BITS 16
/* Length of digit in bytes */
#define NN_DIGIT_LEN (NN_DIGIT_BITS / 8)
/* Maximum length in digits */
#define MAX_NN_DIGITS ((MAX_RSA_MODULUS_LEN + NN_DIGIT_LEN - 1) / NN_DIGIT_LEN + 1)
/* Maximum digits */
#define MAX_NN_DIGIT 0xffffffff
#define MAX_NN_HALF_DIGIT 0xffff
/* Macros.
*/
#define LOW_HALF(x) ((x) & MAX_NN_HALF_DIGIT)
#define HIGH_HALF(x) (((x) >> NN_HALF_DIGIT_BITS) & MAX_NN_HALF_DIGIT)
#define TO_HIGH_HALF(x) (((NN_DIGIT)(x)) << NN_HALF_DIGIT_BITS)
#define DIGIT_MSB(x) (unsigned int)(((x) >> (NN_DIGIT_BITS - 1)) & 1)
#define DIGIT_2MSB(x) (unsigned int)(((x) >> (NN_DIGIT_BITS - 2)) & 3)
/* CONVERSIONS
NN_Decode (a, digits, b, len) Decodes character string b into a.
NN_Encode (a, len, b, digits) Encodes a into character string b.
ASSIGNMENTS
NN_Assign (a, b, digits) Assigns a = b.
NN_ASSIGN_DIGIT (a, b, digits) Assigns a = b, where b is a digit.
NN_AssignZero (a, b, digits) Assigns a = 0.
NN_Assign2Exp (a, b, digits) Assigns a = 2^b.
ARITHMETIC OPERATIONS
NN_Add (a, b, c, digits) Computes a = b + c.
NN_Sub (a, b, c, digits) Computes a = b - c.
NN_Mult (a, b, c, digits) Computes a = b * c.
NN_LShift (a, b, c, digits) Computes a = b * 2^c.
NN_RShift (a, b, c, digits) Computes a = b / 2^c.
NN_Div (a, b, c, cDigits, d, dDigits) Computes a = c div d and b = c mod d.
NUMBER THEORY
NN_Mod (a, b, bDigits, c, cDigits) Computes a = b mod c.
NN_ModMult (a, b, c, d, digits) Computes a = b * c mod d.
NN_ModExp (a, b, c, cDigits, d, dDigits) Computes a = b^c mod d.
NN_ModInv (a, b, c, digits) Computes a = 1/b mod c.
NN_Gcd (a, b, c, digits) Computes a = gcd (b, c).
OTHER OPERATIONS
NN_EVEN (a, digits) Returns 1 iff a is even.
NN_Cmp (a, b, digits) Returns sign of a - b.
NN_EQUAL (a, digits) Returns 1 iff a = b.
NN_Zero (a, digits) Returns 1 iff a = 0.
NN_Digits (a, digits) Returns significant length of a in digits.
NN_Bits (a, digits) Returns significant length of a in bits.
*/
void NN_Decode PROTO_LIST
((NN_DIGIT *, unsigned int, unsigned char *, unsigned int));
void NN_Encode PROTO_LIST
((unsigned char *, unsigned int, NN_DIGIT *, unsigned int));
void NN_DecodeLE PROTO_LIST
((NN_DIGIT *, unsigned int, unsigned char *, unsigned int));
void NN_EncodeLE PROTO_LIST
((unsigned char *, unsigned int, NN_DIGIT *, unsigned int));
void NN_Assign PROTO_LIST ((NN_DIGIT *, NN_DIGIT *, unsigned int));
void NN_AssignZero PROTO_LIST ((NN_DIGIT *, unsigned int));
void NN_Assign2Exp PROTO_LIST ((NN_DIGIT *, unsigned int, unsigned int));
NN_DIGIT NN_Add PROTO_LIST
((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int));
NN_DIGIT NN_Sub PROTO_LIST
((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int));
void NN_Mult PROTO_LIST ((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int));
void NN_Div PROTO_LIST
((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int, NN_DIGIT *,
unsigned int));
NN_DIGIT NN_LShift PROTO_LIST
((NN_DIGIT *, NN_DIGIT *, unsigned int, unsigned int));
NN_DIGIT NN_RShift PROTO_LIST
((NN_DIGIT *, NN_DIGIT *, unsigned int, unsigned int));
void NN_Mod PROTO_LIST
((NN_DIGIT *, NN_DIGIT *, unsigned int, NN_DIGIT *, unsigned int));
void NN_ModMult PROTO_LIST
((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int));
void NN_ModExp PROTO_LIST
((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int, NN_DIGIT *,
unsigned int));
void NN_ModInv PROTO_LIST
((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int));
void NN_Gcd PROTO_LIST ((NN_DIGIT *, NN_DIGIT *, NN_DIGIT *, unsigned int));
int NN_Cmp PROTO_LIST ((NN_DIGIT *, NN_DIGIT *, unsigned int));
int NN_Zero PROTO_LIST ((NN_DIGIT *, unsigned int));
unsigned int NN_Bits PROTO_LIST ((NN_DIGIT *, unsigned int));
unsigned int NN_Digits PROTO_LIST ((NN_DIGIT *, unsigned int));
void NN_DigitMult PROTO_LIST ((NN_DIGIT[2], NN_DIGIT, NN_DIGIT));
void NN_DigitDiv PROTO_LIST ((NN_DIGIT *, NN_DIGIT[2], NN_DIGIT));
#define NN_ASSIGN_DIGIT(a, b, digits) {NN_AssignZero (a, digits); a[0] = b;}
#define NN_EQUAL(a, b, digits) (! NN_Cmp (a, b, digits))
#define NN_EVEN(a, digits) (((digits) == 0) || ! (a[0] & 1))
#ifdef __cplusplus
}
#endif
#endif
İkinci bölümde C++ kodlarını vereceğim.
Saygılar...
Yorum