Created
          July 2, 2017 17:33 
        
      - 
      
 - 
        
Save yorickvP/1f9c67aa8aa74bd7b5f1e7779384bb4d to your computer and use it in GitHub Desktop.  
    sss-cli
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #include <ctype.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <unistd.h> | |
| #include <string.h> | |
| #include "sss.h" | |
| int cli_create(int argc, const char* argv[]) { | |
| if (argc != 4) return 1; | |
| unsigned long int shares = strtoul(argv[2], NULL, 10); | |
| unsigned long int required = strtoul(argv[3], NULL, 10); | |
| if (shares < 1 || shares > 40 || required < 1 || required > shares) | |
| return 1; | |
| uint8_t data[sss_MLEN]; | |
| memset(data, 0, sss_MLEN); | |
| fread(data, 1, sss_MLEN, stdin); | |
| sss_Share ss_shares[shares]; | |
| sss_create_shares(ss_shares, data, shares, required); | |
| for(uint8_t idx = 0; idx < shares; idx++) { | |
| for(uint8_t i = 0; i < sss_SHARE_LEN; i++) | |
| printf("%02x", ss_shares[idx][i]); | |
| printf("\n"); | |
| } | |
| return 0; | |
| } | |
| int cli_combine(int argc, const char* argv[]) { | |
| if (argc != 3) return 1; | |
| unsigned long int shares = strtoul(argv[2], NULL, 10); | |
| if (shares < 1 || shares > 40) return 1; | |
| sss_Share ss_shares[shares]; | |
| for(uint8_t idx = 0; idx < shares; idx++) { | |
| char buffer[sss_SHARE_LEN*2+2]; | |
| fgets(buffer, sizeof(buffer), stdin); | |
| if (strlen(buffer) < sss_SHARE_LEN*2) { | |
| fprintf(stderr, "not enough bytes given for secret\n"); | |
| return 2; | |
| } | |
| for(uint8_t i = 0; i < sss_SHARE_LEN*2; i+=2) { | |
| if(sscanf(&buffer[i], "%2x", &ss_shares[idx][i/2]) != 1) { | |
| fprintf(stderr, "invalid byte in input\n"); | |
| return 2; | |
| } | |
| } | |
| } | |
| uint8_t restored[sss_MLEN]; | |
| if(!sss_combine_shares(restored, ss_shares, shares)) { | |
| fwrite(restored, 1, sss_MLEN, stdout); | |
| return 0; | |
| } else { | |
| printf("Corrupted data or not enough shares.\n"); | |
| return 3; | |
| } | |
| return 0; | |
| } | |
| int main(int argc, const char* argv[]) { | |
| if (argc < 2) goto usage; | |
| if (strcmp(argv[1], "create") == 0) { | |
| if (cli_create(argc, argv) != 0) goto usage; | |
| } else if (strcmp(argv[1], "combine") == 0) { | |
| if (cli_combine(argc, argv) != 0) goto usage; | |
| } | |
| return 0; | |
| usage: | |
| fprintf(stderr,"Usage:\t %s create <shares> <required>\n" | |
| "\t %s combine <shares>\n",argv[0], argv[0]); | |
| return 1; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment