diff options
-rw-r--r-- | src/crypto.c | 36 | ||||
-rw-r--r-- | src/crypto.h | 20 |
2 files changed, 56 insertions, 0 deletions
diff --git a/src/crypto.c b/src/crypto.c index 3ed6070..aca22f0 100644 --- a/src/crypto.c +++ b/src/crypto.c @@ -176,6 +176,42 @@ void RC4_FREE(struct ntlm_rc4_handle **handle) safefree(*handle); } +int RC4_EXPORT(struct ntlm_rc4_handle *handle, struct ntlm_buffer *out) +{ + int i; + + if (out->length < 258) return EAGAIN; + + out->data[0] = (uint8_t)(handle->key.x & 0xff); + out->data[1] = (uint8_t)(handle->key.y & 0xff); + for (i = 0; i < 256; i++) { + out->data[i + 2] = (uint8_t)(handle->key.data[i] & 0xff); + } + + out->length = 258; + return 0; +} + +int RC4_IMPORT(struct ntlm_rc4_handle **_handle, struct ntlm_buffer *in) +{ + struct ntlm_rc4_handle *handle; + int i; + + if (in->length != 258) return EINVAL; + + handle = malloc(sizeof(struct ntlm_rc4_handle)); + if (!handle) return ENOMEM; + + handle->key.x = in->data[0]; + handle->key.y = in->data[1]; + for (i = 0; i < 256; i++) { + handle->key.data[i] = in->data[i + 2]; + } + + *_handle = handle; + return 0; +} + int RC4K(struct ntlm_buffer *key, enum ntlm_cipher_mode mode, struct ntlm_buffer *payload, diff --git a/src/crypto.h b/src/crypto.h index 87ff830..e40852e 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -113,6 +113,26 @@ int RC4_UPDATE(struct ntlm_rc4_handle *handle, void RC4_FREE(struct ntlm_rc4_handle **handle); /** + * @brief Exports the RC4 state + * + * @param handle The RC4 handle to export from + * @param out A buffer at least 258 bytes long + * + * @return 0 on success or EAGAIN if the buffer is too small + */ +int RC4_EXPORT(struct ntlm_rc4_handle *handle, struct ntlm_buffer *out); + +/** + * @brief Import an RC4 state + * + * @param handle A new ntlm_rc4_handle on success + * @param in A buffer containing an exported state + * + * @return 0 on success or EINVAL if the buffer is not an exported state + */ +int RC4_IMPORT(struct ntlm_rc4_handle **handle, struct ntlm_buffer *in); + +/** * @brief RC4 encryption/decryption all in one * * @param key The encryption/decryption key |