Esempio n. 1
0
void CipherKeyImpl::generateKey()
{
	ByteVec vec;

	getRandomBytes(vec, keySize());
	setKey(vec);
	
	getRandomBytes(vec, ivSize());
	setIV(vec);
}
Esempio n. 2
0
bool CCrypt::generateKey(void)
{
	BYTE tmp[KEY_LENGTH], tmpiv[KEY_LENGTH];
	if (!getRandomBytes(tmp, sizeof(tmp)) || !getRandomBytes(tmpiv, sizeof(tmpiv)))
		return false;

	memcpy(m_key, tmp, KEY_LENGTH);
	memcpy(m_iv, tmpiv, KEY_LENGTH);
	init_cbc_14(m_key, m_ctx, m_iv, _countof(m_iv));

	SecureZeroMemory(tmp, _countof(tmp)); SecureZeroMemory(tmpiv, _countof(tmpiv));

	return m_valid = true;
}
Esempio n. 3
0
TYPED_TEST_P(TcTest, ParallelRdWrAFile)
{
	const char *PATH = "TcTest-ParallelRdWrAFile.dat";
	const int T = 6;  /* # of threads */
	const int S = 4096;
	tc_touch(PATH, T * S);

	struct tc_iovec iovs[T];
	char *data1 = getRandomBytes(T * S);
	char *data2 = (char *)malloc(T * S);
	for (int i = 0; i < 1; ++i) { // repeat for 10 times
		for (int t = 0; t < T; ++t) {
			tc_iov2path(&iovs[t], PATH, t * S, S, data1 + t * S);
		}
		DoParallel(T, [&iovs](int i) {
			EXPECT_OK(tc_writev(&iovs[i], 1, false));
		});

		for (int t = 0; t < T; ++t) {
			iovs[t].data = data2 + t * S;
		}
		DoParallel(T, [&iovs](int i) {
			EXPECT_OK(tc_readv(&iovs[i], 1, false));
		});
		EXPECT_EQ(0, memcmp(data1, data2, T * S));
	}

	free(data1);
	free(data2);
}
Esempio n. 4
0
/**
 * TC-Read and Write test using
 * File path
 */
TYPED_TEST_P(TcTest, WritevCanCreateFiles)
{
	const char *PATHS[] = { "WritevCanCreateFiles1.txt",
				"WritevCanCreateFiles2.txt",
				"WritevCanCreateFiles3.txt",
				"WritevCanCreateFiles4.txt" };
	const int count = sizeof(PATHS)/sizeof(PATHS[0]);

	Removev(PATHS, count);

	tc_iovec *writev = (tc_iovec *)malloc(sizeof(tc_iovec) * count);
	for (int i = 0; i < count; ++i) {
		tc_iov4creation(&writev[i], PATHS[i], 4096,
				getRandomBytes(4096));
	}

	EXPECT_OK(tc_writev(writev, count, false));

	tc_iovec *readv = (tc_iovec *)malloc(sizeof(tc_iovec) * count);
	for (int i = 0; i < count; ++i) {
		tc_iov2path(&readv[i], PATHS[i], 0, 4096,
			    (char *)malloc(4096));
	}

	EXPECT_OK(tc_readv(readv, count, false));

	EXPECT_TRUE(compare_content(writev, readv, count));

	free_iovec(writev, count);
	free_iovec(readv, count);
}
Esempio n. 5
0
TYPED_TEST_P(TcTest, ShuffledRdWr)
{
	const char *PATH = "TcTest-ShuffledRdWr.dat";
	const int N = 8;  /* size of iovs */
	struct tc_iovec iovs[N];
	const int S = 4096;
	tc_touch(PATH, N * S);

	char *data1 = getRandomBytes(N * S);
	char *data2 = (char *)malloc(N * S);
	std::vector<int> offsets(N);
	std::iota(offsets.begin(), offsets.end(), 0);
	std::mt19937 rng(8887);
	for (int i = 0; i < 10; ++i) { // repeat for 10 times
		for (int n = 0; n < N; ++n) {
			tc_iov2path(&iovs[n], PATH, offsets[n] * S, S,
				    data1 + offsets[n] * S);
		}
		EXPECT_OK(tc_writev(iovs, N, false));

		for (int n = 0; n < N; ++n) {
			iovs[n].data = data2 + offsets[n] * S;
		}
		EXPECT_OK(tc_readv(iovs, N, false));
		EXPECT_EQ(0, memcmp(data1, data2, N * S));

		std::shuffle(offsets.begin(), offsets.end(), rng);
	}

	free(data1);
	free(data2);
}
Esempio n. 6
0
TYPED_TEST_P(TcTest, RdWrLargeThanRPCLimit)
{
	struct tc_iovec iov;
	char* data1 = getRandomBytes(2_MB);
	tc_iov4creation(&iov, "TcTest-WriteLargeThanRPCLimit.dat", 2_MB, data1);

	EXPECT_OK(tc_writev(&iov, 1, false));
	EXPECT_EQ(2_MB, iov.length);

	char* data2 = (char *)malloc(2_MB);
	iov.is_creation = false;
	iov.data = data2;
	for (size_t s = 8_KB; s <= 2_MB; s += 8_KB) {
		iov.length = s;
		EXPECT_OK(tc_readv(&iov, 1, false));
		EXPECT_EQ(iov.length == 2_MB, iov.is_eof);
		EXPECT_EQ(s, iov.length);
		EXPECT_EQ(0, memcmp(data1, data2, s));
		if (s % 128_KB == 0)
			fprintf(stderr, "read size: %llu\n", s);
	}

	free(data1);
	free(data2);
}
Esempio n. 7
0
CCrypt::~CCrypt()
{
	if (m_valid)
	{
		free_ecb(m_ctx);
		getRandomBytes(m_key, KEY_LENGTH);
	}
}
Esempio n. 8
0
int RandomDevice::getRandomBytesNonBlocking(unsigned char *buffer,
                                            size_t         numBytes)
{
#ifdef BDLB_USE_WIN_CRYPT
    return getRandomBytes(buffer, numBytes);
#else
    return readFile(buffer, numBytes, "/dev/urandom");
#endif
}
Esempio n. 9
0
bool CStdCrypt::generateKey(void)
{
	BYTE tmp[KEY_LENGTH];
	if (!getRandomBytes(tmp, sizeof(tmp)))
		return false;

	memcpy(m_key, tmp, KEY_LENGTH);
	m_aes.MakeKey(m_key, "Miranda", KEY_LENGTH, BLOCK_SIZE);
	return m_valid = true;
}
Esempio n. 10
0
int64_t HHVM_FUNCTION(random_int, int64_t min, int64_t max) {
  if (min > max) {
    SystemLib::throwErrorObject(
      "Minimum value must be less than or equal to the maximum value");
  }

  if (min == max) {
    return min;
  }

  uint64_t umax = max - min;
  uint64_t result;
  if (!getRandomBytes(&result, sizeof(result))) {
    SystemLib::throwErrorObject("Could not gather sufficient random data");
  }

  // Special case where no modulus is required
  if (umax == std::numeric_limits<uint64_t>::max()) {
    return result;
  }

  // Increment the max so the range is inclusive of max
  umax++;

  // Powers of two are not biased
  if ((umax & (umax - 1)) != 0) {
    // Ceiling under which std::numeric_limits<uint64_t>::max() % max == 0
    int64_t limit = std::numeric_limits<uint64_t>::max() -
      (std::numeric_limits<uint64_t>::max() % umax) - 1;

    // Discard numbers over the limit to avoid modulo bias
    while (result > limit) {
      if (!getRandomBytes(&result, sizeof(result))) {
        SystemLib::throwErrorObject(
          "Could not gather sufficient random data");
      }
    }
  }

  return (int64_t)((result % umax) + min);
}
Esempio n. 11
0
String HHVM_FUNCTION(random_bytes, int64_t length) {
  if (length < 1) {
    SystemLib::throwErrorObject("Length must be greater than 0");
  }

  String ret(length, ReserveString);
  if (!getRandomBytes(ret.mutableData(), ret.capacity())) {
    SystemLib::throwErrorObject("Could not gather sufficient random data");
  }

  return ret.setSize(length);
}
Esempio n. 12
0
/**
 * TC-Set/Get Attributes test
 * with symlinks
 */
TYPED_TEST_P(TcTest, AttrsTestSymlinks)
{
	const char *PATHS[] = { "AttrsTestSymlinks-Linked1.txt",
				"AttrsTestSymlinks-Linked2.txt",
				"AttrsTestSymlinks-Linked3.txt" };
	const char *LPATHS[] = { "AttrsTestSymlinks-Link1.txt",
				 "AttrsTestSymlinks-Link2.txt",
				 "AttrsTestSymlinks-Link3.txt" };
	tc_res res = { 0 };
	struct tc_iovec iov;
	int i;
	const int count = 3;
	struct tc_attrs *attrs1 = (tc_attrs *)calloc(count, sizeof(tc_attrs));
	struct tc_attrs *attrs2 = (tc_attrs *)calloc(count, sizeof(tc_attrs));

	EXPECT_NOTNULL(attrs1);
	EXPECT_NOTNULL(attrs2);

	Removev(PATHS, count);
	Removev(LPATHS, count);

	EXPECT_OK(tc_symlinkv(PATHS, LPATHS, count, false));

	for (i = 0; i < count; ++i) {
		tc_iov4creation(&iov, PATHS[i], 100, getRandomBytes(100));
		EXPECT_NOTNULL(iov.data);
		EXPECT_OK(tc_writev(&iov, 1, false));

		attrs1[i].file = tc_file_from_path(LPATHS[i]);
		tc_attrs_set_mode(&attrs1[i], S_IRUSR);
		tc_attrs_set_atime(&attrs1[i], totimespec(time(NULL), 0));
		attrs2[i] = attrs1[i];
	}

	EXPECT_OK(tc_setattrsv(attrs1, count, false));
	EXPECT_OK(tc_getattrsv(attrs2, count, false));
	EXPECT_TRUE(compare_attrs(attrs1, attrs2, count));

	tc_attrs_set_mode(&attrs1[0], S_IRUSR | S_IRGRP);
	EXPECT_OK(tc_setattrsv(attrs1, count, false));
	EXPECT_OK(tc_lgetattrsv(attrs2, count, false));

	EXPECT_FALSE(S_IROTH & attrs1[0].mode);
	EXPECT_TRUE(S_IROTH & attrs2[0].mode);
	EXPECT_FALSE(compare_attrs(attrs1, attrs2, count));

	EXPECT_OK(tc_getattrsv(attrs2, count, false));
	EXPECT_TRUE(compare_attrs(attrs1, attrs2, count));

	free(attrs1);
	free(attrs2);
}
Esempio n. 13
0
TYPED_TEST_P(TcTest, SuccessiveReads)
{
	const char *path = "TcTest-SuccesiveReads.txt";
	struct tc_iovec iov;
	const int N = 4096;
	char *data;
	char *read;
	tc_file *tcf;

	Removev(&path, 1);

	data = (char *)getRandomBytes(5 * N);
	tc_iov4creation(&iov, path, 5 * N, data);

	EXPECT_OK(tc_writev(&iov, 1, false));

	read = (char *)malloc(5 * N);
	EXPECT_NOTNULL(read);

	tcf = tc_open(path, O_RDONLY, 0);
	EXPECT_EQ(0, tc_fseek(tcf, 0, SEEK_CUR));
	EXPECT_NOTNULL(tcf);
	tc_iov2file(&iov, tcf, TC_OFFSET_CUR, N, read);
	EXPECT_OK(tc_readv(&iov, 1, false));
	EXPECT_EQ(N, tc_fseek(tcf, 0, SEEK_CUR));

	iov.data = read + N;
	EXPECT_OK(tc_readv(&iov, 1, false));
	EXPECT_EQ(2 * N, tc_fseek(tcf, 0, SEEK_CUR));

	EXPECT_EQ(3 * N, tc_fseek(tcf, N, SEEK_CUR));
	iov.data = read + 3 * N;
	EXPECT_OK(tc_readv(&iov, 1, false));

	EXPECT_EQ(2 * N, tc_fseek(tcf, 2 * N, SEEK_SET));
	iov.data = read + 2 * N;
	EXPECT_OK(tc_readv(&iov, 1, false));

	EXPECT_EQ(4 * N, tc_fseek(tcf, -N, SEEK_END));
	iov.data = read + 4 * N;
	EXPECT_OK(tc_readv(&iov, 1, false));
	EXPECT_TRUE(iov.is_eof);

	EXPECT_EQ(0, memcmp(data, read, 5 * N));

	free(data);
	free(read);
	tc_close(tcf);
}
Esempio n. 14
0
int maStartHost(MaHost *host)
{
    char    ascii[MA_MAX_SECRET * 2 + 1];

    if (getRandomBytes(host, ascii, sizeof(ascii)) < 0) {
        //  Use the best we can get. getRandomBytes will report.
    }
    host->secret = mprStrdup(host, ascii);

#if BLD_FEATURE_ACCESS_LOG
    return maStartAccessLogging(host);
#else
    return 0;
#endif
}
Esempio n. 15
0
static void CopyOrDupFiles(const char *dir, bool copy, int nfiles)
{
	const int N = 4096;
	std::vector<struct tc_extent_pair> pairs(nfiles);
	std::vector<struct tc_iovec> iovs(nfiles);
	std::vector<struct tc_iovec> read_iovs(nfiles);
	std::vector<std::string> src_paths(nfiles);
	std::vector<std::string> dst_paths(nfiles);
	char buf[PATH_MAX];

	EXPECT_TRUE(tc_rm_recursive(dir));
	EXPECT_OK(tc_ensure_dir(dir, 0755, NULL));

	for (int i = 0; i < nfiles; ++i) {
		src_paths[i].assign(
		    buf, snprintf(buf, PATH_MAX, "%s/src-%d.txt", dir, i));
		dst_paths[i].assign(
		    buf, snprintf(buf, PATH_MAX, "%s/dst-%d.txt", dir, i));
		tc_fill_extent_pair(&pairs[i], src_paths[i].c_str(), 0,
				    dst_paths[i].c_str(), 0, N);

		tc_iov4creation(&iovs[i], pairs[i].src_path, N,
				getRandomBytes(N));
		EXPECT_NOTNULL(iovs[i].data);

		tc_iov2path(&read_iovs[i], pairs[i].dst_path, 0, N,
			    (char *)malloc(N));
		EXPECT_NOTNULL(read_iovs[i].data);
	}

	EXPECT_OK(tc_writev(iovs.data(), nfiles, false));

	// copy or move files
	if (copy) {
		EXPECT_OK(tc_copyv(pairs.data(), nfiles, false));
	} else {
		EXPECT_OK(tc_dupv(pairs.data(), nfiles, false));
	}

	EXPECT_OK(tc_readv(read_iovs.data(), nfiles, false));

	compare_content(iovs.data(), read_iovs.data(), nfiles);

	for (int i = 0; i < nfiles; ++i) {
		free(iovs[i].data);
		free(read_iovs[i].data);
	}
}
Esempio n. 16
0
KeyGenError keygen_createKey(UByte* buffer, const unsigned int length, enum Format format)
{
    if( buffer == NULL || length < KEY_MIN_LENGTH )
    {
        return KG_ERR_ILL_ARGUMENT;
    }
    
    KeyGenError rtn;
    UByte random[length];
    int err = getRandomBytes(random, length);
    
    if( err == ERR_LIB_NONE )
    {
        bool error = false;
        
        switch(format)
        {
            case ASCII:
                transformAscii(random, length);
                break;
            case ASCII_BLANKS:
                transformAsciiBlanks(random, length);
                break;
            default:
                rtn = KG_ERR_ILL_ARGUMENT;
                error = true;
                break;
        }
        
        if( error == false )
        {
            memcpy(buffer, random, length);
            rtn = KG_ERR_SUCCESS;
        }
        else
        {
            keygen_cleanBuffer(random, length);
        }
    }
    else
    {
        rtn = KG_ERR_SECURITY;
    }
    
    keygen_cleanBuffer(random, length);
    
    return rtn;
}
Esempio n. 17
0
bool CStdCrypt::getKey(BYTE *pKey, size_t cbKeyLen)
{
	if (!m_valid || cbKeyLen < sizeof(ExternalKey))
		return false;

	ExternalKey tmp = { 0 };
	memcpy(&tmp.m_key, m_key, KEY_LENGTH);
	tmp.m_crc32 = crc32(0xAbbaDead, (LPCBYTE)m_password.GetString(), m_password.GetLength());
	getRandomBytes(tmp.slack, sizeof(tmp.slack));

	BYTE tmpHash[32];
	slow_hash(m_password, m_password.GetLength(), tmpHash);

	CRijndael tmpAes;
	tmpAes.MakeKey(tmpHash, tmpAes.sm_chain0, KEY_LENGTH, BLOCK_SIZE);
	tmpAes.Encrypt(&tmp, pKey, sizeof(tmp));
	return true;
}
Esempio n. 18
0
static void tc_touchv(const char **paths, int count, int filesize)
{
	tc_iovec *iovs;
	char *buf;

	iovs = (tc_iovec *)alloca(count * sizeof(*iovs));
	buf = filesize ? getRandomBytes(filesize) : NULL;

	for (int i = 0; i < count; ++i) {
		tc_iov4creation(&iovs[i], paths[i], filesize, buf);
	}

	EXPECT_OK(tc_writev(iovs, count, false));

	if (buf) {
		free(buf);
	}
}
Esempio n. 19
0
TYPED_TEST_P(TcTest, SessionTimeout)
{
	const char *path = "SessionTimeout.dat";
	struct tc_iovec iov;
	int size = 4096;
	char *data1 = getRandomBytes(size);

	tc_iov4creation(&iov, path, size, data1);

	EXPECT_OK(tc_writev(&iov, 1, false));

	sleep(60);

	tc_iov2path(&iov, path, 0, size, data1);

	EXPECT_OK(tc_readv(&iov, 1, false));

	free(data1);
}
Esempio n. 20
0
TYPED_TEST_P(TcTest, CopyFirstHalfAsSecondHalf)
{
	const int N = 8096;
	struct tc_extent_pair pairs[2];
	struct tc_iovec iov;
	struct tc_iovec read_iov;

	pairs[0].src_path = "OriginalFile.txt";
	pairs[0].src_offset = 0;
	pairs[0].dst_path = "ReversedFile.txt";
	pairs[0].dst_offset = N / 2;
	pairs[0].length = N / 2;

	pairs[1].src_path = "OriginalFile.txt";
	pairs[1].src_offset = N / 2;
	pairs[1].dst_path = "ReversedFile.txt";
	pairs[1].dst_offset = 0;
	pairs[1].length = UINT64_MAX;  // from src_offset to EOF, i.e., N/2

	// create source files
	tc_iov4creation(&iov, pairs[0].src_path, N, getRandomBytes(N));
	EXPECT_NOTNULL(iov.data);
	EXPECT_OK(tc_writev(&iov, 1, false));

	// remove dest files
	Removev(&pairs[0].dst_path, 1);

	// reverse a file using copy
	EXPECT_OK(tc_copyv(pairs, 2, false));

	tc_iov2path(&read_iov, pairs[1].dst_path, 0, N, (char *)malloc(N));
	EXPECT_NOTNULL(read_iov.data);

	EXPECT_OK(tc_readv(&read_iov, 1, false));

	EXPECT_EQ(0, memcmp(iov.data, read_iov.data + N / 2, N / 2));
	EXPECT_EQ(0, memcmp(iov.data + N / 2, read_iov.data, N / 2));

	free(iov.data);
	free(read_iov.data);
}
Esempio n. 21
0
bool CCrypt::getKey(BYTE *pKey, size_t cbKeyLen)
{
	if (!m_valid || cbKeyLen < sizeof(ExternalKey))
		return false;

	ExternalKey tmp = { 0 };
	memcpy(&tmp.m_key, m_key, KEY_LENGTH);
	memcpy(&tmp.m_iv, m_iv, KEY_LENGTH);
	tmp.m_crc32 = crc32(0xAbbaDead, (LPCBYTE)m_password.GetString(), m_password.GetLength());
	getRandomBytes(tmp.slack, sizeof(tmp.slack));

	BYTE tmpHash[32];
	slow_hash(m_password, m_password.GetLength(), tmpHash);

	BYTE ctx[kCbc14ContextLen];
	init_cbc_14(tmpHash, ctx, iv0, _countof(iv0));
	bool val = !encrypt_cbc(ctx, (BYTE*)&tmp, pKey, cbKeyLen);
	free_cbc(ctx);

	return val;
}
Esempio n. 22
0
int main(int argc, char **argv)
{
    unsigned l_num = 1;
    unsigned i, j;
    
    if(argc > 1)
    {
        l_num = strtoul(argv[1], NULL, 10);
    }
    
    randfd = open("/dev/urandom", O_RDONLY);
    if(randfd == -1)
    {
        printf("No access to urandom\n");
        return -1;
    }
    
    uint32_t l_private[NUM_ECC_DIGITS];
    EccPoint l_public;
    
    for(i=0; i<l_num; ++i)
    {
        getRandomBytes((char *)l_private, NUM_ECC_DIGITS * sizeof(uint32_t));
        ecc_make_key(&l_public, l_private, l_private);
        
        printf("uint32_t private_%u[NUM_ECC_DIGITS] = {", i);
        vli_print(l_private);
        printf("};\n");

        printf("EccPoint public_%u = {\n", i);
        printf("    {");
        vli_print(l_public.x);
        printf("},\n");
        printf("    {");
        vli_print(l_public.y);
        printf("}};\n\n");
    }
    
    return 0;
}
Esempio n. 23
0
/**
 * Append test case
 */
TYPED_TEST_P(TcTest, Append)
{
	const char *PATH = "TcTest-Append.txt";
	int i = 0;
	const int N = 4096;
	struct stat st;
	char *data;
	char *data_read;
	struct tc_iovec iov;

	Removev(&PATH, 1);

	data = (char *)getRandomBytes(3 * N);
	data_read = (char *)malloc(3 * N);
	EXPECT_NOTNULL(data);
	EXPECT_NOTNULL(data_read);

	tc_iov4creation(&iov, PATH, N, data);

	EXPECT_OK(tc_writev(&iov, 1, false));

	for (i = 0; i < 2; ++i) {
		iov.offset = TC_OFFSET_END;
		iov.data = data + N * (i + 1);
		iov.is_creation = false;
		EXPECT_OK(tc_writev(&iov, 1, false));
	}

	iov.offset = 0;
	iov.length = 3 * N;
	iov.data = data_read;
	EXPECT_OK(tc_readv(&iov, 1, false));
	EXPECT_TRUE(iov.is_eof);
	EXPECT_EQ(3 * N, iov.length);
	EXPECT_EQ(0, memcmp(data, data_read, 3 * N));

	free(data);
	free(data_read);
}
Esempio n. 24
0
TYPED_TEST_P(TcTest, SuccessiveWrites)
{
	const char *path = "SuccesiveWrites.dat";
	char *data = (char *)getRandomBytes(16_KB);
	/**
	 * open file one for actual writing
	 * other descriptor to verify
	 */
	tc_file *tcf = tc_open(path, O_RDWR | O_CREAT, 0755);
	EXPECT_NOTNULL(tcf);
	tc_file *tcf2 = tc_open(path, O_RDONLY, 0);
	EXPECT_NE(tcf->fd, tcf2->fd);

	struct tc_iovec iov;
	tc_iov2file(&iov, tcf, TC_OFFSET_CUR, 4_KB, data);
	EXPECT_OK(tc_writev(&iov, 1, false));
	tc_iov2file(&iov, tcf, TC_OFFSET_CUR, 4_KB, data + 4_KB);
	EXPECT_OK(tc_writev(&iov, 1, false));

	char *readbuf = (char *)malloc(16_KB);
	tc_iov2file(&iov, tcf2, 0, 8_KB, readbuf);
	EXPECT_OK(tc_readv(&iov, 1, false));
	EXPECT_EQ(iov.length, 8_KB);
	EXPECT_EQ(0, memcmp(data, readbuf, 8_KB));

	tc_iov2file(&iov, tcf, TC_OFFSET_CUR, 8_KB, data + 8_KB);
	EXPECT_OK(tc_writev(&iov, 1, false));

	tc_iov2file(&iov, tcf2, 0, 16_KB, readbuf);
	EXPECT_OK(tc_readv(&iov, 1, false));
	EXPECT_EQ(iov.length, 16_KB);
	EXPECT_EQ(0, memcmp(data, readbuf, 16_KB));

	tc_close(tcf);
	tc_close(tcf2);
	free(data);
	free(readbuf);
}
CFStringRef 
SecCreateRecoveryPassword(void)
{
	CFStringRef result = NULL;
	CFErrorRef error = NULL;
 	CFDataRef encodedData = NULL;
    CFDataRef randData = getRandomBytes(16);
	int i;
	
	// base32FDE is a "private" base32 encoding, it has no 0/O or L/l/1 in it (it uses 8 and 9).
	SecTransformRef	encodeTrans = SecEncodeTransformCreate(CFSTR("base32FDE"), &error);
    if(error == NULL) {
		SecTransformSetAttribute(encodeTrans, kSecTransformInputAttributeName, randData, &error);
		if(error == NULL) encodedData = SecTransformExecute(encodeTrans, &error);
     	CFRelease(encodeTrans);
   	}
    CFRelease(randData);

	if(encodedData != NULL && error == NULL) {
        CFStringRef	b32string = CFStringCreateFromExternalRepresentation(kCFAllocatorDefault, encodedData, kCFStringEncodingMacRoman);
        CFMutableStringRef encodedString = CFStringCreateMutableCopy(kCFAllocatorDefault, 64, b32string);
       
        // Add some hyphens to make the generated password easier to use
        for(i = 4; i < 34; i += 5)  CFStringInsert(encodedString, i, CFSTR("-"));
        // Trim so the last section is 4 characters long
		CFStringDelete(encodedString, CFRangeMake(29,CFStringGetLength(encodedString)-29));
        result = CFStringCreateCopy(kCFAllocatorDefault, encodedString);
        CFRelease(encodedString);
        CFRelease(b32string);
        CFRelease(encodedData);
	} else {
        secDebug(ASL_LEVEL_ERR, "Failed to base32 encode random data for recovery password\n", NULL);
    }

	return result;
	
}
Esempio n. 26
0
TYPED_TEST_P(TcTest, CopyLargeDirectory)
{
	int i;
	int count;
	struct tc_attrs *contents;
	struct tc_attrs_masks masks = TC_ATTRS_MASK_NONE;
	struct tc_extent_pair *dir_copy_pairs = NULL;
	const char **oldpaths = NULL;
	const char **newpaths = NULL;
	struct tc_attrs *copied_attrs;
	char *dst_path;
	int file_count = 0;
	//Cannot be larger than 9999 or will not fit in str
	#define FILE_COUNT 10
	#define FILE_LENGTH_BYTES (100)
	struct tc_iovec iov[FILE_COUNT];

	EXPECT_OK(tc_ensure_dir("TcTest-CopyLargeDirectory", 0755, NULL));
	EXPECT_OK(tc_ensure_dir("TcTest-CopyLargeDirectory-Dest", 0755, NULL));

	for (i = 0; i < FILE_COUNT; i++) {
		char *path = (char*) alloca(PATH_MAX);
		char *str = (char*) alloca(5);
		sprintf(str, "%d", i);
		tc_path_join("TcTest-CopyLargeDirectory", str, path, PATH_MAX);
		tc_iov4creation(&iov[i], path, FILE_LENGTH_BYTES,
				getRandomBytes(FILE_LENGTH_BYTES));
		EXPECT_NOTNULL(iov[i].data);
	}
	EXPECT_OK(tc_writev(iov, FILE_COUNT, false));

	masks.has_mode = true;

	EXPECT_OK(tc_listdir("TcTest-CopyLargeDirectory", masks, 0, true,
			     &contents, &count));

	dir_copy_pairs = (struct tc_extent_pair *)alloca(
	    sizeof(struct tc_extent_pair) * count);
	copied_attrs =
	    (struct tc_attrs *)alloca(sizeof(struct tc_attrs) * count);

	for (i = 0; i < count; i++) {
		dst_path = (char *) malloc(sizeof(char) * PATH_MAX);
		const char *dst_suffix = contents[i].file.path;

		while (*dst_suffix++ != '/')

			tc_path_join("TcTest-CopyLargeDirectory-Dest",
				     dst_suffix, dst_path, PATH_MAX);

		if (!S_ISDIR(contents[i].mode)) {
			dir_copy_pairs[file_count].src_path =
			    contents[i].file.path;
			dir_copy_pairs[file_count].dst_path = dst_path;
			dir_copy_pairs[file_count].src_offset = 0;
			dir_copy_pairs[file_count].dst_offset = 0;
			dir_copy_pairs[file_count].length = 0;

			file_count++;
		} else {
			EXPECT_OK(tc_ensure_dir (dst_path, 0755, NULL));
			free(dst_path);
		}

	}


	EXPECT_OK(tc_copyv(dir_copy_pairs, file_count, false));
	for (i = 0; i < file_count; i++) {
		free((char *) dir_copy_pairs[i].dst_path);
	}
}