1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
| static void compute_keymats(struct state *st) { if (st->st_ah.present) compute_proto_keymat(st, PROTO_IPSEC_AH, &st->st_ah, "AH"); if (st->st_esp.present) compute_proto_keymat(st, PROTO_IPSEC_ESP, &st->st_esp, "ESP"); }
static void compute_proto_keymat(struct state *st , u_int8_t protoid , struct ipsec_proto_info *pi , const char *satypename) { size_t needed_len = 0;
switch (protoid) { case PROTO_IPSEC_ESP: switch (pi->attrs.transattrs.encrypt) { case ESP_NULL: needed_len = 0; break; case ESP_DES: needed_len = DES_CBC_BLOCK_SIZE; break; case ESP_3DES: needed_len = DES_CBC_BLOCK_SIZE * 3; break; case ESP_AES: needed_len = AES_CBC_BLOCK_SIZE; if(st->st_esp.attrs.transattrs.enckeylen) { needed_len = st->st_esp.attrs.transattrs.enckeylen/8; } break;
default: #ifdef KERNEL_ALG if((needed_len=kernel_alg_esp_enc_keylen(pi->attrs.transattrs.encrypt))>0) { if (pi->attrs.transattrs.enckeylen) { needed_len=pi->attrs.transattrs.enckeylen/8; DBG(DBG_PARSING, DBG_log("compute_proto_keymat:" "key_len=%d from peer", (int)needed_len)); } break; } #endif bad_case(pi->attrs.transattrs.encrypt); } DBG(DBG_PARSING, DBG_log("compute_proto_keymat:" "needed_len (after ESP enc)=%d", (int)needed_len));
switch (pi->attrs.transattrs.integ_hash) { case AUTH_ALGORITHM_NONE: break; case AUTH_ALGORITHM_HMAC_MD5: needed_len += HMAC_MD5_KEY_LEN; break; case AUTH_ALGORITHM_HMAC_SHA1: needed_len += HMAC_SHA1_KEY_LEN; break; default: #ifdef KERNEL_ALG if (kernel_alg_esp_auth_ok(pi->attrs.transattrs.integ_hash, NULL) == NULL) { needed_len += kernel_alg_esp_auth_keylen(pi->attrs.transattrs.integ_hash); break; } #endif case AUTH_ALGORITHM_DES_MAC: bad_case(pi->attrs.transattrs.integ_hash); break;
} DBG(DBG_PARSING, DBG_log("compute_proto_keymat:" "needed_len (after ESP auth)=%d", (int)needed_len)); break;
case PROTO_IPSEC_AH: switch (pi->attrs.transattrs.encrypt) { case AH_MD5: needed_len = HMAC_MD5_KEY_LEN; break; case AH_SHA: needed_len = HMAC_SHA1_KEY_LEN; break; default: #ifdef KERNEL_ALG if (kernel_alg_ah_auth_ok(pi->attrs.transattrs.integ_hash, NULL)) { needed_len += kernel_alg_ah_auth_keylen(pi->attrs.transattrs.integ_hash); break; } #endif bad_case(pi->attrs.transattrs.encrypt); } break;
default: bad_case(protoid); }
pi->keymat_len = needed_len;
{ struct hmac_ctx ctx_me, ctx_peer; size_t needed_space; size_t i;
hmac_init_chunk(&ctx_me, st->st_oakley.prf_hasher, st->st_skeyid_d);
ctx_peer = ctx_me; needed_space = needed_len + pad_up(needed_len, ctx_me.hmac_digest_len); replace(pi->our_keymat, alloc_bytes(needed_space, "keymat in compute_keymat()")); replace(pi->peer_keymat, alloc_bytes(needed_space, "peer_keymat in quick_inI1_outR1()"));
for (i = 0;; ) { if (st->st_shared.ptr != NULL) { hmac_update_chunk(&ctx_me, st->st_shared); hmac_update_chunk(&ctx_peer, st->st_shared); } hmac_update(&ctx_me, &protoid, sizeof(protoid)); hmac_update(&ctx_peer, &protoid, sizeof(protoid)); hmac_update(&ctx_me, (u_char *)&pi->our_spi, sizeof(pi->our_spi)); hmac_update(&ctx_peer, (u_char *)&pi->attrs.spi, sizeof(pi->attrs.spi)); hmac_update_chunk(&ctx_me, st->st_ni); hmac_update_chunk(&ctx_peer, st->st_ni); hmac_update_chunk(&ctx_me, st->st_nr); hmac_update_chunk(&ctx_peer, st->st_nr);
hmac_final(pi->our_keymat + i, &ctx_me); hmac_final(pi->peer_keymat + i, &ctx_peer);
i += ctx_me.hmac_digest_len; if (i >= needed_space) break;
hmac_reinit(&ctx_me); hmac_reinit(&ctx_peer);
hmac_update(&ctx_me, pi->our_keymat + i - ctx_me.hmac_digest_len, ctx_me.hmac_digest_len); hmac_update(&ctx_peer, pi->peer_keymat + i - ctx_peer.hmac_digest_len, ctx_peer.hmac_digest_len); } }
DBG(DBG_CRYPT, DBG_log("%s KEYMAT\n",satypename); DBG_dump(" KEYMAT computed:\n", pi->our_keymat, pi->keymat_len); DBG_dump(" Peer KEYMAT computed:\n", pi->peer_keymat, pi->keymat_len)); }
|