コード例 #1
0
ファイル: entropy.c プロジェクト: 0buffer/polarssl
/*
 * Entropy accumulator update
 */
static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id,
                           const unsigned char *data, size_t len )
{
    unsigned char header[2];
    unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
    size_t use_len = len;
    const unsigned char *p = data;

    if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE )
    {
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
        mbedtls_sha512( data, len, tmp, 0 );
#else
        mbedtls_sha256( data, len, tmp, 0 );
#endif
        p = tmp;
        use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
    }

    header[0] = source_id;
    header[1] = use_len & 0xFF;

#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
    mbedtls_sha512_update( &ctx->accumulator, header, 2 );
    mbedtls_sha512_update( &ctx->accumulator, p, use_len );
#else
    mbedtls_sha256_update( &ctx->accumulator, header, 2 );
    mbedtls_sha256_update( &ctx->accumulator, p, use_len );
#endif

    return( 0 );
}
コード例 #2
0
int
crypto_hash_sha512(unsigned char *out, const unsigned char *in,
                   unsigned long long inlen)
{
    mbedtls_sha512(in, inlen, out, 0);
    return 0;
}
コード例 #3
0
JsVar *jswrap_crypto_SHAx(JsVar *message, int shaNum) {
  JSV_GET_AS_CHAR_ARRAY(msgPtr, msgLen, message);
  if (!msgPtr) return 0;

  int bufferSize = 20;
  if (shaNum>1) bufferSize = shaNum/8;

  char *outPtr = 0;
  JsVar *outArr = jsvNewArrayBufferWithPtr((unsigned int)bufferSize, &outPtr);
  if (!outPtr) {
    jsError("Not enough memory for result");
    return 0;
  }

  if (shaNum==1) mbedtls_sha1((unsigned char *)msgPtr, msgLen, (unsigned char *)outPtr);
  else if (shaNum==224) mbedtls_sha256((unsigned char *)msgPtr, msgLen, (unsigned char *)outPtr, true/*224*/);
  else if (shaNum==256) mbedtls_sha256((unsigned char *)msgPtr, msgLen, (unsigned char *)outPtr, false/*256*/);
  else if (shaNum==384) mbedtls_sha512((unsigned char *)msgPtr, msgLen, (unsigned char *)outPtr, true/*384*/);
  else if (shaNum==512) mbedtls_sha512((unsigned char *)msgPtr, msgLen, (unsigned char *)outPtr, false/*512*/);
  return outArr;
}
コード例 #4
0
ファイル: md_wrap.c プロジェクト: Comojs/comojs
static void sha512_wrap( const unsigned char *input, size_t ilen,
                    unsigned char *output )
{
    mbedtls_sha512( input, ilen, output, 0 );
}
コード例 #5
0
ファイル: entropy.c プロジェクト: 0buffer/polarssl
int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
{
    int ret, count = 0, i, done;
    mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
    unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];

    if( len > MBEDTLS_ENTROPY_BLOCK_SIZE )
        return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );

#if defined(MBEDTLS_THREADING_C)
    if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
        return( ret );
#endif

    /*
     * Always gather extra entropy before a call
     */
    do
    {
        if( count++ > ENTROPY_MAX_LOOP )
        {
            ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
            goto exit;
        }

        if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
            goto exit;

        done = 1;
        for( i = 0; i < ctx->source_count; i++ )
            if( ctx->source[i].size < ctx->source[i].threshold )
                done = 0;
    }
    while( ! done );

    memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );

#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
    mbedtls_sha512_finish( &ctx->accumulator, buf );

    /*
     * Reset accumulator and counters and recycle existing entropy
     */
    memset( &ctx->accumulator, 0, sizeof( mbedtls_sha512_context ) );
    mbedtls_sha512_starts( &ctx->accumulator, 0 );
    mbedtls_sha512_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );

    /*
     * Perform second SHA-512 on entropy
     */
    mbedtls_sha512( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
#else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
    mbedtls_sha256_finish( &ctx->accumulator, buf );

    /*
     * Reset accumulator and counters and recycle existing entropy
     */
    memset( &ctx->accumulator, 0, sizeof( mbedtls_sha256_context ) );
    mbedtls_sha256_starts( &ctx->accumulator, 0 );
    mbedtls_sha256_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );

    /*
     * Perform second SHA-256 on entropy
     */
    mbedtls_sha256( buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf, 0 );
#endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */

    for( i = 0; i < ctx->source_count; i++ )
        ctx->source[i].size = 0;

    memcpy( output, buf, len );

    ret = 0;

exit:
#if defined(MBEDTLS_THREADING_C)
    if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
        return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
#endif

    return( ret );
}
コード例 #6
0
ファイル: tls_benchmark_main.c プロジェクト: tool3210/TizenRT
pthread_addr_t tls_benchmark_cb(void *args)
{
	int i;
	int argc;
	char **argv;
	unsigned char tmp[200];
	char title[TITLE_LEN];
	todo_list todo;
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
	unsigned char alloc_buf[HEAP_SIZE] = { 0 };
#endif

	argc = ((struct pthread_arg *)args)->argc;
	argv = ((struct pthread_arg *)args)->argv;

	if (argc <= 1) {
		memset(&todo, 1, sizeof(todo));
	} else {
		memset(&todo, 0, sizeof(todo));

		for (i = 1; i < argc; i++) {
			if (strcmp(argv[i], "md4") == 0) {
				todo.md4 = 1;
			} else if (strcmp(argv[i], "md5") == 0) {
				todo.md5 = 1;
			} else if (strcmp(argv[i], "ripemd160") == 0) {
				todo.ripemd160 = 1;
			} else if (strcmp(argv[i], "sha1") == 0) {
				todo.sha1 = 1;
			} else if (strcmp(argv[i], "sha256") == 0) {
				todo.sha256 = 1;
			} else if (strcmp(argv[i], "sha512") == 0) {
				todo.sha512 = 1;
			} else if (strcmp(argv[i], "arc4") == 0) {
				todo.arc4 = 1;
			} else if (strcmp(argv[i], "des3") == 0) {
				todo.des3 = 1;
			} else if (strcmp(argv[i], "des") == 0) {
				todo.des = 1;
			} else if (strcmp(argv[i], "aes_cbc") == 0) {
				todo.aes_cbc = 1;
			} else if (strcmp(argv[i], "aes_gcm") == 0) {
				todo.aes_gcm = 1;
			} else if (strcmp(argv[i], "aes_ccm") == 0) {
				todo.aes_ccm = 1;
			} else if (strcmp(argv[i], "aes_cmac") == 0) {
				todo.aes_cmac = 1;
			} else if (strcmp(argv[i], "des3_cmac") == 0) {
				todo.des3_cmac = 1;
			} else if (strcmp(argv[i], "camellia") == 0) {
				todo.camellia = 1;
			} else if (strcmp(argv[i], "blowfish") == 0) {
				todo.blowfish = 1;
			} else if (strcmp(argv[i], "havege") == 0) {
				todo.havege = 1;
			} else if (strcmp(argv[i], "ctr_drbg") == 0) {
				todo.ctr_drbg = 1;
			} else if (strcmp(argv[i], "hmac_drbg") == 0) {
				todo.hmac_drbg = 1;
			} else if (strcmp(argv[i], "rsa") == 0) {
				todo.rsa = 1;
			} else if (strcmp(argv[i], "dhm") == 0) {
				todo.dhm = 1;
			} else if (strcmp(argv[i], "ecdsa") == 0) {
				todo.ecdsa = 1;
			} else if (strcmp(argv[i], "ecdh") == 0) {
				todo.ecdh = 1;
			} else {
				mbedtls_printf("Unrecognized option: %s\n", argv[i]);
				mbedtls_printf("Available options: " OPTIONS);
			}
		}
	}

	mbedtls_printf("\n");

#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
	mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
#endif
	memset(buf, 0xAA, sizeof(buf));
	memset(tmp, 0xBB, sizeof(tmp));

#if defined(MBEDTLS_MD4_C)
	if (todo.md4) {
		TIME_AND_TSC("MD4", mbedtls_md4(buf, BUFSIZE, tmp));
	}
#endif

#if defined(MBEDTLS_MD5_C)
	if (todo.md5) {
		TIME_AND_TSC("MD5", mbedtls_md5(buf, BUFSIZE, tmp));
	}
#endif

#if defined(MBEDTLS_RIPEMD160_C)
	if (todo.ripemd160) {
		TIME_AND_TSC("RIPEMD160", mbedtls_ripemd160(buf, BUFSIZE, tmp));
	}
#endif

#if defined(MBEDTLS_SHA1_C)
	if (todo.sha1) {
		TIME_AND_TSC("SHA-1", mbedtls_sha1(buf, BUFSIZE, tmp));
	}
#endif

#if defined(MBEDTLS_SHA256_C)
	if (todo.sha256) {
		TIME_AND_TSC("SHA-256", mbedtls_sha256(buf, BUFSIZE, tmp, 0));
	}
#endif

#if defined(MBEDTLS_SHA512_C)
	if (todo.sha512) {
		TIME_AND_TSC("SHA-512", mbedtls_sha512(buf, BUFSIZE, tmp, 0));
	}
#endif

#if defined(MBEDTLS_ARC4_C)
	if (todo.arc4) {
		mbedtls_arc4_context arc4;
		mbedtls_arc4_init(&arc4);
		mbedtls_arc4_setup(&arc4, tmp, 32);
		TIME_AND_TSC("ARC4", mbedtls_arc4_crypt(&arc4, BUFSIZE, buf, buf));
		mbedtls_arc4_free(&arc4);
	}
#endif

#if defined(MBEDTLS_DES_C)
#if defined(MBEDTLS_CIPHER_MODE_CBC)
	if (todo.des3) {
		mbedtls_des3_context des3;
		mbedtls_des3_init(&des3);
		mbedtls_des3_set3key_enc(&des3, tmp);
		TIME_AND_TSC("3DES",
					 mbedtls_des3_crypt_cbc(&des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf));
		mbedtls_des3_free(&des3);
	}

	if (todo.des) {
		mbedtls_des_context des;
		mbedtls_des_init(&des);
		mbedtls_des_setkey_enc(&des, tmp);
		TIME_AND_TSC("DES",
					 mbedtls_des_crypt_cbc(&des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf));
		mbedtls_des_free(&des);
	}

#endif /* MBEDTLS_CIPHER_MODE_CBC */
#if defined(MBEDTLS_CMAC_C)
	if (todo.des3_cmac) {
		unsigned char output[8];
		const mbedtls_cipher_info_t *cipher_info;

		memset(buf, 0, sizeof(buf));
		memset(tmp, 0, sizeof(tmp));

		cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_DES_EDE3_ECB);

		TIME_AND_TSC("3DES-CMAC",
					 mbedtls_cipher_cmac(cipher_info, tmp, 192, buf,
										 BUFSIZE, output));
	}
#endif /* MBEDTLS_CMAC_C */
#endif /* MBEDTLS_DES_C */

#if defined(MBEDTLS_AES_C)
#if defined(MBEDTLS_CIPHER_MODE_CBC)
	if (todo.aes_cbc) {
		int keysize;
		mbedtls_aes_context aes;
		mbedtls_aes_init(&aes);
		for (keysize = 128; keysize <= 256; keysize += 64) {
			mbedtls_snprintf(title, sizeof(title), "AES-CBC-%d", keysize);

			memset(buf, 0, sizeof(buf));
			memset(tmp, 0, sizeof(tmp));
			mbedtls_aes_setkey_enc(&aes, tmp, keysize);

			TIME_AND_TSC(title,
						 mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf));
		}
		mbedtls_aes_free(&aes);
	}
#endif
#if defined(MBEDTLS_GCM_C)
	if (todo.aes_gcm) {
		int keysize;
		mbedtls_gcm_context gcm;

		mbedtls_gcm_init(&gcm);
		for (keysize = 128; keysize <= 256; keysize += 64) {
			mbedtls_snprintf(title, sizeof(title), "AES-GCM-%d", keysize);

			memset(buf, 0, sizeof(buf));
			memset(tmp, 0, sizeof(tmp));
			mbedtls_gcm_setkey(&gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);

			TIME_AND_TSC(title,
						 mbedtls_gcm_crypt_and_tag(&gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
												   12, NULL, 0, buf, buf, 16, tmp));

			mbedtls_gcm_free(&gcm);
		}
	}
#endif
#if defined(MBEDTLS_CCM_C)
	if (todo.aes_ccm) {
		int keysize;
		mbedtls_ccm_context ccm;

		mbedtls_ccm_init(&ccm);
		for (keysize = 128; keysize <= 256; keysize += 64) {
			mbedtls_snprintf(title, sizeof(title), "AES-CCM-%d", keysize);

			memset(buf, 0, sizeof(buf));
			memset(tmp, 0, sizeof(tmp));
			mbedtls_ccm_setkey(&ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);

			TIME_AND_TSC(title,
						 mbedtls_ccm_encrypt_and_tag(&ccm, BUFSIZE, tmp,
								 12, NULL, 0, buf, buf, tmp, 16));

			mbedtls_ccm_free(&ccm);
		}
	}
#endif
#if defined(MBEDTLS_CMAC_C)
	if (todo.aes_cmac) {
		unsigned char output[16];
		const mbedtls_cipher_info_t *cipher_info;
		mbedtls_cipher_type_t cipher_type;
		int keysize;

		for (keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
			 keysize <= 256;
			 keysize += 64, cipher_type++) {
			mbedtls_snprintf(title, sizeof(title), "AES-CMAC-%d", keysize);

			memset(buf, 0, sizeof(buf));
			memset(tmp, 0, sizeof(tmp));

			cipher_info = mbedtls_cipher_info_from_type(cipher_type);

			TIME_AND_TSC(title,
						 mbedtls_cipher_cmac(cipher_info, tmp, keysize,
											 buf, BUFSIZE, output));
		}

		memset(buf, 0, sizeof(buf));
		memset(tmp, 0, sizeof(tmp));
		TIME_AND_TSC("AES-CMAC-PRF-128",
					 mbedtls_aes_cmac_prf_128(tmp, 16, buf, BUFSIZE,
											  output));
	}
#endif /* MBEDTLS_CMAC_C */
#endif /* MBEDTLS_AES_C */

#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
	if (todo.camellia) {
		int keysize;
		mbedtls_camellia_context camellia;
		mbedtls_camellia_init(&camellia);
		for (keysize = 128; keysize <= 256; keysize += 64) {
			mbedtls_snprintf(title, sizeof(title), "CAMELLIA-CBC-%d", keysize);

			memset(buf, 0, sizeof(buf));
			memset(tmp, 0, sizeof(tmp));
			mbedtls_camellia_setkey_enc(&camellia, tmp, keysize);

			TIME_AND_TSC(title,
						 mbedtls_camellia_crypt_cbc(&camellia, MBEDTLS_CAMELLIA_ENCRYPT,
													BUFSIZE, tmp, buf, buf));
		}
		mbedtls_camellia_free(&camellia);
	}
#endif

#if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
	if (todo.blowfish) {
		int keysize;
		mbedtls_blowfish_context blowfish;
		mbedtls_blowfish_init(&blowfish);

		for (keysize = 128; keysize <= 256; keysize += 64) {
			mbedtls_snprintf(title, sizeof(title), "BLOWFISH-CBC-%d", keysize);

			memset(buf, 0, sizeof(buf));
			memset(tmp, 0, sizeof(tmp));
			mbedtls_blowfish_setkey(&blowfish, tmp, keysize);

			TIME_AND_TSC(title,
						 mbedtls_blowfish_crypt_cbc(&blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
													tmp, buf, buf));
		}

		mbedtls_blowfish_free(&blowfish);
	}
#endif

#if defined(MBEDTLS_HAVEGE_C)
	if (todo.havege) {
		mbedtls_havege_state hs;
		mbedtls_havege_init(&hs);
		TIME_AND_TSC("HAVEGE", mbedtls_havege_random(&hs, buf, BUFSIZE));
		mbedtls_havege_free(&hs);
	}
#endif

#if defined(MBEDTLS_CTR_DRBG_C)
	if (todo.ctr_drbg) {
		mbedtls_ctr_drbg_context ctr_drbg;

		mbedtls_ctr_drbg_init(&ctr_drbg);

		if (mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0) != 0) {
			mbedtls_exit(1);
		}
		TIME_AND_TSC("CTR_DRBG (NOPR)",
					 if (mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE) != 0)
					 mbedtls_exit(1));

		if (mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0) != 0) {
			mbedtls_exit(1);
		}
		mbedtls_ctr_drbg_set_prediction_resistance(&ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON);
		TIME_AND_TSC("CTR_DRBG (PR)",
					 if (mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE) != 0)
					 mbedtls_exit(1));
		mbedtls_ctr_drbg_free(&ctr_drbg);
	}