Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7887315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:32:53+00:00 2026-06-03T05:32:53+00:00

I am trying to calculate MAC for TLS v 1.1 Client Finished packet using

  • 0

I am trying to calculate MAC for TLS v 1.1 Client Finished packet using CBC as per the details given in section 6.2.3.2 here!

The following is the function I have written:

def SendSSLPacket(self, hsMsg, seq, renegotiate):
        rec = hsMsg
        recLen = len(rec)
        rec_len_packed = pack('>H', recLen)

                    #
                    # The following initIV is just for testing
                    # Will be replaced by random number later
                    #
        initIV = "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02"

        rec1 = ""
        for index in range(0, len(rec)):
            rec1 = rec1 + chr(ord(rec[index]) ^ ord(initIV[index]))

        self.seqNum = pack('>Q', seq)

        m = hmac.new(initIV, 
            digestmod=sha1)
        m.update(self.seqNum)
        m.update("\x16")
        m.update("\x03")
        m.update("\x02")
        m.update(rec_len_packed)
        m.update(rec)
        m = m.digest()

        self.HexStrDisplay("Final MAC", Str2HexStr(m))

        currentLength = len(rec + m) + 1
        blockLength = 16
        pad_len = blockLength - \
            (currentLength % blockLength)

        self.log("Padding Length: %s" % (str(pad_len)))

        padding = ''
        for iter in range(0, pad_len + 1):
            padding = padding + \
            struct.pack('B', pad_len)

        self.HexStrDisplay("Padding", Str2HexStr(padding))

        self.sslStruct['recordPlusMAC'] = \
            initIV + rec1 + m + padding
        self.HexStrDisplay("Final Packet", Str2HexStr(
            self.sslStruct['recordPlusMAC']))

        if renegotiate == 1:
            enc_hs_with_reneg = AES.new( self.sslStruct['wKeyPtr'], AES.MODE_CBC, self.sslStruct['wKeyPtr'])
            encryptedData = enc_hs_with_reneg.encrypt(self.sslStruct['recordPlusMAC'])


        if renegotiate == 0:
            enc_hs_wo_reneg = AES.new( self.sslStruct['wKeyPtr'], AES.MODE_CBC, self.sslStruct['wKeyPtr'] )
            encryptedData = enc_hs_wo_reneg.encrypt(self.sslStruct['recordPlusMAC'])



        packLen = len(encryptedData)

        self.sslStruct['encryptedRecordPlusMAC'] = \
            tls11RecHeaderDefault + \
            Pack2Bytes(packLen) + encryptedData
        self.HexStrDisplay("Encrypted Packet",
            Str2HexStr(self.sslStruct['encryptedRecordPlusMAC']))

        self.socket.send(
            self.sslStruct['encryptedRecordPlusMAC'])

The server is throwing the following error though:

3079400200:error:1408F119:SSL routines:SSL3_GET_RECORD:decryption failed or bad record mac:s3_pkt.c:496:

It would be great if someone could help me out in finding out what went wrong

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:32:54+00:00Added an answer on June 3, 2026 at 5:32 am

    Well, went through the polarssl code (looks simple and clear)

    The following worked for me:

    def SendSSLPacket(self, hsMsg, seq, renegotiate):
            rec = hsMsg
            recLen = len(rec)
            rec_len_packed = pack('>H', recLen)
    
            self.seqNum = pack('>Q', seq)
    
            #
            # The following initIV is just for testing
            # Will be replaced by random number later
            #
            initIV = "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02"
    
    
            m = hmac.new(self.sslStruct['wMacPtr'], 
                digestmod=sha1)
            m.update(self.seqNum)
            m.update("\x16")
            m.update("\x03")
            m.update("\x02")
            m.update(rec_len_packed)
            m.update(rec)
            m = m.digest()
    
    
            self.HexStrDisplay("Final MAC", Str2HexStr(m))
    
            currentLength = len(rec + m) + 1
            blockLength = 16
            pad_len = blockLength - \
                (currentLength % blockLength)
    
            if pad_len == blockLength:
                pad_len = 0
    
            self.log("Padding Length: %s" % (str(pad_len)))
    
            padding = ''
            for iter in range(0, pad_len + 1):
                padding = padding + \
                struct.pack('B', pad_len)
    
            self.HexStrDisplay("Padding", Str2HexStr(padding))
    
            self.sslStruct['recordPlusMAC'] = \
                initIV + rec + m + padding
            self.HexStrDisplay("Final Packet", Str2HexStr(
                self.sslStruct['recordPlusMAC']))
    
            if renegotiate == 1:
                enc_hs_with_reneg = AES.new( self.sslStruct['wKeyPtr'], AES.MODE_CBC, self.sslStruct['wIVPtr'])
                encryptedData = enc_hs_with_reneg.encrypt(self.sslStruct['recordPlusMAC'])
    
            if renegotiate == 0:
                enc_hs_wo_reneg = AES.new( self.sslStruct['wKeyPtr'], AES.MODE_CBC, self.sslStruct['wIVPtr'] )
                encryptedData = enc_hs_wo_reneg.encrypt(self.sslStruct['recordPlusMAC'])
    
    
            packLen = len(encryptedData)
    
            self.sslStruct['encryptedRecordPlusMAC'] = \
                tls11RecHeaderDefault + \
                Pack2Bytes(packLen) + encryptedData
            self.HexStrDisplay("Encrypted Packet",
                Str2HexStr(self.sslStruct['encryptedRecordPlusMAC']))
    
            self.socket.send(
                self.sslStruct['encryptedRecordPlusMAC'])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

am trying to calculate mean and variance using 3X3 window over image(hXw) in opencv...here
I'm trying to calculate time blocks from given date range. Here's my attempt: <?php
I am trying to calculate a determinant using the boost c++ libraries. I found
I am trying to calculate apk file size of the installed applications. Here is
Here I'm trying to calculate the hours between two dates. When i run the
I am trying to calculate the fiscal year from this following code. But I
I am trying to calculate the difference between given two times and i find
I'm trying to calculate SHA1 hash of a unicode string using T-SQL. The below
I'm trying to calculate the size of a few tables as stated here ,
I am trying to calculate if the given specified date is at least six

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.