Nagravision 2 C++ Kaynak Kodları Bölüm 1 [C++ Kütüphane Fonksiyonları]

Kapat
X
 
  • Zaman
  • Gösterim
Clear All
yeni mesajlar
  • hizli
    Junior Member
    • 29-02-2004
    • 100

    Nagravision 2 C++ Kaynak Kodları Bölüm 1 [C++ Kütüphane Fonksiyonları]

    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...
    Son düzenleme hizli; 16-12-2006, 23:02.
  • hizli
    Junior Member
    • 29-02-2004
    • 100

    #2
    Konu: Nagravision 2 C++ Kaynak Kodları Bölüm 1 [C++ Kütüphane Fonksiyonları]

    =================================
    Dosya Adı: nagra2.c
    =================================

    #include "nagra2.h"
    byte exp=0x03;

    int nagra2_ecm(byte *source, byte *dw, byte *opkey, byte *ecmkey)
    {
    int i, octets; byte matrix[64];
    int foundCW=0;
    octets= (source[4]-5)>>3;
    // RSA512 with carry bit
    RSA(source+10,ecmkey,&exp,64,1);
    source[10+63] |= (source[9] & 0x80);
    //Idea CBC with opkey
    ideaCBC(opkey,source+10,source+10,1,octets);
    // RSA512
    RSA(source+10,ecmkey,&exp,64,1);
    // Byteflop
    for(i=0;i<64;i++) matrix[i]=source[10+63-i];
    memcpy(source+10,matrix,64);
    // Parsing of CWs
    for (i = 0; (i < 64) && !foundCW; i++)
    {
    if (((source[10+i] & 0xFE) == 0x10) && (source[10+i+1] == 0x09) && (!source[10+i+2]))
    {
    foundCW = -1;
    if(source[10+i] == 0x10)
    {
    memcpy(dw+8,&source[10+i+3],8);
    memcpy(dw,&source[10+i+14],8);
    }
    if(source[10+i] == 0x11)
    {
    memcpy(dw,&source[10+i+3],8);
    memcpy(dw+8,&source[10+i+14],8);
    }
    }
    }
    return foundCW;
    }
    void nagra2_emm(byte *source, byte *opkey, byte *emmkey)
    {
    int i; byte matrix[96];
    // RSA768 with carry bit
    RSA(source+10,emmkey,&exp,96,1);
    source[10+95] |= (source[9] & 0x80);
    //Idea CBC with opkey
    ideaCBC(opkey,source+10,source+10,1,12);
    // RSA768
    RSA(source+10,emmkey,&exp,96,1);
    // Byteflop
    for(i=0;i<96;i++) matrix[i]=source[10+95-i];
    memcpy(source+10,matrix,96);

    }

    Yorum

    • hizli
      Junior Member
      • 29-02-2004
      • 100

      #3
      Konu: Nagravision 2 C++ Kaynak Kodları Bölüm 1 [C++ Kütüphane Fonksiyonları]

      =================================
      Dosya Adı: nn.c
      =================================
      /* NN.C - natural numbers routines
      */
      /* Copyright (C) RSA Laboratories, a division of RSA Data Security,
      Inc., created 1991. All rights reserved.
      */
      #include "nn.h"
      __inline void NN_DigitMult (a, b, c)
      NN_DIGIT a[2], b, c;
      {
      #ifdef INT64
      INT64 t = (INT64)b*(INT64)c;
      a[0]=(NN_DIGIT) (t&0xFFFFFFFF);
      a[1]=(NN_DIGIT) (t>>32);
      #else
      NN_DIGIT t, u;
      NN_HALF_DIGIT bHigh, bLow, cHigh, cLow;
      bHigh = (NN_HALF_DIGIT)HIGH_HALF (b);
      bLow = (NN_HALF_DIGIT)LOW_HALF (b);
      cHigh = (NN_HALF_DIGIT)HIGH_HALF (c);
      cLow = (NN_HALF_DIGIT)LOW_HALF (c);
      a[0] = (NN_DIGIT)bLow * (NN_DIGIT)cLow;
      t = (NN_DIGIT)bLow * (NN_DIGIT)cHigh;
      u = (NN_DIGIT)bHigh * (NN_DIGIT)cLow;
      a[1] = (NN_DIGIT)bHigh * (NN_DIGIT)cHigh;
      if ((t += u) < u)
      a[1] += TO_HIGH_HALF (1);
      u = TO_HIGH_HALF (t);
      if ((a[0] += u) < u)
      a[1]++;
      a[1] += HIGH_HALF (t);
      #endif
      }
      /* Sets a = b / c, where a and c are digits.
      Lengths: b[2].
      Assumes b[1] < c and HIGH_HALF (c) > 0. For efficiency, c should be
      normalized.
      */
      __inline void NN_DigitDiv (a, b, c)
      NN_DIGIT *a, b[2], c;
      {
      #ifdef INT64
      unsigned INT64 t=(((unsigned INT64)b[1])<<32) + (unsigned INT64)b[0];
      t/=(unsigned INT64) c;
      if (t > 0xFFFFFFFF) t=0xFFFFFFFF;
      *a=(NN_DIGIT)t;
      #else
      NN_DIGIT t[2], u, v;
      NN_HALF_DIGIT aHigh, aLow, cHigh, cLow;
      cHigh = (NN_HALF_DIGIT)HIGH_HALF (c);
      cLow = (NN_HALF_DIGIT)LOW_HALF (c);
      t[0] = b[0];
      t[1] = b[1];
      /* Underestimate high half of quotient and subtract.
      */
      if (cHigh == MAX_NN_HALF_DIGIT)
      aHigh = (NN_HALF_DIGIT)HIGH_HALF (t[1]);
      else
      aHigh = (NN_HALF_DIGIT)(t[1] / (cHigh + 1));
      u = (NN_DIGIT)aHigh * (NN_DIGIT)cLow;
      v = (NN_DIGIT)aHigh * (NN_DIGIT)cHigh;
      if ((t[0] -= TO_HIGH_HALF (u)) > (MAX_NN_DIGIT - TO_HIGH_HALF (u)))
      t[1]--;
      t[1] -= HIGH_HALF (u);
      t[1] -= v;
      /* Correct estimate.
      */
      while ((t[1] > cHigh) ||
      ((t[1] == cHigh) && (t[0] >= TO_HIGH_HALF (cLow)))) {
      if ((t[0] -= TO_HIGH_HALF (cLow)) > MAX_NN_DIGIT - TO_HIGH_HALF (cLow))
      t[1]--;
      t[1] -= cHigh;
      aHigh++;
      }
      /* Underestimate low half of quotient and subtract.
      */
      if (cHigh == MAX_NN_HALF_DIGIT)
      aLow = (NN_HALF_DIGIT)LOW_HALF (t[1]);
      else
      aLow =
      (NN_HALF_DIGIT)((TO_HIGH_HALF (t[1]) + HIGH_HALF (t[0])) / (cHigh + 1));
      u = (NN_DIGIT)aLow * (NN_DIGIT)cLow;
      v = (NN_DIGIT)aLow * (NN_DIGIT)cHigh;
      if ((t[0] -= u) > (MAX_NN_DIGIT - u))
      t[1]--;
      if ((t[0] -= TO_HIGH_HALF (v)) > (MAX_NN_DIGIT - TO_HIGH_HALF (v)))
      t[1]--;
      t[1] -= HIGH_HALF (v);
      /* Correct estimate.
      */
      while ((t[1] > 0) || ((t[1] == 0) && t[0] >= c)) {
      if ((t[0] -= c) > (MAX_NN_DIGIT - c))
      t[1]--;
      aLow++;
      }
      *a = TO_HIGH_HALF (aHigh) + aLow;
      #endif
      }
      static __inline NN_DIGIT NN_AddDigitMult PROTO_LIST
      ((NN_DIGIT *, NN_DIGIT *, NN_DIGIT, NN_DIGIT *, unsigned int));
      static __inline NN_DIGIT NN_SubDigitMult PROTO_LIST
      ((NN_DIGIT *, NN_DIGIT *, NN_DIGIT, NN_DIGIT *, unsigned int));
      static __inline unsigned int NN_DigitBits PROTO_LIST ((NN_DIGIT));
      /* Decodes character string b into a, where character string is ordered
      from most to least significant.
      Lengths: a[digits], b[len].
      Assumes b[i] = 0 for i < len - digits * NN_DIGIT_LEN. (Otherwise most
      significant bytes are truncated.)
      */
      void NN_Decode (a, digits, b, len)
      NN_DIGIT *a;
      unsigned char *b;
      unsigned int digits, len;
      {
      NN_DIGIT t;
      int j;
      unsigned int i, u;
      for (i = 0, j = len - 1; i < digits && j >= 0; i++) {
      t = 0;
      for (u = 0; j >= 0 && u < NN_DIGIT_BITS; j--, u += 8)
      t |= ((NN_DIGIT)b[j]) << u;
      a[i] = t;
      }
      for (; i < digits; i++)
      a[i] = 0;
      }
      /* Decodes character string b into a, where character string is ordered
      from least to most significant.
      Lengths: a[digits], b[len].
      Assumes b[i] = 0 for i < len - digits * NN_DIGIT_LEN. (Otherwise most
      significant bytes are truncated.)
      (Little Endian)
      */
      void NN_DecodeLE (a, digits, b, len)
      NN_DIGIT *a;
      unsigned char *b;
      unsigned int digits, len;
      {
      NN_DIGIT t;
      unsigned int i, j, u;
      for (i = 0, j = 0; i < digits && j < len; i++) {
      t = 0;
      for (u = 0; j < len && u < NN_DIGIT_BITS; j++, u += 8)
      t |= ((NN_DIGIT)b[j]) << u;
      a[i] = t;
      }
      for (; i < digits; i++)
      a[i] = 0;
      }
      /* Encodes b into character string a, where character string is ordered
      from most to least significant.
      Lengths: a[len], b[digits].
      Assumes NN_Bits (b, digits) <= 8 * len. (Otherwise most significant
      digits are truncated.)
      */
      void NN_Encode (a, len, b, digits)
      NN_DIGIT *b;
      unsigned char *a;
      unsigned int digits, len;
      {
      NN_DIGIT t;
      int j;
      unsigned int i, u;
      for (i = 0, j = len - 1; i < digits && j >= 0; i++) {
      t = b[i];
      for (u = 0; j >= 0 && u < NN_DIGIT_BITS; j--, u += 8)
      a[j] = (unsigned char)(t >> u);
      }
      for (; j >= 0; j-
      a[j] = 0;
      }
      /*
      Encodes b into character string a, where character string is ordered
      from least to most significant.
      Lengths: a[len], b[digits].
      Assumes NN_Bits (b, digits) <= 8 * len. (Otherwise most significant
      digits are truncated.)

      (Little Endian)
      */
      void NN_EncodeLE (a, len, b, digits)
      NN_DIGIT *b;
      unsigned char *a;
      unsigned int digits, len;
      {
      NN_DIGIT t;
      unsigned int i, j, u;
      for (i = 0, j = 0; i < digits && j < len; i++) {
      t = b[i];
      for (u = 0; j < len && u < NN_DIGIT_BITS; j++, u += 8)
      a[j] = (unsigned char)(t >> u);
      }
      for (; j < len; j++)
      a[j] = 0;
      }
      /* Assigns a = b.
      Lengths: a[digits], b[digits].
      */
      void NN_Assign (a, b, digits)
      NN_DIGIT *a, *b;
      unsigned int digits;
      {
      #ifdef _WIN32
      memcpy((void*)a,(void*)b,sizeof(NN_DIGIT)*digits);
      #else
      unsigned int i;
      for (i = 0; i < digits; i++)
      a[i] = b[i];
      #endif
      }
      /* Assigns a = 0.
      Lengths: a[digits].
      */
      void NN_AssignZero (a, digits)
      NN_DIGIT *a;
      unsigned int digits;
      {
      #ifndef _WIN32
      unsigned int i;
      for (i = 0; i < digits; i++)
      a[i] = 0;
      #else
      memset((void*)a,0,digits*sizeof(NN_DIGIT));
      #endif
      }
      /* Assigns a = 2^b.
      Lengths: a[digits].
      Requires b < digits * NN_DIGIT_BITS.
      */
      void NN_Assign2Exp (a, b, digits)
      NN_DIGIT *a;
      unsigned int b, digits;
      {
      NN_AssignZero (a, digits);
      if (b >= digits * NN_DIGIT_BITS)
      return;
      a[b / NN_DIGIT_BITS] = (NN_DIGIT)1 << (b % NN_DIGIT_BITS);
      }
      /* Computes a = b + c. Returns carry.
      Lengths: a[digits], b[digits], c[digits].
      */
      NN_DIGIT NN_Add (a, b, c, digits)
      NN_DIGIT *a, *b, *c;
      unsigned int digits;
      {
      NN_DIGIT carry=0;
      unsigned int i=digits;
      while (i-
      {
      NN_DIGIT ai;
      if ((ai = *b++ + carry) < carry) ai = *c++;
      else
      {
      carry = ((ai += *c) < *c);
      c++;
      }
      *a++ = ai;
      }
      return (carry);
      }
      /* Computes a = b - c. Returns borrow.
      Lengths: a[digits], b[digits], c[digits].
      */
      NN_DIGIT NN_Sub (a, b, c, digits)
      NN_DIGIT *a, *b, *c;
      unsigned int digits;
      {
      NN_DIGIT borrow=0;
      unsigned int i=digits;
      while (i-
      {
      NN_DIGIT ai;
      if ((ai = *b++ - borrow) > (MAX_NN_DIGIT - borrow)) ai = MAX_NN_DIGIT - *c++;
      else
      {
      borrow = ((ai -= *c) > (MAX_NN_DIGIT - *c));
      c++;
      }
      *a++ = ai;
      }
      return (borrow);
      }
      /* Computes a = b * c.
      Lengths: a[2*digits], b[digits], c[digits].
      Assumes digits < MAX_NN_DIGITS.
      */
      void NN_Mult (a, b, c, digits)
      NN_DIGIT *a, *b, *c;
      unsigned int digits;
      {
      NN_DIGIT t[2*MAX_NN_DIGITS];
      NN_DIGIT *pt=t;
      unsigned int bDigits, cDigits;
      NN_AssignZero (t, 2 * digits);
      bDigits = NN_Digits (b, digits);
      cDigits = NN_Digits (c, digits);
      while (bDigits-
      {
      pt[cDigits] += NN_AddDigitMult(pt, pt, *b++, c, cDigits);
      pt++;
      }
      NN_Assign (a, t, 2 * digits);
      /* Zeroize potentially sensitive information.
      */
      R_memset ((POINTER)t, 0, sizeof (t));
      }
      /* Computes a = b * 2^c (i.e., shifts left c bits), returning carry.
      Lengths: a[digits], b[digits].
      Requires c < NN_DIGIT_BITS.
      */
      NN_DIGIT NN_LShift (a, b, c, digits)
      NN_DIGIT *a, *b;
      unsigned int c, digits;
      {
      NN_DIGIT carry=0;
      unsigned int i=digits, t=NN_DIGIT_BITS - c;
      if (t <= 0) return (0);
      if (c)
      {
      while (i-
      {
      NN_DIGIT bi = *b++;
      *a++ = (bi << c) | carry;
      carry = (bi >> t);
      }
      }
      else
      {
      while (i-
      {
      *a++ = *b++;
      }
      }
      return (carry);
      }
      /* Computes a = c div 2^c (i.e., shifts right c bits), returning carry.
      Lengths: a[digits], b[digits].
      Requires: c < NN_DIGIT_BITS.
      */
      NN_DIGIT NN_RShift (a, b, c, digits)
      NN_DIGIT *a, *b;
      unsigned int c, digits;
      {
      NN_DIGIT carry=0;
      unsigned int i=digits;
      unsigned int t=NN_DIGIT_BITS-c;
      if (t <= 0) return (0);
      if (c)
      {
      b+=i-1;
      a+=i-1;
      while (i-
      {
      NN_DIGIT bi=*b--;
      *a-- = (bi >> c) | carry;
      carry = (bi << t);
      }
      }
      else
      {
      while (i-
      {
      *a++ = *b++;
      }
      }
      return (carry);
      }
      /* Computes a = c div d and b = c mod d.
      Lengths: a[cDigits], b[dDigits], c[cDigits], d[dDigits].
      Assumes d > 0, cDigits < 2 * MAX_NN_DIGITS,
      dDigits < MAX_NN_DIGITS.
      */
      void NN_Div (a, b, c, cDigits, d, dDigits)
      NN_DIGIT *a, *b, *c, *d;
      unsigned int cDigits, dDigits;
      {
      NN_DIGIT ai, cc[2*MAX_NN_DIGITS+1], dd[MAX_NN_DIGITS], t;
      int i;
      unsigned int ddDigits, shift;
      ddDigits = NN_Digits (d, dDigits);
      if (ddDigits == 0) return;
      /* Normalize operands.
      */
      shift = NN_DIGIT_BITS - NN_DigitBits (d[ddDigits-1]);
      NN_AssignZero (cc, ddDigits);
      cc[cDigits] = NN_LShift (cc, c, shift, cDigits);
      NN_LShift (dd, d, shift, ddDigits);
      t = dd[ddDigits-1];
      NN_AssignZero (a, cDigits);
      for (i = cDigits-ddDigits; i >= 0; i- {
      /* Underestimate quotient digit and subtract.
      */
      if (t == MAX_NN_DIGIT) ai = cc[i+ddDigits];
      else NN_DigitDiv (&ai, &cc[i+ddDigits-1], t + 1);
      cc[i+ddDigits] -= NN_SubDigitMult (&cc[i], &cc[i], ai, dd, ddDigits);
      /* Correct estimate.
      */
      while (cc[i+ddDigits] || (NN_Cmp (&cc[i], dd, ddDigits) >= 0)) {
      ai++;
      cc[i+ddDigits] -= NN_Sub (&cc[i], &cc[i], dd, ddDigits);
      }
      a[i] = ai;
      }
      /* Restore result.
      */
      NN_AssignZero (b, dDigits);
      NN_RShift (b, cc, shift, ddDigits);
      /* Zeroize potentially sensitive information.
      */
      R_memset ((POINTER)cc, 0, sizeof (cc));
      R_memset ((POINTER)dd, 0, sizeof (dd));
      }
      /* Computes a = b mod c.
      Lengths: a[cDigits], b[bDigits], c[cDigits].
      Assumes c > 0, bDigits < 2 * MAX_NN_DIGITS, cDigits < MAX_NN_DIGITS.
      */
      void NN_Mod (a, b, bDigits, c, cDigits)
      NN_DIGIT *a, *b, *c;
      unsigned int bDigits, cDigits;
      {
      static NN_DIGIT t[2 * MAX_NN_DIGITS];
      NN_Div (t, a, b, bDigits, c, cDigits);
      /* Zeroize potentially sensitive information.
      */
      R_memset ((POINTER)t, 0, sizeof (t));
      }
      /* Computes a = b * c mod d.
      Lengths: a[digits], b[digits], c[digits], d[digits].
      Assumes d > 0, digits < MAX_NN_DIGITS.
      */
      void NN_ModMult (a, b, c, d, digits)
      NN_DIGIT *a, *b, *c, *d;
      unsigned int digits;
      {
      NN_DIGIT t[2*MAX_NN_DIGITS];
      NN_Mult (t, b, c, digits);
      NN_Mod (a, t, 2 * digits, d, digits);
      /* Zeroize potentially sensitive information.
      */
      R_memset ((POINTER)t, 0, sizeof (t));
      }
      /* Computes a = b^c mod d.
      Lengths: a[dDigits], b[dDigits], c[cDigits], d[dDigits].
      Assumes d > 0, cDigits > 0, dDigits < MAX_NN_DIGITS.
      */
      void NN_ModExp (a, b, c, cDigits, d, dDigits)
      NN_DIGIT *a, *b, *c, *d;
      unsigned int cDigits, dDigits;
      {
      NN_DIGIT bPower[3][MAX_NN_DIGITS], ci, t[MAX_NN_DIGITS];
      int i;
      unsigned int ciBits, j, s;
      /* Store b, b^2 mod d, and b^3 mod d.
      */
      NN_Assign (bPower[0], b, dDigits);
      NN_ModMult (bPower[1], bPower[0], b, d, dDigits);
      NN_ModMult (bPower[2], bPower[1], b, d, dDigits);
      NN_ASSIGN_DIGIT (t, 1, dDigits);
      cDigits = NN_Digits (c, cDigits);
      for (i = cDigits - 1; i >= 0; i- {
      ci = c[i];
      ciBits = NN_DIGIT_BITS;
      /* Scan past leading zero bits of most significant digit.
      */
      if (i == (int)(cDigits - 1)) {
      while (! DIGIT_2MSB (ci)) {
      ci <<= 2;
      ciBits -= 2;
      }
      }
      for (j = 0; j < ciBits; j += 2, ci <<= 2) {
      /* Compute t = t^4 * b^s mod d, where s = two MSB's of ci.
      */
      NN_ModMult (t, t, t, d, dDigits);
      NN_ModMult (t, t, t, d, dDigits);
      if ((s = DIGIT_2MSB (ci)) != 0)
      NN_ModMult (t, t, bPower[s-1], d, dDigits);
      }
      }
      NN_Assign (a, t, dDigits);
      /* Zeroize potentially sensitive information.
      */
      R_memset ((POINTER)bPower, 0, sizeof (bPower));
      R_memset ((POINTER)t, 0, sizeof (t));
      }
      /* Compute a = 1/b mod c, assuming inverse exists.
      Lengths: a[digits], b[digits], c[digits].
      Assumes gcd (b, c) = 1, digits < MAX_NN_DIGITS.
      */
      void NN_ModInv (a, b, c, digits)
      NN_DIGIT *a, *b, *c;
      unsigned int digits;
      {
      NN_DIGIT q[MAX_NN_DIGITS], t1[MAX_NN_DIGITS], t3[MAX_NN_DIGITS],
      u1[MAX_NN_DIGITS], u3[MAX_NN_DIGITS], v1[MAX_NN_DIGITS],
      v3[MAX_NN_DIGITS], w[2*MAX_NN_DIGITS];
      int u1Sign;
      /* Apply extended Euclidean algorithm, modified to avoid negative
      numbers.
      */
      NN_ASSIGN_DIGIT (u1, 1, digits);
      NN_AssignZero (v1, digits);
      NN_Assign (u3, b, digits);
      NN_Assign (v3, c, digits);
      u1Sign = 1;
      while (! NN_Zero (v3, digits)) {
      NN_Div (q, t3, u3, digits, v3, digits);
      NN_Mult (w, q, v1, digits);
      NN_Add (t1, u1, w, digits);
      NN_Assign (u1, v1, digits);
      NN_Assign (v1, t1, digits);
      NN_Assign (u3, v3, digits);
      NN_Assign (v3, t3, digits);
      u1Sign = -u1Sign;
      }
      /* Negate result if sign is negative.
      */
      if (u1Sign < 0)
      NN_Sub (a, c, u1, digits);
      else
      NN_Assign (a, u1, digits);
      /* Zeroize potentially sensitive information.
      */
      R_memset ((POINTER)q, 0, sizeof (q));
      R_memset ((POINTER)t1, 0, sizeof (t1));
      R_memset ((POINTER)t3, 0, sizeof (t3));
      R_memset ((POINTER)u1, 0, sizeof (u1));
      R_memset ((POINTER)u3, 0, sizeof (u3));
      R_memset ((POINTER)v1, 0, sizeof (v1));
      R_memset ((POINTER)v3, 0, sizeof (v3));
      R_memset ((POINTER)w, 0, sizeof (w));
      }
      /* Computes a = gcd(b, c).
      Lengths: a[digits], b[digits], c[digits].
      Assumes b > c, digits < MAX_NN_DIGITS.
      */
      void NN_Gcd (a, b, c, digits)
      NN_DIGIT *a, *b, *c;
      unsigned int digits;
      {
      NN_DIGIT t[MAX_NN_DIGITS], u[MAX_NN_DIGITS], v[MAX_NN_DIGITS];
      NN_Assign (u, b, digits);
      NN_Assign (v, c, digits);
      while (! NN_Zero (v, digits)) {
      NN_Mod (t, u, digits, v, digits);
      NN_Assign (u, v, digits);
      NN_Assign (v, t, digits);
      }
      NN_Assign (a, u, digits);
      /* Zeroize potentially sensitive information.
      */
      R_memset ((POINTER)t, 0, sizeof (t));
      R_memset ((POINTER)u, 0, sizeof (u));
      R_memset ((POINTER)v, 0, sizeof (v));
      }
      /* Returns sign of a - b.
      Lengths: a[digits], b[digits].
      */
      int NN_Cmp (a, b, digits)
      NN_DIGIT *a, *b;
      unsigned int digits;
      {
      int i;
      for (i = digits - 1; i >= 0; i- {
      if (a[i] > b[i])
      return (1);
      if (a[i] < b[i])
      return (-1);
      }
      return (0);
      }
      /* Returns nonzero iff a is zero.
      Lengths: a[digits].
      */
      int NN_Zero (a, digits)
      NN_DIGIT *a;
      unsigned int digits;
      {
      unsigned int i=digits;
      while (i- if (*a++) return 0;
      return (1);
      }
      /* Returns the significant length of a in bits.
      Lengths: a[digits].
      */
      unsigned int NN_Bits (a, digits)
      NN_DIGIT *a;
      unsigned int digits;
      {
      if ((digits = NN_Digits (a, digits)) == 0)
      return (0);
      return ((digits - 1) * NN_DIGIT_BITS + NN_DigitBits (a[digits-1]));
      }
      /* Returns the significant length of a in digits.
      Lengths: a[digits].
      */
      unsigned int NN_Digits (a, digits)
      NN_DIGIT *a;
      unsigned int digits;
      {
      int i=digits;
      while (--i>=0 && !a[i]);
      return (i + 1);
      }
      /* Computes a = b + c*d, where c is a digit. Returns carry.
      Lengths: a[digits], b[digits], d[digits].
      */
      static NN_DIGIT NN_AddDigitMult (a, b, c, d, digits)
      NN_DIGIT *a, *b, c, *d;
      unsigned int digits;
      {
      NN_DIGIT carry=0, t[2];
      unsigned int i=digits;
      if (c == 0) return (0);
      while (i-
      {
      NN_DigitMult (t, c, *d++);
      carry = ((*a = *b++ + carry) < carry);
      carry += ((*a++ += t[0]) < t[0]) + t[1];
      }
      return (carry);
      }
      /* Computes a = b - c*d, where c is a digit. Returns borrow.
      Lengths: a[digits], b[digits], d[digits].
      */
      static NN_DIGIT NN_SubDigitMult (a, b, c, d, digits)
      NN_DIGIT *a, *b, c, *d;
      unsigned int digits;
      {
      NN_DIGIT borrow=0, t[2];
      unsigned int i=digits;
      if (c == 0) return (0);
      while (i-
      {
      NN_DigitMult (t, c, *d++);
      borrow = ((*a = *b++ - borrow) > (MAX_NN_DIGIT - borrow));
      borrow += ((*a++ -= t[0]) > (MAX_NN_DIGIT - t[0])) + t[1];
      }
      return (borrow);
      }
      /* Returns the significant length of a in bits, where a is a digit.
      */
      static unsigned int NN_DigitBits (a)
      NN_DIGIT a;
      {
      unsigned int i=0;
      while (a && i < NN_DIGIT_BITS) { i++; a>>=1; }
      return (i);
      }
      /*--------------------------------------------------------------------------/
      /* HOW TO USE THIS LIBRARY: */
      /* */
      /* RSA Function.- */
      /* */
      /* msg -> Contains PLAIN before call and CRYPTED after called */
      /* mod -> Modulus for RSA */
      /* exp -> Exponent for RSA */
      /* modbytes -> Number of bytes in Modulus */
      /* expbytes -> Number of bytes in Exponent */
      /* */
      /* msg = (msg ^ exp) % mod */
      /*--------------------------------------------------------------------------/
      void RSA(msg, mod, exp, modbytes, expbytes)
      byte *msg, *mod, *exp;
      unsigned int modbytes, expbytes;
      {
      NN_DIGIT E[MAX_NN_DIGITS], N[MAX_NN_DIGITS];
      NN_DIGIT M[MAX_NN_DIGITS], MSG[MAX_NN_DIGITS];
      int nDigits, eDigits;
      NN_DecodeLE(MSG, MAX_NN_DIGITS, msg, modbytes);
      NN_DecodeLE(E, MAX_NN_DIGITS, exp, expbytes);
      NN_DecodeLE(N, MAX_NN_DIGITS, mod, modbytes);
      nDigits = NN_Digits (MSG, MAX_NN_DIGITS);
      eDigits = NN_Digits (E, MAX_NN_DIGITS);
      R_memset((POINTER)M,0,sizeof(M));
      NN_ModExp(M,MSG,E,eDigits,N,nDigits);
      NN_EncodeLE(msg,modbytes,M,MAX_NN_DIGITS);
      }

      Yorum

      • hizli
        Junior Member
        • 29-02-2004
        • 100

        #4
        Konu: Nagravision 2 C++ Kaynak Kodları Bölüm 1 [C++ Kütüphane Fonksiyonları]

        ve son olarak

        test etmek için aşağıdaki kodlar kullanılıyor.

        =================================
        Dosya adı: test.c
        =================================

        #include <stdio.h>
        #include "nagra2.h"

        main(void)
        {
        byte ECM[]={
        0x81,0x70,0x47,0x07,0x45,
        0x41,0x01,0x86,0x00,0x88,
        0x5F,0x53,0x71,0x1E,0x41,0x9B,0x88,0x9F,
        0x1D,0x6B,0x87,0x68,0x87,0x6B,0x3A,0x7A,
        0xA1,0xA9,0xAA,0xAE,0xCC,0x14,0x8D,0x7D,
        0xFB,0x0D,0x80,0x6A,0xFF,0x05,0xA3,0x2F,
        0xE4,0xE7,0xFF,0x15,0xAB,0x38,0x31,0xF6,
        0xF4,0x87,0x3C,0x92,0xDE,0x11,0x93,0x5F,
        0x39,0x2E,0x75,0x7B,0xD4,0x87,0xB7,0x44,
        0xE6,0xAD,0x24,0xC8,0x73,0xC1,0xF3,0x9A
        };
        byte opkey[]={
        0xF5,0x36,0x55,0x68,0xF5,0x46,0x63,0x32,0x52,0xEE, 0xD5,0x00,0x88,0x1E,0x5A,0x37
        };
        byte ecmkey[]={
        0xD9,0x1F,0xB4,0x82,0xF5,0x4C,0x45,0x35,0x62,0x1D, 0x84,0x5F,0x7E,0xC4,0xAB,0x4D,
        0xC9,0x30,0x9D,0xED,0x26,0xB5,0x40,0x30,0x84,0x8E, 0xB6,0x39,0x68,0x97,0x75,0x29,
        0xFE,0x8F,0xF1,0x86,0x13,0x27,0x61,0x71,0xE5,0x7B, 0xDA,0x8A,0x47,0xAC,0x99,0x37,
        0x03,0xCC,0xE2,0xA1,0xCB,0x07,0x19,0x98,0xEC,0xCB, 0x32,0x7E,0xF6,0x3C,0xCE,0xA7
        };
        byte verifykey[]={
        0x51,0x34,0x01,0x34,0x72,0x4A,0x01,0x69,0x12,0x0B, 0x43,0xA6,0xD4,0xA5,0x1A,0xD7
        };
        int i,j;byte dw[16], out[8];
        nagra2_ecm(ECM, dw, opkey, ecmkey);

        for(i=0;i<6;i++)
        {
        for(j=0;j<16;j++)
        {
        printf("%02X ",ECM[10+16*i+j]);
        }
        printf("\n");
        }
        printf("\nCW odd : ");for(i=0;i<8;i++) printf("%02X ",dw[i]);
        printf("\nCW even : ");for(i=0;i<8;i++) printf("%02X ",dw[i+8]);
        ideasignature(verifykey, out, ECM+18, 7);
        printf("\n");printf("signature : ");
        for(i=0;i<8;i++) printf("%02X ",out[i]);
        printf("\n");

        return 0;
        }


        arkadaşlar benim gibi programlama ile ilgilenen arkadaşarımın dikkatini çekecektir. Saygılar...

        Bilgi: Bu kodlar sadece merak amacıyla verilmiştir..

        Yorum

        • hakan20001357
          Junior Member
          • 22-05-2004
          • 215

          #5
          Konu: Nagravision 2 C++ Kaynak Kodları Bölüm 1 [C++ Kütüphane Fonksiyonları]

          verdigin bilgiler icin tesekkurler arkadas. bunlai nerden buldugunu sorabilirmiyim.

          Yorum

          • melos
            Junior Member
            • 16-12-2005
            • 30

            #6
            Konu: Nagravision 2 C++ Kaynak Kodları Bölüm 1 [C++ Kütüphane Fonksiyonları]

            arkadaşım bunlar ne işe yararlar açıklama yaparsanız iyi olur

            Yorum

            • hizli
              Junior Member
              • 29-02-2004
              • 100

              #7
              Konu: Nagravision 2 C++ Kaynak Kodları Bölüm 1 [C++ Kütüphane Fonksiyonları]

              dostlar bunları yabancı bir dorum sitesinden aldım. bunlar digital+ paketinin şifreleme algoritmasıdır.

              Yorum

              • SEVMEK46
                Senior Member
                • 28-10-2004
                • 6710

                #8
                Re: Nagravision 2 C++ Kaynak Kodları Bölüm 1 [C++ Kütüphane Fonksiyonları]

                bunlar şifre değildir aman ha bunu softcam yapıpda cihaza felan atmayın sonra cihazınız uçar,c++ diye program vardır yazılım dilidir ,,,hepsine bakmadım ama o kodlara benziyor birde hızlı arkadasım anlamadıgınız bılgılerı veya üyelerin anlamadıgı bılgileri bence paylaşmayın ,çünkü forumda keyleri yazmak ta sorun yaşayan üyeler vardır,kafaları karışmasın bunları görünce!!!

                Yorum

                • hizli
                  Junior Member
                  • 29-02-2004
                  • 100

                  #9
                  Konu: Nagravision 2 C++ Kaynak Kodları Bölüm 1 [C++ Kütüphane Fonksiyonları]

                  dostum aslında haklısın bilmiyorum işte meraklı arkadaşlar vardır bakmak isterler diye düşünmüştüm....teşekkürler...

                  Yorum

                  • SEVMEK46
                    Senior Member
                    • 28-10-2004
                    • 6710

                    #10
                    Re: Konu: Nagravision 2 C++ Kaynak Kodları Bölüm 1 [C++ Kütüphane Fonksiyonları]

                    Originally posted by hizli
                    dostum aslında haklısın bilmiyorum işte meraklı arkadaşlar vardır bakmak isterler diye düşünmüştüm....teşekkürler...
                    anladım ,,teşekkür ederim tekrardan!!!

                    Yorum

                    • Kadim
                      Senior Member

                      • 30-01-2004
                      • 4782

                      #11
                      Konu: Nagravision 2 C++ Kaynak Kodları Bölüm 1 [C++ Kütüphane Fonksiyonları]

                      bunlar kaç aydır var dostum, yeni değil ki. Allnagra2src diye ararsan heryerde bulursun.

                      Yorum

                      İşlem Yapılıyor