security/nss/lib/freebl/seed.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial