wp512.c (96916090f488986a4ebb8e9ffa6a3b50881d5ccd) wp512.c (4946510baac6aaa8658528e3deefc7e9ba2951a9)
1/*
2 * Cryptographic API.
3 *
4 * Whirlpool hashing Algorithm
5 *
6 * The Whirlpool algorithm was developed by Paulo S. L. M. Barreto and
7 * Vincent Rijmen. It has been selected as one of cryptographic
8 * primitives by the NESSIE project http://www.cryptonessie.org/

--- 5 unchanged lines hidden (view full) ---

14 * By Aaron Grothe ajgrothe@yahoo.com, August 23, 2004
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 */
1/*
2 * Cryptographic API.
3 *
4 * Whirlpool hashing Algorithm
5 *
6 * The Whirlpool algorithm was developed by Paulo S. L. M. Barreto and
7 * Vincent Rijmen. It has been selected as one of cryptographic
8 * primitives by the NESSIE project http://www.cryptonessie.org/

--- 5 unchanged lines hidden (view full) ---

14 * By Aaron Grothe ajgrothe@yahoo.com, August 23, 2004
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 */
22#include <crypto/internal/hash.h>
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/mm.h>
25#include <asm/byteorder.h>
23#include <linux/init.h>
24#include <linux/module.h>
25#include <linux/mm.h>
26#include <asm/byteorder.h>
26#include <linux/crypto.h>
27#include <linux/types.h>
28
29#define WP512_DIGEST_SIZE 64
30#define WP384_DIGEST_SIZE 48
31#define WP256_DIGEST_SIZE 32
32
33#define WP512_BLOCK_SIZE 64
34#define WP512_LENGTHBYTES 32

--- 940 unchanged lines hidden (view full) ---

975 wctx->hash[3] ^= state[3] ^ block[3];
976 wctx->hash[4] ^= state[4] ^ block[4];
977 wctx->hash[5] ^= state[5] ^ block[5];
978 wctx->hash[6] ^= state[6] ^ block[6];
979 wctx->hash[7] ^= state[7] ^ block[7];
980
981}
982
27#include <linux/types.h>
28
29#define WP512_DIGEST_SIZE 64
30#define WP384_DIGEST_SIZE 48
31#define WP256_DIGEST_SIZE 32
32
33#define WP512_BLOCK_SIZE 64
34#define WP512_LENGTHBYTES 32

--- 940 unchanged lines hidden (view full) ---

975 wctx->hash[3] ^= state[3] ^ block[3];
976 wctx->hash[4] ^= state[4] ^ block[4];
977 wctx->hash[5] ^= state[5] ^ block[5];
978 wctx->hash[6] ^= state[6] ^ block[6];
979 wctx->hash[7] ^= state[7] ^ block[7];
980
981}
982
983static void wp512_init(struct crypto_tfm *tfm) {
984 struct wp512_ctx *wctx = crypto_tfm_ctx(tfm);
983static int wp512_init(struct shash_desc *desc) {
984 struct wp512_ctx *wctx = shash_desc_ctx(desc);
985 int i;
986
987 memset(wctx->bitLength, 0, 32);
988 wctx->bufferBits = wctx->bufferPos = 0;
989 wctx->buffer[0] = 0;
990 for (i = 0; i < 8; i++) {
991 wctx->hash[i] = 0L;
992 }
985 int i;
986
987 memset(wctx->bitLength, 0, 32);
988 wctx->bufferBits = wctx->bufferPos = 0;
989 wctx->buffer[0] = 0;
990 for (i = 0; i < 8; i++) {
991 wctx->hash[i] = 0L;
992 }
993
994 return 0;
993}
994
995}
996
995static void wp512_update(struct crypto_tfm *tfm, const u8 *source,
997static int wp512_update(struct shash_desc *desc, const u8 *source,
996 unsigned int len)
997{
998 unsigned int len)
999{
998 struct wp512_ctx *wctx = crypto_tfm_ctx(tfm);
1000 struct wp512_ctx *wctx = shash_desc_ctx(desc);
999 int sourcePos = 0;
1000 unsigned int bits_len = len * 8; // convert to number of bits
1001 int sourceGap = (8 - ((int)bits_len & 7)) & 7;
1002 int bufferRem = wctx->bufferBits & 7;
1003 int i;
1004 u32 b, carry;
1005 u8 *buffer = wctx->buffer;
1006 u8 *bitLength = wctx->bitLength;

--- 39 unchanged lines hidden (view full) ---

1046 }
1047 buffer[bufferPos] = b << (8 - bufferRem);
1048 bufferBits += (int)bits_len;
1049 }
1050
1051 wctx->bufferBits = bufferBits;
1052 wctx->bufferPos = bufferPos;
1053
1001 int sourcePos = 0;
1002 unsigned int bits_len = len * 8; // convert to number of bits
1003 int sourceGap = (8 - ((int)bits_len & 7)) & 7;
1004 int bufferRem = wctx->bufferBits & 7;
1005 int i;
1006 u32 b, carry;
1007 u8 *buffer = wctx->buffer;
1008 u8 *bitLength = wctx->bitLength;

--- 39 unchanged lines hidden (view full) ---

1048 }
1049 buffer[bufferPos] = b << (8 - bufferRem);
1050 bufferBits += (int)bits_len;
1051 }
1052
1053 wctx->bufferBits = bufferBits;
1054 wctx->bufferPos = bufferPos;
1055
1056 return 0;
1054}
1055
1057}
1058
1056static void wp512_final(struct crypto_tfm *tfm, u8 *out)
1059static int wp512_final(struct shash_desc *desc, u8 *out)
1057{
1060{
1058 struct wp512_ctx *wctx = crypto_tfm_ctx(tfm);
1061 struct wp512_ctx *wctx = shash_desc_ctx(desc);
1059 int i;
1060 u8 *buffer = wctx->buffer;
1061 u8 *bitLength = wctx->bitLength;
1062 int bufferBits = wctx->bufferBits;
1063 int bufferPos = wctx->bufferPos;
1064 __be64 *digest = (__be64 *)out;
1065
1066 buffer[bufferPos] |= 0x80U >> (bufferBits & 7);

--- 12 unchanged lines hidden (view full) ---

1079 bufferPos = WP512_BLOCK_SIZE - WP512_LENGTHBYTES;
1080 memcpy(&buffer[WP512_BLOCK_SIZE - WP512_LENGTHBYTES],
1081 bitLength, WP512_LENGTHBYTES);
1082 wp512_process_buffer(wctx);
1083 for (i = 0; i < WP512_DIGEST_SIZE/8; i++)
1084 digest[i] = cpu_to_be64(wctx->hash[i]);
1085 wctx->bufferBits = bufferBits;
1086 wctx->bufferPos = bufferPos;
1062 int i;
1063 u8 *buffer = wctx->buffer;
1064 u8 *bitLength = wctx->bitLength;
1065 int bufferBits = wctx->bufferBits;
1066 int bufferPos = wctx->bufferPos;
1067 __be64 *digest = (__be64 *)out;
1068
1069 buffer[bufferPos] |= 0x80U >> (bufferBits & 7);

--- 12 unchanged lines hidden (view full) ---

1082 bufferPos = WP512_BLOCK_SIZE - WP512_LENGTHBYTES;
1083 memcpy(&buffer[WP512_BLOCK_SIZE - WP512_LENGTHBYTES],
1084 bitLength, WP512_LENGTHBYTES);
1085 wp512_process_buffer(wctx);
1086 for (i = 0; i < WP512_DIGEST_SIZE/8; i++)
1087 digest[i] = cpu_to_be64(wctx->hash[i]);
1088 wctx->bufferBits = bufferBits;
1089 wctx->bufferPos = bufferPos;
1090
1091 return 0;
1087}
1088
1092}
1093
1089static void wp384_final(struct crypto_tfm *tfm, u8 *out)
1094static int wp384_final(struct shash_desc *desc, u8 *out)
1090{
1091 u8 D[64];
1092
1095{
1096 u8 D[64];
1097
1093 wp512_final(tfm, D);
1098 wp512_final(desc, D);
1094 memcpy (out, D, WP384_DIGEST_SIZE);
1095 memset (D, 0, WP512_DIGEST_SIZE);
1099 memcpy (out, D, WP384_DIGEST_SIZE);
1100 memset (D, 0, WP512_DIGEST_SIZE);
1101
1102 return 0;
1096}
1097
1103}
1104
1098static void wp256_final(struct crypto_tfm *tfm, u8 *out)
1105static int wp256_final(struct shash_desc *desc, u8 *out)
1099{
1100 u8 D[64];
1101
1106{
1107 u8 D[64];
1108
1102 wp512_final(tfm, D);
1109 wp512_final(desc, D);
1103 memcpy (out, D, WP256_DIGEST_SIZE);
1104 memset (D, 0, WP512_DIGEST_SIZE);
1110 memcpy (out, D, WP256_DIGEST_SIZE);
1111 memset (D, 0, WP512_DIGEST_SIZE);
1112
1113 return 0;
1105}
1106
1114}
1115
1107static struct crypto_alg wp512 = {
1108 .cra_name = "wp512",
1109 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
1110 .cra_blocksize = WP512_BLOCK_SIZE,
1111 .cra_ctxsize = sizeof(struct wp512_ctx),
1112 .cra_module = THIS_MODULE,
1113 .cra_list = LIST_HEAD_INIT(wp512.cra_list),
1114 .cra_u = { .digest = {
1115 .dia_digestsize = WP512_DIGEST_SIZE,
1116 .dia_init = wp512_init,
1117 .dia_update = wp512_update,
1118 .dia_final = wp512_final } }
1116static struct shash_alg wp512 = {
1117 .digestsize = WP512_DIGEST_SIZE,
1118 .init = wp512_init,
1119 .update = wp512_update,
1120 .final = wp512_final,
1121 .descsize = sizeof(struct wp512_ctx),
1122 .base = {
1123 .cra_name = "wp512",
1124 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
1125 .cra_blocksize = WP512_BLOCK_SIZE,
1126 .cra_module = THIS_MODULE,
1127 }
1119};
1120
1128};
1129
1121static struct crypto_alg wp384 = {
1122 .cra_name = "wp384",
1123 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
1124 .cra_blocksize = WP512_BLOCK_SIZE,
1125 .cra_ctxsize = sizeof(struct wp512_ctx),
1126 .cra_module = THIS_MODULE,
1127 .cra_list = LIST_HEAD_INIT(wp384.cra_list),
1128 .cra_u = { .digest = {
1129 .dia_digestsize = WP384_DIGEST_SIZE,
1130 .dia_init = wp512_init,
1131 .dia_update = wp512_update,
1132 .dia_final = wp384_final } }
1130static struct shash_alg wp384 = {
1131 .digestsize = WP384_DIGEST_SIZE,
1132 .init = wp512_init,
1133 .update = wp512_update,
1134 .final = wp384_final,
1135 .descsize = sizeof(struct wp512_ctx),
1136 .base = {
1137 .cra_name = "wp384",
1138 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
1139 .cra_blocksize = WP512_BLOCK_SIZE,
1140 .cra_module = THIS_MODULE,
1141 }
1133};
1134
1142};
1143
1135static struct crypto_alg wp256 = {
1136 .cra_name = "wp256",
1137 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
1138 .cra_blocksize = WP512_BLOCK_SIZE,
1139 .cra_ctxsize = sizeof(struct wp512_ctx),
1140 .cra_module = THIS_MODULE,
1141 .cra_list = LIST_HEAD_INIT(wp256.cra_list),
1142 .cra_u = { .digest = {
1143 .dia_digestsize = WP256_DIGEST_SIZE,
1144 .dia_init = wp512_init,
1145 .dia_update = wp512_update,
1146 .dia_final = wp256_final } }
1144static struct shash_alg wp256 = {
1145 .digestsize = WP256_DIGEST_SIZE,
1146 .init = wp512_init,
1147 .update = wp512_update,
1148 .final = wp256_final,
1149 .descsize = sizeof(struct wp512_ctx),
1150 .base = {
1151 .cra_name = "wp256",
1152 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
1153 .cra_blocksize = WP512_BLOCK_SIZE,
1154 .cra_module = THIS_MODULE,
1155 }
1147};
1148
1149static int __init wp512_mod_init(void)
1150{
1151 int ret = 0;
1152
1156};
1157
1158static int __init wp512_mod_init(void)
1159{
1160 int ret = 0;
1161
1153 ret = crypto_register_alg(&wp512);
1162 ret = crypto_register_shash(&wp512);
1154
1155 if (ret < 0)
1156 goto out;
1157
1163
1164 if (ret < 0)
1165 goto out;
1166
1158 ret = crypto_register_alg(&wp384);
1167 ret = crypto_register_shash(&wp384);
1159 if (ret < 0)
1160 {
1168 if (ret < 0)
1169 {
1161 crypto_unregister_alg(&wp512);
1170 crypto_unregister_shash(&wp512);
1162 goto out;
1163 }
1164
1171 goto out;
1172 }
1173
1165 ret = crypto_register_alg(&wp256);
1174 ret = crypto_register_shash(&wp256);
1166 if (ret < 0)
1167 {
1175 if (ret < 0)
1176 {
1168 crypto_unregister_alg(&wp512);
1169 crypto_unregister_alg(&wp384);
1177 crypto_unregister_shash(&wp512);
1178 crypto_unregister_shash(&wp384);
1170 }
1171out:
1172 return ret;
1173}
1174
1175static void __exit wp512_mod_fini(void)
1176{
1179 }
1180out:
1181 return ret;
1182}
1183
1184static void __exit wp512_mod_fini(void)
1185{
1177 crypto_unregister_alg(&wp512);
1178 crypto_unregister_alg(&wp384);
1179 crypto_unregister_alg(&wp256);
1186 crypto_unregister_shash(&wp512);
1187 crypto_unregister_shash(&wp384);
1188 crypto_unregister_shash(&wp256);
1180}
1181
1182MODULE_ALIAS("wp384");
1183MODULE_ALIAS("wp256");
1184
1185module_init(wp512_mod_init);
1186module_exit(wp512_mod_fini);
1187
1188MODULE_LICENSE("GPL");
1189MODULE_DESCRIPTION("Whirlpool Message Digest Algorithm");
1189}
1190
1191MODULE_ALIAS("wp384");
1192MODULE_ALIAS("wp256");
1193
1194module_init(wp512_mod_init);
1195module_exit(wp512_mod_fini);
1196
1197MODULE_LICENSE("GPL");
1198MODULE_DESCRIPTION("Whirlpool Message Digest Algorithm");