1 /*
2 * GStreamer gstreamer-aeshelper
3 *
4 * Copyright, LCC (C) 2015 RidgeRun, LCC <carsten.behling@ridgerun.com>
5 * Copyright, LCC (C) 2016 RidgeRun, LCC <jose.jimenez@ridgerun.com>
6 * Copyright (C) 2020 Nice, Contact: Rabindra Harlalka <Rabindra.Harlalka@nice.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 * Alternatively, the contents of this file may be used under the
27 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
28 * which case the following provisions apply instead of the ones
29 * mentioned above:
30 *
31 * This library is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU Library General Public
33 * License as published by the Free Software Foundation; either
34 * version 2 of the License, or (at your option) any later version.
35 *
36 * This library is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 * Library General Public License for more details.
40 *
41 * You should have received a copy of the GNU Library General Public
42 * License along with this library; if not, write to the
43 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
44 * Boston, MA 02110-1335, USA.
45 */
46
47 #ifdef HAVE_CONFIG_H
48 # include <config.h>
49 #endif
50
51 #include "gstaeshelper.h"
52
53 GType
gst_aes_cipher_get_type(void)54 gst_aes_cipher_get_type (void)
55 {
56 static GType aes_cipher_type = 0;
57
58 if (g_once_init_enter (&aes_cipher_type)) {
59 static GEnumValue aes_cipher_types[] = {
60 {GST_AES_CIPHER_128_CBC, "AES 128 bit cipher key using CBC method",
61 "aes-128-cbc"},
62 {GST_AES_CIPHER_256_CBC,
63 "AES 256 bit cipher key using CBC method",
64 "aes-256-cbc"},
65 {0, NULL, NULL},
66 };
67
68 GType temp = g_enum_register_static ("GstAesCipher",
69 aes_cipher_types);
70
71 g_once_init_leave (&aes_cipher_type, temp);
72 }
73
74 return aes_cipher_type;
75 }
76
77 const gchar *
gst_aes_cipher_enum_to_string(GstAesCipher cipher)78 gst_aes_cipher_enum_to_string (GstAesCipher cipher)
79 {
80 switch (cipher) {
81 case GST_AES_CIPHER_128_CBC:
82 return "aes-128-cbc";
83 break;
84 case GST_AES_CIPHER_256_CBC:
85 return "aes-256-cbc";
86 break;
87 }
88
89 return "";
90 }
91
92
93 gchar
gst_aes_nibble_to_hex(gchar in)94 gst_aes_nibble_to_hex (gchar in)
95 {
96 return in < 10 ? in + 48 : in + 55;
97 }
98
99 /*
100 * gst_aes_bytearray2hexstring
101 *
102 * convert array of bytes to hex string
103 *
104 * @param in input byte array
105 * @param out allocated hex string for output
106 * @param len length of input byte array
107 *
108 * @return output hex string
109 */
110 gchar *
gst_aes_bytearray2hexstring(const guchar * in,gchar * const out,const gushort len)111 gst_aes_bytearray2hexstring (const guchar * in, gchar * const out,
112 const gushort len)
113 {
114 gushort i;
115 gchar high;
116 gchar low;
117
118 for (i = 0; i < len; i++) {
119 high = (in[i] & 0xF0) >> 4;
120 low = in[i] & 0x0F;
121 out[i * 2] = gst_aes_nibble_to_hex (high);
122 out[i * 2 + 1] = gst_aes_nibble_to_hex (low);
123 }
124 out[len * 2] = 0; /* null terminate */
125
126 return out;
127 }
128
129 /*
130 * gst_aes_hexstring2bytearray
131 *
132 * convert hex string to array of bytes
133 *
134 * @param filter calling element
135 * @param in input hex string
136 * @param allocated byte array for output
137 *
138 * @return output byte array
139 */
140 guint
gst_aes_hexstring2bytearray(GstElement * filter,const gchar * in,guchar * out)141 gst_aes_hexstring2bytearray (GstElement * filter, const gchar * in,
142 guchar * out)
143 {
144 gchar byte_val;
145 guint hex_count = 0;
146
147 GST_LOG_OBJECT (filter, "Converting hex string to number");
148
149 g_return_val_if_fail (in && out, 0);
150
151 while (*in != 0) {
152 /* Compute fist half-byte */
153 if (*in >= 'A' && *in <= 'F') {
154 byte_val = (*in - 55) << 4;
155 } else if (*in >= 'a' && *in <= 'f') {
156 byte_val = (*in - 87) << 4;
157 } else if (*in >= '0' && *in <= '9') {
158 byte_val = (*in - 48) << 4;
159 } else {
160 return 0;
161 }
162 in++;
163 if (*in == 0) {
164 break;
165 }
166 /* Compute second half-byte */
167 if (*in >= 'A' && *in <= 'F') {
168 *out = (*in - 55) + byte_val;
169 } else if (*in >= 'a' && *in <= 'f') {
170 *out = (*in - 87) + byte_val;
171 } else if (*in >= '0' && *in <= '9') {
172 *out = (*in - 48) + byte_val;
173 } else {
174 return 0;
175 }
176
177 GST_LOG_OBJECT (filter, "ch: %c%c, hex: 0x%x", *(in - 1), *in, *out);
178 in++;
179 out++;
180 if (!in || !out)
181 return 0;
182 hex_count++;
183 }
184 GST_LOG_OBJECT (filter, "Hex string conversion successful");
185
186 return hex_count;
187 }
188