]> git.sesse.net Git - vlc/blobdiff - libs/srtp/srtp.c
Remove my old email address
[vlc] / libs / srtp / srtp.c
index e6d09c97325949e082da2d00574bb68b4b9aa40b..f2f2c95731d54dde758a89690330cb35b4f181a0 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Secure RTP with libgcrypt
- * Copyright (C) 2007  Rémi Denis-Courmont <rdenis # simphalempin , com>
+ * Copyright (C) 2007  Rémi Denis-Courmont
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -372,6 +372,53 @@ srtp_setkey (srtp_session_t *s, const void *key, size_t keylen,
     return srtp_derive (s, key, keylen, salt, saltlen) ? EINVAL : 0;
 }
 
+static int hexdigit (char c)
+{
+    if ((c >= '0') && (c <= '9'))
+        return c - '0';
+    if ((c >= 'A') && (c <= 'F'))
+        return c - 'A' + 0xA;
+    if ((c >= 'a') && (c <= 'f'))
+        return c - 'a' + 0xa;
+    return -1;
+}
+
+static ssize_t hexstring (const char *in, uint8_t *out, size_t outlen)
+{
+    size_t inlen = strlen (in);
+
+    if ((inlen > (2 * outlen)) || (inlen & 1))
+        return -1;
+
+    for (size_t i = 0; i < inlen; i += 2)
+    {
+        int a = hexdigit (in[i]), b = hexdigit (in[i + 1]);
+        if ((a == -1) || (b == -1))
+            return -1;
+        out[i / 2] = (a << 4) | b;
+    }
+    return inlen / 2;
+}
+
+/**
+ * Sets (or resets) the master key and master salt for a SRTP session
+ * from hexadecimal strings. See also srtp_setkey().
+ *
+ * @return 0 on success, in case of error:
+ *  EINVAL  invalid or unsupported key/salt sizes combination
+ */
+int
+srtp_setkeystring (srtp_session_t *s, const char *key, const char *salt)
+{
+    uint8_t bkey[16]; /* TODO/NOTE: hard-coded for AES */
+    uint8_t bsalt[14]; /* TODO/NOTE: hard-coded for the PRF-AES-CM */
+    ssize_t bkeylen = hexstring (key, bkey, sizeof (bkey));
+    ssize_t bsaltlen = hexstring (salt, bsalt, sizeof (bsalt));
+
+    if ((bkeylen == -1) || (bsaltlen == -1))
+        return EINVAL;
+    return srtp_setkey (s, bkey, bkeylen, bsalt, bsaltlen) ? EINVAL : 0;
+}
 
 /**
  * Sets Roll-over-Counter Carry (RCC) rate for the SRTP session. If not
@@ -553,17 +600,21 @@ int
 srtp_send (srtp_session_t *s, uint8_t *buf, size_t *lenp, size_t bufsize)
 {
     size_t len = *lenp;
-    int val = srtp_crypt (s, buf, len);
-    if (val)
-        return val;
+    size_t tag_len = s->tag_len;
 
     if (!(s->flags & SRTP_UNAUTHENTICATED))
     {
-        size_t tag_len = s->tag_len;
         *lenp = len + tag_len;
         if (bufsize < (len + tag_len))
             return ENOSPC;
+    }
+
+    int val = srtp_crypt (s, buf, len);
+    if (val)
+        return val;
 
+    if (!(s->flags & SRTP_UNAUTHENTICATED))
+    {
         uint32_t roc = srtp_compute_roc (s, rtp_seq (buf));
         const uint8_t *tag = rtp_digest (s, buf, len, roc);
         if (rcc_mode (s))