* esp_init() is called when an SPI is being set up. */
| 140 | * esp_init() is called when an SPI is being set up. |
| 141 | */ |
| 142 | static int |
| 143 | esp_init(struct secasvar *sav, struct xformsw *xsp) |
| 144 | { |
| 145 | const struct enc_xform *txform; |
| 146 | struct crypto_session_params csp; |
| 147 | int keylen; |
| 148 | int error; |
| 149 | |
| 150 | txform = enc_algorithm_lookup(sav->alg_enc); |
| 151 | if (txform == NULL) { |
| 152 | DPRINTF(("%s: unsupported encryption algorithm %d\n", |
| 153 | __func__, sav->alg_enc)); |
| 154 | return EINVAL; |
| 155 | } |
| 156 | if (sav->key_enc == NULL) { |
| 157 | DPRINTF(("%s: no encoding key for %s algorithm\n", |
| 158 | __func__, txform->name)); |
| 159 | return EINVAL; |
| 160 | } |
| 161 | if ((sav->flags & (SADB_X_EXT_OLD | SADB_X_EXT_IV4B)) == |
| 162 | SADB_X_EXT_IV4B) { |
| 163 | DPRINTF(("%s: 4-byte IV not supported with protocol\n", |
| 164 | __func__)); |
| 165 | return EINVAL; |
| 166 | } |
| 167 | |
| 168 | /* subtract off the salt, RFC4106, 8.1 and RFC3686, 5.1 */ |
| 169 | keylen = _KEYLEN(sav->key_enc) - SAV_ISCTRORGCM(sav) * 4; |
| 170 | if (txform->minkey > keylen || keylen > txform->maxkey) { |
| 171 | DPRINTF(("%s: invalid key length %u, must be in the range " |
| 172 | "[%u..%u] for algorithm %s\n", __func__, |
| 173 | keylen, txform->minkey, txform->maxkey, |
| 174 | txform->name)); |
| 175 | return EINVAL; |
| 176 | } |
| 177 | |
| 178 | if (SAV_ISCTRORGCM(sav)) |
| 179 | sav->ivlen = 8; /* RFC4106 3.1 and RFC3686 3.1 */ |
| 180 | else |
| 181 | sav->ivlen = txform->ivsize; |
| 182 | |
| 183 | memset(&csp, 0, sizeof(csp)); |
| 184 | |
| 185 | /* |
| 186 | * Setup AH-related state. |
| 187 | */ |
| 188 | if (sav->alg_auth != 0) { |
| 189 | error = ah_init0(sav, xsp, &csp); |
| 190 | if (error) |
| 191 | return error; |
| 192 | } |
| 193 | |
| 194 | /* NB: override anything set in ah_init0 */ |
| 195 | sav->tdb_xform = xsp; |
| 196 | sav->tdb_encalgxform = txform; |
| 197 | |
| 198 | /* |
| 199 | * Whenever AES-GCM is used for encryption, one |
nothing calls this directly
no test coverage detected