aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorErlang/OTP <[email protected]>2010-04-01 06:11:22 +0000
committerErlang/OTP <[email protected]>2010-04-01 06:11:22 +0000
commit39e0191d985078d6e9c12c504370425f1de5a6c5 (patch)
treeb59410869e50ab4fd99fb0f7253405516d7d6cf1 /lib
parent00ee51ffa60db202fa3cf845bd59e9e208251eb1 (diff)
parentb869aef371836879f0dd1c306a90acb5f93f3ad0 (diff)
downloadotp-39e0191d985078d6e9c12c504370425f1de5a6c5.tar.gz
otp-39e0191d985078d6e9c12c504370425f1de5a6c5.tar.bz2
otp-39e0191d985078d6e9c12c504370425f1de5a6c5.zip
Merge branch 'au/crypto' into dev
* au/crypto: Add missing docs for crypto:md4/1 Add des_ecb_encrypt/2 and des_ecb_decrypt/2 to crypto module OTP-8551 au/crypto des_ecb_encrypt/2 and des_ecb_decrypt/2 has been added to the crypto module. The crypto:md4/1 function has been documented.
Diffstat (limited to 'lib')
-rw-r--r--lib/crypto/c_src/crypto_drv.c30
-rw-r--r--lib/crypto/doc/src/crypto.xml82
-rw-r--r--lib/crypto/src/crypto.erl24
-rw-r--r--lib/crypto/test/crypto_SUITE.erl34
4 files changed, 151 insertions, 19 deletions
diff --git a/lib/crypto/c_src/crypto_drv.c b/lib/crypto/c_src/crypto_drv.c
index 5b6d750dde..8c1356c4cf 100644
--- a/lib/crypto/c_src/crypto_drv.c
+++ b/lib/crypto/c_src/crypto_drv.c
@@ -1,19 +1,19 @@
/*
* %CopyrightBegin%
- *
- * Copyright Ericsson AB 1999-2009. All Rights Reserved.
- *
+ *
+ * Copyright Ericsson AB 1999-2010. All Rights Reserved.
+ *
* The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved online at http://www.erlang.org/.
- *
+ *
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
- *
+ *
* %CopyrightEnd%
*/
@@ -239,12 +239,15 @@ static ErlDrvEntry crypto_driver_entry = {
#define DRV_BF_CBC_ENCRYPT 64
#define DRV_BF_CBC_DECRYPT 65
+#define DRV_ECB_DES_ENCRYPT 66
+#define DRV_ECB_DES_DECRYPT 67
+
/* #define DRV_CBC_IDEA_ENCRYPT 34 */
/* #define DRV_CBC_IDEA_DECRYPT 35 */
/* Not DRV_DH_GENERATE_PARAMS DRV_DH_CHECK
* Calc RSA_VERIFY_* and RSA_SIGN once */
-#define NUM_CRYPTO_FUNCS 46
+#define NUM_CRYPTO_FUNCS 48
#define MD5_CTX_LEN (sizeof(MD5_CTX))
#define MD5_LEN 16
@@ -538,6 +541,21 @@ static int crypto_control(ErlDrvData drv_data, unsigned int command, char *buf,
(command == DRV_CBC_DES_ENCRYPT));
return dlen;
+ case DRV_ECB_DES_ENCRYPT:
+ case DRV_ECB_DES_DECRYPT:
+ /* buf = key[8] data */
+ dlen = len - 8;
+ if (dlen != 8)
+ return -1;
+ des_key = (const_DES_cblock*) buf;
+ des_dbuf = (unsigned char *) (buf + 8);
+ bin = return_binary(rbuf,rlen,dlen);
+ if (bin==NULL) return -1;
+ DES_set_key(des_key, &schedule);
+ DES_ecb_encrypt((const_DES_cblock*) des_dbuf, (DES_cblock*) bin, &schedule,
+ (command == DRV_ECB_DES_ENCRYPT));
+ return dlen;
+
case DRV_BF_ECB_ENCRYPT:
case DRV_BF_ECB_DECRYPT:
{
diff --git a/lib/crypto/doc/src/crypto.xml b/lib/crypto/doc/src/crypto.xml
index cfc6996332..256eab3e3c 100644
--- a/lib/crypto/doc/src/crypto.xml
+++ b/lib/crypto/doc/src/crypto.xml
@@ -4,7 +4,7 @@
<erlref>
<header>
<copyright>
- <year>1999</year><year>2009</year>
+ <year>1999</year><year>2010</year>
<holder>Ericsson AB. All Rights Reserved.</holder>
</copyright>
<legalnotice>
@@ -13,12 +13,12 @@
compliance with the License. You should have received a copy of the
Erlang Public License along with this software. If not, it can be
retrieved online at http://www.erlang.org/.
-
+
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
the License for the specific language governing rights and limitations
under the License.
-
+
</legalnotice>
<title>crypto</title>
@@ -35,6 +35,9 @@
<p>References:</p>
<list type="bulleted">
<item>
+ <p>md4: The MD4 Message Digest Algorithm (RFC 1320)</p>
+ </item>
+ <item>
<p>md5: The MD5 Message Digest Algorithm (RFC 1321)</p>
</item>
<item>
@@ -115,6 +118,52 @@ Mpint() = <![CDATA[<<ByteLen:32/integer-big, Bytes:ByteLen/binary>>]]>
</desc>
</func>
<func>
+ <name>md4(Data) -> Digest</name>
+ <fsummary>Compute an <c>MD4</c>message digest from <c>Data</c></fsummary>
+ <type>
+ <v>Data = iolist() | binary()</v>
+ <v>Digest = binary()</v>
+ </type>
+ <desc>
+ <p>Computes an <c>MD4</c> message digest from <c>Data</c>, where
+ the length of the digest is 128 bits (16 bytes).</p>
+ </desc>
+ </func>
+ <func>
+ <name>md4_init() -> Context</name>
+ <fsummary>Creates an MD4 context</fsummary>
+ <type>
+ <v>Context = binary()</v>
+ </type>
+ <desc>
+ <p>Creates an MD4 context, to be used in subsequent calls to
+ <c>md4_update/2</c>.</p>
+ </desc>
+ </func>
+ <func>
+ <name>md4_update(Context, Data) -> NewContext</name>
+ <fsummary>Update an MD4 <c>Context</c>with <c>Data</c>, and return a <c>NewContext</c></fsummary>
+ <type>
+ <v>Data = iolist() | binary()</v>
+ <v>Context = NewContext = binary()</v>
+ </type>
+ <desc>
+ <p>Updates an MD4 <c>Context</c> with <c>Data</c>, and returns
+ a <c>NewContext</c>.</p>
+ </desc>
+ </func>
+ <func>
+ <name>md4_final(Context) -> Digest</name>
+ <fsummary>Finish the update of an MD4 <c>Context</c>and return the computed <c>MD4</c>message digest</fsummary>
+ <type>
+ <v>Context = Digest = binary()</v>
+ </type>
+ <desc>
+ <p>Finishes the update of an MD4 <c>Context</c> and returns
+ the computed <c>MD4</c> message digest.</p>
+ </desc>
+ </func>
+ <func>
<name>md5(Data) -> Digest</name>
<fsummary>Compute an <c>MD5</c>message digest from <c>Data</c></fsummary>
<type>
@@ -339,6 +388,33 @@ Mpint() = <![CDATA[<<ByteLen:32/integer-big, Bytes:ByteLen/binary>>]]>
</func>
<func>
+ <name>des_ecb_encrypt(Key, Text) -> Cipher</name>
+ <fsummary>Encrypt <c>Text</c>according to DES in ECB mode</fsummary>
+ <type>
+ <v>Key = Text = iolist() | binary()</v>
+ <v>Cipher = binary()</v>
+ </type>
+ <desc>
+ <p>Encrypts <c>Text</c> according to DES in ECB mode.
+ <c>Key</c> is the DES key. The lengths of <c>Key</c> and
+ <c>Text</c> must be 64 bits (8 bytes).</p>
+ </desc>
+ </func>
+ <func>
+ <name>des_ecb_decrypt(Key, Cipher) -> Text</name>
+ <fsummary>Decrypt <c>Cipher</c>according to DES in ECB mode</fsummary>
+ <type>
+ <v>Key = Cipher = iolist() | binary()</v>
+ <v>Text = binary()</v>
+ </type>
+ <desc>
+ <p>Decrypts <c>Cipher</c> according to DES in ECB mode.
+ <c>Key</c> is the DES key. The lengths of <c>Key</c> and
+ <c>Cipher</c> must be 64 bits (8 bytes).</p>
+ </desc>
+ </func>
+
+ <func>
<name>blowfish_ecb_encrypt(Key, Text) -> Cipher</name>
<fsummary>Encrypt the first 64 bits of <c>Text</c> using Blowfish in ECB mode</fsummary>
<type>
diff --git a/lib/crypto/src/crypto.erl b/lib/crypto/src/crypto.erl
index fa33bad2e0..8cb750759f 100644
--- a/lib/crypto/src/crypto.erl
+++ b/lib/crypto/src/crypto.erl
@@ -1,19 +1,19 @@
%%
%% %CopyrightBegin%
-%%
-%% Copyright Ericsson AB 1999-2009. All Rights Reserved.
-%%
+%%
+%% Copyright Ericsson AB 1999-2010. All Rights Reserved.
+%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
-%%
+%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
-%%
+%%
%% %CopyrightEnd%
%%
@@ -29,6 +29,7 @@
%-export([sha512/1, sha512_init/0, sha512_update/2, sha512_final/1]).
-export([md5_mac/2, md5_mac_96/2, sha_mac/2, sha_mac_96/2]).
-export([des_cbc_encrypt/3, des_cbc_decrypt/3, des_cbc_ivec/1]).
+-export([des_ecb_encrypt/2, des_ecb_decrypt/2]).
-export([des3_cbc_encrypt/5, des3_cbc_decrypt/5]).
-export([blowfish_ecb_encrypt/2, blowfish_ecb_decrypt/2]).
-export([blowfish_cbc_encrypt/3, blowfish_cbc_decrypt/3]).
@@ -124,6 +125,9 @@
-define(BF_CBC_ENCRYPT, 64).
-define(BF_CBC_DECRYPT, 65).
+-define(DES_ECB_ENCRYPT, 66).
+-define(DES_ECB_DECRYPT, 67).
+
%% -define(IDEA_CBC_ENCRYPT, 34).
%% -define(IDEA_CBC_DECRYPT, 35).
@@ -135,6 +139,7 @@
md5_mac, md5_mac_96,
sha_mac, sha_mac_96,
des_cbc_encrypt, des_cbc_decrypt,
+ des_ecb_encrypt, des_ecb_decrypt,
des_ede3_cbc_encrypt, des_ede3_cbc_decrypt,
aes_cfb_128_encrypt, aes_cfb_128_decrypt,
rand_bytes,
@@ -295,6 +300,15 @@ des_cbc_ivec(Data) when is_list(Data) ->
des_cbc_ivec(list_to_binary(Data)).
%%
+%% DES - in electronic codebook mode (ECB)
+%%
+des_ecb_encrypt(Key, Data) ->
+ control(?DES_ECB_ENCRYPT, [Key, Data]).
+
+des_ecb_decrypt(Key, Data) ->
+ control(?DES_ECB_DECRYPT, [Key, Data]).
+
+%%
%% DES3 - in cipher block chaining mode (CBC)
%%
des3_cbc_encrypt(Key1, Key2, Key3, IVec, Data) ->
diff --git a/lib/crypto/test/crypto_SUITE.erl b/lib/crypto/test/crypto_SUITE.erl
index 290ef19160..8467ff2274 100644
--- a/lib/crypto/test/crypto_SUITE.erl
+++ b/lib/crypto/test/crypto_SUITE.erl
@@ -1,19 +1,19 @@
%%
%% %CopyrightBegin%
-%%
-%% Copyright Ericsson AB 1999-2009. All Rights Reserved.
-%%
+%%
+%% Copyright Ericsson AB 1999-2010. All Rights Reserved.
+%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
-%%
+%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
-%%
+%%
%% %CopyrightEnd%
%%
-module(crypto_SUITE).
@@ -40,6 +40,7 @@
md5_mac_io/1,
des_cbc/1,
des_cbc_iter/1,
+ des_ecb/1,
aes_cfb/1,
aes_cbc/1,
aes_cbc_iter/1,
@@ -78,6 +79,7 @@ all(suite) ->
aes_cbc,
aes_cbc_iter,
des_cbc_iter,
+ des_ecb,
rand_uniform_test,
rsa_verify_test,
dsa_verify_test,
@@ -445,6 +447,28 @@ des_cbc_iter(Config) when is_list(Config) ->
%%
%%
+des_ecb(doc) ->
+ "Encrypt and decrypt according to ECB DES and check the result. "
+ "Example are from FIPS-81.";
+des_ecb(suite) ->
+ [];
+des_ecb(Config) when is_list(Config) ->
+ ?line Key = hexstr2bin("0123456789abcdef"),
+ ?line Cipher1 = crypto:des_ecb_encrypt(Key, "Now is t"),
+ ?line m(Cipher1, hexstr2bin("3fa40e8a984d4815")),
+ ?line Cipher2 = crypto:des_ecb_encrypt(Key, "he time "),
+ ?line m(Cipher2, hexstr2bin("6a271787ab8883f9")),
+ ?line Cipher3 = crypto:des_ecb_encrypt(Key, "for all "),
+ ?line m(Cipher3, hexstr2bin("893d51ec4b563b53")),
+ ?line Cipher4 = crypto:des_ecb_decrypt(Key, hexstr2bin("3fa40e8a984d4815")),
+ ?line m(Cipher4, <<"Now is t">>),
+ ?line Cipher5 = crypto:des_ecb_decrypt(Key, hexstr2bin("6a271787ab8883f9")),
+ ?line m(Cipher5, <<"he time ">>),
+ ?line Cipher6 = crypto:des_ecb_decrypt(Key, hexstr2bin("893d51ec4b563b53")),
+ ?line m(Cipher6, <<"for all ">>).
+
+%%
+%%
aes_cfb(doc) ->
"Encrypt and decrypt according to AES CFB 128 bit and check "
"the result. Example are from NIST SP 800-38A.";