1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci/* 11e1051a39Sopenharmony_ci * BF low level APIs are deprecated for public use, but still ok for internal 12e1051a39Sopenharmony_ci * use. 13e1051a39Sopenharmony_ci */ 14e1051a39Sopenharmony_ci#include "internal/deprecated.h" 15e1051a39Sopenharmony_ci 16e1051a39Sopenharmony_ci#include <stdio.h> 17e1051a39Sopenharmony_ci#include <string.h> 18e1051a39Sopenharmony_ci#include <openssl/blowfish.h> 19e1051a39Sopenharmony_ci#include "bf_local.h" 20e1051a39Sopenharmony_ci#include "bf_pi.h" 21e1051a39Sopenharmony_ci 22e1051a39Sopenharmony_civoid BF_set_key(BF_KEY *key, int len, const unsigned char *data) 23e1051a39Sopenharmony_ci{ 24e1051a39Sopenharmony_ci int i; 25e1051a39Sopenharmony_ci BF_LONG *p, ri, in[2]; 26e1051a39Sopenharmony_ci const unsigned char *d, *end; 27e1051a39Sopenharmony_ci 28e1051a39Sopenharmony_ci memcpy(key, &bf_init, sizeof(BF_KEY)); 29e1051a39Sopenharmony_ci p = key->P; 30e1051a39Sopenharmony_ci 31e1051a39Sopenharmony_ci if (len > ((BF_ROUNDS + 2) * 4)) 32e1051a39Sopenharmony_ci len = (BF_ROUNDS + 2) * 4; 33e1051a39Sopenharmony_ci 34e1051a39Sopenharmony_ci d = data; 35e1051a39Sopenharmony_ci end = &(data[len]); 36e1051a39Sopenharmony_ci for (i = 0; i < (BF_ROUNDS + 2); i++) { 37e1051a39Sopenharmony_ci ri = *(d++); 38e1051a39Sopenharmony_ci if (d >= end) 39e1051a39Sopenharmony_ci d = data; 40e1051a39Sopenharmony_ci 41e1051a39Sopenharmony_ci ri <<= 8; 42e1051a39Sopenharmony_ci ri |= *(d++); 43e1051a39Sopenharmony_ci if (d >= end) 44e1051a39Sopenharmony_ci d = data; 45e1051a39Sopenharmony_ci 46e1051a39Sopenharmony_ci ri <<= 8; 47e1051a39Sopenharmony_ci ri |= *(d++); 48e1051a39Sopenharmony_ci if (d >= end) 49e1051a39Sopenharmony_ci d = data; 50e1051a39Sopenharmony_ci 51e1051a39Sopenharmony_ci ri <<= 8; 52e1051a39Sopenharmony_ci ri |= *(d++); 53e1051a39Sopenharmony_ci if (d >= end) 54e1051a39Sopenharmony_ci d = data; 55e1051a39Sopenharmony_ci 56e1051a39Sopenharmony_ci p[i] ^= ri; 57e1051a39Sopenharmony_ci } 58e1051a39Sopenharmony_ci 59e1051a39Sopenharmony_ci in[0] = 0L; 60e1051a39Sopenharmony_ci in[1] = 0L; 61e1051a39Sopenharmony_ci for (i = 0; i < (BF_ROUNDS + 2); i += 2) { 62e1051a39Sopenharmony_ci BF_encrypt(in, key); 63e1051a39Sopenharmony_ci p[i] = in[0]; 64e1051a39Sopenharmony_ci p[i + 1] = in[1]; 65e1051a39Sopenharmony_ci } 66e1051a39Sopenharmony_ci 67e1051a39Sopenharmony_ci p = key->S; 68e1051a39Sopenharmony_ci for (i = 0; i < 4 * 256; i += 2) { 69e1051a39Sopenharmony_ci BF_encrypt(in, key); 70e1051a39Sopenharmony_ci p[i] = in[0]; 71e1051a39Sopenharmony_ci p[i + 1] = in[1]; 72e1051a39Sopenharmony_ci } 73e1051a39Sopenharmony_ci} 74