<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: RT117x: key wrap algorithm for OTFAD in i.MX RT Crossover MCUs</title>
    <link>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/RT117x-key-wrap-algorithm-for-OTFAD/m-p/1776443#M28074</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/124967"&gt;@mastupristi&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;I will ask SPSDK developers regarding key wrapping function.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Lots of people is already on holidays, so it might take some time.&lt;/P&gt;
&lt;P&gt;Thank you very much for your understanding.&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;BR /&gt;Martin H.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 18 Dec 2023 14:54:53 GMT</pubDate>
    <dc:creator>martin_hrncarek</dc:creator>
    <dc:date>2023-12-18T14:54:53Z</dc:date>
    <item>
      <title>RT117x: key wrap algorithm for OTFAD</title>
      <link>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/RT117x-key-wrap-algorithm-for-OTFAD/m-p/1760712#M27670</link>
      <description>&lt;P&gt;for mostly educational purposes I am trying to understand how the key wrap algorithm works for encryption keys on RT117x.&lt;BR /&gt;I refer to section 11.5.2 of the Security Reference Manual, where algorithms are given in c for both wrapping and unwrapping keys.&lt;/P&gt;&lt;P&gt;Specifically: I have a properly functioning board with an encrypted FW, I have the wrapped keyblob file that it uses, and I have the KEK that I used to create said keyblob.&lt;/P&gt;&lt;P&gt;The key used, given as input to the image_enc tool is &lt;FONT face="courier new,courier"&gt;ffeeddccbbaa99887766554433221100&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;What I want to try to do is to try to unwrap "by hand" the keyblob&lt;/P&gt;&lt;P&gt;I transcribed the C functions &lt;FONT face="courier new,courier"&gt;do_aes128_key_unwrap()&lt;/FONT&gt; which doesn't work though, I tried some key permutations:&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;00112233445566778899aabbccddeeff&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;3322110077665544bbaa9988ffeeddcc&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;ffeeddccbbaa99887766554433221100&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;ccddeeff8899aabb4455667700112233&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;To implement the InvCipher() function (not described in the manual) I used this:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;#include &amp;lt;openssl/evp.h&amp;gt;

void InvCipher(unsigned char *in, unsigned char *expanded_kek, int rounds, unsigned char *out) {
    EVP_CIPHER_CTX *ctx;
    int len;
    ctx = EVP_CIPHER_CTX_new();
    EVP_DecryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, expanded_kek, NULL);
    EVP_DecryptUpdate(ctx, out, &amp;amp;len, in, 16);
    EVP_CIPHER_CTX_free(ctx);
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;Is this correct? If not, could you illustrate to me what the implementation of the function should be?&lt;BR /&gt;I used ECB since this function was created to decipher a single block, though if it's wrong let me know.&lt;/P&gt;&lt;P&gt;Another question: the manual refers in multiple places to RFC3394. I find these lines in the algorithm comments, though:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;    // step 1: initialize variables
    // set A = C[0]
    // for i = 1 to n
    // R[i] = C[i]

    // step 2: calculate intermediate values
    // for j = 5 to 0
    // for i = n to 1
    // B = AES‐1(K, (A ^ (n*j+i) | R[i])
    // A = MSB(64, B)
    // R[i] = LSB(64, B)

    // step 3: output the results
    // if A == IV
    //     then
    //         for i = 1 to n
    //             P[i] = R[i]
    //     else
    //         return an error&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;But in section 2.2.2 of RFC3394 the same steps are described differently:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;1) Initialize variables.

       Set A[s] = C[0] where s = 6n
       For i = 1 to n
           R[s][i] = C[i]

   2) Calculate the intermediate values.

       For t = s to 1
           A[t-1] = MSB(64, AES-1(K, ((A[t] ^ t) | R[t][n]))
           R[t-1][1] = LSB(64, AES-1(K, ((A[t]^t) | R[t][n]))
           For i = 2 to n
               R[t-1][i] = R[t][i-1]

   3) Output the results.

       If A[0] is an appropriate initial value (see 2.2.3),
       Then
           For i = 1 to n
               P[i] = R[0][i]
       Else
           Return an error&lt;/LI-CODE&gt;&lt;P&gt;So is the algorithm described in the manual adherent to RFC3394 or not?&lt;/P&gt;&lt;P&gt;Finally, I found the sources of a version of 'image_enc' (here &lt;A href="https://community.nxp.com/t5/i-MX-RT/image-enc2-zip-download/m-p/1174943#M10980" target="_blank" rel="noopener"&gt;https://community.nxp.com/t5/i-MX-RT/image-enc2-zip-download/m-p/1174943#M10980&lt;/A&gt;) which though doesn't seem related to the version I have (Bundled in Provisioning tool 6)&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Package:                   image_enc
Version:                   1.0.0
Description:               AES encryption utility 
License:                   LA_OPT_NXP_SOFTWARE_License
License description:       NXP proprietary
Distribution Type:         Binary
Location:                  \tools\image_enc&lt;/LI-CODE&gt;&lt;P&gt;can you get me the sources of this version as well?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;best regards&lt;/P&gt;&lt;P&gt;Max&lt;/P&gt;</description>
      <pubDate>Tue, 21 Nov 2023 11:31:24 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/RT117x-key-wrap-algorithm-for-OTFAD/m-p/1760712#M27670</guid>
      <dc:creator>mastupristi</dc:creator>
      <dc:date>2023-11-21T11:31:24Z</dc:date>
    </item>
    <item>
      <title>Re: RT117x: key wrap algorithm for OTFAD</title>
      <link>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/RT117x-key-wrap-algorithm-for-OTFAD/m-p/1762869#M27713</link>
      <description>&lt;P&gt;I tried to take a look at spsdk. It's not easy for me to figure out, but in the otfad.py file (&lt;A href="https://github.com/nxp-mcuxpresso/spsdk/blob/09c711a8fd4c54f126a7dfe1b3ae8bb361c5473e/spsdk/utils/crypto/otfad.py#L224" target="_blank"&gt;https://github.com/nxp-mcuxpresso/spsdk/blob/09c711a8fd4c54f126a7dfe1b3ae8bb361c5473e/spsdk/utils/crypto/otfad.py#L224&lt;/A&gt;) there's a call to aes_key_wrap(), which links back to symmetric.py (&lt;A href="https://github.com/nxp-mcuxpresso/spsdk/blob/09c711a8fd4c54f126a7dfe1b3ae8bb361c5473e/spsdk/crypto/symmetric.py#L59" target="_blank"&gt;https://github.com/nxp-mcuxpresso/spsdk/blob/09c711a8fd4c54f126a7dfe1b3ae8bb361c5473e/spsdk/crypto/symmetric.py#L59&lt;/A&gt;), and I see that a few lines below there's also the function to unwrap.&lt;BR /&gt;Both are part of the cryptography module&lt;BR /&gt;The unwrap is documented here: &lt;A href="https://cryptography.io/en/latest/hazmat/primitives/keywrap/#cryptography.hazmat.primitives.keywrap.aes_key_unwrap" target="_blank"&gt;https://cryptography.io/en/latest/hazmat/primitives/keywrap/#cryptography.hazmat.primitives.keywrap.aes_key_unwrap&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I a tried to see if there was already an example, but I didn't find anything useful, so I tried a little by myself &lt;STRONG&gt;without success&lt;/STRONG&gt;.&lt;BR /&gt;This i the script:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from Cryptodome.Cipher import AES
import argparse
import sys

def InvCipher(in_data, expanded_kek):
    cipher = AES.new(expanded_kek, AES.MODE_ECB)
    out_data = cipher.decrypt(in_data)
    return out_data

def do_aes128_key_unwrap(wrapped_ciphertext, expanded_kek):
    N = 6
    iv = [0xa6] * 8  # 64-bit initialization vector
    a = wrapped_ciphertext[:8]
    r = [wrapped_ciphertext[8 * (i + 1):8 * (i + 2)] for i in range(N)]

    for j in range(5, -1, -1):
        for i in range(N, 0, -1):
            b = bytearray(a[:] + r[i - 1])
            b[7] = b[7] ^ (N * j) + i
            b = InvCipher(bytes(b), expanded_kek)

            a = b[:8]
            r[i - 1] = b[8:]

    unwrapped_plaintext = a + bytes([item for sublist in r for item in sublist])

    if unwrapped_plaintext[:8] == iv:
        return unwrapped_plaintext
    else:
        return None


if __name__ == '__main__':
    parser = argparse.ArgumentParser(conflict_handler="resolve")
    parser.add_argument("infile", help="input file name")
    parser.add_argument("kek", help="kek")
    args = parser.parse_args()

    with open(args.infile, "rb") as inFile:
        wrapped_ciphertext = inFile.read()
    
    kek = bytearray.fromhex(args.kek)

    unwrapped_plaintext = do_aes128_key_unwrap(wrapped_ciphertext, kek)

    if None != unwrapped_plaintext:
        sys.stdout.write(unwrapped_plaintext)
    else:
        print("Error unwrapping")
    &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Nov 2023 17:09:33 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/RT117x-key-wrap-algorithm-for-OTFAD/m-p/1762869#M27713</guid>
      <dc:creator>mastupristi</dc:creator>
      <dc:date>2023-11-23T17:09:33Z</dc:date>
    </item>
    <item>
      <title>Re: RT117x: key wrap algorithm for OTFAD</title>
      <link>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/RT117x-key-wrap-algorithm-for-OTFAD/m-p/1766282#M27805</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/124967"&gt;@mastupristi&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1. InvCipher() needs to be implemented as below:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Kan_Li_0-1701330152716.png" style="width: 545px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/252254i301BC7AFB4202918/image-dimensions/545x275?v=v2" width="545" height="275" role="button" title="Kan_Li_0-1701330152716.png" alt="Kan_Li_0-1701330152716.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Please kindly refer to&amp;nbsp;&lt;A href="https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197-upd1.pdf" target="_blank"&gt;https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197-upd1.pdf&lt;/A&gt;&amp;nbsp;for more details.&lt;/P&gt;
&lt;P&gt;2. Regarding the comments between RM and &lt;SPAN&gt;RFC3394&amp;nbsp;, actually the comments from RM is&amp;nbsp;an index based operation which is also specified in RFC3394:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Kan_Li_1-1701330379581.png" style="width: 641px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/252255iA9B839A53C883B3B/image-dimensions/641x526?v=v2" width="641" height="526" role="button" title="Kan_Li_1-1701330379581.png" alt="Kan_Li_1-1701330379581.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;3. Regarding the source of&amp;nbsp;'image_enc'&amp;nbsp;, please kindly refer to&amp;nbsp;&lt;A href="https://github.com/JayHeng/NXP-MCUBootUtility/tree/master/tools/image_enc/code" target="_blank"&gt;https://github.com/JayHeng/NXP-MCUBootUtility/tree/master/tools/image_enc/code&lt;/A&gt;&amp;nbsp;for details.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope that helps,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Have a great day,&lt;BR /&gt;Kan&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;-------------------------------------------------------------------------------&lt;BR /&gt;Note:&lt;BR /&gt;- If this post answers your question, please click the "Mark Correct" button. Thank you!&lt;BR /&gt;- We are following threads for 7 weeks after the last post, later replies are ignored&lt;BR /&gt;Please open a new thread and refer to the closed one, if you have a related question at a later point in time.&lt;BR /&gt;-------------------------------------------------------------------------------&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Nov 2023 07:50:04 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/RT117x-key-wrap-algorithm-for-OTFAD/m-p/1766282#M27805</guid>
      <dc:creator>Kan_Li</dc:creator>
      <dc:date>2023-11-30T07:50:04Z</dc:date>
    </item>
    <item>
      <title>Re: RT117x: key wrap algorithm for OTFAD</title>
      <link>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/RT117x-key-wrap-algorithm-for-OTFAD/m-p/1766472#M27812</link>
      <description>&lt;OL&gt;&lt;LI&gt;I read. In fact I read that ECB decryption is nothing but the InvCiher described in that paper.&lt;BR /&gt;And that's what I tried, unsuccessfully.&lt;/LI&gt;&lt;LI&gt;OK&lt;/LI&gt;&lt;LI&gt;This is what I had already found here:&lt;A href="https://community.nxp.com/t5/i-MX-RT/image-enc2-zip-download/m-p/1174943#M10980" target="_blank"&gt;https://community.nxp.com/t5/i-MX-RT/image-enc2-zip-download/m-p/1174943#M10980&lt;/A&gt;&lt;BR /&gt;These sources cannot be those of the tool distributed with Provisioning tool v6&lt;BR /&gt;Look at the command help:&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ptv6_image_enc_help.png" style="width: 779px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/252297i64C70AB7FCFE3489/image-size/large?v=v2&amp;amp;px=999" role="button" title="ptv6_image_enc_help.png" alt="ptv6_image_enc_help.png" /&gt;&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;Is now look at the sources:&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image_enc_sources.png" style="width: 745px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/252298i68D6914B976AA3CA/image-size/large?v=v2&amp;amp;px=999" role="button" title="image_enc_sources.png" alt="image_enc_sources.png" /&gt;&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;they do not absolutely match&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;Please help me! I'll share the otfad_keyblob.bin file with you (it's an "easy" key&lt;BR /&gt;test anyway). Can you try to write the sources (or a python script) to unwrap it (try them) and share the code with me?&lt;/P&gt;&lt;P&gt;I generated the file with this command line:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;/opt/nxp/MCUX_Provi_v6/bin/tools/image_enc/linux/amd64/image_enc \
    ifile=Infile_nopadding.bin \
    ofile=Outfile.enc.bin \
    base_addr=0x30001000 \
    kek=ffeeddccbbaa99887766554433221100 \
    otfad_arg=[00112233445566778899aabbccddeeff,0123456789abcdef,0x30001000,0x1c000] \
    otfad_ctx_lock=0,0,0,0 \
    is_boot_image=0 \
    hw_eng=otfad&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the meantime, I delved into SPSDK, and wrote another python script:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import argparse
import sys
from cryptography.hazmat.primitives import keywrap

if __name__ == '__main__':
    parser = argparse.ArgumentParser(conflict_handler="resolve")
    parser.add_argument("infile", help="input file name")
    parser.add_argument("kek", help="kek")
    args = parser.parse_args()

    with open(args.infile, "rb") as inFile:
        wrapped_ciphertext = inFile.read()[:64]

    # print(' '.join(['%02x' % x for x in wrapped_ciphertext]))
    kek = bytearray.fromhex(args.kek)
    print(' '.join(['%02x' % x for x in kek]))
    try:
        unwrapped_plaintext = keywrap.aes_key_unwrap(kek, wrapped_ciphertext)
        sys.stdout.write(unwrapped_plaintext) # you must pipe the stdout of this script to `hd`
    except:
        print("Failed to unwrap")
        sys.exit(1)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But even then it doesn't work&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;best regards&lt;/P&gt;&lt;P&gt;Max&lt;/P&gt;</description>
      <pubDate>Thu, 30 Nov 2023 11:40:01 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/RT117x-key-wrap-algorithm-for-OTFAD/m-p/1766472#M27812</guid>
      <dc:creator>mastupristi</dc:creator>
      <dc:date>2023-11-30T11:40:01Z</dc:date>
    </item>
    <item>
      <title>Re: RT117x: key wrap algorithm for OTFAD</title>
      <link>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/RT117x-key-wrap-algorithm-for-OTFAD/m-p/1776443#M28074</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/124967"&gt;@mastupristi&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;I will ask SPSDK developers regarding key wrapping function.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Lots of people is already on holidays, so it might take some time.&lt;/P&gt;
&lt;P&gt;Thank you very much for your understanding.&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;BR /&gt;Martin H.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Dec 2023 14:54:53 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/RT117x-key-wrap-algorithm-for-OTFAD/m-p/1776443#M28074</guid>
      <dc:creator>martin_hrncarek</dc:creator>
      <dc:date>2023-12-18T14:54:53Z</dc:date>
    </item>
    <item>
      <title>Re: RT117x: key wrap algorithm for OTFAD</title>
      <link>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/RT117x-key-wrap-algorithm-for-OTFAD/m-p/1776463#M28075</link>
      <description>&lt;P&gt;Hello Martin,&lt;/P&gt;&lt;P&gt;Wow, I must say, your timing is almost as impeccable as Santa's sleigh on a foggy Christmas Eve! It took a neat four weeks to tell me that everyone's on holiday for Christmas. Here I was, thinking my calendar was playing tricks on me, because last I checked, four weeks ago wasn't quite the season to be jolly just yet.&lt;/P&gt;&lt;P&gt;I can't help but feel a bit like I'm in a holiday comedy special - you know, the one where the main character waits forever for a plot twist, only to find out it's just another commercial break. I do appreciate the update, though it's a bit like receiving a Christmas gift in January.&lt;/P&gt;&lt;P&gt;No rush though, I guess good things come to those who wait... and wait... and wait some more. Looking forward to hearing from you in the New Year – or maybe Easter?&lt;/P&gt;&lt;P&gt;Cheers and happy holidays,&lt;/P&gt;&lt;P&gt;Max&lt;/P&gt;</description>
      <pubDate>Mon, 18 Dec 2023 15:22:23 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/RT117x-key-wrap-algorithm-for-OTFAD/m-p/1776463#M28075</guid>
      <dc:creator>mastupristi</dc:creator>
      <dc:date>2023-12-18T15:22:23Z</dc:date>
    </item>
    <item>
      <title>Re: RT117x: key wrap algorithm for OTFAD</title>
      <link>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/RT117x-key-wrap-algorithm-for-OTFAD/m-p/1777089#M28087</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/124967"&gt;@mastupristi&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;the full OTFAD keyblob unwrap function is not available in SPSDK. I have created a ticket to add this function for verification purposes. Difficult to estimate when it will be integrated as the team is busy with high priority tasks.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let me explain how it works:&lt;/P&gt;
&lt;P&gt;Current SPSDK contains &lt;STRONG&gt;aes_key_wrap/unwrap&lt;/STRONG&gt; present in the s&lt;STRONG&gt;psdk-&amp;gt;crypto-&amp;gt;symmetric.py&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;aes_key_wrap &lt;/STRONG&gt;is used in the &lt;STRONG&gt;"def export"&lt;/STRONG&gt; function available in&amp;nbsp;&lt;STRONG&gt;spsdk-&amp;gt;utils-crypto-&amp;gt;otfad.py &lt;/STRONG&gt;file.&lt;STRONG&gt;&lt;BR /&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;The OTFAD configuration structure is composed of 4 blocks to support 4 keys along with corresponding memory regions.&lt;/P&gt;
&lt;P&gt;In &lt;STRONG&gt;export&lt;/STRONG&gt; function each block of 40 bytes is wrapped, then it is aligned to 64 bytes by zeros. This is called 4 times. In total the OFAD keyblobk is 256 bytes, concatenation of 4 wrapped blocks + padding.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It means that &lt;STRONG&gt;aes_key_unwrap&lt;/STRONG&gt; must be call 4 times to unwrap only the 40 bytes per each blocks (need to points to the correct data within the keyblob).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;OTFAD keyblob:&lt;BR /&gt;-------------------------------&lt;BR /&gt;40 bytes of&amp;nbsp;wrapped&amp;nbsp; data - block 1 &lt;STRONG&gt;- &amp;gt; this data must be unwrapped&lt;/STRONG&gt;&lt;BR /&gt;16 bytes of zeros&lt;BR /&gt;-------------------------------&lt;BR /&gt;40 bytes of wrapped&amp;nbsp; data - block 2&amp;nbsp; &lt;STRONG&gt;- &amp;gt; this data must be unwrapped&lt;/STRONG&gt;&lt;BR /&gt;16 bytes of zeros&lt;BR /&gt;-------------------------------&lt;BR /&gt;40 bytes of wrapped data - block 3&amp;nbsp; &lt;STRONG&gt;- &amp;gt; this data must be unwrapped&lt;/STRONG&gt;&lt;BR /&gt;16 bytes of zeros&lt;BR /&gt;-------------------------------&lt;BR /&gt;40 bytes of wrapped data - block 4 -&lt;STRONG&gt; &amp;gt; this data must be unwrapped&lt;/STRONG&gt;&lt;BR /&gt;16 bytes of zeros&lt;BR /&gt;-------------------------------&lt;/P&gt;
&lt;P&gt;Let me know if you have any questions.&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;BR /&gt;Martin H.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Dec 2023 09:59:07 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/RT117x-key-wrap-algorithm-for-OTFAD/m-p/1777089#M28087</guid>
      <dc:creator>martin_hrncarek</dc:creator>
      <dc:date>2023-12-19T09:59:07Z</dc:date>
    </item>
    <item>
      <title>Re: RT117x: key wrap algorithm for OTFAD</title>
      <link>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/RT117x-key-wrap-algorithm-for-OTFAD/m-p/1777231#M28091</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;P&gt;Current SPSDK contains &lt;STRONG&gt;aes_key_wrap/unwrap&lt;/STRONG&gt; present in the s&lt;STRONG&gt;psdk-&amp;gt;crypto-&amp;gt;symmetric.py&lt;/STRONG&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;yes I knew that, in fact I wrote my last attempt based on that.&lt;/P&gt;&lt;P&gt;They use this cryptography.hazmat:&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/nxp-mcuxpresso/spsdk/blob/0144584ce804eb1c4152bbef18bd43074c500289/spsdk/crypto/symmetric.py#L69" target="_blank"&gt;https://github.com/nxp-mcuxpresso/spsdk/blob/0144584ce804eb1c4152bbef18bd43074c500289/spsdk/crypto/symmetric.py#L69&lt;/A&gt;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="unwrap.png" style="width: 797px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/255083iE3AEA4F3CD08D377/image-size/large?v=v2&amp;amp;px=999" role="button" title="unwrap.png" alt="unwrap.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;the description of the parameters is clear and I wanted to try it too:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import argparse
import sys
from cryptography.hazmat.primitives import keywrap

if __name__ == '__main__':
    parser = argparse.ArgumentParser(conflict_handler="resolve")
    parser.add_argument("infile", help="input file name")
    parser.add_argument("kek", help="kek")
    parser.add_argument("bytes", help="bytes", type=int)
    args = parser.parse_args()

    with open(args.infile, "rb") as inFile:
        wrapped_ciphertext = inFile.read()[:args.bytes]

    kek = bytearray.fromhex(args.kek)
    try:
        unwrapped_plaintext = keywrap.aes_key_unwrap(kek, wrapped_ciphertext)
        sys.stdout.write(unwrapped_plaintext) # you must pipe the stdout of this script to `hd`
    except:
        print("Failed to unwrap")
        sys.exit(1)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;however it does not work.&lt;/P&gt;&lt;P&gt;My keyblob (note that this is working on my EVK using the KEK ffeeddccbbaa99887766554433221100) is like this:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;00000000 3f e3 81 f6 e8 a1 47 d2 fa 6c ca 69 53 c6 1c 80 |?.....G..l.iS...|&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;00000010 1f 4a a3 ea f9 4f 10 f2 90 71 3c 52 5b 39 a6 a4 |.J...O...q&amp;lt;R[9..|&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;00000020 ba 19 b1 3b b5 48 ae 3f 73 b6 ac c6 60 6a 2a b2 |...;.H.?s...`j*.|&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;00000040 79 0f 0d c7 d4 4d 61 a1 de 7f f4 09 45 3e fa f0 |y....Ma.....E&amp;gt;..|&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;00000050 cb 74 01 13 0e 12 12 c0 56 1f bb 89 e3 73 ac 44 |.t......V....s.D|&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;00000060 11 a2 40 d1 52 1d 7f ec b0 4b 40 d5 16 3b 84 0c |..@.R....K@..;..|&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;00000070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;00000080 79 0f 0d c7 d4 4d 61 a1 de 7f f4 09 45 3e fa f0 |y....Ma.....E&amp;gt;..|&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;00000090 cb 74 01 13 0e 12 12 c0 56 1f bb 89 e3 73 ac 44 |.t......V....s.D|&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;000000a0 11 a2 40 d1 52 1d 7f ec b0 4b 40 d5 16 3b 84 0c |..@.R....K@..;..|&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;000000b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;000000c0 79 0f 0d c7 d4 4d 61 a1 de 7f f4 09 45 3e fa f0 |y....Ma.....E&amp;gt;..|&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;000000d0 cb 74 01 13 0e 12 12 c0 56 1f bb 89 e3 73 ac 44 |.t......V....s.D|&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;000000e0 11 a2 40 d1 52 1d 7f ec b0 4b 40 d5 16 3b 84 0c |..@.R....K@..;..|&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;000000f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;you can clearly see the 4 blocks, where only the first one has values, and the others are not used (I think they are populated with 0). Padding to 0 is evident.&amp;nbsp;The encrypted part is evidently in the first 48 bytes of the block.&lt;/P&gt;&lt;P&gt;So to the aes_key_unwrap() function I pass 48 bytes and the kek. But despite this it fails to decode.&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;The full OTFAD keyblob unwrap function is not available in SPSDK. I have created a ticket to add this function for verification purposes. Difficult to estimate when it will be integrated as the team is busy with high priority tasks.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;OK, I'm glad that in the SPSDK it was asked to add such a function, but in the meantime can you please review my python source and tell me where I'm wrong?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;regards&lt;/P&gt;&lt;P&gt;Max&lt;/P&gt;</description>
      <pubDate>Tue, 19 Dec 2023 14:18:33 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/RT117x-key-wrap-algorithm-for-OTFAD/m-p/1777231#M28091</guid>
      <dc:creator>mastupristi</dc:creator>
      <dc:date>2023-12-19T14:18:33Z</dc:date>
    </item>
    <item>
      <title>Re: RT117x: key wrap algorithm for OTFAD</title>
      <link>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/RT117x-key-wrap-algorithm-for-OTFAD/m-p/1781255#M28231</link>
      <description>&lt;P&gt;Trying to approach the problem from a different point, I made other findings that contradict the documentation. Please read all this post&lt;/P&gt;&lt;P&gt;I made a point. To validate my algorithms, it is preferable to test on the "unused" contexts. And it's most likely better to validate a wrapping procedure to get useful information for unwrapping. In fact by wrapping I have both the input data (the plain-text data structure that in unused contexts should be all 0), I have the ciphertext and the KEK key.&lt;/P&gt;&lt;P&gt;I will proceed in the chronological order that I followed.&lt;/P&gt;&lt;P&gt;the command line that was used to create the keyblob used image_enc:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="old_cmd_line.png" style="width: 999px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/256251i3C6A0784C5B74E98/image-size/large?v=v2&amp;amp;px=999" role="button" title="old_cmd_line.png" alt="old_cmd_line.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I highlighted the second context that is not used, and all fields are 0&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="keyblob_dump.png" style="width: 947px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/256252i177AFA67AA58D7C3/image-size/large?v=v2&amp;amp;px=999" role="button" title="keyblob_dump.png" alt="keyblob_dump.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;at this point I wanted to try to do the same thing but using spsdk taken from github (nxpimage.py, version 2.0.1, commit 0144584ce8), trying to follow the execution in debug.&lt;/P&gt;&lt;P&gt;I added some print within the code to "see" KEK, plain-text and cipher text&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="instrumented_keywrap.png" style="width: 813px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/256253i7C6F7FB8B2F3C2E5/image-size/large?v=v2&amp;amp;px=999" role="button" title="instrumented_keywrap.png" alt="instrumented_keywrap.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;In doing this I realized that the plaintext is not all 0 but has 4 non-zero bytes:&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="instrumented_out.png" style="width: 999px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/256254iD4EE8C4A59FB9C06/image-size/large?v=v2&amp;amp;px=999" role="button" title="instrumented_out.png" alt="instrumented_out.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;the cipher text is exactly that contained in the keyblob file produced by image_enc. However, the 40-byte plaintext has the last 4 non-zero bytes&lt;/P&gt;&lt;P&gt;the place where these bytes are populated is in the otfad.py file:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="plain_text_construct.png" style="width: 999px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/256255iF1BF22DEF5F7F4DF/image-size/large?v=v2&amp;amp;px=999" role="button" title="plain_text_construct.png" alt="plain_text_construct.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The only documentation I have found so far is this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="doc.png" style="width: 999px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/256258iE2EF6FC6276B5E60/image-size/large?v=v2&amp;amp;px=999" role="button" title="doc.png" alt="doc.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;It is possible to use it for testing but they recommend using random values, and this, as we shall see, contradicts the documentation. In my case &lt;EM&gt;zero_fill&lt;/EM&gt; is a 4-byte array 0-filled while &lt;EM&gt;crc&lt;/EM&gt; is None.&lt;/P&gt;&lt;P&gt;This isn't documented anywhere else (from the research I've done so far), in fact, on the Security Reference Manual it is extensively written that padding &lt;U&gt;&lt;STRONG&gt;must&lt;/STRONG&gt;&lt;/U&gt; be 0:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="zeroPadding_0.png" style="width: 996px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/256256iEBE7974F78FA6E99/image-size/large?v=v2&amp;amp;px=999" role="button" title="zeroPadding_0.png" alt="zeroPadding_0.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="zeroPadding_1.png" style="width: 999px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/256257i5956116A2EF35F00/image-size/large?v=v2&amp;amp;px=999" role="button" title="zeroPadding_1.png" alt="zeroPadding_1.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I have highlighted the padding bytes, and in red those that are not actually 0.&lt;/P&gt;&lt;P&gt;so other questions to add to the others are related to this:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;what are those four bytes? Where are they documented?&lt;/LI&gt;&lt;LI&gt;Are they used by the rom bootloader? And to do what with them? From the code it looks like in all cases they have to do with CRC&lt;/LI&gt;&lt;LI&gt;is the documentation wrong?&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;regards&lt;/P&gt;&lt;P&gt;Max&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jan 2024 07:48:41 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/RT117x-key-wrap-algorithm-for-OTFAD/m-p/1781255#M28231</guid>
      <dc:creator>mastupristi</dc:creator>
      <dc:date>2024-01-03T07:48:41Z</dc:date>
    </item>
    <item>
      <title>Re: RT117x: key wrap algorithm for OTFAD</title>
      <link>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/RT117x-key-wrap-algorithm-for-OTFAD/m-p/1787066#M28423</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/19880"&gt;@martin_hrncarek&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;in these two weeks have you had a chance to elaborate on what I said in my last post?&lt;/P&gt;&lt;P&gt;I lean toward believing that it is the documentation that needs to align with what SPSDK has implemented. After all, the implementers of SPSDK will have followed some specification.&lt;/P&gt;&lt;P&gt;Please answer my questions at the bottom of the previous post, and I suggest you contact the team responsible for documentation to solicit the necessary fixes&lt;/P&gt;&lt;P&gt;regards&lt;/P&gt;&lt;P&gt;Max&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2024 09:19:31 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/RT117x-key-wrap-algorithm-for-OTFAD/m-p/1787066#M28423</guid>
      <dc:creator>mastupristi</dc:creator>
      <dc:date>2024-01-11T09:19:31Z</dc:date>
    </item>
  </channel>
</rss>

