int mac_init(struct sshmac *mac) { if (mac->key == NULL) return SSH_ERR_INVALID_ARGUMENT; switch (mac->type) { case SSH_EVP: if (mac->evp_md == NULL) return SSH_ERR_INVALID_ARGUMENT; HMAC_CTX_init(&mac->evp_ctx); if (HMAC_Init(&mac->evp_ctx, mac->key, mac->key_len, mac->evp_md) != 1) { HMAC_CTX_cleanup(&mac->evp_ctx); return SSH_ERR_LIBCRYPTO_ERROR; } return 0; case SSH_UMAC: if ((mac->umac_ctx = umac_new(mac->key)) == NULL) return SSH_ERR_ALLOC_FAIL; return 0; case SSH_UMAC128: mac->umac_ctx = umac128_new(mac->key); return 0; default: return SSH_ERR_INVALID_ARGUMENT; } }
int mac_init(Mac *mac) { if (mac->key == NULL) fatal("mac_init: no key"); switch (mac->type) { case SSH_EVP: if (mac->evp_md == NULL) return -1; HMAC_Init(&mac->evp_ctx, mac->key, mac->key_len, mac->evp_md); return 0; case SSH_UMAC: mac->umac_ctx = umac_new(mac->key); return 0; default: return -1; } }
int mac_init(Mac *mac) { if (mac->key == NULL) fatal("%s: no key", __func__); switch (mac->type) { case SSH_DIGEST: if (mac->hmac_ctx == NULL || ssh_hmac_init(mac->hmac_ctx, mac->key, mac->key_len) < 0) return -1; return 0; case SSH_UMAC: mac->umac_ctx = umac_new(mac->key); return 0; case SSH_UMAC128: mac->umac_ctx = umac128_new(mac->key); return 0; default: return -1; } }
fs_rid umac_wrapper(const char *str, fs_rid nonce_in) { if (!str) return 0; long long __attribute__((aligned(16))) data; long long __attribute__((aligned(16))) nonce = nonce_in; if (!umac_data) { umac_data = umac_new("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); } const int slen = strlen(str); char *buffer = NULL; void *heap_buffer = NULL; char __attribute__((aligned(16))) stack_buffer[1024]; if (slen < 1024) { buffer = stack_buffer; } else { #ifdef __APPLE__ heap_buffer = malloc(slen+1); buffer = heap_buffer; #else int tmp = (slen + 1) & 31; void **ptr = &heap_buffer; if (posix_memalign(ptr, 16, slen + 33 - tmp)) { fs_error(LOG_ERR, "posix_memalign: %s", strerror(errno)); /* XXX free memory here? return NULL? */ } buffer = heap_buffer; #endif } strncpy(buffer, str, slen+1); umac(umac_data, buffer, slen, (char *)&data, (char *)&nonce); if (heap_buffer) { free(heap_buffer); } return data; }
int mac_init(Mac *mac) { if (mac->key == NULL) fatal("mac_init: no key"); switch (mac->type) { case SSH_EVP: if (mac->evp_md == NULL) return -1; #ifdef HAVE_HMAC_CTX_INIT HMAC_CTX_init(&mac->evp_ctx); HMAC_Init_ex(&mac->evp_ctx, mac->key, mac->key_len, mac->evp_md, NULL); #else HMAC_Init(&mac->evp_ctx, mac->key, mac->key_len, mac->evp_md); #endif return 0; case SSH_UMAC: mac->umac_ctx = umac_new(mac->key); return 0; default: return -1; } }
int mac_init(Mac *mac) { if (mac->key == NULL) fatal("mac_init: no key"); switch (mac->type) { case SSH_EVP: if (mac->evp_md == NULL) return -1; HMAC_CTX_init(&mac->evp_ctx); HMAC_Init(&mac->evp_ctx, mac->key, mac->key_len, mac->evp_md); return 0; #ifdef UMAC_HAS_BEEN_UNBROKEN case SSH_UMAC: mac->umac_ctx = umac_new(mac->key); return 0; case SSH_UMAC128: mac->umac_ctx = umac128_new(mac->key); return 0; #endif default: return -1; } }