summaryrefslogtreecommitdiff
path: root/ext/aes/gstaeshelper.c
blob: 663593a464b4892287dc51ac9008cab48fb87add (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * GStreamer gstreamer-aeshelper
 *
 * Copyright, LCC (C) 2015 RidgeRun, LCC <carsten.behling@ridgerun.com>
 * Copyright, LCC (C) 2016 RidgeRun, LCC <jose.jimenez@ridgerun.com>
 * Copyright (C) 2020 Nice, Contact: Rabindra Harlalka <Rabindra.Harlalka@nice.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Alternatively, the contents of this file may be used under the
 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
 * which case the following provisions apply instead of the ones
 * mentioned above:
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1335, USA.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include "gstaeshelper.h"

GType
gst_aes_cipher_get_type (void)
{
  static GType aes_cipher_type = 0;

  if (g_once_init_enter (&aes_cipher_type)) {
    static GEnumValue aes_cipher_types[] = {
      {GST_AES_CIPHER_128_CBC, "AES 128 bit cipher key using CBC method",
          "aes-128-cbc"},
      {GST_AES_CIPHER_256_CBC,
            "AES 256 bit cipher key using CBC method",
          "aes-256-cbc"},
      {0, NULL, NULL},
    };

    GType temp = g_enum_register_static ("GstAesCipher",
        aes_cipher_types);

    g_once_init_leave (&aes_cipher_type, temp);
  }

  return aes_cipher_type;
}

const gchar *
gst_aes_cipher_enum_to_string (GstAesCipher cipher)
{
  switch (cipher) {
    case GST_AES_CIPHER_128_CBC:
      return "aes-128-cbc";
      break;
    case GST_AES_CIPHER_256_CBC:
      return "aes-256-cbc";
      break;
  }

  return "";
}


gchar
gst_aes_nibble_to_hex (gchar in)
{
  return in < 10 ? in + 48 : in + 55;
}

/*
 * gst_aes_bytearray2hexstring
 *
 * convert array of bytes to hex string
 *
 * @param in input byte array
 * @param out allocated hex string for output
 * @param len length of input byte array
 *
 * @return output hex string
 */
gchar *
gst_aes_bytearray2hexstring (const guchar * in, gchar * const out,
    const gushort len)
{
  gushort i;
  gchar high;
  gchar low;

  for (i = 0; i < len; i++) {
    high = (in[i] & 0xF0) >> 4;
    low = in[i] & 0x0F;
    out[i * 2] = gst_aes_nibble_to_hex (high);
    out[i * 2 + 1] = gst_aes_nibble_to_hex (low);
  }
  out[len * 2] = 0;             /* null terminate */

  return out;
}

/*
 * gst_aes_hexstring2bytearray
 *
 * convert hex string to array of bytes
 *
 * @param filter calling element
 * @param in input hex string
 * @param allocated byte array for output
 *
 * @return output byte array
 */
guint
gst_aes_hexstring2bytearray (GstElement * filter, const gchar * in,
    guchar * out)
{
  gchar byte_val;
  guint hex_count = 0;

  GST_LOG_OBJECT (filter, "Converting hex string to number");

  g_return_val_if_fail (in && out, 0);

  while (*in != 0) {
    /* Compute fist half-byte */
    if (*in >= 'A' && *in <= 'F') {
      byte_val = (*in - 55) << 4;
    } else if (*in >= 'a' && *in <= 'f') {
      byte_val = (*in - 87) << 4;
    } else if (*in >= '0' && *in <= '9') {
      byte_val = (*in - 48) << 4;
    } else {
      return 0;
    }
    in++;
    if (*in == 0) {
      break;
    }
    /* Compute second half-byte */
    if (*in >= 'A' && *in <= 'F') {
      *out = (*in - 55) + byte_val;
    } else if (*in >= 'a' && *in <= 'f') {
      *out = (*in - 87) + byte_val;
    } else if (*in >= '0' && *in <= '9') {
      *out = (*in - 48) + byte_val;
    } else {
      return 0;
    }

    GST_LOG_OBJECT (filter, "ch: %c%c, hex: 0x%x", *(in - 1), *in, *out);
    in++;
    out++;
    if (!in || !out)
      return 0;
    hex_count++;
  }
  GST_LOG_OBJECT (filter, "Hex string conversion successful");

  return hex_count;
}