Author: Amaury Forgeot d'Arc <amaur...@gmail.com>
Branch: py3.3
Changeset: r82016:9a47d88b9973
Date: 2016-01-31 22:29 +0100
http://bitbucket.org/pypy/pypy/changeset/9a47d88b9973/

Log:    Add _ssl.RAND_bytes

diff --git a/pypy/module/_ssl/__init__.py b/pypy/module/_ssl/__init__.py
--- a/pypy/module/_ssl/__init__.py
+++ b/pypy/module/_ssl/__init__.py
@@ -43,8 +43,8 @@
 
         if HAVE_OPENSSL_RAND:
             Module.interpleveldefs['RAND_add'] = "interp_ssl.RAND_add"
-            Module.interpleveldefs['RAND_bytes'] = "space.w_None"  # so far
-            Module.interpleveldefs['RAND_pseudo_bytes'] = "space.w_None"  # so 
far
+            Module.interpleveldefs['RAND_bytes'] = "interp_ssl.RAND_bytes"
+            Module.interpleveldefs['RAND_pseudo_bytes'] = 
"interp_ssl.RAND_pseudo_bytes"
             Module.interpleveldefs['RAND_status'] = "interp_ssl.RAND_status"
             Module.interpleveldefs['RAND_egd'] = "interp_ssl.RAND_egd"
 
diff --git a/pypy/module/_ssl/interp_ssl.py b/pypy/module/_ssl/interp_ssl.py
--- a/pypy/module/_ssl/interp_ssl.py
+++ b/pypy/module/_ssl/interp_ssl.py
@@ -237,6 +237,43 @@
         with rffi.scoped_str2charp(string) as buf:
             libssl_RAND_add(buf, len(string), entropy)
 
+    def _RAND_bytes(space, n, pseudo):
+        if n < 0:
+            raise OperationError(space.w_ValueError, space.wrap(
+                "num must be positive"))
+
+        with rffi.scoped_alloc_buffer(n) as buf:
+            if pseudo:
+                ok = libssl_RAND_pseudo_bytes(
+                    rffi.cast(rffi.UCHARP, buf.raw), n)
+                if ok == 0 or ok == 1:
+                    return space.newtuple([
+                        space.wrapbytes(buf.str(n)),
+                        space.wrap(ok == 1),
+                    ])
+            else:
+                ok = libssl_RAND_bytes(
+                    rffi.cast(rffi.UCHARP, buf.raw), n)
+                if ok == 1:
+                    return space.wrapbytes(buf.str(n))
+
+        raise ssl_error(space, "", errcode=libssl_ERR_get_error())
+
+    @unwrap_spec(n=int)
+    def RAND_bytes(space, n):
+        """RAND_bytes(n) -> bytes
+
+        Generate n cryptographically strong pseudo-random bytes."""
+        return _RAND_bytes(space, n, pseudo=False)
+
+    @unwrap_spec(n=int)
+    def RAND_pseudo_bytes(space, n):
+        """RAND_pseudo_bytes(n) -> (bytes, is_cryptographic)
+
+        Generate n pseudo-random bytes. is_cryptographic is True if the bytes
+        generated are cryptographically strong."""
+        return _RAND_bytes(space, n, pseudo=True)
+
     def RAND_status(space):
         """RAND_status() -> 0 or 1
 
diff --git a/pypy/module/_ssl/test/test_ssl.py 
b/pypy/module/_ssl/test/test_ssl.py
--- a/pypy/module/_ssl/test/test_ssl.py
+++ b/pypy/module/_ssl/test/test_ssl.py
@@ -48,6 +48,16 @@
         raises(TypeError, _ssl.RAND_add, "xyz", "zyx")
         _ssl.RAND_add("xyz", 1.2345)
 
+    def test_RAND_bytes(self):
+        import _ssl
+        b = _ssl.RAND_bytes(3)
+        assert type(b) is bytes
+        assert len(b) == 3
+        b, ok = _ssl.RAND_pseudo_bytes(3)
+        assert type(b) is bytes
+        assert len(b) == 3
+        assert ok is True or ok is False
+
     def test_RAND_status(self):
         import _ssl
         if not hasattr(_ssl, "RAND_status"):
diff --git a/rpython/rlib/ropenssl.py b/rpython/rlib/ropenssl.py
--- a/rpython/rlib/ropenssl.py
+++ b/rpython/rlib/ropenssl.py
@@ -311,6 +311,8 @@
 
 if HAVE_OPENSSL_RAND:
     ssl_external('RAND_add', [rffi.CCHARP, rffi.INT, rffi.DOUBLE], lltype.Void)
+    ssl_external('RAND_bytes', [rffi.UCHARP, rffi.INT], rffi.INT)
+    ssl_external('RAND_pseudo_bytes', [rffi.UCHARP, rffi.INT], rffi.INT)
     ssl_external('RAND_status', [], rffi.INT)
     if HAVE_OPENSSL_RAND_EGD:
         ssl_external('RAND_egd', [rffi.CCHARP], rffi.INT)
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to