security/nss/lib/freebl/seed.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #ifndef HEADER_SEED_H
     6 #define HEADER_SEED_H
     8 #include <string.h>
     9 #include "blapi.h"
    11 #if !defined(NO_SYS_TYPES_H)
    12 # include <sys/types.h>
    13 #endif
    15 typedef PRUint32 seed_word;
    17 #define G_FUNC(v) \
    18     SS[0][((v)     & 0xff)] ^ \
    19     SS[1][((v)>> 8 & 0xff)] ^ \
    20     SS[2][((v)>>16 & 0xff)] ^ \
    21     SS[3][((v)>>24 & 0xff)]
    23 #define char2word(c, i)  \
    24     (i) = ((((seed_word)((c)[0])) << 24) | \
    25            (((seed_word)((c)[1])) << 16) | \
    26            (((seed_word)((c)[2])) <<  8) | \
    27             ((seed_word)((c)[3])))
    29 #define word2char(l, c)  \
    30     *((c)+0) = (unsigned char)((l)>>24); \
    31     *((c)+1) = (unsigned char)((l)>>16); \
    32     *((c)+2) = (unsigned char)((l)>> 8); \
    33     *((c)+3) = (unsigned char)((l)    )
    35 #define KEYSCHEDULE_UPDATE0(T0, T1, K0, K1, K2, K3, KC)  \
    36     (T0) = (K2);                                          \
    37     (K2) = (((K2)<<8) ^ ((K3)>>24));                     \
    38     (K3) = (((K3)<<8) ^ ((T0)>>24));                     \
    39     (T0) = ((K0) + (K2) - (KC));                         \
    40     (T1) = ((K1) + (KC) - (K3))
    42 #define KEYSCHEDULE_UPDATE1(T0, T1, K0, K1, K2, K3, KC) \
    43     (T0) = (K0);                                         \
    44     (K0) = (((K0)>>8) ^ ((K1)<<24));                    \
    45     (K1) = (((K1)>>8) ^ ((T0)<<24));                    \
    46     (T0) = ((K0) + (K2) - (KC));                         \
    47     (T1) = ((K1) + (KC) - (K3))
    49 #define KEYUPDATE_TEMP(T0, T1, K)   \
    50     (K)[0] = G_FUNC((T0));          \
    51     (K)[1] = G_FUNC((T1))
    53 #define XOR_SEEDBLOCK(DST, SRC)  \
    54     (DST)[0] ^= (SRC)[0];    \
    55     (DST)[1] ^= (SRC)[1];    \
    56     (DST)[2] ^= (SRC)[2];    \
    57     (DST)[3] ^= (SRC)[3]
    59 #define MOV_SEEDBLOCK(DST, SRC)  \
    60     (DST)[0] = (SRC)[0];     \
    61     (DST)[1] = (SRC)[1];     \
    62     (DST)[2] = (SRC)[2];     \
    63     (DST)[3] = (SRC)[3]
    65 # define CHAR2WORD(C, I)          \
    66     char2word((C),    (I)[0]);    \
    67     char2word((C)+4,  (I)[1]);    \
    68     char2word((C)+8,  (I)[2]);    \
    69     char2word((C)+12, (I)[3])
    71 # define WORD2CHAR(I, C)          \
    72     word2char((I)[0], (C));       \
    73     word2char((I)[1], (C+4));     \
    74     word2char((I)[2], (C+8));     \
    75     word2char((I)[3], (C+12))
    77 # define E_SEED(T0, T1, X1, X2, X3, X4, rbase)  \
    78     (T0)  = (X3) ^ (ks->data)[(rbase)];         \
    79     (T1)  = (X4) ^ (ks->data)[(rbase)+1];       \
    80     (T1) ^= (T0);       \
    81     (T1)  = G_FUNC(T1); \
    82     (T0) += (T1);       \
    83     (T0)  = G_FUNC(T0); \
    84     (T1) += (T0);       \
    85     (T1)  = G_FUNC(T1); \
    86     (T0) += (T1);       \
    87     (X1) ^= (T0);       \
    88     (X2) ^= (T1)
    91 #ifdef  __cplusplus
    92 extern "C" {
    93 #endif
    95 typedef struct seed_key_st {
    96     PRUint32 data[32];
    97 } SEED_KEY_SCHEDULE;
   101 struct SEEDContextStr {
   102     unsigned char iv[SEED_BLOCK_SIZE];
   103     SEED_KEY_SCHEDULE ks;
   104     int mode;
   105     unsigned int encrypt;
   106 };
   108 void SEED_set_key(const unsigned char rawkey[SEED_KEY_LENGTH], 
   109                   SEED_KEY_SCHEDULE *ks);
   111 void SEED_encrypt(const unsigned char s[SEED_BLOCK_SIZE], 
   112                   unsigned char d[SEED_BLOCK_SIZE], 
   113                   const SEED_KEY_SCHEDULE *ks);
   114 void SEED_decrypt(const unsigned char s[SEED_BLOCK_SIZE], 
   115                   unsigned char d[SEED_BLOCK_SIZE], 
   116                   const SEED_KEY_SCHEDULE *ks);
   118 void SEED_ecb_encrypt(const unsigned char *in, unsigned char *out, 
   119                       const SEED_KEY_SCHEDULE *ks, int enc);
   120 void SEED_cbc_encrypt(const unsigned char *in, unsigned char *out, 
   121                       size_t len, const SEED_KEY_SCHEDULE *ks, 
   122                       unsigned char ivec[SEED_BLOCK_SIZE], int enc);
   124 #ifdef  __cplusplus
   125 }
   126 #endif
   128 #endif /* HEADER_SEED_H */

mercurial