]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
New function _nettle_write_le64.
authorNiels Möller <nisse@lysator.liu.se>
Mon, 12 Nov 2012 20:12:28 +0000 (21:12 +0100)
committerNiels Möller <nisse@lysator.liu.se>
Mon, 12 Nov 2012 20:13:25 +0000 (21:13 +0100)
ChangeLog
Makefile.in
macros.h
nettle-write.h
write-le64.c [new file with mode: 0644]

index 605238191ebda8fbdc3523962abac37194478139..c083ab98acd8ab9340f6e5a4a108357ed96d9b8e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2012-11-12  Niels Möller  <nisse@lysator.liu.se>
 
+       * macros.h (LE_WRITE_UINT64): New macro.
+       * write-le64.c (_nettle_write_le64): New file and function.
+       * nettle-write.h (_nettle_write_le64): Declare. Also deleted
+       declaration of non-existent _nettle_write_be64.
+       * Makefile.in (nettle_SOURCES): Added write-le64.c.
+
        * macros.h (ROTL64): New macro, moved from...
        * sha512-compress.c (ROTL64): ... old location, deleted.
 
index 5657b1c30e1221d7537503b105fef58f9c554ed9..f27eb16bbb1e27e88dd25f8be09fe21ec98edc99 100644 (file)
@@ -95,7 +95,7 @@ nettle_SOURCES = aes-decrypt-internal.c aes-decrypt.c \
                 buffer.c buffer-init.c realloc.c \
                 nettle-meta-hashes.c nettle-meta-ciphers.c \
                 nettle-meta-armors.c \
-                write-be32.c write-le32.c
+                write-be32.c write-le32.c write-le64.c
 
 hogweed_SOURCES = sexp.c sexp-format.c \
                  sexp-transport.c sexp-transport-format.c \
index b2d6076f5d53d8f43707383a532fd44f57fe3f4a..dee35f8f0a3b30a7dffb612216112f62148c8d45 100644 (file)
--- a/macros.h
+++ b/macros.h
@@ -87,6 +87,18 @@ do {                                         \
 } while(0)
 
 /* And the other, little-endian, byteorder */
+#define LE_WRITE_UINT64(p, i)                  \
+do {                                           \
+  (p)[7] = ((i) >> 56) & 0xff;                 \
+  (p)[6] = ((i) >> 48) & 0xff;                 \
+  (p)[5] = ((i) >> 40) & 0xff;                 \
+  (p)[4] = ((i) >> 32) & 0xff;                 \
+  (p)[3] = ((i) >> 24) & 0xff;                 \
+  (p)[2] = ((i) >> 16) & 0xff;                 \
+  (p)[1] = ((i) >> 8) & 0xff;                  \
+  (p)[0] = (i) & 0xff;                         \
+} while (0)
+    
 #define LE_READ_UINT32(p)                      \
 (  (((uint32_t) (p)[3]) << 24)                 \
  | (((uint32_t) (p)[2]) << 16)                 \
index 5bea243bb7122b0a224d0f740f40601fd020165e..0213a6d1ff053aaa53c56cd6aa1e92c49d4cf2df 100644 (file)
 /* Write the word array at SRC to the byte array at DST, using little
    endian (le) or big endian (be) byte order, and truncating the
    result to LENGTH bytes. */
+
+/* FIXME: Use a macro shortcut to memcpy for native endianness. */
 void
 _nettle_write_be32(unsigned length, uint8_t *dst,
                   uint32_t *src);
 void
 _nettle_write_le32(unsigned length, uint8_t *dst,
                   uint32_t *src);
+
 void
-_nettle_write_be64(unsigned length, uint8_t *dst,
+_nettle_write_le64(unsigned length, uint8_t *dst,
                   uint64_t *src);
 
 #endif /* NETTLE_WRITE_H_INCLUDED */
diff --git a/write-le64.c b/write-le64.c
new file mode 100644 (file)
index 0000000..fe6592f
--- /dev/null
@@ -0,0 +1,58 @@
+/* write-le64.c */
+
+/* nettle, low-level cryptographics library
+ *
+ * Copyright (C) 2001, 2011, 2012 Niels Möller
+ *  
+ * The nettle library is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or (at your
+ * option) any later version.
+ *
+ * The nettle 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 Lesser General Public
+ * License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with the nettle library; see the file COPYING.LIB.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02111-1301, USA.
+ */
+
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "nettle-write.h"
+
+#include "macros.h"
+
+void
+_nettle_write_le64(unsigned length, uint8_t *dst,
+                  uint64_t *src)
+{
+  unsigned i;
+  unsigned words;
+  unsigned leftover;
+  
+  words = length / 8;
+  leftover = length % 8;
+
+  for (i = 0; i < words; i++, dst += 8)
+    LE_WRITE_UINT64(dst, src[i]);
+
+  if (leftover)
+    {
+      uint64_t word;
+      
+      word = src[i];
+
+      do
+       {
+         *dst++ = word & 0xff;
+         word >>= 8;
+       }
+      while (--leftover);
+    }
+}