blob: 5d46441815e0c0a41c0a7d95d60b347c7fc7f109 [file] [log] [blame]
Julian Halla7e89b02020-11-23 17:33:31 +01001/*
Julian Hall7d7b24c2021-08-13 13:40:38 +01002 * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
Julian Halla7e89b02020-11-23 17:33:31 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include "uuid.h"
8#include <string.h>
9#include <ctype.h>
Julian Hall7d7b24c2021-08-13 13:40:38 +010010
11static uint8_t hex_to_nibble(char hex)
12{
13 uint8_t nibble = 0;
14
15 if (hex >= '0' && hex <= '9') {
16 nibble = hex - '0';
17 }
18 else {
19 nibble = ((hex | 0x20) - 'a') + 10;
20 }
21
22 return nibble;
23}
24
25static uint8_t hex_to_byte(const char *hex)
26{
27 /* Takes a validated input and returns the byte value */
28 uint8_t byte = hex_to_nibble(hex[0]) << 4;
29 byte |= (hex_to_nibble(hex[1]) & 0x0f);
30 return byte;
31}
Julian Halla7e89b02020-11-23 17:33:31 +010032
33size_t uuid_is_valid(const char *canonical_form)
34{
Julian Hall7d7b24c2021-08-13 13:40:38 +010035 size_t valid_chars = 0;
36 size_t input_len = strlen(canonical_form);
Julian Halla7e89b02020-11-23 17:33:31 +010037
Julian Hall7d7b24c2021-08-13 13:40:38 +010038 if (input_len >= UUID_CANONICAL_FORM_LEN) {
Julian Halla7e89b02020-11-23 17:33:31 +010039
Julian Hall7d7b24c2021-08-13 13:40:38 +010040 size_t i;
41 valid_chars = UUID_CANONICAL_FORM_LEN;
Julian Halla7e89b02020-11-23 17:33:31 +010042
Julian Hall7d7b24c2021-08-13 13:40:38 +010043 for (i = 0; i < UUID_CANONICAL_FORM_LEN; ++i) {
Julian Halla7e89b02020-11-23 17:33:31 +010044
Julian Hall7d7b24c2021-08-13 13:40:38 +010045 if (i == 8 || i == 13 || i == 18 || i == 23) {
46 if (canonical_form[i] != '-') return 0;
47 }
48 else {
49 if (!isxdigit(canonical_form[i])) return 0;
50 }
51 }
52 }
Julian Halla7e89b02020-11-23 17:33:31 +010053
Julian Hall7d7b24c2021-08-13 13:40:38 +010054 return valid_chars;
Julian Halla7e89b02020-11-23 17:33:31 +010055}
56
57size_t uuid_parse_to_octets(const char *canonical_form, uint8_t *buf, size_t buf_size)
58{
Julian Hall7d7b24c2021-08-13 13:40:38 +010059 size_t octet_index = 0;
60 const char *pos;
61 size_t valid_chars = uuid_is_valid(canonical_form);
Julian Halla7e89b02020-11-23 17:33:31 +010062
Julian Hall7d7b24c2021-08-13 13:40:38 +010063 if ((buf_size < UUID_OCTETS_LEN) ||
64 (valid_chars != UUID_CANONICAL_FORM_LEN)) {
65 /* Invalid input */
66 return 0;
67 }
Julian Halla7e89b02020-11-23 17:33:31 +010068
Julian Hall7d7b24c2021-08-13 13:40:38 +010069 /*
70 * UUID string has been validates as having the following form:
71 * xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
72 * 4 2 2 2 6
73 */
74 pos = &canonical_form[0];
75 while (octet_index < 4) {
76 buf[octet_index++] = hex_to_byte(pos);
77 pos += 2;
78 }
Julian Halla7e89b02020-11-23 17:33:31 +010079
Julian Hall7d7b24c2021-08-13 13:40:38 +010080 pos = &canonical_form[9];
81 while (octet_index < 6) {
82 buf[octet_index++] = hex_to_byte(pos);
83 pos += 2;
84 }
Julian Halla7e89b02020-11-23 17:33:31 +010085
Julian Hall7d7b24c2021-08-13 13:40:38 +010086 pos = &canonical_form[14];
87 while (octet_index < 8) {
88 buf[octet_index++] = hex_to_byte(pos);
89 pos += 2;
90 }
Julian Halla7e89b02020-11-23 17:33:31 +010091
Julian Hall7d7b24c2021-08-13 13:40:38 +010092 pos = &canonical_form[19];
93 while (octet_index < 10) {
94 buf[octet_index++] = hex_to_byte(pos);
95 pos += 2;
96 }
Julian Halla7e89b02020-11-23 17:33:31 +010097
Julian Hall7d7b24c2021-08-13 13:40:38 +010098 pos = &canonical_form[24];
99 while (octet_index < 16) {
100 buf[octet_index++] = hex_to_byte(pos);
101 pos += 2;
102 }
Julian Halla7e89b02020-11-23 17:33:31 +0100103
Julian Hall7d7b24c2021-08-13 13:40:38 +0100104 return valid_chars;
Julian Halla7e89b02020-11-23 17:33:31 +0100105}
106
107/*
108 * TODO: Temorary workaround for optee compatibility
109 * The byte order is reversed for the first 4 bytes, then 2 bytes, then 2 bytes.
110 * This is because the UUID type in OP-TEE consists of an uint32_t, 2x uint16_t,
111 * then uint8_t array.
112 */
113size_t uuid_parse_to_octets_reversed(const char *canonical_form, uint8_t *buf, size_t buf_size)
114{
Julian Hall7d7b24c2021-08-13 13:40:38 +0100115 size_t valid_chars;
116 uint8_t standard_octets[UUID_OCTETS_LEN];
Julian Halla7e89b02020-11-23 17:33:31 +0100117
Julian Hall7d7b24c2021-08-13 13:40:38 +0100118 valid_chars = uuid_parse_to_octets(canonical_form, standard_octets, sizeof(standard_octets));
Julian Halla7e89b02020-11-23 17:33:31 +0100119
Julian Hall7d7b24c2021-08-13 13:40:38 +0100120 if ((valid_chars == UUID_CANONICAL_FORM_LEN) && (buf_size >= UUID_OCTETS_LEN)) {
121 /* Reverse bytes in each section */
122 buf[0] = standard_octets[3];
123 buf[1] = standard_octets[2];
124 buf[2] = standard_octets[1];
125 buf[3] = standard_octets[0];
Julian Halla7e89b02020-11-23 17:33:31 +0100126
Julian Hall7d7b24c2021-08-13 13:40:38 +0100127 buf[4] = standard_octets[5];
128 buf[5] = standard_octets[4];
Julian Halla7e89b02020-11-23 17:33:31 +0100129
Julian Hall7d7b24c2021-08-13 13:40:38 +0100130 buf[6] = standard_octets[7];
131 buf[7] = standard_octets[6];
Julian Halla7e89b02020-11-23 17:33:31 +0100132
Julian Hall7d7b24c2021-08-13 13:40:38 +0100133 buf[8] = standard_octets[8];
134 buf[9] = standard_octets[9];
Julian Halla7e89b02020-11-23 17:33:31 +0100135
Julian Hall7d7b24c2021-08-13 13:40:38 +0100136 buf[10] = standard_octets[10];
137 buf[11] = standard_octets[11];
138 buf[12] = standard_octets[12];
139 buf[13] = standard_octets[13];
140 buf[14] = standard_octets[14];
141 buf[15] = standard_octets[15];
142 }
Julian Halla7e89b02020-11-23 17:33:31 +0100143
Julian Hall7d7b24c2021-08-13 13:40:38 +0100144 return valid_chars;
Julian Halla7e89b02020-11-23 17:33:31 +0100145}