示例#1
0
static void
test_fuzz_once(struct base64_encode_ctx *encode,
	       struct base64_decode_ctx *decode,
	       size_t size, const uint8_t *input)
{
  size_t base64_len = BASE64_ENCODE_RAW_LENGTH (size);
  size_t out_len;
  uint8_t *base64 = xalloc (base64_len + 2);
  uint8_t *decoded = xalloc (size + 2);

  *base64++ = 0x12;
  base64[base64_len] = 0x34;

  *decoded++ = 0x56;
  decoded[size] = 0x78;

  out_len = base64_encode_update(encode, base64, size, input);
  ASSERT (out_len <= base64_len);
  out_len += base64_encode_final(encode, base64 + out_len);
  ASSERT (out_len == base64_len);
  ASSERT (base64[-1] == 0x12);
  ASSERT (base64[base64_len] == 0x34);

  ASSERT(base64_decode_update(decode, &out_len, decoded,
			      base64_len, base64));
  ASSERT(base64_decode_final(decode));
  ASSERT (out_len == size);
  ASSERT (decoded[-1] == 0x56);
  ASSERT (decoded[size] == 0x78);
  
  ASSERT(MEMEQ(size, input, decoded));
  free (base64 - 1);
  free (decoded - 1);
}
示例#2
0
/*
 * Create Server's accept key in *dst*.
 * *client_key* is the value of |Sec-WebSocket-Key| header field in
 * client's handshake and it must be length of 24.
 * *dst* must be at least BASE64_ENCODE_RAW_LENGTH(20)+1.
 */
void create_accept_key(char *dst, const char *client_key)
{
  uint8_t sha1buf[20], key_src[60];
  memcpy(key_src, client_key, 24);
  memcpy(key_src+24, WS_GUID, 36);
  sha1(sha1buf, key_src, sizeof(key_src));
  base64((uint8_t*)dst, sha1buf, 20);
  dst[BASE64_ENCODE_RAW_LENGTH(20)] = '\0';
}
示例#3
0
void
test_main(void)
{
  ASSERT(BASE64_ENCODE_LENGTH(0) == 0);   /* At most   4 bits */
  ASSERT(BASE64_ENCODE_LENGTH(1) == 2);   /* At most  12 bits */
  ASSERT(BASE64_ENCODE_LENGTH(2) == 3);   /* At most  20 bits */
  ASSERT(BASE64_ENCODE_LENGTH(3) == 4);   /* At most  28 bits */
  ASSERT(BASE64_ENCODE_LENGTH(4) == 6);   /* At most  36 bits */
  ASSERT(BASE64_ENCODE_LENGTH(5) == 7);   /* At most  44 bits */
  ASSERT(BASE64_ENCODE_LENGTH(12) == 16); /* At most 100 bits */
  ASSERT(BASE64_ENCODE_LENGTH(13) == 18); /* At most 108 bits */

  ASSERT(BASE64_DECODE_LENGTH(0) == 0); /* At most  6 bits */
  ASSERT(BASE64_DECODE_LENGTH(1) == 1); /* At most 12 bits */
  ASSERT(BASE64_DECODE_LENGTH(2) == 2); /* At most 18 bits */
  ASSERT(BASE64_DECODE_LENGTH(3) == 3); /* At most 24 bits */
  ASSERT(BASE64_DECODE_LENGTH(4) == 3); /* At most 30 bits */
  
  test_armor(&nettle_base64, LDATA(""), "");
  test_armor(&nettle_base64, LDATA("H"), "SA==");
  test_armor(&nettle_base64, LDATA("He"), "SGU=");
  test_armor(&nettle_base64, LDATA("Hel"), "SGVs");
  test_armor(&nettle_base64, LDATA("Hell"), "SGVsbA==");
  test_armor(&nettle_base64, LDATA("Hello"), "SGVsbG8=");
  test_armor(&nettle_base64, LDATA("Hello\0"), "SGVsbG8A");
  test_armor(&nettle_base64, LDATA("Hello?>>>"), "SGVsbG8/Pj4+");
  test_armor(&nettle_base64, LDATA("\xff\xff\xff\xff"), "/////w==");

  test_armor(&nettle_base64url, LDATA(""), "");
  test_armor(&nettle_base64url, LDATA("H"), "SA==");
  test_armor(&nettle_base64url, LDATA("He"), "SGU=");
  test_armor(&nettle_base64url, LDATA("Hel"), "SGVs");
  test_armor(&nettle_base64url, LDATA("Hell"), "SGVsbA==");
  test_armor(&nettle_base64url, LDATA("Hello"), "SGVsbG8=");
  test_armor(&nettle_base64url, LDATA("Hello\0"), "SGVsbG8A");
  test_armor(&nettle_base64url, LDATA("Hello?>>>"), "SGVsbG8_Pj4-");
  test_armor(&nettle_base64url, LDATA("\xff\xff\xff\xff"), "_____w==");

  {
    /* Test overlapping areas */
    uint8_t buffer[] = "Helloxxxx";
    struct base64_decode_ctx ctx;
    size_t dst_length;
    
    ASSERT(BASE64_ENCODE_RAW_LENGTH(5) == 8);
    base64_encode_raw(buffer, 5, buffer);
    ASSERT(MEMEQ(9, buffer, "SGVsbG8=x"));

    base64_decode_init(&ctx);
    dst_length = 0; /* Output parameter only. */
    ASSERT(base64_decode_update(&ctx, &dst_length, buffer, 8, buffer));
    ASSERT(dst_length == 5);
    
    ASSERT(MEMEQ(9, buffer, "HelloG8=x"));
  }
  test_fuzz ();
}
示例#4
0
std::string base64(const std::string& src)
{
  base64_encode_ctx ctx;
  base64_encode_init(&ctx);
  int dstlen = BASE64_ENCODE_RAW_LENGTH(src.size());
  uint8_t *dst = new uint8_t[dstlen];
  base64_encode_raw(dst, src.size(),
                    reinterpret_cast<const uint8_t*>(src.c_str()));
  std::string res(&dst[0], &dst[dstlen]);
  delete [] dst;
  return res;
}
示例#5
0
static int raw_pubkey_to_base64(const gnutls_datum_t * raw,
				gnutls_datum_t * b64)
{
	size_t size;

	size = BASE64_ENCODE_RAW_LENGTH(raw->size);

	b64->data = gnutls_malloc(size);
	if (b64->data == NULL)
		return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);

	base64_encode_raw((void*)b64->data, raw->size, raw->data);
	b64->size = size;

	return 0;
}
示例#6
0
文件: binary.c 项目: idtek/knot
_public_
int dnssec_binary_to_base64(const dnssec_binary_t *binary,
			    dnssec_binary_t *base64)
{
	if (!binary || !base64) {
		return DNSSEC_EINVAL;
	}

	size_t base64_size = BASE64_ENCODE_RAW_LENGTH(binary->size);
	int r = dnssec_binary_resize(base64, base64_size);
	if (r != DNSSEC_EOK) {
		return r;
	}

	base64_encode_raw(base64->data, binary->size, binary->data);

	return DNSSEC_EOK;
}
unsigned
sexp_transport_vformat(struct nettle_buffer *buffer,
		       const char *format, va_list args)
{
  unsigned start = 0;
  unsigned length;
  unsigned base64_length;

  if (buffer)
    {
      if (!NETTLE_BUFFER_PUTC(buffer, '{'))
	return 0;

      start = buffer->size;
    }
  
  length = sexp_vformat(buffer, format, args);

  if (!length)
    return 0;

  base64_length = BASE64_ENCODE_RAW_LENGTH(length);

  if (buffer)
    {
      if (!nettle_buffer_space(buffer, base64_length - length))
	return 0;

      base64_encode_raw(buffer->contents + start,
			length, buffer->contents + start);
      
      if (!NETTLE_BUFFER_PUTC(buffer, '}'))
	return 0;
    }
  
  return base64_length + 2;
}
示例#8
0
/* encodes data and puts the result into result (locally allocated)
 * The result_size (including the null terminator) is the return value.
 */
int
_gnutls_fbase64_encode(const char *msg, const uint8_t * data,
		       size_t data_size, gnutls_datum_t * result)
{
	int tmp;
	unsigned int i;
	uint8_t tmpres[66];
	uint8_t *ptr;
	char top[80];
	char bottom[80];
	size_t size, max, bytes;
	int pos, top_len = 0, bottom_len = 0;

	if (msg != NULL) {
		if (strlen(msg) > 50) {
			gnutls_assert();
			return GNUTLS_E_BASE64_ENCODING_ERROR;
		}

		_gnutls_str_cpy(top, sizeof(top), "-----BEGIN ");
		_gnutls_str_cat(top, sizeof(top), msg);
		_gnutls_str_cat(top, sizeof(top), "-----\n");

		_gnutls_str_cpy(bottom, sizeof(bottom), "-----END ");
		_gnutls_str_cat(bottom, sizeof(bottom), msg);
		_gnutls_str_cat(bottom, sizeof(bottom), "-----\n");

		top_len = strlen(top);
		bottom_len = strlen(bottom);
	}

	max = B64FSIZE(top_len + bottom_len, data_size);

	result->data = gnutls_malloc(max + 1);
	if (result->data == NULL) {
		gnutls_assert();
		return GNUTLS_E_MEMORY_ERROR;
	}

	bytes = pos = 0;
	INCR(bytes, top_len, max);
	pos = top_len;

	memcpy(result->data, top, top_len);

	for (i = 0; i < data_size; i += 48) {
		if (data_size - i < 48)
			tmp = data_size - i;
		else
			tmp = 48;

		size = BASE64_ENCODE_RAW_LENGTH(tmp);
		if (sizeof(tmpres) < size)
			return gnutls_assert_val(GNUTLS_E_BASE64_ENCODING_ERROR);

		base64_encode_raw(tmpres, tmp, &data[i]);

		INCR(bytes, size + 1, max);
		ptr = &result->data[pos];

		memcpy(ptr, tmpres, size);
		ptr += size;
		pos += size;
		if (msg != NULL) {
			*ptr++ = '\n';
			pos++;
		} else {
			bytes--;
		}
	}

	INCR(bytes, bottom_len, max);

	memcpy(&result->data[bytes - bottom_len], bottom, bottom_len);
	result->data[bytes] = 0;
	result->size = bytes;

	return max + 1;
}