Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

riglol

By: a guest on Sep 5th, 2013  |  syntax: C  |  size: 9.66 KB  |  views: 1,171  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2. ** rigol keygen / cybernet & the-eevblog-users
  3. **
  4. ** to compile this you need MIRACL from [url]https://github.com/CertiVox/MIRACL[/url]
  5. ** download the master.zip into a new folder and run 'unzip -j -aa -L master.zip'
  6. ** then run 'bash linux' to build the miracle.a library
  7. **
  8. ** BUILD WITH:
  9. **
  10. ** gcc riglol.c -I../MIRACL ../MIRACL/miracl.a -o riglol
  11. **
  12. ** adapt -I and path to miracl.a to your environment
  13. **
  14. ** more info: http://www.eevblog.com/forum/testgear/sniffing-the-rigol's-internal-i2c-bus/
  15. */
  16.  
  17. // #define DEBUG
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24. #include <stdio.h>
  25. #include "miracl.h"
  26.  
  27. // ECC SETTINGS
  28. char DP832_private_key[]  = "5C393C30FACCF4";
  29. char DS2000_private_key[] = "8EEBD4D04C3771";
  30. char DSA815_private_key[] = "80444DFECE903E";
  31. char private_key[]        = "";
  32. char prime1[]  = "AEBF94CEE3E707";
  33. char prime2[]  = "AEBF94D5C6AA71";
  34. char curve_a[] = "2982";
  35. char curve_b[] = "3408";
  36. char point1[]  = "7A3E808599A525";
  37. char point2[]  = "28BE7FAFD2A052";
  38.  
  39.  
  40. /*
  41. ** take serial and options make sha1 hash out of it
  42. */
  43. static void hashing(char *opt_str, big hash) {
  44.     char *p;
  45.     char h[20];
  46.     int ch;
  47.     sha sh;
  48.     shs_init(&sh);
  49.     p = opt_str;
  50.  
  51.     while(*p) {
  52.         shs_process(&sh, *p);
  53.         p++;
  54.     }
  55.  
  56.     shs_hash(&sh, h);
  57.     bytes_to_big(20, h, hash);
  58. }
  59.  
  60. /*
  61. ** sign the secret message (serial + opts) with the private key
  62. */
  63. void ecssign(char *serial, char *options, char *privk, char *lic1, char *lic2) {
  64.     int k_offset = 0; // optionally change ecssign starting offset (changes lic1; makes different licenses)
  65.     mirsys(800, 16)->IOBASE = 16;
  66.  
  67.     sha sha1;
  68.     shs_init(&sha1);
  69.  
  70.     char *ptr = serial;
  71.     while(*ptr) shs_process(&sha1, *ptr++);
  72.     ptr = options;
  73.     while(*ptr) shs_process(&sha1, *ptr++);
  74.  
  75.     char h[20];
  76.     shs_hash(&sha1, h);
  77.     big hash = mirvar(0);
  78.     bytes_to_big(20, h, hash);
  79.  
  80.     big a = mirvar(0);
  81.     instr(a, curve_a);
  82.     big b = mirvar(0);
  83.     instr(b, curve_b);
  84.     big p = mirvar(0);
  85.     instr(p, prime1);
  86.     big q = mirvar(0);
  87.     instr(q, prime2);
  88.     big Gx = mirvar(0);
  89.     instr(Gx, point1);
  90.     big Gy = mirvar(0);
  91.     instr(Gy, point2);
  92.     big d = mirvar(0);
  93.     instr(d, privk);
  94.     big k = mirvar(0);
  95.     big r = mirvar(0);
  96.     big s = mirvar(0);
  97.     big k1 = mirvar(0);
  98.     big zero = mirvar(0);
  99.  
  100.     big f1 = mirvar(17);
  101.     big f2 = mirvar(53);
  102.     big f3 = mirvar(905461);
  103.     big f4 = mirvar(60291817);
  104.  
  105.     incr(k, k_offset, k);
  106.  
  107.     epoint *G = epoint_init();
  108.     epoint *kG = epoint_init();
  109.     ecurve_init(a, b, p, MR_PROJECTIVE);
  110.     epoint_set(Gx, Gy, 0, G);
  111.  
  112.     for(;;) {
  113.         incr(k, 1, k);
  114.  
  115.         if(divisible(k, f1) || divisible(k, f2) || divisible(k, f3) || divisible(k, f4))
  116.             continue;
  117.  
  118.         ecurve_mult(k, G, kG);
  119.         epoint_get(kG, r, r);
  120.         divide(r, q, q);
  121.  
  122.         if(mr_compare(r, zero) == 0)
  123.             continue;
  124.  
  125.         xgcd(k, q, k1, k1, k1);
  126.         mad(d, r, hash, q, q, s);
  127.         mad(s, k1, k1, q, q, s);
  128.  
  129.         if(!divisible(s, f1) && !divisible(s, f2) && !divisible(s, f3) && !divisible(s, f4))
  130.             break;
  131.     }
  132.  
  133.     cotstr(r, lic1);
  134.     cotstr(s, lic2);
  135. }
  136.  
  137. /*
  138. ** convert string to uppercase chars
  139. */
  140. char * strtoupper(char *str) {
  141.     char *newstr, *p;
  142.     p = newstr = (char*) strdup((char*)str);
  143.     while ((*p++ = toupper(*p)));
  144.     return newstr;
  145. }
  146.  
  147. /*
  148. ** prepend a char to a string
  149. */
  150. char * prepend(char *c, char *str) {
  151.     int i;
  152.  
  153.     for (i = strlen(str); i >= 0; i--) {
  154.         str[i + 1] = str[i];
  155.     }
  156.  
  157.     str[0] = *c;
  158.     return c;
  159. }
  160.  
  161. /*
  162. ** convert hex-ascii-string to rigol license format
  163. */
  164. void map_hex_to_rigol(char *io) {
  165.     unsigned long long b = 0;
  166.     int i = 0;
  167.     char map[] = {
  168.         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  169.         'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R',
  170.         'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  171.         '2', '3', '4', '5', '6', '7', '8', '9'
  172.     };
  173.  
  174.     /* hex2dez */
  175.     while (io[i] != '\0') {
  176.         if (io[i] >= '0' && io[i] <= '9') {
  177.             b = b * 16 + io[i] - '0';
  178.         } else if (io[i] >= 'A' && io[i] <= 'F') {
  179.             b = b * 16 + io[i] - 'A' + 10;
  180.         } else if (io[i] >= 'a' && io[i] <= 'f') {
  181.             b = b * 16 + io[i] - 'a' + 10;
  182.         }
  183.  
  184.         i++;
  185.     }
  186.  
  187.     for (i = 3; ; i--) {
  188.         io[i] = map[b & 0x1F];
  189.         if (i == 0) break;
  190.         b >>= 5;
  191.     }
  192.  
  193.     io[4] = '\0';
  194. }
  195.  
  196. void show_help(char *cmd) {
  197.     printf("Usage: %s <sn> <opts> <privkey>\n", cmd);
  198.     printf("  <sn>       serial number of device (D............)\n");
  199.     printf("  <opts>     device options, 4 characters, see below\n");
  200.     printf("  <privkey>  private key (optional)\n");
  201.     printf("\n");
  202.     printf("DS2000 and DS4000 device options:\n");
  203.     printf("  first character:  D = official, V = trial\n");
  204.     printf("  second character: S\n");
  205.     printf("  third character:  A = DS2000, H = DS4000\n");
  206.     printf("  last character :  your options, use the following table to generate for DS2000:\n");
  207.     printf("  --------- A B C D E F G H J K L M N P Q R S T U V W X Y Z 2 3 4 5 6 7 8 9\n");
  208.     printf("  100MHz    ' ' ' ' ' ' ' ' * * * * * * * * ' ' ' ' ' ' ' ' * * * * * * * *\n");
  209.     printf("  200MHz    ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' * * * * * * * * * * * * * * * *\n");
  210.     printf("  Memory56M ' ' ' ' * * * * ' ' ' ' * * * * ' ' ' ' * * * * ' ' ' ' * * * *\n");
  211.     printf("  Decode    ' ' * * ' ' * * ' ' * * ' ' * * ' ' * * ' ' * * ' ' * * ' ' * *\n");
  212.     printf("  Triggers  ' * ' * ' * ' * ' * ' * ' * ' * ' * ' * ' * ' * ' * ' * ' * ' *\n");
  213.     printf("  For DS4000, try DSH9, VSH9 to enable all options.\n");
  214.     printf("\n");
  215.     printf("DP832 device options:\n");
  216.     printf("  first character:  M = official, 5 = trial\n");
  217.     printf("  MWSS - Trigger\n");
  218.     printf("  MWTB - Accuracy\n");
  219.     printf("  MWTC - LAN and RS232\n");
  220.     printf("  MWTE - Analyzer and Monitor\n");
  221.     printf("\n");
  222.     printf("DSA815 device options:\n");
  223.     printf("  first character:  A = official, S = trial\n");
  224.     printf("  AAAB - tracking generator\n");
  225.     printf("  AAAC - adv measurement kit\n");
  226.     printf("  AAAD - 10Hz RBW\n");
  227.     printf("  AAAE - EMI/Quasi Peak\n");
  228.     printf("  AAAF - VSWR\n");
  229.     printf("\n");
  230.     printf("MAKE SURE YOUR FIRMWARE IS UP TO DATE BEFORE APPLYING ANY KEYS\n");
  231. }
  232.  
  233. /*
  234. ** the world ends here
  235. */
  236. int main(int argc, char *argv[0]) {
  237.     char *options, *lic1_code, *lic2_code, *lic_all;
  238.     char *chunk, *temp, *final;
  239.     char *priv_key;
  240.     char *serial;
  241.     int            i = 0, j = 0;
  242.  
  243.     /* parse input */
  244.     if (!((argc == 3 || argc == 4))) {
  245.         show_help(argv[0]);
  246.         exit(-1);
  247.     }
  248.     serial = strtoupper((char*)argv[1]);
  249.     options = strtoupper((char*)argv[2]);
  250.     if (argc == 4) priv_key = (char*)argv[3];
  251.     else if (!strncmp(serial, "DS2", 3)) priv_key = DS2000_private_key;
  252.     else if (!strncmp(serial, "DS4", 3)) priv_key = DS2000_private_key;
  253.     else if (!strncmp(serial, "DS6", 3)) priv_key = DS2000_private_key;
  254.     else if (!strncmp(serial, "DSA", 3)) priv_key = DSA815_private_key;
  255.     else if (!strncmp(serial, "DP8", 3)) priv_key = DP832_private_key;
  256.     else {
  257.         show_help(argv[0]);
  258.         printf("\nERROR: UNKNOW DEVICE WITHOUT PRIVATKEY\n");
  259.         exit(-1);
  260.     }
  261.  
  262.     strtoupper(priv_key);
  263.     if (strlen(priv_key) != 14) {
  264.         show_help(argv[0]);
  265.         printf("\nERROR: INVALID PRIVATE KEY LENGTH\n");
  266.         exit(-1);
  267.     }
  268.     if (strlen(serial) < 13) {
  269.         show_help(argv[0]);
  270.         printf("\nERROR: INVALID SERIAL LENGTH\n");
  271.         exit(-1);
  272.     }
  273.     if (strlen(options) != 4) {
  274.         show_help(argv[0]);
  275.         printf("\nERROR: INVALID OPTIONS LENGTH\n");
  276.         exit(-1);
  277.     }
  278. #ifdef DEBUG
  279.     printf("private-key:      %s\n", priv_key);
  280.     printf("serial:           %s\n", serial);
  281.     printf("options:          %s\n", options);
  282. #endif
  283.  
  284.     /* sign the message */
  285.     lic1_code = calloc(64, 1);
  286.     lic2_code = calloc(64, 1);
  287.     ecssign(serial, options, priv_key, lic1_code, lic2_code);
  288.  
  289.     /* fix missing zeroes */
  290.     while (strlen(lic1_code) < 14) {
  291.         prepend("0", lic1_code);
  292.     }
  293.     while (strlen(lic2_code) < 14) {
  294.         prepend("0", lic2_code);
  295.     }
  296. #ifdef DEBUG
  297.     printf("lic1-code:        %s\n", lic1_code);
  298.     printf("lic2-code:        %s\n", lic2_code);
  299. #endif
  300.  
  301.     /* combine lic1 and lic2 */
  302.     lic_all = calloc(128, 1);
  303.     temp = calloc(128, 1);
  304.     chunk = calloc(6, 1);
  305.     final = calloc(128, 1);
  306.     strcpy(lic_all, lic1_code);
  307.     strcat(lic_all, "0");
  308.     strcat(lic_all, lic2_code);
  309.     strcat(lic_all, "0");
  310. #ifdef DEBUG
  311.     printf("target-code:      %s\n", lic_all);
  312. #endif
  313.  
  314.     /* generate serial */
  315.     while (i < strlen(lic_all)) {
  316.         memcpy(chunk, lic_all + i, 5);
  317.         map_hex_to_rigol(chunk);
  318.         strcat(temp, chunk);
  319.         i = i + 5;
  320.     }
  321.  
  322.     /* now add the options */
  323.     memcpy(final,      temp     , 1);
  324.     final[1] = options[0];
  325.     memcpy(final +  2, temp +  1, 7);
  326.     final[9] = options[1];
  327.     memcpy(final + 10, temp +  8, 7);
  328.     final[17] = options[2];
  329.     memcpy(final + 18, temp + 15, 7);
  330.     final[25] = options[3];
  331.     memcpy(final + 26, temp + 22, 4);
  332. #ifdef DEBUG
  333.     printf("----------------------------------------------------\n");
  334.     printf("your-license-key: ");
  335. #endif
  336.     for(i = 0; i < strlen(final); i++) {
  337.         if (i % 7 == 0 && i > 0) printf("-");
  338.         printf("%c", final[i]);
  339.     }
  340.     printf("\n");
  341. #ifdef DEBUG
  342.     printf("----------------------------------------------------\n");
  343. #endif
  344. }