Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/agc-netpgp-standalone]: src/crypto/external/bsd/netpgp/dist/src/libciphe...



details:   https://anonhg.NetBSD.org/src/rev/7fb1a780abdc
branches:  agc-netpgp-standalone
changeset: 777798:7fb1a780abdc
user:      agc <agc%NetBSD.org@localhost>
date:      Sun May 06 17:40:07 2012 +0000

description:
Add an API-alike equivalent for openssl cipher functionality.

The ciphers implemented are CAST5 and AES. Camellia code is present,
but has not been fully implemented yet.

The rijndael code is from the NetBSD kernel sources, the CAST source
is based on the libtomcrypt implementation (although the internal
methods of storing intermediate values has been modified to mimic
the openssl code, just in case someone is relying on internal state
for this - probably overkill, but whatever).

diffstat:

 crypto/external/bsd/netpgp/dist/src/libcipher/Makefile           |    14 +
 crypto/external/bsd/netpgp/dist/src/libcipher/aes.h              |    49 +
 crypto/external/bsd/netpgp/dist/src/libcipher/camellia.c         |   301 ++
 crypto/external/bsd/netpgp/dist/src/libcipher/camellia.h         |    26 +
 crypto/external/bsd/netpgp/dist/src/libcipher/cast.c             |   752 ++++++
 crypto/external/bsd/netpgp/dist/src/libcipher/cast.h             |    68 +
 crypto/external/bsd/netpgp/dist/src/libcipher/libnetpgpcipher.3  |   113 +
 crypto/external/bsd/netpgp/dist/src/libcipher/modes.c            |   309 ++
 crypto/external/bsd/netpgp/dist/src/libcipher/modes.h            |    64 +
 crypto/external/bsd/netpgp/dist/src/libcipher/rijndael-alg-fst.c |  1230 ++++++++++
 crypto/external/bsd/netpgp/dist/src/libcipher/rijndael-alg-fst.h |    54 +
 crypto/external/bsd/netpgp/dist/src/libcipher/rijndael-api-fst.c |   395 +++
 crypto/external/bsd/netpgp/dist/src/libcipher/rijndael-api-fst.h |   119 +
 crypto/external/bsd/netpgp/dist/src/libcipher/rijndael.c         |   128 +
 crypto/external/bsd/netpgp/dist/src/libcipher/rijndael.h         |    76 +
 crypto/external/bsd/netpgp/dist/src/libcipher/rijndael_local.h   |     7 +
 16 files changed, 3705 insertions(+), 0 deletions(-)

diffs (truncated from 3769 to 300 lines):

diff -r 6dbb691bc790 -r 7fb1a780abdc crypto/external/bsd/netpgp/dist/src/libcipher/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/libcipher/Makefile    Sun May 06 17:40:07 2012 +0000
@@ -0,0 +1,14 @@
+LIB=netcipher
+SRCS= rijndael-alg-fst.c rijndael-api-fst.c rijndael.c
+SRCS+= modes.c
+SRCS+= c_skey.c c_ecb.c c_enc.c c_cfb64.c c_ofb64.c # XXX
+
+#SRCS+=cast5.c
+
+MKMAN=no # XXX
+WARNS=4
+
+INCS=rijndael.h cast5.h
+INCSDIR=/usr/include/netpgp
+
+.include <bsd.lib.mk>
diff -r 6dbb691bc790 -r 7fb1a780abdc crypto/external/bsd/netpgp/dist/src/libcipher/aes.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/libcipher/aes.h       Sun May 06 17:40:07 2012 +0000
@@ -0,0 +1,49 @@
+/*-
+ * Copyright (c) 2012 Alistair Crooks <agc%NetBSD.org@localhost>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef AES_H_
+#define AES_H_ 20120412
+
+#include "rijndael.h"
+
+#ifndef __BEGIN_DECLS
+#  if defined(__cplusplus)
+#  define __BEGIN_DECLS           extern "C" {
+#  define __END_DECLS             }
+#  else
+#  define __BEGIN_DECLS
+#  define __END_DECLS
+#  endif
+#endif
+
+__BEGIN_DECLS
+
+#define AES_DECRYPT    0
+#define AES_ENCRYPT    1
+
+#define AES_BLOCK_SIZE 16
+
+__END_DECLS
+
+#endif
diff -r 6dbb691bc790 -r 7fb1a780abdc crypto/external/bsd/netpgp/dist/src/libcipher/camellia.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/libcipher/camellia.c  Sun May 06 17:40:07 2012 +0000
@@ -0,0 +1,301 @@
+/**************************************************
+ *                                               *
+ *     Camellia Block Encryption Algorithm       *
+ *       in ANSI-C Language : Camellia.c         *
+ *                                               *
+ *       Version M1.02 September 24 2001         *
+ *  Copyright Mitsubishi Electric Corp 2000-2001  *
+ *                                               *
+ **************************************************/
+
+#include "camellia.h"
+
+const uint8_t SIGMA[48] = {
+       0xa0,0x9e,0x66,0x7f,0x3b,0xcc,0x90,0x8b,
+       0xb6,0x7a,0xe8,0x58,0x4c,0xaa,0x73,0xb2,
+       0xc6,0xef,0x37,0x2f,0xe9,0x4f,0x82,0xbe,
+       0x54,0xff,0x53,0xa5,0xf1,0xd3,0x6f,0x1c,
+       0x10,0xe5,0x27,0xfa,0xde,0x68,0x2d,0x1d,
+       0xb0,0x56,0x88,0xc2,0xb3,0xe6,0xc1,0xfd
+};
+
+const unsigned KSFT1[26] = {
+       0,64,0,64,15,79,15,79,30,94,45,109,45,124,60,124,77,13,
+       94,30,94,30,111,47,111,47
+};
+const unsigned KIDX1[26] = {
+       0,0,4,4,0,0,4,4,4,4,0,0,4,0,4,4,0,0,0,0,4,4,0,0,4,4
+};
+const unsigned KSFT2[34] = {
+       0,64,0,64,15,79,15,79,30,94,30,94,45,109,45,109,60,124,
+       60,124,60,124,77,13,77,13,94,30,94,30,111,47,111,47
+};
+const unsigned KIDX2[34] = {
+       0,0,12,12,8,8,4,4,8,8,12,12,0,0,4,4,0,0,8,8,12,12,
+       0,0,4,4,8,8,4,4,0,0,12,12
+};
+
+const uint8_t SBOX[256] = {
+       112,130, 44,236,179, 39,192,229,228,133, 87, 53,234, 12,174, 65,
+        35,239,107,147, 69, 25,165, 33,237, 14, 79, 78, 29,101,146,189,
+       134,184,175,143,124,235, 31,206, 62, 48,220, 95, 94,197, 11, 26,
+       166,225, 57,202,213, 71, 93, 61,217,  1, 90,214, 81, 86,108, 77,
+       139, 13,154,102,251,204,176, 45,116, 18, 43, 32,240,177,132,153,
+       223, 76,203,194, 52,126,118,  5,109,183,169, 49,209, 23,  4,215,
+        20, 88, 58, 97,222, 27, 17, 28, 50, 15,156, 22, 83, 24,242, 34,
+       254, 68,207,178,195,181,122,145, 36,  8,232,168, 96,252,105, 80,
+       170,208,160,125,161,137, 98,151, 84, 91, 30,149,224,255,100,210,
+        16,196,  0, 72,163,247,117,219,138,  3,230,218,  9, 63,221,148,
+       135, 92,131,  2,205, 74,144, 51,115,103,246,243,157,127,191,226,
+        82,155,216, 38,200, 55,198, 59,129,150,111, 75, 19,190, 99, 46,
+       233,121,167,140,159,110,188,142, 41,245,249,182, 47,253,180, 89,
+       120,152,  6,106,231, 70,113,186,212, 37,171, 66,136,162,141,250,
+       114,  7,185, 85,248,238,172, 10, 54, 73, 42,104, 60, 56,241,164,
+        64, 40,211,123,187,201, 67,193, 21,227,173,244,119,199,128,158
+};
+
+#define SBOX1(n) SBOX[(n)]
+#define SBOX2(n) (uint8_t)((SBOX[(n)] >> 7 ^ SBOX[(n)] << 1) & 0xff)
+#define SBOX3(n) (uint8_t)((SBOX[(n)] >> 1 ^ SBOX[(n)] << 7) & 0xff)
+#define SBOX4(n) SBOX[((n) << 1 ^ (n) >> 7) & 0xff]
+
+static void
+ByteWord(const uint8_t *x, uint32_t *y)
+{
+       int i;
+
+       for (i = 0; i < 4; i++){
+               y[i] = ((uint32_t)x[(i<<2)+0]<<24) + ((uint32_t)x[(i<<2)+1]<<16)
+                    + ((uint32_t)x[(i<<2)+2]<<8 ) + ((uint32_t)x[(i<<2)+3]<<0 );
+       }
+}
+
+static void
+WordByte(const uint32_t *x, uint8_t *y)
+{
+       int i;
+
+       for (i = 0; i < 4; i++){
+               y[(i << 2) + 0] = (uint8_t)(x[i] >> 24 & 0xff);
+               y[(i << 2) + 1] = (uint8_t)(x[i] >> 16 & 0xff);
+               y[(i << 2) + 2] = (uint8_t)(x[i] >>  8 & 0xff);
+               y[(i << 2) + 3] = (uint8_t)(x[i] >>  0 & 0xff);
+       }
+}
+
+static void
+RotBlock(const uint32_t *x, const unsigned n, uint32_t *y)
+{
+       unsigned r;
+
+       if ((r = (n & 31)) != 0) {
+               y[0] = x[((n >> 5) + 0) & 3] << r ^ x[((n >> 5) + 1) & 3] >> (32 - r);
+               y[1] = x[((n >> 5) + 1) & 3] << r ^ x[((n >> 5) + 2) & 3] >> (32 - r);
+       } else{
+               y[0] = x[((n >> 5) + 0) & 3];
+               y[1] = x[((n >> 5) + 1) & 3];
+       }
+}
+
+static void
+SwapHalf(uint8_t *x)
+{
+       uint8_t t;
+       int     i;
+
+       for (i = 0; i < 8; i++){
+               t = x[i];
+               x[i] = x[8 + i];
+               x[8 + i] = t;
+       }
+}
+
+static void
+XorBlock(const uint8_t *x, const uint8_t *y, uint8_t *z)
+{
+       int i;
+
+       for (i=0; i<16; i++) {
+               z[i] = x[i] ^ y[i];
+       }
+}
+
+/*********************************************************************/
+
+void
+Camellia_Ekeygen(const int n, const uint8_t *k, uint8_t *e)
+{
+       uint32_t        u[20];
+       uint8_t         t[64];
+       int             i;
+
+       if (n == 128) {
+               for (i = 0 ; i < 16; i++) {
+                       t[i] = k[i];
+               }
+               for (i = 16; i < 32; i++) {
+                       t[i] = 0;
+               }
+       } else if (n == 192) {
+               for (i = 0 ; i < 24; i++) {
+                       t[i] = k[i];
+               }
+               for (i = 24; i < 32; i++) {
+                       t[i] = k[i - 8] ^ 0xff;
+               }
+       } else if (n == 256) {
+               for (i = 0 ; i < 32; i++) {
+                       t[i] = k[i];
+               }
+       }
+
+       XorBlock(t+0, t+16, t+32);
+
+       Camellia_Feistel(t+32, SIGMA+0, t+40);
+       Camellia_Feistel(t+40, SIGMA+8, t+32);
+
+       XorBlock(t+32, t+0, t+32);
+
+       Camellia_Feistel(t+32, SIGMA+16, t+40);
+       Camellia_Feistel(t+40, SIGMA+24, t+32);
+
+       ByteWord(t+0,  u+0);
+       ByteWord(t+32, u+4);
+
+       if (n == 128) {
+               for (i=0; i<26; i+=2) {
+                       RotBlock(u+KIDX1[i+0], KSFT1[i+0], u+16);
+                       RotBlock(u+KIDX1[i+1], KSFT1[i+1], u+18);
+                       WordByte(u+16, e+i*8);
+               }
+       } else {
+               XorBlock(t+32, t+16, t+48);
+
+               Camellia_Feistel(t+48, SIGMA+32, t+56);
+               Camellia_Feistel(t+56, SIGMA+40, t+48);
+
+               ByteWord(t+16, u+8);
+               ByteWord(t+48, u+12);
+
+               for (i=0; i<34; i+=2) {
+                       RotBlock(u+KIDX2[i+0], KSFT2[i+0], u+16);
+                       RotBlock(u+KIDX2[i+1], KSFT2[i+1], u+18);
+                       WordByte(u+16, e+(i<<3));
+               }
+       }
+}
+
+void
+Camellia_Encrypt(const int n, const uint8_t *p, const uint8_t *e, uint8_t *c)
+{
+       int i;
+
+       XorBlock( p, e+0, c );
+       for (i=0; i<3; i++) {
+               Camellia_Feistel( c+0, e+16+(i<<4), c+8 );
+               Camellia_Feistel( c+8, e+24+(i<<4), c+0 );
+       }
+       Camellia_FLlayer( c, e+64, e+72 );
+       for (i=0; i<3; i++) {
+               Camellia_Feistel( c+0, e+80+(i<<4), c+8 );
+               Camellia_Feistel( c+8, e+88+(i<<4), c+0 );
+       }
+       Camellia_FLlayer( c, e+128, e+136 );
+       for (i=0; i<3; i++) {
+               Camellia_Feistel( c+0, e+144+(i<<4), c+8 );
+               Camellia_Feistel( c+8, e+152+(i<<4), c+0 );
+       }
+       if (n == 128) {
+               SwapHalf(c);
+               XorBlock(c, e+192, c);
+       } else {
+               Camellia_FLlayer(c, e+192, e+200);
+               for (i=0; i<3; i++) {
+                       Camellia_Feistel( c+0, e+208+(i<<4), c+8);
+                       Camellia_Feistel( c+8, e+216+(i<<4), c+0);
+               }
+               SwapHalf(c);
+               XorBlock(c, e+256, c);
+       }
+}
+
+void
+Camellia_Decrypt(const int n, const uint8_t *c, const uint8_t *e, uint8_t *p)
+{
+       int i;



Home | Main Index | Thread Index | Old Index