File: server.c

package info (click to toggle)
mbedtls 3.6.5-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,488 kB
  • sloc: ansic: 164,842; sh: 25,443; python: 15,512; makefile: 3,131; perl: 1,043; tcl: 4
file content (105 lines) | stat: -rw-r--r-- 3,276 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* psasim test server */

/*
 *  Copyright The Mbed TLS Contributors
 *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
 */

#include <psa/service.h>
#include "psa_manifest/manifest.h"
#include <unistd.h>
#include <stdio.h>

void printbits(uint32_t num)
{
    for (int i = 0; i < 32; i++) {
        if ((num >> (31-i) & 0x1)) {
            printf("1");
        } else {
            printf("0");
        }
    }
    printf("\n");
}

#define BUF_SIZE 25

int psa_sha256_main()
{
    psa_status_t ret = PSA_ERROR_PROGRAMMER_ERROR;
    psa_msg_t msg = { -1 };
    char foo[BUF_SIZE] = { 0 };
    const int magic_num = 66;

    puts("Starting");

    while (1) {
        puts("Calling psa_wait");
        psa_signal_t signals = psa_wait(PSA_WAIT_ANY, PSA_BLOCK);

        if (signals > 0) {
            printbits(signals);
        }

        if (signals & PSA_SHA256_SIGNAL) {
            puts("Oooh a signal!");

            if (PSA_SUCCESS == psa_get(PSA_SHA256_SIGNAL, &msg)) {
                printf("My handle is %d\n", msg.handle);
                printf("My rhandle is %p\n", (int *) msg.rhandle);
                switch (msg.type) {
                    case PSA_IPC_CONNECT:
                        puts("Got a connection message");
                        psa_set_rhandle(msg.handle, (void *) &magic_num);
                        ret = PSA_SUCCESS;
                        break;
                    case PSA_IPC_DISCONNECT:
                        puts("Got a disconnection message");
                        ret = PSA_SUCCESS;
                        break;

                    default:
                        printf("Got an IPC call of type %d\n", msg.type);
                        ret = 42;
                        size_t size = msg.in_size[0];

                        if ((size > 0) && (size <= sizeof(foo))) {
                            psa_read(msg.handle, 0, foo, 6);
                            foo[(BUF_SIZE-1)] = '\0';
                            printf("Reading payload: %s\n", foo);
                            psa_read(msg.handle, 0, foo+6, 6);
                            foo[(BUF_SIZE-1)] = '\0';
                            printf("Reading payload: %s\n", foo);
                        }

                        size = msg.out_size[0];
                        if ((size > 0)) {
                            puts("Writing response");
                            psa_write(msg.handle, 0, "RESP", 4);
                            psa_write(msg.handle, 0, "ONSE", 4);
                        }

                        if (msg.client_id > 0) {
                            psa_notify(msg.client_id);
                        } else {
                            puts("Client is non-secure, so won't notify");
                        }

                }

                psa_reply(msg.handle, ret);
            } else {
                puts("Failed to retrieve message");
            }
        } else if (SIGSTP_SIG & signals) {
            puts("Recieved SIGSTP signal. Gonna EOI it.");
            psa_eoi(SIGSTP_SIG);
        } else if (SIGINT_SIG & signals) {
            puts("Handling interrupt!\n");
            puts("Gracefully quitting");
            psa_panic();
        } else {
            puts("No signal asserted");
        }
    }
}