Since i don't know where to get that include (mclcrypto.h), I made the
attached code from openSSL functions (~/openssl-0.9.8k/ssl/t1_enc.c).
And the output I get is totally different:

0x51 0xc7 0x9f 0x01 0x94 0x7d 0x04 0xcd 
0x99 0xc7 0x3c 0x7a 0x28 0x85 0x6a 0x52

Let me know what you think.

Thanks!
        Sergei


On Fri, Apr 12, 2013 at 04:04:20PM +0200, marek.marc...@malkom.pl wrote:
> Hello,
> 
> Output from attached code:
> admin# ./mcl_tls1_PRF 
>  0x8b 0x13 0xc7 0x58 0xc3 0x4f 0x99 0x3a 
>  0x18 0x7d 0x29 0x45 0xed 0x5b 0x69 0x1d 
> 
> Best Regards,
> --
> Marek Marcola <marek.marc...@malkom.pl>
> 
> 
> 
> owner-openssl-us...@openssl.org wrote on 04/11/2013 09:48:51 PM:
> 
> > Sergei Gerasenko <ser...@publicschoolworks.com> 
> > Sent by: owner-openssl-us...@openssl.org
> > 
> > 04/12/2013 11:57 AM
> > 
> > Please respond to
> > openssl-users@openssl.org
> > 
> > To
> > 
> > openssl-users@openssl.org, 
> > 
> > cc
> > 
> > Subject
> > 
> > RFC 2246
> > 
> > Hello,
> > 
> > Does anybody have sample input and output for the tls1_PRF function
> > which is described in RFC 2246? I've used several implementations of it
> > including the one from openSSL and I'm not sure if what I'm getting is 
> right
> > -- and all of them return something different.
> > 
> > Thanks!
> > ______________________________________________________________________
> > OpenSSL Project                                 http://www.openssl.org
> > User Support Mailing List                    openssl-users@openssl.org
> > Automated List Manager                           majord...@openssl.org


#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>

static void tls1_P_hash(const EVP_MD *md, const unsigned char *sec,
			int sec_len, unsigned char *seed, int seed_len,
			unsigned char *out, int olen)
	{
	int chunk,n;
	unsigned int j;
	HMAC_CTX ctx;
	HMAC_CTX ctx_tmp;
	unsigned char A1[EVP_MAX_MD_SIZE];
	unsigned int A1_len;
	
	chunk=EVP_MD_size(md);

	HMAC_CTX_init(&ctx);
	HMAC_CTX_init(&ctx_tmp);
	HMAC_CTX_set_flags(&ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
	HMAC_CTX_set_flags(&ctx_tmp, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
	HMAC_Init_ex(&ctx,sec,sec_len,md, NULL);
	HMAC_Init_ex(&ctx_tmp,sec,sec_len,md, NULL);
	HMAC_Update(&ctx,seed,seed_len);
	HMAC_Final(&ctx,A1,&A1_len);

	n=0;
	for (;;)
		{
		HMAC_Init_ex(&ctx,NULL,0,NULL,NULL); /* re-init */
		HMAC_Init_ex(&ctx_tmp,NULL,0,NULL,NULL); /* re-init */
		HMAC_Update(&ctx,A1,A1_len);
		HMAC_Update(&ctx_tmp,A1,A1_len);
		HMAC_Update(&ctx,seed,seed_len);

		if (olen > chunk)
			{
			HMAC_Final(&ctx,out,&j);
			out+=j;
			olen-=j;
			HMAC_Final(&ctx_tmp,A1,&A1_len); /* calc the next A1 value */
			}
		else	/* last one */
			{
			HMAC_Final(&ctx,A1,&A1_len);
			memcpy(out,A1,olen);
			break;
			}
		}
	HMAC_CTX_cleanup(&ctx);
	HMAC_CTX_cleanup(&ctx_tmp);
	OPENSSL_cleanse(A1,sizeof(A1));
	}

static void tls1_PRF(const EVP_MD *md5, const EVP_MD *sha1,
		     unsigned char *label, int label_len,
		     const unsigned char *sec, int slen, unsigned char *out1,
		     unsigned char *out2, int olen)
	{
	int len,i;
	const unsigned char *S1,*S2;

	len=slen/2;
	S1=sec;
	S2= &(sec[len]);
	len+=(slen&1); /* add for odd, make longer */

	
	tls1_P_hash(md5 ,S1,len,label,label_len,out1,olen);
	tls1_P_hash(sha1,S2,len,label,label_len,out2,olen);

	for (i=0; i<olen; i++)
		out1[i]^=out2[i];
}

int print_hex(unsigned char *buf, int len) {
        int i;
        int n;

        printf(" ");

        for (i = 0, n = 0; i < len; i++) {
                if (n > 7) {
                        printf("\n ");
                        n = 0;
                }
                printf("0x%02x ", buf[i]);
                n++;
        }
        printf("\n");

        return (0);
}

int main() {
	const EVP_MD *md5,*sha;
	md5=EVP_md5();
	sha=EVP_sha();

	char *key = "abcdefgh";
	int key_len = 8;

	char *label = "1234567890";
	int label_len = 10;

	char *seed = "xyz";
	int seed_len = 3;

	char dgst1[128];
	int dgst1_len = 16;

	char dgst2[128];
	int dgst2_len = 16;

	tls1_PRF(md5, sha, label, label_len, key, key_len, dgst1, dgst2, dgst1_len);

	print_hex(dgst2, dgst1_len);

	return (0);
}

Reply via email to