Exemplo n.º 1
0
static const char*
selftest(void)
{
  ARCFOUR_context ctx;
  byte scratch[16];

  /* Test vector from Cryptlib labeled there: "from the
     State/Commerce Department". */
  static const byte key_1[] =
    { 0x61, 0x8A, 0x63, 0xD2, 0xFB };
  static const byte plaintext_1[] =
    { 0xDC, 0xEE, 0x4C, 0xF9, 0x2C };
  static const byte ciphertext_1[] =
    { 0xF1, 0x38, 0x29, 0xC9, 0xDE };

  arcfour_setkey( &ctx, key_1, sizeof(key_1));
  encrypt_stream( &ctx, scratch, plaintext_1, sizeof(plaintext_1));
  if ( memcmp (scratch, ciphertext_1, sizeof (ciphertext_1)))
    return "Arcfour encryption test 1 failed.";
  arcfour_setkey( &ctx, key_1, sizeof(key_1));
  encrypt_stream(&ctx, scratch, scratch, sizeof(plaintext_1)); /* decrypt */
  if ( memcmp (scratch, plaintext_1, sizeof (plaintext_1)))
    return "Arcfour decryption test 1 failed.";
  return NULL;
}
Exemplo n.º 2
0
int wmain(int argc, wchar_t* argv[])
{
    int rc=0, passlen=0;
    FILE *infp = NULL;
    FILE *outfp = NULL;
    encryptmode_t mode=UNINIT;
    wchar_t *infile = NULL,
            pass[MAX_PASSWD_LEN+1];
    int file_count = 0;
    wchar_t outfile[1024];

    // Initialize the output filename
    outfile[0] = '\0';
    
    while ((rc = getopt(argc, argv, L"vhdep:o:")) != -1)
    {
        switch (rc)
        {
            case 'h':
                usage(argv[0]);
                return 0;
            case 'v':
                version(argv[0]);
                return 0;
            case 'd':
                if (mode != UNINIT)
                {
                    fprintf(stderr, "Error: only specify one of -d or -e\n");
                    cleanup(outfile);
                    return -1;
                }
                mode = DEC;
                break;
            case 'e':
                if (mode != UNINIT)
                {
                    fprintf(stderr, "Error: only specify one of -d or -e\n");
                    cleanup(outfile);
                    return -1;
                }
                mode = ENC;
                break;
            case 'p':
                if (optarg != 0)
                {
                    wcscpy(pass, optarg);
                    passlen = (int) wcslen(pass);
                    if (passlen < 0)
                    {
                        cleanup(outfile);
                        return -1;
                    }
                }
                break;
            case 'o':
                // outfile argument
                if (!wcsncmp(L"-", optarg, 2))
                {
                    // if '-' is outfile name then out to stdout
                    outfp = stdout;
                    _setmode(_fileno(stdout), _O_BINARY);
                }
                else if ((outfp = _wfopen(optarg, L"wb")) == NULL)
                {
                    fwprintf(stderr, L"Error opening output file %s:", optarg);
                    perror("");
                    cleanup(outfile);
                    return  -1;
                }
                wcsncpy(outfile, optarg, 1024);
                outfile[1023] = '\0';
                break;
            default:
                fwprintf(stderr, L"Error: Unknown option '%c'\n", rc);
        }
    }
    
    if (optind >= argc)
    {
        fprintf(stderr, "Error: No file argument specified\n");
        usage(argv[0]);
        cleanup(outfile);
        return -1;
    }

    if (mode == UNINIT)
    {
        fprintf(stderr, "Error: -e or -d not specified\n");
        usage(argv[0]);
        cleanup(outfile);
        return -1;
    }

    // Prompt for password if not provided on the command line
    if (passlen == 0)
    {
        passlen = read_password(pass, mode);

        switch (passlen)
        {
            case 0: //no password in input
                fprintf(stderr, "Error: No password supplied.\n");
                cleanup(outfile);
                return -1;
            case AESCRYPT_READPWD_GETWCHAR:
            case AESCRYPT_READPWD_TOOLONG:
                fwprintf(stderr, L"Error in read_password: %s.\n",
                        read_password_error(passlen));
                cleanup(outfile);
                return -1;
            case AESCRYPT_READPWD_NOMATCH:
                fprintf(stderr, "Error: Passwords don't match.\n");
                cleanup(outfile);
                return -1;
        }

        if (passlen < 0)
        {
            cleanup(outfile);
            // For security reasons, erase the password
            wmemset(pass, 0, passlen);
            return -1;
        }
    }

    file_count = argc - optind;
    if ((file_count > 1) && (outfp != NULL))
    {
        if (outfp != stdout)
        {
            fclose(outfp);
        }
        fprintf(stderr, "Error: A single output file may not be specified with multiple input files.\n");
        usage(argv[0]);
        cleanup(outfile);
        // For security reasons, erase the password
        wmemset(pass, 0, passlen);
        return -1;
    }

    while (optind < argc)
    {
        infile = argv[optind++];

        if(!wcsncmp(L"-", infile, 2))
        {
            if (file_count > 1)
            {
                if ((outfp != stdout) && (outfp != NULL))
                {
                    fclose(outfp);
                }
                fprintf(stderr, "Error: STDIN may not be specified with multiple input files.\n");
                usage(argv[0]);
                cleanup(outfile);
                // For security reasons, erase the password
                wmemset(pass, 0, passlen);
                return -1;
            }
            infp = stdin;
            _setmode(_fileno(stdin), _O_BINARY);
            if (outfp == NULL)
            {
                outfp = stdout;
                _setmode(_fileno(stdout), _O_BINARY);
            }
        }
        else if ((infp = _wfopen(infile, L"rb")) == NULL)
        {
            if ((outfp != stdout) && (outfp != NULL))
            {
                fclose(outfp);
            }
            fwprintf(stderr, L"Error opening input file %s : ", infile);
            perror("");
            cleanup(outfile);
            // For security reasons, erase the password
            wmemset(pass, 0, passlen);
            return  -1;
        }
        
        if (mode == ENC)
        {
            if (outfp == NULL)
            {
                _snwprintf(outfile, 1024, L"%s.aes", infile);
                if ((outfp = _wfopen(outfile, L"wb")) == NULL)
                {
                    if ((infp != stdin) && (infp != NULL))
                    {
                        fclose(infp);
                    }
                    fwprintf(stderr, L"Error opening output file %s : ", outfile);
                    perror("");
                    cleanup(outfile);
                    // For security reasons, erase the password
                    wmemset(pass, 0, passlen);
                    return  -1;
                }
            }
            
            rc = encrypt_stream(infp, outfp, pass, passlen);
        }
        else if (mode == DEC)
        {
            if (outfp == NULL)
            {
                // assume .aes extension
                wcsncpy(outfile, infile, wcslen(infile)-4);
                outfile[wcslen(infile)-4] = '\0';
                if ((outfp = _wfopen(outfile, L"wb")) == NULL)
                {
                    if ((infp != stdin) && (infp != NULL))
                    {
                        fclose(infp);
                    }
                    fwprintf(stderr, L"Error opening output file %s : ", outfile);
                    perror("");
                    cleanup(outfile);
                    // For security reasons, erase the password
                    wmemset(pass, 0, passlen);
                    return  -1;
                }
            }
            
            // should probably test against ascii, utf-16le, and utf-16be encodings
            rc = decrypt_stream(infp, outfp, pass, passlen);
        }
        
        if ((infp != stdin) && (infp != NULL))
        {
            fclose(infp);
        }
        if ((outfp != stdout) && (outfp != NULL))
        {
            fclose(outfp);
        }

        // If there was an error, remove the output file
        if (rc)
        {
            cleanup(outfile);
            // For security reasons, erase the password
            wmemset(pass, 0, passlen);
            return -1;
        }

        // Reset input/output file names and desriptors
        outfile[0] = '\0';
        infp = NULL;
        outfp = NULL;
    }

    // For security reasons, erase the password
    wmemset(pass, 0, passlen);
    
    return rc;
}