seed.h (4029B)
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/. */ 4 5 #ifndef HEADER_SEED_H 6 #define HEADER_SEED_H 7 8 #include <string.h> 9 #include "blapi.h" 10 11 #if !defined(NO_SYS_TYPES_H) 12 #include <sys/types.h> 13 #endif 14 15 typedef PRUint32 seed_word; 16 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)] 22 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]))) 28 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)) 34 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)) 41 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)) 48 49 #define KEYUPDATE_TEMP(T0, T1, K) \ 50 (K)[0] = G_FUNC((T0)); \ 51 (K)[1] = G_FUNC((T1)) 52 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] 58 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] 64 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]) 70 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)) 76 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) 89 90 #ifdef __cplusplus 91 extern "C" { 92 #endif 93 94 typedef struct seed_key_st { 95 PRUint32 data[32]; 96 } SEED_KEY_SCHEDULE; 97 98 struct SEEDContextStr { 99 unsigned char iv[SEED_BLOCK_SIZE]; 100 SEED_KEY_SCHEDULE ks; 101 int mode; 102 unsigned int encrypt; 103 }; 104 105 void SEED_set_key(const unsigned char rawkey[SEED_KEY_LENGTH], 106 SEED_KEY_SCHEDULE *ks); 107 108 void SEED_encrypt(const unsigned char s[SEED_BLOCK_SIZE], 109 unsigned char d[SEED_BLOCK_SIZE], 110 const SEED_KEY_SCHEDULE *ks); 111 void SEED_decrypt(const unsigned char s[SEED_BLOCK_SIZE], 112 unsigned char d[SEED_BLOCK_SIZE], 113 const SEED_KEY_SCHEDULE *ks); 114 115 void SEED_ecb_encrypt(const unsigned char *in, unsigned char *out, 116 size_t inLen, const SEED_KEY_SCHEDULE *ks, int enc); 117 void SEED_cbc_encrypt(const unsigned char *in, unsigned char *out, 118 size_t len, const SEED_KEY_SCHEDULE *ks, 119 unsigned char ivec[SEED_BLOCK_SIZE], int enc); 120 121 #ifdef __cplusplus 122 } 123 #endif 124 125 #endif /* HEADER_SEED_H */