Exemplo n.º 1
0
/* M.decrypt(aes_ctx, ciphered_string) returns message_string or nil+error_num. */
static int api_decrypt( lua_State *L) {
    OAES_CTX *ctx = checkctx( L, 1);
    size_t srclen, dstlen;
    const uint8_t *src = (const uint8_t *) luaL_checklstring( L, 2, & srclen);
    uint8_t *dst;
    oaes_decrypt( ctx, src, srclen, NULL, & dstlen);
    dst = malloc( dstlen);
    if( ! dst) { lua_pushnil( L); lua_pushinteger( L, OAES_RET_MEM); return 2; }
    int r = oaes_decrypt( ctx, src, srclen, dst, & dstlen);
    if( r) { free( dst); lua_pushnil( L); lua_pushinteger( L, r); return 2; }
    lua_pushlstring( L, (const char *) dst, dstlen);
    free( dst);
    return 1;
}
Exemplo n.º 2
0
Arquivo: oaes.c Projeto: ekr/hacrypto
// caller must free b->out if it's not NULL
static OAES_RET _do_aes_decrypt(do_block *b)
{
	OAES_CTX * ctx = NULL;
	OAES_RET _rc = OAES_RET_SUCCESS;

	if( NULL == b )
		return OAES_RET_ARG1;

	ctx = oaes_alloc();
	if( NULL == ctx )
	{
		fprintf(stderr, "Error: Failed to initialize OAES.\n");
		return OAES_RET_MEM;
	}

	if( _is_ecb )
		if( OAES_RET_SUCCESS != oaes_set_option( ctx, OAES_OPTION_ECB, NULL ) )
			fprintf(stderr, "Error: Failed to set OAES options.\n");

	oaes_key_import_data( ctx, _key_data, _key_data_len );

	b->out = NULL;
	b->out_len = 0;
	_rc = oaes_decrypt( ctx,
			b->in, b->in_len, b->out, &(b->out_len) );
	if( OAES_RET_SUCCESS != _rc )
	{
		fprintf(stderr, "Error: Failed to decrypt.\n");
		oaes_free(&ctx);
		return _rc;
	}
	b->out = (uint8_t *) calloc(b->out_len, sizeof(uint8_t));
	if( NULL == b->out )
	{
		fprintf(stderr, "Error: Failed to allocate memory.\n");
		oaes_free(&ctx);
		return OAES_RET_MEM;
	}
	_rc = oaes_decrypt( ctx,
			b->in, b->in_len, b->out, &(b->out_len) );

	if( OAES_RET_SUCCESS !=  oaes_free(&ctx) )
		fprintf(stderr, "Error: Failed to uninitialize OAES.\n");
	
	return _rc;
}
Exemplo n.º 3
0
int TWFunc::Try_Decrypting_File(string fn, string password) {
#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
	OAES_CTX * ctx = NULL;
	uint8_t _key_data[32] = "";
	FILE *f;
	uint8_t buffer[4096];
	uint8_t *buffer_out = NULL;
	uint8_t *ptr = NULL;
	size_t read_len = 0, out_len = 0;
	int firstbyte = 0, secondbyte = 0, key_len;
	size_t _j = 0;
	size_t _key_data_len = 0;

	// mostly kanged from OpenAES oaes.c
	for( _j = 0; _j < 32; _j++ )
		_key_data[_j] = _j + 1;
	_key_data_len = password.size();
	if( 16 >= _key_data_len )
		_key_data_len = 16;
	else if( 24 >= _key_data_len )
		_key_data_len = 24;
	else
	_key_data_len = 32;
	memcpy(_key_data, password.c_str(), password.size());

	ctx = oaes_alloc();
	if (ctx == NULL) {
		LOGERR("Failed to allocate OAES\n");
		return -1;
	}

	oaes_key_import_data(ctx, _key_data, _key_data_len);

	f = fopen(fn.c_str(), "rb");
	if (f == NULL) {
		LOGERR("Failed to open '%s' to try decrypt\n", fn.c_str());
		return -1;
	}
	read_len = fread(buffer, sizeof(uint8_t), 4096, f);
	if (read_len <= 0) {
		LOGERR("Read size during try decrypt failed\n");
		fclose(f);
		return -1;
	}
	if (oaes_decrypt(ctx, buffer, read_len, NULL, &out_len) != OAES_RET_SUCCESS) {
		LOGERR("Error: Failed to retrieve required buffer size for trying decryption.\n");
		fclose(f);
		return -1;
	}
	buffer_out = (uint8_t *) calloc(out_len, sizeof(char));
	if (buffer_out == NULL) {
		LOGERR("Failed to allocate output buffer for try decrypt.\n");
		fclose(f);
		return -1;
	}
	if (oaes_decrypt(ctx, buffer, read_len, buffer_out, &out_len) != OAES_RET_SUCCESS) {
		LOGERR("Failed to decrypt file '%s'\n", fn.c_str());
		fclose(f);
		free(buffer_out);
		return 0;
	}
	fclose(f);
	if (out_len < 2) {
		LOGINFO("Successfully decrypted '%s' but read length %i too small.\n", fn.c_str(), out_len);
		free(buffer_out);
		return 1; // Decrypted successfully
	}
	ptr = buffer_out;
	firstbyte = *ptr & 0xff;
	ptr++;
	secondbyte = *ptr & 0xff;
	if (firstbyte == 0x1f && secondbyte == 0x8b) {
		LOGINFO("Successfully decrypted '%s' and file is compressed.\n", fn.c_str());
		free(buffer_out);
		return 3; // Compressed
	}
	if (out_len >= 262) {
		ptr = buffer_out + 257;
		if (strncmp((char*)ptr, "ustar", 5) == 0) {
			LOGINFO("Successfully decrypted '%s' and file is tar format.\n", fn.c_str());
			free(buffer_out);
			return 2; // Tar
		}
	}
	free(buffer_out);
	LOGINFO("No errors decrypting '%s' but no known file format.\n", fn.c_str());
	return 1; // Decrypted successfully
#else
	LOGERR("Encrypted backup support not included.\n");
	return -1;
#endif
}
Exemplo n.º 4
0
int main(int argc, char** argv)
{
  size_t _i;
  OAES_CTX * ctx = NULL;
  uint8_t *_encbuf, *_decbuf, *_key_data = NULL, *_bin_data = NULL;
  size_t _encbuf_len, _decbuf_len, _buf_len;
  size_t _key_data_len = 0, _bin_data_len = 0;
  char *_buf;
  short _is_ecb = 0, _is_bin = 0;
  char * _text = NULL, * _key_text = NULL;
  int _key_len = 128;
  uint8_t _iv[OAES_BLOCK_SIZE] = "";
  uint8_t _pad = 0;
  
  if( argc < 2 )
  {
    usage( argv[0] );
    return EXIT_FAILURE;
  }

  for( _i = 1; _i < argc; _i++ )
  {
    int _found = 0;

    if( 0 == strcmp( argv[_i], "-nostep" ) )
    {
      _found = 1;
      _is_step = 0;
    }

    if( 0 == strcmp( argv[_i], "-ecb" ) )
    {
      _found = 1;
      _is_ecb = 1;
    }
    
    if( 0 == strcmp( argv[_i], "-bin" ) )
    {
      _found = 1;
      _is_bin = 1;
    }
    
    if( 0 == strcmp( argv[_i], "-key" ) )
    {
      _found = 1;
      _i++; // len
      if( _i >= argc )
      {
        printf("Error: No value specified for '-%s'.\n",
            "key");
        usage( argv[0] );
        return EXIT_FAILURE;
      }
      _key_len = atoi( argv[_i] );
      switch( _key_len )
      {
        case 128:
        case 192:
        case 256:
          break;
        default:
          _key_text = argv[_i];
          if( to_binary( NULL, &_key_data_len, _key_text ) )
          {
            printf( "Error: Invalid value [%s] specified for '-%s'.\n",
                argv[_i], "key" );
            return EXIT_FAILURE;
          }
          switch( _key_data_len )
          {
            case 16:
            case 24:
            case 32:
              break;
            default:
              printf("Error: key_data [%s] specified for '-%s' has an invalid "
                  "size.\n", argv[_i], "key");
              usage( argv[0] );
              return EXIT_FAILURE;
          }
      }
    }
    
    if( 0 == _found )
    {
      if( _text )
      {
        printf("Error: Invalid option '%s'.\n", argv[_i]);
        usage( argv[0] );
        return EXIT_FAILURE;
      }
      else
      {
        _text = argv[_i];
        if( _is_bin && to_binary( NULL, &_bin_data_len, _text ) )
        {
          printf( "Error: Invalid value [%s] specified for '-%s'.\n",
              argv[_i], "bin" );
          return EXIT_FAILURE;
        }
      }
    }      
  }

  if( NULL == _text )
  {
    usage( argv[0] );
    return EXIT_FAILURE;
  }

  if( _is_step )
    printf( "\nEnabling step mode, press Return to step.\n\n" );

  if( _is_bin )
  {
    _bin_data = (uint8_t *) calloc(_bin_data_len, sizeof(uint8_t));
    if( NULL == _bin_data )
    {
      printf( "Error: Failed to allocate memory.\n" );
      return EXIT_FAILURE;
    }
    if( to_binary( _bin_data, &_bin_data_len, _text ) )
    {
      printf( "Error: Could not load data [%s].\n", _text);
      free( _bin_data );
      return EXIT_FAILURE;
    }
  }
  else
  {
    oaes_sprintf( NULL, &_buf_len, (const uint8_t *)_text, strlen(_text));
    _buf = (char *) calloc(_buf_len, sizeof(char));
    printf( "\n***** plaintext  *****\n" );
    if( _buf )
    {
      oaes_sprintf( _buf, &_buf_len,
          (const uint8_t *)_text, strlen( _text ) );
      printf( "%s", _buf );
    }
    printf( "\n**********************\n" );
    free( _buf );
  }
  
  ctx = oaes_alloc();
  if( NULL == ctx )
  {
    printf("Error: Failed to initialize OAES.\n");
    if( _bin_data )
      free( _bin_data );
    return EXIT_FAILURE;
  }
  if( OAES_RET_SUCCESS != oaes_set_option( ctx, OAES_OPTION_STEP_ON, step_cb ) )
    printf("Error: Failed to set OAES options.\n");
  if( _is_ecb )
    if( OAES_RET_SUCCESS != oaes_set_option( ctx, OAES_OPTION_ECB, NULL ) )
      printf("Error: Failed to set OAES options.\n");

  if( _key_text )
  {
    _key_data = (uint8_t *) calloc(_key_data_len, sizeof(uint8_t));
    if( NULL == _key_data )
    {
      printf( "Error: Failed to allocate memory.\n" );
      if( _bin_data )
        free( _bin_data );
      return EXIT_FAILURE;
    }
    if( to_binary( _key_data, &_key_data_len, _key_text ) )
    {
      printf( "Error: Could not load key [%s].\n", _key_text);
      free( _key_data );
      return EXIT_FAILURE;
    }
    oaes_key_import_data( ctx, _key_data, _key_data_len );
  }
  else
    switch( _key_len )
    {
      case 128:
        if( OAES_RET_SUCCESS != oaes_key_gen_128(ctx) )
          printf("Error: Failed to generate OAES %d bit key.\n", _key_len);
        break;
      case 192:
        if( OAES_RET_SUCCESS != oaes_key_gen_192(ctx) )
          printf("Error: Failed to generate OAES %d bit key.\n", _key_len);
        break;
      case 256:
        if( OAES_RET_SUCCESS != oaes_key_gen_256(ctx) )
          printf("Error: Failed to generate OAES %d bit key.\n", _key_len);
        break;
      default:
        break;
    }

  if( _bin_data )
  {
    if( OAES_RET_SUCCESS != oaes_encrypt( ctx,
        _bin_data, _bin_data_len, NULL, &_encbuf_len, NULL, NULL ) )
      printf("Error: Failed to retrieve required buffer size for encryption.\n");
    _encbuf = (uint8_t *) calloc(_encbuf_len, sizeof(uint8_t));
    if( NULL == _encbuf )
    {
      printf( "Error: Failed to allocate memory.\n" );
      if( _key_data )
        free( _key_data );
      free( _bin_data );
      return EXIT_FAILURE;
    }
    printf( "\n" );
    if( OAES_RET_SUCCESS != oaes_encrypt( ctx,
        _bin_data, _bin_data_len, _encbuf, &_encbuf_len, _iv, &_pad ) )
      printf("Error: Encryption failed.\n");
    printf( "\n**********************\n\n" );
  }
  else
  {
    if( OAES_RET_SUCCESS != oaes_encrypt( ctx,
        (const uint8_t *)_text, strlen( _text ), NULL, &_encbuf_len,
        NULL, NULL ) )
      printf("Error: Failed to retrieve required buffer size for encryption.\n");
    _encbuf = (uint8_t *) calloc(_encbuf_len, sizeof(uint8_t));
    if( NULL == _encbuf )
    {
      printf( "Error: Failed to allocate memory.\n" );
      if( _key_data )
        free( _key_data );
      return EXIT_FAILURE;
    }
    printf( "\n" );
    if( OAES_RET_SUCCESS != oaes_encrypt( ctx,
        (const uint8_t *)_text, strlen( _text ), _encbuf, &_encbuf_len,
        _iv, &_pad ))
      printf("Error: Encryption failed.\n");
    printf( "\n**********************\n\n" );
  }

  if( OAES_RET_SUCCESS != oaes_decrypt( ctx,
      _encbuf, _encbuf_len, NULL, &_decbuf_len, NULL, NULL ) )
    printf("Error: Failed to retrieve required buffer size for encryption.\n");
  _decbuf = (uint8_t *) calloc(_decbuf_len, sizeof(uint8_t));
  if( NULL == _decbuf )
  {
    printf( "Error: Failed to allocate memory.\n" );
    if( _key_data )
      free( _key_data );
    if( _bin_data )
      free( _bin_data );
    free( _encbuf );
    return EXIT_FAILURE;
  }
  if( OAES_RET_SUCCESS != oaes_decrypt( ctx,
      _encbuf, _encbuf_len, _decbuf, &_decbuf_len, _iv, _pad ) )
    printf("Error: Decryption failed.\n");

  if( OAES_RET_SUCCESS !=  oaes_free( &ctx ) )
    printf("Error: Failed to uninitialize OAES.\n");
  
  oaes_sprintf( NULL, &_buf_len, _encbuf, _encbuf_len );
  _buf = (char *) calloc(_buf_len, sizeof(char));
  printf( "\n***** cyphertext *****\n" );
  if( _buf )
  {
    oaes_sprintf( _buf, &_buf_len, _encbuf, _encbuf_len );
    printf( "%s", _buf );
  }
  printf( "\n**********************\n" );
  free( _buf );
  
  oaes_sprintf( NULL, &_buf_len, _decbuf, _decbuf_len );
  _buf = (char *) calloc(_buf_len, sizeof(char));
  printf( "\n***** plaintext  *****\n" );
  if( _buf )
  {
    oaes_sprintf( _buf, &_buf_len, _decbuf, _decbuf_len );
    printf( "%s", _buf );
  }
  printf( "\n**********************\n\n" );
  free( _buf );
  
  free( _encbuf );
  free( _decbuf );
  if( _key_data )
    free( _key_data );
  if( _bin_data )
    free( _bin_data );

  return (EXIT_SUCCESS);
}
int main(int argc, char** argv)
{
	size_t _i;
	OAES_CTX * ctx = NULL;
	uint8_t *_encbuf, *_decbuf;
	size_t _encbuf_len, _decbuf_len, _buf_len;
	char *_buf;
	short _is_ecb = 0;
	char * _text = NULL;
	int _key_len = 128;
	
	if( argc < 2 )
	{
		usage( argv[0] );
		return EXIT_FAILURE;
	}

	for( _i = 1; _i < argc; _i++ )
	{
		int _found = 0;
		
		if( 0 == strcmp( argv[_i], "-ecb" ) )
		{
			_found = 1;
			_is_ecb = 1;
		}
		
		if( 0 == strcmp( argv[_i], "-key" ) )
		{
			_found = 1;
			_i++; // len
			if( _i >= argc )
			{
				printf("Error: No value specified for '-%s'.\n",
						"key");
				usage( argv[0] );
				return EXIT_FAILURE;
			}
			_key_len = atoi( argv[_i] );
			switch( _key_len )
			{
				case 128:
				case 192:
				case 256:
					break;
				default:
					printf("Error: Invalid value [%d] specified for '-%s'.\n",
							_key_len, "key");
					usage( argv[0] );
					return EXIT_FAILURE;
			}
		}
		
		if( 0 == _found )
		{
			if( _text )
			{
				printf("Error: Invalid option '%s'.\n", argv[_i]);
				usage( argv[0] );
				return EXIT_FAILURE;
			}
			else
			{
				_text = (char *) calloc(strlen( argv[_i] ) + 1, sizeof(char));
				if( NULL == _text )
				{
					printf("Error: Failed to allocate memory.\n", argv[_i]);
					return EXIT_FAILURE;
				}
				strcpy( _text, argv[_i] );
			}
		}			
	}

	if( NULL == _text )
	{
		usage( argv[0] );
		return EXIT_FAILURE;
	}

	oaes_sprintf( NULL, &_buf_len,
			(const uint8_t *)_text, strlen( _text ) );
	_buf = (char *) calloc(_buf_len, sizeof(char));
	printf( "\n***** plaintext  *****\n" );
	if( _buf )
	{
		oaes_sprintf( _buf, &_buf_len,
				(const uint8_t *)_text, strlen( _text ) );
		printf( "%s", _buf );
	}
	printf( "\n**********************\n" );
	free( _buf );
	
	ctx = oaes_alloc();
	if( NULL == ctx )
	{
		printf("Error: Failed to initialize OAES.\n");
		free( _text );
		return EXIT_FAILURE;
	}
	if( _is_ecb )
		if( OAES_RET_SUCCESS != oaes_set_option( ctx, OAES_OPTION_ECB, NULL ) )
			printf("Error: Failed to set OAES options.\n");
	switch( _key_len )
	{
		case 128:
			if( OAES_RET_SUCCESS != oaes_key_gen_128(ctx) )
				printf("Error: Failed to generate OAES %d bit key.\n", _key_len);
			break;
		case 192:
			if( OAES_RET_SUCCESS != oaes_key_gen_192(ctx) )
				printf("Error: Failed to generate OAES %d bit key.\n", _key_len);
			break;
		case 256:
			if( OAES_RET_SUCCESS != oaes_key_gen_256(ctx) )
				printf("Error: Failed to generate OAES %d bit key.\n", _key_len);
			break;
		default:
			break;
	}

	if( OAES_RET_SUCCESS != oaes_encrypt( ctx,
			(const uint8_t *)_text, strlen( _text ), NULL, &_encbuf_len ) )
		printf("Error: Failed to retrieve required buffer size for encryption.\n");
	_encbuf = (uint8_t *) calloc( _encbuf_len, sizeof(uint8_t) );
	if( NULL == _encbuf )
	{
		printf( "Error: Failed to allocate memory.\n" );
		free( _text );
		return EXIT_FAILURE;
	}
	if( OAES_RET_SUCCESS != oaes_encrypt( ctx,
			(const uint8_t *)_text, strlen( _text ), _encbuf, &_encbuf_len ) )
		printf("Error: Encryption failed.\n");

	if( OAES_RET_SUCCESS != oaes_decrypt( ctx,
			_encbuf, _encbuf_len, NULL, &_decbuf_len ) )
		printf("Error: Failed to retrieve required buffer size for encryption.\n");
	_decbuf = (uint8_t *) calloc( _decbuf_len, sizeof(uint8_t) );
	if( NULL == _decbuf )
	{
		printf( "Error: Failed to allocate memory.\n" );
		free( _text );
		free( _encbuf );
		return EXIT_FAILURE;
	}
	if( OAES_RET_SUCCESS != oaes_decrypt( ctx,
			_encbuf, _encbuf_len, _decbuf, &_decbuf_len ) )
		printf("Error: Decryption failed.\n");

	if( OAES_RET_SUCCESS !=  oaes_free( &ctx ) )
		printf("Error: Failed to uninitialize OAES.\n");
	
	oaes_sprintf( NULL, &_buf_len, _encbuf, _encbuf_len );
	_buf = (char *) calloc(_buf_len, sizeof(char));
	printf( "\n***** cyphertext *****\n" );
	if( _buf )
	{
		oaes_sprintf( _buf, &_buf_len, _encbuf, _encbuf_len );
		printf( "%s", _buf );
	}
	printf( "\n**********************\n" );
	free( _buf );
	
	oaes_sprintf( NULL, &_buf_len, _decbuf, _decbuf_len );
	_buf = (char *) calloc(_buf_len, sizeof( char));
	printf( "\n***** plaintext  *****\n" );
	if( _buf )
	{
		oaes_sprintf( _buf, &_buf_len, _decbuf, _decbuf_len );
		printf( "%s", _buf );
	}
	printf( "\n**********************\n\n" );
	free( _buf );
	
	free( _encbuf );
	free( _decbuf );
	free( _text );

	return (EXIT_SUCCESS);
}