[riot-notifications] [RIOT-OS/RIOT] sys/crypto: Enable support for AES-192, AES-256 (#16183)

Nils Ollrogge notifications at github.com
Wed Apr 14 22:35:14 CEST 2021


As requested here is the code to parse the testvectors. It is a little specific for the AES_CCM usecase.

```python
import sys
import re
import os

TEST_PREFIX = ""
TEST_GROUP = -1
TEST_NUMBER = -1

def hex_to_arr(inp):
    out = ""
    for i in range(0, len(inp), 2):
        if i % 16 == 0 and i > 0:
            out += "\n"
        out += f"0x{inp[i:i+2]}, "

    return out

def parse_mac_len(f, l):
    global TEST_GROUP
    global TEST_PREFIX

    mac = l.rstrip().split('Tlen = ')[-1][:-1]
    mac = int(mac, 10)

    tmp = f"static const uint8_t TEST_{TEST_PREFIX}_GROUP_{TEST_GROUP}_MAC_LEN = {mac};\n\n"
    f.write(tmp)

def parse_group_header(f, l):
    tmp = l.replace('[', '/* ')
    tmp = tmp.replace(']', ' */')
    f.write(tmp)

    parse_mac_len(f, l)

def parse_key_len(f, n):
    global TEST_PREFIX
    global TEST_NUMBER
    tmp = f"static const size_t TEST_{TEST_PREFIX}_GROUP_{TEST_GROUP}_KEY_LEN = {n};\n\n"
    f.write(tmp)

def parse_nonce_len(b, n):
    global TEST_PREFIX
    global TEST_NUMBER
    tmp = f"static const size_t TEST_{TEST_PREFIX}_{TEST_NUMBER}_NONCE_LEN = {n};\n\n"
    b.append(tmp)

def parse_adata_len(b, n):
    global TEST_PREFIX
    global TEST_NUMBER
    tmp = f"static const size_t TEST_{TEST_PREFIX}_{TEST_NUMBER}_ADATA_LEN = {n};\n\n"
    b.append(tmp)

def parse_ciphertext_len(b, n):
    global TEST_PREFIX
    global TEST_NUMBER
    tmp = f"static const size_t TEST_{TEST_PREFIX}_{TEST_NUMBER}_EXPECTED_LEN = {n};\n\n"
    b.append(tmp)

def parse_input_len(b, n):
    global TEST_PREFIX
    global TEST_NUMBER
    tmp = f"static const size_t TEST_{TEST_PREFIX}_{TEST_NUMBER}_INPUT_LEN = {n};\n\n"
    b.append(tmp)

def parse_key(f, l):
    global TEST_PREFIX
    global TEST_GROUP

    l = l.strip().split('= ')[-1]
    key = f"static const uint8_t TEST_{TEST_PREFIX}_GROUP_{TEST_GROUP}_KEY[] = {{ \n"
    arr =  hex_to_arr(l.rstrip().split('= ')[-1])
    key += arr
    key += "\n};\n"
    f.write(key)

    parse_key_len(f, len(l) // 2)

def parse_test_number(l):
    global TEST_NUMBER
    TEST_NUMBER = int(l.rstrip().split('= ')[-1], 10)

def parse_nonce(b, l):
    global TEST_PREFIX
    global TEST_NUMBER

    l = l.strip().split('= ')[-1]

    nonce = f"static const uint8_t TEST_{TEST_PREFIX}_{TEST_NUMBER}_NONCE[] = {{ \n"
    arr = hex_to_arr(l)
    nonce += arr
    nonce += "\n};\n"
    b.append(nonce)

    if len(l) == 2:
        l = ""

    parse_nonce_len(b, len(l) // 2)

def parse_adata(b, l):
    global TEST_PREFIX
    global TEST_NUMBER

    l = l.strip().split('= ')[-1]

    adata = f"static const uint8_t TEST_{TEST_PREFIX}_{TEST_NUMBER}_ADATA[] = {{ \n"
    arr = hex_to_arr(l)
    adata += arr
    adata += "\n};\n"
    b.append(adata)

    if len(l) == 2:
        l = ""

    parse_adata_len(b, len(l) // 2)

def parse_ciphertext(b, l):
    global TEST_PREFIX
    global TEST_NUMBER

    l = l.strip().split('= ')[-1]

    ciphertext = f"static const uint8_t TEST_{TEST_PREFIX}_{TEST_NUMBER}_EXPECTED[] = {{ \n"
    arr = hex_to_arr(l)
    ciphertext += arr
    ciphertext += "\n};\n"
    b.append(ciphertext)

    if len(l) == 2:
        l = ""

    parse_ciphertext_len(b, len(l) // 2)

def parse_input(b, l):
    global TEST_PREFIX
    global TEST_NUMBER

    l = l.strip().split('= ')[-1]

    inp = f"static const uint8_t TEST_{TEST_PREFIX}_{TEST_NUMBER}_INPUT[] = {{ \n"
    arr = hex_to_arr(l)
    inp += arr
    inp += "\n};\n"
    b.append(inp)

    if len(l) == 2:
        l = ""

    parse_input_len(b, len(l) // 2)

def commit_testcase(f, tc):
    for l in tc:
        f.write(l)

def add_test_calls(f, test_groups, test_nums):
    global TEST_PREFIX
    global TEST_GROUP

    buf = "static void test_crypto_modes_ccm_decrypt2(void)\n"
    buf += "{\n"

    cnt = 0
    for i, group_cnt in enumerate(test_groups):
        for j in range(group_cnt):
            buf += f"do_test_decrypt_op({TEST_PREFIX}, {test_nums[cnt]}, {i});\n"

            cnt += 1

    buf += "}\n"

    buf += "static void test_crypto_modes_ccm_encrypt2(void)\n"
    buf += "{\n"

    cnt = 0
    for i, group_cnt in enumerate(test_groups):
        for j in range(group_cnt):
            buf += f"do_test_encrypt_op({TEST_PREFIX}, {test_nums[cnt]}, {i});\n"

            cnt += 1

    buf += "}\n"

    f.write(buf)

def do_work():
    global TEST_PREFIX
    global TEST_GROUP

    with open(sys.argv[1], "r") as f:
        data = f.readlines()

    TEST_PREFIX = os.path.basename(sys.argv[1]).split('.')[0]

    with open("./test/out.c", "w") as f:
        f.write('''
        #include <limits.h>

        #include <stdlib.h>
        #include <stdio.h>
        #include <string.h>

        #include "embUnit.h"
        #include "crypto/ciphers.h"
        #include "crypto/modes/ccm.h"
        #include "tests-crypto.h"

        static const size_t nonce_and_len_encoding_size = 15;

        ''')

        test_groups = []
        test_nums = []
        test_case = []

        group_cnt = -1

        for l in data:
            if l[0] == '[':
                TEST_GROUP += 1
                parse_group_header(f, l)
                if group_cnt > 0:
                    test_groups.append(group_cnt)

                group_cnt = 0
            elif "Key " in l:
                parse_key(f, l)
            elif "Count " in l:
                test_case = []
                parse_test_number(l)
            elif "Nonce " in l:
                parse_nonce(test_case, l)
            elif "Adata " in l:
                parse_adata(test_case, l)
            elif "CT " in l:
                parse_ciphertext(test_case, l)
            elif "Payload " in l:
                parse_input(test_case, l)
                commit_testcase(f, test_case)

                test_nums.append(TEST_NUMBER)
                group_cnt += 1

        test_groups.append(group_cnt)

        with open("./skeleton.c") as f2:
            f.write(f2.read())

        add_test_calls(f, test_groups, test_nums)

if __name__ == "__main__":
    do_work()
```

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/RIOT-OS/RIOT/pull/16183#issuecomment-819814108
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20210414/e6096ee8/attachment-0001.htm>


More information about the notifications mailing list